Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testImageMorphology.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Test image morphology.
33 *
34*****************************************************************************/
35
42#include <visp3/core/vpConfig.h>
43
44#if defined(VISP_HAVE_CATCH2)
45#define CATCH_CONFIG_RUNNER
46#include "common.hpp"
47#include <catch.hpp>
48#include <visp3/core/vpImageFilter.h>
49#include <visp3/core/vpImageMorphology.h>
50
51TEST_CASE("Binary image morphology", "[image_morphology]")
52{
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 };
61
62 vpImage<unsigned char> I(image_data, 8, 16, true);
63
64 SECTION("Dilatation")
65 {
66 SECTION("4-connexity")
67 {
69 vpImage<unsigned char> I_morpho_ref = I;
70 vpImage<unsigned char> I_morpho_tpl = I;
71 vpImage<unsigned char> I_morpho = I;
72
73 common_tools::imageDilatationRef(I_morpho_ref, connexity);
74 vpImageMorphology::dilatation(I_morpho_tpl, (unsigned char)1, (unsigned char)0, connexity);
75 vpImageMorphology::dilatation<unsigned char>(I_morpho, connexity);
76
77 CHECK((I_morpho_ref == I_morpho_tpl));
78 CHECK((I_morpho_ref == I_morpho));
79 }
80 SECTION("8-connexity")
81 {
83 vpImage<unsigned char> I_morpho_ref = I;
84 vpImage<unsigned char> I_morpho_tpl = I;
85 vpImage<unsigned char> I_morpho = I;
86
87 common_tools::imageDilatationRef(I_morpho_ref, connexity);
88 vpImageMorphology::dilatation(I_morpho_tpl, (unsigned char)1, (unsigned char)0, connexity);
89 vpImageMorphology::dilatation<unsigned char>(I_morpho, connexity);
90
91 CHECK((I_morpho_ref == I_morpho_tpl));
92 CHECK((I_morpho_ref == I_morpho));
93 }
94 }
95
96 SECTION("Erosion")
97 {
98 SECTION("4-connexity")
99 {
101 vpImage<unsigned char> I_morpho_ref = I;
102 vpImage<unsigned char> I_morpho_tpl = I;
103 vpImage<unsigned char> I_morpho = I;
104
105 common_tools::imageErosionRef(I_morpho_ref, connexity);
106 vpImageMorphology::erosion(I_morpho_tpl, (unsigned char)1, (unsigned char)0, connexity);
107 vpImageMorphology::erosion<unsigned char>(I_morpho, connexity);
108
109 CHECK((I_morpho_ref == I_morpho_tpl));
110 CHECK((I_morpho_ref == I_morpho));
111 }
112
113 SECTION("8-connexity")
114 {
116 vpImage<unsigned char> I_morpho_ref = I;
117 vpImage<unsigned char> I_morpho_tpl = I;
118 vpImage<unsigned char> I_morpho = I;
119
120 common_tools::imageErosionRef(I_morpho_ref, connexity);
121 vpImageMorphology::erosion(I_morpho_tpl, (unsigned char)1, (unsigned char)0, connexity);
122 vpImageMorphology::erosion<unsigned char>(I_morpho, connexity);
123
124 CHECK((I_morpho_ref == I_morpho_tpl));
125 CHECK((I_morpho_ref == I_morpho));
126 }
127 }
128
129 SECTION("Matlab reference")
130 {
131 SECTION("4-connexity")
132 {
134 vpImage<unsigned char> I_dilatation = I;
135 vpImageMorphology::dilatation<unsigned char>(I_dilatation, connexity);
136
137 unsigned char image_data_dilatation[8 * 16] = {
138 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,
139 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,
140 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,
141 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 };
142 vpImage<unsigned char> I_dilatation_ref(image_data_dilatation, 8, 16, true);
143 CHECK((I_dilatation_ref == I_dilatation));
144
145 vpImage<unsigned char> I_erosion = I_dilatation;
146 vpImageMorphology::erosion<unsigned char>(I_erosion, connexity);
147
148 unsigned char image_data_erosion[8 * 16] = {
149 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,
150 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,
151 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,
152 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 };
153 vpImage<unsigned char> I_erosion_ref(image_data_erosion, 8, 16, true);
154 CHECK((I_erosion_ref == I_erosion));
155 }
156
157 SECTION("8-connexity")
158 {
160 vpImage<unsigned char> I_dilatation = I;
161 vpImageMorphology::dilatation<unsigned char>(I_dilatation, connexity);
162
163 unsigned char image_data_dilatation[8 * 16] = {
164 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,
165 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,
166 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,
167 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 };
168 vpImage<unsigned char> I_dilatation_ref(image_data_dilatation, 8, 16, true);
169 CHECK((I_dilatation_ref == I_dilatation));
170
171 vpImage<unsigned char> I_erosion = I_dilatation;
172 vpImageMorphology::erosion<unsigned char>(I_erosion, connexity);
173
174 unsigned char image_data_erosion[8 * 16] = {
175 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,
176 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,
177 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,
178 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 };
179 vpImage<unsigned char> I_erosion_ref(image_data_erosion, 8, 16, true);
180 CHECK((I_erosion_ref == I_erosion));
181 }
182 }
183}
184
185TEST_CASE("Gray image morphology", "[image_morphology]")
186{
188 common_tools::magicSquare(I, 17);
189
190 SECTION("Dilatation")
191 {
192 SECTION("4-connexity")
193 {
195 vpImage<unsigned char> I_morpho_ref = I;
196 vpImage<unsigned char> I_morpho = I;
197
198 common_tools::imageDilatationRef(I_morpho_ref, connexity);
199 vpImageMorphology::dilatation<unsigned char>(I_morpho, connexity);
200
201 CHECK((I_morpho_ref == I_morpho));
202 }
203 SECTION("8-connexity")
204 {
206 vpImage<unsigned char> I_morpho_ref = I;
207 vpImage<unsigned char> I_morpho = I;
208
209 common_tools::imageDilatationRef(I_morpho_ref, connexity);
210 vpImageMorphology::dilatation<unsigned char>(I_morpho, connexity);
211
212 CHECK((I_morpho_ref == I_morpho));
213 }
214 }
215
216 SECTION("Erosion")
217 {
218 SECTION("4-connexity")
219 {
221 vpImage<unsigned char> I_morpho_ref = I;
222 vpImage<unsigned char> I_morpho = I;
223
224 common_tools::imageErosionRef(I_morpho_ref, connexity);
225 vpImageMorphology::erosion<unsigned char>(I_morpho, connexity);
226
227 CHECK((I_morpho_ref == I_morpho));
228 }
229
230 SECTION("8-connexity")
231 {
233 vpImage<unsigned char> I_morpho_ref = I;
234 vpImage<unsigned char> I_morpho = I;
235
236 common_tools::imageErosionRef(I_morpho_ref, connexity);
237 vpImageMorphology::erosion<unsigned char>(I_morpho, connexity);
238
239 CHECK((I_morpho_ref == I_morpho));
240 }
241 }
242
243 SECTION("Matlab reference")
244 {
245 SECTION("4-connexity")
246 {
248 vpImage<unsigned char> I_dilatation = I;
249 vpImageMorphology::dilatation<unsigned char>(I_dilatation, connexity);
250
251 unsigned char image_data_dilatation[17 * 17] = {
252 174, 193, 212, 231, 250, 255, 255, 255, 255, 39, 58, 77, 96, 115, 134, 153, 154, 192, 211, 230, 249,
253 255, 255, 255, 255, 38, 57, 76, 95, 114, 133, 152, 170, 172, 210, 229, 248, 255, 255, 255, 255, 37,
254 56, 75, 94, 113, 132, 151, 170, 172, 190, 228, 247, 255, 255, 255, 255, 36, 55, 74, 93, 112, 131,
255 150, 169, 187, 190, 208, 246, 255, 255, 255, 255, 51, 54, 73, 92, 111, 130, 149, 168, 187, 189, 208,
256 226, 255, 255, 255, 255, 51, 53, 72, 91, 110, 129, 148, 167, 186, 204, 207, 226, 244, 255, 255, 255,
257 50, 68, 71, 90, 109, 128, 147, 166, 185, 204, 206, 225, 244, 255, 255, 255, 49, 68, 70, 89, 108,
258 127, 146, 165, 184, 203, 221, 224, 243, 255, 255, 255, 48, 67, 85, 88, 107, 126, 145, 164, 183, 202,
259 221, 223, 242, 255, 255, 255, 47, 66, 85, 87, 106, 125, 144, 163, 182, 201, 220, 238, 241, 255, 255,
260 255, 255, 65, 84, 102, 105, 124, 143, 162, 181, 200, 219, 238, 240, 255, 255, 255, 255, 45, 83, 102,
261 104, 123, 142, 161, 180, 199, 218, 237, 255, 255, 255, 255, 255, 45, 63, 101, 119, 122, 141, 160, 179,
262 198, 217, 236, 255, 255, 255, 255, 255, 44, 63, 81, 119, 121, 140, 159, 178, 197, 216, 235, 254, 255,
263 255, 255, 255, 43, 62, 81, 99, 136, 139, 158, 177, 196, 215, 234, 253, 255, 255, 255, 255, 42, 61,
264 80, 99, 117, 138, 157, 176, 195, 214, 233, 252, 255, 255, 255, 255, 41, 60, 79, 98, 117, 135, 156,
265 175, 194, 213, 232, 251, 255, 255, 255, 255, 40, 59, 78, 97, 116, 135, 135 };
266 vpImage<unsigned char> I_dilatation_ref(image_data_dilatation, 17, 17, true);
267 CHECK((I_dilatation_ref == I_dilatation));
268
269 vpImage<unsigned char> I_erosion = I_dilatation;
270 vpImageMorphology::erosion<unsigned char>(I_erosion, connexity);
271
272 unsigned char image_data_erosion[17 * 17] = {
273 174, 174, 193, 212, 231, 250, 255, 255, 38, 39, 39, 58, 77, 96, 115, 134, 153, 174, 192, 211, 230,
274 249, 255, 255, 37, 38, 38, 57, 76, 95, 114, 133, 152, 154, 192, 210, 229, 248, 255, 255, 36, 37,
275 37, 56, 75, 94, 113, 132, 151, 170, 172, 210, 228, 247, 255, 255, 36, 36, 36, 55, 74, 93, 112,
276 131, 150, 169, 172, 190, 228, 246, 255, 255, 51, 51, 36, 54, 73, 92, 111, 130, 149, 168, 187, 189,
277 208, 246, 255, 255, 50, 51, 51, 53, 72, 91, 110, 129, 148, 167, 186, 189, 207, 226, 255, 255, 49,
278 50, 50, 53, 71, 90, 109, 128, 147, 166, 185, 204, 206, 225, 244, 255, 48, 49, 49, 68, 70, 89,
279 108, 127, 146, 165, 184, 203, 206, 224, 243, 255, 47, 48, 48, 67, 70, 88, 107, 126, 145, 164, 183,
280 202, 221, 223, 242, 255, 255, 47, 47, 66, 85, 87, 106, 125, 144, 163, 182, 201, 220, 223, 241, 255,
281 255, 45, 47, 65, 84, 87, 105, 124, 143, 162, 181, 200, 219, 238, 240, 255, 255, 45, 45, 65, 83,
282 102, 104, 123, 142, 161, 180, 199, 218, 237, 240, 255, 255, 44, 45, 45, 83, 101, 104, 122, 141, 160,
283 179, 198, 217, 236, 255, 255, 255, 43, 44, 44, 63, 101, 119, 121, 140, 159, 178, 197, 216, 235, 254,
284 255, 255, 42, 43, 43, 62, 81, 119, 121, 139, 158, 177, 196, 215, 234, 253, 255, 255, 41, 42, 42,
285 61, 80, 99, 136, 138, 157, 176, 195, 214, 233, 252, 255, 255, 40, 41, 41, 60, 79, 98, 117, 138,
286 156, 175, 194, 213, 232, 251, 255, 255, 40, 40, 40, 59, 78, 97, 116, 135 };
287 vpImage<unsigned char> I_erosion_ref(image_data_erosion, 17, 17, true);
288 CHECK((I_erosion_ref == I_erosion));
289 }
290
291 SECTION("8-connexity")
292 {
294 vpImage<unsigned char> I_dilatation = I;
295 vpImageMorphology::dilatation<unsigned char>(I_dilatation, connexity);
296
297 unsigned char image_data_dilatation[17 * 17] = {
298 192, 211, 230, 249, 255, 255, 255, 255, 255, 57, 76, 95, 114, 133, 152, 154, 154, 210, 229, 248, 255,
299 255, 255, 255, 255, 255, 75, 94, 113, 132, 151, 170, 172, 172, 228, 247, 255, 255, 255, 255, 255, 255,
300 74, 93, 112, 131, 150, 169, 171, 190, 190, 246, 255, 255, 255, 255, 255, 255, 73, 92, 111, 130, 149,
301 168, 187, 189, 208, 208, 255, 255, 255, 255, 255, 255, 72, 91, 110, 129, 148, 167, 186, 188, 207, 226,
302 226, 255, 255, 255, 255, 255, 71, 90, 109, 128, 147, 166, 185, 204, 206, 225, 244, 244, 255, 255, 255,
303 255, 70, 89, 108, 127, 146, 165, 184, 203, 205, 224, 243, 255, 255, 255, 255, 255, 69, 88, 107, 126,
304 145, 164, 183, 202, 221, 223, 242, 255, 255, 255, 255, 255, 85, 87, 106, 125, 144, 163, 182, 201, 220,
305 222, 241, 255, 255, 255, 255, 65, 84, 86, 105, 124, 143, 162, 181, 200, 219, 238, 240, 255, 255, 255,
306 255, 255, 83, 102, 104, 123, 142, 161, 180, 199, 218, 237, 239, 255, 255, 255, 255, 255, 255, 101, 103,
307 122, 141, 160, 179, 198, 217, 236, 255, 255, 255, 255, 255, 255, 255, 63, 119, 121, 140, 159, 178, 197,
308 216, 235, 254, 255, 255, 255, 255, 255, 255, 81, 81, 120, 139, 158, 177, 196, 215, 234, 253, 255, 255,
309 255, 255, 255, 255, 80, 99, 99, 138, 157, 176, 195, 214, 233, 252, 255, 255, 255, 255, 255, 255, 79,
310 98, 117, 117, 156, 175, 194, 213, 232, 251, 255, 255, 255, 255, 255, 255, 78, 97, 116, 135, 135, 156,
311 175, 194, 213, 232, 251, 255, 255, 255, 255, 255, 59, 78, 97, 116, 135, 135 };
312 vpImage<unsigned char> I_dilatation_ref(image_data_dilatation, 17, 17, true);
313 CHECK((I_dilatation_ref == I_dilatation));
314
315 vpImage<unsigned char> I_erosion = I_dilatation;
316 vpImageMorphology::erosion<unsigned char>(I_erosion, connexity);
317
318 unsigned char image_data_erosion[17 * 17] = {
319 192, 192, 211, 230, 249, 255, 255, 255, 57, 57, 57, 76, 95, 114, 133, 152, 154, 192, 192, 211, 230,
320 249, 255, 255, 74, 57, 57, 57, 76, 95, 114, 133, 152, 154, 210, 210, 229, 248, 255, 255, 73, 73,
321 73, 74, 75, 94, 113, 132, 151, 170, 172, 228, 228, 247, 255, 255, 72, 72, 72, 73, 74, 93, 112,
322 131, 150, 169, 171, 190, 246, 246, 255, 255, 71, 71, 71, 72, 73, 92, 111, 130, 149, 168, 187, 189,
323 208, 255, 255, 255, 70, 70, 70, 71, 72, 91, 110, 129, 148, 167, 186, 188, 207, 226, 255, 255, 69,
324 69, 69, 70, 71, 90, 109, 128, 147, 166, 185, 204, 206, 225, 244, 255, 85, 69, 69, 69, 70, 89,
325 108, 127, 146, 165, 184, 203, 205, 224, 243, 255, 65, 65, 69, 69, 69, 88, 107, 126, 145, 164, 183,
326 202, 221, 223, 242, 255, 255, 65, 65, 84, 85, 87, 106, 125, 144, 163, 182, 201, 220, 222, 241, 255,
327 255, 255, 65, 65, 84, 86, 105, 124, 143, 162, 181, 200, 219, 238, 240, 255, 255, 63, 63, 83, 83,
328 102, 104, 123, 142, 161, 180, 199, 218, 237, 239, 255, 255, 81, 63, 63, 101, 101, 103, 122, 141, 160,
329 179, 198, 217, 236, 255, 255, 255, 80, 80, 63, 63, 119, 119, 121, 140, 159, 178, 197, 216, 235, 254,
330 255, 255, 79, 79, 79, 80, 81, 120, 120, 139, 158, 177, 196, 215, 234, 253, 255, 255, 78, 78, 78,
331 79, 80, 99, 138, 138, 157, 176, 195, 214, 233, 252, 255, 255, 59, 59, 59, 78, 79, 98, 117, 156,
332 156, 175, 194, 213, 232, 251, 255, 255, 255, 59, 59, 59, 78, 97, 116, 135 };
333 vpImage<unsigned char> I_erosion_ref(image_data_erosion, 17, 17, true);
334 CHECK((I_erosion_ref == I_erosion));
335 }
336 }
337}
338
339int main(int argc, char *argv [])
340{
341 Catch::Session session; // There must be exactly one instance
342
343 // Let Catch (using Clara) parse the command line
344 session.applyCommandLine(argc, argv);
345
346 int numFailed = session.run();
347
348 // numFailed is clamped to 255 as some unices only use the lower 8 bits.
349 // This clamping has already been applied, so just return it here
350 // You can also do any post run clean-up here
351 return numFailed;
352}
353#else
354int main() { return EXIT_SUCCESS; }
355#endif
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)
Definition of the vpImage class member functions.
Definition vpImage.h:135