test_houghcircles.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2014, Itseez, Inc, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #include "test_precomp.hpp"
  44. namespace opencv_test { namespace {
  45. #ifndef DEBUG_IMAGES
  46. #define DEBUG_IMAGES 0
  47. #endif
  48. //#define GENERATE_DATA // generate data in debug mode via CPU code path (without IPP / OpenCL and other accelerators)
  49. using namespace cv;
  50. using namespace std;
  51. static string getTestCaseName(const string& picture_name, double minDist, double edgeThreshold, double accumThreshold, int minRadius, int maxRadius)
  52. {
  53. string results_name = cv::format("circles_%s_%.0f_%.0f_%.0f_%d_%d",
  54. picture_name.c_str(), minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
  55. string temp(results_name);
  56. size_t pos = temp.find_first_of("\\/.");
  57. while (pos != string::npos) {
  58. temp.replace(pos, 1, "_");
  59. pos = temp.find_first_of("\\/.");
  60. }
  61. return temp;
  62. }
  63. #if DEBUG_IMAGES
  64. static void highlightCircles(const string& imagePath, const vector<Vec3f>& circles, const string& outputImagePath)
  65. {
  66. Mat imgDebug = imread(imagePath, IMREAD_COLOR);
  67. const Scalar yellow(0, 255, 255);
  68. for (vector<Vec3f>::const_iterator iter = circles.begin(); iter != circles.end(); ++iter)
  69. {
  70. const Vec3f& circle = *iter;
  71. float x = circle[0];
  72. float y = circle[1];
  73. float r = max(circle[2], 2.0f);
  74. cv::circle(imgDebug, Point(int(x), int(y)), int(r), yellow);
  75. }
  76. imwrite(outputImagePath, imgDebug);
  77. }
  78. #endif
  79. typedef tuple<string, double, double, double, int, int> Image_MinDist_EdgeThreshold_AccumThreshold_MinRadius_MaxRadius_t;
  80. class HoughCirclesTestFixture : public testing::TestWithParam<Image_MinDist_EdgeThreshold_AccumThreshold_MinRadius_MaxRadius_t>
  81. {
  82. string picture_name;
  83. double minDist;
  84. double edgeThreshold;
  85. double accumThreshold;
  86. int minRadius;
  87. int maxRadius;
  88. public:
  89. HoughCirclesTestFixture()
  90. {
  91. picture_name = get<0>(GetParam());
  92. minDist = get<1>(GetParam());
  93. edgeThreshold = get<2>(GetParam());
  94. accumThreshold = get<3>(GetParam());
  95. minRadius = get<4>(GetParam());
  96. maxRadius = get<5>(GetParam());
  97. }
  98. HoughCirclesTestFixture(const string& picture, double minD, double edge, double accum, int minR, int maxR) :
  99. picture_name(picture), minDist(minD), edgeThreshold(edge), accumThreshold(accum), minRadius(minR), maxRadius(maxR)
  100. {
  101. }
  102. template <typename CircleType>
  103. void run_test(const char* xml_name)
  104. {
  105. string test_case_name = getTestCaseName(picture_name, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
  106. string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
  107. Mat src = imread(filename, IMREAD_GRAYSCALE);
  108. EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
  109. GaussianBlur(src, src, Size(9, 9), 2, 2);
  110. vector<CircleType> circles;
  111. const double dp = 1.0;
  112. HoughCircles(src, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
  113. string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
  114. #if DEBUG_IMAGES
  115. highlightCircles(filename, circles, imgProc + test_case_name + ".png");
  116. #endif
  117. string xml = imgProc + xml_name;
  118. #ifdef GENERATE_DATA
  119. {
  120. FileStorage fs(xml, FileStorage::READ);
  121. ASSERT_TRUE(!fs.isOpened() || fs[test_case_name].empty());
  122. }
  123. {
  124. FileStorage fs(xml, FileStorage::APPEND);
  125. EXPECT_TRUE(fs.isOpened()) << "Cannot open sanity data file: " << xml;
  126. fs << test_case_name << circles;
  127. }
  128. #else
  129. FileStorage fs(xml, FileStorage::READ);
  130. FileNode node = fs[test_case_name];
  131. ASSERT_FALSE(node.empty()) << "Missing test data: " << test_case_name << std::endl << "XML: " << xml;
  132. vector<CircleType> exp_circles;
  133. read(fs[test_case_name], exp_circles, vector<CircleType>());
  134. fs.release();
  135. EXPECT_EQ(exp_circles.size(), circles.size());
  136. #endif
  137. }
  138. };
  139. TEST_P(HoughCirclesTestFixture, regression)
  140. {
  141. run_test<Vec3f>("HoughCircles.xml");
  142. }
  143. TEST_P(HoughCirclesTestFixture, regression4f)
  144. {
  145. run_test<Vec4f>("HoughCircles4f.xml");
  146. }
  147. INSTANTIATE_TEST_CASE_P(ImgProc, HoughCirclesTestFixture, testing::Combine(
  148. // picture_name:
  149. testing::Values("imgproc/stuff.jpg"),
  150. // minDist:
  151. testing::Values(20),
  152. // edgeThreshold:
  153. testing::Values(20),
  154. // accumThreshold:
  155. testing::Values(30),
  156. // minRadius:
  157. testing::Values(20),
  158. // maxRadius:
  159. testing::Values(200)
  160. ));
  161. class HoughCirclesTest : public testing::TestWithParam<HoughModes>
  162. {
  163. protected:
  164. HoughModes method;
  165. public:
  166. HoughCirclesTest() { method = GetParam(); }
  167. };
  168. TEST_P(HoughCirclesTest, DefaultMaxRadius)
  169. {
  170. string picture_name = "imgproc/stuff.jpg";
  171. string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
  172. Mat src = imread(filename, IMREAD_GRAYSCALE);
  173. EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
  174. GaussianBlur(src, src, Size(9, 9), 2, 2);
  175. double dp = 1.0;
  176. double minDist = 20.0;
  177. double edgeThreshold = 20.0;
  178. double param2 = method == HOUGH_GRADIENT_ALT ? 0.9 : 30.;
  179. int minRadius = method == HOUGH_GRADIENT_ALT ? 10 : 20;
  180. int maxRadius = 0;
  181. vector<Vec3f> circles;
  182. vector<Vec4f> circles4f;
  183. HoughCircles(src, circles, method, dp, minDist, edgeThreshold, param2, minRadius, maxRadius);
  184. HoughCircles(src, circles4f, method, dp, minDist, edgeThreshold, param2, minRadius, maxRadius);
  185. #if DEBUG_IMAGES
  186. string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
  187. highlightCircles(filename, circles, imgProc + "HoughCirclesTest_DefaultMaxRadius.png");
  188. #endif
  189. int maxDimension = std::max(src.rows, src.cols);
  190. if(method == HOUGH_GRADIENT_ALT)
  191. {
  192. EXPECT_EQ(circles.size(), size_t(3)) << "Should find 3 circles";
  193. }
  194. else
  195. {
  196. EXPECT_GT(circles.size(), size_t(0)) << "Should find at least some circles";
  197. }
  198. for (size_t i = 0; i < circles.size(); ++i)
  199. {
  200. EXPECT_GE(circles[i][2], minRadius) << "Radius should be >= minRadius";
  201. EXPECT_LE(circles[i][2], maxDimension) << "Radius should be <= max image dimension";
  202. }
  203. }
  204. TEST_P(HoughCirclesTest, CentersOnly)
  205. {
  206. string picture_name = "imgproc/stuff.jpg";
  207. string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
  208. Mat src = imread(filename, IMREAD_GRAYSCALE);
  209. EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
  210. GaussianBlur(src, src, Size(9, 9), 2, 2);
  211. double dp = 1.0;
  212. double minDist = 20.0;
  213. double edgeThreshold = 20.0;
  214. double param2 = method == HOUGH_GRADIENT_ALT ? 0.9 : 30.;
  215. int minRadius = method == HOUGH_GRADIENT_ALT ? 10 : 20;
  216. int maxRadius = -1;
  217. vector<Vec3f> circles;
  218. vector<Vec4f> circles4f;
  219. HoughCircles(src, circles, method, dp, minDist, edgeThreshold, param2, minRadius, maxRadius);
  220. HoughCircles(src, circles4f, method, dp, minDist, edgeThreshold, param2, minRadius, maxRadius);
  221. #if DEBUG_IMAGES
  222. string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
  223. highlightCircles(filename, circles, imgProc + "HoughCirclesTest_DefaultMaxRadius.png");
  224. #endif
  225. if(method == HOUGH_GRADIENT_ALT)
  226. {
  227. EXPECT_EQ(circles.size(), size_t(3)) << "Should find 3 circles";
  228. }
  229. else
  230. {
  231. EXPECT_GT(circles.size(), size_t(0)) << "Should find at least some circles";
  232. }
  233. for (size_t i = 0; i < circles.size(); ++i)
  234. {
  235. if( method == HOUGH_GRADIENT )
  236. {
  237. EXPECT_EQ(circles[i][2], 0.0f) << "Did not ask for radius";
  238. }
  239. EXPECT_EQ(circles[i][0], circles4f[i][0]);
  240. EXPECT_EQ(circles[i][1], circles4f[i][1]);
  241. EXPECT_EQ(circles[i][2], circles4f[i][2]);
  242. }
  243. }
  244. TEST_P(HoughCirclesTest, ManySmallCircles)
  245. {
  246. string picture_name = "imgproc/beads.jpg";
  247. string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
  248. Mat src = imread(filename, IMREAD_GRAYSCALE);
  249. EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
  250. const double dp = method == HOUGH_GRADIENT_ALT ? 1.5 : 1.0;
  251. double minDist = 10;
  252. double edgeThreshold = 90;
  253. double accumThreshold = 11;
  254. double minCos2 = 0.85;
  255. double param2 = method == HOUGH_GRADIENT_ALT ? minCos2 : accumThreshold;
  256. int minRadius = 7;
  257. int maxRadius = 18;
  258. int ncircles_min = method == HOUGH_GRADIENT_ALT ? 2000 : 3000;
  259. Mat src_smooth;
  260. if( method == HOUGH_GRADIENT_ALT )
  261. GaussianBlur(src, src_smooth, Size(7, 7), 1.5, 1.5);
  262. else
  263. src.copyTo(src_smooth);
  264. vector<Vec3f> circles;
  265. vector<Vec4f> circles4f;
  266. HoughCircles(src_smooth, circles, method, dp, minDist, edgeThreshold, param2, minRadius, maxRadius);
  267. HoughCircles(src_smooth, circles4f, method, dp, minDist, edgeThreshold, param2, minRadius, maxRadius);
  268. #if DEBUG_IMAGES
  269. string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
  270. string test_case_name = getTestCaseName(picture_name, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
  271. highlightCircles(filename, circles, imgProc + test_case_name + ".png");
  272. #endif
  273. EXPECT_GT(circles.size(), size_t(ncircles_min)) << "Should find a lot of circles";
  274. EXPECT_EQ(circles.size(), circles4f.size());
  275. }
  276. INSTANTIATE_TEST_CASE_P(HoughGradient, HoughCirclesTest, testing::Values(HOUGH_GRADIENT));
  277. INSTANTIATE_TEST_CASE_P(HoughGradientAlt, HoughCirclesTest, testing::Values(HOUGH_GRADIENT_ALT));
  278. }} // namespace