/opencv/samples/c/mser_sample.cpp

https://github.com/malcolmreynolds/OpenCV · C++ · 134 lines · 111 code · 18 blank · 5 comment · 7 complexity · 554c0f77f5ae9594dab77a55e71f1c5c MD5 · raw file

  1. /* This sample code was originally provided by Liu Liu
  2. * Copyright� 2009, Liu Liu All rights reserved.
  3. */
  4. #include "opencv2/highgui/highgui.hpp"
  5. #include "opencv2/features2d/features2d.hpp"
  6. #include "opencv2/imgproc/imgproc_c.h"
  7. #include <stdio.h>
  8. void help()
  9. {
  10. printf("\nThis program demonstrates the Maximal Extremal Region interest point detector.\n"
  11. "It finds the most stable (in size) dark and white regions as a threshold is increased.\n"
  12. "\nCall:\n"
  13. "./mser_sample <path_and_image_filename, Default is 'puzzle.png'>\n\n");
  14. }
  15. static CvScalar colors[] =
  16. {
  17. {{0,0,255}},
  18. {{0,128,255}},
  19. {{0,255,255}},
  20. {{0,255,0}},
  21. {{255,128,0}},
  22. {{255,255,0}},
  23. {{255,0,0}},
  24. {{255,0,255}},
  25. {{255,255,255}},
  26. {{196,255,255}},
  27. {{255,255,196}}
  28. };
  29. static uchar bcolors[][3] =
  30. {
  31. {0,0,255},
  32. {0,128,255},
  33. {0,255,255},
  34. {0,255,0},
  35. {255,128,0},
  36. {255,255,0},
  37. {255,0,0},
  38. {255,0,255},
  39. {255,255,255}
  40. };
  41. int main( int argc, char** argv )
  42. {
  43. char path[1024];
  44. IplImage* img;
  45. help();
  46. if (argc!=2)
  47. {
  48. strcpy(path,"puzzle.png");
  49. img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
  50. if (!img)
  51. {
  52. printf("\nUsage: mser_sample <path_to_image>\n");
  53. return 0;
  54. }
  55. }
  56. else
  57. {
  58. strcpy(path,argv[1]);
  59. img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
  60. }
  61. if (!img)
  62. {
  63. printf("Unable to load image %s\n",path);
  64. return 0;
  65. }
  66. IplImage* rsp = cvLoadImage( path, CV_LOAD_IMAGE_COLOR );
  67. IplImage* ellipses = cvCloneImage(rsp);
  68. cvCvtColor(img,ellipses,CV_GRAY2BGR);
  69. CvSeq* contours;
  70. CvMemStorage* storage= cvCreateMemStorage();
  71. IplImage* hsv = cvCreateImage( cvGetSize( rsp ), IPL_DEPTH_8U, 3 );
  72. cvCvtColor( rsp, hsv, CV_BGR2YCrCb );
  73. CvMSERParams params = cvMSERParams();//cvMSERParams( 5, 60, cvRound(.2*img->width*img->height), .25, .2 );
  74. double t = (double)cvGetTickCount();
  75. cvExtractMSER( hsv, NULL, &contours, storage, params );
  76. t = cvGetTickCount() - t;
  77. printf( "MSER extracted %d contours in %g ms.\n", contours->total, t/((double)cvGetTickFrequency()*1000.) );
  78. uchar* rsptr = (uchar*)rsp->imageData;
  79. // draw mser with different color
  80. for ( int i = contours->total-1; i >= 0; i-- )
  81. {
  82. CvSeq* r = *(CvSeq**)cvGetSeqElem( contours, i );
  83. for ( int j = 0; j < r->total; j++ )
  84. {
  85. CvPoint* pt = CV_GET_SEQ_ELEM( CvPoint, r, j );
  86. rsptr[pt->x*3+pt->y*rsp->widthStep] = bcolors[i%9][2];
  87. rsptr[pt->x*3+1+pt->y*rsp->widthStep] = bcolors[i%9][1];
  88. rsptr[pt->x*3+2+pt->y*rsp->widthStep] = bcolors[i%9][0];
  89. }
  90. }
  91. // find ellipse ( it seems cvfitellipse2 have error or sth?
  92. for ( int i = 0; i < contours->total; i++ )
  93. {
  94. CvContour* r = *(CvContour**)cvGetSeqElem( contours, i );
  95. CvBox2D box = cvFitEllipse2( r );
  96. box.angle=(float)CV_PI/2-box.angle;
  97. if ( r->color > 0 )
  98. cvEllipseBox( ellipses, box, colors[9], 2 );
  99. else
  100. cvEllipseBox( ellipses, box, colors[2], 2 );
  101. }
  102. cvSaveImage( "rsp.png", rsp );
  103. cvNamedWindow( "original", 0 );
  104. cvShowImage( "original", img );
  105. cvNamedWindow( "response", 0 );
  106. cvShowImage( "response", rsp );
  107. cvNamedWindow( "ellipses", 0 );
  108. cvShowImage( "ellipses", ellipses );
  109. cvWaitKey(0);
  110. cvDestroyWindow( "original" );
  111. cvDestroyWindow( "response" );
  112. cvDestroyWindow( "ellipses" );
  113. cvReleaseImage(&rsp);
  114. cvReleaseImage(&img);
  115. cvReleaseImage(&ellipses);
  116. }