Guest User

Untitled

a guest
Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. // GlowLoop.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. //#include "stdafx.h"
  5.  
  6. /*
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9. return 0;
  10. }
  11. */
  12.  
  13. #ifdef _CH_
  14. #pragma package <opencv>
  15. #endif
  16.  
  17. #define CV_NO_BACKWARD_COMPATIBILITY
  18.  
  19. #ifndef _EiC
  20. #include "cv.h"
  21. #include "highgui.h"
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #endif
  25.  
  26.  
  27. // Two windows, one to diaplay the output lines
  28. #define GLOW_WINDOW_NAME "Glow"
  29.  
  30. // and one for the input image
  31. #define INPUT_WINDOW_NAME "Input"
  32.  
  33. #define CONTROL_WINDOW_NAME GLOW_WINDOW_NAME
  34.  
  35. IplImage *image=0, *smoothImage = 0, *edgeImage = 0, *cannyImage = 0, *greyImage = 0;
  36.  
  37. // Our configuration parameters, which are adjusted by the sliders
  38.  
  39. // Initial blurring of the iamge, to get rid of uninteresting details
  40. int smoothRadius = 2;
  41.  
  42. // Parameters to the Canny edge-finder
  43. int lowThresh = 10;
  44. int highThresh = 100;
  45.  
  46. // Shortest vector that we want to keep (as the short vectors are mostly noise we don't care about)
  47. int minLength = 100;
  48.  
  49. // Apply straightening to our vectors to reduce the storage requirement in the projector
  50. int straightener = 10;
  51.  
  52.  
  53.  
  54. void writeContours( CvSeq* contours );
  55.  
  56.  
  57. int main( int argc, char** argv )
  58. {
  59. CvCapture* capture = 0;
  60.  
  61. int doWrite = 0;
  62.  
  63. int levels = 3;
  64. CvSeq* contours = 0;
  65. //CvSeq* filteredContours = 0;
  66.  
  67. CvMemStorage* contourStorage = cvCreateMemStorage(0);
  68.  
  69. //filteredContours = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvSeq), storage );
  70.  
  71. if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
  72. capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
  73. else if( argc == 2 )
  74. capture = cvCaptureFromAVI( argv[1] );
  75.  
  76. if( !capture )
  77. {
  78. fprintf(stderr,"Could not initialize capturing...\n");
  79. return -1;
  80. }
  81.  
  82. printf( "Hot keys: \n"
  83. "\tESC - quit the program\n"
  84. "\tSpacebar - write contours\n"
  85. );
  86.  
  87. cvNamedWindow( GLOW_WINDOW_NAME, 1 );
  88. cvNamedWindow( INPUT_WINDOW_NAME, 1 );
  89. //cvSetMouseCallback( INPUT_WINDOW_NAME, on_mouse, 0 );
  90. cvCreateTrackbar( "Blur", CONTROL_WINDOW_NAME, &smoothRadius, 10, 0 );
  91. cvCreateTrackbar( "Canny low", CONTROL_WINDOW_NAME, &lowThresh, 256, 0 );
  92. cvCreateTrackbar( "Canny high", CONTROL_WINDOW_NAME, &highThresh, 256, 0 );
  93. cvCreateTrackbar( "Straighten", CONTROL_WINDOW_NAME, &straightener, 50, 0 );
  94. cvCreateTrackbar( "Min length", CONTROL_WINDOW_NAME, &minLength, 256, 0 );
  95.  
  96. for(;;)
  97. {
  98.  
  99. IplImage* frame = 0;
  100. int c;
  101.  
  102. frame = cvQueryFrame( capture );
  103. if( !frame )
  104. break;
  105.  
  106. if( !image )
  107. {
  108. /* allocate all the buffers */
  109. image = cvCreateImage( cvGetSize(frame), 8, 3 );
  110. image->origin = frame->origin;
  111.  
  112. greyImage = cvCreateImage( cvGetSize(frame), 8, 1 );
  113. greyImage->origin = frame->origin;
  114.  
  115. smoothImage = cvCreateImage( cvGetSize(frame), 8, 1 );
  116. smoothImage->origin = frame->origin;
  117.  
  118. cannyImage = cvCreateImage( cvGetSize(frame), 8, 1 );
  119. cannyImage->origin = frame->origin;
  120.  
  121. edgeImage = cvCreateImage( cvGetSize(frame), 8, 1 );
  122. edgeImage->origin = frame->origin;
  123.  
  124. }
  125.  
  126.  
  127. cvCopy( frame, image, 0 );
  128. //cvCvtColor( image, hsv, CV_BGR2HSV );
  129.  
  130. // copy input frame to a greyscale iamge
  131. cvCvtColor( frame, greyImage, CV_BGR2GRAY );
  132.  
  133. if( smoothRadius > 0 )
  134. {
  135. int smoothWidth = (smoothRadius - 1 ) * 2 + 1;
  136. cvSmooth( greyImage, smoothImage, CV_GAUSSIAN, smoothWidth, smoothWidth );
  137. }
  138. else
  139. {
  140. // no smooth, so just copy
  141. cvCopy( greyImage, smoothImage, 0 );
  142. }
  143.  
  144. //int g_thresh = 100;
  145. // threshold the image - a cheap alternative to canny
  146. //cvThreshold( greyImage, cannyImage, g_thresh, 255, CV_THRESH_BINARY );
  147.  
  148. // Apply Canny edge-find
  149. int aperture = 3;
  150. cvCanny( smoothImage, cannyImage, lowThresh, highThresh, aperture );
  151.  
  152.  
  153.  
  154. CvContourScanner scanner = cvStartFindContours(cannyImage, contourStorage, sizeof(CvContour),
  155. CV_RETR_LIST/*CV_RETR_EXTERNAL*/,
  156. CV_CHAIN_APPROX_SIMPLE);
  157.  
  158. CvSeq* contour;
  159.  
  160. while ((contour = cvFindNextContour(scanner)) != NULL)
  161. {
  162. double len = cvContourPerimeter(contour);
  163.  
  164. if (len < minLength)
  165. {
  166. // contour too short - throw it away
  167. cvSubstituteContour(scanner, NULL);
  168. }
  169. else
  170. {
  171. // contour is long enough, so keep it
  172. // but apply some straightening to reduce the amount of storage we need to draw it with our projector
  173.  
  174. CvSeq* c_new = cvApproxPoly(contour, sizeof(CvContour), contourStorage, CV_POLY_APPROX_DP, straightener, 0);
  175.  
  176. cvSubstituteContour(scanner, c_new);
  177. }
  178. }
  179.  
  180. contours = cvEndFindContours(&scanner);
  181.  
  182. // clear our output image
  183. cvSet(edgeImage, cvScalar(0));
  184.  
  185. // draw the finished set of contours into the output image
  186. cvDrawContours(
  187. edgeImage,
  188. contours,
  189. cvScalarAll(255),
  190. cvScalarAll(255),
  191. 100
  192. );
  193.  
  194. if( doWrite != 0)
  195. {
  196. writeContours( contours );
  197. doWrite = 0;
  198. }
  199.  
  200. //cvClearSeq( contours ); does not return the memory used by the contours! Need to clear contourStorage instead
  201. cvClearMemStorage(contourStorage);
  202.  
  203. // Show our output image
  204. cvShowImage( GLOW_WINDOW_NAME, edgeImage );
  205.  
  206. // Show the input image for reference
  207. cvShowImage( INPUT_WINDOW_NAME, image );
  208.  
  209.  
  210.  
  211. c = cvWaitKey(10);
  212. if( (char) c == 27 ) // Hit Esc to quit the app
  213. break;
  214. switch( (char) c )
  215. {
  216. case ' ': // hit space to write a contour list
  217. doWrite = 1;
  218. break;
  219.  
  220. case 'b':
  221. //backproject_mode ^= 1;
  222. break;
  223. case 'c':
  224. //track_object = 0;
  225. //cvZero( histimg );
  226. break;
  227. default:
  228. ;
  229. }
  230.  
  231. }
  232.  
  233. cvReleaseCapture( &capture );
  234. cvDestroyWindow(INPUT_WINDOW_NAME);
  235. cvDestroyWindow(GLOW_WINDOW_NAME);
  236.  
  237. cvReleaseMemStorage( &contourStorage );
  238.  
  239. return 0;
  240. }
  241.  
  242.  
  243. void writeContours( CvSeq* contours )
  244. {
  245. printf("%d contours:\n", contours->total );
  246.  
  247. // loop over all the contours
  248. for( CvSeq* c=contours; c!=NULL; c=c->h_next )
  249. {
  250. CvSeqReader reader;
  251. cvStartReadSeq(c, &reader);
  252. const int points = c->total;
  253.  
  254. //Each contour is a series of points:
  255. printf(" %d points:\n", points );
  256. for (int point = 0; point < points; ++point)
  257. {
  258. CvPoint p;
  259. CV_READ_SEQ_ELEM(p, reader);
  260. // Do stuff with p
  261.  
  262. printf(" (%d,%d)\n", p.x, p.y );
  263.  
  264. }
  265.  
  266.  
  267. }
  268. }
  269.  
  270.  
  271.  
  272. //unused
  273.  
  274. //*hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0;
  275. //CvHistogram *hist = 0;
  276. /*
  277. int backproject_mode = 0;
  278. int select_object = 0;
  279. int track_object = 0;
  280.  
  281. CvPoint origin;
  282. CvRect selection;
  283. CvRect track_window;
  284. CvBox2D track_box;
  285. CvConnectedComp track_comp;
  286. int hdims = 16;
  287. float hranges_arr[] = {0,180};
  288. float* hranges = hranges_arr;
  289. int vmin = 10, vmax = 256, smin = 30;
  290. */
  291.  
  292. /*
  293. void on_mouse( int event, int x, int y, int flags, void* param )
  294. {
  295. if( !image )
  296. return;
  297.  
  298. if( image->origin )
  299. y = image->height - y;
  300.  
  301. if( select_object )
  302. {
  303. selection.x = MIN(x,origin.x);
  304. selection.y = MIN(y,origin.y);
  305. selection.width = selection.x + CV_IABS(x - origin.x);
  306. selection.height = selection.y + CV_IABS(y - origin.y);
  307.  
  308. selection.x = MAX( selection.x, 0 );
  309. selection.y = MAX( selection.y, 0 );
  310. selection.width = MIN( selection.width, image->width );
  311. selection.height = MIN( selection.height, image->height );
  312. selection.width -= selection.x;
  313. selection.height -= selection.y;
  314. }
  315.  
  316. switch( event )
  317. {
  318. case CV_EVENT_LBUTTONDOWN:
  319. origin = cvPoint(x,y);
  320. selection = cvRect(x,y,0,0);
  321. select_object = 1;
  322. break;
  323. case CV_EVENT_LBUTTONUP:
  324. select_object = 0;
  325. if( selection.width > 0 && selection.height > 0 )
  326. track_object = -1;
  327. break;
  328. }
  329. }
  330.  
  331. //unused
  332. CvScalar hsv2rgb( float hue )
  333. {
  334. int rgb[3], p, sector;
  335. static const int sector_data[][3]=
  336. {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
  337. hue *= 0.033333333333333333333333333333333f;
  338. sector = cvFloor(hue);
  339. p = cvRound(255*(hue - sector));
  340. p ^= sector & 1 ? 255 : 0;
  341.  
  342. rgb[sector_data[sector][0]] = 255;
  343. rgb[sector_data[sector][1]] = 0;
  344. rgb[sector_data[sector][2]] = p;
  345.  
  346. return cvScalar(rgb[2], rgb[1], rgb[0],0);
  347. }
  348.  
  349.  
  350. */
  351.  
  352. #ifdef _EiC
  353. main(1,"camshiftdemo.c");
  354. #endif
Add Comment
Please, Sign In to add comment