detect_blob.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/imgproc.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <opencv2/features2d.hpp>
  5. #include <vector>
  6. #include <map>
  7. #include <iostream>
  8. using namespace std;
  9. using namespace cv;
  10. static void help(char** argv)
  11. {
  12. cout << "\n This program demonstrates how to use BLOB to detect and filter region \n"
  13. << "Usage: \n"
  14. << argv[0]
  15. << " <image1(detect_blob.png as default)>\n"
  16. << "Press a key when image window is active to change descriptor";
  17. }
  18. static String Legende(SimpleBlobDetector::Params &pAct)
  19. {
  20. String s = "";
  21. if (pAct.filterByArea)
  22. {
  23. String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minArea).str();
  24. String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxArea).str();
  25. s = " Area range [" + inf + " to " + sup + "]";
  26. }
  27. if (pAct.filterByCircularity)
  28. {
  29. String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minCircularity).str();
  30. String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxCircularity).str();
  31. if (s.length() == 0)
  32. s = " Circularity range [" + inf + " to " + sup + "]";
  33. else
  34. s += " AND Circularity range [" + inf + " to " + sup + "]";
  35. }
  36. if (pAct.filterByColor)
  37. {
  38. String inf = static_cast<const ostringstream&>(ostringstream() << (int)pAct.blobColor).str();
  39. if (s.length() == 0)
  40. s = " Blob color " + inf;
  41. else
  42. s += " AND Blob color " + inf;
  43. }
  44. if (pAct.filterByConvexity)
  45. {
  46. String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minConvexity).str();
  47. String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxConvexity).str();
  48. if (s.length() == 0)
  49. s = " Convexity range[" + inf + " to " + sup + "]";
  50. else
  51. s += " AND Convexity range[" + inf + " to " + sup + "]";
  52. }
  53. if (pAct.filterByInertia)
  54. {
  55. String inf = static_cast<const ostringstream&>(ostringstream() << pAct.minInertiaRatio).str();
  56. String sup = static_cast<const ostringstream&>(ostringstream() << pAct.maxInertiaRatio).str();
  57. if (s.length() == 0)
  58. s = " Inertia ratio range [" + inf + " to " + sup + "]";
  59. else
  60. s += " AND Inertia ratio range [" + inf + " to " + sup + "]";
  61. }
  62. return s;
  63. }
  64. int main(int argc, char *argv[])
  65. {
  66. String fileName;
  67. cv::CommandLineParser parser(argc, argv, "{@input |detect_blob.png| }{h help | | }");
  68. if (parser.has("h"))
  69. {
  70. help(argv);
  71. return 0;
  72. }
  73. fileName = parser.get<string>("@input");
  74. Mat img = imread(samples::findFile(fileName), IMREAD_COLOR);
  75. if (img.empty())
  76. {
  77. cout << "Image " << fileName << " is empty or cannot be found\n";
  78. return 1;
  79. }
  80. SimpleBlobDetector::Params pDefaultBLOB;
  81. // This is default parameters for SimpleBlobDetector
  82. pDefaultBLOB.thresholdStep = 10;
  83. pDefaultBLOB.minThreshold = 10;
  84. pDefaultBLOB.maxThreshold = 220;
  85. pDefaultBLOB.minRepeatability = 2;
  86. pDefaultBLOB.minDistBetweenBlobs = 10;
  87. pDefaultBLOB.filterByColor = false;
  88. pDefaultBLOB.blobColor = 0;
  89. pDefaultBLOB.filterByArea = false;
  90. pDefaultBLOB.minArea = 25;
  91. pDefaultBLOB.maxArea = 5000;
  92. pDefaultBLOB.filterByCircularity = false;
  93. pDefaultBLOB.minCircularity = 0.9f;
  94. pDefaultBLOB.maxCircularity = (float)1e37;
  95. pDefaultBLOB.filterByInertia = false;
  96. pDefaultBLOB.minInertiaRatio = 0.1f;
  97. pDefaultBLOB.maxInertiaRatio = (float)1e37;
  98. pDefaultBLOB.filterByConvexity = false;
  99. pDefaultBLOB.minConvexity = 0.95f;
  100. pDefaultBLOB.maxConvexity = (float)1e37;
  101. // Descriptor array for BLOB
  102. vector<String> typeDesc;
  103. // Param array for BLOB
  104. vector<SimpleBlobDetector::Params> pBLOB;
  105. vector<SimpleBlobDetector::Params>::iterator itBLOB;
  106. // Color palette
  107. vector< Vec3b > palette;
  108. for (int i = 0; i<65536; i++)
  109. {
  110. uchar c1 = (uchar)rand();
  111. uchar c2 = (uchar)rand();
  112. uchar c3 = (uchar)rand();
  113. palette.push_back(Vec3b(c1, c2, c3));
  114. }
  115. help(argv);
  116. // These descriptors are going to be detecting and computing BLOBS with 6 different params
  117. // Param for first BLOB detector we want all
  118. typeDesc.push_back("BLOB"); // see http://docs.opencv.org/4.x/d0/d7a/classcv_1_1SimpleBlobDetector.html
  119. pBLOB.push_back(pDefaultBLOB);
  120. pBLOB.back().filterByArea = true;
  121. pBLOB.back().minArea = 1;
  122. pBLOB.back().maxArea = float(img.rows*img.cols);
  123. // Param for second BLOB detector we want area between 500 and 2900 pixels
  124. typeDesc.push_back("BLOB");
  125. pBLOB.push_back(pDefaultBLOB);
  126. pBLOB.back().filterByArea = true;
  127. pBLOB.back().minArea = 500;
  128. pBLOB.back().maxArea = 2900;
  129. // Param for third BLOB detector we want only circular object
  130. typeDesc.push_back("BLOB");
  131. pBLOB.push_back(pDefaultBLOB);
  132. pBLOB.back().filterByCircularity = true;
  133. // Param for Fourth BLOB detector we want ratio inertia
  134. typeDesc.push_back("BLOB");
  135. pBLOB.push_back(pDefaultBLOB);
  136. pBLOB.back().filterByInertia = true;
  137. pBLOB.back().minInertiaRatio = 0;
  138. pBLOB.back().maxInertiaRatio = (float)0.2;
  139. // Param for fifth BLOB detector we want ratio inertia
  140. typeDesc.push_back("BLOB");
  141. pBLOB.push_back(pDefaultBLOB);
  142. pBLOB.back().filterByConvexity = true;
  143. pBLOB.back().minConvexity = 0.;
  144. pBLOB.back().maxConvexity = (float)0.9;
  145. // Param for six BLOB detector we want blob with gravity center color equal to 0
  146. typeDesc.push_back("BLOB");
  147. pBLOB.push_back(pDefaultBLOB);
  148. pBLOB.back().filterByColor = true;
  149. pBLOB.back().blobColor = 0;
  150. itBLOB = pBLOB.begin();
  151. vector<double> desMethCmp;
  152. Ptr<Feature2D> b;
  153. String label;
  154. // Descriptor loop
  155. vector<String>::iterator itDesc;
  156. for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
  157. {
  158. vector<KeyPoint> keyImg1;
  159. if (*itDesc == "BLOB")
  160. {
  161. b = SimpleBlobDetector::create(*itBLOB);
  162. label = Legende(*itBLOB);
  163. ++itBLOB;
  164. }
  165. try
  166. {
  167. // We can detect keypoint with detect method
  168. vector<KeyPoint> keyImg;
  169. vector<Rect> zone;
  170. vector<vector <Point> > region;
  171. Mat desc, result(img.rows, img.cols, CV_8UC3);
  172. if (b.dynamicCast<SimpleBlobDetector>().get())
  173. {
  174. Ptr<SimpleBlobDetector> sbd = b.dynamicCast<SimpleBlobDetector>();
  175. sbd->detect(img, keyImg, Mat());
  176. drawKeypoints(img, keyImg, result);
  177. int i = 0;
  178. for (vector<KeyPoint>::iterator k = keyImg.begin(); k != keyImg.end(); ++k, ++i)
  179. circle(result, k->pt, (int)k->size, palette[i % 65536]);
  180. }
  181. namedWindow(*itDesc + label, WINDOW_AUTOSIZE);
  182. imshow(*itDesc + label, result);
  183. imshow("Original", img);
  184. waitKey();
  185. }
  186. catch (const Exception& e)
  187. {
  188. cout << "Feature : " << *itDesc << "\n";
  189. cout << e.msg << endl;
  190. }
  191. }
  192. return 0;
  193. }