MRPT  1.9.9
img/CImage.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, 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 #pragma once
10 
11 #include <mrpt/core/pimpl.h>
12 #include <mrpt/img/CCanvas.h>
13 #include <mrpt/img/TCamera.h>
14 #include <mrpt/img/TPixelCoord.h>
15 #include <mrpt/math/eigen_frwds.h>
17 
18 // Forwards decls:
19 // clang-format off
20 struct _IplImage;
21 using IplImage = struct _IplImage;
22 namespace cv { class Mat; }
23 namespace mrpt::io { class CStream; }
24 // clang-format on
25 
26 // Add for declaration of mexplus::from template specialization
28 
29 namespace mrpt::img
30 {
31 enum class PixelDepth : int32_t
32 {
33  D8U = 0,
34  D8S = 1,
35  D16U = 2,
36  D16S = 3,
37  D32S = 4,
38  D32F = 5,
39  D64F = 6
40 };
41 
42 /** Interpolation methods for images.
43  * Used for OpenCV related operations with images, but also with MRPT native
44  * classes.
45  * \sa mrpt::img::CMappedImage, CImage::scaleImage
46  * \note These are numerically compatible to cv::InterpolationFlags
47  * \ingroup mrpt_img_grp
48  */
50 {
55 };
56 
57 /** For use in mrpt::img::CImage */
59 {
60  CH_GRAY = 1,
61  CH_RGB = 3
62 };
63 
64 /** For usage in one of the CImage constructors */
66 {
68 };
69 
70 /** Define kind of copies */
72 {
73  /** Shallow copy: the copied object is a reference to the original one */
75  /** Deep copy: the copied object has a duplicate of all data, becoming
76  independent */
78 };
79 
80 /** Used in mrpt::img::CImage */
81 class CExceptionExternalImageNotFound : public std::runtime_error
82 {
83  public:
85 };
86 
87 /** A class for storing images as grayscale or RGB bitmaps.
88  * I/O is supported as:
89  * - Binary dump using the CSerializable interface(<< and >> operators),
90  *just as most objects in MRPT. This format is not compatible with any
91  *standarized image format but it is fast.
92  * - Saving/loading from files of different formats (bmp,jpg,png,...) using
93  *the methods CImage::loadFromFile and CImage::saveToFile. See OpenCV for the
94  *list of supported formats.
95  * - Importing from an XPM array (.xpm file format) using CImage::loadFromXPM()
96  * - Importing TGA images. See CImage::loadTGA()
97  *
98  * How to create color/grayscale images:
99  * \code
100  * CImage img1(width, height, CH_GRAY ); // Grayscale image (8U1C)
101  * CImage img2(width, height, CH_RGB ); // RGB image (8U3C)
102  * \endcode
103  *
104  * Additional notes:
105  * - The OpenCV `cv::Mat` format is used internally for compatibility with
106  * all OpenCV functions. Use CImage::asCvMat() to retrieve it. Example:
107  * \code
108  * CImage img;
109  * ...
110  * cv::Mat m = img.asCvMat()
111  * \endcode
112  * - By default, all images use unsigned 8-bit storage format for pixels (on
113  *each channel), but it can be changed by flags in the constructor.
114  * - An **external storage mode** can be enabled by calling
115  *CImage::setExternalStorage, useful for storing large collections of image
116  *objects in memory while loading the image data itself only for the relevant
117  *images at any time. See CImage::forceLoad() and CImage::unload().
118  * - Operator = and copy ctor make shallow copies. For deep copies, see
119  * CImage::makeDeepCopy() or CImage(const CImage&, copy_type_t), e.g:
120  * \code
121  * CImage a(20, 10, CH_GRAY);
122  * // Shallow copy ctor:
123  * CImage b(a, mrpt::img::SHALLOW_COPY);
124  * CImage c(a, mrpt::img::DEEP_COPY);
125  * \endcode
126  * - If you are interested in a smart pointer to an image, use:
127  * \code
128  * CImage::Ptr myImg = CImage::Create(); // optional ctor arguments
129  * // or:
130  * CImage::Ptr myImg = std::make_shared<CImage>(...);
131  * \endcode
132  * - To set a CImage from an OpenCV `cv::Mat` use
133  *CImage::CImage(cv::Mat,copy_type_t).
134  *
135  * Some functions are implemented in MRPT with highly optimized SSE2/SSE3
136  *routines, in suitable platforms and compilers. To see the list of
137  * optimizations refer to \ref sse_optimizations, falling back to default OpenCV
138  *methods where unavailable.
139  *
140  * For computer vision functions that use CImage as its image data type,
141  *see mrpt::vision.
142  *
143  * \sa mrpt::vision, mrpt::vision::CFeatureExtractor,
144  *mrpt::vision::CImagePyramid, CSerializable, CCanvas
145  * \ingroup mrpt_img_grp
146  */
148 {
150 
151  // This must be added for declaration of MEX-related functions
153 
154  public:
155  /** @name Constructors & destructor
156  @{ */
157 
158  /** Default constructor: initialize to empty image. It's an error trying to
159  * access the image in such a state (except reading the image width/height,
160  * which are both zero). Either call resize(), assign from another image,
161  * load from disk, deserialize from an archive, etc. to properly initialize
162  * the image.
163  */
164  CImage();
165 
166  /** Constructor for a given image size and type.
167  * Examples:
168  * \code
169  * CImage img1(width, height, CH_GRAY ); // Grayscale image (8U1C)
170  * CImage img2(width, height, CH_RGB ); // RGB image (8U3C)
171  * \endcode
172  */
173  CImage(
174  unsigned int width, unsigned int height,
175  TImageChannels nChannels = CH_RGB);
176 
177  /** Fast constructor of a grayscale version of another image, making a
178  * **shallow copy** from the original image if it already was grayscale, or
179  * otherwise creating a new grayscale image and converting the original
180  * image into it.
181  * Example of usage:
182  * \code
183  * void my_func(const CImage &in_img) {
184  * const CImage gray_img(in_img, FAST_REF_OR_CONVERT_TO_GRAY);
185  * // We can now operate on "gray_img" being sure it's in grayscale.
186  * }
187  * \endcode
188  */
189  inline CImage(const CImage& other_img, ctor_CImage_ref_or_gray)
190  {
191  if (other_img.isColor())
192  {
193  *this = CImage();
194  other_img.grayscale(*this);
195  }
196  else
197  {
198  // shallow copy
199  *this = other_img;
200  }
201  }
202 
203  /** Constructor from a cv::Mat image, making or not a deep copy of the data
204  */
205  CImage(const cv::Mat& img, copy_type_t copy_type);
206 
207  /** Constructor from another CImage, making or not a deep copy of the data
208  */
209  CImage(const CImage& img, copy_type_t copy_type);
210 
211  /** Explicit constructor from a matrix, interpreted as grayscale
212  * intensity values, in the range [0,1] (normalized=true) or [0,255]
213  * (normalized=false)
214  * \sa setFromMatrix
215  */
216  template <typename Derived>
217  explicit inline CImage(
218  const Eigen::MatrixBase<Derived>& m, bool matrix_is_normalized)
219  : CImage()
220  {
221 #if MRPT_HAS_OPENCV
222  this->setFromMatrix(m, matrix_is_normalized);
223 #else
224  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
225 #endif
226  }
227 
228  /** @} */
229 
230  /** @name Behavior-changing global flags
231  @{ */
232 
233  /** By default, when storing images through the CSerializable interface,
234  * grayscale images will be ZIP compressed if they are larger than 16Kb:
235  * this flag can be turn on to disable ZIP compression and gain speed versus
236  * occupied space.
237  * (Default = false) */
238  static void DISABLE_ZIP_COMPRESSION(bool val);
239  static bool DISABLE_ZIP_COMPRESSION();
240 
241  /** By default, when storing images through the CSerializable interface, RGB
242  * images are JPEG-compressed to save space. If for some reason you prefer
243  * storing RAW image data, disable this feature by setting this flag to
244  * true.
245  * (Default = true) */
246  static void DISABLE_JPEG_COMPRESSION(bool val);
247  static bool DISABLE_JPEG_COMPRESSION();
248 
249  /** Unless DISABLE_JPEG_COMPRESSION=true, this sets the JPEG quality (range
250  * 1-100) of serialized RGB images.
251  * (Default = 95) */
252  static void SERIALIZATION_JPEG_QUALITY(int q);
253  static int SERIALIZATION_JPEG_QUALITY();
254 
255  /** @} */
256 
257  /** @name Manipulate the image contents or size, various computer-vision
258  methods (image filters, undistortion, etc.)
259  @{ */
260 
261  /** Resets the image to the state after a default ctor. Accessing the image
262  * after will throw an exception, unless it is formerly initialized somehow:
263  * loading an image from disk, calling rezize(), etc. */
264  void clear();
265 
266  /** Changes the size of the image, erasing previous contents (does NOT scale
267  * its current content, for that, see scaleImage).
268  * - nChannels: Can be 3 for RGB images or 1 for grayscale images.
269  * \sa scaleImage
270  */
271  void resize(
272  std::size_t width, std::size_t height, TImageChannels nChannels,
274 
275  PixelDepth getPixelDepth() const;
276 
277  /** Scales this image to a new size, interpolating as needed, saving the new
278  * image in a different output object, or operating in-place if
279  * `out_img==this`. \sa resize, rotateImage
280  */
281  void scaleImage(
282  CImage& out_img, unsigned int width, unsigned int height,
284 
285  /** Rotates the image by the given angle around the given center point, with
286  * an optional scale factor.
287  * \sa resize, scaleImage
288  */
289  void rotateImage(
290  CImage& out_img, double ang, unsigned int cx, unsigned int cy,
291  double scale = 1.0) const;
292 
293  /** Changes the value of the pixel (x,y).
294  * Pixel coordinates starts at the left-top corner of the image, and start
295  * in (0,0).
296  * The meaning of the parameter "color" depends on the implementation: it
297  * will usually
298  * be a 24bit RGB value (0x00RRGGBB), but it can also be just a 8bit gray
299  * level.
300  * This method must support (x,y) values OUT of the actual image size
301  * without neither
302  * raising exceptions, nor leading to memory access errors.
303  * \sa at, ptr
304  */
305  void setPixel(int x, int y, size_t color) override;
306 
307  // See CCanvas docs
308  void line(
309  int x0, int y0, int x1, int y1, const mrpt::img::TColor color,
310  unsigned int width = 1, TPenStyle penStyle = psSolid) override;
311 
312  // See CCanvas docs
313  void drawCircle(
314  int x, int y, int radius,
315  const mrpt::img::TColor& color = mrpt::img::TColor(255, 255, 255),
316  unsigned int width = 1) override;
317 
318  // See CCanvas docs
319  void drawImage(int x, int y, const mrpt::img::CImage& img) override;
320 
321  /** Equalize the image histogram, saving the new image in the given output
322  * object. \note RGB images are first converted to HSV color space, then
323  * equalized for brightness (V) */
324  void equalizeHist(CImage& out_img) const;
325 
326  /** Returns a new image scaled down to half its original size
327  * \exception std::exception On odd size
328  * \sa scaleDouble, scaleImage, scaleHalfSmooth
329  */
331  {
332  CImage ret;
333  this->scaleHalf(ret, interp);
334  return ret;
335  }
336 
337  /** \overload
338  * \return true if an optimized SSE2/SSE3 version could be used. */
339  bool scaleHalf(CImage& out_image, TInterpolationMethod interp) const;
340 
341  /** Returns a new image scaled up to double its original size.
342  * \exception std::exception On odd size
343  * \sa scaleHalf, scaleImage
344  */
346  {
347  CImage ret;
348  this->scaleDouble(ret, interp);
349  return ret;
350  }
351 
352  //! \overload
353  void scaleDouble(CImage& out_image, TInterpolationMethod interp) const;
354 
355  /** Update a part of this image with the "patch" given as argument.
356  * The "patch" will be "pasted" at the (col,row) coordinates of this image.
357  * \exception std::exception if patch pasted on the pixel (_row, _column)
358  * jut out
359  * of the image.
360  * \sa extract_patch
361  */
362  void update_patch(
363  const CImage& patch, const unsigned int col, const unsigned int row);
364 
365  /** Extract a patch from this image, saveing it into "patch" (its previous
366  * contents will be overwritten).
367  * The patch to extract starts at (col,row) and has the given dimensions.
368  * \sa update_patch
369  */
370  void extract_patch(
371  CImage& patch, const unsigned int col = 0, const unsigned int row = 0,
372  const unsigned int width = 1, const unsigned int height = 1) const;
373 
374  /** Computes the correlation coefficient (returned as val), between two
375  *images
376  * This function use grayscale images only
377  * img1, img2 must be same size
378  * (by AJOGD @ DEC-2006)
379  */
380  float correlate(
381  const CImage& img2int, int width_init = 0, int height_init = 0) const;
382 
383  /** Computes the correlation matrix between this image and another one.
384  * This implementation uses the 2D FFT for achieving reduced computation
385  * time.
386  * \param in_img The "patch" image, which must be equal, or smaller than
387  * "this" image. This function supports gray-scale (1 channel only) images.
388  * \param u_search_ini The "x" coordinate of the search window.
389  * \param v_search_ini The "y" coordinate of the search window.
390  * \param u_search_size The width of the search window.
391  * \param v_search_size The height of the search window.
392  * \param out_corr The output for the correlation matrix, which will be
393  * "u_search_size" x "v_search_size"
394  * \param biasThisImg This optional parameter is a fixed "bias" value to be
395  * substracted to the pixels of "this" image before performing correlation.
396  * \param biasInImg This optional parameter is a fixed "bias" value to be
397  * substracted to the pixels of "in_img" image before performing
398  * correlation. Note: By default, the search area is the whole (this) image.
399  * (by JLBC @ JAN-2006)
400  * \sa cross_correlation
401  */
403  const CImage& in_img, math::CMatrixFloat& out_corr,
404  int u_search_ini = -1, int v_search_ini = -1, int u_search_size = -1,
405  int v_search_size = -1, float biasThisImg = 0,
406  float biasInImg = 0) const;
407 
408  /** Optimize the brightness range of an image without using histogram
409  * Only for one channel images.
410  * \sa equalizeHist
411  */
412  void normalize();
413 
414  /** Flips the image vertically. \sa swapRB(), flipHorizontal() */
415  void flipVertical();
416  /** Flips the image horizontally \sa swapRB(), flipVertical() */
417  void flipHorizontal();
418 
419  /** Swaps red and blue channels. */
420  void swapRB();
421 
422  /** Undistort the image according to some camera parameters, and
423  * returns an output undistorted image.
424  * \param out_img The output undistorted image
425  * \param cameraParams The input camera params (containing the intrinsic
426  * and distortion parameters of the camera)
427  * \sa mrpt::vision::CUndistortMap
428  */
429  void undistort(
430  CImage& out_img, const mrpt::img::TCamera& cameraParams) const;
431 
432  /** Rectify an image (undistorts and rectification) from a stereo pair
433  * according to a pair of precomputed rectification maps
434  * \param mapX, mapY [IN] The pre-computed maps of the rectification
435  * (should be computed beforehand)
436  * \sa mrpt::vision::CStereoRectifyMap,
437  * mrpt::vision::computeStereoRectificationMaps
438  */
439  void rectifyImageInPlace(void* mapX, void* mapY);
440 
441  /** Filter the image with a Median filter with a window size WxW, returning
442  * the filtered image in out_img. For inplace operation, set out_img to
443  * this. */
444  void filterMedian(CImage& out_img, int W = 3) const;
445 
446  /** Filter the image with a Gaussian filter with a window size WxH,
447  * replacing "this" image by the filtered one. For inplace operation, set
448  * out_img to this. */
449  void filterGaussian(
450  CImage& out_img, int W = 3, int H = 3, double sigma = 1.0) const;
451 
452  /** Draw onto this image the detected corners of a chessboard. The length of
453  * cornerCoords must be the product of the two check_sizes.
454  *
455  * \param cornerCoords [IN] The pixel coordinates of all the corners.
456  * \param check_size_x [IN] The number of squares, in the X direction
457  * \param check_size_y [IN] The number of squares, in the Y direction
458  *
459  * \return false if the length of cornerCoords is inconsistent (nothing is
460  * drawn then).
461  *
462  * \sa mrpt::vision::findChessboardCorners
463  */
465  std::vector<TPixelCoordf>& cornerCoords, unsigned int check_size_x,
466  unsigned int check_size_y, unsigned int lines_width = 1,
467  unsigned int circles_radius = 4);
468 
469  /** Joins two images side-by-side horizontally. Both images must have the
470  * same number of rows and be of the same type (i.e. depth and color mode)
471  *
472  * \param im1 [IN] The first image.
473  * \param im2 [IN] The other image.
474  */
475  void joinImagesHorz(const CImage& im1, const CImage& im2);
476 
477  /** Compute the KLT response at a given pixel (x,y) - Only for grayscale
478  * images (for efficiency it avoids converting to grayscale internally).
479  * See KLT_response() for more details on the internal
480  * optimizations of this method, but this graph shows a general view:
481  * <img src="KLT_response_performance_SSE2.png" >
482  */
483  float KLT_response(
484  const unsigned int x, const unsigned int y,
485  const unsigned int half_window_size) const;
486 
487  /** @} */
488 
489  /** @name Copy, move & swap operations
490  @{ */
491  [[deprecated("Use makeShallowCopy() instead")]] inline void
493  {
494  *this = o.makeShallowCopy();
495  }
496 
497  /** Returns a shallow copy of the original image */
498  inline CImage makeShallowCopy() const
499  {
500  CImage r = *this;
501  return r;
502  }
503 
504  /** Returns a deep copy of this image.
505  * If the image is externally-stored, there is no difference with a shallow
506  * copy. \sa makeShallowCopy() */
507  CImage makeDeepCopy() const;
508 
509  /** Copies from another image (shallow copy), and, if it is externally
510  * stored, the image file will be actually loaded into memory in "this"
511  * object. \sa operator = \exception CExceptionExternalImageNotFound If the
512  * external image couldn't be loaded.
513  */
514  void copyFromForceLoad(const CImage& o);
515 
516  /** Moves an image from another object, erasing the origin image in the
517  * process.
518  * \sa operator =
519  */
520  [[deprecated("Use a=std::move(b); instead ")]] inline void copyFastFrom(
521  CImage& o)
522  {
523  *this = std::move(o);
524  }
525 
526  /** Assigns from an image in IplImage format */
527  inline void loadFromIplImage(
528  const IplImage* iplImage, copy_type_t c = DEEP_COPY)
529  {
530  internal_fromIPL(iplImage, c);
531  }
532 
533  [[deprecated(
534  "Prefer a ctor from a cv::Mat instead or use loadFromIplImage() "
535  "explicitly specifying the kind of copy to be done")]] inline void
537  {
538  internal_fromIPL(iplImage, SHALLOW_COPY);
539  }
540 
541  /** Efficiently swap of two images */
542  void swap(CImage& o);
543 
544  /** @} */
545 
546  /** @name Access to image contents (OpenCV data structure, and raw pixels)
547  @{ */
548 
549  /** Makes a shallow or deep copy of this image into the provided cv::Mat.
550  * \sa asCvMatRef */
551  void asCvMat(cv::Mat& out_img, copy_type_t copy_type) const;
552 
553  template <typename CV_MAT>
554  CV_MAT asCvMat(copy_type_t copy_type) const
555  {
556  CV_MAT ret;
557  asCvMat(ret, copy_type);
558  return ret;
559  }
560 
561  /** Get a reference to the internal cv::Mat, which can be resized, etc. and
562  * changes will be reflected in this CImage object. */
563  cv::Mat& asCvMatRef();
564 
565  /** \overload */
566  const cv::Mat& asCvMatRef() const;
567 
568  /** Access to pixels without checking boundaries - Use normally the ()
569  operator better, which checks the coordinates.
570  \sa CImage::operator()
571  */
572  [[deprecated("Use at<>(), ptr<>() or ptrLine() instead ")]] uint8_t*
573  get_unsafe(
574  unsigned int col, unsigned int row, uint8_t channel = 0) const;
575 
576  /** Access to pixels without checking boundaries, and doing a
577  * reinterpret_cast<> of the data as the given type.
578  *\sa The CImage::operator() which does check for coordinate limits.
579  */
580  template <typename T>
581  const T& at(
582  unsigned int col, unsigned int row, unsigned int channel = 0) const
583  {
584  return *reinterpret_cast<const T*>(internal_get(col, row, channel));
585  }
586  /** \overload Non-const case */
587  template <typename T>
588  T& at(unsigned int col, unsigned int row, unsigned int channel = 0)
589  {
590  return *reinterpret_cast<T*>(internal_get(col, row, channel));
591  }
592 
593  /** Returns a pointer to a given pixel, without checking for boundaries.
594  *\sa The CImage::operator() which does check for coordinate limits.
595  */
596  template <typename T>
597  const T* ptr(
598  unsigned int col, unsigned int row, unsigned int channel = 0) const
599  {
600  return reinterpret_cast<const T*>(internal_get(col, row, channel));
601  }
602  /** \overload Non-const case */
603  template <typename T>
604  T* ptr(unsigned int col, unsigned int row, unsigned int channel = 0)
605  {
606  return reinterpret_cast<T*>(internal_get(col, row, channel));
607  }
608 
609  /** Returns a pointer to the first pixel of the given line.\sa ptr, at */
610  template <typename T>
611  const T* ptrLine(unsigned int row) const
612  {
613  return reinterpret_cast<const T*>(internal_get(0, row, 0));
614  }
615  /** \overload Non-const case */
616  template <typename T>
617  T* ptrLine(unsigned int row)
618  {
619  return reinterpret_cast<T*>(internal_get(0, row, 0));
620  }
621 
622  /** Returns the contents of a given pixel at the desired channel, in float
623  * format: [0,255]->[0,1]
624  * The coordinate origin is pixel(0,0)=top-left corner of the image.
625  * \exception std::exception On pixel coordinates out of bounds
626  * \sa operator()
627  */
628  float getAsFloat(
629  unsigned int col, unsigned int row, unsigned int channel) const;
630 
631  /** Returns the contents of a given pixel (for gray-scale images, in color
632  * images the gray scale equivalent is computed for the pixel), in float
633  * format: [0,255]->[0,1]
634  * The coordinate origin is pixel(0,0)=top-left corner of the image.
635  * \exception std::exception On pixel coordinates out of bounds
636  * \sa operator()
637  */
638  float getAsFloat(unsigned int col, unsigned int row) const;
639 
640  /** Returns a pointer to a given pixel information.
641  * The coordinate origin is pixel(0,0)=top-left corner of the image.
642  * \exception std::exception On pixel coordinates out of bounds
643  */
644  unsigned char* operator()(
645  unsigned int col, unsigned int row, unsigned int channel = 0) const;
646 
647  /** @} */
648 
649  /** @name Query image properties
650  @{ */
651 
652  /** Returns the width of the image in pixels \sa getSize */
653  size_t getWidth() const override;
654  /** Returns the height of the image in pixels \sa getSize */
655  size_t getHeight() const override;
656 
657  /** Return the size of the image \sa getWidth, getHeight */
658  void getSize(TImageSize& s) const;
659  /** Return the size of the image \sa getWidth, getHeight */
660  inline TImageSize getSize() const
661  {
662  TImageSize ret;
663  getSize(ret);
664  return ret;
665  }
666 
667  /** Returns the row stride of the image: this is the number of *bytes*
668  * between two consecutive rows. You can access the pointer to the first row
669  * with ptrLine(0)
670  * \sa getSize, as, ptr, ptrLine */
671  size_t getRowStride() const;
672 
673  /** As of mrpt 2.0.0, this returns either "GRAY" or "BGR". */
675 
676  /** Return the maximum pixel value of the image, as a float value in the
677  * range [0,1]
678  * \sa getAsFloat */
679  float getMaxAsFloat() const;
680 
681  /** Returns true if the image is RGB, false if it is grayscale */
682  bool isColor() const;
683 
684  /** Returns true (as of MRPT v2.0.0, it's fixed) */
685  bool isOriginTopLeft() const;
686 
687  /** Returns the number of channels, typically 1 (GRAY) or 3 (RGB)
688  * \sa isColor
689  */
691 
692  /** Returns the image as a matrix with pixel grayscale values in the range
693  * [0,1]. Matrix indexes in this order: M(row,column)
694  * \param doResize If set to true (default), the output matrix will be
695  * always the size of the image at output. If set to false, the matrix will
696  * be enlarged to the size of the image, but it will not be cropped if it
697  * has room enough (useful for FFT2D,...)
698  * \param x_min The starting "x" coordinate to extract (default=0=the
699  * first column)
700  * \param y_min The starting "y" coordinate to extract (default=0=the
701  * first row)
702  * \param x_max The final "x" coordinate (inclusive) to extract
703  * (default=-1=the last column)
704  * \param y_max The final "y" coordinate (inclusive) to extract
705  * (default=-1=the last row)
706  * \param normalize_01 Normalize the image values such that they fall in the
707  * range [0,1] (default: true). If set to false, the matrix will hold
708  * numbers in the range [0,255]. \sa setFromMatrix
709  */
710  void getAsMatrix(
711  mrpt::math::CMatrixFloat& outMatrix, bool doResize = true,
712  int x_min = 0, int y_min = 0, int x_max = -1, int y_max = -1,
713  bool normalize_01 = true) const;
714 
715  /** Returns the image as RGB matrices with pixel values in the range [0,1].
716  * Matrix indexes in this order: M(row,column)
717  * \param doResize If set to true (default), the output matrix will be
718  * always the size of the image at output. If set to false, the matrix will
719  * be enlarged to the size of the image, but it will not be cropped if it
720  * has room enough (useful for FFT2D,...)
721  * \param x_min The starting "x" coordinate to extract (default=0=the
722  * first column)
723  * \param y_min The starting "y" coordinate to extract (default=0=the
724  * first row)
725  * \param x_max The final "x" coordinate (inclusive) to extract
726  * (default=-1=the last column)
727  * \param y_max The final "y" coordinate (inclusive) to extract
728  * (default=-1=the last row)
729  * \sa setFromRGBMatrices
730  */
731  void getAsRGBMatrices(
732  mrpt::math::CMatrixFloat& outMatrixR,
733  mrpt::math::CMatrixFloat& outMatrixG,
734  mrpt::math::CMatrixFloat& outMatrixB, bool doResize = true,
735  int x_min = 0, int y_min = 0, int x_max = -1, int y_max = -1) const;
736 
737  /** Returns the image as a matrix, where the image is "tiled" (repeated)
738  * the required number of times to fill the entire size of the matrix on
739  * input.
740  */
741  void getAsMatrixTiled(math::CMatrix& outMatrix) const;
742 
743  /** @} */
744 
745  /** @name External storage-mode methods
746  @{ */
747 
748  /** By using this method the image is marked as referenced to an external
749  * file, which will be loaded only under demand.
750  * A CImage with external storage does not consume memory until some
751  * method trying to access the image is invoked (e.g. getWidth(),
752  * isColor(),...)
753  * At any moment, the image can be unloaded from memory again by invoking
754  * unload.
755  * An image becomes of type "external storage" only through calling
756  * setExternalStorage. This property remains after serializing the object.
757  * File names can be absolute, or relative to the
758  * CImage::getImagesPathBase() directory. Filenames staring with "X:\" or
759  * "/"
760  * are considered absolute paths.
761  * By calling this method the current contents of the image are NOT saved
762  * to that file, because this method can be also called
763  * to let the object know where to load the image in case its contents
764  * are required. Thus, for saving images in this format (not when loading)
765  * the proper order of commands should be:
766  * \code
767  * img.saveToFile( fileName );
768  * img.setExternalStorage( fileName );
769  * \endcode
770  *
771  * \note Modifications to the memory copy of the image are not
772  * automatically saved to disk.
773  * \sa unload, isExternallyStored
774  */
775  void setExternalStorage(const std::string& fileName) noexcept;
776 
777  /** By default, "." \sa setExternalStorage */
778  static const std::string& getImagesPathBase();
779  static void setImagesPathBase(const std::string& path);
780 
781  /** See setExternalStorage(). */
782  bool isExternallyStored() const noexcept { return m_imgIsExternalStorage; }
783  /** Only if isExternallyStored() returns true. \sa
784  * getExternalStorageFileAbsolutePath */
785  inline std::string getExternalStorageFile() const noexcept
786  {
787  return m_externalFile;
788  }
789 
790  /** Only if isExternallyStored() returns true. \sa getExternalStorageFile */
791  void getExternalStorageFileAbsolutePath(std::string& out_path) const;
792 
793  /** Only if isExternallyStored() returns true. \sa getExternalStorageFile */
795  {
796  std::string tmp;
798  return tmp;
799  }
800 
801  /** For external storage image objects only, this method makes sure the
802  * image is loaded in memory. Note that usually images are loaded on-the-fly
803  * on first access and there's no need to call this.
804  * \unload
805  */
806  inline void forceLoad() const { makeSureImageIsLoaded(); }
807  /** For external storage image objects only, this method unloads the image
808  * from memory (or does nothing if already unloaded).
809  * It does not need to be called explicitly, unless the user wants to save
810  * memory for images that will not be used often.
811  * If called for an image without the flag "external storage", it is
812  * simply ignored.
813  * \sa setExternalStorage, forceLoad
814  */
815  void unload() const noexcept;
816 
817  /** @} */
818  // ================================================================
819 
820  // ================================================================
821  /** @name Set, load & save methods
822  @{ */
823 
824  /** Reads the image from raw pixels buffer in memory.
825  */
827  unsigned int width, unsigned int height, bool color,
828  unsigned char* rawpixels, bool swapRedBlue = false);
829 
830  /** Reads a color image from three raw pixels buffers in memory.
831  * bytesPerRow is the number of bytes per row per channel, i.e. the row
832  * increment.
833  */
835  unsigned int width, unsigned int height, unsigned int bytesPerRow,
836  unsigned char* red, unsigned char* green, unsigned char* blue);
837 
838  /** Set the image from a matrix, interpreted as grayscale intensity values,
839  *in the range [0,1] (normalized=true) or [0,255] (normalized=false)
840  * Matrix indexes are assumed to be in this order: M(row,column)
841  * \sa getAsMatrix
842  */
843  template <typename Derived>
845  const Eigen::MatrixBase<Derived>& m, bool matrix_is_normalized = true)
846  {
847  MRPT_START
848  const unsigned int lx = m.cols();
849  const unsigned int ly = m.rows();
850  this->resize(lx, ly, CH_GRAY);
851  if (matrix_is_normalized)
852  { // Matrix: [0,1]
853  for (unsigned int y = 0; y < ly; y++)
854  {
855  auto* pixels = ptrLine<uint8_t>(y);
856  for (unsigned int x = 0; x < lx; x++)
857  (*pixels++) = static_cast<uint8_t>(m.coeff(y, x) * 255);
858  }
859  }
860  else
861  { // Matrix: [0,255]
862  for (unsigned int y = 0; y < ly; y++)
863  {
864  auto* pixels = ptrLine<uint8_t>(y);
865  for (unsigned int x = 0; x < lx; x++)
866  (*pixels++) = static_cast<uint8_t>(m.coeff(y, x));
867  }
868  }
869  MRPT_END
870  }
871 
872  /** Set the image from RGB matrices, given the pixels in the range [0,1]
873  * (normalized=true) or [0,255] (normalized=false)
874  * Matrix indexes are assumed to be in this order: M(row,column)
875  * \sa getAsRGBMatrices
876  */
877  template <typename Derived>
881  const Eigen::MatrixBase<Derived>& b, bool matrix_is_normalized = true)
882  {
883  MRPT_START
884  makeSureImageIsLoaded(); // For delayed loaded images stored externally
885  ASSERT_((r.size() == g.size()) && (r.size() == b.size()));
886  const unsigned int lx = r.cols(), ly = r.rows();
887  this->resize(lx, ly, CH_RGB);
888 
889  if (matrix_is_normalized)
890  { // Matrix: [0,1]
891  for (unsigned int y = 0; y < ly; y++)
892  {
893  auto* pixels = ptrLine<uint8_t>(y);
894  for (unsigned int x = 0; x < lx; x++)
895  {
896  (*pixels++) = static_cast<uint8_t>(r.coeff(y, x) * 255);
897  (*pixels++) = static_cast<uint8_t>(g.coeff(y, x) * 255);
898  (*pixels++) = static_cast<uint8_t>(b.coeff(y, x) * 255);
899  }
900  }
901  }
902  else
903  { // Matrix: [0,255]
904  for (unsigned int y = 0; y < ly; y++)
905  {
906  auto* pixels = ptrLine<uint8_t>(y);
907  for (unsigned int x = 0; x < lx; x++)
908  {
909  (*pixels++) = static_cast<uint8_t>(r.coeff(y, x));
910  (*pixels++) = static_cast<uint8_t>(g.coeff(y, x));
911  (*pixels++) = static_cast<uint8_t>(b.coeff(y, x));
912  }
913  }
914  }
915  MRPT_END
916  }
917 
918  /** Reads the image from a binary stream containing a binary jpeg file.
919  * \exception std::exception On pixel coordinates out of bounds
920  */
922 
923  /** Load image from a file, whose format is determined from the extension
924  * (internally uses OpenCV).
925  * \param fileName The file to read from.
926  * \param isColor Specifies colorness of the loaded image:
927  * - if >0, the loaded image is forced to be color 3-channel image;
928  * - if 0, the loaded image is forced to be grayscale;
929  * - if <0, the loaded image will be loaded as is (with number of channels
930  * depends on the file).
931  * The supported formats are:
932  *
933  * - Windows bitmaps - BMP, DIB;
934  * - JPEG files - JPEG, JPG, JPE;
935  * - Portable Network Graphics - PNG;
936  * - Portable image format - PBM, PGM, PPM;
937  * - Sun rasters - SR, RAS;
938  * - TIFF files - TIFF, TIF.
939  *
940  * \return False on any error
941  * \sa saveToFile, setExternalStorage,loadFromXPM, loadTGA
942  */
943  bool loadFromFile(const std::string& fileName, int isColor = -1);
944 
945  /** Loads a TGA true-color RGBA image as two CImage objects, one for the RGB
946  * channels plus a separate gray-level image with A channel.
947  * \return true on success
948  */
949  static bool loadTGA(
950  const std::string& fileName, mrpt::img::CImage& out_RGB,
951  mrpt::img::CImage& out_alpha);
952 
953  /** Loads the image from an XPM array, as #include'd from a ".xpm" file.
954  * \param[in] swap_rb Swaps red/blue channels from loaded image. *Seems* to
955  * be always needed, so it's enabled by default.
956  * \sa loadFromFile
957  * \return false on any error */
958  bool loadFromXPM(const char* const* xpm_array, bool swap_rb = true);
959 
960  /** Save the image to a file, whose format is determined from the extension
961  * (internally uses OpenCV).
962  * \param fileName The file to write to.
963  *
964  * The supported formats are:
965  *
966  * - Windows bitmaps - BMP, DIB;
967  * - JPEG files - JPEG, JPG, JPE;
968  * - Portable Network Graphics - PNG;
969  * - Portable image format - PBM, PGM, PPM;
970  * - Sun rasters - SR, RAS;
971  * - TIFF files - TIFF, TIF.
972  *
973  * \param jpeg_quality Only for JPEG files, the quality of the compression
974  * in the range [0-100]. Larger is better quality but slower.
975  * \note jpeg_quality is only effective if MRPT is compiled against OpenCV
976  * 1.1.0 or newer.
977  * \return False on any error
978  * \sa loadFromFile
979  */
980  bool saveToFile(const std::string& fileName, int jpeg_quality = 95) const;
981 
982  /** Save image to binary stream as a JPEG (.jpg) compressed format.
983  * \exception std::exception On number of rows or cols equal to zero or
984  * other errors.
985  * \sa saveToJPEG
986  */
987  void saveToStreamAsJPEG(
988  mrpt::io::CStream& out, const int jpeg_quality = 95) const;
989 
990  /** @} */
991  // ================================================================
992 
993  // ================================================================
994  /** @name Color/Grayscale conversion
995  @{ */
996 
997  /** Returns a grayscale version of the image, or a shallow copy of itself if
998  * it is already a grayscale image.
999  */
1000  CImage grayscale() const;
1001 
1002  /** \overload.
1003  * In-place is supported by setting `ret=*this`.
1004  * \return true if SSE2 version has been run (or if the image was already
1005  * grayscale)
1006  */
1007  bool grayscale(CImage& ret) const;
1008 
1009  /** Returns a color (RGB) version of the grayscale image, or a shallow copy
1010  * of itself if it is already a color image.
1011  * \sa grayscale
1012  */
1013  CImage colorImage() const;
1014 
1015  /** \overload.
1016  * In-place is supported by setting `ret=*this`. */
1017  void colorImage(CImage& ret) const;
1018 
1019  /** @} */
1020 
1021  protected:
1022  /** @name Data members
1023  @{ */
1024 
1025  /** PIMPL to cv::Mat object actually holding the image */
1026  struct Impl;
1028 
1029  /** Set to true only when using setExternalStorage.
1030  * \sa setExternalStorage
1031  */
1032  mutable bool m_imgIsExternalStorage{false};
1033 
1034  /** The file name of a external storage image. */
1036 
1037  /** @} */
1038 
1039  /** Checks if the image is of type "external storage", and if so and not
1040  * loaded yet, load it.
1041  * \exception CExceptionExternalImageNotFound */
1042  void makeSureImageIsLoaded() const;
1043  uint8_t* internal_get(int col, int row, uint8_t channel = 0) const;
1044  void internal_fromIPL(const IplImage* iplImage, copy_type_t c);
1045 }; // End of class
1046 } // namespace mrpt::img
void update_patch(const CImage &patch, const unsigned int col, const unsigned int row)
Update a part of this image with the "patch" given as argument.
Definition: CImage.cpp:1172
void drawCircle(int x, int y, int radius, const mrpt::img::TColor &color=mrpt::img::TColor(255, 255, 255), unsigned int width=1) override
Draws a circle of a given radius.
Definition: CImage.cpp:1148
void line(int x0, int y0, int x1, int y1, const mrpt::img::TColor color, unsigned int width=1, TPenStyle penStyle=psSolid) override
Draws a line.
Definition: CImage.cpp:1135
CImage makeShallowCopy() const
Returns a shallow copy of the original image.
Definition: img/CImage.h:498
Shallow copy: the copied object is a reference to the original one.
Definition: img/CImage.h:74
bool loadFromXPM(const char *const *xpm_array, bool swap_rb=true)
Loads the image from an XPM array, as #include&#39;d from a ".xpm" file.
Used in mrpt::img::CImage.
Definition: img/CImage.h:81
ctor_CImage_ref_or_gray
For usage in one of the CImage constructors.
Definition: img/CImage.h:65
#define MRPT_START
Definition: exceptions.h:282
This virtual class defines the interface of any object accepting drawing primitives on it...
Definition: CCanvas.h:41
TPenStyle
Definition of pen styles.
Definition: CCanvas.h:55
bool drawChessboardCorners(std::vector< TPixelCoordf > &cornerCoords, unsigned int check_size_x, unsigned int check_size_y, unsigned int lines_width=1, unsigned int circles_radius=4)
Draw onto this image the detected corners of a chessboard.
Definition: CImage.cpp:1740
static bool DISABLE_ZIP_COMPRESSION()
Definition: CImage.cpp:58
unsigned char red[10]
Definition: PbMapMaker.cpp:810
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:108
void setFromMatrix(const Eigen::MatrixBase< Derived > &m, bool matrix_is_normalized=true)
Set the image from a matrix, interpreted as grayscale intensity values, in the range [0...
Definition: img/CImage.h:844
CImage scaleHalf(TInterpolationMethod interp) const
Returns a new image scaled down to half its original size.
Definition: img/CImage.h:330
void getAsMatrix(mrpt::math::CMatrixFloat &outMatrix, bool doResize=true, int x_min=0, int y_min=0, int x_max=-1, int y_max=-1, bool normalize_01=true) const
Returns the image as a matrix with pixel grayscale values in the range [0,1].
Definition: CImage.cpp:1259
const T * ptrLine(unsigned int row) const
Returns a pointer to the first pixel of the given line.
Definition: img/CImage.h:611
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:6604
void copyFromForceLoad(const CImage &o)
Copies from another image (shallow copy), and, if it is externally stored, the image file will be act...
Definition: CImage.cpp:184
spimpl::impl_ptr< T > pimpl
Definition: pimpl.h:15
void getAsRGBMatrices(mrpt::math::CMatrixFloat &outMatrixR, mrpt::math::CMatrixFloat &outMatrixG, mrpt::math::CMatrixFloat &outMatrixB, bool doResize=true, int x_min=0, int y_min=0, int x_max=-1, int y_max=-1) const
Returns the image as RGB matrices with pixel values in the range [0,1].
Definition: CImage.cpp:1316
static bool DISABLE_JPEG_COMPRESSION()
Definition: CImage.cpp:63
#define DECLARE_MEXPLUS_FROM(complete_type)
This must be inserted if a custom conversion method for MEX API is implemented in the class...
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3727
copy_type_t
Define kind of copies.
Definition: img/CImage.h:71
CExceptionExternalImageNotFound(const std::string &s)
Definition: CImage.cpp:76
float getMaxAsFloat() const
Return the maximum pixel value of the image, as a float value in the range [0,1]. ...
Definition: CImage.cpp:941
void drawImage(int x, int y, const mrpt::img::CImage &img) override
Draws an image as a bitmap at a given position.
Definition: CImage.cpp:1160
cv::Mat & asCvMatRef()
Get a reference to the internal cv::Mat, which can be resized, etc.
Definition: CImage.cpp:233
void flipHorizontal()
Flips the image horizontally.
Definition: CImage.cpp:1591
TImageChannels getChannelCount() const
Returns the number of channels, typically 1 (GRAY) or 3 (RGB)
Definition: CImage.cpp:898
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:878
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 ...
Definition: CImage.cpp:1682
std::string getChannelsOrder() const
As of mrpt 2.0.0, this returns either "GRAY" or "BGR".
Definition: CImage.cpp:857
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...
Definition: CImage.cpp:1511
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:3606
float correlate(const CImage &img2int, int width_init=0, int height_init=0) const
Computes the correlation coefficient (returned as val), between two images This function use grayscal...
Definition: CImage.cpp:1197
void makeSureImageIsLoaded() const
Checks if the image is of type "external storage", and if so and not loaded yet, load it...
Definition: CImage.cpp:1525
void filterGaussian(CImage &out_img, int W=3, int H=3, double sigma=1.0) const
Filter the image with a Gaussian filter with a window size WxH, replacing "this" image by the filtere...
Definition: CImage.cpp:1668
void joinImagesHorz(const CImage &im1, const CImage &im2)
Joins two images side-by-side horizontally.
Definition: CImage.cpp:1831
bool isExternallyStored() const noexcept
See setExternalStorage().
Definition: img/CImage.h:782
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...
Definition: img/CImage.h:581
GLdouble s
Definition: glext.h:3682
Definition: img/CImage.h:22
void unload() const noexcept
For external storage image objects only, this method unloads the image from memory (or does nothing i...
Definition: CImage.cpp:1518
float getAsFloat(unsigned int col, unsigned int row, unsigned int channel) const
Returns the contents of a given pixel at the desired channel, in float format: [0,255]->[0,1] The coordinate origin is pixel(0,0)=top-left corner of the image.
Definition: CImage.cpp:913
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:28
GLenum GLsizei width
Definition: glext.h:3535
mrpt::pimpl< Impl > m_impl
Definition: img/CImage.h:1026
void swap(CImage &o)
Efficiently swap of two images.
Definition: CImage.cpp:177
void asCvMat(cv::Mat &out_img, copy_type_t copy_type) const
Makes a shallow or deep copy of this image into the provided cv::Mat.
Definition: CImage.cpp:223
unsigned char uint8_t
Definition: rptypes.h:44
T & at(unsigned int col, unsigned int row, unsigned int channel=0)
Definition: img/CImage.h:588
void swapRB()
Swaps red and blue channels.
Definition: CImage.cpp:1599
CImage colorImage() const
Returns a color (RGB) version of the grayscale image, or a shallow copy of itself if it is already a ...
Definition: CImage.cpp:1805
GLuint color
Definition: glext.h:8459
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:161
bool loadFromFile(const std::string &fileName, int isColor=-1)
Load image from a file, whose format is determined from the extension (internally uses OpenCV)...
Definition: CImage.cpp:311
void setFromRGBMatrices(const Eigen::MatrixBase< Derived > &r, const Eigen::MatrixBase< Derived > &g, const Eigen::MatrixBase< Derived > &b, bool matrix_is_normalized=true)
Set the image from RGB matrices, given the pixels in the range [0,1] (normalized=true) or [0...
Definition: img/CImage.h:878
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:847
void resize(std::size_t width, std::size_t height, TImageChannels nChannels, PixelDepth depth=PixelDepth::D8U)
Changes the size of the image, erasing previous contents (does NOT scale its current content...
Definition: CImage.cpp:253
unsigned char * operator()(unsigned int col, unsigned int row, unsigned int channel=0) const
Returns a pointer to a given pixel information.
Definition: CImage.cpp:432
CImage(const Eigen::MatrixBase< Derived > &m, bool matrix_is_normalized)
Explicit constructor from a matrix, interpreted as grayscale intensity values, in the range [0...
Definition: img/CImage.h:217
void copyFastFrom(CImage &o)
Moves an image from another object, erasing the origin image in the process.
Definition: img/CImage.h:520
const GLubyte * c
Definition: glext.h:6406
#define DECLARE_MEX_CONVERSION
This must be inserted if a custom conversion method for MEX API is implemented in the class...
GLint GLvoid * img
Definition: glext.h:3769
void internal_fromIPL(const IplImage *iplImage, copy_type_t c)
Definition: CImage.cpp:357
void saveToStreamAsJPEG(mrpt::io::CStream &out, const int jpeg_quality=95) const
Save image to binary stream as a JPEG (.jpg) compressed format.
void loadFromIplImage(const IplImage *iplImage, copy_type_t c=DEEP_COPY)
Assigns from an image in IplImage format.
Definition: img/CImage.h:527
void normalize()
Optimize the brightness range of an image without using histogram Only for one channel images...
Definition: CImage.cpp:1251
A pair (x,y) of pixel coordinates (integer resolution).
Definition: TPixelCoord.h:39
void flipVertical()
Flips the image vertically.
Definition: CImage.cpp:1583
GLubyte g
Definition: glext.h:6372
int val
Definition: mrpt_jpeglib.h:957
GLubyte GLubyte b
Definition: glext.h:6372
uint8_t * get_unsafe(unsigned int col, unsigned int row, uint8_t channel=0) const
Access to pixels without checking boundaries - Use normally the () operator better, which checks the coordinates.
Definition: CImage.cpp:482
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:27
std::string getExternalStorageFile() const noexcept
Only if isExternallyStored() returns true.
Definition: img/CImage.h:785
void loadFromStreamAsJPEG(mrpt::io::CStream &in)
Reads the image from a binary stream containing a binary jpeg file.
void rectifyImageInPlace(void *mapX, void *mapY)
Rectify an image (undistorts and rectification) from a stereo pair according to a pair of precomputed...
Definition: CImage.cpp:1607
TImageSize getSize() const
Return the size of the image.
Definition: img/CImage.h:660
GLsizei const GLchar ** string
Definition: glext.h:4116
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: glext.h:3606
void getAsMatrixTiled(math::CMatrix &outMatrix) const
Returns the image as a matrix, where the image is "tiled" (repeated) the required number of times to ...
Definition: CImage.cpp:1455
void clear()
Resets the image to the state after a default ctor.
Definition: CImage.cpp:1505
uint8_t * internal_get(int col, int row, uint8_t channel=0) const
Definition: CImage.cpp:470
CImage grayscale() const
Returns a grayscale version of the image, or a shallow copy of itself if it is already a grayscale im...
Definition: CImage.cpp:953
void cross_correlation_FFT(const CImage &in_img, math::CMatrixFloat &out_corr, int u_search_ini=-1, int v_search_ini=-1, int u_search_size=-1, int v_search_size=-1, float biasThisImg=0, float biasInImg=0) const
Computes the correlation matrix between this image and another one.
Definition: CImage.cpp:1375
CV_MAT asCvMat(copy_type_t copy_type) const
Definition: img/CImage.h:554
void setFromIplImageReadOnly(IplImage *iplImage)
Definition: img/CImage.h:536
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
Definition: CImage.cpp:1094
__int32 int32_t
Definition: rptypes.h:49
T * ptr(unsigned int col, unsigned int row, unsigned int channel=0)
Definition: img/CImage.h:604
PixelDepth getPixelDepth() const
Definition: CImage.cpp:300
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void filterMedian(CImage &out_img, int W=3) const
Filter the image with a Median filter with a window size WxW, returning the filtered image in out_img...
Definition: CImage.cpp:1653
CImage()
Default constructor: initialize to empty image.
Definition: CImage.cpp:165
CImage(const CImage &other_img, ctor_CImage_ref_or_gray)
Fast constructor of a grayscale version of another image, making a shallow copy from the original ima...
Definition: img/CImage.h:189
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
CImage makeDeepCopy() const
Returns a deep copy of this image.
Definition: CImage.cpp:212
bool isColor() const
Returns true if the image is RGB, false if it is grayscale.
Definition: CImage.cpp:888
TInterpolationMethod
Interpolation methods for images.
Definition: img/CImage.h:49
bool isOriginTopLeft() const
Returns true (as of MRPT v2.0.0, it&#39;s fixed)
Definition: CImage.cpp:908
float KLT_response(const unsigned int x, const unsigned int y, const unsigned int half_window_size) const
Compute the KLT response at a given pixel (x,y) - Only for grayscale images (for efficiency it avoids...
Definition: CImage.cpp:1896
GLclampf green
Definition: glext.h:3529
struct _IplImage IplImage
Definition: img/CImage.h:21
bool m_imgIsExternalStorage
Set to true only when using setExternalStorage.
Definition: img/CImage.h:1032
Deep copy: the copied object has a duplicate of all data, becoming independent.
Definition: img/CImage.h:77
GLenum GLenum GLvoid * row
Definition: glext.h:3580
#define MRPT_END
Definition: exceptions.h:286
const T * ptr(unsigned int col, unsigned int row, unsigned int channel=0) const
Returns a pointer to a given pixel, without checking for boundaries.
Definition: img/CImage.h:597
size_t getRowStride() const
Returns the row stride of the image: this is the number of bytes between two consecutive rows...
Definition: CImage.cpp:868
GLuint in
Definition: glext.h:7391
CImage scaleDouble(TInterpolationMethod interp) const
Returns a new image scaled up to double its original size.
Definition: img/CImage.h:345
void rotateImage(CImage &out_img, double ang, unsigned int cx, unsigned int cy, double scale=1.0) const
Rotates the image by the given angle around the given center point, with an optional scale factor...
Definition: CImage.cpp:1708
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
void extract_patch(CImage &patch, const unsigned int col=0, const unsigned int row=0, const unsigned int width=1, const unsigned int height=1) const
Extract a patch from this image, saveing it into "patch" (its previous contents will be overwritten)...
Definition: CImage.cpp:1184
GLenum GLint GLint y
Definition: glext.h:3542
T * ptrLine(unsigned int row)
Definition: img/CImage.h:617
bool saveToFile(const std::string &fileName, int jpeg_quality=95) const
Save the image to a file, whose format is determined from the extension (internally uses OpenCV)...
Definition: CImage.cpp:336
TImageChannels
For use in mrpt::img::CImage.
Definition: img/CImage.h:58
static int SERIALIZATION_JPEG_QUALITY()
Definition: CImage.cpp:71
GLuint interp
Definition: glext.h:7247
A RGB color - 8bit.
Definition: TColor.h:20
GLclampf GLclampf blue
Definition: glext.h:3529
GLenum GLint x
Definition: glext.h:3542
void undistort(CImage &out_img, const mrpt::img::TCamera &cameraParams) const
Undistort the image according to some camera parameters, and returns an output undistorted image...
Definition: CImage.cpp:1625
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:22
GLenum GLsizei GLsizei height
Definition: glext.h:3558
std::string getExternalStorageFileAbsolutePath() const
Only if isExternallyStored() returns true.
Definition: img/CImage.h:794
std::string m_externalFile
The file name of a external storage image.
Definition: img/CImage.h:1035
static void setImagesPathBase(const std::string &path)
Definition: CImage.cpp:83
void equalizeHist(CImage &out_img) const
Equalize the image histogram, saving the new image in the given output object.
Definition: CImage.cpp:1846
void forceLoad() const
For external storage image objects only, this method makes sure the image is loaded in memory...
Definition: img/CImage.h:806
static bool loadTGA(const std::string &fileName, mrpt::img::CImage &out_RGB, mrpt::img::CImage &out_alpha)
Loads a TGA true-color RGBA image as two CImage objects, one for the RGB channels plus a separate gra...
Definition: CImage.cpp:2032
void setFromImageReadOnly(const CImage &o)
Definition: img/CImage.h:492
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:147
void loadFromMemoryBuffer(unsigned int width, unsigned int height, bool color, unsigned char *rawpixels, bool swapRedBlue=false)
Reads the image from raw pixels buffer in memory.
Definition: CImage.cpp:371
static const std::string & getImagesPathBase()
By default, ".".
Definition: CImage.cpp:82



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 5887d2b31 Wed Apr 24 13:03:27 2019 +0200 at miƩ abr 24 13:10:13 CEST 2019