10 #include <CTraitsTest.h>
11 #include <gtest/gtest.h>
20 #include <test_mrpt_common.h>
23 #include <mrpt/3rdparty/do_opencv_includes.h>
25 template class mrpt::CTraitsTest<mrpt::img::CImage>;
27 using namespace std::string_literals;
29 mrpt::UNITTEST_BASEDIR +
"/samples/img_basic_example/frame_color.jpg"s;
37 for (
unsigned y = 0; y < img.
getHeight(); y++)
39 for (
unsigned x = 0; x < img.
getWidth(); x++)
41 const uint8_t c =
static_cast<uint8_t
>(rnd.drawUniform32bit());
42 img.
at<uint8_t>(x, y) = c;
50 const std::string& s = std::string())
54 for (
unsigned int y = 0; y < a.
getHeight(); y++)
55 for (
unsigned int x = 0; x < a.
getWidth(); x++)
58 if (a.
at<uint8_t>(x, y) != b.
at<uint8_t>(x, y))
return false;
66 EXPECT_THROW(img.
isColor(), std::exception);
78 EXPECT_EQ(img.getPixelDepth(), PixelDepth::D8U);
79 EXPECT_FALSE(img.isColor());
90 EXPECT_EQ(img.getPixelDepth(), PixelDepth::D8U);
93 for (
unsigned w = 64; w < 70; w++)
103 img.setPixel(10, 2,
TColor(0x80, 0x80, 0x80));
107 img.setPixel(11, 2,
TColor(0x0, 0x0, 0x0));
110 img.setPixel(12, 2,
TColor(0xff, 0xff, 0xff));
113 img.at<uint8_t>(13, 2) = 0x70;
116 auto* line = img.ptrLine<uint8_t>(5);
117 for (uint8_t i = 0; i < 20; i++)
124 img.getAsMatrix(M,
true, 0, 0, -1, -1,
false );
125 for (uint8_t i = 0; i < 20; i++)
127 EXPECT_NEAR(
static_cast<double>(M(5, i)), i, 1e-8);
136 a.at<uint8_t>(1, 2) = 0x80;
141 a.at<uint8_t>(1, 3) = 0x81;
148 c.at<uint8_t>(1, 3) = 0x0;
149 a.at<uint8_t>(1, 3) = 0x81;
150 EXPECT_NE(c.at<uint8_t>(1, 3), 0x81);
155 a.at<uint8_t>(1, 2) = 0x80;
160 a.at<uint8_t>(1, 3) = 0x81;
167 c.at<uint8_t>(1, 3) = 0x0;
168 a.at<uint8_t>(1, 3) = 0x81;
169 EXPECT_NE(c.at<uint8_t>(1, 3), 0x81);
174 a.at<uint8_t>(1, 2) = 0x80;
181 b.at<uint8_t>(1, 3) = 0x0;
182 a.at<uint8_t>(1, 3) = 0x81;
183 EXPECT_NE(b.at<uint8_t>(1, 3), 0x81);
188 a.at<uint8_t>(1, 2) = 0x80;
198 a.
at<uint8_t>(1, 2) = 0x80;
237 EXPECT_FALSE(b.isColor());
252 EXPECT_FALSE(b.isColor());
256 a.at<uint8_t>(1, 2) = 0x80;
261 EXPECT_FALSE(b.isColor());
271 a.at<uint8_t>(0, 0) = 0x80;
272 a.at<uint8_t>(0, 1) = 0x80;
273 a.at<uint8_t>(1, 0) = 0x80;
274 a.at<uint8_t>(1, 1) = 0x80;
279 EXPECT_EQ(imgH.getWidth(), a.getWidth() / 2);
280 EXPECT_EQ(imgH.getHeight(), a.getHeight() / 2);
282 EXPECT_EQ(imgH.at<uint8_t>(0, 0), a.at<uint8_t>(0, 0));
287 EXPECT_EQ(imgD.getWidth(), a.getWidth() * 2);
288 EXPECT_EQ(imgD.getHeight(), a.getHeight() * 2);
299 EXPECT_EQ(std::string(
"BGR"), a.getChannelsOrder());
303 EXPECT_EQ(std::string(
"GRAY"), a.getChannelsOrder());
313 a.at<uint8_t>(1, 2) = 0x80;
316 m.at<uint8_t>(2, 1) = 0x70;
321 cv::Mat& m2 = a.asCvMatRef();
322 cv::Mat& m3 = a.asCvMatRef();
325 m2 = cv::Mat(40, 40, CV_8UC1);
327 cv::Mat& m4 = a.asCvMatRef();
335 a.at<uint8_t>(1, 2) = 0x80;
337 cv::Mat m = a.asCvMat<cv::Mat>(
DEEP_COPY);
338 m.at<uint8_t>(2, 1) = 0x70;
343 m = cv::Mat(40, 40, CV_8UC1);
365 for (
int pass = 0; pass < 2; pass++)
372 const auto cw = c.getWidth(), ch = c.getHeight();
405 EXPECT_EQ(b.getWidth(), a.getWidth() / 2);
406 EXPECT_EQ(b.getHeight(), a.getHeight() / 2);
413 EXPECT_EQ(b.getWidth(), a.getWidth() * 2);
414 EXPECT_EQ(b.getHeight(), a.getHeight() * 2);
421 EXPECT_EQ(b.getWidth(), a.getWidth() * 2);
422 EXPECT_EQ(b.getHeight(), a.getHeight() * 2);
436 a.getAsMatrix(am,
true, 0, 0, -1, -1,
false );
446 b.getAsMatrix(bm,
true, 0, 0, -1, -1,
false );
457 a.filledRectangle(0, 0, 99, 99,
TColor(0x10));
458 a.filledRectangle(40, 30, 41, 31,
TColor(0x20));
460 for (
int w = 2; w < 12; w++)
462 const auto resp = a.KLT_response(40, 30, w);
471 using namespace std::string_literals;
474 mrpt::UNITTEST_BASEDIR +
"/tests/test_pseudorandom_img_seed70.png"s;
489 using namespace std::string_literals;
491 for (
unsigned h = 7; h < 20; h += 17)
493 for (
unsigned w = 10; w < 33; w++)
503 bool saved_ok = a.saveToFile(f);
512 GTEST_FAIL() <<
"a:\n"
513 << a.asCvMatRef() <<
"\nb:\n"
514 << b.asCvMatRef() <<
"\n";
528 for (
unsigned r = 0; r < 3; r++)
530 for (
unsigned c = 0; c < 3; c++)
532 for (
int ch = 0; ch < 3; ch++)
534 EXPECT_EQ(*a(c, r, ch), a.at<uint8_t>(c, r, ch))
535 <<
"ch=" << ch <<
"\n";
536 EXPECT_EQ(*a(c, r, ch), *a.ptr<uint8_t>(c, r, ch))
537 <<
"ch=" << ch <<
"\n";
538 EXPECT_EQ(*a(c, r, ch), a.ptrLine<uint8_t>(r)[c * 3 + ch])
539 <<
"(c,r,ch)=(" << c <<
"," << r <<
"," << ch <<
")"
540 <<
"\n a(c, r, ch)=" <<
static_cast<void*
>(a(c, r, ch))
541 <<
"\n &a.ptrLine<uint8_t>(r)[c * 3 + ch] = "
542 <<
static_cast<void*
>(&a.ptrLine<uint8_t>(r)[c * 3 + ch])
543 <<
"\n a(0, r, ch)=" <<
static_cast<void*
>(a(0, r, ch))
544 <<
"\n a.ptrLine<uint8_t>(r) = "
545 <<
static_cast<void*
>(a.ptrLine<uint8_t>(r)) <<
"\n";
558 EXPECT_FALSE(a.isColor());
560 for (
unsigned r = 5; r < 7; r++)
562 for (
unsigned c = 10; c < 12; c++)
564 EXPECT_EQ(*a(c, r), a.at<uint8_t>(c, r));
565 EXPECT_EQ(*a(c, r), *a.ptr<uint8_t>(c, r));
566 EXPECT_EQ(*a(c, r), a.ptrLine<uint8_t>(r)[c]);
571 #endif // MRPT_HAS_OPENCV