snake.cpp
Go to the documentation of this file.
00001 #include <cstdio>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <vector>
00005 #include <map>
00006 #include <iostream>
00007 #include <sstream>
00008 #include <fstream>
00009 #include <opencv/cv.h>
00010 #include <opencv/highgui.h>
00011 #include <opencv/cvaux.h>
00012 
00013 
00014 #define KEY_ESC 1048603
00015 
00016 int posFrame = 0;
00017 int currPos = 0;
00018 
00019 IplImage* image = 0;
00020 std::vector<CvPoint> contourPoints;
00021 
00022 /*
00023  * Threshold a color image
00024  */
00025 void sum_rgb( IplImage* src, IplImage* dst ) {
00026 
00027   double minval, maxval;
00028   int threshold;
00029 
00030   // Allocate individual image  planes.
00031   IplImage* r = cvCreateImage(  cvGetSize(src), IPL_DEPTH_8U, 1 );
00032   IplImage* g = cvCreateImage(  cvGetSize(src), IPL_DEPTH_8U, 1 );
00033   IplImage* b = cvCreateImage(  cvGetSize(src), IPL_DEPTH_8U, 1 );
00034   // Split image onto the color planes.
00035   cvSplit( src, r, g, b, NULL );
00036   // Temporary storage.
00037   IplImage* s = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
00038   // Add equally weighted rgb values.
00039   cvAddWeighted( r, 1./3., g, 1./3., 0.0, s );
00040   cvAddWeighted( s, 2./3., b, 1./3., 0.0, s );
00041   // Truncate values above 100.
00042   cvMinMaxLoc(s, &minval, &maxval);
00043   threshold = (int)((minval + maxval)/2);
00044   cvThreshold( s, dst, threshold, 255, CV_THRESH_BINARY );
00045 
00046   cvReleaseImage(  &r );
00047   cvReleaseImage(  &g );
00048   cvReleaseImage(  &b );
00049   cvReleaseImage(  &s );
00050 }
00051 
00052 
00053 /*
00054  * Convert hsv to rgb
00055  */
00056 CvScalar hsv2rgb( float hue )
00057 {
00058     int rgb[3], p, sector;
00059     static const int sector_data[][3]=
00060         {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
00061     hue *= 0.033333333333333333333333333333333f;
00062     sector = cvFloor(hue);
00063     p = cvRound(255*(hue - sector));
00064     p ^= sector & 1 ? 255 : 0;
00065 
00066     rgb[sector_data[sector][0]] = 255;
00067     rgb[sector_data[sector][1]] = 0;
00068     rgb[sector_data[sector][2]] = p;
00069 
00070     return cvScalar(rgb[2], rgb[1], rgb[0],0);
00071 }
00072 
00073 
00074 void on_mouse( int event, int x, int y, int flags, void* param )
00075 {
00076     if( !image )
00077         return;
00078 
00079     if( image->origin )
00080         y = image->height - y;
00081 
00082     switch( event )
00083     {
00084                 case CV_EVENT_LBUTTONDOWN:
00085                         //std::cout << "Event = " << event << std::endl;
00086                         break;
00087                 case CV_EVENT_LBUTTONUP:
00088                         //std::cout << "Event = " << event << std::endl;
00089                         cvCircle(image,cvPoint(x,y),2,cvScalar(0,0,255),2);
00090                         contourPoints.push_back(cvPoint(x,y));
00091                         cvShowImage("Original",image);
00092                         break;
00093     }
00094 }
00095 
00096 int main(int argc, char** argv)
00097 {
00098 
00099         float alpha = atof(argv[2]);
00100         float beta = atof(argv[3]);
00101         float gamma = atof(argv[4]);
00102 
00103         CvSize win;
00104         CvTermCriteria criteria;
00105 
00106     criteria.max_iter = 20; // Do max N iterations
00107     criteria.epsilon = 30; // If only N points is moved then terminate
00108     criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
00109 
00110     win.width = 5; // search for energy minimizing in this area around snake points
00111     win.height = 5; // Be sure that width and heigth is uneven
00112 
00113         contourPoints = std::vector<CvPoint>();
00114 
00115         //char* filename = "/home/robotcontrol/Desktop/camshift/sequence.avi";
00116         char* filename = argv[1];
00117         //char* filename = "/home/robotcontrol/Desktop/camshift/tracker.avi";
00118         //char* filename = "/home/robotcontrol/Desktop/camshift/rope1.avi";
00119 
00120         cvNamedWindow("Original", 1);
00121         // cvNamedWindow("Snake" , 1);
00122 
00123     cvSetMouseCallback( "Original", on_mouse, 0 );
00124 
00125         image = cvLoadImage(filename);
00126 
00127     IplImage *binary = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
00128     IplImage *dummy = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 3);
00129     //canny = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
00130 
00131     cvCvtColor(image, binary, CV_BGR2GRAY);
00132         cvShowImage("Original",image);
00133         // cvShowImage("Snake",binary);
00134 
00135         char key ;
00136 
00137         //choose the search object first
00138 
00139         // while (1)
00140         // {
00141         //      key = cvWaitKey(10);
00142         //      if (key == 27)
00143         //      {
00144         //              break;
00145         //      }
00146         // }
00147 
00148         std::cout << "Click n to start snaking" << std::endl;
00149         //start tracking
00150         bool updated = true;
00151         while (1)
00152         {
00153                 if (updated)
00154                 {
00155                         // real computational part of the algo
00156                 cvShowImage( "Original", image );
00157                 // cvShowImage("Snake", binary);
00158                         updated = false;
00159                 }
00160 
00161                 // control the picture flow
00162                 key = cvWaitKey(10);
00163                 if (key == 27)
00164                 {
00165                         break;
00166                 }
00167 
00168                 if (key == 'n')
00169                 {
00170                         //std::cout << "alpha = " << alpha << ", beta = " << beta << ", gamma = " << gamma << std::endl;
00171                         //std::cout << "Size = " << contourPoints.size() << std::endl;
00172 
00173                         cvSnakeImage(binary, &contourPoints.at(0), contourPoints.size(), &alpha, &beta, &gamma, CV_VALUE, win, criteria, 1);
00174 
00175                         for( int i=0; i<contourPoints.size(); i++ ) {
00176                                 // cvCircle(image,contourPoints[i],1,cvScalar(255,0,0),0);
00177               int j = (i+1)%contourPoints.size();
00178               cvLine(image, contourPoints[i], contourPoints[j], cvScalar(255,0,0),1,CV_AA);
00179                         }
00180                         updated = true;
00181                 }
00182         }
00183 
00184         cvDestroyWindow("Original");
00185         std::cout << "Done." << std::endl;
00186 
00187         return 0;
00188 }
00189 


contracting_curve_density_algorithm
Author(s): Shulei Zhu, Dejan Pangercic
autogenerated on Mon Oct 6 2014 10:42:03