test_fld.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. const Size img_size(320, 240);
  7. const int FLD_TEST_SEED = 0x134679;
  8. const int EPOCHS = 10;
  9. class FLDBase : public testing::Test
  10. {
  11. public:
  12. FLDBase() { }
  13. protected:
  14. Mat test_image;
  15. vector<Vec4f> lines;
  16. RNG rng;
  17. int passedtests;
  18. void GenerateWhiteNoise(Mat& image);
  19. void GenerateConstColor(Mat& image);
  20. void GenerateLines(Mat& image, const unsigned int numLines);
  21. void GenerateEdgeLines(Mat& image, const unsigned int numLines);
  22. void GenerateBrokenLines(Mat& image, const unsigned int numLines);
  23. void GenerateRotatedRect(Mat& image);
  24. virtual void SetUp();
  25. };
  26. class ximgproc_FLD: public FLDBase
  27. {
  28. public:
  29. ximgproc_FLD() { }
  30. protected:
  31. };
  32. class ximgproc_ED: public FLDBase
  33. {
  34. public:
  35. ximgproc_ED()
  36. {
  37. detector = createEdgeDrawing();
  38. }
  39. protected:
  40. Ptr<EdgeDrawing> detector;
  41. };
  42. void FLDBase::GenerateWhiteNoise(Mat& image)
  43. {
  44. image = Mat(img_size, CV_8UC1);
  45. rng.fill(image, RNG::UNIFORM, 0, 256);
  46. }
  47. void FLDBase::GenerateConstColor(Mat& image)
  48. {
  49. image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 256)));
  50. }
  51. void FLDBase::GenerateLines(Mat& image, const unsigned int numLines)
  52. {
  53. image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 128)));
  54. for(unsigned int i = 0; i < numLines; ++i)
  55. {
  56. int y = rng.uniform(10, img_size.width - 10);
  57. Point p1(y, 10);
  58. Point p2(y, img_size.height - 10);
  59. line(image, p1, p2, Scalar(255), 2);
  60. }
  61. }
  62. void FLDBase::GenerateEdgeLines(Mat& image, const unsigned int numLines)
  63. {
  64. image = Mat(img_size, CV_8UC1, Scalar::all(0));
  65. for(unsigned int i = 0; i < numLines; ++i)
  66. {
  67. int y = rng.uniform(10, img_size.width - 10);
  68. Point p1(y, 10);
  69. Point p2(y, img_size.height - 10);
  70. line(image, p1, p2, Scalar(255), 1);
  71. }
  72. }
  73. void FLDBase::GenerateBrokenLines(Mat& image, const unsigned int numLines)
  74. {
  75. image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 128)));
  76. for(unsigned int i = 0; i < numLines; ++i)
  77. {
  78. int y = rng.uniform(10, img_size.width - 10);
  79. Point p1(y, 10);
  80. Point p2(y, img_size.height/2);
  81. line(image, p1, p2, Scalar(255), 2);
  82. p1 = Point2i(y, img_size.height/2 + 3);
  83. p2 = Point2i(y, img_size.height - 10);
  84. line(image, p1, p2, Scalar(255), 2);
  85. }
  86. }
  87. void FLDBase::GenerateRotatedRect(Mat& image)
  88. {
  89. image = Mat::zeros(img_size, CV_8UC1);
  90. Point center(rng.uniform(img_size.width/4, img_size.width*3/4),
  91. rng.uniform(img_size.height/4, img_size.height*3/4));
  92. Size rect_size(rng.uniform(img_size.width/8, img_size.width/6),
  93. rng.uniform(img_size.height/8, img_size.height/6));
  94. float angle = rng.uniform(0.f, 360.f);
  95. Point2f vertices[4];
  96. RotatedRect rRect = RotatedRect(center, rect_size, angle);
  97. rRect.points(vertices);
  98. for (int i = 0; i < 4; i++)
  99. {
  100. line(image, vertices[i], vertices[(i + 1) % 4], Scalar(255), 3);
  101. }
  102. }
  103. void FLDBase::SetUp()
  104. {
  105. lines.clear();
  106. test_image = Mat();
  107. rng = RNG(FLD_TEST_SEED);
  108. passedtests = 0;
  109. }
  110. TEST_F(ximgproc_FLD, whiteNoise)
  111. {
  112. for (int i = 0; i < EPOCHS; ++i)
  113. {
  114. GenerateWhiteNoise(test_image);
  115. Ptr<FastLineDetector> detector = createFastLineDetector(20);
  116. detector->detect(test_image, lines);
  117. if(40u >= lines.size()) ++passedtests;
  118. }
  119. ASSERT_EQ(EPOCHS, passedtests);
  120. }
  121. TEST_F(ximgproc_FLD, constColor)
  122. {
  123. for (int i = 0; i < EPOCHS; ++i)
  124. {
  125. GenerateConstColor(test_image);
  126. Ptr<FastLineDetector> detector = createFastLineDetector();
  127. detector->detect(test_image, lines);
  128. if(0u == lines.size()) ++passedtests;
  129. }
  130. ASSERT_EQ(EPOCHS, passedtests);
  131. }
  132. TEST_F(ximgproc_FLD, lines)
  133. {
  134. for (int i = 0; i < EPOCHS; ++i)
  135. {
  136. const unsigned int numOfLines = 1;
  137. GenerateLines(test_image, numOfLines);
  138. Ptr<FastLineDetector> detector = createFastLineDetector();
  139. detector->detect(test_image, lines);
  140. if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect
  141. }
  142. ASSERT_EQ(EPOCHS, passedtests);
  143. }
  144. TEST_F(ximgproc_FLD, edgeLines)
  145. {
  146. for (int i = 0; i < EPOCHS; ++i)
  147. {
  148. const unsigned int numOfLines = 1;
  149. GenerateEdgeLines(test_image, numOfLines);
  150. Ptr<FastLineDetector> detector = createFastLineDetector(10, 1.414213562f, 50, 50, 0);
  151. detector->detect(test_image, lines);
  152. if(numOfLines == lines.size()) ++passedtests;
  153. }
  154. ASSERT_EQ(EPOCHS, passedtests);
  155. }
  156. TEST_F(ximgproc_FLD, mergeLines)
  157. {
  158. for (int i = 0; i < EPOCHS; ++i)
  159. {
  160. const unsigned int numOfLines = 1;
  161. GenerateBrokenLines(test_image, numOfLines);
  162. Ptr<FastLineDetector> detector = createFastLineDetector(10, 1.414213562f, true);
  163. detector->detect(test_image, lines);
  164. if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect
  165. }
  166. ASSERT_EQ(EPOCHS, passedtests);
  167. }
  168. TEST_F(ximgproc_FLD, rotatedRect)
  169. {
  170. for (int i = 0; i < EPOCHS; ++i)
  171. {
  172. GenerateRotatedRect(test_image);
  173. Ptr<FastLineDetector> detector = createFastLineDetector();
  174. detector->detect(test_image, lines);
  175. if(2u <= lines.size()) ++passedtests;
  176. }
  177. ASSERT_EQ(EPOCHS, passedtests);
  178. }
  179. //************** EDGE DRAWING *******************
  180. TEST_F(ximgproc_ED, whiteNoise)
  181. {
  182. for (int i = 0; i < EPOCHS; ++i)
  183. {
  184. GenerateWhiteNoise(test_image);
  185. detector->detectEdges(test_image);
  186. detector->detectLines(lines);
  187. if(2u >= lines.size()) ++passedtests;
  188. }
  189. ASSERT_EQ(EPOCHS, passedtests);
  190. }
  191. TEST_F(ximgproc_ED, constColor)
  192. {
  193. for (int i = 0; i < EPOCHS; ++i)
  194. {
  195. GenerateConstColor(test_image);
  196. detector->detectEdges(test_image);
  197. if(0u == detector->getSegments().size()) ++passedtests;
  198. }
  199. ASSERT_EQ(EPOCHS, passedtests);
  200. }
  201. TEST_F(ximgproc_ED, lines)
  202. {
  203. for (int i = 0; i < EPOCHS; ++i)
  204. {
  205. const unsigned int numOfLines = 1;
  206. GenerateLines(test_image, numOfLines);
  207. detector->detectEdges(test_image);
  208. detector->detectLines(lines);
  209. if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect
  210. }
  211. ASSERT_EQ(EPOCHS, passedtests);
  212. }
  213. TEST_F(ximgproc_ED, mergeLines)
  214. {
  215. for (int i = 0; i < EPOCHS; ++i)
  216. {
  217. const unsigned int numOfLines = 1;
  218. GenerateBrokenLines(test_image, numOfLines);
  219. detector->detectEdges(test_image);
  220. detector->detectLines(lines);
  221. if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect
  222. }
  223. ASSERT_EQ(EPOCHS, passedtests);
  224. }
  225. TEST_F(ximgproc_ED, rotatedRect)
  226. {
  227. for (int i = 0; i < EPOCHS; ++i)
  228. {
  229. GenerateRotatedRect(test_image);
  230. detector->detectEdges(test_image);
  231. detector->detectLines(lines);
  232. if(6u <= lines.size()) ++passedtests;
  233. }
  234. ASSERT_EQ(EPOCHS, passedtests);
  235. }
  236. TEST_F(ximgproc_ED, ManySmallCircles)
  237. {
  238. string picture_name = "cv/imgproc/beads.jpg";
  239. string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
  240. test_image = imread(filename, IMREAD_GRAYSCALE);
  241. EXPECT_FALSE(test_image.empty()) << "Invalid test image: " << filename;
  242. vector<Vec6d> ellipses;
  243. detector->detectEdges(test_image);
  244. detector->detectLines(lines);
  245. detector->detectEllipses(ellipses);
  246. size_t segments_size = 6458;
  247. size_t lines_size = 6264;
  248. size_t ellipses_size = 2449;
  249. EXPECT_EQ(detector->getSegments().size(), segments_size);
  250. EXPECT_GE(lines.size(), lines_size);
  251. EXPECT_LE(lines.size(), lines_size + 2);
  252. EXPECT_EQ(ellipses.size(), ellipses_size);
  253. }
  254. }} // namespace