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>
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  /** @} */
212 
213  /** @name Behavior-changing global flags
214  @{ */
215 
216  /** By default, when storing images through the CSerializable interface,
217  * grayscale images will be ZIP compressed if they are larger than 16Kb:
218  * this flag can be turn on to disable ZIP compression and gain speed versus
219  * occupied space.
220  * (Default = false) */
221  static void DISABLE_ZIP_COMPRESSION(bool val);
222  static bool DISABLE_ZIP_COMPRESSION();
223 
224  /** By default, when storing images through the CSerializable interface, RGB
225  * images are JPEG-compressed to save space. If for some reason you prefer
226  * storing RAW image data, disable this feature by setting this flag to
227  * true.
228  * (Default = true) */
229  static void DISABLE_JPEG_COMPRESSION(bool val);
230  static bool DISABLE_JPEG_COMPRESSION();
231 
232  /** Unless DISABLE_JPEG_COMPRESSION=true, this sets the JPEG quality (range
233  * 1-100) of serialized RGB images.
234  * (Default = 95) */
235  static void SERIALIZATION_JPEG_QUALITY(int q);
236  static int SERIALIZATION_JPEG_QUALITY();
237 
238  /** @} */
239 
240  /** @name Manipulate the image contents or size, various computer-vision
241  methods (image filters, undistortion, etc.)
242  @{ */
243 
244  /** Resets the image to the state after a default ctor. Accessing the image
245  * after will throw an exception, unless it is formerly initialized somehow:
246  * loading an image from disk, calling rezize(), etc. */
247  void clear();
248 
249  /** Changes the size of the image, erasing previous contents (does NOT scale
250  * its current content, for that, see scaleImage).
251  * - nChannels: Can be 3 for RGB images or 1 for grayscale images.
252  * \sa scaleImage
253  */
254  void resize(
255  std::size_t width, std::size_t height, TImageChannels nChannels,
257 
258  PixelDepth getPixelDepth() const;
259 
260  /** Scales this image to a new size, interpolating as needed, saving the new
261  * image in a different output object, or operating in-place if
262  * `out_img==this`. \sa resize, rotateImage
263  */
264  void scaleImage(
265  CImage& out_img, unsigned int width, unsigned int height,
267 
268  /** Rotates the image by the given angle around the given center point, with
269  * an optional scale factor.
270  * \sa resize, scaleImage
271  */
272  void rotateImage(
273  CImage& out_img, double ang, unsigned int cx, unsigned int cy,
274  double scale = 1.0) const;
275 
276  /** Changes the value of the pixel (x,y).
277  * Pixel coordinates starts at the left-top corner of the image, and start
278  * in (0,0).
279  * The meaning of the parameter "color" depends on the implementation: it
280  * will usually
281  * be a 24bit RGB value (0x00RRGGBB), but it can also be just a 8bit gray
282  * level.
283  * This method must support (x,y) values OUT of the actual image size
284  * without neither
285  * raising exceptions, nor leading to memory access errors.
286  * \sa at, ptr
287  */
288  void setPixel(int x, int y, size_t color) override;
289 
290  // See CCanvas docs
291  void line(
292  int x0, int y0, int x1, int y1, const mrpt::img::TColor color,
293  unsigned int width = 1, TPenStyle penStyle = psSolid) override;
294 
295  // See CCanvas docs
296  void drawCircle(
297  int x, int y, int radius,
298  const mrpt::img::TColor& color = mrpt::img::TColor(255, 255, 255),
299  unsigned int width = 1) override;
300 
301  // See CCanvas docs
302  void drawImage(int x, int y, const mrpt::img::CImage& img) override;
303 
304  /** Equalize the image histogram, saving the new image in the given output
305  * object. \note RGB images are first converted to HSV color space, then
306  * equalized for brightness (V) */
307  void equalizeHist(CImage& out_img) const;
308 
309  /** Returns a new image scaled down to half its original size
310  * \exception std::exception On odd size
311  * \sa scaleDouble, scaleImage, scaleHalfSmooth
312  */
314  {
315  CImage ret;
316  this->scaleHalf(ret, interp);
317  return ret;
318  }
319 
320  /** \overload
321  * \return true if an optimized SSE2/SSE3 version could be used. */
322  bool scaleHalf(CImage& out_image, TInterpolationMethod interp) const;
323 
324  /** Returns a new image scaled up to double its original size.
325  * \exception std::exception On odd size
326  * \sa scaleHalf, scaleImage
327  */
329  {
330  CImage ret;
331  this->scaleDouble(ret, interp);
332  return ret;
333  }
334 
335  //! \overload
336  void scaleDouble(CImage& out_image, TInterpolationMethod interp) const;
337 
338  /** Update a part of this image with the "patch" given as argument.
339  * The "patch" will be "pasted" at the (col,row) coordinates of this image.
340  * \exception std::exception if patch pasted on the pixel (_row, _column)
341  * jut out
342  * of the image.
343  * \sa extract_patch
344  */
345  void update_patch(
346  const CImage& patch, const unsigned int col, const unsigned int row);
347 
348  /** Extract a patch from this image, saveing it into "patch" (its previous
349  * contents will be overwritten).
350  * The patch to extract starts at (col,row) and has the given dimensions.
351  * \sa update_patch
352  */
353  void extract_patch(
354  CImage& patch, const unsigned int col = 0, const unsigned int row = 0,
355  const unsigned int width = 1, const unsigned int height = 1) const;
356 
357  /** Computes the correlation coefficient (returned as val), between two
358  *images
359  * This function use grayscale images only
360  * img1, img2 must be same size
361  * (by AJOGD @ DEC-2006)
362  */
363  float correlate(
364  const CImage& img2int, int width_init = 0, int height_init = 0) const;
365 
366  /** Computes the correlation matrix between this image and another one.
367  * This implementation uses the 2D FFT for achieving reduced computation
368  * time.
369  * \param in_img The "patch" image, which must be equal, or smaller than
370  * "this" image. This function supports gray-scale (1 channel only) images.
371  * \param u_search_ini The "x" coordinate of the search window.
372  * \param v_search_ini The "y" coordinate of the search window.
373  * \param u_search_size The width of the search window.
374  * \param v_search_size The height of the search window.
375  * \param out_corr The output for the correlation matrix, which will be
376  * "u_search_size" x "v_search_size"
377  * \param biasThisImg This optional parameter is a fixed "bias" value to be
378  * substracted to the pixels of "this" image before performing correlation.
379  * \param biasInImg This optional parameter is a fixed "bias" value to be
380  * substracted to the pixels of "in_img" image before performing
381  * correlation. Note: By default, the search area is the whole (this) image.
382  * (by JLBC @ JAN-2006)
383  * \sa cross_correlation
384  */
386  const CImage& in_img, math::CMatrixFloat& out_corr,
387  int u_search_ini = -1, int v_search_ini = -1, int u_search_size = -1,
388  int v_search_size = -1, float biasThisImg = 0,
389  float biasInImg = 0) const;
390 
391  /** Optimize the brightness range of an image without using histogram
392  * Only for one channel images.
393  * \sa equalizeHist
394  */
395  void normalize();
396 
397  /** Flips the image vertically. \sa swapRB(), flipHorizontal() */
398  void flipVertical();
399  /** Flips the image horizontally \sa swapRB(), flipVertical() */
400  void flipHorizontal();
401 
402  /** Swaps red and blue channels. */
403  void swapRB();
404 
405  /** Undistort the image according to some camera parameters, and
406  * returns an output undistorted image.
407  * \param out_img The output undistorted image
408  * \param cameraParams The input camera params (containing the intrinsic
409  * and distortion parameters of the camera)
410  * \sa mrpt::vision::CUndistortMap
411  */
412  void undistort(
413  CImage& out_img, const mrpt::img::TCamera& cameraParams) const;
414 
415  /** Rectify an image (undistorts and rectification) from a stereo pair
416  * according to a pair of precomputed rectification maps
417  * \param mapX, mapY [IN] The pre-computed maps of the rectification
418  * (should be computed beforehand)
419  * \sa mrpt::vision::CStereoRectifyMap,
420  * mrpt::vision::computeStereoRectificationMaps
421  */
422  void rectifyImageInPlace(void* mapX, void* mapY);
423 
424  /** Filter the image with a Median filter with a window size WxW, returning
425  * the filtered image in out_img. For inplace operation, set out_img to
426  * this. */
427  void filterMedian(CImage& out_img, int W = 3) const;
428 
429  /** Filter the image with a Gaussian filter with a window size WxH,
430  * replacing "this" image by the filtered one. For inplace operation, set
431  * out_img to this. */
432  void filterGaussian(
433  CImage& out_img, int W = 3, int H = 3, double sigma = 1.0) const;
434 
435  /** Draw onto this image the detected corners of a chessboard. The length of
436  * cornerCoords must be the product of the two check_sizes.
437  *
438  * \param cornerCoords [IN] The pixel coordinates of all the corners.
439  * \param check_size_x [IN] The number of squares, in the X direction
440  * \param check_size_y [IN] The number of squares, in the Y direction
441  *
442  * \return false if the length of cornerCoords is inconsistent (nothing is
443  * drawn then).
444  *
445  * \sa mrpt::vision::findChessboardCorners
446  */
448  std::vector<TPixelCoordf>& cornerCoords, unsigned int check_size_x,
449  unsigned int check_size_y, unsigned int lines_width = 1,
450  unsigned int circles_radius = 4);
451 
452  /** Joins two images side-by-side horizontally. Both images must have the
453  * same number of rows and be of the same type (i.e. depth and color mode)
454  *
455  * \param im1 [IN] The first image.
456  * \param im2 [IN] The other image.
457  */
458  void joinImagesHorz(const CImage& im1, const CImage& im2);
459 
460  /** Compute the KLT response at a given pixel (x,y) - Only for grayscale
461  * images (for efficiency it avoids converting to grayscale internally).
462  * See KLT_response() for more details on the internal
463  * optimizations of this method, but this graph shows a general view:
464  * <img src="KLT_response_performance_SSE2.png" >
465  */
466  float KLT_response(
467  const unsigned int x, const unsigned int y,
468  const unsigned int half_window_size) const;
469 
470  /** @} */
471 
472  /** @name Copy, move & swap operations
473  @{ */
474  [[deprecated("Use makeShallowCopy() instead")]] inline void
476  {
477  *this = o.makeShallowCopy();
478  }
479 
480  /** Returns a shallow copy of the original image */
481  inline CImage makeShallowCopy() const
482  {
483  CImage r = *this;
484  return r;
485  }
486 
487  /** Returns a deep copy of this image.
488  * If the image is externally-stored, there is no difference with a shallow
489  * copy. \sa makeShallowCopy() */
490  CImage makeDeepCopy() const;
491 
492  /** Copies from another image (shallow copy), and, if it is externally
493  * stored, the image file will be actually loaded into memory in "this"
494  * object. \sa operator = \exception CExceptionExternalImageNotFound If the
495  * external image couldn't be loaded.
496  */
497  void copyFromForceLoad(const CImage& o);
498 
499  /** Moves an image from another object, erasing the origin image in the
500  * process.
501  * \sa operator =
502  */
503  [[deprecated("Use a=std::move(b); instead ")]] inline void copyFastFrom(
504  CImage& o)
505  {
506  *this = std::move(o);
507  }
508 
509  /** Assigns from an image in IplImage format */
510  inline void loadFromIplImage(
511  const IplImage* iplImage, copy_type_t c = DEEP_COPY)
512  {
513  internal_fromIPL(iplImage, c);
514  }
515 
516  [[deprecated(
517  "Prefer a ctor from a cv::Mat instead or use loadFromIplImage() "
518  "explicitly specifying the kind of copy to be done")]] inline void
520  {
521  internal_fromIPL(iplImage, SHALLOW_COPY);
522  }
523 
524  /** Efficiently swap of two images */
525  void swap(CImage& o);
526 
527  /** @} */
528 
529  /** @name Access to image contents (OpenCV data structure, and raw pixels)
530  @{ */
531 
532  /** Makes a shallow or deep copy of this image into the provided cv::Mat.
533  * \sa asCvMatRef */
534  void asCvMat(cv::Mat& out_img, copy_type_t copy_type) const;
535 
536  template <typename CV_MAT>
537  CV_MAT asCvMat(copy_type_t copy_type) const
538  {
539  CV_MAT ret;
540  asCvMat(ret, copy_type);
541  return ret;
542  }
543 
544  /** Get a reference to the internal cv::Mat, which can be resized, etc. and
545  * changes will be reflected in this CImage object. */
546  cv::Mat& asCvMatRef();
547 
548  /** \overload */
549  const cv::Mat& asCvMatRef() const;
550 
551  /** Access to pixels without checking boundaries - Use normally the ()
552  operator better, which checks the coordinates.
553  \sa CImage::operator()
554  */
555  [[deprecated("Use at<>(), ptr<>() or ptrLine() instead ")]] uint8_t*
556  get_unsafe(
557  unsigned int col, unsigned int row, uint8_t channel = 0) const;
558 
559  /** Access to pixels without checking boundaries, and doing a
560  * reinterpret_cast<> of the data as the given type.
561  *\sa The CImage::operator() which does check for coordinate limits.
562  */
563  template <typename T>
564  const T& at(
565  unsigned int col, unsigned int row, unsigned int channel = 0) const
566  {
567  return *reinterpret_cast<const T*>(internal_get(col, row, channel));
568  }
569  /** \overload Non-const case */
570  template <typename T>
571  T& at(unsigned int col, unsigned int row, unsigned int channel = 0)
572  {
573  return *reinterpret_cast<T*>(internal_get(col, row, channel));
574  }
575 
576  /** Returns a pointer to a given pixel, without checking for boundaries.
577  *\sa The CImage::operator() which does check for coordinate limits.
578  */
579  template <typename T>
580  const T* ptr(
581  unsigned int col, unsigned int row, unsigned int channel = 0) const
582  {
583  return reinterpret_cast<const T*>(internal_get(col, row, channel));
584  }
585  /** \overload Non-const case */
586  template <typename T>
587  T* ptr(unsigned int col, unsigned int row, unsigned int channel = 0)
588  {
589  return reinterpret_cast<T*>(internal_get(col, row, channel));
590  }
591 
592  /** Returns a pointer to the first pixel of the given line.\sa ptr, at */
593  template <typename T>
594  const T* ptrLine(unsigned int row) const
595  {
596  return reinterpret_cast<const T*>(internal_get(0, row, 0));
597  }
598  /** \overload Non-const case */
599  template <typename T>
600  T* ptrLine(unsigned int row)
601  {
602  return reinterpret_cast<T*>(internal_get(0, row, 0));
603  }
604 
605  /** Returns the contents of a given pixel at the desired channel, in float
606  * format: [0,255]->[0,1]
607  * The coordinate origin is pixel(0,0)=top-left corner of the image.
608  * \exception std::exception On pixel coordinates out of bounds
609  * \sa operator()
610  */
611  float getAsFloat(
612  unsigned int col, unsigned int row, unsigned int channel) const;
613 
614  /** Returns the contents of a given pixel (for gray-scale images, in color
615  * images the gray scale equivalent is computed for the pixel), in float
616  * format: [0,255]->[0,1]
617  * The coordinate origin is pixel(0,0)=top-left corner of the image.
618  * \exception std::exception On pixel coordinates out of bounds
619  * \sa operator()
620  */
621  float getAsFloat(unsigned int col, unsigned int row) const;
622 
623  /** Returns a pointer to a given pixel information.
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  */
627  unsigned char* operator()(
628  unsigned int col, unsigned int row, unsigned int channel = 0) const;
629 
630  /** @} */
631 
632  /** @name Query image properties
633  @{ */
634 
635  /** Returns the width of the image in pixels \sa getSize */
636  size_t getWidth() const override;
637  /** Returns the height of the image in pixels \sa getSize */
638  size_t getHeight() const override;
639 
640  /** Return the size of the image \sa getWidth, getHeight */
641  void getSize(TImageSize& s) const;
642  /** Return the size of the image \sa getWidth, getHeight */
643  inline TImageSize getSize() const
644  {
645  TImageSize ret;
646  getSize(ret);
647  return ret;
648  }
649 
650  /** Returns the row stride of the image: this is the number of *bytes*
651  * between two consecutive rows. You can access the pointer to the first row
652  * with ptrLine(0)
653  * \sa getSize, as, ptr, ptrLine */
654  size_t getRowStride() const;
655 
656  /** As of mrpt 2.0.0, this returns either "GRAY" or "BGR". */
658 
659  /** Return the maximum pixel value of the image, as a float value in the
660  * range [0,1]
661  * \sa getAsFloat */
662  float getMaxAsFloat() const;
663 
664  /** Returns true if the image is RGB, false if it is grayscale */
665  bool isColor() const;
666 
667  /** Returns true (as of MRPT v2.0.0, it's fixed) */
668  bool isOriginTopLeft() const;
669 
670  /** Returns the number of channels, typically 1 (GRAY) or 3 (RGB)
671  * \sa isColor
672  */
674 
675  /** Returns the image as a matrix with pixel grayscale values in the range
676  * [0,1]. Matrix indexes in this order: M(row,column)
677  * \param doResize If set to true (default), the output matrix will be
678  * always the size of the image at output. If set to false, the matrix will
679  * be enlarged to the size of the image, but it will not be cropped if it
680  * has room enough (useful for FFT2D,...)
681  * \param x_min The starting "x" coordinate to extract (default=0=the
682  * first column)
683  * \param y_min The starting "y" coordinate to extract (default=0=the
684  * first row)
685  * \param x_max The final "x" coordinate (inclusive) to extract
686  * (default=-1=the last column)
687  * \param y_max The final "y" coordinate (inclusive) to extract
688  * (default=-1=the last row)
689  * \param normalize_01 Normalize the image values such that they fall in the
690  * range [0,1] (default: true). If set to false, the matrix will hold
691  * numbers in the range [0,255]. \sa setFromMatrix
692  */
693  void getAsMatrix(
694  mrpt::math::CMatrixFloat& outMatrix, bool doResize = true,
695  int x_min = 0, int y_min = 0, int x_max = -1, int y_max = -1,
696  bool normalize_01 = true) const;
697 
698  /** Returns the image as RGB matrices with pixel values in the range [0,1].
699  * Matrix indexes in this order: M(row,column)
700  * \param doResize If set to true (default), the output matrix will be
701  * always the size of the image at output. If set to false, the matrix will
702  * be enlarged to the size of the image, but it will not be cropped if it
703  * has room enough (useful for FFT2D,...)
704  * \param x_min The starting "x" coordinate to extract (default=0=the
705  * first column)
706  * \param y_min The starting "y" coordinate to extract (default=0=the
707  * first row)
708  * \param x_max The final "x" coordinate (inclusive) to extract
709  * (default=-1=the last column)
710  * \param y_max The final "y" coordinate (inclusive) to extract
711  * (default=-1=the last row)
712  * \sa setFromRGBMatrices
713  */
714  void getAsRGBMatrices(
715  mrpt::math::CMatrixFloat& outMatrixR,
716  mrpt::math::CMatrixFloat& outMatrixG,
717  mrpt::math::CMatrixFloat& outMatrixB, bool doResize = true,
718  int x_min = 0, int y_min = 0, int x_max = -1, int y_max = -1) const;
719 
720  /** Returns the image as a matrix, where the image is "tiled" (repeated)
721  * the required number of times to fill the entire size of the matrix on
722  * input.
723  */
724  void getAsMatrixTiled(mrpt::math::CMatrixFloat& outMatrix) const;
725 
726  /** @} */
727 
728  /** @name External storage-mode methods
729  @{ */
730 
731  /** By using this method the image is marked as referenced to an external
732  * file, which will be loaded only under demand.
733  * A CImage with external storage does not consume memory until some
734  * method trying to access the image is invoked (e.g. getWidth(),
735  * isColor(),...)
736  * At any moment, the image can be unloaded from memory again by invoking
737  * unload.
738  * An image becomes of type "external storage" only through calling
739  * setExternalStorage. This property remains after serializing the object.
740  * File names can be absolute, or relative to the
741  * CImage::getImagesPathBase() directory. Filenames staring with "X:\" or
742  * "/"
743  * are considered absolute paths.
744  * By calling this method the current contents of the image are NOT saved
745  * to that file, because this method can be also called
746  * to let the object know where to load the image in case its contents
747  * are required. Thus, for saving images in this format (not when loading)
748  * the proper order of commands should be:
749  * \code
750  * img.saveToFile( fileName );
751  * img.setExternalStorage( fileName );
752  * \endcode
753  *
754  * \note Modifications to the memory copy of the image are not
755  * automatically saved to disk.
756  * \sa unload, isExternallyStored
757  */
758  void setExternalStorage(const std::string& fileName) noexcept;
759 
760  /** By default, "." \sa setExternalStorage */
761  static const std::string& getImagesPathBase();
762  static void setImagesPathBase(const std::string& path);
763 
764  /** See setExternalStorage(). */
765  bool isExternallyStored() const noexcept { return m_imgIsExternalStorage; }
766  /** Only if isExternallyStored() returns true. \sa
767  * getExternalStorageFileAbsolutePath */
768  inline std::string getExternalStorageFile() const noexcept
769  {
770  return m_externalFile;
771  }
772 
773  /** Only if isExternallyStored() returns true. \sa getExternalStorageFile */
774  void getExternalStorageFileAbsolutePath(std::string& out_path) const;
775 
776  /** Only if isExternallyStored() returns true. \sa getExternalStorageFile */
778  {
779  std::string tmp;
781  return tmp;
782  }
783 
784  /** For external storage image objects only, this method makes sure the
785  * image is loaded in memory. Note that usually images are loaded on-the-fly
786  * on first access and there's no need to call this.
787  * \unload
788  */
789  inline void forceLoad() const { makeSureImageIsLoaded(); }
790  /** For external storage image objects only, this method unloads the image
791  * from memory (or does nothing if already unloaded).
792  * It does not need to be called explicitly, unless the user wants to save
793  * memory for images that will not be used often.
794  * If called for an image without the flag "external storage", it is
795  * simply ignored.
796  * \sa setExternalStorage, forceLoad
797  */
798  void unload() const noexcept;
799 
800  /** @} */
801  // ================================================================
802 
803  // ================================================================
804  /** @name Set, load & save methods
805  @{ */
806 
807  /** Reads the image from raw pixels buffer in memory.
808  */
810  unsigned int width, unsigned int height, bool color,
811  unsigned char* rawpixels, bool swapRedBlue = false);
812 
813  /** Reads a color image from three raw pixels buffers in memory.
814  * bytesPerRow is the number of bytes per row per channel, i.e. the row
815  * increment.
816  */
818  unsigned int width, unsigned int height, unsigned int bytesPerRow,
819  unsigned char* red, unsigned char* green, unsigned char* blue);
820 
821  /** Set the image from a matrix, interpreted as grayscale intensity values,
822  *in the range [0,1] (normalized=true) or [0,255] (normalized=false)
823  * Matrix indexes are assumed to be in this order: M(row,column)
824  * \sa getAsMatrix
825  */
826  template <typename MAT>
827  void setFromMatrix(const MAT& m, bool matrix_is_normalized = true)
828  {
829  MRPT_START
830  const unsigned int lx = m.cols();
831  const unsigned int ly = m.rows();
832  this->resize(lx, ly, CH_GRAY);
833  if (matrix_is_normalized)
834  { // Matrix: [0,1]
835  for (unsigned int y = 0; y < ly; y++)
836  {
837  auto* pixels = ptrLine<uint8_t>(y);
838  for (unsigned int x = 0; x < lx; x++)
839  (*pixels++) = static_cast<uint8_t>(m.coeff(y, x) * 255);
840  }
841  }
842  else
843  { // Matrix: [0,255]
844  for (unsigned int y = 0; y < ly; y++)
845  {
846  auto* pixels = ptrLine<uint8_t>(y);
847  for (unsigned int x = 0; x < lx; x++)
848  (*pixels++) = static_cast<uint8_t>(m.coeff(y, x));
849  }
850  }
851  MRPT_END
852  }
853 
854  /** Set the image from RGB matrices, given the pixels in the range [0,1]
855  * (normalized=true) or [0,255] (normalized=false)
856  * Matrix indexes are assumed to be in this order: M(row,column)
857  * \sa getAsRGBMatrices
858  */
859  template <typename MAT>
861  const MAT& r, const MAT& g, const MAT& b,
862  bool matrix_is_normalized = true)
863  {
864  MRPT_START
865  makeSureImageIsLoaded(); // For delayed loaded images stored externally
866  ASSERT_((r.size() == g.size()) && (r.size() == b.size()));
867  const unsigned int lx = r.cols(), ly = r.rows();
868  this->resize(lx, ly, CH_RGB);
869 
870  if (matrix_is_normalized)
871  { // Matrix: [0,1]
872  for (unsigned int y = 0; y < ly; y++)
873  {
874  auto* pixels = ptrLine<uint8_t>(y);
875  for (unsigned int x = 0; x < lx; x++)
876  {
877  (*pixels++) = static_cast<uint8_t>(r.coeff(y, x) * 255);
878  (*pixels++) = static_cast<uint8_t>(g.coeff(y, x) * 255);
879  (*pixels++) = static_cast<uint8_t>(b.coeff(y, x) * 255);
880  }
881  }
882  }
883  else
884  { // Matrix: [0,255]
885  for (unsigned int y = 0; y < ly; y++)
886  {
887  auto* pixels = ptrLine<uint8_t>(y);
888  for (unsigned int x = 0; x < lx; x++)
889  {
890  (*pixels++) = static_cast<uint8_t>(r.coeff(y, x));
891  (*pixels++) = static_cast<uint8_t>(g.coeff(y, x));
892  (*pixels++) = static_cast<uint8_t>(b.coeff(y, x));
893  }
894  }
895  }
896  MRPT_END
897  }
898 
899  /** Reads the image from a binary stream containing a binary jpeg file.
900  * \exception std::exception On pixel coordinates out of bounds
901  */
903 
904  /** Load image from a file, whose format is determined from the extension
905  * (internally uses OpenCV).
906  * \param fileName The file to read from.
907  * \param isColor Specifies colorness of the loaded image:
908  * - if >0, the loaded image is forced to be color 3-channel image;
909  * - if 0, the loaded image is forced to be grayscale;
910  * - if <0, the loaded image will be loaded as is (with number of channels
911  * depends on the file).
912  * The supported formats are:
913  *
914  * - Windows bitmaps - BMP, DIB;
915  * - JPEG files - JPEG, JPG, JPE;
916  * - Portable Network Graphics - PNG;
917  * - Portable image format - PBM, PGM, PPM;
918  * - Sun rasters - SR, RAS;
919  * - TIFF files - TIFF, TIF.
920  *
921  * \return False on any error
922  * \sa saveToFile, setExternalStorage,loadFromXPM, loadTGA
923  */
924  bool loadFromFile(const std::string& fileName, int isColor = -1);
925 
926  /** Loads a TGA true-color RGBA image as two CImage objects, one for the RGB
927  * channels plus a separate gray-level image with A channel.
928  * \return true on success
929  */
930  static bool loadTGA(
931  const std::string& fileName, mrpt::img::CImage& out_RGB,
932  mrpt::img::CImage& out_alpha);
933 
934  /** Loads the image from an XPM array, as #include'd from a ".xpm" file.
935  * \param[in] swap_rb Swaps red/blue channels from loaded image. *Seems* to
936  * be always needed, so it's enabled by default.
937  * \sa loadFromFile
938  * \return false on any error */
939  bool loadFromXPM(const char* const* xpm_array, bool swap_rb = true);
940 
941  /** Save the image to a file, whose format is determined from the extension
942  * (internally uses OpenCV).
943  * \param fileName The file to write to.
944  *
945  * The supported formats are:
946  *
947  * - Windows bitmaps - BMP, DIB;
948  * - JPEG files - JPEG, JPG, JPE;
949  * - Portable Network Graphics - PNG;
950  * - Portable image format - PBM, PGM, PPM;
951  * - Sun rasters - SR, RAS;
952  * - TIFF files - TIFF, TIF.
953  *
954  * \param jpeg_quality Only for JPEG files, the quality of the compression
955  * in the range [0-100]. Larger is better quality but slower.
956  * \note jpeg_quality is only effective if MRPT is compiled against OpenCV
957  * 1.1.0 or newer.
958  * \return False on any error
959  * \sa loadFromFile
960  */
961  bool saveToFile(const std::string& fileName, int jpeg_quality = 95) const;
962 
963  /** Save image to binary stream as a JPEG (.jpg) compressed format.
964  * \exception std::exception On number of rows or cols equal to zero or
965  * other errors.
966  * \sa saveToJPEG
967  */
968  void saveToStreamAsJPEG(
969  mrpt::io::CStream& out, const int jpeg_quality = 95) const;
970 
971  /** @} */
972  // ================================================================
973 
974  // ================================================================
975  /** @name Color/Grayscale conversion
976  @{ */
977 
978  /** Returns a grayscale version of the image, or a shallow copy of itself if
979  * it is already a grayscale image.
980  */
981  CImage grayscale() const;
982 
983  /** \overload.
984  * In-place is supported by setting `ret=*this`.
985  * \return true if SSE2 version has been run (or if the image was already
986  * grayscale)
987  */
988  bool grayscale(CImage& ret) const;
989 
990  /** Returns a color (RGB) version of the grayscale image, or a shallow copy
991  * of itself if it is already a color image.
992  * \sa grayscale
993  */
994  CImage colorImage() const;
995 
996  /** \overload.
997  * In-place is supported by setting `ret=*this`. */
998  void colorImage(CImage& ret) const;
999 
1000  /** @} */
1001 
1002  protected:
1003  /** @name Data members
1004  @{ */
1005 
1006  /** PIMPL to cv::Mat object actually holding the image */
1007  struct Impl;
1009 
1010  /** Set to true only when using setExternalStorage.
1011  * \sa setExternalStorage
1012  */
1013  mutable bool m_imgIsExternalStorage{false};
1014 
1015  /** The file name of a external storage image. */
1017 
1018  /** @} */
1019 
1020  /** Checks if the image is of type "external storage", and if so and not
1021  * loaded yet, load it.
1022  * \exception CExceptionExternalImageNotFound */
1023  void makeSureImageIsLoaded() const;
1024  uint8_t* internal_get(int col, int row, uint8_t channel = 0) const;
1025  void internal_fromIPL(const IplImage* iplImage, copy_type_t c);
1026 }; // End of class
1027 } // 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:481
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: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:1740
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:1455
static bool DISABLE_ZIP_COMPRESSION()
Definition: CImage.cpp:58
unsigned char red[10]
Definition: PbMapMaker.cpp:810
CImage scaleHalf(TInterpolationMethod interp) const
Returns a new image scaled down to half its original size.
Definition: img/CImage.h:313
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:594
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:765
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:564
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:1007
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:571
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: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:311
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:847
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:1905
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
void copyFastFrom(CImage &o)
Moves an image from another object, erasing the origin image in the process.
Definition: img/CImage.h:503
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:510
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
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:827
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:25
std::string getExternalStorageFile() const noexcept
Only if isExternallyStored() returns true.
Definition: img/CImage.h:768
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:643
GLsizei const GLchar ** string
Definition: glext.h:4116
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: glext.h:3606
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:537
void setFromIplImageReadOnly(IplImage *iplImage)
Definition: img/CImage.h:519
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:587
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
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:1013
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: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:580
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:328
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:600
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
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:860
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
GLenum GLsizei GLsizei height
Definition: glext.h:3558
std::string getExternalStorageFileAbsolutePath() const
Only if isExternallyStored() returns true.
Definition: img/CImage.h:777
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:1016
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:789
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:2041
void setFromImageReadOnly(const CImage &o)
Definition: img/CImage.h:475
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: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019