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>;
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);
474 mrpt::UNITTEST_BASEDIR +
"/tests/test_pseudorandom_img_seed70.png"s;
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 Shallow copy: the copied object is a reference to the original one.
Used in mrpt::img::CImage.
EXPECT_TRUE(mrpt::system::fileExists(ini_fil))
EXPECT_GT(out.final_iters, 10UL)
std::string std::string format(std::string_view fmt, ARGS &&... args)
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)
size_t getHeight() const override
Returns the height of the image in pixels.
void scaleImage(CImage &out_img, unsigned int width, unsigned int height, TInterpolationMethod interp=IMG_INTERP_CUBIC) const
Scales this image to a new size, interpolating as needed, saving the new image in a different output ...
void setExternalStorage(const std::string &fileName) noexcept
By using this method the image is marked as referenced to an external file, which will be loaded only...
const auto tstImgFileColor
const T & at(unsigned int col, unsigned int row, unsigned int channel=0) const
Access to pixels without checking boundaries, and doing a reinterpret_cast<> of the data as the given...
void swap(CImage &o)
Efficiently swap of two images.
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.
bool loadFromFile(const std::string &fileName, int isColor=-1)
Load image from a file, whose format is determined from the extension (internally uses OpenCV)...
std::string getTempFileName()
Returns the name of a proposed temporary file name.
size_t getWidth() const override
Returns the width of the image in pixels.
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.
CImage grayscale() const
Returns a grayscale version of the image, or a shallow copy of itself if it is already a grayscale im...
CImage makeDeepCopy() const
Returns a deep copy of this image.
bool isColor() const
Returns true if the image is RGB, false if it is grayscale.
static void CtorSized_gray(unsigned int w, unsigned int h)
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())
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
CImage scaleDouble(TInterpolationMethod interp) const
Returns a new image scaled up to double its original size.
EXPECT_NEAR(out.cam_params.rightCameraPose.x, 0.1194, 0.005)
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...
A class for storing images as grayscale or RGB bitmaps.