1 #include "test_precomp.hpp" 2 3 #include <vector> 4 5 using namespace cv; 6 using namespace std; 7 8 const Size img_size(640, 480); 9 const int LSD_TEST_SEED = 0x134679; 10 const int EPOCHS = 20; 11 12 class LSDBase : public testing::Test 13 { 14 public: 15 LSDBase() { } 16 17 protected: 18 Mat test_image; 19 vector<Vec4f> lines; 20 RNG rng; 21 int passedtests; 22 23 void GenerateWhiteNoise(Mat& image); 24 void GenerateConstColor(Mat& image); 25 void GenerateLines(Mat& image, const unsigned int numLines); 26 void GenerateRotatedRect(Mat& image); 27 virtual void SetUp(); 28 }; 29 30 class Imgproc_LSD_ADV: public LSDBase 31 { 32 public: 33 Imgproc_LSD_ADV() { } 34 protected: 35 36 }; 37 38 class Imgproc_LSD_STD: public LSDBase 39 { 40 public: 41 Imgproc_LSD_STD() { } 42 protected: 43 44 }; 45 46 class Imgproc_LSD_NONE: public LSDBase 47 { 48 public: 49 Imgproc_LSD_NONE() { } 50 protected: 51 52 }; 53 54 void LSDBase::GenerateWhiteNoise(Mat& image) 55 { 56 image = Mat(img_size, CV_8UC1); 57 rng.fill(image, RNG::UNIFORM, 0, 256); 58 } 59 60 void LSDBase::GenerateConstColor(Mat& image) 61 { 62 image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 256))); 63 } 64 65 void LSDBase::GenerateLines(Mat& image, const unsigned int numLines) 66 { 67 image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 128))); 68 69 for(unsigned int i = 0; i < numLines; ++i) 70 { 71 int y = rng.uniform(10, img_size.width - 10); 72 Point p1(y, 10); 73 Point p2(y, img_size.height - 10); 74 line(image, p1, p2, Scalar(255), 3); 75 } 76 } 77 78 void LSDBase::GenerateRotatedRect(Mat& image) 79 { 80 image = Mat::zeros(img_size, CV_8UC1); 81 82 Point center(rng.uniform(img_size.width/4, img_size.width*3/4), 83 rng.uniform(img_size.height/4, img_size.height*3/4)); 84 Size rect_size(rng.uniform(img_size.width/8, img_size.width/6), 85 rng.uniform(img_size.height/8, img_size.height/6)); 86 float angle = rng.uniform(0.f, 360.f); 87 88 Point2f vertices[4]; 89 90 RotatedRect rRect = RotatedRect(center, rect_size, angle); 91 92 rRect.points(vertices); 93 for (int i = 0; i < 4; i++) 94 { 95 line(image, vertices[i], vertices[(i + 1) % 4], Scalar(255), 3); 96 } 97 } 98 99 void LSDBase::SetUp() 100 { 101 lines.clear(); 102 test_image = Mat(); 103 rng = RNG(LSD_TEST_SEED); 104 passedtests = 0; 105 } 106 107 108 TEST_F(Imgproc_LSD_ADV, whiteNoise) 109 { 110 for (int i = 0; i < EPOCHS; ++i) 111 { 112 GenerateWhiteNoise(test_image); 113 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_ADV); 114 detector->detect(test_image, lines); 115 116 if(40u >= lines.size()) ++passedtests; 117 } 118 ASSERT_EQ(EPOCHS, passedtests); 119 } 120 121 TEST_F(Imgproc_LSD_ADV, constColor) 122 { 123 for (int i = 0; i < EPOCHS; ++i) 124 { 125 GenerateConstColor(test_image); 126 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_ADV); 127 detector->detect(test_image, lines); 128 129 if(0u == lines.size()) ++passedtests; 130 } 131 ASSERT_EQ(EPOCHS, passedtests); 132 } 133 134 TEST_F(Imgproc_LSD_ADV, lines) 135 { 136 for (int i = 0; i < EPOCHS; ++i) 137 { 138 const unsigned int numOfLines = 1; 139 GenerateLines(test_image, numOfLines); 140 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_ADV); 141 detector->detect(test_image, lines); 142 143 if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect 144 } 145 ASSERT_EQ(EPOCHS, passedtests); 146 } 147 148 TEST_F(Imgproc_LSD_ADV, rotatedRect) 149 { 150 for (int i = 0; i < EPOCHS; ++i) 151 { 152 GenerateRotatedRect(test_image); 153 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_ADV); 154 detector->detect(test_image, lines); 155 156 if(2u <= lines.size()) ++passedtests; 157 } 158 ASSERT_EQ(EPOCHS, passedtests); 159 } 160 161 TEST_F(Imgproc_LSD_STD, whiteNoise) 162 { 163 for (int i = 0; i < EPOCHS; ++i) 164 { 165 GenerateWhiteNoise(test_image); 166 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD); 167 detector->detect(test_image, lines); 168 169 if(50u >= lines.size()) ++passedtests; 170 } 171 ASSERT_EQ(EPOCHS, passedtests); 172 } 173 174 TEST_F(Imgproc_LSD_STD, constColor) 175 { 176 for (int i = 0; i < EPOCHS; ++i) 177 { 178 GenerateConstColor(test_image); 179 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD); 180 detector->detect(test_image, lines); 181 182 if(0u == lines.size()) ++passedtests; 183 } 184 ASSERT_EQ(EPOCHS, passedtests); 185 } 186 187 TEST_F(Imgproc_LSD_STD, lines) 188 { 189 for (int i = 0; i < EPOCHS; ++i) 190 { 191 const unsigned int numOfLines = 1; 192 GenerateLines(test_image, numOfLines); 193 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD); 194 detector->detect(test_image, lines); 195 196 if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect 197 } 198 ASSERT_EQ(EPOCHS, passedtests); 199 } 200 201 TEST_F(Imgproc_LSD_STD, rotatedRect) 202 { 203 for (int i = 0; i < EPOCHS; ++i) 204 { 205 GenerateRotatedRect(test_image); 206 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD); 207 detector->detect(test_image, lines); 208 209 if(4u <= lines.size()) ++passedtests; 210 } 211 ASSERT_EQ(EPOCHS, passedtests); 212 } 213 214 TEST_F(Imgproc_LSD_NONE, whiteNoise) 215 { 216 for (int i = 0; i < EPOCHS; ++i) 217 { 218 GenerateWhiteNoise(test_image); 219 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD); 220 detector->detect(test_image, lines); 221 222 if(50u >= lines.size()) ++passedtests; 223 } 224 ASSERT_EQ(EPOCHS, passedtests); 225 } 226 227 TEST_F(Imgproc_LSD_NONE, constColor) 228 { 229 for (int i = 0; i < EPOCHS; ++i) 230 { 231 GenerateConstColor(test_image); 232 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_NONE); 233 detector->detect(test_image, lines); 234 235 if(0u == lines.size()) ++passedtests; 236 } 237 ASSERT_EQ(EPOCHS, passedtests); 238 } 239 240 TEST_F(Imgproc_LSD_NONE, lines) 241 { 242 for (int i = 0; i < EPOCHS; ++i) 243 { 244 const unsigned int numOfLines = 1; 245 GenerateLines(test_image, numOfLines); 246 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_NONE); 247 detector->detect(test_image, lines); 248 249 if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect 250 } 251 ASSERT_EQ(EPOCHS, passedtests); 252 } 253 254 TEST_F(Imgproc_LSD_NONE, rotatedRect) 255 { 256 for (int i = 0; i < EPOCHS; ++i) 257 { 258 GenerateRotatedRect(test_image); 259 Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_NONE); 260 detector->detect(test_image, lines); 261 262 if(8u <= lines.size()) ++passedtests; 263 } 264 ASSERT_EQ(EPOCHS, passedtests); 265 } 266