Debian Codegraph @

libs/img/src/CImage.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 "img-precomp.h" // Precompiled headers
11 //
12 #include <mrpt/core/cpu.h>
13 #include <mrpt/core/get_env.h>
14 #include <mrpt/core/round.h> // for round()
15 #include <mrpt/img/CImage.h>
16 #include <mrpt/io/CFileInputStream.h>
17 #include <mrpt/io/CFileOutputStream.h>
18 #include <mrpt/io/CMemoryStream.h>
19 #include <mrpt/io/lazy_load_path.h>
20 #include <mrpt/io/vector_loadsave.h>
21 #include <mrpt/math/CMatrixF.h>
22 #include <mrpt/math/fourier.h>
23 #include <mrpt/math/utils.h> // for roundup()
24 #include <mrpt/serialization/CArchive.h>
25 #include <mrpt/system/CTicTac.h>
26 #include <mrpt/system/CTimeLogger.h>
27 #include <mrpt/system/filesystem.h>
28 #include <mrpt/system/memory.h>
29
30 #include <iostream>
31
32 // Universal include for all versions of OpenCV
33 #include <mrpt/3rdparty/do_opencv_includes.h>
34
35 #include "CImage_impl.h"
36
37 #if MRPT_HAS_MATLAB
38 #include <mexplus/mxarray.h>
39 #endif
40
41 // Prototypes of SSE2/SSE3/SSSE3 optimized functions:
42 #include "CImage.SSEx.h"
43
44 using namespace mrpt;
45 using namespace mrpt::img;
46 using namespace mrpt::math;
47 using namespace mrpt::system;
48 using namespace std;
49
50 // This must be added to any CSerializable class implementation file.
51 IMPLEMENTS_SERIALIZABLE(CImage, CSerializable, mrpt::img)
52
53 static bool DISABLE_JPEG_COMPRESSION_value = true;
54 static int SERIALIZATION_JPEG_QUALITY_value = 95;
55
56 const thread_local bool MRPT_DEBUG_IMG_LAZY_LOAD =
57 mrpt::get_env<bool>("MRPT_DEBUG_IMG_LAZY_LOAD", false);
58
59 void CImage::DISABLE_JPEG_COMPRESSION(bool val) { DISABLE_JPEG_COMPRESSION_value = val; }
60 bool CImage::DISABLE_JPEG_COMPRESSION() { return DISABLE_JPEG_COMPRESSION_value; }
61 void CImage::SERIALIZATION_JPEG_QUALITY(int q) { SERIALIZATION_JPEG_QUALITY_value = q; }
62 int CImage::SERIALIZATION_JPEG_QUALITY() { return SERIALIZATION_JPEG_QUALITY_value; }
63
64 CExceptionExternalImageNotFound::CExceptionExternalImageNotFound(const std::string& s) :
65 std::runtime_error(s)
66 {
67 }
68
69 const std::string& CImage::getImagesPathBase() { return mrpt::io::getLazyLoadPathBase(); }
70 void CImage::setImagesPathBase(const std::string& path) { mrpt::io::setLazyLoadPathBase(path); }
71
72 // Do performance time logging?
73 #define IMAGE_ALLOC_PERFLOG 0
74
75 #if IMAGE_ALLOC_PERFLOG
76 mrpt::img::CTimeLogger alloc_tims;
77 #endif
78
79 #if MRPT_HAS_OPENCV
80 namespace
81 {
82 int interpolationMethod2Cv(TInterpolationMethod i)
83 {
84 // clang-format off
85 switch (i)
86 {
87 case IMG_INTERP_NN: return cv::INTER_NEAREST;
88 case IMG_INTERP_LINEAR: return cv::INTER_LINEAR;
89 case IMG_INTERP_CUBIC: return cv::INTER_CUBIC;
90 case IMG_INTERP_AREA: return cv::INTER_AREA;
91 };
92 // clang-format on
93 return -1;
94 }
95 } // namespace
96
97 template <typename RET = uint32_t>
98 constexpr RET pixelDepth2CvDepth(PixelDepth d)
99 {
100 // clang-format off
101 switch (d)
102 {
103 case PixelDepth::D8U: return static_cast<RET>(CV_8U);
104 case PixelDepth::D8S: return static_cast<RET>(CV_8S);
105 case PixelDepth::D16U: return static_cast<RET>(CV_16U);
106 case PixelDepth::D16S: return static_cast<RET>(CV_16S);
107 case PixelDepth::D32S: return static_cast<RET>(CV_32S);
108 case PixelDepth::D32F: return static_cast<RET>(CV_32F);
109 case PixelDepth::D64F: return static_cast<RET>(CV_64F);
110 }
111 // clang-format on
112 return std::numeric_limits<RET>::max();
113 }
114 template <typename RET = uint32_t>
115 RET pixelDepth2IPLCvDepth(PixelDepth d)
116 {
117 // clang-format off
118 switch (d)
119 {
120 case PixelDepth::D8U: return static_cast<RET>(IPL_DEPTH_8U);
121 case PixelDepth::D8S: return static_cast<RET>(IPL_DEPTH_8S);
122 case PixelDepth::D16U: return static_cast<RET>(IPL_DEPTH_16U);
123 case PixelDepth::D16S: return static_cast<RET>(IPL_DEPTH_16S);
124 case PixelDepth::D32S: return static_cast<RET>(IPL_DEPTH_32S);
125 case PixelDepth::D32F: return static_cast<RET>(IPL_DEPTH_32F);
126 case PixelDepth::D64F: return static_cast<RET>(IPL_DEPTH_64F);
127 }
128 // clang-format on
129 return std::numeric_limits<RET>::max();
130 }
131
132 namespace
133 {
134 PixelDepth cvDepth2PixelDepth(int64_t d)
135 {
136 // clang-format off
137 switch (d)
138 {
139 case CV_8U: return PixelDepth::D8U;
140 case CV_8S: return PixelDepth::D8S;
141 case CV_16U: return PixelDepth::D16U;
142 case CV_16S: return PixelDepth::D16S;
143 case CV_32S: return PixelDepth::D32S;
144 case CV_32F: return PixelDepth::D32F;
145 case CV_64F: return PixelDepth::D64F;
146 }
147 // clang-format on
148 return PixelDepth::D8U;
149 }
150 } // namespace
151 #endif // MRPT_HAS_OPENCV
152
153 // Default ctor
154 CImage::CImage() : m_impl(mrpt::make_impl<CImage::Impl>()) {}
155
156 // Ctor with size
157 CImage::CImage(unsigned int width, unsigned int height, TImageChannels nChannels) : CImage()
158 {
159 MRPT_START
160 resize(width, height, nChannels);
161 MRPT_END
162 }
163
164 void CImage::swap(CImage& o)
165 {
166 std::swap(m_impl, o.m_impl);
167 std::swap(m_imgIsExternalStorage, o.m_imgIsExternalStorage);
168 std::swap(m_externalFile, o.m_externalFile);
169 }
170
171 void CImage::copyFromForceLoad(const CImage& o)
172 {
173 *this = o;
174 forceLoad();
175 }
176
177 CImage::CImage(const cv::Mat& img, copy_type_t copy_type) : CImage()
178 {
179 #if MRPT_HAS_OPENCV
180 MRPT_START
181 if (copy_type == DEEP_COPY)
182 m_impl->img = img.clone();
183 else
184 m_impl->img = img;
185 MRPT_END
186 #endif
187 }
188
189 CImage::CImage(const CImage& img, copy_type_t copy_type) : CImage()
190 {
191 #if MRPT_HAS_OPENCV
192 MRPT_START
193
194 // Also, copy our custom fields, only if making a shallow copy!
195 m_imgIsExternalStorage = img.m_imgIsExternalStorage;
196 m_externalFile = img.m_externalFile;
197
198 // this new image is *not* lazy-load.
199 if (copy_type == DEEP_COPY && !img.asCvMatRef().empty())
200 {
201 // deep copy
202 m_impl->img = img.asCvMatRef().clone();
203 }
204 else
205 {
206 // shallow copy
207 m_impl->img = img.m_impl->img;
208 }
209 MRPT_END
210 #endif
211 }
212
213 CImage CImage::makeDeepCopy() const
214 {
215 #if MRPT_HAS_OPENCV
216 CImage ret(*this);
217 ret.makeSureImageIsLoaded();
218 ret.m_impl->img = m_impl->img.clone();
219 return ret;
220 #else
221 THROW_EXCEPTION("Operation not supported: build MRPT against OpenCV!");
222 #endif
223 }
224
225 void CImage::asCvMat(cv::Mat& out_img, copy_type_t copy_type) const
226 {
227 #if MRPT_HAS_OPENCV
228 makeSureImageIsLoaded();
229 if (copy_type == DEEP_COPY)
230 out_img = m_impl->img.clone();
231 else
232 out_img = m_impl->img;
233 #endif
234 }
235
236 cv::Mat& CImage::asCvMatRef()
237 {
238 #if MRPT_HAS_OPENCV
239 makeSureImageIsLoaded();
240 return m_impl->img;
241 #else
242 THROW_EXCEPTION("Operation not supported: build MRPT against OpenCV!");
243 #endif
244 }
245
246 const cv::Mat& CImage::asCvMatRef() const
247 {
248 #if MRPT_HAS_OPENCV
249 makeSureImageIsLoaded();
250 return m_impl->img;
251 #else
252 THROW_EXCEPTION("Operation not supported: build MRPT against OpenCV!");
253 #endif
254 }
255
256 void CImage::resize(
257 std::size_t width, std::size_t height, TImageChannels nChannels, PixelDepth depth)
258 {
259 MRPT_START
260
261 #if MRPT_HAS_OPENCV
262 // Dont call makeSureImageIsLoaded() here,
263 // since it will throw if resize() is called from a ctor, where it's
264 // legit for the img to be uninitialized.
265
266 // If we're resizing to exactly the current size, do nothing:
267 if (static_cast<unsigned>(m_impl->img.cols) == width &&
268 static_cast<unsigned>(m_impl->img.rows) == height && m_impl->img.channels() == nChannels &&
269 m_impl->img.depth() == static_cast<int>(pixelDepth2CvDepth(depth)))
270 {
271 // Nothing to do:
272 return;
273 }
274
275 #if IMAGE_ALLOC_PERFLOG
276 const std::string sLog = mrpt::format("cvCreateImage %ux%u", width, height);
277 alloc_tims.enter(sLog.c_str());
278 #endif
279
280 static_assert(pixelDepth2CvDepth<int>(PixelDepth::D8U) + CV_8UC(3) == CV_8UC3);
281
282 m_impl->img = cv::Mat(
283 static_cast<int>(height), static_cast<int>(width),
284 pixelDepth2CvDepth<int>(depth) + ((nChannels - 1) << CV_CN_SHIFT));
285
286 #if IMAGE_ALLOC_PERFLOG
287 alloc_tims.leave(sLog.c_str());
288 #endif
289
290 #else
291 THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
292 #endif
293 MRPT_END
294 }
295
296 PixelDepth CImage::getPixelDepth() const
297 {
298 MRPT_START
299 #if MRPT_HAS_OPENCV
300 makeSureImageIsLoaded(); // For delayed loaded images stored externally
301 return cvDepth2PixelDepth(m_impl->img.depth());
302 #else
303 THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
304 #endif
305 MRPT_END
306 }
307
308 mrpt::img::CImage CImage::LoadFromFile(const std::string& fileName, int isColor)
309 {
310 CImage im;
311 bool ok = im.loadFromFile(fileName, isColor);
312 if (!ok) THROW_EXCEPTION_FMT("Error loading image from '%s'", fileName.c_str());
313 return im;
314 }
315
316 bool CImage::loadFromFile(const std::string& fileName, int isColor)
317 {
318 MRPT_START
319
320 #if MRPT_HAS_OPENCV
321 #ifdef HAVE_OPENCV_IMGCODECS
322 std::vector<uint8_t> fileData;
323 if (!mrpt::io::loadBinaryFile(fileData, fileName)) return false;
324 const cv::Mat data(fileData.size(), 1, CV_8UC1, fileData.data());
325
326 // Reuse the buffer (save memory allocations) if possible:
327 if (m_impl->img.empty())
328 m_impl->img = cv::imdecode(data, isColor);
329 else
330 cv::imdecode(data, isColor, &m_impl->img);
331 #else
332 IplImage* newImg = cvLoadImage(fileName.c_str(), isColor);
333 if (!newImg) return false;
334 m_impl->img = cv::cvarrToMat(newImg);
335 #endif
336
337 m_imgIsExternalStorage = false;
338 m_externalFile.clear();
339
340 if (m_impl->img.empty()) return false;
341
342 return true;
343 #else
344 THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
345 #endif
346 MRPT_END
347 }
348
349 bool CImage::saveToFile(const std::string& fileName, int jpeg_quality) const
350 {
351 MRPT_START
352 #if MRPT_HAS_OPENCV
353 makeSureImageIsLoaded(); // For delayed loaded images stored externally
354 ASSERT_(!m_impl->img.empty());
355
356 #ifdef HAVE_OPENCV_IMGCODECS
357 const std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, jpeg_quality};
358 return cv::imwrite(fileName, m_impl->img, params);
359 #else
360 int p[3] = {CV_IMWRITE_JPEG_QUALITY, jpeg_quality, 0};
361 _IplImage ipl = m_impl->img;
362 return (0 != cvSaveImage(fileName.c_str(), &ipl, p));
363 #endif
364 #else
365 THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
366 #endif
367 MRPT_END
368 }
369
370 void CImage::internal_fromIPL(const IplImage* iplImage, copy_type_t c)
371 {
372 MRPT_START
373 #if MRPT_HAS_OPENCV
374 ASSERT_(iplImage != nullptr);
375 clear();
376 m_impl->img = cv::cvarrToMat(iplImage, c == DEEP_COPY ? true : false /*copyData*/);
377 #else
378 THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
379 #endif
380 MRPT_END
381 }
382
383 void CImage::loadFromMemoryBuffer(
384 unsigned int width, unsigned int height, bool color, unsigned char* rawpixels, bool swapRedBlue)
385 {
386 MRPT_START
387
388 #if MRPT_HAS_OPENCV
389 resize(width, height, color ? CH_RGB : CH_GRAY);
390 m_imgIsExternalStorage = false;
391 m_externalFile.clear();
392
393 auto* imgData = m_impl->img.data;
394 const auto imgWidthStep = m_impl->img.step[0];
395
396 if (color && swapRedBlue)
397 {
398 // Do copy & swap at once:
399 unsigned char* ptr_src = rawpixels;
400 auto* ptr_dest = imgData;
401 const int bytes_per_row_out = imgWidthStep;
402
403 for (int h = height; h--;)
404 {
405 for (unsigned int i = 0; i < width; i++, ptr_src += 3, ptr_dest += 3)
406 {
407 unsigned char t0 = ptr_src[0], t1 = ptr_src[1], t2 = ptr_src[2];
408 ptr_dest[2] = t0;
409 ptr_dest[1] = t1;
410 ptr_dest[0] = t2;
411 }
412 ptr_dest += bytes_per_row_out - width * 3;
413 }
414 }
415 else
416 {
417 if (imgWidthStep == static_cast<size_t>(m_impl->img.cols * m_impl->img.channels()))
418 {
419 // Copy the image data:
420 memcpy(imgData, rawpixels, m_impl->img.dataend - m_impl->img.data);
421 }
422 else
423 {
424 // Copy the image row by row:
425 unsigned char* ptr_src = rawpixels;
426 auto* ptr_dest = imgData;
427 int bytes_per_row = width * (color ? 3 : 1);
428 int bytes_per_row_out = imgWidthStep;
429 for (unsigned int y = 0; y < height; y++)
430 {
431 memcpy(ptr_dest, ptr_src, bytes_per_row);
432 ptr_src += bytes_per_row;
433 ptr_dest += bytes_per_row_out;
434 }
435 }
436 }
437 #else
438 THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
439 #endif
440 MRPT_END
441 }
442
443 unsigned char* CImage::operator()(unsigned int ucol, unsigned int urow, unsigned int uchannel) const
444 {
445 #if MRPT_HAS_OPENCV
446
447 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG)
448 MRPT_START
449 #endif
450
451 makeSureImageIsLoaded(); // For delayed loaded images stored externally
452 const auto col = static_cast<int>(ucol);
453 const auto row = static_cast<int>(urow);
454 const auto channel = static_cast<int>(uchannel);
455
456 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG)
457 ASSERT_(m_impl && !m_impl->img.empty());
458 if (row >= m_impl->img.rows || col >= m_impl->img.cols || channel >= m_impl->img.channels())
459 {
460 THROW_EXCEPTION(format(
461 "Pixel coordinates/channel out of bounds: row=%u/%u col=%u/%u "
462 "chan=%u/%u",
463 row, m_impl->img.rows, col, m_impl->img.cols, channel, m_impl->img.channels()));
464 }
465 #endif
466 auto p = (&m_impl->img.at<uint8_t>(row, m_impl->img.channels() * col)) + channel;
467 return const_cast<unsigned char*>(p);
468 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG)
469 MRPT_END
470 #endif
471
472 #else
473 THROW_EXCEPTION("MRPT was compiled without OpenCV");
474 #endif
475 }
476
477 uint8_t* CImage::internal_get(int col, int row, uint8_t channel) const
478 {
479 #if MRPT_HAS_OPENCV
480 makeSureImageIsLoaded(); // For delayed loaded images stored externally
481 auto p = (&m_impl->img.at<uint8_t>(row, m_impl->img.channels() * col)) + channel;
482 return const_cast<uint8_t*>(p);
483 #else
484 return nullptr;
485 #endif
486 }
487
488 uint8_t CImage::serializeGetVersion() const
489 {
490 #if !MRPT_HAS_OPENCV
491 return 100;
492 #else
493 return 9;
494 #endif
495 }
496 void CImage::serializeTo(mrpt::serialization::CArchive& out) const
497 {
498 #if !MRPT_HAS_OPENCV
499 out << m_imgIsExternalStorage;
500 if (m_imgIsExternalStorage) out << m_externalFile;
501 // Nothing else to serialize!
502 #else
503
504 // Added in version 6: possibility of being stored offline:
505 out << m_imgIsExternalStorage;
506
507 if (m_imgIsExternalStorage)
508 {
509 out << m_externalFile;
510 return;
511 }
512 // Normal image loaded in memory:
513 ASSERT_(m_impl);
514
515 const bool hasColor = m_impl->img.empty() ? false : isColor();
516
517 out << hasColor;
518
519 // Version >2: Color->JPEG, GrayScale->BYTE's array!
520 const int32_t width = m_impl->img.cols;
521 const int32_t height = m_impl->img.rows;
522 if (!hasColor)
523 {
524 // GRAY-SCALE: Raw bytes:
525 // Version 3: ZIP compression!
526 // Version 4: Skip zip if the image size <= 16Kb
527 int32_t origin = 0; // not used mrpt v1.9.9
528 uint32_t imageSize = height * m_impl->img.step[0];
529 // Version 10: depth
530 int32_t depth = m_impl->img.depth();
531
532 out << width << height << origin << imageSize << int32_t(cvDepth2PixelDepth(depth));
533
534 // Version 5: Use CImage::DISABLE_ZIP_COMPRESSION
535 // Dec 2019: Remove this feature since it's not worth.
536 // We still spend 1 byte for this constant bool just not to
537 // bump the serialization number.
538 bool imageStoredAsZip = false;
539
540 out << imageStoredAsZip;
541
542 if (imageSize > 0 && m_impl->img.data != nullptr) out.WriteBuffer(m_impl->img.data, imageSize);
543 }
544 else
545 {
546 // COLOR: High quality JPEG image
547
548 // v7: If size is 0xN or Nx0, don't call
549 // "saveToStreamAsJPEG"!!
550
551 // v8: If DISABLE_JPEG_COMPRESSION
552 if (!CImage::DISABLE_JPEG_COMPRESSION())
553 {
554 // normal behavior: compress images:
555 out << width << height;
556
557 if (width >= 1 && height >= 1)
558 {
559 // Save to temporary memory stream:
560 mrpt::io::CMemoryStream aux;
561 saveToStreamAsJPEG(aux, CImage::SERIALIZATION_JPEG_QUALITY());
562
563 const auto nBytes = static_cast<uint32_t>(aux.getTotalBytesCount());
564
565 out << nBytes;
566 out.WriteBuffer(aux.getRawBufferData(), nBytes);
567 }
568 }
569 else
570 { // (New in v8)
571 // Don't JPEG-compress behavior:
572 // Use negative image sizes to signal this behavior:
573 const int32_t neg_width = -width;
574 const int32_t neg_height = -height;
575
576 out << neg_width << neg_height;
577
578 // Dump raw image data:
579 const auto bytes_per_row = width * 3;
580
581 out.WriteBuffer(m_impl->img.data, bytes_per_row * height);
582 }
583 }
584
585 #endif
586 }
587
588 void CImage::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version)
589 {
590 #if !MRPT_HAS_OPENCV
591 if (version == 100)
592 {
593 in >> m_imgIsExternalStorage;
594 if (m_imgIsExternalStorage)
595 in >> m_externalFile;
596 else
597 {
598 THROW_EXCEPTION(
599 "[CImage] Cannot deserialize image since MRPT has been "
600 "compiled without OpenCV");
601 }
602 }
603 #else
604 // First, free current image.
605 clear();
606
607 switch (version)
608 {
609 case 100: // Saved from an MRPT build without OpenCV:
610 {
611 in >> m_imgIsExternalStorage;
612 if (m_imgIsExternalStorage) in >> m_externalFile;
613 }
614 break;
615 case 0:
616 {
617 uint32_t width, height, nChannels, imgLength;
618 uint8_t originTopLeft;
619
620 in >> width >> height >> nChannels >> originTopLeft >> imgLength;
621
622 resize(width, height, static_cast<TImageChannels>(nChannels));
623 in.ReadBuffer(m_impl->img.data, imgLength);
624 }
625 break;
626 case 1:
627 {
628 // Version 1: High quality JPEG image
629 uint32_t nBytes;
630 in >> nBytes;
631 std::vector<uint8_t> buf(nBytes);
632 in.ReadBuffer(buf.data(), nBytes);
633
634 mrpt::io::CMemoryStream aux;
635 aux.assignMemoryNotOwn(buf.data(), buf.size());
636 aux.Seek(0);
637 loadFromStreamAsJPEG(aux);
638 }
639 break;
640 case 2:
641 case 3:
642 case 4:
643 case 5:
644 case 6:
645 case 7:
646 case 8:
647 case 9:
648 {
649 // Version 6: m_imgIsExternalStorage ??
650 if (version >= 6)
651 in >> m_imgIsExternalStorage;
652 else
653 m_imgIsExternalStorage = false;
654
655 if (m_imgIsExternalStorage)
656 {
657 // Just the file name:
658 in >> m_externalFile;
659 }
660 else
661 { // Normal, the whole image data:
662
663 // Version 2: Color->JPEG, GrayScale->BYTE's array!
664 uint8_t hasColor;
665 in >> hasColor;
666 if (!hasColor)
667 {
668 // GRAY SCALE:
669 int32_t width, height, origin, imageSize;
670 in >> width >> height >> origin >> imageSize;
671 PixelDepth depth(PixelDepth::D8U);
672 if (version >= 9)
673 {
674 int32_t tempdepth;
675 in >> tempdepth;
676 depth = PixelDepth(tempdepth);
677 }
678 resize(static_cast<uint32_t>(width), static_cast<uint32_t>(height), CH_GRAY, depth);
679 ASSERT_EQUAL_(
680 static_cast<uint32_t>(imageSize),
681 static_cast<uint32_t>(height) * m_impl->img.step[0]);
682
683 if (version == 2)
684 {
685 // RAW BYTES:
686 in.ReadBuffer(m_impl->img.data, imageSize);
687 }
688 else
689 {
690 // Version 3: ZIP compression!
691 bool imageIsZIP = true;
692
693 // Version 4: Skip zip if the image size <= 16Kb
694 // Version 5: Use CImage::DISABLE_ZIP_COMPRESSION
695 if (version == 4 && imageSize <= 16 * 1024) imageIsZIP = false;
696
697 if (version >= 5)
698 {
699 // It is stored int the stream:
700 in >> imageIsZIP;
701 }
702
703 if (imageIsZIP)
704 {
705 uint32_t zipDataLen;
706 in >> zipDataLen;
707 THROW_EXCEPTION(
708 "ZIP image deserialization not supported "
709 "anymore");
710 }
711 else
712 {
713 // Raw bytes:
714 if (imageSize) in.ReadBuffer(m_impl->img.data, imageSize);
715 }
716 }
717 }
718 else
719 {
720 bool loadJPEG = true;
721
722 if (version >= 7)
723 {
724 int32_t width, height;
725 in >> width >> height;
726
727 if (width >= 1 && height >= 1)
728 {
729 loadJPEG = true;
730 }
731 else
732 {
733 loadJPEG = false;
734
735 if (width < 0 && height < 0)
736 {
737 // v8: raw image:
738 const int32_t real_w = -width;
739 const int32_t real_h = -height;
740
741 resize(real_w, real_h, CH_RGB);
742
743 auto& img = m_impl->img;
744 const size_t bytes_per_row = img.cols * 3;
745 for (int y = 0; y < img.rows; y++)
746 {
747 const size_t nRead = in.ReadBuffer(img.ptr<void>(y), bytes_per_row);
748 if (nRead != bytes_per_row)
749 THROW_EXCEPTION(
750 "Error: Truncated data stream "
751 "while parsing raw image?");
752 }
753 }
754 else
755 {
756 // it's a 0xN or Nx0 image: just resize and
757 // load nothing:
758 resize(width, height, CH_RGB);
759 }
760 }
761 }
762
763 // COLOR IMAGE: JPEG
764 if (loadJPEG)
765 {
766 uint32_t nBytes;
767 in >> nBytes;
768
769 std::vector<uint8_t> buf(nBytes);
770 in.ReadBuffer(buf.data(), nBytes);
771
772 mrpt::io::CMemoryStream aux;
773 aux.assignMemoryNotOwn(buf.data(), buf.size());
774 aux.Seek(0);
775
776 loadFromStreamAsJPEG(aux);
777 }
778 }
779 }
780 }
781 break;
782 default:
783 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
784 };
785 #endif
786 }
787
788 /*---------------------------------------------------------------
789 Implements the writing to a mxArray for Matlab
790 ---------------------------------------------------------------*/
791 #if MRPT_HAS_MATLAB
792 // Add to implement mexplus::from template specialization
793 IMPLEMENTS_MEXPLUS_FROM(mrpt::img::CImage)
794 #endif
795
796 mxArray* CImage::writeToMatlab() const
797 {
798 #if MRPT_HAS_MATLAB
799 return mexplus::from(this->asCvMatRef());
800 #else
801 THROW_EXCEPTION("MRPT built without MATLAB/Mex support");
802 #endif
803 }
804
805 void CImage::getSize(TImageSize& s) const
806 {
807 #if MRPT_HAS_OPENCV
808 makeSureImageIsLoaded(); // For delayed loaded images stored externally
809 s.x = m_impl->img.cols;
810 s.y = m_impl->img.rows;
811 #else
812 THROW_EXCEPTION("MRPT built without OpenCV support");
813 #endif
814 }
815
816 size_t CImage::getWidth() const
817 {
818 #if MRPT_HAS_OPENCV
819 makeSureImageIsLoaded();
820 return m_impl->img.cols;
821 #else
822 THROW_EXCEPTION("MRPT built without OpenCV support");
823 #endif
824 }
825
826 std::string CImage::getChannelsOrder() const
827 {
828 #if MRPT_HAS_OPENCV
829 makeSureImageIsLoaded(); // For delayed loaded images stored externally
830
831 // modern opencv versions used these fixed order (see opencv
832 // core/src/array.cpp)
833 const int chCount = m_impl->img.channels();
834 ASSERT_GE_(chCount, 1);
835 ASSERT_LE_(chCount, 4);
836 const std::array<const char*, 4> orderNames = {"GRAY", "", "BGR", "BGRA"};
837 return std::string(orderNames.at(chCount - 1));
838 #else
839 THROW_EXCEPTION("MRPT built without OpenCV support");
840 #endif
841 }
842
843 size_t CImage::getRowStride() const
844 {
845 #if MRPT_HAS_OPENCV
846 makeSureImageIsLoaded(); // For delayed loaded images stored externally
847 return m_impl->img.step[0];
848 #else
849 THROW_EXCEPTION("MRPT built without OpenCV support");
850 #endif
851 }
852
853 size_t CImage::getHeight() const
854 {
855 #if MRPT_HAS_OPENCV
856 makeSureImageIsLoaded();
857 return m_impl->img.rows;
858 #else
859 THROW_EXCEPTION("MRPT built without OpenCV support");
860 #endif
861 }
862
863 bool CImage::isColor() const
864 {
865 #if MRPT_HAS_OPENCV
866 makeSureImageIsLoaded(); // For delayed loaded images stored externally
867 return m_impl->img.channels() >= 3;
868 #else
869 THROW_EXCEPTION("MRPT built without OpenCV support");
870 #endif
871 }
872
873 int CImage::channelCount() const
874 {
875 #if MRPT_HAS_OPENCV
876 makeSureImageIsLoaded(); // For delayed loaded images stored externally
877 return m_impl->img.channels();
878 #else
879 THROW_EXCEPTION("MRPT built without OpenCV support");
880 #endif
881 }
882
883 bool CImage::isEmpty() const
884 {
885 #if MRPT_HAS_OPENCV
886 return !m_imgIsExternalStorage && m_impl->img.empty();
887 #else
888 return true;
889 #endif
890 }
891
892 TImageChannels CImage::getChannelCount() const
893 {
894 #if MRPT_HAS_OPENCV
895 makeSureImageIsLoaded(); // For delayed loaded images stored externally
896 return static_cast<TImageChannels>(m_impl->img.channels());
897 #else
898 THROW_EXCEPTION("MRPT built without OpenCV support");
899 #endif
900 }
901
902 bool CImage::isOriginTopLeft() const
903 {
904 return true; // As of mrpt v1.9.9
905 }
906
907 float CImage::getAsFloat(unsigned int col, unsigned int row, unsigned int channel) const
908 {
909 makeSureImageIsLoaded(); // For delayed loaded images stored externally
910 // [0,255]->[0,1]
911 return (*(*this)(col, row, channel)) / 255.0f;
912 }
913
914 float CImage::getAsFloat(unsigned int col, unsigned int row) const
915 {
916 // Is a RGB image??
917 if (isColor())
918 {
919 // Luminance: Y = 0.3R + 0.59G + 0.11B
920 unsigned char* pixels = (*this)(col, row, 0);
921 return (pixels[0] * 0.3f + pixels[1] * 0.59f + pixels[2] * 0.11f) / 255.0f;
922 }
923 else
924 {
925 // [0,255]->[0,1]
926 return (*(*this)(col, row, 0 /* Channel 0:Gray level */)) / 255.0f;
927 }
928 }
929
930 /*---------------------------------------------------------------
931 getMaxAsFloat
932 ---------------------------------------------------------------*/
933 float CImage::getMaxAsFloat() const
934 {
935 int x, y, cx = getWidth(), cy = getHeight();
936
937 float maxPixel = 0;
938
939 for (x = 0; x < cx; x++)
940 for (y = 0; y < cy; y++) maxPixel = max(maxPixel, getAsFloat(x, y));
941
942 return maxPixel;
943 }
944
945 CImage CImage::grayscale() const
946 {
947 CImage ret;
948 grayscale(ret);
949 return ret;
950 }
951
952 // Auxiliary function for both ::grayscale() and ::grayscaleInPlace()
953 #if MRPT_HAS_OPENCV
954 namespace
955 {
956 bool my_img_to_grayscale(const cv::Mat& src, cv::Mat& dest)
957 {
958 if (dest.size() != src.size() || dest.type() != src.type())
959 dest = cv::Mat(src.rows, src.cols, CV_8UC1);
960
961 // If possible, use SSE optimized version:
962 #if MRPT_ARCH_INTEL_COMPATIBLE
963 if ((src.step[0] & 0x0f) == 0 && (dest.step[0] & 0x0f) == 0 &&
964 mrpt::cpu::supports(mrpt::cpu::feature::SSSE3))
965 {
966 image_SSSE3_bgr_to_gray_8u(
967 src.ptr<uint8_t>(), dest.ptr<uint8_t>(), src.cols, src.rows, src.step[0], dest.step[0]);
968 return true;
969 }
970 #endif
971
972 // OpenCV Method:
973 cv::cvtColor(src, dest, cv::COLOR_BGR2GRAY);
974 return false;
975 }
976 } // namespace
977 #endif
978
979 bool CImage::grayscale(CImage& ret) const
980 {
981 #if MRPT_HAS_OPENCV
982 // The image is already grayscale??
983 makeSureImageIsLoaded(); // For delayed loaded images stored externally
984 if (m_impl->img.channels() == 1)
985 {
986 ret = *this; // shallow copy
987 return true;
988 }
989 else
990 {
991 // Convert to a single luminance channel image
992 cv::Mat src = m_impl->img;
993 // Detect in-place op and make deep copy:
994 if (src.data == ret.m_impl->img.data) src = src.clone();
995
996 return my_img_to_grayscale(src, ret.m_impl->img);
997 }
998 #else
999 THROW_EXCEPTION("Operation not supported: build MRPT against OpenCV!");
1000 #endif
1001 }
1002
1003 bool CImage::scaleHalf(CImage& out, TInterpolationMethod interp) const
1004 {
1005 #if MRPT_HAS_OPENCV
1006 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1007 // Get this image size:
1008 auto& img = m_impl->img;
1009 const int w = img.cols, h = img.rows;
1010
1011 // Create target image:
1012 out.resize(w >> 1, h >> 1, getChannelCount());
1013 auto& img_out = out.m_impl->img;
1014
1015 // If possible, use SSE optimized version:
1016 #if MRPT_ARCH_INTEL_COMPATIBLE
1017 if (img.channels() == 3 && interp == IMG_INTERP_NN &&
1018 mrpt::cpu::supports(mrpt::cpu::feature::SSSE3))
1019 {
1020 image_SSSE3_scale_half_3c8u(img.data, img_out.data, w, h, img.step[0], img_out.step[0]);
1021 return true;
1022 }
1023
1024 if (img.channels() == 1 && mrpt::cpu::supports(mrpt::cpu::feature::SSE2))
1025 {
1026 if (interp == IMG_INTERP_NN)
1027 {
1028 image_SSE2_scale_half_1c8u(img.data, img_out.data, w, h, img.step[0], img_out.step[0]);
1029 return true;
1030 }
1031 else if (interp == IMG_INTERP_LINEAR)
1032 {
1033 image_SSE2_scale_half_smooth_1c8u(img.data, img_out.data, w, h, img.step[0], img_out.step[0]);
1034 return true;
1035 }
1036 }
1037 #endif
1038
1039 // Fall back to slow method:
1040 cv::resize(img, img_out, img_out.size(), 0, 0, interpolationMethod2Cv(interp));
1041 return false;
1042 #else
1043 THROW_EXCEPTION("Operation not supported: build MRPT against OpenCV!");
1044 #endif
1045 }
1046
1047 void CImage::scaleDouble(CImage& out, TInterpolationMethod interp) const
1048 {
1049 out = *this;
1050 const TImageSize siz = this->getSize();
1051 out.scaleImage(out, siz.x * 2, siz.y * 2, interp);
1052 }
1053
1054 void CImage::loadFromMemoryBuffer(
1055 unsigned int width,
1056 unsigned int height,
1057 unsigned int bytesPerRow,
1058 unsigned char* red,
1059 unsigned char* green,
1060 unsigned char* blue)
1061 {
1062 #if MRPT_HAS_OPENCV
1063 MRPT_START
1064
1065 resize(width, height, CH_RGB, PixelDepth::D8U);
1066
1067 // Copy the image data:
1068 for (unsigned int y = 0; y < height; y++)
1069 {
1070 // The target pixels:
1071 auto* dest = m_impl->img.ptr<uint8_t>(y);
1072
1073 // Source channels:
1074 unsigned char* srcR = red + bytesPerRow * y;
1075 unsigned char* srcG = green + bytesPerRow * y;
1076 unsigned char* srcB = blue + bytesPerRow * y;
1077
1078 for (unsigned int x = 0; x < width; x++)
1079 {
1080 *(dest++) = *(srcB++);
1081 *(dest++) = *(srcG++);
1082 *(dest++) = *(srcR++);
1083 } // end of x
1084 } // end of y
1085
1086 MRPT_END
1087 #endif
1088 }
1089
1090 void CImage::setPixel(int x, int y, size_t color)
1091 {
1092 #if MRPT_HAS_OPENCV
1093
1094 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG)
1095 MRPT_START
1096 #endif
1097
1098 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1099 auto& img = m_impl->img;
1100
1101 ASSERT_(this->getPixelDepth() == mrpt::img::PixelDepth::D8U);
1102
1103 if (x >= 0 && y >= 0 && y < img.rows && x < img.cols)
1104 {
1105 // The pixel coordinates is valid:
1106 if (img.channels() == 1)
1107 {
1108 img.ptr<uint8_t>(y)[x] = static_cast<uint8_t>(color);
1109 }
1110 else
1111 {
1112 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG)
1113 ASSERT_(img.channels() == 3);
1114 #endif
1115 auto* dest = &img.ptr<uint8_t>(y)[3 * x];
1116 const auto* src = reinterpret_cast<uint8_t*>(&color);
1117 // Copy the color:
1118 *dest++ = *src++; // R
1119 *dest++ = *src++; // G
1120 *dest++ = *src++; // B
1121 }
1122 }
1123
1124 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG)
1125 MRPT_END
1126 #endif
1127
1128 #endif
1129 }
1130
1131 void CImage::line(
1132 int x0,
1133 int y0,
1134 int x1,
1135 int y1,
1136 const mrpt::img::TColor color,
1137 unsigned int width,
1138 [[maybe_unused]] TPenStyle penStyle)
1139 {
1140 #if MRPT_HAS_OPENCV
1141 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1142
1143 cv::line(
1144 m_impl->img, cv::Point(x0, y0), cv::Point(x1, y1), CV_RGB(color.R, color.G, color.B),
1145 static_cast<int>(width));
1146 #endif
1147 }
1148
1149 void CImage::drawCircle(
1150 int x, int y, int radius, const mrpt::img::TColor& color, unsigned int width)
1151 {
1152 #if MRPT_HAS_OPENCV
1153 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1154 cv::circle(
1155 m_impl->img, cv::Point(x, y), radius, CV_RGB(color.R, color.G, color.B),
1156 static_cast<int>(width));
1157 #endif
1158 }
1159
1160 void CImage::filledRectangle(int x0, int y0, int x1, int y1, const mrpt::img::TColor color)
1161 {
1162 #if MRPT_HAS_OPENCV
1163 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1164 cv::rectangle(m_impl->img, {x0, y0}, {x1, y1}, CV_RGB(color.R, color.G, color.B), cv::FILLED);
1165 #endif
1166 }
1167
1168 void CImage::drawImage(int x, int y, const mrpt::img::CImage& img)
1169 {
1170 #if MRPT_HAS_OPENCV
1171 makeSureImageIsLoaded();
1172 img.makeSureImageIsLoaded();
1173
1174 cv::Rect roi(cv::Point(x, y), cv::Size(img.getWidth(), img.getHeight()));
1175 cv::Mat dest = m_impl->img(roi);
1176 img.m_impl->img.copyTo(dest);
1177 #endif
1178 }
1179
1180 void CImage::update_patch(const CImage& patch, const unsigned int col_, const unsigned int row_)
1181 {
1182 #if MRPT_HAS_OPENCV
1183 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1184 const auto& src = m_impl->img;
1185 auto& dest = patch.m_impl->img;
1186
1187 src(cv::Rect(col_, row_, dest.cols, dest.rows)).copyTo(dest);
1188 #endif
1189 }
1190
1191 void CImage::extract_patch(
1192 CImage& patch,
1193 const unsigned int col_,
1194 const unsigned int row_,
1195 const unsigned int col_num,
1196 const unsigned int row_num) const
1197 {
1198 #if MRPT_HAS_OPENCV
1199 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1200 const auto& src = m_impl->img;
1201 auto& dest = patch.m_impl->img;
1202
1203 src(cv::Rect(col_, row_, col_num, row_num)).copyTo(dest);
1204 #endif
1205 }
1206
1207 float CImage::correlate(const CImage& img2, int width_init, int height_init) const
1208 {
1209 #if MRPT_HAS_OPENCV
1210 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1211
1212 if ((img2.getWidth() + width_init > getWidth()) | (img2.getHeight() + height_init > getHeight()))
1213 THROW_EXCEPTION("Correlation Error!, image to correlate out of bounds");
1214
1215 float x1, x2;
1216 float syy = 0.0f, sxy = 0.0f, sxx = 0.0f, m1 = 0.0f, m2 = 0.0f,
1217 n = (float)(img2.getHeight() * img2.getWidth());
1218
1219 // find the means
1220 for (size_t i = 0; i < img2.getHeight(); i++)
1221 {
1222 for (size_t j = 0; j < img2.getWidth(); j++)
1223 {
1224 m1 += *(*this)(j + width_init, i + height_init);
1225 m2 += *img2(j, i);
1226 }
1227 }
1228 m1 /= n;
1229 m2 /= n;
1230
1231 for (size_t i = 0; i < img2.getHeight(); i++)
1232 {
1233 for (size_t j = 0; j < img2.getWidth(); j++)
1234 {
1235 x1 = *(*this)(j + width_init, i + height_init) - m1;
1236 x2 = *img2(j, i) - m2;
1237 sxx += x1 * x1;
1238 syy += x2 * x2;
1239 sxy += x1 * x2;
1240 }
1241 }
1242
1243 return sxy / sqrt(sxx * syy);
1244 #else
1245 return 0;
1246 #endif
1247 }
1248
1249 void CImage::normalize()
1250 {
1251 #if MRPT_HAS_OPENCV
1252 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1253 cv::normalize(m_impl->img, m_impl->img, 255, 0, cv::NORM_MINMAX);
1254 #endif
1255 }
1256
1257 void CImage::getAsMatrix(
1258 CMatrixFloat& outMatrix,
1259 bool doResize,
1260 int x_min,
1261 int y_min,
1262 int x_max,
1263 int y_max,
1264 bool normalize_01) const
1265 {
1266 #if MRPT_HAS_OPENCV
1267 MRPT_START
1268 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1269
1270 const auto& img = m_impl->img;
1271
1272 // Set sizes:
1273 if (x_max == -1) x_max = img.cols - 1;
1274 if (y_max == -1) y_max = img.rows - 1;
1275
1276 ASSERT_(x_min >= 0 && x_min < img.cols && x_min < x_max);
1277 ASSERT_(y_min >= 0 && y_min < img.rows && y_min < y_max);
1278
1279 int lx = (x_max - x_min + 1);
1280 int ly = (y_max - y_min + 1);
1281
1282 if (doResize || outMatrix.rows() < ly || outMatrix.cols() < lx)
1283 outMatrix.setSize(y_max - y_min + 1, x_max - x_min + 1);
1284
1285 const bool is_color = isColor();
1286
1287 // Luminance: Y = 0.3R + 0.59G + 0.11B
1288 for (int y = 0; y < ly; y++)
1289 {
1290 const uint8_t* pixels = ptr<uint8_t>(x_min, y_min + y);
1291 for (int x = 0; x < lx; x++)
1292 {
1293 float aux;
1294 if (is_color)
1295 {
1296 aux = *pixels++ * 0.3f;
1297 aux += *pixels++ * 0.59f;
1298 aux += *pixels++ * 0.11f;
1299 }
1300 else
1301 {
1302 aux = (*pixels++);
1303 }
1304 outMatrix.coeffRef(y, x) = aux;
1305 }
1306 }
1307 if (normalize_01) outMatrix *= (1.0f / 255);
1308
1309 MRPT_END
1310 #endif
1311 }
1312
1313 void CImage::getAsMatrix(
1314 CMatrix_u8& outMatrix, bool doResize, int x_min, int y_min, int x_max, int y_max) const
1315 {
1316 #if MRPT_HAS_OPENCV
1317 MRPT_START
1318 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1319
1320 const auto& img = m_impl->img;
1321
1322 // Set sizes:
1323 if (x_max == -1) x_max = img.cols - 1;
1324 if (y_max == -1) y_max = img.rows - 1;
1325
1326 ASSERT_(x_min >= 0 && x_min < img.cols && x_min < x_max);
1327 ASSERT_(y_min >= 0 && y_min < img.rows && y_min < y_max);
1328
1329 int lx = (x_max - x_min + 1);
1330 int ly = (y_max - y_min + 1);
1331
1332 if (doResize || outMatrix.rows() < ly || outMatrix.cols() < lx)
1333 outMatrix.setSize(y_max - y_min + 1, x_max - x_min + 1);
1334
1335 const bool is_color = isColor();
1336
1337 // Luminance: Y = 0.3R + 0.59G + 0.11B
1338 for (int y = 0; y < ly; y++)
1339 {
1340 const uint8_t* pixels = ptr<uint8_t>(x_min, y_min + y);
1341 for (int x = 0; x < lx; x++)
1342 {
1343 if (is_color)
1344 {
1345 unsigned int aux = *pixels++ * 3000; // 0.3f;
1346 aux += *pixels++ * 5900; // 0.59f;
1347 aux += *pixels++ * 1100; // 0.11f;
1348 outMatrix.coeffRef(y, x) = static_cast<uint8_t>(aux / 1000);
1349 }
1350 else
1351 {
1352 outMatrix.coeffRef(y, x) = (*pixels++);
1353 }
1354 }
1355 }
1356
1357 MRPT_END
1358 #endif
1359 }
1360 void CImage::getAsRGBMatrices(
1361 mrpt::math::CMatrixFloat& R,
1362 mrpt::math::CMatrixFloat& G,
1363 mrpt::math::CMatrixFloat& B,
1364 bool doResize,
1365 int x_min,
1366 int y_min,
1367 int x_max,
1368 int y_max) const
1369 {
1370 #if MRPT_HAS_OPENCV
1371 MRPT_START
1372
1373 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1374 const auto& img = m_impl->img;
1375
1376 // Set sizes:
1377 if (x_max == -1) x_max = img.cols - 1;
1378 if (y_max == -1) y_max = img.rows - 1;
1379
1380 ASSERT_(x_min >= 0 && x_min < img.cols && x_min < x_max);
1381 ASSERT_(y_min >= 0 && y_min < img.rows && y_min < y_max);
1382
1383 int lx = (x_max - x_min + 1);
1384 int ly = (y_max - y_min + 1);
1385
1386 if (doResize || R.rows() < ly || R.cols() < lx) R.setSize(ly, lx);
1387 if (doResize || G.rows() < ly || G.cols() < lx) G.setSize(ly, lx);
1388 if (doResize || B.rows() < ly || B.cols() < lx) B.setSize(ly, lx);
1389
1390 const bool is_color = isColor();
1391 for (int y = 0; y < ly; y++)
1392 {
1393 const uint8_t* pixels = ptr<uint8_t>(x_min, y_min + y);
1394 for (int x = 0; x < lx; x++)
1395 {
1396 if (is_color)
1397 {
1398 R.coeffRef(y, x) = u8tof(*pixels++);
1399 G.coeffRef(y, x) = u8tof(*pixels++);
1400 B.coeffRef(y, x) = u8tof(*pixels++);
1401 }
1402 else
1403 {
1404 R.coeffRef(y, x) = G.coeffRef(y, x) = B.coeffRef(y, x) = u8tof(*pixels++);
1405 }
1406 }
1407 }
1408
1409 MRPT_END
1410 #endif
1411 }
1412
1413 void CImage::getAsRGBMatrices(
1414 mrpt::math::CMatrix_u8& R,
1415 mrpt::math::CMatrix_u8& G,
1416 mrpt::math::CMatrix_u8& B,
1417 bool doResize,
1418 int x_min,
1419 int y_min,
1420 int x_max,
1421 int y_max) const
1422 {
1423 #if MRPT_HAS_OPENCV
1424 MRPT_START
1425
1426 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1427 const auto& img = m_impl->img;
1428
1429 // Set sizes:
1430 if (x_max == -1) x_max = img.cols - 1;
1431 if (y_max == -1) y_max = img.rows - 1;
1432
1433 ASSERT_(x_min >= 0 && x_min < img.cols && x_min < x_max);
1434 ASSERT_(y_min >= 0 && y_min < img.rows && y_min < y_max);
1435
1436 int lx = (x_max - x_min + 1);
1437 int ly = (y_max - y_min + 1);
1438
1439 if (doResize || R.rows() < ly || R.cols() < lx) R.setSize(ly, lx);
1440 if (doResize || G.rows() < ly || G.cols() < lx) G.setSize(ly, lx);
1441 if (doResize || B.rows() < ly || B.cols() < lx) B.setSize(ly, lx);
1442
1443 const bool is_color = isColor();
1444 for (int y = 0; y < ly; y++)
1445 {
1446 const uint8_t* pixels = ptr<uint8_t>(x_min, y_min + y);
1447 for (int x = 0; x < lx; x++)
1448 {
1449 if (is_color)
1450 {
1451 R.coeffRef(y, x) = *pixels++;
1452 G.coeffRef(y, x) = *pixels++;
1453 B.coeffRef(y, x) = *pixels++;
1454 }
1455 else
1456 {
1457 R.coeffRef(y, x) = G.coeffRef(y, x) = B.coeffRef(y, x) = *pixels++;
1458 }
1459 }
1460 }
1461
1462 MRPT_END
1463 #endif
1464 }
1465
1466 void CImage::cross_correlation_FFT(
1467 const CImage& in_img,
1468 CMatrixFloat& out_corr,
1469 int u_search_ini,
1470 int v_search_ini,
1471 int u_search_size,
1472 int v_search_size,
1473 float biasThisImg,
1474 float biasInImg) const
1475 {
1476 MRPT_START
1477
1478 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1479
1480 // Set limits:
1481 if (u_search_ini == -1) u_search_ini = 0;
1482 if (v_search_ini == -1) v_search_ini = 0;
1483 if (u_search_size == -1) u_search_size = static_cast<int>(getWidth());
1484 if (v_search_size == -1) v_search_size = static_cast<int>(getHeight());
1485
1486 int u_search_end = u_search_ini + u_search_size - 1;
1487 int v_search_end = v_search_ini + v_search_size - 1;
1488
1489 ASSERT_(u_search_end < static_cast<int>(getWidth()));
1490 ASSERT_(v_search_end < static_cast<int>(getHeight()));
1491
1492 // Find smallest valid size:
1493 size_t x, y;
1494 size_t actual_lx = std::max(static_cast<size_t>(u_search_size), in_img.getWidth());
1495 size_t actual_ly = std::max(static_cast<size_t>(v_search_size), in_img.getHeight());
1496 size_t lx = mrpt::round2up<size_t>(actual_lx);
1497 size_t ly = mrpt::round2up<size_t>(actual_ly);
1498
1499 CMatrixF i1(ly, lx), i2(ly, lx);
1500
1501 // We fill the images with the bias, such as when we substract the bias
1502 // later on,
1503 // those pixels not really occupied by the image really becomes zero:
1504 i1.fill(biasInImg);
1505 i2.fill(biasThisImg);
1506
1507 // Get as matrixes, padded with zeros up to power-of-two sizes:
1508 getAsMatrix(
1509 i2, false, u_search_ini, v_search_ini, u_search_ini + u_search_size - 1,
1510 v_search_ini + v_search_size - 1);
1511 in_img.getAsMatrix(i1, false);
1512
1513 // Remove the bias now:
1514 i2 -= biasThisImg;
1515 i1 -= biasInImg;
1516
1517 // FFT:
1518 CMatrixF I1_R, I1_I, I2_R, I2_I, ZEROS(ly, lx);
1519 math::dft2_complex(i1, ZEROS, I1_R, I1_I);
1520 math::dft2_complex(i2, ZEROS, I2_R, I2_I);
1521
1522 // Compute the COMPLEX division of I2 by I1:
1523 for (y = 0; y < ly; y++)
1524 for (x = 0; x < lx; x++)
1525 {
1526 float r1 = I1_R(y, x);
1527 float r2 = I2_R(y, x);
1528
1529 float ii1 = I1_I(y, x);
1530 float ii2 = I2_I(y, x);
1531
1532 float den = square(r1) + square(ii1);
1533 I2_R(y, x) = (r1 * r2 + ii1 * ii2) / den;
1534 I2_I(y, x) = (ii2 * r1 - r2 * ii1) / den;
1535 }
1536
1537 // IFFT:
1538 CMatrixF res_R, res_I;
1539 math::idft2_complex(I2_R, I2_I, res_R, res_I);
1540
1541 out_corr.setSize(actual_ly, actual_lx);
1542 for (y = 0; y < actual_ly; y++)
1543 for (x = 0; x < actual_lx; x++)
1544 out_corr(y, x) = sqrt(square(res_R(y, x)) + square(res_I(y, x)));
1545
1546 MRPT_END
1547 }
1548
1549 void CImage::getAsMatrixTiled(CMatrixFloat& outMatrix) const
1550 {
1551 #if MRPT_HAS_OPENCV
1552 MRPT_START
1553
1554 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1555 const auto& img = m_impl->img;
1556
1557 // The size of the matrix:
1558 const auto matrix_lx = outMatrix.cols();
1559 const auto matrix_ly = outMatrix.rows();
1560
1561 if (isColor())
1562 {
1563 // Luminance: Y = 0.3R + 0.59G + 0.11B
1564 for (CMatrixFloat::Index y = 0; y < matrix_ly; y++)
1565 {
1566 unsigned char* min_pixels = (*this)(0, y % img.rows, 0);
1567 unsigned char* max_pixels = min_pixels + img.cols * 3;
1568 unsigned char* pixels = min_pixels;
1569 float aux;
1570 for (CMatrixFloat::Index x = 0; x < matrix_lx; x++)
1571 {
1572 aux = *pixels++ * 0.30f;
1573 aux += *pixels++ * 0.59f;
1574 aux += *pixels++ * 0.11f;
1575 outMatrix(y, x) = aux;
1576 if (pixels >= max_pixels) pixels = min_pixels;
1577 }
1578 }
1579 }
1580 else
1581 {
1582 for (CMatrixFloat::Index y = 0; y < matrix_ly; y++)
1583 {
1584 unsigned char* min_pixels = (*this)(0, y % img.rows, 0);
1585 unsigned char* max_pixels = min_pixels + img.cols;
1586 unsigned char* pixels = min_pixels;
1587 for (CMatrixFloat::Index x = 0; x < matrix_lx; x++)
1588 {
1589 outMatrix(y, x) = *pixels++;
1590 if (pixels >= max_pixels) pixels = min_pixels;
1591 }
1592 }
1593 }
1594
1595 MRPT_END
1596 #endif
1597 }
1598
1599 void CImage::clear()
1600 {
1601 // Reset to defaults:
1602 *this = CImage();
1603 }
1604
1605 void CImage::setExternalStorage(const std::string& fileName) noexcept
1606 {
1607 clear();
1608 m_externalFile = fileName;
1609 m_imgIsExternalStorage = true;
1610 }
1611
1612 void CImage::unload() const noexcept
1613 {
1614 #if MRPT_HAS_OPENCV
1615 if (m_imgIsExternalStorage)
1616 {
1617 if (MRPT_DEBUG_IMG_LAZY_LOAD)
1618 std::cout << "[CImage::unload()] Called on this=" << reinterpret_cast<const void*>(this)
1619 << std::endl;
1620
1621 const_cast<cv::Mat&>(m_impl->img) = cv::Mat();
1622 }
1623 #endif
1624 }
1625
1626 void CImage::makeSureImageIsLoaded(bool allowNonInitialized) const
1627 {
1628 #if MRPT_HAS_OPENCV
1629 if (!m_impl->img.empty()) return; // OK, continue
1630 #endif
1631
1632 if (m_imgIsExternalStorage)
1633 {
1634 // Load the file:
1635 string wholeFile;
1636 getExternalStorageFileAbsolutePath(wholeFile);
1637
1638 const std::string tmpFile = m_externalFile;
1639
1640 bool ret = const_cast<CImage*>(this)->loadFromFile(wholeFile);
1641
1642 // These are removed by "loadFromFile", and that's good, just fix it
1643 // here and carry on.
1644 m_imgIsExternalStorage = true;
1645 m_externalFile = tmpFile;
1646
1647 if (!ret)
1648 THROW_TYPED_EXCEPTION_FMT(
1649 CExceptionExternalImageNotFound, "Error loading externally-stored image from: %s",
1650 wholeFile.c_str());
1651
1652 if (MRPT_DEBUG_IMG_LAZY_LOAD)
1653 std::cout << "[CImage] Loaded lazy-load image file '" << wholeFile
1654 << "' on this=" << reinterpret_cast<const void*>(this) << std::endl;
1655 }
1656 else if (!allowNonInitialized)
1657 {
1658 THROW_EXCEPTION(
1659 "Trying to access uninitialized image in a non "
1660 "externally-stored "
1661 "image.");
1662 }
1663 }
1664
1665 void CImage::getExternalStorageFileAbsolutePath(std::string& out_path) const
1666 {
1667 out_path = mrpt::io::lazy_load_absolute_path(m_externalFile);
1668 }
1669
1670 void CImage::flipVertical()
1671 {
1672 #if MRPT_HAS_OPENCV
1673 makeSureImageIsLoaded();
1674 cv::flip(m_impl->img, m_impl->img, 0 /* x-axis */);
1675 #endif
1676 }
1677
1678 void CImage::flipHorizontal()
1679 {
1680 #if MRPT_HAS_OPENCV
1681 makeSureImageIsLoaded();
1682 cv::flip(m_impl->img, m_impl->img, 1 /* y-axis */);
1683 #endif
1684 }
1685
1686 void CImage::swapRB()
1687 {
1688 #if MRPT_HAS_OPENCV
1689 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1690 cv::cvtColor(m_impl->img, m_impl->img, cv::COLOR_RGB2BGR);
1691 #endif
1692 }
1693
1694 void CImage::rectifyImageInPlace(void* mapX, void* mapY)
1695 {
1696 #if MRPT_HAS_OPENCV
1697 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1698
1699 auto& srcImg = m_impl->img;
1700 cv::Mat outImg(srcImg.rows, srcImg.cols, srcImg.type());
1701
1702 auto mapXm = static_cast<cv::Mat*>(mapX);
1703 auto mapYm = static_cast<cv::Mat*>(mapX);
1704
1705 cv::remap(srcImg, outImg, *mapXm, *mapYm, cv::INTER_CUBIC);
1706
1707 clear();
1708 srcImg = outImg;
1709 #endif
1710 }
1711
1712 void CImage::undistort(CImage& out_img, const mrpt::img::TCamera& cameraParams) const
1713 {
1714 #if MRPT_HAS_OPENCV
1715 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1716
1717 ASSERTMSG_(out_img.m_impl->img.data != m_impl->img.data, "In-place undistort() not supported");
1718
1719 auto& srcImg = const_cast<cv::Mat&>(m_impl->img);
1720 // This will avoid re-alloc if size already matches.
1721 out_img.resize(srcImg.cols, srcImg.rows, getChannelCount());
1722
1723 const auto& intrMat = cameraParams.intrinsicParams;
1724 const auto& dist = cameraParams.dist;
1725
1726 cv::Mat distM(1, dist.size(), CV_64F, const_cast<double*>(&dist[0]));
1727 cv::Mat inMat(3, 3, CV_64F);
1728
1729 for (int i = 0; i < 3; i++)
1730 for (int j = 0; j < 3; j++) inMat.at<double>(i, j) = intrMat(i, j);
1731
1732 cv::undistort(srcImg, out_img.m_impl->img, inMat, distM);
1733 #endif
1734 }
1735
1736 void CImage::filterMedian(CImage& out_img, int W) const
1737 {
1738 #if MRPT_HAS_OPENCV
1739 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1740
1741 auto srcImg = const_cast<cv::Mat&>(m_impl->img);
1742 if (this == &out_img)
1743 srcImg = srcImg.clone();
1744 else
1745 out_img.resize(srcImg.cols, srcImg.rows, getChannelCount());
1746
1747 cv::medianBlur(srcImg, out_img.m_impl->img, W);
1748 #endif
1749 }
1750
1751 void CImage::filterGaussian(CImage& out_img, int W, int H, double sigma) const
1752 {
1753 #if MRPT_HAS_OPENCV
1754 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1755 auto srcImg = const_cast<cv::Mat&>(m_impl->img);
1756 if (this == &out_img)
1757 srcImg = srcImg.clone();
1758 else
1759 out_img.resize(srcImg.cols, srcImg.rows, getChannelCount());
1760
1761 cv::GaussianBlur(srcImg, out_img.m_impl->img, cv::Size(W, H), sigma);
1762 #endif
1763 }
1764
1765 void CImage::scaleImage(
1766 CImage& out_img, unsigned int width, unsigned int height, TInterpolationMethod interp) const
1767 {
1768 #if MRPT_HAS_OPENCV
1769 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1770
1771 auto srcImg = m_impl->img;
1772 // Detect in-place operation and make a deep copy if needed:
1773 if (out_img.m_impl->img.data == srcImg.data) srcImg = srcImg.clone();
1774
1775 // Already done?
1776 if (srcImg.cols == static_cast<int>(width) && srcImg.rows == static_cast<int>(height))
1777 {
1778 out_img.m_impl->img = srcImg;
1779 return;
1780 }
1781 out_img.resize(width, height, getChannelCount());
1782
1783 // Resize:
1784 cv::resize(
1785 srcImg, out_img.m_impl->img, out_img.m_impl->img.size(), 0, 0,
1786 interpolationMethod2Cv(interp));
1787 #endif
1788 }
1789
1790 void CImage::rotateImage(
1791 CImage& out_img, double ang, unsigned int cx, unsigned int cy, double scale) const
1792 {
1793 #if MRPT_HAS_OPENCV
1794 makeSureImageIsLoaded(); // For delayed loaded images stored externally
1795
1796 auto srcImg = m_impl->img;
1797 // Detect in-place operation and make a deep copy if needed:
1798 if (out_img.m_impl->img.data == srcImg.data) srcImg = srcImg.clone();
1799
1800 // quick rotation?
1801 if (std::abs(M_PI * 0.5 - std::abs(ang)) < 1e-3 || std::abs(M_PI - std::abs(ang)) < 1e-3)
1802 {
1803 int rotCode = 0;
1804 if (std::abs(M_PI * 0.5 - ang) < 1e-3)
1805 rotCode = cv::ROTATE_90_COUNTERCLOCKWISE;
1806 else if (std::abs(-M_PI * 0.5 - ang) < 1e-3)
1807 rotCode = cv::ROTATE_90_CLOCKWISE;
1808 else if (std::abs(M_PI - ang) < 1e-3)
1809 rotCode = cv::ROTATE_180;
1810
1811 cv::rotate(srcImg, out_img.m_impl->img, rotCode);
1812 return;
1813 }
1814 // else: general rotation:
1815
1816 out_img.resize(getWidth(), getHeight(), getChannelCount());
1817
1818 // Based on the blog entry:
1819 // http://blog.weisu.org/2007/12/opencv-image-rotate-and-zoom-rotation.html
1820
1821 // Apply rotation & scale:
1822 double m[2 * 3] = {scale * cos(ang), -scale * sin(ang), 1.0 * cx,
1823 scale * sin(ang), scale * cos(ang), 1.0 * cy};
1824 cv::Mat M(2, 3, CV_64F, m);
1825
1826 double dx = (srcImg.cols - 1) * 0.5;
1827 double dy = (srcImg.rows - 1) * 0.5;
1828 m[2] -= m[0] * dx + m[1] * dy;
1829 m[5] -= m[3] * dx + m[4] * dy;
1830
1831 cv::warpAffine(
1832 srcImg, out_img.m_impl->img, M, out_img.m_impl->img.size(),
1833 cv::INTER_LINEAR + cv::WARP_INVERSE_MAP, cv::BORDER_REPLICATE);
1834 #endif
1835 }
1836
1837 bool CImage::drawChessboardCorners(
1838 const std::vector<TPixelCoordf>& cornerCoords,
1839 unsigned int check_size_x,
1840 unsigned int check_size_y,
1841 unsigned int lines_width,
1842 unsigned int r)
1843 {
1844 #if MRPT_HAS_OPENCV
1845
1846 if (cornerCoords.size() != check_size_x * check_size_y) return false;
1847
1848 auto& img = m_impl->img;
1849
1850 unsigned int x, y, i;
1851 cv::Point prev_pt = cvPoint(0, 0);
1852 const int line_max = 8;
1853 cv::Scalar line_colors[8];
1854
1855 line_colors[0] = CV_RGB(255, 0, 0);
1856 line_colors[1] = CV_RGB(255, 128, 0);
1857 line_colors[2] = CV_RGB(255, 128, 0);
1858 line_colors[3] = CV_RGB(200, 200, 0);
1859 line_colors[4] = CV_RGB(0, 255, 0);
1860 line_colors[5] = CV_RGB(0, 200, 200);
1861 line_colors[6] = CV_RGB(0, 0, 255);
1862 line_colors[7] = CV_RGB(255, 0, 255);
1863
1864 CCanvas::selectTextFont("10x20");
1865
1866 for (y = 0, i = 0; y < check_size_y; y++)
1867 {
1868 const auto color = line_colors[y % line_max];
1869 for (x = 0; x < check_size_x; x++, i++)
1870 {
1871 cv::Point pt;
1872 pt.x = cvRound(cornerCoords[i].x);
1873 pt.y = cvRound(cornerCoords[i].y);
1874
1875 if (i != 0) cv::line(img, prev_pt, pt, color, lines_width);
1876
1877 cv::line(img, cvPoint(pt.x - r, pt.y - r), cvPoint(pt.x + r, pt.y + r), color, lines_width);
1878 cv::line(img, cvPoint(pt.x - r, pt.y + r), cvPoint(pt.x + r, pt.y - r), color, lines_width);
1879
1880 if (r > 0) cv::circle(img, pt, r + 1, color);
1881 prev_pt = pt;
1882
1883 // Text label with the corner index in the first and last
1884 // corners:
1885 if (i == 0 || i == cornerCoords.size() - 1)
1886 CCanvas::textOut(pt.x + 5, pt.y - 5, mrpt::format("%u", i), mrpt::img::TColor::blue());
1887 }
1888 }
1889
1890 return true;
1891 #else
1892 return false;
1893 #endif
1894 }
1895
1896 CImage CImage::colorImage() const
1897 {
1898 CImage ret;
1899 colorImage(ret);
1900 return ret;
1901 }
1902
1903 void CImage::colorImage(CImage& ret) const
1904 {
1905 #if MRPT_HAS_OPENCV
1906 if (this->isColor())
1907 {
1908 if (&ret != this) ret = *this;
1909 return;
1910 }
1911
1912 auto srcImg = m_impl->img;
1913 // Detect in-place op. and make deep copy:
1914 if (srcImg.data == ret.m_impl->img.data) srcImg = srcImg.clone();
1915
1916 ret.resize(getWidth(), getHeight(), CH_RGB);
1917
1918 cv::cvtColor(srcImg, ret.m_impl->img, cv::COLOR_GRAY2BGR);
1919 #endif
1920 }
1921
1922 void CImage::joinImagesHorz(const CImage& img1, const CImage& img2)
1923 {
1924 #if MRPT_HAS_OPENCV
1925 ASSERT_(img1.getHeight() == img2.getHeight());
1926
1927 auto im1 = img1.m_impl->img, im2 = img2.m_impl->img;
1928 ASSERT_(im1.type() == im2.type());
1929
1930 this->resize(im1.cols + im2.cols, im1.rows, img1.getChannelCount());
1931
1932 im1.copyTo(m_impl->img(cv::Rect(0, 0, im1.cols, im1.rows)));
1933 im2.copyTo(m_impl->img(cv::Rect(im1.cols, 0, im2.cols, im2.rows)));
1934 #endif
1935 } // end
1936
1937 void CImage::equalizeHist(CImage& out_img) const
1938 {
1939 #if MRPT_HAS_OPENCV
1940 // Convert to a single luminance channel image
1941 auto srcImg = m_impl->img;
1942 if (this != &out_img) out_img.resize(srcImg.cols, srcImg.rows, getChannelCount());
1943 auto outImg = out_img.m_impl->img;
1944
1945 if (srcImg.channels() == 1)
1946 cv::equalizeHist(srcImg, outImg);
1947 else
1948 THROW_EXCEPTION("Operation only supported for grayscale images");
1949 #endif
1950 }
1951
1952 #if MRPT_HAS_OPENCV
1953 template <unsigned int HALF_WIN_SIZE>
1954 void image_KLT_response_template(
1955 const cv::Mat& im, unsigned int x, unsigned int y, int32_t& _gxx, int32_t& _gyy, int32_t& _gxy)
1956 {
1957 const auto min_x = x - HALF_WIN_SIZE;
1958 const auto min_y = y - HALF_WIN_SIZE;
1959
1960 int32_t gxx = 0;
1961 int32_t gxy = 0;
1962 int32_t gyy = 0;
1963
1964 const unsigned int WIN_SIZE = 1 + 2 * HALF_WIN_SIZE;
1965
1966 unsigned int yy = min_y;
1967 for (unsigned int iy = WIN_SIZE; iy; --iy, ++yy)
1968 {
1969 unsigned int xx = min_x;
1970 for (unsigned int ix = WIN_SIZE; ix; --ix, ++xx)
1971 {
1972 const int32_t dx = static_cast<int32_t>(im.at<uint8_t>(yy, xx + 1)) -
1973 static_cast<int32_t>(im.at<uint8_t>(yy, xx - 1));
1974 const int32_t dy = static_cast<int32_t>(im.at<uint8_t>(yy + 1, xx)) -
1975 static_cast<int32_t>(im.at<uint8_t>(yy - 1, xx));
1976 gxx += dx * dx;
1977 gxy += dx * dy;
1978 gyy += dy * dy;
1979 }
1980 }
1981 _gxx = gxx;
1982 _gyy = gyy;
1983 _gxy = gxy;
1984 }
1985 #endif
1986
1987 float CImage::KLT_response(
1988 const unsigned int x, const unsigned int y, const unsigned int half_window_size) const
1989 {
1990 #if MRPT_HAS_OPENCV
1991
1992 const auto& im1 = m_impl->img;
1993 const auto img_w = static_cast<unsigned int>(im1.cols),
1994 img_h = static_cast<unsigned int>(im1.rows);
1995
1996 // If any of those predefined values worked, do the generic way:
1997 const unsigned int min_x = x - half_window_size;
1998 const unsigned int max_x = x + half_window_size;
1999 const unsigned int min_y = y - half_window_size;
2000 const unsigned int max_y = y + half_window_size;
2001
2002 // Since min_* are "unsigned", checking "<" will detect negative
2003 // numbers:
2004 ASSERTMSG_(
2005 min_x < img_w && max_x < img_w && min_y < img_h && max_y < img_h,
2006 "Window is out of image bounds");
2007
2008 // Gradient sums: Use integers since they're much faster than
2009 // doubles/floats!!
2010 int32_t gxx = 0;
2011 int32_t gxy = 0;
2012 int32_t gyy = 0;
2013
2014 switch (half_window_size)
2015 {
2016 case 2:
2017 image_KLT_response_template<2>(im1, x, y, gxx, gyy, gxy);
2018 break;
2019 case 3:
2020 image_KLT_response_template<3>(im1, x, y, gxx, gyy, gxy);
2021 break;
2022 case 4:
2023 image_KLT_response_template<4>(im1, x, y, gxx, gyy, gxy);
2024 break;
2025 case 5:
2026 image_KLT_response_template<5>(im1, x, y, gxx, gyy, gxy);
2027 break;
2028 case 6:
2029 image_KLT_response_template<6>(im1, x, y, gxx, gyy, gxy);
2030 break;
2031 case 7:
2032 image_KLT_response_template<7>(im1, x, y, gxx, gyy, gxy);
2033 break;
2034 case 8:
2035 image_KLT_response_template<8>(im1, x, y, gxx, gyy, gxy);
2036 break;
2037 case 9:
2038 image_KLT_response_template<9>(im1, x, y, gxx, gyy, gxy);
2039 break;
2040 case 10:
2041 image_KLT_response_template<10>(im1, x, y, gxx, gyy, gxy);
2042 break;
2043 case 11:
2044 image_KLT_response_template<11>(im1, x, y, gxx, gyy, gxy);
2045 break;
2046 case 12:
2047 image_KLT_response_template<12>(im1, x, y, gxx, gyy, gxy);
2048 break;
2049 case 13:
2050 image_KLT_response_template<13>(im1, x, y, gxx, gyy, gxy);
2051 break;
2052 case 14:
2053 image_KLT_response_template<14>(im1, x, y, gxx, gyy, gxy);
2054 break;
2055 case 15:
2056 image_KLT_response_template<15>(im1, x, y, gxx, gyy, gxy);
2057 break;
2058 case 16:
2059 image_KLT_response_template<16>(im1, x, y, gxx, gyy, gxy);
2060 break;
2061 case 32:
2062 image_KLT_response_template<32>(im1, x, y, gxx, gyy, gxy);
2063 break;
2064
2065 default:
2066 for (unsigned int yy = min_y; yy <= max_y; yy++)
2067 {
2068 for (unsigned int xx = min_x; xx <= max_x; xx++)
2069 {
2070 const int32_t dx = static_cast<int32_t>(im1.at<uint8_t>(yy, xx + 1)) -
2071 static_cast<int32_t>(im1.at<uint8_t>(yy, xx - 1));
2072 const int32_t dy = static_cast<int32_t>(im1.at<uint8_t>(yy + 1, xx)) -
2073 static_cast<int32_t>(im1.at<uint8_t>(yy - 1, xx));
2074
2075 gxx += dx * dx;
2076 gxy += dx * dy;
2077 gyy += dy * dy;
2078 }
2079 }
2080 break;
2081 }
2082 // Convert to float's and normalize in the way:
2083 const float K = 0.5f / ((max_y - min_y + 1) * (max_x - min_x + 1));
2084 const float Gxx = gxx * K;
2085 const float Gxy = gxy * K;
2086 const float Gyy = gyy * K;
2087
2088 // Return the minimum eigenvalue of:
2089 // ( gxx gxy )
2090 // ( gxy gyy )
2091 // See, for example:
2092 // mrpt::math::detail::eigenVectorsMatrix_special_2x2():
2093 const float t = Gxx + Gyy; // Trace
2094 const float de = Gxx * Gyy - Gxy * Gxy; // Det
2095 const float discr = std::max<float>(.0f, t * t - 4.0f * de);
2096 // The smallest eigenvalue is:
2097 return 0.5f * (t - std::sqrt(discr));
2098 #else
2099 return 0;
2100 #endif
2101 }
2102
2103 // Load from TGA files. Used in loadFromFile()
2104 // Contains code from
2105 // https://github.com/tjohnman/Simple-Targa-Library/blob/master/src/simpleTGA.cpp
2106 // (FreeBSD license)
2107 bool CImage::loadTGA(
2108 const std::string& fileName, mrpt::img::CImage& out_RGB, mrpt::img::CImage& out_alpha)
2109 {
2110 #if MRPT_HAS_OPENCV
2111 std::fstream stream;
2112 stream.open(fileName.c_str(), std::fstream::in | std::fstream::binary);
2113 if (!stream.is_open())
2114 {
2115 std::cerr << "[CImage::loadTGA] Couldn't open file '" << fileName << "'.\n";
2116 return false;
2117 }
2118
2119 stream.seekg(0, std::ios_base::end);
2120 // long length = stream.tellg();
2121 stream.seekg(0, std::ios_base::beg);
2122
2123 // Simple uncompressed true-color image
2124 char dumpBuffer[12];
2125 char trueColorHeader[] = "\0\0\2\0\0\0\0\0\0\0\0\0";
2126 stream.read(dumpBuffer, 12);
2127 if (memcmp(dumpBuffer, trueColorHeader, 12) != 0)
2128 {
2129 std::cerr << "[CImage::loadTGA] Unsupported format or invalid file.\n";
2130 return false;
2131 }
2132
2133 unsigned short width, height;
2134 unsigned char bpp;
2135
2136 stream.read((char*)&width, 2);
2137 stream.read((char*)&height, 2);
2138 bpp = stream.get();
2139 if (bpp != 32)
2140 {
2141 std::cerr << "[CImage::loadTGA] Only 32 bpp format supported!\n";
2142 return false;
2143 }
2144
2145 unsigned char desc;
2146 desc = stream.get();
2147 if (desc != 8 && desc != 32)
2148 {
2149 std::cerr << "[CImage::loadTGA] Unsupported format or invalid file.\n";
2150 return false;
2151 }
2152 const bool origin_is_low_corner = (desc == 8);
2153
2154 // Data section
2155 std::vector<uint8_t> bytes(width * height * 4);
2156 stream.read((char*)&bytes[0], width * height * 4);
2157 stream.close();
2158
2159 // Move data to images:
2160 out_RGB.resize(width, height, CH_RGB);
2161 out_alpha.resize(width, height, CH_GRAY);
2162
2163 size_t idx = 0;
2164 for (int r = 0; r < height; r++)
2165 {
2166 const auto actual_row = origin_is_low_corner ? (height - 1 - r) : r;
2167 auto& img = out_RGB.m_impl->img;
2168 auto data = img.ptr<uint8_t>(actual_row);
2169
2170 auto& img_alpha = out_alpha.m_impl->img;
2171 auto data_alpha = img_alpha.ptr<uint8_t>(actual_row);
2172
2173 for (unsigned int c = 0; c < width; c++)
2174 {
2175 *data++ = bytes[idx++]; // R
2176 *data++ = bytes[idx++]; // G
2177 *data++ = bytes[idx++]; // B
2178 *data_alpha++ = bytes[idx++]; // A
2179 }
2180 }
2181
2182 return true;
2183 #else
2184 return false;
2185 #endif // MRPT_HAS_OPENCV
2186 }
2187
2188 void CImage::getAsIplImage(IplImage* dest) const
2189 {
2190 #if MRPT_HAS_OPENCV
2191 makeSureImageIsLoaded();
2192
2193 #if MRPT_OPENCV_VERSION_NUM < 0x030000
2194 ASSERT_(dest != nullptr);
2195 *dest = cvIplImage(m_impl->img);
2196 #else
2197 THROW_EXCEPTION("Method not supported in OpenCV>=3.0");
2198 #endif
2199 #endif
2200 }
2201
2202 std::ostream& mrpt::img::operator<<(std::ostream& o, const TPixelCoordf& p)
2203 {
2204 o << "(" << p.x << "," << p.y << ")";
2205 return o;
2206 }
2207 std::ostream& mrpt::img::operator<<(std::ostream& o, const TPixelCoord& p)
2208 {
2209 o << "(" << p.x << "," << p.y << ")";
2210 return o;
2211 }