42 #include <visp3/core/vpConfig.h>
44 #if defined(VISP_HAVE_CATCH2)
45 #define CATCH_CONFIG_RUNNER
48 #include <visp3/core/vpImageFilter.h>
49 #include <visp3/core/vpImageMorphology.h>
51 TEST_CASE(
"Binary image morphology",
"[image_morphology]")
53 unsigned char image_data[8 * 16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0,
55 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0,
56 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1,
57 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
58 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
59 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
60 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
66 SECTION(
"4-connexity")
73 common_tools::imageDilatationRef(I_morpho_ref, connexity);
75 vpImageMorphology::dilatation<unsigned char>(I_morpho, connexity);
77 CHECK((I_morpho_ref == I_morpho_tpl));
78 CHECK((I_morpho_ref == I_morpho));
80 SECTION(
"8-connexity")
87 common_tools::imageDilatationRef(I_morpho_ref, connexity);
89 vpImageMorphology::dilatation<unsigned char>(I_morpho, connexity);
91 CHECK((I_morpho_ref == I_morpho_tpl));
92 CHECK((I_morpho_ref == I_morpho));
98 SECTION(
"4-connexity")
105 common_tools::imageErosionRef(I_morpho_ref, connexity);
107 vpImageMorphology::erosion<unsigned char>(I_morpho, connexity);
109 CHECK((I_morpho_ref == I_morpho_tpl));
110 CHECK((I_morpho_ref == I_morpho));
113 SECTION(
"8-connexity")
120 common_tools::imageErosionRef(I_morpho_ref, connexity);
122 vpImageMorphology::erosion<unsigned char>(I_morpho, connexity);
124 CHECK((I_morpho_ref == I_morpho_tpl));
125 CHECK((I_morpho_ref == I_morpho));
128 SECTION(
"8-connexity-size5")
131 I_dilatation_ref[0][0] = 0;
132 I_dilatation_ref[0][1] = 0;
133 I_dilatation_ref[0][2] = 0;
134 I_dilatation_ref[6][12] = 0;
135 I_dilatation_ref[7][12] = 0;
144 CHECK((I_dilatation_ref == I_dilatation));
145 CHECK((I_erosion_ref == I_erosion));
149 SECTION(
"Matlab reference")
151 SECTION(
"4-connexity")
155 vpImageMorphology::dilatation<unsigned char>(I_dilatation, connexity);
157 unsigned char image_data_dilatation[8 * 16] = {
158 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,
159 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
160 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1,
161 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 };
163 CHECK((I_dilatation_ref == I_dilatation));
166 vpImageMorphology::erosion<unsigned char>(I_erosion, connexity);
168 unsigned char image_data_erosion[8 * 16] = {
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0,
170 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1,
171 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
172 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
174 CHECK((I_erosion_ref == I_erosion));
177 SECTION(
"8-connexity")
181 vpImageMorphology::dilatation<unsigned char>(I_dilatation, connexity);
183 unsigned char image_data_dilatation[8 * 16] = {
184 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
185 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
186 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1,
187 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1 };
189 CHECK((I_dilatation_ref == I_dilatation));
192 vpImageMorphology::erosion<unsigned char>(I_erosion, connexity);
194 unsigned char image_data_erosion[8 * 16] = {
195 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0,
196 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
197 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
198 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1 };
200 CHECK((I_erosion_ref == I_erosion));
205 TEST_CASE(
"Gray image morphology",
"[image_morphology]")
208 common_tools::magicSquare(I, 17);
210 SECTION(
"Dilatation")
212 SECTION(
"4-connexity")
218 common_tools::imageDilatationRef(I_morpho_ref, connexity);
219 vpImageMorphology::dilatation<unsigned char>(I_morpho, connexity);
221 CHECK((I_morpho_ref == I_morpho));
223 SECTION(
"8-connexity")
229 common_tools::imageDilatationRef(I_morpho_ref, connexity);
230 vpImageMorphology::dilatation<unsigned char>(I_morpho, connexity);
232 CHECK((I_morpho_ref == I_morpho));
235 SECTION(
"8-connexity-size5")
239 unsigned char count = 1;
240 for (
int r = 0; r < 12; r++) {
241 for (
int c = 0; c < 12; c++) {
242 I_morpho[r][c] = count;
246 unsigned char image_data_dilatation[12 * 12] = {
247 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 36,
248 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 48, 48,
249 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 60, 60,
250 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 72, 72,
251 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 84, 84,
252 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 96, 96,
253 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 108, 108,
254 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 120, 120,
255 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 132, 132,
256 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 144, 144,
257 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 144, 144,
258 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 144, 144 };
261 vpImageMorphology::dilatation<unsigned char>(I_morpho, size);
263 CHECK((I_dilatation_ref == I_morpho));
269 SECTION(
"4-connexity")
275 common_tools::imageErosionRef(I_morpho_ref, connexity);
276 vpImageMorphology::erosion<unsigned char>(I_morpho, connexity);
278 CHECK((I_morpho_ref == I_morpho));
281 SECTION(
"8-connexity")
287 common_tools::imageErosionRef(I_morpho_ref, connexity);
288 vpImageMorphology::erosion<unsigned char>(I_morpho, connexity);
290 CHECK((I_morpho_ref == I_morpho));
293 SECTION(
"8-connexity-size5")
297 unsigned char count = 1;
298 for (
int r = 0; r < 12; r++) {
299 for (
int c = 0; c < 12; c++) {
300 I_morpho[r][c] = count;
304 unsigned char image_data_erosion[12 * 12] = {
305 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
306 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
307 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
308 13, 13, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
309 25, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
310 37, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
311 49, 49, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
312 61, 61, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
313 73, 73, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
314 85, 85, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
315 97, 97, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
316 109, 109, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118 };
319 vpImageMorphology::erosion<unsigned char>(I_morpho, size);
321 CHECK((I_erosion_ref == I_morpho));
325 SECTION(
"Matlab reference")
327 SECTION(
"4-connexity")
331 vpImageMorphology::dilatation<unsigned char>(I_dilatation, connexity);
333 unsigned char image_data_dilatation[17 * 17] = {
334 174, 193, 212, 231, 250, 255, 255, 255, 255, 39, 58, 77, 96, 115, 134, 153, 154, 192, 211, 230, 249,
335 255, 255, 255, 255, 38, 57, 76, 95, 114, 133, 152, 170, 172, 210, 229, 248, 255, 255, 255, 255, 37,
336 56, 75, 94, 113, 132, 151, 170, 172, 190, 228, 247, 255, 255, 255, 255, 36, 55, 74, 93, 112, 131,
337 150, 169, 187, 190, 208, 246, 255, 255, 255, 255, 51, 54, 73, 92, 111, 130, 149, 168, 187, 189, 208,
338 226, 255, 255, 255, 255, 51, 53, 72, 91, 110, 129, 148, 167, 186, 204, 207, 226, 244, 255, 255, 255,
339 50, 68, 71, 90, 109, 128, 147, 166, 185, 204, 206, 225, 244, 255, 255, 255, 49, 68, 70, 89, 108,
340 127, 146, 165, 184, 203, 221, 224, 243, 255, 255, 255, 48, 67, 85, 88, 107, 126, 145, 164, 183, 202,
341 221, 223, 242, 255, 255, 255, 47, 66, 85, 87, 106, 125, 144, 163, 182, 201, 220, 238, 241, 255, 255,
342 255, 255, 65, 84, 102, 105, 124, 143, 162, 181, 200, 219, 238, 240, 255, 255, 255, 255, 45, 83, 102,
343 104, 123, 142, 161, 180, 199, 218, 237, 255, 255, 255, 255, 255, 45, 63, 101, 119, 122, 141, 160, 179,
344 198, 217, 236, 255, 255, 255, 255, 255, 44, 63, 81, 119, 121, 140, 159, 178, 197, 216, 235, 254, 255,
345 255, 255, 255, 43, 62, 81, 99, 136, 139, 158, 177, 196, 215, 234, 253, 255, 255, 255, 255, 42, 61,
346 80, 99, 117, 138, 157, 176, 195, 214, 233, 252, 255, 255, 255, 255, 41, 60, 79, 98, 117, 135, 156,
347 175, 194, 213, 232, 251, 255, 255, 255, 255, 40, 59, 78, 97, 116, 135, 135 };
349 CHECK((I_dilatation_ref == I_dilatation));
352 vpImageMorphology::erosion<unsigned char>(I_erosion, connexity);
354 unsigned char image_data_erosion[17 * 17] = {
355 174, 174, 193, 212, 231, 250, 255, 255, 38, 39, 39, 58, 77, 96, 115, 134, 153, 174, 192, 211, 230,
356 249, 255, 255, 37, 38, 38, 57, 76, 95, 114, 133, 152, 154, 192, 210, 229, 248, 255, 255, 36, 37,
357 37, 56, 75, 94, 113, 132, 151, 170, 172, 210, 228, 247, 255, 255, 36, 36, 36, 55, 74, 93, 112,
358 131, 150, 169, 172, 190, 228, 246, 255, 255, 51, 51, 36, 54, 73, 92, 111, 130, 149, 168, 187, 189,
359 208, 246, 255, 255, 50, 51, 51, 53, 72, 91, 110, 129, 148, 167, 186, 189, 207, 226, 255, 255, 49,
360 50, 50, 53, 71, 90, 109, 128, 147, 166, 185, 204, 206, 225, 244, 255, 48, 49, 49, 68, 70, 89,
361 108, 127, 146, 165, 184, 203, 206, 224, 243, 255, 47, 48, 48, 67, 70, 88, 107, 126, 145, 164, 183,
362 202, 221, 223, 242, 255, 255, 47, 47, 66, 85, 87, 106, 125, 144, 163, 182, 201, 220, 223, 241, 255,
363 255, 45, 47, 65, 84, 87, 105, 124, 143, 162, 181, 200, 219, 238, 240, 255, 255, 45, 45, 65, 83,
364 102, 104, 123, 142, 161, 180, 199, 218, 237, 240, 255, 255, 44, 45, 45, 83, 101, 104, 122, 141, 160,
365 179, 198, 217, 236, 255, 255, 255, 43, 44, 44, 63, 101, 119, 121, 140, 159, 178, 197, 216, 235, 254,
366 255, 255, 42, 43, 43, 62, 81, 119, 121, 139, 158, 177, 196, 215, 234, 253, 255, 255, 41, 42, 42,
367 61, 80, 99, 136, 138, 157, 176, 195, 214, 233, 252, 255, 255, 40, 41, 41, 60, 79, 98, 117, 138,
368 156, 175, 194, 213, 232, 251, 255, 255, 40, 40, 40, 59, 78, 97, 116, 135 };
370 CHECK((I_erosion_ref == I_erosion));
373 SECTION(
"8-connexity")
377 vpImageMorphology::dilatation<unsigned char>(I_dilatation, connexity);
379 unsigned char image_data_dilatation[17 * 17] = {
380 192, 211, 230, 249, 255, 255, 255, 255, 255, 57, 76, 95, 114, 133, 152, 154, 154, 210, 229, 248, 255,
381 255, 255, 255, 255, 255, 75, 94, 113, 132, 151, 170, 172, 172, 228, 247, 255, 255, 255, 255, 255, 255,
382 74, 93, 112, 131, 150, 169, 171, 190, 190, 246, 255, 255, 255, 255, 255, 255, 73, 92, 111, 130, 149,
383 168, 187, 189, 208, 208, 255, 255, 255, 255, 255, 255, 72, 91, 110, 129, 148, 167, 186, 188, 207, 226,
384 226, 255, 255, 255, 255, 255, 71, 90, 109, 128, 147, 166, 185, 204, 206, 225, 244, 244, 255, 255, 255,
385 255, 70, 89, 108, 127, 146, 165, 184, 203, 205, 224, 243, 255, 255, 255, 255, 255, 69, 88, 107, 126,
386 145, 164, 183, 202, 221, 223, 242, 255, 255, 255, 255, 255, 85, 87, 106, 125, 144, 163, 182, 201, 220,
387 222, 241, 255, 255, 255, 255, 65, 84, 86, 105, 124, 143, 162, 181, 200, 219, 238, 240, 255, 255, 255,
388 255, 255, 83, 102, 104, 123, 142, 161, 180, 199, 218, 237, 239, 255, 255, 255, 255, 255, 255, 101, 103,
389 122, 141, 160, 179, 198, 217, 236, 255, 255, 255, 255, 255, 255, 255, 63, 119, 121, 140, 159, 178, 197,
390 216, 235, 254, 255, 255, 255, 255, 255, 255, 81, 81, 120, 139, 158, 177, 196, 215, 234, 253, 255, 255,
391 255, 255, 255, 255, 80, 99, 99, 138, 157, 176, 195, 214, 233, 252, 255, 255, 255, 255, 255, 255, 79,
392 98, 117, 117, 156, 175, 194, 213, 232, 251, 255, 255, 255, 255, 255, 255, 78, 97, 116, 135, 135, 156,
393 175, 194, 213, 232, 251, 255, 255, 255, 255, 255, 59, 78, 97, 116, 135, 135 };
395 CHECK((I_dilatation_ref == I_dilatation));
398 vpImageMorphology::erosion<unsigned char>(I_erosion, connexity);
400 unsigned char image_data_erosion[17 * 17] = {
401 192, 192, 211, 230, 249, 255, 255, 255, 57, 57, 57, 76, 95, 114, 133, 152, 154, 192, 192, 211, 230,
402 249, 255, 255, 74, 57, 57, 57, 76, 95, 114, 133, 152, 154, 210, 210, 229, 248, 255, 255, 73, 73,
403 73, 74, 75, 94, 113, 132, 151, 170, 172, 228, 228, 247, 255, 255, 72, 72, 72, 73, 74, 93, 112,
404 131, 150, 169, 171, 190, 246, 246, 255, 255, 71, 71, 71, 72, 73, 92, 111, 130, 149, 168, 187, 189,
405 208, 255, 255, 255, 70, 70, 70, 71, 72, 91, 110, 129, 148, 167, 186, 188, 207, 226, 255, 255, 69,
406 69, 69, 70, 71, 90, 109, 128, 147, 166, 185, 204, 206, 225, 244, 255, 85, 69, 69, 69, 70, 89,
407 108, 127, 146, 165, 184, 203, 205, 224, 243, 255, 65, 65, 69, 69, 69, 88, 107, 126, 145, 164, 183,
408 202, 221, 223, 242, 255, 255, 65, 65, 84, 85, 87, 106, 125, 144, 163, 182, 201, 220, 222, 241, 255,
409 255, 255, 65, 65, 84, 86, 105, 124, 143, 162, 181, 200, 219, 238, 240, 255, 255, 63, 63, 83, 83,
410 102, 104, 123, 142, 161, 180, 199, 218, 237, 239, 255, 255, 81, 63, 63, 101, 101, 103, 122, 141, 160,
411 179, 198, 217, 236, 255, 255, 255, 80, 80, 63, 63, 119, 119, 121, 140, 159, 178, 197, 216, 235, 254,
412 255, 255, 79, 79, 79, 80, 81, 120, 120, 139, 158, 177, 196, 215, 234, 253, 255, 255, 78, 78, 78,
413 79, 80, 99, 138, 138, 157, 176, 195, 214, 233, 252, 255, 255, 59, 59, 59, 78, 79, 98, 117, 156,
414 156, 175, 194, 213, 232, 251, 255, 255, 255, 59, 59, 59, 78, 97, 116, 135 };
416 CHECK((I_erosion_ref == I_erosion));
421 int main(
int argc,
char *argv[])
423 Catch::Session session;
426 session.applyCommandLine(argc, argv);
428 int numFailed = session.run();
436 int main() {
return EXIT_SUCCESS; }
static void dilatation(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
static void erosion(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)