Debian Codegraph @

libs/img/src/CImage_unittest.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, Individual contributors, see AUTHORS file |
6 | See: https://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See: https://www.mrpt.org/License |
8 +------------------------------------------------------------------------+ */
9
10 #include <CTraitsTest.h>
11 #include <gtest/gtest.h>
12 #include <mrpt/img/CImage.h>
13 #include <mrpt/img/TColor.h>
14 #include <mrpt/io/CMemoryStream.h>
15 #include <mrpt/math/CMatrixDynamic.h>
16 #include <mrpt/random.h>
17 #include <mrpt/serialization/CArchive.h>
18 #include <mrpt/system/filesystem.h>
19 #include <mrpt/system/memory.h>
20 #include <test_mrpt_common.h>
21
22 // Universal include for all versions of OpenCV
23 #include <mrpt/3rdparty/do_opencv_includes.h>
24
25 template class mrpt::CTraitsTest<mrpt::img::CImage>;
26
27 using namespace std::string_literals;
28 const auto tstImgFileColor =
29 mrpt::UNITTEST_BASEDIR() + "/samples/img_basic_example/frame_color.jpg"s;
30
31 // Generate random img:
32 namespace
33 {
34 void fillImagePseudoRandom(uint32_t seed, mrpt::img::CImage& img)
35 {
36 mrpt::random::Randomize(seed);
37 auto& rnd = mrpt::random::getRandomGenerator();
38
39 for (unsigned y = 0; y < img.getHeight(); y++)
40 {
41 for (unsigned x = 0; x < img.getWidth(); x++)
42 {
43 const uint8_t c = static_cast<uint8_t>(rnd.drawUniform32bit());
44 img.at<uint8_t>(x, y) = c;
45 }
46 }
47 }
48
49 // Expect images to be identical:
50 bool expect_identical(
51 const mrpt::img::CImage& a, const mrpt::img::CImage& b, const std::string& s = std::string())
52 {
53 EXPECT_EQ(a.getWidth(), b.getWidth());
54 EXPECT_EQ(a.getHeight(), b.getHeight());
55 for (unsigned int y = 0; y < a.getHeight(); y++)
56 for (unsigned int x = 0; x < a.getWidth(); x++)
57 {
58 EXPECT_EQ(a.at<uint8_t>(x, y), b.at<uint8_t>(x, y)) << s;
59 if (a.at<uint8_t>(x, y) != b.at<uint8_t>(x, y)) return false;
60 }
61 return true;
62 }
63 } // namespace
64
65 TEST(CImage, CtorDefault)
66 {
67 mrpt::img::CImage img;
68 EXPECT_TRUE(img.isEmpty());
69
70 // clang-format off
71 EXPECT_THROW([&img]() { auto b = img.isColor(); ((void)b); }(), std::exception);
72 EXPECT_THROW([&img]() { auto b = img.getWidth(); ((void)b); }(), std::exception);
73 EXPECT_THROW([&img]() { auto b = img.getHeight(); ((void)b);}(), std::exception);
74 // clang-format on
75
76 EXPECT_THROW(img.getPixelDepth(), std::exception);
77 }
78
79 #if MRPT_HAS_OPENCV
80
81 namespace
82 {
83 void CtorSized_gray(unsigned int w, unsigned int h)
84 {
85 using namespace mrpt::img;
86 CImage img(w, h, CH_GRAY);
87 EXPECT_EQ(img.getWidth(), w);
88 EXPECT_EQ(img.getHeight(), 48U);
89 EXPECT_EQ(img.getChannelCount(), 1U);
90 EXPECT_EQ(img.getPixelDepth(), PixelDepth::D8U);
91 EXPECT_FALSE(img.isColor());
92 }
93 } // namespace
94
95 TEST(CImage, CtorSized)
96 {
97 using namespace mrpt::img;
98 {
99 CImage img(64, 48, CH_RGB);
100 EXPECT_EQ(img.getWidth(), 64U);
101 EXPECT_EQ(img.getHeight(), 48U);
102 EXPECT_EQ(img.getChannelCount(), 3U);
103 EXPECT_EQ(img.getPixelDepth(), PixelDepth::D8U);
104 EXPECT_TRUE(img.isColor());
105 }
106 for (unsigned w = 64; w < 70; w++)
107 {
108 CtorSized_gray(w, 48);
109 }
110 }
111
112 TEST(CImage, GetSetPixel)
113 {
114 using namespace mrpt::img;
115 CImage img(20, 10, CH_GRAY);
116 img.setPixel(10, 2, TColor(0x80, 0x80, 0x80));
117 EXPECT_EQ(img.at<uint8_t>(10, 2), 0x80);
118 EXPECT_EQ(*img(10, 2), 0x80);
119
120 img.setPixel(11, 2, TColor(0x0, 0x0, 0x0));
121 EXPECT_EQ(img.at<uint8_t>(11, 2), 0x00);
122
123 img.setPixel(12, 2, TColor(0xff, 0xff, 0xff));
124 EXPECT_EQ(img.at<uint8_t>(12, 2), 0xff);
125
126 img.at<uint8_t>(13, 2) = 0x70;
127 EXPECT_EQ(img.at<uint8_t>(13, 2), 0x70);
128
129 auto* line = img.ptrLine<uint8_t>(5);
130 for (uint8_t i = 0; i < 20; i++)
131 {
132 line[i] = i;
133 EXPECT_EQ(img.at<uint8_t>(i, 5), i);
134 }
135
136 mrpt::math::CMatrixFloat M;
137 img.getAsMatrix(M, true, 0, 0, -1, -1, false /* dont normalize (0,1) */);
138 for (uint8_t i = 0; i < 20; i++)
139 {
140 EXPECT_NEAR(static_cast<double>(M(5, i)), i, 1e-8);
141 }
142 }
143
144 TEST(CImage, CopyMoveSwap)
145 {
146 using namespace mrpt::img;
147 {
148 CImage a(20, 10, CH_GRAY);
149 a.at<uint8_t>(1, 2) = 0x80;
150 // Shallow copy:
151 CImage b = a;
152 EXPECT_EQ(b.at<uint8_t>(1, 2), 0x80);
153
154 a.at<uint8_t>(1, 3) = 0x81;
155 EXPECT_EQ(b.at<uint8_t>(1, 3), 0x81);
156
157 // Deep copy:
158 CImage c = a.makeDeepCopy();
159 EXPECT_EQ(c.at<uint8_t>(1, 2), 0x80);
160
161 c.at<uint8_t>(1, 3) = 0x0;
162 a.at<uint8_t>(1, 3) = 0x81;
163 EXPECT_NE(c.at<uint8_t>(1, 3), 0x81);
164 }
165
166 {
167 CImage a(20, 10, CH_GRAY);
168 a.at<uint8_t>(1, 2) = 0x80;
169 // Shallow copy ctor:
170 CImage b(a, mrpt::img::SHALLOW_COPY);
171 EXPECT_EQ(b.at<uint8_t>(1, 2), 0x80);
172
173 a.at<uint8_t>(1, 3) = 0x81;
174 EXPECT_EQ(b.at<uint8_t>(1, 3), 0x81);
175
176 // Deep copy ctor:
177 CImage c(a, mrpt::img::DEEP_COPY);
178 EXPECT_EQ(c.at<uint8_t>(1, 2), 0x80);
179
180 c.at<uint8_t>(1, 3) = 0x0;
181 a.at<uint8_t>(1, 3) = 0x81;
182 EXPECT_NE(c.at<uint8_t>(1, 3), 0x81);
183 }
184
185 {
186 CImage a(20, 10, CH_GRAY);
187 a.at<uint8_t>(1, 2) = 0x80;
188 // Deep copy:
189 CImage b = a.makeDeepCopy();
190 EXPECT_EQ(b.at<uint8_t>(1, 2), 0x80);
191
192 a.clear();
193 a.resize(30, 30, CH_RGB);
194 b.at<uint8_t>(1, 3) = 0x0;
195 a.at<uint8_t>(1, 3) = 0x81;
196 EXPECT_NE(b.at<uint8_t>(1, 3), 0x81);
197 }
198
199 {
200 CImage a(20, 10, CH_GRAY);
201 a.at<uint8_t>(1, 2) = 0x80;
202 // move:
203 CImage b = std::move(a);
204 EXPECT_EQ(b.getWidth(), 20U);
205 EXPECT_EQ(b.getHeight(), 10U);
206 EXPECT_EQ(b.at<uint8_t>(1, 2), 0x80);
207 }
208
209 {
210 CImage a(20, 10, CH_GRAY), b;
211 a.at<uint8_t>(1, 2) = 0x80;
212 // swap:
213 a.swap(b);
214 EXPECT_EQ(b.getWidth(), 20U);
215 EXPECT_EQ(b.getHeight(), 10U);
216 EXPECT_EQ(b.at<uint8_t>(1, 2), 0x80);
217 }
218 }
219
220 TEST(CImage, ExternalImage)
221 {
222 using namespace mrpt::img;
223 {
224 CImage a;
225 a.setExternalStorage(tstImgFileColor);
226 // Test automatic load-on-the-fly:
227 EXPECT_EQ(a.getWidth(), 320U);
228 EXPECT_EQ(a.getHeight(), 240U);
229 }
230
231 {
232 CImage a;
233 a.setExternalStorage("./foo_61717181.png");
234 // Test exception on not found
235 EXPECT_THROW(
236 [&a]
237 {
238 bool w = a.getWidth();
239 ((void)w);
240 }(),
241 mrpt::img::CExceptionExternalImageNotFound);
242 }
243 }
244
245 TEST(CImage, ConvertGray)
246 {
247 using namespace mrpt::img;
248 {
249 CImage a;
250 bool load_ok = a.loadFromFile(tstImgFileColor);
251 EXPECT_TRUE(load_ok);
252
253 CImage b = a.grayscale();
254 EXPECT_EQ(b.getWidth(), a.getWidth());
255 EXPECT_EQ(b.getHeight(), a.getHeight());
256 EXPECT_FALSE(b.isColor());
257 }
258 }
259
260 TEST(CImage, CtorRefOrGray)
261 {
262 using namespace mrpt::img;
263 {
264 CImage a;
265 bool load_ok = a.loadFromFile(tstImgFileColor);
266 EXPECT_TRUE(load_ok);
267
268 const CImage b(a, FAST_REF_OR_CONVERT_TO_GRAY);
269 EXPECT_EQ(b.getWidth(), a.getWidth());
270 EXPECT_EQ(b.getHeight(), a.getHeight());
271 EXPECT_FALSE(b.isColor());
272 }
273 {
274 CImage a(20, 10, CH_GRAY);
275 a.at<uint8_t>(1, 2) = 0x80;
276
277 const CImage b(a, FAST_REF_OR_CONVERT_TO_GRAY);
278 EXPECT_EQ(b.getWidth(), a.getWidth());
279 EXPECT_EQ(b.getHeight(), a.getHeight());
280 EXPECT_FALSE(b.isColor());
281 EXPECT_EQ(b.at<uint8_t>(1, 2), 0x80);
282 }
283 }
284
285 TEST(CImage, HalfAndDouble)
286 {
287 using namespace mrpt::img;
288
289 CImage a(32, 10, CH_GRAY);
290 a.at<uint8_t>(0, 0) = 0x80;
291 a.at<uint8_t>(0, 1) = 0x80;
292 a.at<uint8_t>(1, 0) = 0x80;
293 a.at<uint8_t>(1, 1) = 0x80;
294
295 // Half:
296 {
297 const CImage imgH = a.scaleHalf(mrpt::img::IMG_INTERP_NN);
298 EXPECT_EQ(imgH.getWidth(), a.getWidth() / 2);
299 EXPECT_EQ(imgH.getHeight(), a.getHeight() / 2);
300 EXPECT_EQ(imgH.isColor(), a.isColor());
301 EXPECT_EQ(imgH.at<uint8_t>(0, 0), a.at<uint8_t>(0, 0));
302 }
303 // Double:
304 {
305 const CImage imgD = a.scaleDouble(mrpt::img::IMG_INTERP_NN);
306 EXPECT_EQ(imgD.getWidth(), a.getWidth() * 2);
307 EXPECT_EQ(imgD.getHeight(), a.getHeight() * 2);
308 EXPECT_EQ(imgD.isColor(), a.isColor());
309 }
310 }
311 TEST(CImage, getChannelsOrder)
312 {
313 using namespace mrpt::img;
314 {
315 CImage a;
316 bool load_ok = a.loadFromFile(tstImgFileColor);
317 EXPECT_TRUE(load_ok);
318 EXPECT_EQ(std::string("BGR"), a.getChannelsOrder());
319 }
320 {
321 CImage a(32, 10, CH_GRAY);
322 EXPECT_EQ(std::string("GRAY"), a.getChannelsOrder());
323 }
324 }
325
326 TEST(CImage, ChangeCvMatCopies)
327 {
328 using namespace mrpt::img;
329
330 {
331 CImage a(20, 10, CH_GRAY);
332 a.at<uint8_t>(1, 2) = 0x80;
333 // change shallow copy:
334 cv::Mat m = a.asCvMat<cv::Mat>(SHALLOW_COPY);
335 m.at<uint8_t>(2, 1) = 0x70;
336 // Expect change in source:
337 EXPECT_EQ(a.at<uint8_t>(1, 2), 0x70);
338
339 // size:
340 cv::Mat& m2 = a.asCvMatRef();
341 cv::Mat& m3 = a.asCvMatRef();
342 EXPECT_EQ(&m2, &m3);
343
344 m2 = cv::Mat(40, 40, CV_8UC1);
345
346 cv::Mat& m4 = a.asCvMatRef();
347 EXPECT_EQ(&m2, &m4);
348
349 EXPECT_EQ(a.getWidth(), 40U);
350 EXPECT_EQ(a.getHeight(), 40U);
351 }
352 {
353 CImage a(20, 10, CH_GRAY);
354 a.at<uint8_t>(1, 2) = 0x80;
355 // change deep copy:
356 cv::Mat m = a.asCvMat<cv::Mat>(DEEP_COPY);
357 m.at<uint8_t>(2, 1) = 0x70;
358 // Expect NO change in source:
359 EXPECT_EQ(a.at<uint8_t>(1, 2), 0x80);
360
361 // size:
362 m = cv::Mat(40, 40, CV_8UC1);
363 EXPECT_EQ(a.getWidth(), 20U);
364 EXPECT_EQ(a.getHeight(), 10U);
365 }
366 }
367
368 TEST(CImage, ScaleImage)
369 {
370 using namespace mrpt::img;
371 CImage a;
372 bool load_ok = a.loadFromFile(tstImgFileColor);
373 EXPECT_TRUE(load_ok);
374
375 {
376 CImage b(600, 400);
377 a.scaleImage(b, 600, 400);
378 EXPECT_EQ(b.getWidth(), 600U);
379 EXPECT_EQ(b.getHeight(), 400U);
380 EXPECT_EQ(a.getWidth(), 320U);
381 EXPECT_EQ(a.getHeight(), 240U);
382 }
383 {
384 CImage b;
385 a.scaleImage(b, 600, 400);
386 EXPECT_EQ(b.getWidth(), 600U);
387 EXPECT_EQ(b.getHeight(), 400U);
388 EXPECT_EQ(a.getWidth(), 320U);
389 EXPECT_EQ(a.getHeight(), 240U);
390 }
391
392 for (int pass = 0; pass < 2; pass++)
393 {
394 CImage c;
395 if (pass == 0)
396 c = a.makeDeepCopy();
397 else
398 a.scaleImage(c, 311, 211);
399 const auto cw = c.getWidth(), ch = c.getHeight();
400
401 {
402 CImage b;
403 c.scaleHalf(b, IMG_INTERP_NN);
404 EXPECT_EQ(b.getWidth(), cw / 2);
405 EXPECT_EQ(b.getHeight(), ch / 2);
406 EXPECT_EQ(c.getWidth(), cw);
407 EXPECT_EQ(c.getHeight(), ch);
408 }
409 {
410 CImage ag = c.grayscale();
411 CImage b;
412 ag.scaleHalf(b, IMG_INTERP_LINEAR);
413 EXPECT_EQ(b.getWidth(), cw / 2);
414 EXPECT_EQ(b.getHeight(), ch / 2);
415 EXPECT_EQ(ag.getWidth(), cw);
416 EXPECT_EQ(ag.getHeight(), ch);
417 }
418 {
419 CImage ag = c.grayscale();
420 CImage b;
421 ag.scaleHalf(b, IMG_INTERP_NN);
422 EXPECT_EQ(b.getWidth(), cw / 2);
423 EXPECT_EQ(b.getHeight(), ch / 2);
424 EXPECT_EQ(ag.getWidth(), cw);
425 EXPECT_EQ(ag.getHeight(), ch);
426 }
427 } // two passes
428
429 {
430 CImage b;
431 a.scaleHalf(b, IMG_INTERP_LINEAR);
432 EXPECT_EQ(b.getWidth(), a.getWidth() / 2);
433 EXPECT_EQ(b.getHeight(), a.getHeight() / 2);
434 EXPECT_EQ(a.getWidth(), 320U);
435 EXPECT_EQ(a.getHeight(), 240U);
436 }
437 {
438 CImage b;
439 a.scaleDouble(b, IMG_INTERP_NN);
440 EXPECT_EQ(b.getWidth(), a.getWidth() * 2);
441 EXPECT_EQ(b.getHeight(), a.getHeight() * 2);
442 EXPECT_EQ(a.getWidth(), 320U);
443 EXPECT_EQ(a.getHeight(), 240U);
444 }
445 {
446 CImage b;
447 a.scaleDouble(b, IMG_INTERP_LINEAR);
448 EXPECT_EQ(b.getWidth(), a.getWidth() * 2);
449 EXPECT_EQ(b.getHeight(), a.getHeight() * 2);
450 EXPECT_EQ(a.getWidth(), 320U);
451 EXPECT_EQ(a.getHeight(), 240U);
452 }
453 }
454
455 TEST(CImage, Serialize)
456 {
457 using namespace mrpt::img;
458 CImage a;
459 bool load_ok = a.loadFromFile(tstImgFileColor);
460 EXPECT_TRUE(load_ok);
461
462 mrpt::math::CMatrixFloat am;
463 a.getAsMatrix(am, true, 0, 0, -1, -1, false /* dont normalize to [0,1] */);
464
465 mrpt::io::CMemoryStream buf;
466 auto arch = mrpt::serialization::archiveFrom(buf);
467 arch << a;
468 buf.Seek(0);
469 CImage b;
470 arch >> b;
471
472 mrpt::math::CMatrixFloat bm;
473 b.getAsMatrix(bm, true, 0, 0, -1, -1, false /* dont normalize to [0,1] */);
474
475 EXPECT_EQ(am, bm);
476 }
477
478 TEST(CImage, KLT_response)
479 {
480 using namespace mrpt::img;
481
482 {
483 CImage a(100, 90, CH_GRAY);
484 a.filledRectangle(0, 0, 99, 99, TColor(0x10));
485 a.filledRectangle(40, 30, 41, 31, TColor(0x20));
486
487 for (int w = 2; w < 12; w++)
488 {
489 const auto resp = a.KLT_response(40, 30, w);
490 EXPECT_GT(resp, 0.9f) << " w=" << w;
491
492 const auto flatResp = a.KLT_response(20, 20, w);
493 EXPECT_LT(flatResp, 0.1f) << " w=" << w;
494 }
495 }
496 }
497
498 TEST(CImage, LoadAndComparePseudoRnd)
499 {
500 using namespace mrpt::img;
501 using namespace std::string_literals;
502
503 const auto tstimg = mrpt::UNITTEST_BASEDIR() + "/tests/test_pseudorandom_img_seed70.png"s;
504
505 CImage a;
506 bool load_ok = a.loadFromFile(tstimg);
507 EXPECT_TRUE(load_ok) << "Cannot load: " << tstimg;
508
509 CImage b(10, 7, CH_GRAY);
510 fillImagePseudoRandom(70, b);
511
512 expect_identical(a, b, "LoadAndComparePseudoRnd"s);
513 }
514
515 TEST(CImage, LoadAndSave)
516 {
517 using namespace mrpt::img;
518 using namespace std::string_literals;
519
520 for (unsigned h = 7; h < 20; h += 17)
521 {
522 for (unsigned w = 10; w < 33; w++)
523 {
524 CImage a(w, h, CH_GRAY);
525 fillImagePseudoRandom(w * h, a);
526
527 const auto f = mrpt::system::getTempFileName() + ".png"s;
528
529 const auto tstName = mrpt::format("From: LoadAndSave test w=%u h=%u", w, h);
530
531 bool saved_ok = a.saveToFile(f);
532 EXPECT_TRUE(saved_ok) << tstName;
533
534 CImage b;
535 bool load_ok = b.loadFromFile(f);
536 EXPECT_TRUE(load_ok) << tstName;
537
538 if (!expect_identical(a, b, tstName))
539 {
540 GTEST_FAIL() << "a:\n" << a.asCvMatRef() << "\nb:\n" << b.asCvMatRef() << "\n";
541 }
542 }
543 }
544 }
545
546 TEST(CImage, DifferentAccessMethodsColor)
547 {
548 using namespace mrpt::img;
549 CImage a;
550 bool load_ok = a.loadFromFile(tstImgFileColor);
551 EXPECT_TRUE(load_ok);
552 EXPECT_TRUE(a.isColor());
553
554 for (unsigned r = 0; r < 3; r++)
555 {
556 for (unsigned c = 0; c < 3; c++)
557 {
558 for (int ch = 0; ch < 3; ch++)
559 {
560 EXPECT_EQ(*a(c, r, ch), a.at<uint8_t>(c, r, ch)) << "ch=" << ch << "\n";
561 EXPECT_EQ(*a(c, r, ch), *a.ptr<uint8_t>(c, r, ch)) << "ch=" << ch << "\n";
562 EXPECT_EQ(*a(c, r, ch), a.ptrLine<uint8_t>(r)[c * 3 + ch])
563 << "(c,r,ch)=(" << c << "," << r << "," << ch << ")"
564 << "\n a(c, r, ch)=" << static_cast<void*>(a(c, r, ch))
565 << "\n &a.ptrLine<uint8_t>(r)[c * 3 + ch] = "
566 << static_cast<void*>(&a.ptrLine<uint8_t>(r)[c * 3 + ch])
567 << "\n a(0, r, ch)=" << static_cast<void*>(a(0, r, ch))
568 << "\n a.ptrLine<uint8_t>(r) = " << static_cast<void*>(a.ptrLine<uint8_t>(r)) << "\n";
569 }
570 }
571 }
572 }
573
574 TEST(CImage, DifferentAccessMethodsGray)
575 {
576 using namespace mrpt::img;
577 CImage a;
578 bool load_ok = a.loadFromFile(tstImgFileColor);
579 EXPECT_TRUE(load_ok);
580 a = a.grayscale();
581 EXPECT_FALSE(a.isColor());
582
583 for (unsigned r = 5; r < 7; r++)
584 {
585 for (unsigned c = 10; c < 12; c++)
586 {
587 EXPECT_EQ(*a(c, r), a.at<uint8_t>(c, r));
588 EXPECT_EQ(*a(c, r), *a.ptr<uint8_t>(c, r));
589 EXPECT_EQ(*a(c, r), a.ptrLine<uint8_t>(r)[c]);
590 }
591 }
592 }
593
594 #endif // MRPT_HAS_OPENCV