detect_mser.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/imgproc.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <opencv2/features2d.hpp>
  5. #include "opencv2/core/opengl.hpp"
  6. #include <vector>
  7. #include <map>
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <limits>
  11. #include <stdint.h>
  12. #ifdef HAVE_OPENGL
  13. #ifdef _WIN32
  14. #define WIN32_LEAN_AND_MEAN 1
  15. #define NOMINMAX 1
  16. #include <windows.h>
  17. #endif
  18. #if defined(_WIN64)
  19. #include <windows.h>
  20. #endif
  21. #if defined(__APPLE__)
  22. #include <OpenGL/gl.h>
  23. #include <OpenGL/glu.h>
  24. #else
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #endif
  28. #endif
  29. using namespace std;
  30. using namespace cv;
  31. static void help(char** argv)
  32. {
  33. cout << "\nThis program demonstrates how to use MSER to detect extremal regions\n"
  34. "Usage:\n"
  35. << argv[0] << " <image1(without parameter a synthetic image is used as default)>\n"
  36. "Press esc key when image window is active to change descriptor parameter\n"
  37. "Press 2, 8, 4, 6, +, -, or 5 keys in openGL windows to change view or use mouse\n";
  38. }
  39. struct MSERParams
  40. {
  41. MSERParams(int _delta = 5, int _min_area = 60, int _max_area = 14400,
  42. double _max_variation = 0.25, double _min_diversity = .2,
  43. int _max_evolution = 200, double _area_threshold = 1.01,
  44. double _min_margin = 0.003, int _edge_blur_size = 5)
  45. {
  46. delta = _delta;
  47. minArea = _min_area;
  48. maxArea = _max_area;
  49. maxVariation = _max_variation;
  50. minDiversity = _min_diversity;
  51. maxEvolution = _max_evolution;
  52. areaThreshold = _area_threshold;
  53. minMargin = _min_margin;
  54. edgeBlurSize = _edge_blur_size;
  55. pass2Only = false;
  56. }
  57. int delta;
  58. int minArea;
  59. int maxArea;
  60. double maxVariation;
  61. double minDiversity;
  62. bool pass2Only;
  63. int maxEvolution;
  64. double areaThreshold;
  65. double minMargin;
  66. int edgeBlurSize;
  67. };
  68. static String Legende(const MSERParams &pAct)
  69. {
  70. ostringstream ss;
  71. ss << "Area[" << pAct.minArea << "," << pAct.maxArea << "] ";
  72. ss << "del. [" << pAct.delta << "] ";
  73. ss << "var. [" << pAct.maxVariation << "] ";
  74. ss << "div. [" << (int)pAct.minDiversity << "] ";
  75. ss << "pas. [" << (int)pAct.pass2Only << "] ";
  76. ss << "RGb->evo. [" << pAct.maxEvolution << "] ";
  77. ss << "are. [" << (int)pAct.areaThreshold << "] ";
  78. ss << "mar. [" << (int)pAct.minMargin << "] ";
  79. ss << "siz. [" << pAct.edgeBlurSize << "]";
  80. return ss.str();
  81. }
  82. #ifdef HAVE_OPENGL
  83. const int win_width = 800;
  84. const int win_height = 640;
  85. #endif
  86. bool rotateEnable=true;
  87. bool keyPressed=false;
  88. Vec4f rotAxis(1,0,1,0);
  89. Vec3f zoom(1,0,0);
  90. float obsX = 0.f;
  91. float obsY = 0.f;
  92. float obsZ = -10.f;
  93. float tx = 0.f;
  94. float ty = 0.f;
  95. float thetaObs = -1.570f;
  96. float phiObs = 1.570f;
  97. float rObs = 10.f;
  98. int prevX = -1;
  99. int prevY = -1;
  100. int prevTheta = -1000;
  101. int prevPhi = -1000;
  102. #ifdef HAVE_OPENGL
  103. struct DrawData
  104. {
  105. ogl::Arrays arr;
  106. ogl::Texture2D tex;
  107. ogl::Buffer indices;
  108. };
  109. static void draw(void* userdata)
  110. {
  111. DrawData* data = static_cast<DrawData*>(userdata);
  112. glMatrixMode(GL_MODELVIEW);
  113. glLoadIdentity();
  114. gluLookAt(obsX, obsY, obsZ, 0, 0, .0, .0, 10.0, 0.0);
  115. glTranslatef(tx,ty,0);
  116. keyPressed = false;
  117. ogl::render(data->arr, data->indices, ogl::TRIANGLES);
  118. }
  119. static void onMouse(int event, int x, int y, int flags, void*)
  120. {
  121. if (event == EVENT_RBUTTONDOWN)
  122. {
  123. prevX = x;
  124. prevY = y;
  125. }
  126. if (event == EVENT_RBUTTONUP)
  127. {
  128. prevX = -1;
  129. prevY = -1;
  130. }
  131. if (prevX != -1)
  132. {
  133. tx += float((x - prevX) / 100.0);
  134. ty -= float((y - prevY) / 100.0);
  135. prevX = x;
  136. prevY = y;
  137. }
  138. if (event == EVENT_LBUTTONDOWN)
  139. {
  140. prevTheta = x;
  141. prevPhi = y;
  142. }
  143. if (event == EVENT_LBUTTONUP)
  144. {
  145. prevTheta = -1000;
  146. prevPhi = -1000;
  147. }
  148. if (prevTheta != -1000)
  149. {
  150. if (x - prevTheta<0)
  151. {
  152. thetaObs += 0.02f;
  153. }
  154. else if (x - prevTheta>0)
  155. {
  156. thetaObs -= 0.02f;
  157. }
  158. if (y - prevPhi<0)
  159. {
  160. phiObs -= 0.02f;
  161. }
  162. else if (y - prevPhi>0)
  163. {
  164. phiObs += 0.02f;
  165. }
  166. prevTheta = x;
  167. prevPhi = y;
  168. }
  169. if (event==EVENT_MOUSEWHEEL)
  170. {
  171. if (getMouseWheelDelta(flags)>0)
  172. rObs += 0.1f;
  173. else
  174. rObs -= 0.1f;
  175. }
  176. float pi = static_cast<float>(CV_PI);
  177. if (thetaObs>pi)
  178. {
  179. thetaObs = -2 * pi + thetaObs;
  180. }
  181. if (thetaObs<-pi)
  182. {
  183. thetaObs = 2 * pi + thetaObs;
  184. }
  185. if (phiObs>pi / 2)
  186. {
  187. phiObs = pi / 2 - 0.0001f;
  188. }
  189. if (phiObs<-pi / 2)
  190. {
  191. phiObs = -pi / 2 + 0.00001f;
  192. }
  193. if (rObs<0)
  194. {
  195. rObs = 0;
  196. }
  197. }
  198. #endif
  199. #ifdef HAVE_OPENGL
  200. static void DrawOpenGLMSER(Mat img, Mat result)
  201. {
  202. Mat imgGray;
  203. if (img.type() != CV_8UC1)
  204. cvtColor(img, imgGray, COLOR_BGR2GRAY);
  205. else
  206. imgGray = img;
  207. namedWindow("OpenGL", WINDOW_OPENGL);
  208. setMouseCallback("OpenGL", onMouse, NULL);
  209. Mat_<Vec3f> vertex(1, img.cols*img.rows);
  210. Mat_<Vec2f> texCoords(1, img.cols*img.rows);
  211. for (int i = 0, nbPix = 0; i<img.rows; i++)
  212. {
  213. for (int j = 0; j<img.cols; j++, nbPix++)
  214. {
  215. float x = (j) / (float)img.cols;
  216. float y = (i) / (float)img.rows;
  217. vertex.at< Vec3f >(0, nbPix) = Vec3f(float(2 * (x - 0.5)), float(2 * (0.5 - y)), float(imgGray.at<uchar>(i, j) / 512.0));
  218. texCoords.at< Vec2f>(0, nbPix) = Vec2f(x, y);
  219. }
  220. }
  221. Mat_<int> indices(1, (img.rows - 1)*(6 * img.cols));
  222. for (int i = 1, nbPix = 0; i<img.rows; i++)
  223. {
  224. for (int j = 1; j<img.cols; j++)
  225. {
  226. int c = i*img.cols + j;
  227. indices.at<int>(0, nbPix++) = c;
  228. indices.at<int>(0, nbPix++) = c - 1;
  229. indices.at<int>(0, nbPix++) = c - img.cols - 1;
  230. indices.at<int>(0, nbPix++) = c - img.cols - 1;
  231. indices.at<int>(0, nbPix++) = c - img.cols;
  232. indices.at<int>(0, nbPix++) = c;
  233. }
  234. }
  235. DrawData *data = new DrawData;
  236. data->arr.setVertexArray(vertex);
  237. data->arr.setTexCoordArray(texCoords);
  238. data->indices.copyFrom(indices);
  239. data->tex.copyFrom(result);
  240. glMatrixMode(GL_PROJECTION);
  241. glLoadIdentity();
  242. gluPerspective(45.0, (double)win_width / win_height, 0.0, 1000.0);
  243. glMatrixMode(GL_MODELVIEW);
  244. glLoadIdentity();
  245. glEnable(GL_TEXTURE_2D);
  246. data->tex.bind();
  247. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  248. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  249. glDisable(GL_CULL_FACE);
  250. setOpenGlDrawCallback("OpenGL", draw, data);
  251. for (;;)
  252. {
  253. updateWindow("OpenGL");
  254. char key = (char)waitKey(40);
  255. if (key == 27)
  256. break;
  257. if (key == 0x20)
  258. rotateEnable = !rotateEnable;
  259. float pi = static_cast<float>(CV_PI);
  260. switch (key) {
  261. case '5':
  262. obsX = 0, obsY = 0, obsZ = -10;
  263. thetaObs = -pi/2, phiObs = pi/2, rObs = 10;
  264. tx=0; ty=0;
  265. break;
  266. case '4':
  267. thetaObs += 0.1f;
  268. break;
  269. case '6':
  270. thetaObs -= 0.1f;
  271. break;
  272. case '2':
  273. phiObs -= 0.1f;
  274. break;
  275. case '8':
  276. phiObs += 0.1f;
  277. break;
  278. case '+':
  279. rObs -= 0.1f;
  280. break;
  281. case '-':
  282. rObs += 0.1f;
  283. break;
  284. }
  285. if (thetaObs>pi)
  286. {
  287. thetaObs = -2 * pi + thetaObs;
  288. }
  289. if (thetaObs<-pi)
  290. thetaObs = 2 * pi + thetaObs;
  291. if (phiObs>pi / 2)
  292. phiObs = pi / 2 - 0.0001f;
  293. if (phiObs<-pi / 2)
  294. phiObs = -pi / 2 + 0.00001f;
  295. if (rObs<0)
  296. rObs = 0;
  297. obsX = rObs*cos(thetaObs)*cos(phiObs);
  298. obsY = rObs*sin(thetaObs)*cos(phiObs);
  299. obsZ = rObs*sin(phiObs);
  300. }
  301. setOpenGlDrawCallback("OpenGL", 0, 0);
  302. destroyAllWindows();
  303. }
  304. #endif
  305. // Add nested rectangles of different widths and colors to an image
  306. static void addNestedRectangles(Mat &img, Point p0, int* width, int *color, int n) {
  307. for (int i = 0; i<n; i++)
  308. {
  309. rectangle(img, Rect(p0, Size(width[i], width[i])), Scalar(color[i]), 1);
  310. p0 += Point((width[i] - width[i + 1]) / 2, (width[i] - width[i + 1]) / 2);
  311. floodFill(img, p0, Scalar(color[i]));
  312. }
  313. }
  314. // Add nested circles of different widths and colors to an image
  315. static void addNestedCircles(Mat &img, Point p0, int *width, int *color, int n) {
  316. for (int i = 0; i<n; i++)
  317. {
  318. circle(img, p0, width[i] / 2, Scalar(color[i]), 1);
  319. floodFill(img, p0, Scalar(color[i]));
  320. }
  321. }
  322. static Mat MakeSyntheticImage()
  323. {
  324. const int fond = 0;
  325. Mat img(800, 800, CV_8UC1);
  326. img = Scalar(fond);
  327. int width[] = { 390, 380, 300, 290, 280, 270, 260, 250, 210, 190, 150, 100, 80, 70 };
  328. int color1[] = { 80, 180, 160, 140, 120, 100, 90, 110, 170, 150, 140, 100, 220 };
  329. int color2[] = { 81, 181, 161, 141, 121, 101, 91, 111, 171, 151, 141, 101, 221 };
  330. int color3[] = { 175, 75, 95, 115, 135, 155, 165, 145, 85, 105, 115, 155, 35 };
  331. int color4[] = { 173, 73, 93, 113, 133, 153, 163, 143, 83, 103, 113, 153, 33 };
  332. addNestedRectangles(img, Point(10, 10), width, color1, 13);
  333. addNestedCircles(img, Point(200, 600), width, color2, 13);
  334. addNestedRectangles(img, Point(410, 10), width, color3, 13);
  335. addNestedCircles(img, Point(600, 600), width, color4, 13);
  336. int histSize = 256;
  337. float range[] = { 0, 256 };
  338. const float* histRange[] = { range };
  339. Mat hist;
  340. // we compute the histogram
  341. calcHist(&img, 1, 0, Mat(), hist, 1, &histSize, histRange, true, false);
  342. cout << "****************Maximal region************************\n";
  343. for (int i = 0; i < hist.rows; i++)
  344. {
  345. if (hist.at<float>(i, 0)!=0)
  346. {
  347. cout << "h" << setw(3) << left << i << "\t=\t" << hist.at<float>(i, 0) << "\n";
  348. }
  349. }
  350. return img;
  351. }
  352. int main(int argc, char *argv[])
  353. {
  354. Mat imgOrig, img;
  355. Size blurSize(5, 5);
  356. cv::CommandLineParser parser(argc, argv, "{ help h | | }{ @input | | }");
  357. if (parser.has("help"))
  358. {
  359. help(argv);
  360. return 0;
  361. }
  362. string input = parser.get<string>("@input");
  363. if (!input.empty())
  364. {
  365. imgOrig = imread(samples::findFile(input), IMREAD_GRAYSCALE);
  366. blur(imgOrig, img, blurSize);
  367. }
  368. else
  369. {
  370. imgOrig = MakeSyntheticImage();
  371. img = imgOrig;
  372. }
  373. // Descriptor array MSER
  374. vector<String> typeDesc;
  375. // Param array for MSER
  376. vector<MSERParams> pMSER;
  377. // Color palette
  378. vector<Vec3b> palette;
  379. for (int i = 0; i<=numeric_limits<uint16_t>::max(); i++)
  380. palette.push_back(Vec3b((uchar)rand(), (uchar)rand(), (uchar)rand()));
  381. help(argv);
  382. MSERParams params;
  383. params.delta = 10;
  384. params.minArea = 100;
  385. params.maxArea = 5000;
  386. params.maxVariation = 2;
  387. params.minDiversity = 0;
  388. params.pass2Only = true;
  389. typeDesc.push_back("MSER");
  390. pMSER.push_back(params);
  391. params.pass2Only = false;
  392. typeDesc.push_back("MSER");
  393. pMSER.push_back(params);
  394. params.delta = 100;
  395. typeDesc.push_back("MSER");
  396. pMSER.push_back(params);
  397. vector<MSERParams>::iterator itMSER = pMSER.begin();
  398. Ptr<Feature2D> b;
  399. String label;
  400. // Descriptor loop
  401. vector<String>::iterator itDesc;
  402. Mat result(img.rows, img.cols, CV_8UC3);
  403. for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
  404. {
  405. vector<KeyPoint> keyImg1;
  406. if (*itDesc == "MSER")
  407. {
  408. if (img.type() == CV_8UC3)
  409. {
  410. b = MSER::create(itMSER->delta, itMSER->minArea, itMSER->maxArea, itMSER->maxVariation, itMSER->minDiversity, itMSER->maxEvolution,
  411. itMSER->areaThreshold, itMSER->minMargin, itMSER->edgeBlurSize);
  412. label = Legende(*itMSER);
  413. ++itMSER;
  414. }
  415. else
  416. {
  417. b = MSER::create(itMSER->delta, itMSER->minArea, itMSER->maxArea, itMSER->maxVariation, itMSER->minDiversity);
  418. b.dynamicCast<MSER>()->setPass2Only(itMSER->pass2Only);
  419. label = Legende(*itMSER);
  420. ++itMSER;
  421. }
  422. }
  423. if (img.type()==CV_8UC3)
  424. {
  425. img.copyTo(result);
  426. }
  427. else
  428. {
  429. vector<Mat> plan;
  430. plan.push_back(img);
  431. plan.push_back(img);
  432. plan.push_back(img);
  433. merge(plan,result);
  434. }
  435. try
  436. {
  437. // We can detect regions using detectRegions method
  438. vector<KeyPoint> keyImg;
  439. vector<Rect> zone;
  440. vector<vector <Point> > region;
  441. Mat desc;
  442. if (b.dynamicCast<MSER>().get())
  443. {
  444. Ptr<MSER> sbd = b.dynamicCast<MSER>();
  445. sbd->detectRegions(img, region, zone);
  446. //result = Scalar(0, 0, 0);
  447. int nbPixelInMSER=0;
  448. for (vector<vector <Point> >::iterator itr = region.begin(); itr != region.end(); ++itr)
  449. {
  450. for (vector <Point>::iterator itp = itr->begin(); itp != itr->end(); ++itp)
  451. {
  452. // all pixels belonging to region become blue
  453. result.at<Vec3b>(itp->y, itp->x) = Vec3b(128, 0, 0);
  454. nbPixelInMSER++;
  455. }
  456. }
  457. cout << "Number of MSER region: " << region.size() << "; Number of pixels in all MSER region: " << nbPixelInMSER << "\n";
  458. }
  459. const string winName = *itDesc + label;
  460. namedWindow(winName, WINDOW_AUTOSIZE);
  461. imshow(winName, result);
  462. imshow("Original", img);
  463. }
  464. catch (const Exception& e)
  465. {
  466. cout << "Feature: " << *itDesc << "\n";
  467. cout << e.msg << endl;
  468. }
  469. #ifdef HAVE_OPENGL
  470. DrawOpenGLMSER(img, result);
  471. #endif
  472. waitKey();
  473. }
  474. return 0;
  475. }