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] = (uch55,153,153);line-height:18px;list-style-position:outside !important;">   
  153.             color_tab = cvCreateMat( 1, comp_count, CV_8UC3 );  
  154.             for( i = 0; i < comp_count; i++ )  
  155.             {  
  156.                 uchar* ptr = color_tab->data.ptr + i*3;  
  157.                 ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);  
  158.                 ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);  
  159.                 ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);  
  160.             }  
  161.   
  162.             {  
  163.             double t = (double)cvGetTickCount();  
  164.             cvWatershed( img0, markers );  
  165.             t = (double)cvGetTickCount() - t;  
  166.             printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );  
  167.             }  
  168.   
  169.             // paint the watershed image  
  170.             for( i = 0; i < markers->height; i++ )  
  171.             {  
  172.                 uchar* ptr = color_tab->data.ptr + i*3;  
  173.                 ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);  
  174.                 ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);  
  175.                 ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);  
  176.             }  
  177.   
  178.             {  
  179.             double t = (double)cvGetTickCount();  
  180.             cvWatershed( img0, markers );  
  181.             t = (double)cvGetTickCount() - t;  
  182.             printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );  
  183.             }  
  184.   
  185.             // paint the watershed image  
  186.             for( i = 0; i < markers->height; i++ )  
  187.                              {  
  188.                 uchar* ptr = color_tab->data.ptr + i*3;  
相關文章
相關標籤/搜索