Guest User

Untitled

a guest
Nov 18th, 2011
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. #ifdef _CH_
  2. #pragma package <opencv>
  3. #endif
  4.  
  5. #ifndef _EiC
  6. #include "cv.h"
  7. #include "highgui.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #endif
  11.  
  12.  
  13. IplImage* marker_mask = 0;
  14. IplImage* markers = 0;
  15. IplImage* img0 = 0, *img = 0, *img_gray = 0, *wshed = 0;
  16. CvPoint prev_pt = {-1,-1};
  17.  
  18. void on_mouse( int event, int x, int y, int flags, void* param )
  19.  {
  20.      if( !img )
  21.          return;
  22.  
  23.      if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
  24.         prev_pt = cvPoint(-1,-1);
  25.      else if( event == CV_EVENT_LBUTTONDOWN )
  26.          prev_pt = cvPoint(x,y);
  27.    else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )
  28.      {
  29.          CvPoint pt = cvPoint(x,y);
  30.         if( prev_pt.x < 0 )
  31.              prev_pt = pt;
  32.         cvLine( marker_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
  33.          cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
  34.         prev_pt = pt;
  35.         cvShowImage( "image", img );
  36.    }
  37.  }
  38.  
  39.  
  40.  int main( int argc, char** argv )
  41.  {
  42.      char* filename = argc >= 2 ? argv[1] : (char*)"normal.jpg";
  43.      CvRNG rng = cvRNG(-1);
  44.  
  45.      if( (img0 = cvLoadImage(filename,1)) == 0 )
  46.          return 0;
  47.  
  48.      printf( "Hot keys: \n"
  49.              "\tESC - quit the program\n"
  50.              "\tr - restore the original image\n"
  51.              "\tw or ENTER - run watershed algorithm\n"
  52.             "\t\t(before running it, roughly mark the areas on the image)\n"
  53.             "\t  (before that, roughly outline several markers on the image)\n" );
  54.      
  55.     cvNamedWindow( "image", 1 );
  56.      cvNamedWindow( "watershed transform", 1 );
  57.  
  58.     img = cvCloneImage( img0 );
  59.      img_gray = cvCloneImage( img0 );
  60.      wshed = cvCloneImage( img0 );
  61.      marker_mask = cvCreateImage( cvGetSize(img), 8, 1 );
  62.      markers = cvCreateImage( cvGetSize(img), IPL_DEPTH_32S, 1 );
  63.      cvCvtColor( img, marker_mask, CV_BGR2GRAY );
  64.      cvCvtColor( marker_mask, img_gray, CV_GRAY2BGR );
  65.  
  66.      cvZero( marker_mask );
  67.      cvZero( wshed );
  68.     cvShowImage( "image", img );
  69.      cvShowImage( "watershed transform", wshed );
  70.      cvSetMouseCallback( "image", on_mouse, 0 );
  71.  
  72.      for(;;)
  73.      {
  74.          int c = cvWaitKey(0);
  75.  
  76.          if( (char)c == 27 )
  77.              break;
  78.  
  79.          if( (char)c == 'r' )
  80.          {
  81.              cvZero( marker_mask );
  82.              cvCopy( img0, img );
  83.             cvShowImage( "image", img );
  84.          }
  85.  
  86.          if( (char)c == 'w' || (char)c == '\n' )
  87.          {
  88.              CvMemStorage* storage = cvCreateMemStorage(0);
  89.              CvSeq* contours = 0;
  90.              CvMat* color_tab;
  91.              int i, j, comp_count = 0;
  92.              //cvSaveImage( "wshed_mask.png", marker_mask );
  93.              //marker_mask = cvLoadImage( "wshed_mask.png", 0 );
  94.              cvFindContours( marker_mask, storage, &contours, sizeof(CvContour),
  95.                              CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
  96.              cvZero( markers );
  97.              for( ; contours != 0; contours = contours->h_next, comp_count++ )
  98.              {
  99.                 cvDrawContours( markers, contours, cvScalarAll(comp_count+1),
  100.                                  cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) );
  101.             }
  102.  
  103.              color_tab = cvCreateMat( 1, comp_count, CV_8UC3 );
  104.              for( i = 0; i < comp_count; i++ )
  105.              {
  106.                  uchar* ptr = color_tab->data.ptr + i*3;
  107.                  ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);
  108.                  ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);
  109.                  ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);
  110.              }
  111.  
  112.              {
  113.              double t = (double)cvGetTickCount();
  114.              cvWatershed( img0, markers );
  115.              t = (double)cvGetTickCount() - t;
  116.              printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );
  117.              }
  118.  
  119.              // paint the watershed image
  120.             for( i = 0; i < markers->height; i++ )
  121.                 for( j = 0; j < markers->width; j++ )
  122.                 {
  123.                     int idx = CV_IMAGE_ELEM( markers, int, i, j );
  124.                     uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );
  125.                     if( idx == -1 )
  126.                          dst[0] = dst[1] = dst[2] = (uchar)255;
  127.                      else if( idx <= 0 || idx > comp_count )
  128.                          dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here
  129.                      else
  130.                      {
  131.                          uchar* ptr = color_tab->data.ptr + (idx-1)*3;
  132.                          dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];
  133.                      }
  134.                  }
  135.  
  136.              cvAddWeighted( wshed, 0.5, img_gray, 0.5, 0, wshed );
  137.              cvShowImage( "watershed transform", wshed );
  138.              cvReleaseMemStorage( &storage );
  139.              cvReleaseMat( &color_tab );
  140.         }
  141.      }
  142.  
  143.     return 1;
  144.  }
  145.  
  146.  #ifdef _EiC
  147.  main(1,"watershed.cpp");
  148.  #endif
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment