|
8 | 8 | namespace opencv_test {
|
9 | 9 | namespace {
|
10 | 10 |
|
11 |
| -// todo: ユニットテスト追加、テンプレート型引数でMatType・minmax対応、サンプル追加 |
12 |
| - |
13 |
| -TEST(ximgproc_StMorph_dev, compare_with_original_erode) |
14 |
| -{ |
15 |
| - // preparation |
16 |
| - int kRadius = 19; |
17 |
| - //Size sz(200, 150); |
18 |
| - Size sz = szVGA; |
19 |
| - int type = CV_8UC3; |
20 |
| - |
21 |
| - int kSize = kRadius * 2 + 1; |
22 |
| - Point anchor(kRadius, kRadius); |
23 |
| - Mat src(sz, type); |
24 |
| - Mat expected(sz, type); |
25 |
| - Mat actual(sz, type); |
26 |
| - Size kernelSize(kSize, kSize); |
27 |
| - Mat kernel = getStructuringElement(cv::MorphShapes::MORPH_RECT, kernelSize, Point(kRadius, kRadius)); |
28 |
| - |
29 |
| - src.setTo(240); |
30 |
| - putText(src, "A", Point(sz.height / 5 * 1, sz.height / 20 * 15), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(255, 40, 40), 30, LineTypes::FILLED); |
31 |
| - putText(src, "B", Point(sz.height / 5 * 2, sz.height / 20 * 16), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(20, 255, 0), 30, LineTypes::FILLED); |
32 |
| - putText(src, "C", Point(sz.height / 5 * 3, sz.height / 20 * 17), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(10, 10, 255), 30, LineTypes::FILLED); |
33 |
| - |
34 |
| - cv::TickMeter timer; |
35 |
| - |
36 |
| - // original |
37 |
| - timer.start(); |
38 |
| - cv::erode(src, expected, kernel); // 482ms for Elipse, kSize = 101 |
39 |
| - timer.stop(); |
40 |
| - double originalTime = timer.getTimeMilli(); |
41 |
| - timer.reset(); |
42 |
| - |
43 |
| - // proposal |
44 |
| - timer.start(); |
45 |
| - stMorph::erode(src, actual, kernel); // 217ms for Elipse, kSize = 101 |
46 |
| - timer.stop(); |
47 |
| - double proposalTime = timer.getTimeMilli(); |
48 |
| - |
49 |
| - // assertion |
50 |
| - Mat diff; |
51 |
| - cv::absdiff(expected, actual, diff); |
52 |
| - |
53 |
| -#if 1 |
54 |
| - putText(expected, std::to_string(originalTime), cv::Point(10, 20), HersheyFonts::FONT_HERSHEY_TRIPLEX, 1, Scalar(250, 40, 40), 1, LineTypes::FILLED); |
55 |
| - putText(actual, std::to_string(proposalTime), cv::Point(10, 20), HersheyFonts::FONT_HERSHEY_TRIPLEX, 1, Scalar(250, 40, 40), 1, LineTypes::FILLED); |
56 |
| - Mat con; |
57 |
| - double rate = 300.0 / src.cols; |
58 |
| - cv::hconcat(src, expected, con); |
59 |
| - cv::hconcat(con, actual, con); |
60 |
| - cv::resize(con, con, Size(), rate, rate, InterpolationFlags::INTER_NEAREST); |
61 |
| - cv::resize(diff, diff, Size(), rate, rate, InterpolationFlags::INTER_NEAREST); |
62 |
| - imshow("Bordered source", con); |
63 |
| - imshow("diff", diff); |
64 |
| - waitKey(); |
65 |
| - destroyAllWindows(); |
66 |
| -#endif |
67 |
| - |
68 |
| - double min, max; |
69 |
| - cv::minMaxLoc(diff, &min, &max); |
70 |
| - CV_Assert(max == 0); |
71 |
| -} |
72 |
| - |
73 |
| -TEST(develop, POW2RECT_COVERING) |
74 |
| -{ |
75 |
| - uchar ary[] { |
76 |
| - 0, 1, 0, 1, 0, 1, 0, 1, |
77 |
| - 1, 1, 0, 1, 1, 1, 1, 1, |
78 |
| - 1, 0, 0, 1, 0, 1, 0, 1, |
79 |
| - 0, 0, 1, 1, 1, 1, 1, 1, |
80 |
| - 0, 1, 0, 1, 0, 1, 1, 1, |
81 |
| - 1, 1, 1, 1, 1, 1, 1, 1, |
82 |
| - 0, 1, 0, 1, 0, 1, 0, 1, |
83 |
| - 1, 1, 1, 1, 1, 1, 1, 1, |
84 |
| - }; |
85 |
| - Mat kernel(8, 8, CV_8UC1, ary); |
86 |
| - std::vector<Rect> rects = stMorph::genPow2RectsToCoverKernel(kernel); |
87 |
| - |
88 |
| - int rate = 20; |
89 |
| - resize(kernel * 255, kernel, Size(), rate, rate, InterpolationFlags::INTER_NEAREST); |
90 |
| - cvtColor(kernel, kernel, cv::COLOR_GRAY2BGR); |
91 |
| - Scalar color[20]{ |
92 |
| - Scalar(83, 89, 73), Scalar(49, 238, 73), Scalar(220, 192, 189), Scalar(174, 207, 34), |
93 |
| - Scalar(144, 169, 187), Scalar(137, 94, 76), Scalar(42, 11, 215), Scalar(113, 11, 204), |
94 |
| - Scalar(71, 124, 8), Scalar(192, 38, 8), Scalar(82, 201, 8), Scalar(70, 7, 112), |
95 |
| - Scalar(166, 219, 201), Scalar(154, 173, 0), Scalar(132, 127, 139), Scalar(154, 1, 68), |
96 |
| - Scalar(231, 131, 56), Scalar(206, 238, 136), Scalar(188, 78, 173), Scalar(27, 178, 206) |
97 |
| - }; |
98 |
| - for (int i = 0; i < rects.size(); i++) |
99 |
| - { |
100 |
| - Rect rect = rects[i]; |
101 |
| - Point lt((rect.x ) * rate + i % 11, (rect.y ) * rate + i % 11); |
102 |
| - Point lb((rect.x ) * rate + i % 11, (rect.y + (1 << rect.height)) * rate - 11 + i % 11); |
103 |
| - Point rb((rect.x + (1 << rect.width)) * rate - 11 + i % 11, (rect.y + (1 << rect.height)) * rate - 11 + i % 11); |
104 |
| - Point rt((rect.x + (1 << rect.width)) * rate - 11 + i % 11, (rect.y ) * rate + i % 11); |
105 |
| - cv::line(kernel, lt, lb, color[i % 20], 2); |
106 |
| - cv::line(kernel, lb, rb, color[i % 20], 2); |
107 |
| - cv::line(kernel, rb, rt, color[i % 20], 2); |
108 |
| - cv::line(kernel, rt, lt, color[i % 20], 2); |
109 |
| - } |
110 |
| - imshow("kernel", kernel); |
111 |
| - |
112 |
| - waitKey(); |
113 |
| - destroyAllWindows(); |
114 |
| -} |
115 |
| - |
116 |
| -TEST(develop, PLANNING) |
117 |
| -{ |
118 |
| - std::vector<std::vector<bool>> map{ |
119 |
| - std::vector<bool>{0,0,0,0,0,0,0,1}, |
120 |
| - std::vector<bool>{0,0,0,0,0,0,1,0}, |
121 |
| - std::vector<bool>{0,0,0,0,0,1,0,0}, |
122 |
| - std::vector<bool>{0,0,0,0,1,0,0,0}, |
123 |
| - std::vector<bool>{0,0,0,1,0,0,0,0}, |
124 |
| - std::vector<bool>{0,0,1,0,0,0,0,0}, |
125 |
| - std::vector<bool>{0,1,0,0,0,0,0,0}, |
126 |
| - std::vector<bool>{1,0,0,0,0,0,0,0}, |
127 |
| - }; |
128 |
| - auto res = stMorph::planSparseTableConstr(map); |
129 |
| - |
130 |
| - int g = 30; |
131 |
| - int r = map.size(); |
132 |
| - int c = map[0].size(); |
133 |
| - Mat m = Mat::zeros(r * g, c * g, CV_8UC3); |
134 |
| - for (int row = 0; row < r; row++) |
135 |
| - { |
136 |
| - for (int col = 0; col < c; col++) |
137 |
| - { |
138 |
| - if (map[row][col]) cv::rectangle(m, Rect(col * g + g / 2 - 5, row * g + g / 2 - 5, 11, 11), Scalar(20, 20, 255), -1); |
139 |
| - } |
140 |
| - } |
141 |
| - for (int i = 0; i < res.size(); i++) |
142 |
| - { |
143 |
| - auto edge = res[i]; |
144 |
| - if (edge.ax == stMorph::Dim::Row) |
145 |
| - { |
146 |
| - cv::line(m, Point(edge.dimCol * g + g / 2, edge.dimRow * g + g / 2), Point(edge.dimCol * g + g / 2, (edge.dimRow + 1) * g + g / 2), Scalar(100, 100, 100), 2); |
147 |
| - } |
148 |
| - else |
149 |
| - { |
150 |
| - cv::line(m, Point(edge.dimCol * g + g / 2, edge.dimRow * g + g / 2), Point((edge.dimCol + 1) * g + g / 2, edge.dimRow * g + g / 2), Scalar(100, 100, 100), 2); |
151 |
| - } |
152 |
| - } |
153 |
| - imshow("Map", m); |
154 |
| - |
155 |
| - waitKey(); |
156 |
| - destroyAllWindows(); |
157 |
| -} |
158 |
| - |
159 | 11 | #pragma region Common test methods
|
160 | 12 |
|
161 | 13 | void assertArraysIdentical(InputArray ary1, InputArray ary2)
|
@@ -338,4 +190,186 @@ TEST(ximgproc_StMorph_ex, regression_hitmiss) { ex_rgr(im(CV_8UC1), MORPH_HITMIS
|
338 | 190 |
|
339 | 191 | #pragma endregion
|
340 | 192 |
|
| 193 | +#pragma region morph_dev |
| 194 | + |
| 195 | +TEST(ximgproc_StMorph_dev, compare_with_original_erode) |
| 196 | +{ |
| 197 | + // preparation |
| 198 | + int kRadius = 15; |
| 199 | + //Size sz(200, 150); |
| 200 | + Size sz = szVGA; |
| 201 | + int type = CV_8UC3; |
| 202 | + |
| 203 | + int kSize = kRadius * 2 + 1; |
| 204 | + Point anchor(kRadius, kRadius); |
| 205 | + Mat src(sz, type); |
| 206 | + Mat expected(sz, type); |
| 207 | + Mat actual(sz, type); |
| 208 | + Size kernelSize(kSize, kSize); |
| 209 | + Mat kernel = getStructuringElement(cv::MorphShapes::MORPH_RECT, kernelSize, Point(kRadius, kRadius)); |
| 210 | + |
| 211 | + src.setTo(240); |
| 212 | + putText(src, "A", Point(sz.height / 5 * 1, sz.height / 20 * 15), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(255, 40, 40), 30, LineTypes::FILLED); |
| 213 | + putText(src, "B", Point(sz.height / 5 * 2, sz.height / 20 * 16), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(20, 255, 0), 30, LineTypes::FILLED); |
| 214 | + putText(src, "C", Point(sz.height / 5 * 3, sz.height / 20 * 17), HersheyFonts::FONT_HERSHEY_TRIPLEX, 10, Scalar(10, 10, 255), 30, LineTypes::FILLED); |
| 215 | + |
| 216 | + cv::TickMeter timer; |
| 217 | + |
| 218 | + // original |
| 219 | + timer.start(); |
| 220 | + cv::erode(src, expected, kernel); // 482ms for Elipse, kSize = 101 |
| 221 | + timer.stop(); |
| 222 | + double originalTime = timer.getTimeMilli(); |
| 223 | + timer.reset(); |
| 224 | + |
| 225 | + // proposal |
| 226 | + timer.start(); |
| 227 | + stMorph::erode(src, actual, kernel); // 217ms for Elipse, kSize = 101 |
| 228 | + timer.stop(); |
| 229 | + double proposalTime = timer.getTimeMilli(); |
| 230 | + |
| 231 | + // assertion |
| 232 | + Mat diff; |
| 233 | + cv::absdiff(expected, actual, diff); |
| 234 | + |
| 235 | +#if 1 |
| 236 | + putText(expected, std::to_string(originalTime), cv::Point(10, 20), HersheyFonts::FONT_HERSHEY_TRIPLEX, 1, Scalar(250, 40, 40), 1, LineTypes::FILLED); |
| 237 | + putText(actual, std::to_string(proposalTime), cv::Point(10, 20), HersheyFonts::FONT_HERSHEY_TRIPLEX, 1, Scalar(250, 40, 40), 1, LineTypes::FILLED); |
| 238 | + Mat con; |
| 239 | + double rate = 300.0 / src.cols; |
| 240 | + cv::hconcat(src, expected, con); |
| 241 | + cv::hconcat(con, actual, con); |
| 242 | + cv::resize(con, con, Size(), rate, rate, InterpolationFlags::INTER_NEAREST); |
| 243 | + cv::resize(diff, diff, Size(), rate, rate, InterpolationFlags::INTER_NEAREST); |
| 244 | + imshow("Bordered source", con); |
| 245 | + imshow("diff", diff); |
| 246 | + waitKey(); |
| 247 | + destroyAllWindows(); |
| 248 | +#endif |
| 249 | + |
| 250 | + double min, max; |
| 251 | + cv::minMaxLoc(diff, &min, &max); |
| 252 | + CV_Assert(max == 0); |
| 253 | +} |
| 254 | + |
| 255 | +TEST(ximgproc_StMorph_dev, POW2RECT_COVERING) |
| 256 | +{ |
| 257 | + uchar ary[]{ |
| 258 | + 0, 1, 0, 1, 0, 1, 0, 1, |
| 259 | + 1, 1, 0, 1, 1, 1, 1, 1, |
| 260 | + 1, 0, 0, 1, 0, 1, 0, 1, |
| 261 | + 0, 0, 1, 1, 1, 1, 1, 1, |
| 262 | + 0, 1, 0, 1, 0, 1, 1, 1, |
| 263 | + 1, 1, 1, 1, 1, 1, 1, 1, |
| 264 | + 0, 1, 0, 1, 0, 1, 0, 1, |
| 265 | + 1, 1, 1, 1, 1, 1, 1, 1, |
| 266 | + }; |
| 267 | + Mat kernel(8, 8, CV_8UC1, ary); |
| 268 | + std::vector<Rect> rects = stMorph::genPow2RectsToCoverKernel(kernel); |
| 269 | + |
| 270 | + int rate = 20; |
| 271 | + resize(kernel * 255, kernel, Size(), rate, rate, InterpolationFlags::INTER_NEAREST); |
| 272 | + cvtColor(kernel, kernel, cv::COLOR_GRAY2BGR); |
| 273 | + Scalar color[20]{ |
| 274 | + Scalar(83, 89, 73), Scalar(49, 238, 73), Scalar(220, 192, 189), Scalar(174, 207, 34), |
| 275 | + Scalar(144, 169, 187), Scalar(137, 94, 76), Scalar(42, 11, 215), Scalar(113, 11, 204), |
| 276 | + Scalar(71, 124, 8), Scalar(192, 38, 8), Scalar(82, 201, 8), Scalar(70, 7, 112), |
| 277 | + Scalar(166, 219, 201), Scalar(154, 173, 0), Scalar(132, 127, 139), Scalar(154, 1, 68), |
| 278 | + Scalar(231, 131, 56), Scalar(206, 238, 136), Scalar(188, 78, 173), Scalar(27, 178, 206) |
| 279 | + }; |
| 280 | + for (int i = 0; i < rects.size(); i++) |
| 281 | + { |
| 282 | + Rect rect = rects[i]; |
| 283 | + Point lt((rect.x) * rate + i % 11, (rect.y) * rate + i % 11); |
| 284 | + Point lb((rect.x) * rate + i % 11, (rect.y + (1 << rect.height)) * rate - 11 + i % 11); |
| 285 | + Point rb((rect.x + (1 << rect.width)) * rate - 11 + i % 11, (rect.y + (1 << rect.height)) * rate - 11 + i % 11); |
| 286 | + Point rt((rect.x + (1 << rect.width)) * rate - 11 + i % 11, (rect.y) * rate + i % 11); |
| 287 | + cv::line(kernel, lt, lb, color[i % 20], 2); |
| 288 | + cv::line(kernel, lb, rb, color[i % 20], 2); |
| 289 | + cv::line(kernel, rb, rt, color[i % 20], 2); |
| 290 | + cv::line(kernel, rt, lt, color[i % 20], 2); |
| 291 | + } |
| 292 | + imshow("kernel", kernel); |
| 293 | + |
| 294 | + waitKey(); |
| 295 | + destroyAllWindows(); |
| 296 | +} |
| 297 | + |
| 298 | +TEST(ximgproc_StMorph_dev, PLANNING) |
| 299 | +{ |
| 300 | + std::vector<std::vector<bool>> map{ |
| 301 | + std::vector<bool>{0,0,0,0,0,0,0,1}, |
| 302 | + std::vector<bool>{0,0,0,0,0,0,1,0}, |
| 303 | + std::vector<bool>{0,0,0,0,0,1,0,0}, |
| 304 | + std::vector<bool>{0,0,0,0,1,0,0,0}, |
| 305 | + std::vector<bool>{0,0,0,1,0,0,0,0}, |
| 306 | + std::vector<bool>{0,0,1,0,0,0,0,0}, |
| 307 | + std::vector<bool>{0,1,0,0,0,0,0,0}, |
| 308 | + std::vector<bool>{1,0,0,0,0,0,0,0}, |
| 309 | + }; |
| 310 | + auto res = stMorph::planSparseTableConstr(map); |
| 311 | + |
| 312 | + int g = 30; |
| 313 | + int r = map.size(); |
| 314 | + int c = map[0].size(); |
| 315 | + Mat m = Mat::zeros(r * g, c * g, CV_8UC3); |
| 316 | + for (int row = 0; row < r; row++) |
| 317 | + { |
| 318 | + for (int col = 0; col < c; col++) |
| 319 | + { |
| 320 | + if (map[row][col]) cv::rectangle(m, Rect(col * g + g / 2 - 5, row * g + g / 2 - 5, 11, 11), Scalar(20, 20, 255), -1); |
| 321 | + } |
| 322 | + } |
| 323 | + for (int i = 0; i < res.size(); i++) |
| 324 | + { |
| 325 | + auto edge = res[i]; |
| 326 | + if (edge.ax == stMorph::Dim::Row) |
| 327 | + { |
| 328 | + cv::line(m, Point(edge.dimCol * g + g / 2, edge.dimRow * g + g / 2), Point(edge.dimCol * g + g / 2, (edge.dimRow + 1) * g + g / 2), Scalar(100, 100, 100), 2); |
| 329 | + } |
| 330 | + else |
| 331 | + { |
| 332 | + cv::line(m, Point(edge.dimCol * g + g / 2, edge.dimRow * g + g / 2), Point((edge.dimCol + 1) * g + g / 2, edge.dimRow * g + g / 2), Scalar(100, 100, 100), 2); |
| 333 | + } |
| 334 | + } |
| 335 | + imshow("Map", m); |
| 336 | + |
| 337 | + waitKey(); |
| 338 | + destroyAllWindows(); |
| 339 | +} |
| 340 | + |
| 341 | +void cvDilate(InputArray src, InputArray kernel, Point anchor = Point(-1, -1), |
| 342 | + int iterations = 1, |
| 343 | + BorderTypes bdrType = BorderTypes::BORDER_CONSTANT, Scalar& bdrVal = Scalar::all(DBL_MAX)) |
| 344 | +{ |
| 345 | + Mat actual; |
| 346 | + erode(src, actual, kernel, anchor, iterations, bdrType, bdrVal); |
| 347 | +} |
| 348 | +void cvErode(InputArray src, InputArray kernel, Point anchor = Point(-1, -1), |
| 349 | + int iterations = 1, |
| 350 | + BorderTypes bdrType = BorderTypes::BORDER_CONSTANT, Scalar& bdrVal = Scalar::all(DBL_MAX)) |
| 351 | +{ |
| 352 | + Mat actual; |
| 353 | + erode(src, actual, kernel, anchor, iterations, bdrType, bdrVal); |
| 354 | +} |
| 355 | +Mat kn51() { return getStructuringElement(cv::MorphShapes::MORPH_ELLIPSE, Size(51, 51)); } |
| 356 | +TEST(ximgproc_StMorph_dev, big_stDilate) |
| 357 | +{ |
| 358 | + dilate_ftr(im(CV_8UC3), kn51()); |
| 359 | +} |
| 360 | +TEST(ximgproc_StMorph_dev, big_stEerode) |
| 361 | +{ |
| 362 | + dilate_ftr(im(CV_8UC3), kn51()); |
| 363 | +} |
| 364 | +TEST(ximgproc_StMorph_dev, big_cvDilate) |
| 365 | +{ |
| 366 | + cvDilate(im(CV_8UC3), kn51()); |
| 367 | +} |
| 368 | +TEST(ximgproc_StMorph_dev, big_cvErode) |
| 369 | +{ |
| 370 | + cvErode(im(CV_8UC3), kn51()); |
| 371 | +} |
| 372 | + |
| 373 | +#pragma endregion |
| 374 | + |
341 | 375 | }} // ::opencv_test::
|
0 commit comments