10 #include <CTraitsTest.h> 11 #include <gtest/gtest.h> 20 #include <test_mrpt_common.h> 23 #include <mrpt/otherlibs/do_opencv_includes.h> 25 template class mrpt::CTraitsTest<mrpt::img::CImage>;
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++)
52 EXPECT_EQ(
a.getWidth(),
b.getWidth());
53 EXPECT_EQ(
a.getHeight(),
b.getHeight());
54 for (
unsigned int y = 0;
y <
a.getHeight();
y++)
55 for (
unsigned int x = 0;
x <
a.getWidth();
x++)
66 EXPECT_THROW(
img.isColor(), std::exception);
75 EXPECT_EQ(
img.getWidth(),
w);
76 EXPECT_EQ(
img.getHeight(), 48U);
77 EXPECT_EQ(
img.getChannelCount(), 1U);
78 EXPECT_EQ(
img.getPixelDepth(), PixelDepth::D8U);
79 EXPECT_FALSE(
img.isColor());
87 EXPECT_EQ(
img.getWidth(), 64U);
88 EXPECT_EQ(
img.getHeight(), 48U);
89 EXPECT_EQ(
img.getChannelCount(), 3U);
90 EXPECT_EQ(
img.getPixelDepth(), PixelDepth::D8U);
91 EXPECT_TRUE(
img.isColor());
93 for (
unsigned w = 64;
w < 70;
w++)
103 img.setPixel(10, 2,
TColor(0x80, 0x80, 0x80));
105 EXPECT_EQ(*
img(10, 2), 0x80);
107 img.setPixel(11, 2,
TColor(0x0, 0x0, 0x0));
110 img.setPixel(12, 2,
TColor(0xff, 0xff, 0xff));
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);
139 EXPECT_EQ(
b.at<
uint8_t>(1, 2), 0x80);
142 EXPECT_EQ(
b.at<
uint8_t>(1, 3), 0x81);
146 EXPECT_EQ(
c.at<
uint8_t>(1, 2), 0x80);
150 EXPECT_NE(
c.at<
uint8_t>(1, 3), 0x81);
158 EXPECT_EQ(
b.at<
uint8_t>(1, 2), 0x80);
161 EXPECT_EQ(
b.at<
uint8_t>(1, 3), 0x81);
165 EXPECT_EQ(
c.at<
uint8_t>(1, 2), 0x80);
169 EXPECT_NE(
c.at<
uint8_t>(1, 3), 0x81);
177 EXPECT_EQ(
b.at<
uint8_t>(1, 2), 0x80);
183 EXPECT_NE(
b.at<
uint8_t>(1, 3), 0x81);
191 EXPECT_EQ(
b.getWidth(), 20U);
192 EXPECT_EQ(
b.getHeight(), 10U);
193 EXPECT_EQ(
b.at<
uint8_t>(1, 2), 0x80);
201 EXPECT_EQ(
b.getWidth(), 20U);
202 EXPECT_EQ(
b.getHeight(), 10U);
203 EXPECT_EQ(
b.at<
uint8_t>(1, 2), 0x80);
214 EXPECT_EQ(
a.getWidth(), 320U);
215 EXPECT_EQ(
a.getHeight(), 240U);
220 a.setExternalStorage(
"./foo_61717181.png");
232 EXPECT_TRUE(load_ok);
235 EXPECT_EQ(
b.getWidth(),
a.getWidth());
236 EXPECT_EQ(
b.getHeight(),
a.getHeight());
237 EXPECT_FALSE(
b.isColor());
247 EXPECT_TRUE(load_ok);
250 EXPECT_EQ(
b.getWidth(),
a.getWidth());
251 EXPECT_EQ(
b.getHeight(),
a.getHeight());
252 EXPECT_FALSE(
b.isColor());
259 EXPECT_EQ(
b.getWidth(),
a.getWidth());
260 EXPECT_EQ(
b.getHeight(),
a.getHeight());
261 EXPECT_FALSE(
b.isColor());
262 EXPECT_EQ(
b.at<
uint8_t>(1, 2), 0x80);
279 EXPECT_EQ(imgH.getWidth(),
a.getWidth() / 2);
280 EXPECT_EQ(imgH.getHeight(),
a.getHeight() / 2);
281 EXPECT_EQ(imgH.isColor(),
a.isColor());
287 EXPECT_EQ(imgD.getWidth(),
a.getWidth() * 2);
288 EXPECT_EQ(imgD.getHeight(),
a.getHeight() * 2);
289 EXPECT_EQ(imgD.isColor(),
a.isColor());
298 EXPECT_TRUE(load_ok);
318 EXPECT_EQ(
a.at<
uint8_t>(1, 2), 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();
330 EXPECT_EQ(
a.getWidth(), 40U);
331 EXPECT_EQ(
a.getHeight(), 40U);
340 EXPECT_EQ(
a.at<
uint8_t>(1, 2), 0x80);
343 m = cv::Mat(40, 40, CV_8UC1);
344 EXPECT_EQ(
a.getWidth(), 20U);
345 EXPECT_EQ(
a.getHeight(), 10U);
354 EXPECT_TRUE(load_ok);
358 a.scaleImage(
b, 600, 400);
359 EXPECT_EQ(
b.getWidth(), 600U);
360 EXPECT_EQ(
b.getHeight(), 400U);
361 EXPECT_EQ(
a.getWidth(), 320U);
362 EXPECT_EQ(
a.getHeight(), 240U);
365 for (
int pass = 0; pass < 2; pass++)
369 c =
a.makeDeepCopy();
371 a.scaleImage(
c, 311, 211);
372 const auto cw =
c.getWidth(), ch =
c.getHeight();
377 EXPECT_EQ(
b.getWidth(), cw / 2);
378 EXPECT_EQ(
b.getHeight(), ch / 2);
379 EXPECT_EQ(
c.getWidth(), cw);
380 EXPECT_EQ(
c.getHeight(), ch);
386 EXPECT_EQ(
b.getWidth(), cw / 2);
387 EXPECT_EQ(
b.getHeight(), ch / 2);
388 EXPECT_EQ(ag.getWidth(), cw);
389 EXPECT_EQ(ag.getHeight(), ch);
395 EXPECT_EQ(
b.getWidth(), cw / 2);
396 EXPECT_EQ(
b.getHeight(), ch / 2);
397 EXPECT_EQ(ag.getWidth(), cw);
398 EXPECT_EQ(ag.getHeight(), ch);
405 EXPECT_EQ(
b.getWidth(),
a.getWidth() / 2);
406 EXPECT_EQ(
b.getHeight(),
a.getHeight() / 2);
407 EXPECT_EQ(
a.getWidth(), 320U);
408 EXPECT_EQ(
a.getHeight(), 240U);
413 EXPECT_EQ(
b.getWidth(),
a.getWidth() * 2);
414 EXPECT_EQ(
b.getHeight(),
a.getHeight() * 2);
415 EXPECT_EQ(
a.getWidth(), 320U);
416 EXPECT_EQ(
a.getHeight(), 240U);
421 EXPECT_EQ(
b.getWidth(),
a.getWidth() * 2);
422 EXPECT_EQ(
b.getHeight(),
a.getHeight() * 2);
423 EXPECT_EQ(
a.getWidth(), 320U);
424 EXPECT_EQ(
a.getHeight(), 240U);
433 EXPECT_TRUE(load_ok);
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);
463 EXPECT_GT(resp, 0.5f);
474 mrpt::UNITTEST_BASEDIR +
"/tests/test_pseudorandom_img_seed70.png"s;
477 bool load_ok =
a.loadFromFile(tstimg);
478 EXPECT_TRUE(load_ok) <<
"Cannot load: " << tstimg;
491 for (
unsigned h = 7; h < 20; h += 17)
493 for (
unsigned w = 10;
w < 33;
w++)
503 bool saved_ok =
a.saveToFile(f);
504 EXPECT_TRUE(saved_ok) << tstName;
507 bool load_ok =
b.loadFromFile(f);
508 EXPECT_TRUE(load_ok) << tstName;
512 GTEST_FAIL() <<
"a:\n" 513 <<
a.asCvMatRef() <<
"\nb:\n" 514 <<
b.asCvMatRef() <<
"\n";
525 EXPECT_TRUE(load_ok);
526 EXPECT_TRUE(
a.isColor());
528 for (
unsigned r = 0;
r < 3;
r++)
530 for (
unsigned c = 0;
c < 3;
c++)
532 for (
int ch = 0; ch < 3; ch++)
535 <<
"ch=" << ch <<
"\n";
537 <<
"ch=" << ch <<
"\n";
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";
556 EXPECT_TRUE(load_ok);
558 EXPECT_FALSE(
a.isColor());
560 for (
unsigned r = 5;
r < 7;
r++)
562 for (
unsigned c = 10;
c < 12;
c++)
571 #endif // MRPT_HAS_OPENCV Shallow copy: the copied object is a reference to the original one.
Used in mrpt::img::CImage.
CImage scaleHalf(TInterpolationMethod interp) const
Returns a new image scaled down to half its original size.
static void fillImagePseudoRandom(uint32_t seed, mrpt::img::CImage &img)
TEST(CImage, CtorDefault)
const auto tstImgFileColor
GLubyte GLubyte GLubyte GLubyte w
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream, std::istream, std::ostream, std::stringstream.
std::string getTempFileName()
Returns the name of a proposed temporary file name.
This CStream derived class allow using a memory buffer as a CStream.
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
void Randomize(const uint32_t seed)
Randomize the generators.
GLsizei const GLchar ** string
CImage grayscale() const
Returns a grayscale version of the image, or a shallow copy of itself if it is already a grayscale im...
GLdouble GLdouble GLdouble r
CImage makeDeepCopy() const
Returns a deep copy of this image.
static void CtorSized_gray(unsigned int w, unsigned int h)
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Deep copy: the copied object has a duplicate of all data, becoming independent.
static bool expect_identical(const mrpt::img::CImage &a, const mrpt::img::CImage &b, const std::string &s=std::string())
CImage scaleDouble(TInterpolationMethod interp) const
Returns a new image scaled up to double its original size.
unsigned __int32 uint32_t
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
This template class provides the basic functionality for a general 2D any-size, resizable container o...
GLubyte GLubyte GLubyte a
A class for storing images as grayscale or RGB bitmaps.