watershed.cpp

00001 #ifdef _CH_
00002 #pragma package <opencv>
00003 #endif
00004 
00005 #ifndef _EiC
00006 #include "cv.h"
00007 #include "highgui.h"
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #endif
00011 
00012 IplImage* marker_mask = 0;
00013 IplImage* markers = 0;
00014 IplImage* img0 = 0, *img = 0, *img_gray = 0, *wshed = 0;
00015 CvPoint prev_pt = {-1,-1};
00016 
00017 void on_mouse( int event, int x, int y, int flags, void* param )
00018 {
00019     if( !img )
00020         return;
00021 
00022     if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
00023         prev_pt = cvPoint(-1,-1);
00024     else if( event == CV_EVENT_LBUTTONDOWN )
00025         prev_pt = cvPoint(x,y);
00026     else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )
00027     {
00028         CvPoint pt = cvPoint(x,y);
00029         if( prev_pt.x < 0 )
00030             prev_pt = pt;
00031         cvLine( marker_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
00032         cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
00033         prev_pt = pt;
00034         cvShowImage( "image", img );
00035     }
00036 }
00037 
00038 
00039 int main( int argc, char** argv )
00040 {
00041     char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
00042     CvRNG rng = cvRNG(-1);
00043 
00044     if( (img0 = cvLoadImage(filename,1)) == 0 )
00045         return 0;
00046 
00047     printf( "Hot keys: \n"
00048             "\tESC - quit the program\n"
00049             "\tr - restore the original image\n"
00050             "\tw or ENTER - run watershed algorithm\n"
00051             "\t\t(before running it, roughly mark the areas on the image)\n"
00052             "\t  (before that, roughly outline several markers on the image)\n" );
00053     
00054     cvNamedWindow( "image", 1 );
00055     cvNamedWindow( "watershed transform", 1 );
00056 
00057     img = cvCloneImage( img0 );
00058     img_gray = cvCloneImage( img0 );
00059     wshed = cvCloneImage( img0 );
00060     marker_mask = cvCreateImage( cvGetSize(img), 8, 1 );
00061     markers = cvCreateImage( cvGetSize(img), IPL_DEPTH_32S, 1 );
00062     cvCvtColor( img, marker_mask, CV_BGR2GRAY );
00063     cvCvtColor( marker_mask, img_gray, CV_GRAY2BGR );
00064 
00065     cvZero( marker_mask );
00066     cvZero( wshed );
00067     cvShowImage( "image", img );
00068     cvShowImage( "watershed transform", wshed );
00069     cvSetMouseCallback( "image", on_mouse, 0 );
00070 
00071     for(;;)
00072     {
00073         int c = cvWaitKey(0);
00074 
00075         if( (char)c == 27 )
00076             break;
00077 
00078         if( (char)c == 'r' )
00079         {
00080             cvZero( marker_mask );
00081             cvCopy( img0, img );
00082             cvShowImage( "image", img );
00083         }
00084 
00085         if( (char)c == 'w' || (char)c == '\n' )
00086         {
00087             CvMemStorage* storage = cvCreateMemStorage(0);
00088             CvSeq* contours = 0;
00089             CvMat* color_tab;
00090             int i, j, comp_count = 0;
00091             //cvSaveImage( "wshed_mask.png", marker_mask );
00092             //marker_mask = cvLoadImage( "wshed_mask.png", 0 );
00093             cvFindContours( marker_mask, storage, &contours, sizeof(CvContour),
00094                             CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
00095             cvZero( markers );
00096             for( ; contours != 0; contours = contours->h_next, comp_count++ )
00097             {
00098                 cvDrawContours( markers, contours, cvScalarAll(comp_count+1),
00099                                 cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) );
00100             }
00101 
00102             color_tab = cvCreateMat( 1, comp_count, CV_8UC3 );
00103             for( i = 0; i < comp_count; i++ )
00104             {
00105                 uchar* ptr = color_tab->data.ptr + i*3;
00106                 ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);
00107                 ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);
00108                 ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);
00109             }
00110 
00111             {
00112             double t = (double)cvGetTickCount();
00113             cvWatershed( img0, markers );
00114             t = (double)cvGetTickCount() - t;
00115             printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );
00116             }
00117 
00118             // paint the watershed image
00119             for( i = 0; i < markers->height; i++ )
00120                 for( j = 0; j < markers->width; j++ )
00121                 {
00122                     int idx = CV_IMAGE_ELEM( markers, int, i, j );
00123                     uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );
00124                     if( idx == -1 )
00125                         dst[0] = dst[1] = dst[2] = (uchar)255;
00126                     else if( idx <= 0 || idx > comp_count )
00127                         dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here
00128                     else
00129                     {
00130                         uchar* ptr = color_tab->data.ptr + (idx-1)*3;
00131                         dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];
00132                     }
00133                 }
00134 
00135             cvAddWeighted( wshed, 0.5, img_gray, 0.5, 0, wshed );
00136             cvShowImage( "watershed transform", wshed );
00137             cvReleaseMemStorage( &storage );
00138             cvReleaseMat( &color_tab );
00139         }
00140     }
00141 
00142     return 1;
00143 }
00144 
00145 #ifdef _EiC
00146 main(1,"watershed.cpp");
00147 #endif

Wygenerowano Thu Mar 1 12:15:24 2007 dla OpenCV programem  doxygen 1.4.7