cvWatershed例子:


[cpp]  view plain copy



    1. #include<cv.h>  
    2. #include<highgui.h>  
    3. #include<iostream>  
    4.   
    5. using namespace  std;  
    6.   
    7. IplImage* marker_mask = 0;  
    8. IplImage* markers = 0;  
    9. IplImage* img0 = 0, *img = 0, *img_gray = 0, *wshed = 0;  
    10. CvPoint prev_pt = {-1,-1};  
    11. void on_mouse( int event, int x, int y, int flags, void* param )//opencv 会自动给函数传入合适的值  
    12. {  
    13. if( !img )  
    14. return;  
    15. if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )  
    16.         prev_pt = cvPoint(-1,-1);  
    17. else if( event == CV_EVENT_LBUTTONDOWN )  
    18.         prev_pt = cvPoint(x,y);  
    19. else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )  
    20.     {  
    21.         CvPoint pt = cvPoint(x,y);  
    22. if( prev_pt.x < 0 )  
    23.             prev_pt = pt;  
    24. //CvScalar 成员:double val[4] RGBA值A=alpha  
    25.         cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );  
    26.         prev_pt = pt;  
    27. "image", img);  
    28.     }  
    29. }  
    30.   
    31. int main( int argc, char** argv )  
    32. {  
    33. char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";  
    34.     CvMemStorage* storage = cvCreateMemStorage(0);  
    35.     CvRNG rng = cvRNG(-1);  
    36. if( (img0 = cvLoadImage(filename,1)) == 0 )  
    37. return 0;  
    38. "Hot keys: \n"  
    39. "\tESC - quit the program\n"  
    40. "\tr - restore the original image\n"  
    41. "\tw or SPACE - run watershed algorithm\n"  
    42. "\t\t(before running it, roughly mark the areas on the image)\n"  
    43. "\t  (before that, roughly outline several markers on the image)\n" );  
    44. "image", 1 );  
    45. "watershed transform", 1 );  
    46.     img = cvCloneImage( img0 );  
    47.     img_gray = cvCloneImage( img0 );  
    48.     wshed = cvCloneImage( img0 );  
    49.     marker_mask = cvCreateImage( cvGetSize(img), 8, 1 );  
    50.     markers = cvCreateImage( cvGetSize(img), IPL_DEPTH_32S, 1 );  
    51.     cvCvtColor( img, marker_mask, CV_BGR2GRAY );  
    52. //这两句只用将RGB转成3通道的灰度图即R=G=B,用来显示用  
    53.     cvZero( marker_mask );  
    54.     cvZero( wshed );  
    55. "image", img );  
    56. "watershed transform", wshed );  
    57. "image", on_mouse, 0 );  
    58. for(;;)  
    59.     {  
    60. int c = cvWaitKey(0);  
    61. if( (char)c == 27 )  
    62. break;  
    63. if( (char)c == 'r' )  
    64.         {  
    65.             cvZero( marker_mask );  
    66. //cvCopy()也可以这样用,不影响原img0图像,也随时更新  
    67. "image", img );  
    68.         }  
    69. if( (char)c == 'w' || (char)c == ' ' )  
    70.         {  
    71.             CvSeq* contours = 0;  
    72.             CvMat* color_tab = 0;  
    73. int i, j, comp_count = 0;  
    74.   
    75. //下面选将标记的图像取得其轮廓, 将每种轮廓用不同的整数表示  
    76. //不同的整数使用分水岭算法时,就成为不同的种子点  
    77. //算法本来就是以各个不同的种子点为中心扩张  
    78.             cvClearMemStorage(storage);  
    79. sizeof(CvContour),  
    80.                 CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );  
    81.             cvZero( markers );  
    82. for( ; contours != 0; contours = contours->h_next, comp_count++ )  
    83.             {  
    84.                 cvDrawContours(markers, contours, cvScalarAll(comp_count+1),  
    85.                     cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) );  
    86.             }  
    87. //cvShowImage("image",markers);  
    88. if( comp_count == 0 )  
    89. continue;  
    90. //创建随机颜色列表  
    91. for( i = 0; i < comp_count; i++ ) //不同的整数标记  
    92.             {  
    93.                 uchar* ptr = color_tab->data.ptr + i*3;  
    94.                 ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);  
    95.                 ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);  
    96.                 ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);  
    97.             }  
    98.             {  
    99. double t = (double)cvGetTickCount();  
    100.                 cvWatershed( img0, markers );  
    101. "img0.xml",markers);  
    102. double)cvGetTickCount() - t;  
    103. "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );  
    104.             }  
    105. // paint the watershed image  
    106. for( i = 0; i < markers->height; i++ )  
    107. for( j = 0; j < markers->width; j++ )  
    108.                 {  
    109. int idx = CV_IMAGE_ELEM( markers, int, i, j );//markers的数据类型为IPL_DEPTH_32S  
    110. //BGR三个通道的数是一起的,故要j*3  
    111. if( idx == -1 ) //输出时若为-1,表示各个部分的边界  
    112.                         dst[0] = dst[1] = dst[2] = (uchar)255;  
    113. else if( idx <= 0 || idx > comp_count )  //异常情况  
    114. // should not get here  
    115. else //正常情况  
    116.                     {  
    117.                         uchar* ptr = color_tab->data.ptr + (idx-1)*3;  
    118.                         dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];  
    119.                     }  
    120.                 }  
    121. //wshed.x.y=0.5*wshed.x.y+0.5*img_gray+0加权融合图像  
    122. "watershed transform", wshed );  
    123.                 cvReleaseMat( &color_tab );  
    124.         }  
    125.     }  
    126. return 1;  
    127. }


    运行情况:




    同上面分析可看出,因为不相连的标记后,程序在masker中的数值不同

    经过分水岭算法后,

    不同的标记肯定会在不同的区域中,

    例如头发部分,我画了一条线标记 ,, 处理后就把头发部分分割了出来 

    还比如胳膊那一块,正好也分割出来了

    我对算法的感性认识:


    opencv中的算法是先把输入图像转化成梯度图(标量)


    如果把梯度图看成是一个地形的话,就会发现,梯度高的地方就成了山脉,梯度低的地方就是山谷


    我们经过标记为不同的区域后,


    就从各个标记的地方注水进去,注入的水越来越多的时候,就会出现把流过低些的山脉,从而流到别的山谷中,那么他们就连一了一片区域。


    区域分割的要求是把不同的标记分割成不同的地方。所以如果一直注水,可能就会覆盖别的区域了。这时算法就采取某种方法,修大坝使标记的不同区域不会因为注水而相连


    他们会互不相干的扩张领地,直到把整个领地都扩张完为止。




    函数输出时,自己的标记扩张出来的区域都用之前标记的值表示,代表一个区域。 不同的值代表不同的区域