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



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020