OpenCV 实现分水岭算法

 转自:

OpenCV 实现分水岭算法

种子点的标记没有太搞懂,这个算法的速度还是很快的

 

OpenCV 实现分水岭算法

 

[cpp] view plain copy
 print?
  1. // watershed_test20140801.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6. //  
  7. // ch9_watershed image  
  8. //   This is an exact copy of the watershed.cpp demo in the OpenCV ../samples/c directory  
  9. //  
  10. // Think about using a morphologically eroded forground and background segmented image as the template  
  11. // for the watershed algorithm to segment objects by color and edges for collecting   
  12. //  
  13. /* *************** License:************************** 
  14.    Oct. 3, 2008 
  15.    Right to use this code in any way you want without warrenty, support or any guarentee of it working. 
  16.  
  17.    BOOK: It would be nice if you cited it: 
  18.    Learning OpenCV: Computer Vision with the OpenCV Library 
  19.      by Gary Bradski and Adrian Kaehler 
  20.      Published by O'Reilly Media, October 3, 2008 
  21.   
  22.    AVAILABLE AT:  
  23.      http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134 
  24.      Or: http://oreilly.com/catalog/9780596516130/ 
  25.      ISBN-10: 0596516134 or: ISBN-13: 978-0596516130     
  26.  
  27.    OTHER OPENCV SITES: 
  28.    * The source code is on sourceforge at: 
  29.      http://sourceforge.net/projects/opencvlibrary/ 
  30.    * The OpenCV wiki page (As of Oct 1, 2008 this is down for changing over servers, but should come back): 
  31.      http://opencvlibrary.sourceforge.net/ 
  32.    * An active user group is at: 
  33.      http://tech.groups.yahoo.com/group/OpenCV/ 
  34.    * The minutes of weekly OpenCV development meetings are at: 
  35.      http://pr.willowgarage.com/wiki/OpenCV 
  36.    ************************************************** */  
  37.   
  38. #include "cv.h"  
  39. #include "highgui.h"  
  40. #include <stdio.h>  
  41. #include <stdlib.h>  
  42. #include <iostream>  
  43. using namespace std;  
  44. using namespace cv;  
  45.   
  46.   
  47. #pragma comment(lib,"opencv_core2410d.lib")        
  48. #pragma comment(lib,"opencv_highgui2410d.lib")        
  49. #pragma comment(lib,"opencv_imgproc2410d.lib")    
  50.   
  51. IplImage* marker_mask = 0;  
  52. IplImage* markers = 0;  
  53. IplImage* img0 = 0, *img = 0, *img_gray = 0, *wshed = 0;  
  54. CvPoint prev_pt = {-1,-1};  
  55.   
  56. void on_mouse( int event, int x, int y, int flags, void* param )  
  57. {  
  58.     if( !img )  
  59.         return;  
  60.   
  61.     if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )  
  62.         prev_pt = cvPoint(-1,-1);  
  63.     else if( event == CV_EVENT_LBUTTONDOWN )  
  64.         prev_pt = cvPoint(x,y);  
  65.     else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )  
  66.     {  
  67.         CvPoint pt = cvPoint(x,y);  
  68.         if( prev_pt.x < 0 )  
  69.             prev_pt = pt;  
  70.         cvLine( marker_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );  
  71.         cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );  
  72.         prev_pt = pt;  
  73.         cvShowImage( "image", img );  
  74.     }  
  75. }  
  76.   
  77.   
  78. int main( int argc, char** argv )  
  79. {  
  80.     cout<<"input image name:  "<<endl;   
  81.     string file;  
  82.     cin>>file;  
  83.   
  84.   
  85.     char* filename = (char *)file.c_str();  
  86.   
  87.     CvRNG rng = cvRNG(-1);  
  88.   
  89.     if( (img0 = cvLoadImage(filename,1)) == 0 )  
  90.         return 0;  
  91.   
  92.     printf( "Hot keys: \n"  
  93.             "\tESC - quit the program\n"  
  94.             "\tr - restore the original image\n"  
  95.             "\tw or ENTER - run watershed algorithm\n"  
  96.             "\t\t(before running it, roughly mark the areas on the image)\n"  
  97.             "\t  (before that, roughly outline several markers on the image)\n" );  
  98.       
  99.     cvNamedWindow( "image", 1 );  
  100.     cvNamedWindow( "watershed transform", 1 );  
  101.   
  102.     img = cvCloneImage( img0 );  
  103.     img_gray = cvCloneImage( img0 );  
  104.     wshed = cvCloneImage( img0 );  
  105.     marker_mask = cvCreateImage( cvGetSize(img), 8, 1 );  
  106.     markers = cvCreateImage( cvGetSize(img), IPL_DEPTH_32S, 1 );  
  107.     cvCvtColor( img, marker_mask, CV_BGR2GRAY );  
  108.     cvCvtColor( marker_mask, img_gray, CV_GRAY2BGR );  
  109.   
  110.     cvZero( marker_mask );  
  111.     cvZero( wshed );  
  112.     cvShowImage( "image", img );  
  113.     cvShowImage( "watershed transform", wshed );  
  114.     cvSetMouseCallback( "image", on_mouse, 0 );  
  115.   
  116.     for(;;)  
  117.     {  
  118.         int c = cvWaitKey(0);  
  119.   
  120.         if( (char)c == 27 )  
  121.             break;  
  122.   
  123.         if( (char)c == 'r' )  
  124.         {  
  125.             cvZero( marker_mask );  
  126.             cvCopy( img0, img );  
  127.             cvShowImage( "image", img );  
  128.         }  
  129.   
  130.         if( (char)c == 'w' || (char)c == '\n' )  
  131.         {  
  132.             CvMemStorage* storage = cvCreateMemStorage(0);  
  133.             CvSeq* contours = 0;  
  134.             CvMat* color_tab;  
  135.             int i, j, comp_count = 0;  
  136.             //cvSaveImage( "wshed_mask.png", marker_mask );  
  137.             //marker_mask = cvLoadImage( "wshed_mask.png", 0 );  
  138.             cvFindContours( marker_mask, storage, &contours, sizeof(CvContour),  
  139.                             CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );  
  140.             cvZero( markers );  
  141.             for( ; contours != 0; contours = contours->h_next, comp_count++ )  
  142.             {  
  143.                 cvDrawContours( markers, contours, cvScalarAll(comp_count+1),  
  144.                                 cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) );  
  145.             }  
  146.   
  147.             color_tab = cvCreateMat( 1, comp_count, CV_8UC3 );  
  148.             for( i = 0; i < comp_count; i++ )  
  149.             {  
  150.                 uchar* ptr = color_tab->data.ptr + i*3;  
  151.                 ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);  
  152.                 ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);  
  153.                 ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);  
  154.             }  
  155.   
  156.             {  
  157.             double t = (double)cvGetTickCount();  
  158.             cvWatershed( img0, markers );  
  159.             t = (double)cvGetTickCount() - t;  
  160.             printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );  
  161.             }  
  162.   
  163.             // paint the watershed image  
  164.             for( i = 0; i < markers->height; i++ )  
  165.                 for( j = 0; j < markers->width; j++ )  
  166.                 {  
  167.                     int idx = CV_IMAGE_ELEM( markers, int, i, j );  
  168.                     uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );  
  169.                     if( idx == -1 )  
  170.                         dst[0] = dst[1] = dst[2] = (uchar)255;  
  171.                     else if( idx <= 0 || idx > comp_count )  
  172.                         dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here  
  173.                     else  
  174.                     {  
  175.                         uchar* ptr = color_tab->data.ptr + (idx-1)*3;  
  176.                         dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];  
  177.                     }  
  178.                 }  
  179.   
  180.             cvAddWeighted( wshed, 0.5, img_gray, 0.5, 0, wshed );  
  181.             cvShowImage( "watershed transform", wshed );  
  182.             cvReleaseMemStorage( &storage );  
  183.             cvReleaseMat( &color_tab );  
  184.         }  
  185.     }  
  186.   
  187.     return 1;  
  188. }  


 

 

 

实现效果:

 

OpenCV 实现分水岭算法