Main MRPT website > C++ reference for MRPT 1.9.9
img/CImage.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/math/eigen_frwds.h>
13 #include <mrpt/img/CCanvas.h>
14 #include <mrpt/img/TCamera.h>
15 #include <mrpt/img/TPixelCoord.h>
16 
17 // Add for declaration of mexplus::from template specialization
19 
20 namespace mrpt
21 {
22 namespace io
23 {
24 class CStream;
25 }
26 namespace img
27 {
28 /** Interpolation methods for images.
29  * Used for OpenCV related operations with images, but also with MRPT native
30  * classes.
31  * \sa mrpt::img::CMappedImage, CImage::scaleImage
32  * \ingroup mrpt_img_grp
33  */
35 {
40 };
41 
42 /** For use in mrpt::img::CImage */
43 using TImageChannels = int;
44 #define CH_GRAY 1
45 #define CH_RGB 3
46 
47 /** For usage in one of the CImage constructors */
49 {
52 };
53 
54 /** Used in mrpt::img::CImage */
55 class CExceptionExternalImageNotFound : public std::runtime_error
56 {
57  public:
59  : std::runtime_error(s)
60  {
61  }
62 };
63 
64 /** A class for storing images as grayscale or RGB bitmaps.
65  * File I/O is supported as:
66  * - Binary dump using the CSerializable interface(<< and >> operators),
67  *just
68  *as most objects
69  * in the MRPT library. This format is not compatible with any
70  *standarized image format.
71  * - Saving/loading from files of different formats (bmp,jpg,png,...) using
72  *the methods CImage::loadFromFile and CImage::saveToFile.
73  * Available formats are all those supported by OpenCV
74  * - Importing from an XPM array (.xpm file format) using
75  *CImage::loadFromXPM
76  * - Importing TGA images. See CImage::loadTGA()
77  *
78  * How to create color/grayscale images:
79  * \code
80  * CImage img1(width, height, CH_GRAY ); // Grayscale image (8U1C)
81  * CImage img2(width, height, CH_RGB ); // RGB image (8U3C)
82  * \endcode
83  *
84  * Additional notes:
85  * - The OpenCV "IplImage" format is used internally for compatibility with
86  *all OpenCV functions. Use CImage::getAs<IplImage>() to retrieve the internal
87  *structure. Example:
88  * \code
89  * CImage img;
90  * ...
91  * // Call to OpenCV function expecting an "IplImage *" or a "void*
92  *arr":
93  * cv::Mat cvImg = cv::cvarrToMat( img.getAs<IplImage>() );
94  * cvFunction( img.getAs<IplImage>(), ... );
95  * \endcode
96  * - Only the unsigned 8-bit storage format for pixels (on each channel) is
97  *supported.
98  * - An external storage mode can be enabled by calling
99  *CImage::setExternalStorage, useful for storing large collections of image
100  *objects in memory while loading the image data itself only for the relevant
101  *images at any time.
102  * - To move images from one object to the another, use
103  *CImage::copyFastFrom
104  *rather than the copy operator =.
105  * - If you are interested in a smart pointer to an image, use:
106  * \code
107  * CImage::Ptr myImg::Ptr = CImage::Ptr( new CImage(...) );
108  * \endcode
109  * - To set a CImage from an OpenCV "IPLImage*", use the methods:
110  * - CImage::loadFromIplImage
111  * - CImage::setFromIplImage
112  * - CImage::CImage(void *IPL)
113  *
114  * Some functions are implemented in MRPT with highly optimized SSE2/SSE3
115  *routines, in suitable platforms and compilers. To
116  * see the list of optimizations refer to \ref sse_optimizations "this page".
117  *If optimized versions are not available in some
118  * platform it falls back to default OpenCV methods.
119  *
120  * For many computer vision functions that use CImage as its image data type,
121  *see mrpt::vision.
122  *
123  * \note This class acts as a wrapper class to a small subset of OpenCV
124  *functions. IplImage is the internal storage structure.
125  *
126  * \sa mrpt::vision, mrpt::vision::CFeatureExtractor,
127  *mrpt::vision::CImagePyramid, CSerializable, CCanvas
128  * \ingroup mrpt_img_grp
129  */
131 {
133 
134  // This must be added for declaration of MEX-related functions
136 
137  public:
138  // ================================================================
139  /** @name Constructors & destructor
140  @{ */
141 
142  /** Default constructor: initialize an 1x1 RGB image. */
143  CImage();
144 
145  /** Constructor for a given image size and type.
146  * Examples:
147  * \code
148  * CImage img1(width, height, CH_GRAY ); // Grayscale image (8U1C)
149  * CImage img2(width, height, CH_RGB ); // RGB image (8U3C)
150  * \endcode
151  */
152  CImage(
153  unsigned int width, unsigned int height,
154  TImageChannels nChannels = CH_RGB, bool originTopLeft = true);
155 
156  /** Copy constructor, makes a full copy of the original image contents
157  * (unless it was externally stored, in that case, this new image will just
158  * point to the same image file). */
159  CImage(const CImage& o);
160 
161  /** Fast constructor that leaves the image uninitialized (the internal
162  * IplImage pointer set to nullptr).
163  * Use only when you know the image will be soon be assigned another
164  * image.
165  * Example of usage:
166  * \code
167  * CImage myImg(UNINITIALIZED_IMAGE);
168  * \endcode
169  */
171  : img(nullptr), m_imgIsReadOnly(false), m_imgIsExternalStorage(false)
172  {
173  }
174 
175  /** Fast constructor of a grayscale version of another image, making a
176  * <b>reference</b> to the original image if it already was in grayscale, or
177  * otherwise creating a new grayscale image and converting the original
178  * image into it.
179  * It's <b>very important to keep in mind</b> that the original image
180  * can't be destroyed before the new object being created with this
181  * constructor.
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(
191  const CImage& other_img, TConstructorFlags_CImage constructor_flag)
192  : img(nullptr), m_imgIsReadOnly(false), m_imgIsExternalStorage(false)
193  {
194  MRPT_UNUSED_PARAM(constructor_flag);
195  if (other_img.isColor())
196  other_img.grayscale(*this);
197  else
198  this->setFromImageReadOnly(other_img);
199  }
200 
201  /** Constructor from an IPLImage*, making a copy of the image.
202  * \sa loadFromIplImage, setFromIplImage
203  */
204  CImage(void* iplImage);
205 
206  /** Explicit constructor from a matrix, interpreted as grayscale intensity
207  * values, in the range [0,1] (normalized=true) or [0,255]
208  * (normalized=false)
209  * \sa setFromMatrix
210  */
211  template <typename Derived>
212  explicit inline CImage(
213  const Eigen::MatrixBase<Derived>& m, bool matrix_is_normalized)
214  : img(nullptr), m_imgIsReadOnly(false), m_imgIsExternalStorage(false)
215  {
216  this->setFromMatrix(m, matrix_is_normalized);
217  }
218 
219  /** Destructor: */
220  virtual ~CImage();
221 
222  /** @} */
223  // ================================================================
224 
225  /** @name Serialization format global flags
226  @{ */
227 
228  /** By default, when storing images through the CSerializable interface,
229  * grayscale images will be ZIP compressed if they are larger than 16Kb:
230  * this flag can be turn on to disable ZIP compression and gain speed versus
231  * occupied space.
232  * (Default = false) */
234 
235  /** By default, when storing images through the CSerializable interface, RGB
236  * images are JPEG-compressed to save space. If for some reason you prefer
237  * storing RAW image data, disable this feature by setting this flag to
238  * true.
239  * (Default = false) */
241 
242  /** Unless DISABLE_JPEG_COMPRESSION=true, this sets the JPEG quality (range
243  * 1-100) of serialized RGB images.
244  * (Default = 95) */
246 
247  /** @} */
248 
249  // ================================================================
250  /** @name Manipulate the image contents or size, various computer-vision
251  methods (image filters, undistortion, etc.)
252  @{ */
253 
254  /** Changes the size of the image, erasing previous contents (does NOT scale
255  * its current content, for that, see scaleImage).
256  * - nChannels: Can be 3 for RGB images or 1 for grayscale images.
257  * - originTopLeft: Is true if the top-left corner is (0,0). In other
258  * case, the reference is the bottom-left corner.
259  * \sa scaleImage
260  */
261  inline void resize(
262  unsigned int width, unsigned int height, TImageChannels nChannels,
263  bool originTopLeft)
264  {
265  changeSize(width, height, nChannels, originTopLeft);
266  }
267 
268  /** Scales this image to a new size, interpolating as needed.
269  * \sa resize, rotateImage
270  */
271  void scaleImage(
272  unsigned int width, unsigned int height,
274 
275  /** Scales this image to a new size, interpolating as needed, saving the new
276  * image in a different output object.
277  * \sa resize, rotateImage
278  */
279  void scaleImage(
280  CImage& out_img, unsigned int width, unsigned int height,
282 
283  /** Rotates the image by the given angle around the given center point, with
284  * an optional scale factor.
285  * \sa resize, scaleImage
286  */
287  void rotateImage(
288  double angle_radians, unsigned int center_x, unsigned int center_y,
289  double scale = 1.0);
290 
291  /** Changes the value of the pixel (x,y).
292  * Pixel coordinates starts at the left-top corner of the image, and start
293  * in (0,0).
294  * The meaning of the parameter "color" depends on the implementation: it
295  * will usually
296  * be a 24bit RGB value (0x00RRGGBB), but it can also be just a 8bit gray
297  * level.
298  * This method must support (x,y) values OUT of the actual image size
299  * without neither
300  * raising exceptions, nor leading to memory access errors.
301  */
302  void setPixel(int x, int y, size_t color) override;
303 
304  /** Changes the property of the image stating if the top-left corner (vs.
305  * bottom-left) is the coordinate reference */
306  void setOriginTopLeft(bool val);
307 
308  /** Draws a line.
309  * \param x0 The starting point x coordinate
310  * \param y0 The starting point y coordinate
311  * \param x1 The end point x coordinate
312  * \param y1 The end point y coordinate
313  * \param color The color of the line
314  * \param width The desired width of the line (this is IGNORED in this
315  * virtual class)
316  * This method may be redefined in some classes implementing this
317  * interface in a more appropiate manner.
318  */
319  void line(
320  int x0, int y0, int x1, int y1, const mrpt::img::TColor color,
321  unsigned int width = 1, TPenStyle penStyle = psSolid) override;
322 
323  /** Draws a circle of a given radius.
324  * \param x The center - x coordinate in pixels.
325  * \param y The center - y coordinate in pixels.
326  * \param radius The radius - in pixels.
327  * \param color The color of the circle.
328  * \param width The desired width of the line
329  */
330  void drawCircle(
331  int x, int y, int radius,
332  const mrpt::img::TColor& color = mrpt::img::TColor(255, 255, 255),
333  unsigned int width = 1) override;
334 
335  /** Equalize the image histogram, replacing the original image. \note RGB
336  * images are first converted to HSV color space, then equalized for
337  * brightness (V) */
338  void equalizeHistInPlace();
339  /** Equalize the image histogram, saving the new image in the given output
340  * object. \note RGB images are first converted to HSV color space, then
341  * equalized for brightness (V) */
342  void equalizeHist(CImage& outImg) const;
343 
344  /** Returns a new image scaled down to half its original size.
345  * \exception std::exception On odd size
346  * \sa scaleDouble, scaleImage, scaleHalfSmooth
347  */
349  {
351  this->scaleHalf(ret);
352  return ret;
353  }
354 
355  //! \overload
356  void scaleHalf(CImage& out_image) const;
357 
358  /** Returns a new image scaled down to half its original size (averaging
359  * between every two rows)
360  * \exception std::exception On odd size
361  * \sa scaleDouble, scaleImage, scaleHalf
362  */
364  {
366  this->scaleHalfSmooth(ret);
367  return ret;
368  }
369 
370  //! \overload
371  void scaleHalfSmooth(CImage& out_image) const;
372 
373  /** Returns a new image scaled up to double its original size.
374  * \exception std::exception On odd size
375  * \sa scaleHalf, scaleImage
376  */
378  {
380  this->scaleDouble(ret);
381  return ret;
382  }
383 
384  //! \overload
385  void scaleDouble(CImage& out_image) const;
386 
387  /** Update a part of this image with the "patch" given as argument.
388  * The "patch" will be "pasted" at the (col,row) coordinates of this image.
389  * \exception std::exception if patch pasted on the pixel (_row, _column)
390  * jut out
391  * of the image.
392  * \sa extract_patch
393  */
394  void update_patch(
395  const CImage& patch, const unsigned int col, const unsigned int row);
396 
397  /** Extract a patch from this image, saveing it into "patch" (its previous
398  * contents will be overwritten).
399  * The patch to extract starts at (col,row) and has the given dimensions.
400  * \sa update_patch
401  */
402  void extract_patch(
403  CImage& patch, const unsigned int col = 0, const unsigned int row = 0,
404  const unsigned int width = 1, const unsigned int height = 1) const;
405 
406  /** Computes the correlation coefficient (returned as val), between two
407  *images
408  * This function use grayscale images only
409  * img1, img2 must be same size
410  * (by AJOGD @ DEC-2006)
411  */
412  float correlate(
413  const CImage& img2int, int width_init = 0, int height_init = 0) const;
414 
415  /** Computes the correlation between this image and another one,
416  * encapsulating the openCV function cvMatchTemplate
417  *
418  * \param patch_img The "patch" image, which must be equal, or smaller than
419  * "this" image. This function supports gray-scale (1 channel only) images.
420  * \param u_search_ini The "x" coordinate of the search window.
421  * \param v_search_ini The "y" coordinate of the search window.
422  * \param u_search_size The width of the search window.
423  * \param v_search_size The height of the search window.
424  * \param u_max The u coordinate where find the maximun cross correlation
425  * value.
426  * \param v_max The v coordinate where find the maximun cross correlation
427  * value
428  * \param max_val The maximun value of cross correlation which we can find
429  * \param out_corr_image If a !=nullptr pointer is provided, it will be
430  * saved here the correlation image. The size of the output image is
431  * (this_width-patch_width+1, this_height-patch_height+1 )
432  * Note: By default, the search area is the whole (this) image.
433  * (by AJOGD @ MAR-2007)
434  */
435  void cross_correlation(
436  const CImage& patch_img, size_t& u_max, size_t& v_max, double& max_val,
437  int u_search_ini = -1, int v_search_ini = -1, int u_search_size = -1,
438  int v_search_size = -1, CImage* out_corr_image = nullptr) const;
439 
440  /** Computes the correlation matrix between this image and another one.
441  * This implementation uses the 2D FFT for achieving reduced computation
442  * time.
443  * \param in_img The "patch" image, which must be equal, or smaller than
444  * "this" image. This function supports gray-scale (1 channel only) images.
445  * \param u_search_ini The "x" coordinate of the search window.
446  * \param v_search_ini The "y" coordinate of the search window.
447  * \param u_search_size The width of the search window.
448  * \param v_search_size The height of the search window.
449  * \param out_corr The output for the correlation matrix, which will be
450  * "u_search_size" x "v_search_size"
451  * \param biasThisImg This optional parameter is a fixed "bias" value to be
452  * substracted to the pixels of "this" image before performing correlation.
453  * \param biasInImg This optional parameter is a fixed "bias" value to be
454  * substracted to the pixels of "in_img" image before performing
455  * correlation. Note: By default, the search area is the whole (this) image.
456  * (by JLBC @ JAN-2006)
457  * \sa cross_correlation
458  */
460  const CImage& in_img, math::CMatrixFloat& out_corr,
461  int u_search_ini = -1, int v_search_ini = -1, int u_search_size = -1,
462  int v_search_size = -1, float biasThisImg = 0,
463  float biasInImg = 0) const;
464 
465  /** Optimize the brightness range of an image without using histogram
466  * Only for one channel images.
467  * \sa equalizeHist
468  */
469  void normalize();
470 
471  /** Flips the image vertically. \sa swapRB(), flipHorizontal() */
472  void flipVertical(bool also_swapRB = false);
473  /** Flips the image horizontally \sa swapRB(), flipVertical() */
474  void flipHorizontal();
475 
476  /** Swaps red and blue channels. */
477  void swapRB();
478 
479  /** Rectify (un-distort) the image according to some camera parameters, and
480  * returns an output un-distorted image.
481  * \param out_img The output rectified image
482  * \param cameraParams The input camera params (containing the intrinsic
483  * and distortion parameters of the camera)
484  * \sa mrpt::vision::CUndistortMap
485  */
486  void rectifyImage(
487  CImage& out_img, const mrpt::img::TCamera& cameraParams) const;
488 
489  /** Rectify (un-distort) the image according to a certain camera matrix and
490  * vector of distortion coefficients, replacing "this" with the rectified
491  * image
492  * \param cameraParams The input camera params (containing the intrinsic
493  * and distortion parameters of the camera)
494  * \sa mrpt::vision::CUndistortMap
495  */
496  void rectifyImageInPlace(const mrpt::img::TCamera& cameraParams);
497 
498  /** Rectify an image (undistorts and rectification) from a stereo pair
499  * according to a pair of precomputed rectification maps
500  * \param mapX, mapY [IN] The pre-computed maps of the rectification
501  * (should be computed beforehand)
502  * \sa mrpt::vision::CStereoRectifyMap,
503  * mrpt::vision::computeStereoRectificationMaps
504  */
505  void rectifyImageInPlace(void* mapX, void* mapY);
506 
507  /** Filter the image with a Median filter with a window size WxW, returning
508  * the filtered image in out_img */
509  void filterMedian(CImage& out_img, int W = 3) const;
510 
511  /** Filter the image with a Median filter with a window size WxH, replacing
512  * "this" image by the filtered one. */
513  void filterMedianInPlace(int W = 3);
514 
515  /** Filter the image with a Gaussian filter with a window size WxH,
516  * returning the filtered image in out_img */
517  void filterGaussianInPlace(int W = 3, int H = 3);
518 
519  /** Filter the image with a Gaussian filter with a window size WxH,
520  * replacing "this" image by the filtered one. */
521  void filterGaussian(CImage& out_img, int W = 3, int H = 3) const;
522 
523  /** Draw onto this image the detected corners of a chessboard. The length of
524  * cornerCoords must be the product of the two check_sizes.
525  *
526  * \param cornerCoords [IN] The pixel coordinates of all the corners.
527  * \param check_size_x [IN] The number of squares, in the X direction
528  * \param check_size_y [IN] The number of squares, in the Y direction
529  *
530  * \return false if the length of cornerCoords is inconsistent (nothing is
531  * drawn then).
532  *
533  * \sa mrpt::vision::findChessboardCorners
534  */
536  std::vector<TPixelCoordf>& cornerCoords, unsigned int check_size_x,
537  unsigned int check_size_y, unsigned int lines_width = 1,
538  unsigned int circles_radius = 4);
539 
540  /** Joins two images side-by-side horizontally. Both images must have the
541  * same number of rows and be of the same type (i.e. depth and color mode)
542  *
543  * \param im1 [IN] The first image.
544  * \param im2 [IN] The other image.
545  */
546  void joinImagesHorz(const CImage& im1, const CImage& im2);
547 
548  /** Compute the KLT response at a given pixel (x,y) - Only for grayscale
549  * images (for efficiency it avoids converting to grayscale internally).
550  * See KLT_response_optimized for more details on the internal
551  * optimizations of this method, but this graph shows a general view:
552  * <img src="KLT_response_performance_SSE2.png" >
553  */
554  float KLT_response(
555  const unsigned int x, const unsigned int y,
556  const unsigned int half_window_size) const;
557 
558  /** @} */
559  // ================================================================
560 
561  // ================================================================
562  /** @name Copy, move & swap operations
563  @{ */
564 
565  /** Copy operator (if the image is externally stored, the writen image will
566  * be such as well).
567  * \sa copyFastFrom
568  */
569  CImage& operator=(const CImage& o);
570 
571  /** Copies from another image, and, if that one is externally stored, the
572  * image file will be actually loaded into memory in "this" object.
573  * \sa operator =
574  * \exception CExceptionExternalImageNotFound If the external image
575  * couldn't be loaded.
576  */
577  void copyFromForceLoad(const CImage& o);
578 
579  /** Moves an image from another object, erasing the origin image in the
580  * process (this is much faster than copying)
581  * \sa operator =
582  */
583  void copyFastFrom(CImage& o);
584 
585  /** Very efficient swap of two images (just swap the internal pointers) */
586  void swap(CImage& o);
587 
588  /** @} */
589  // ================================================================
590 
591  // ================================================================
592  /** @name Access to image contents (IplImage structure and raw pixels).
593  @{ */
594 
595  /** Returns a pointer to a const T* containing the image - the idea is to
596  * call like "img.getAs<IplImage>()" so we can avoid here including OpenCV's
597  * headers. */
598  template <typename T>
599  inline const T* getAs() const
600  {
602  return static_cast<const T*>(img);
603  }
604  /** Returns a pointer to a T* containing the image - the idea is to call
605  * like "img.getAs<IplImage>()" so we can avoid here including OpenCV's
606  * headers. */
607  template <typename T>
608  inline T* getAs()
609  {
611  return static_cast<T*>(img);
612  }
613 
614  /** Access to pixels without checking boundaries - Use normally the ()
615  operator better, which checks the coordinates.
616  \sa CImage::operator()
617  */
618  unsigned char* get_unsafe(
619  unsigned int col, unsigned int row, unsigned int channel = 0) const;
620 
621  /** Returns the contents of a given pixel at the desired channel, in float
622  * format: [0,255]->[0,1]
623  * The coordinate origin is pixel(0,0)=top-left corner of the image.
624  * \exception std::exception On pixel coordinates out of bounds
625  * \sa operator()
626  */
627  float getAsFloat(
628  unsigned int col, unsigned int row, unsigned int channel) const;
629 
630  /** Returns the contents of a given pixel (for gray-scale images, in color
631  * images the gray scale equivalent is computed for the pixel), in float
632  * format: [0,255]->[0,1]
633  * The coordinate origin is pixel(0,0)=top-left corner of the image.
634  * \exception std::exception On pixel coordinates out of bounds
635  * \sa operator()
636  */
637  float getAsFloat(unsigned int col, unsigned int row) const;
638 
639  /** Returns a pointer to a given pixel information.
640  * The coordinate origin is pixel(0,0)=top-left corner of the image.
641  * \exception std::exception On pixel coordinates out of bounds
642  */
643  unsigned char* operator()(
644  unsigned int col, unsigned int row, unsigned int channel = 0) const;
645 
646  /** @} */
647  // ================================================================
648 
649  // ================================================================
650  /** @name Query image properties
651  @{ */
652 
653  /** Returns the width of the image in pixels \sa getSize */
654  size_t getWidth() const override;
655  /** Returns the height of the image in pixels \sa getSize */
656  size_t getHeight() const override;
657 
658  /** Return the size of the image \sa getWidth, getHeight */
659  void getSize(TImageSize& s) const;
660  /** Return the size of the image \sa getWidth, getHeight */
661  inline TImageSize getSize() const
662  {
663  TImageSize ret;
664  getSize(ret);
665  return ret;
666  }
667 
668  /** Returns the row stride of the image: this is the number of *bytes*
669  * between two consecutive rows. You can access the pointer to the first row
670  * with get_unsafe(0,0)
671  * \sa getSize, get_unsafe */
672  size_t getRowStride() const;
673 
674  /** Return the maximum pixel value of the image, as a float value in the
675  * range [0,1]
676  * \sa getAsFloat */
677  float getMaxAsFloat() const;
678 
679  /** Returns true if the image is RGB, false if it is grayscale */
680  bool isColor() const;
681 
682  /** Returns true if the coordinates origin is top-left, or false if it is
683  * bottom-left */
684  bool isOriginTopLeft() const;
685 
686  /** Returns a string of the form "BGR","RGB" or "GRAY" indicating the
687  * channels ordering. \sa setChannelsOrder, swapRB */
688  const char* getChannelsOrder() const;
689 
690  /** Marks the channel ordering in a color image as "RGB" (this doesn't
691  * actually modify the image data, just the format description) \sa
692  * getChannelsOrder, swapRB */
693  void setChannelsOrder_RGB();
694  /** Marks the channel ordering in a color image as "BGR" (this doesn't
695  * actually modify the image data, just the format description) \sa
696  * getChannelsOrder, swapRB */
697  void setChannelsOrder_BGR();
698 
699  /** Returns the number of channels, typically 1 (GRAY) or 3 (RGB)
700  * \sa isColor
701  */
703 
704  /** Returns the image as a matrix with pixel grayscale values in the range
705  * [0,1]. Matrix indexes in this order: M(row,column)
706  * \param doResize If set to true (default), the output matrix will be
707  * always the size of the image at output. If set to false, the matrix will
708  * be enlarged to the size of the image, but it will not be cropped if it
709  * has room enough (useful for FFT2D,...)
710  * \param x_min The starting "x" coordinate to extract (default=0=the
711  * first column)
712  * \param y_min The starting "y" coordinate to extract (default=0=the
713  * first row)
714  * \param x_max The final "x" coordinate (inclusive) to extract
715  * (default=-1=the last column)
716  * \param y_max The final "y" coordinate (inclusive) to extract
717  * (default=-1=the last row)
718  * \sa setFromMatrix
719  */
720  void getAsMatrix(
721  mrpt::math::CMatrixFloat& outMatrix, bool doResize = true,
722  int x_min = 0, int y_min = 0, int x_max = -1, int y_max = -1) const;
723 
724  /** Returns the image as RGB matrices with pixel values in the range [0,1].
725  * Matrix indexes in this order: M(row,column)
726  * \param doResize If set to true (default), the output matrix will be
727  * always the size of the image at output. If set to false, the matrix will
728  * be enlarged to the size of the image, but it will not be cropped if it
729  * has room enough (useful for FFT2D,...)
730  * \param x_min The starting "x" coordinate to extract (default=0=the
731  * first column)
732  * \param y_min The starting "y" coordinate to extract (default=0=the
733  * first row)
734  * \param x_max The final "x" coordinate (inclusive) to extract
735  * (default=-1=the last column)
736  * \param y_max The final "y" coordinate (inclusive) to extract
737  * (default=-1=the last row)
738  * \sa setFromRGBMatrices
739  */
740  void getAsRGBMatrices(
741  mrpt::math::CMatrixFloat& outMatrixR,
742  mrpt::math::CMatrixFloat& outMatrixG,
743  mrpt::math::CMatrixFloat& outMatrixB, bool doResize = true,
744  int x_min = 0, int y_min = 0, int x_max = -1, int y_max = -1) const;
745 
746  /** Returns the image as a matrix, where the image is "tiled" (repeated)
747  * the required number of times to fill the entire size of the matrix on
748  * input.
749  */
750  void getAsMatrixTiled(math::CMatrix& outMatrix) const;
751 
752  /** @} */
753  // ================================================================
754 
755  // ================================================================
756  /** @name External storage-mode methods
757  @{ */
758 
759  /** By using this method the image is marked as referenced to an external
760  * file, which will be loaded only under demand.
761  * A CImage with external storage does not consume memory until some
762  * method trying to access the image is invoked (e.g. getWidth(),
763  * isColor(),...)
764  * At any moment, the image can be unloaded from memory again by invoking
765  * unload.
766  * An image becomes of type "external storage" only through calling
767  * setExternalStorage. This property remains after serializing the object.
768  * File names can be absolute, or relative to the
769  * CImage::getImagesPathBase() directory. Filenames staring with "X:\" or
770  * "/"
771  * are considered absolute paths.
772  * By calling this method the current contents of the image are NOT saved
773  * to that file, because this method can be also called
774  * to let the object know where to load the image in case its contents
775  * are required. Thus, for saving images in this format (not when loading)
776  * the proper order of commands should be:
777  * \code
778  * img.saveToFile( fileName );
779  * img.setExternalStorage( fileName );
780  * \endcode
781  *
782  * \note Modifications to the memory copy of the image are not
783  * automatically saved to disk.
784  * \sa unload, isExternallyStored
785  */
786  void setExternalStorage(const std::string& fileName) noexcept;
787 
788  /** By default, "." \sa setExternalStorage */
789  static const std::string& getImagesPathBase();
790  static void setImagesPathBase(const std::string& path);
791 
792  /** See setExternalStorage(). */
793  bool isExternallyStored() const noexcept { return m_imgIsExternalStorage; }
794  /** Only if isExternallyStored() returns true. \sa
795  * getExternalStorageFileAbsolutePath */
796  inline std::string getExternalStorageFile() const noexcept
797  {
798  return m_externalFile;
799  }
800 
801  /** Only if isExternallyStored() returns true. \sa getExternalStorageFile */
802  void getExternalStorageFileAbsolutePath(std::string& out_path) const;
803 
804  /** Only if isExternallyStored() returns true. \sa getExternalStorageFile */
806  {
807  std::string tmp;
809  return tmp;
810  }
811 
812  /** For external storage image objects only, this method makes sure the
813  * image is loaded in memory. Note that usually images are loaded on-the-fly
814  * on first access and there's no need to call this.
815  * \unload
816  */
817  inline void forceLoad() const { makeSureImageIsLoaded(); }
818  /** For external storage image objects only, this method unloads the image
819  * from memory (or does nothing if already unloaded).
820  * It does not need to be called explicitly, unless the user wants to save
821  * memory for images that will not be used often.
822  * If called for an image without the flag "external storage", it is
823  * simply ignored.
824  * \sa setExternalStorage, forceLoad
825  */
826  void unload() const noexcept;
827 
828  /** @} */
829  // ================================================================
830 
831  // ================================================================
832  /** @name Set, load & save methods
833  @{ */
834 
835  /** Reads the image from raw pixels buffer in memory.
836  */
838  unsigned int width, unsigned int height, bool color,
839  unsigned char* rawpixels, bool swapRedBlue = false);
840 
841  /** Reads a color image from three raw pixels buffers in memory.
842  * bytesPerRow is the number of bytes per row per channel, i.e. the row
843  * increment.
844  */
846  unsigned int width, unsigned int height, unsigned int bytesPerRow,
847  unsigned char* red, unsigned char* green, unsigned char* blue);
848 
849  /** Reads the image from a OpenCV IplImage object (making a COPY).
850  */
851  void loadFromIplImage(void* iplImage);
852 
853  /** Reads the image from a OpenCV IplImage object (WITHOUT making a copy).
854  * This object will own the memory of the passed object and free the
855  * IplImage upon destruction,
856  * so the caller CAN'T free the original object.
857  * This method provides a fast method to grab images from a camera
858  * without making a copy of every frame.
859  */
860  void setFromIplImage(void* iplImage);
861 
862  /** Reads the image from a OpenCV IplImage object (WITHOUT making a copy)
863  * and from now on the image cannot be modified, just read.
864  * When assigning an IPLImage to this object with this method, the
865  * IPLImage will NOT be released/freed at this object destructor.
866  * This method provides a fast method to grab images from a camera
867  * without making a copy of every frame.
868  * \sa setFromImageReadOnly
869  */
870  void setFromIplImageReadOnly(void* iplImage);
871 
872  /** Sets the internal IplImage pointer to that of another given image,
873  * WITHOUT making a copy, and from now on the image cannot be modified in
874  * this object (it will be neither freed, so the memory responsibility will
875  * still be of the original image object).
876  * When assigning an IPLImage to this object with this method, the
877  * IPLImage will NOT be released/freed at this object destructor.
878  * \sa setFromIplImageReadOnly
879  */
880  inline void setFromImageReadOnly(const CImage& other_img)
881  {
882  setFromIplImageReadOnly(const_cast<void*>(other_img.getAs<void>()));
883  }
884 
885  /** Set the image from a matrix, interpreted as grayscale intensity values,
886  *in the range [0,1] (normalized=true) or [0,255] (normalized=false)
887  * Matrix indexes are assumed to be in this order: M(row,column)
888  * \sa getAsMatrix
889  */
890  template <typename Derived>
892  const Eigen::MatrixBase<Derived>& m, bool matrix_is_normalized = true)
893  {
894  MRPT_START
895  const unsigned int lx = m.cols();
896  const unsigned int ly = m.rows();
897  this->changeSize(lx, ly, 1, true);
898  if (matrix_is_normalized)
899  { // Matrix: [0,1]
900  for (unsigned int y = 0; y < ly; y++)
901  {
902  unsigned char* pixels = this->get_unsafe(0, y, 0);
903  for (unsigned int x = 0; x < lx; x++)
904  (*pixels++) =
905  static_cast<unsigned char>(m.get_unsafe(y, x) * 255);
906  }
907  }
908  else
909  { // Matrix: [0,255]
910  for (unsigned int y = 0; y < ly; y++)
911  {
912  unsigned char* pixels = this->get_unsafe(0, y, 0);
913  for (unsigned int x = 0; x < lx; x++)
914  (*pixels++) =
915  static_cast<unsigned char>(m.get_unsafe(y, x));
916  }
917  }
918  MRPT_END
919  }
920 
921  /** Set the image from RGB matrices, given the pixels in the range [0,1]
922  * (normalized=true) or [0,255] (normalized=false)
923  * Matrix indexes are assumed to be in this order: M(row,column)
924  * \sa getAsRGBMatrices
925  */
926  template <typename Derived>
928  const Eigen::MatrixBase<Derived>& m_r,
929  const Eigen::MatrixBase<Derived>& m_g,
930  const Eigen::MatrixBase<Derived>& m_b, bool matrix_is_normalized = true)
931  {
932  MRPT_START
933  makeSureImageIsLoaded(); // For delayed loaded images stored externally
934  ASSERT_(img);
935  ASSERT_((m_r.size() == m_g.size()) && (m_r.size() == m_b.size()));
936  const unsigned int lx = m_r.cols();
937  const unsigned int ly = m_r.rows();
938  this->changeSize(lx, ly, 3, true);
939  this->setChannelsOrder_RGB();
940 
941  if (matrix_is_normalized)
942  { // Matrix: [0,1]
943  for (unsigned int y = 0; y < ly; y++)
944  {
945  unsigned char* pixels = this->get_unsafe(0, y, 0);
946  for (unsigned int x = 0; x < lx; x++)
947  {
948  (*pixels++) =
949  static_cast<unsigned char>(m_r.get_unsafe(y, x) * 255);
950  (*pixels++) =
951  static_cast<unsigned char>(m_g.get_unsafe(y, x) * 255);
952  (*pixels++) =
953  static_cast<unsigned char>(m_b.get_unsafe(y, x) * 255);
954  }
955  }
956  }
957  else
958  { // Matrix: [0,255]
959  for (unsigned int y = 0; y < ly; y++)
960  {
961  unsigned char* pixels = this->get_unsafe(0, y, 0);
962  for (unsigned int x = 0; x < lx; x++)
963  {
964  (*pixels++) =
965  static_cast<unsigned char>(m_r.get_unsafe(y, x));
966  (*pixels++) =
967  static_cast<unsigned char>(m_g.get_unsafe(y, x));
968  (*pixels++) =
969  static_cast<unsigned char>(m_b.get_unsafe(y, x));
970  }
971  }
972  }
973  MRPT_END
974  }
975 
976  /** Reads the image from a binary stream containing a binary jpeg file.
977  * \exception std::exception On pixel coordinates out of bounds
978  */
980 
981  /** Load image from a file, whose format is determined from the extension
982  * (internally uses OpenCV).
983  * \param fileName The file to read from.
984  * \param isColor Specifies colorness of the loaded image:
985  * - if >0, the loaded image is forced to be color 3-channel image;
986  * - if 0, the loaded image is forced to be grayscale;
987  * - if <0, the loaded image will be loaded as is (with number of channels
988  * depends on the file).
989  * The supported formats are:
990  *
991  * - Windows bitmaps - BMP, DIB;
992  * - JPEG files - JPEG, JPG, JPE;
993  * - Portable Network Graphics - PNG;
994  * - Portable image format - PBM, PGM, PPM;
995  * - Sun rasters - SR, RAS;
996  * - TIFF files - TIFF, TIF.
997  *
998  * \return False on any error
999  * \sa saveToFile, setExternalStorage,loadFromXPM, loadTGA
1000  */
1001  bool loadFromFile(const std::string& fileName, int isColor = -1);
1002 
1003  /** Loads a TGA true-color RGBA image as two CImage objects, one for the RGB
1004  * channels plus a separate gray-level image with A channel.
1005  * \return true on success
1006  */
1007  static bool loadTGA(
1008  const std::string& fileName, mrpt::img::CImage& out_RGB,
1009  mrpt::img::CImage& out_alpha);
1010 
1011  /** Loads the image from an XPM array, as #include'd from a ".xpm" file.
1012  * \param[in] swap_rb Swaps red/blue channels from loaded image. *Seems* to
1013  * be always needed, so it's enabled by default.
1014  * \sa loadFromFile
1015  * \return false on any error */
1016  bool loadFromXPM(const char* const* xpm_array, bool swap_rb = true);
1017 
1018  /** Save the image to a file, whose format is determined from the extension
1019  * (internally uses OpenCV).
1020  * \param fileName The file to write to.
1021  *
1022  * The supported formats are:
1023  *
1024  * - Windows bitmaps - BMP, DIB;
1025  * - JPEG files - JPEG, JPG, JPE;
1026  * - Portable Network Graphics - PNG;
1027  * - Portable image format - PBM, PGM, PPM;
1028  * - Sun rasters - SR, RAS;
1029  * - TIFF files - TIFF, TIF.
1030  *
1031  * \param jpeg_quality Only for JPEG files, the quality of the compression
1032  * in the range [0-100]. Larger is better quality but slower.
1033  * \note jpeg_quality is only effective if MRPT is compiled against OpenCV
1034  * 1.1.0 or newer.
1035  * \return False on any error
1036  * \sa loadFromFile
1037  */
1038  bool saveToFile(const std::string& fileName, int jpeg_quality = 95) const;
1039 
1040  /** Save image to binary stream as a JPEG (.jpg) compressed format.
1041  * \exception std::exception On number of rows or cols equal to zero or
1042  * other errors.
1043  * \sa saveToJPEG
1044  */
1045  void saveToStreamAsJPEG(
1046  mrpt::io::CStream& out, const int jpeg_quality = 95) const;
1047 
1048  /** @} */
1049  // ================================================================
1050 
1051  // ================================================================
1052  /** @name Color/Grayscale conversion
1053  @{ */
1054 
1055  /** Returns a grayscale version of the image, or itself if it is already a
1056  * grayscale image.
1057  */
1058  CImage grayscale() const;
1059 
1060  /** Returns a grayscale version of the image, or itself if it is already a
1061  * grayscale image.
1062  * \sa colorImage
1063  */
1064  void grayscale(CImage& ret) const;
1065 
1066  /** Returns a RGB version of the grayscale image, or itself if it is already
1067  * a RGB image.
1068  * \sa grayscale
1069  */
1070  void colorImage(CImage& ret) const;
1071 
1072  /** Replaces this grayscale image with a RGB version of it.
1073  * \sa grayscaleInPlace
1074  */
1075  void colorImageInPlace();
1076 
1077  /** Replaces the image with a grayscale version of it.
1078  * \sa colorImageInPlace
1079  */
1080  void grayscaleInPlace();
1081 
1082  /** @} */
1083  // ================================================================
1084 
1085  protected:
1086  /** @name Data members
1087  @{ */
1088 
1089  /** The internal IplImage pointer to the actual image content. */
1090  void* img;
1091 
1092  /** Set to true only when using setFromIplImageReadOnly.
1093  * \sa setFromIplImageReadOnly */
1095  /** Set to true only when using setExternalStorage.
1096  * \sa setExternalStorage
1097  */
1099  /** The file name of a external storage image. */
1101 
1102  /** @} */
1103 
1104  /** Resize the buffers in "img" to accomodate a new image size and/or
1105  * format.
1106  */
1107  void changeSize(
1108  unsigned int width, unsigned int height, TImageChannels nChannels,
1109  bool originTopLeft);
1110 
1111  /** Release the internal IPL image, if not nullptr or read-only. */
1112  void releaseIpl(bool thisIsExternalImgUnload = false) noexcept;
1113 
1114  /** Checks if the image is of type "external storage", and if so and not
1115  * loaded yet, load it.
1116  * \exception CExceptionExternalImageNotFound */
1117  void makeSureImageIsLoaded() const;
1118 
1119 }; // End of class
1120 } // end of namespace img
1121 } // end of namespace mrpt
mrpt::img::CImage::operator()
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:452
mrpt::img::CImage::saveToStreamAsJPEG
void saveToStreamAsJPEG(mrpt::io::CStream &out, const int jpeg_quality=95) const
Save image to binary stream as a JPEG (.jpg) compressed format.
Definition: CImage_JPEG_streams.cpp:348
mrpt::img::CImage::resize
void resize(unsigned int width, unsigned int height, TImageChannels nChannels, bool originTopLeft)
Changes the size of the image, erasing previous contents (does NOT scale its current content,...
Definition: img/CImage.h:261
mrpt::img::CExceptionExternalImageNotFound::CExceptionExternalImageNotFound
CExceptionExternalImageNotFound(const std::string &s)
Definition: img/CImage.h:58
mrpt::img::CImage::CImage
CImage(const CImage &other_img, TConstructorFlags_CImage constructor_flag)
Fast constructor of a grayscale version of another image, making a reference to the original image if...
Definition: img/CImage.h:190
mrpt::img::CImage::getWidth
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:864
mrpt::img::CImage::getExternalStorageFile
std::string getExternalStorageFile() const noexcept
Only if isExternallyStored() returns true.
Definition: img/CImage.h:796
mrpt::img::CImage::flipHorizontal
void flipHorizontal()
Flips the image horizontally.
Definition: CImage.cpp:1994
mrpt::img::CImage::filterMedianInPlace
void filterMedianInPlace(int W=3)
Filter the image with a Median filter with a window size WxH, replacing "this" image by the filtered ...
Definition: CImage.cpp:2143
mrpt::img::TConstructorFlags_CImage
TConstructorFlags_CImage
For usage in one of the CImage constructors.
Definition: img/CImage.h:48
mrpt::img::CImage::getChannelCount
TImageChannels getChannelCount() const
Returns the number of channels, typically 1 (GRAY) or 3 (RGB)
Definition: CImage.cpp:920
mrpt::img::CCanvas::psSolid
@ psSolid
Definition: CCanvas.h:58
mrpt::img::CImage::getAsMatrix
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) const
Returns the image as a matrix with pixel grayscale values in the range [0,1].
Definition: CImage.cpp:1616
red
unsigned char red[10]
Definition: PbMapMaker.cpp:1298
mrpt::img::CImage::setImagesPathBase
static void setImagesPathBase(const std::string &path)
Definition: CImage.cpp:54
s
GLdouble s
Definition: glext.h:3676
green
GLclampf green
Definition: glext.h:3525
mrpt::img::CImage::scaleImage
void scaleImage(unsigned int width, unsigned int height, TInterpolationMethod interp=IMG_INTERP_CUBIC)
Scales this image to a new size, interpolating as needed.
Definition: CImage.cpp:2217
mrpt::img::CImage::scaleHalfSmooth
CImage scaleHalfSmooth() const
Returns a new image scaled down to half its original size (averaging between every two rows)
Definition: img/CImage.h:363
mrpt::img::CImage::filterMedian
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:2117
mrpt::img::CImage::m_imgIsExternalStorage
bool m_imgIsExternalStorage
Set to true only when using setExternalStorage.
Definition: img/CImage.h:1098
mrpt::img::CImage::getSize
TImageSize getSize() const
Return the size of the image.
Definition: img/CImage.h:661
mrpt::img::CImage::flipVertical
void flipVertical(bool also_swapRB=false)
Flips the image vertically.
Definition: CImage.cpp:1984
mrpt::img::CImage::getRowStride
size_t getRowStride() const
Returns the row stride of the image: this is the number of bytes between two consecutive rows.
Definition: CImage.cpp:878
mrpt::img::CImage::DISABLE_ZIP_COMPRESSION
static bool DISABLE_ZIP_COMPRESSION
By default, when storing images through the CSerializable interface, grayscale images will be ZIP com...
Definition: img/CImage.h:233
mrpt::img::CImage::m_imgIsReadOnly
bool m_imgIsReadOnly
Set to true only when using setFromIplImageReadOnly.
Definition: img/CImage.h:1094
mrpt::img::CImage::makeSureImageIsLoaded
void makeSureImageIsLoaded() const
Checks if the image is of type "external storage", and if so and not loaded yet, load it.
Definition: CImage.cpp:1928
CH_RGB
#define CH_RGB
Definition: img/CImage.h:45
mrpt::img::CImage::setFromImageReadOnly
void setFromImageReadOnly(const CImage &other_img)
Sets the internal IplImage pointer to that of another given image, WITHOUT making a copy,...
Definition: img/CImage.h:880
mrpt::img::CImage::colorImageInPlace
void colorImageInPlace()
Replaces this grayscale image with a RGB version of it.
Definition: CImage.cpp:2415
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
mrpt::img::CImage::SERIALIZATION_JPEG_QUALITY
static int SERIALIZATION_JPEG_QUALITY
Unless DISABLE_JPEG_COMPRESSION=true, this sets the JPEG quality (range 1-100) of serialized RGB imag...
Definition: img/CImage.h:245
mrpt::img::CImage::cross_correlation
void cross_correlation(const CImage &patch_img, size_t &u_max, size_t &v_max, double &max_val, int u_search_ini=-1, int v_search_ini=-1, int u_search_size=-1, int v_search_size=-1, CImage *out_corr_image=nullptr) const
Computes the correlation between this image and another one, encapsulating the openCV function cvMatc...
Definition: CImage.cpp:1454
mrpt::img::CImage::CImage
CImage(const Eigen::MatrixBase< Derived > &m, bool matrix_is_normalized)
Explicit constructor from a matrix, interpreted as grayscale intensity values, in the range [0,...
Definition: img/CImage.h:212
mrpt::img::CImage::operator=
CImage & operator=(const CImage &o)
Copy operator (if the image is externally stored, the writen image will be such as well).
Definition: CImage.cpp:105
mrpt::img::CImage::grayscale
CImage grayscale() const
Returns a grayscale version of the image, or itself if it is already a grayscale image.
Definition: CImage.cpp:994
mrpt::img::CImage::copyFromForceLoad
void copyFromForceLoad(const CImage &o)
Copies from another image, and, if that one is externally stored, the image file will be actually loa...
Definition: CImage.cpp:148
mrpt::img::CImage::m_externalFile
std::string m_externalFile
The file name of a external storage image.
Definition: img/CImage.h:1100
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::img::CImage::getAs
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img....
Definition: img/CImage.h:599
mrpt::img::CImage::correlate
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:1398
interp
GLuint interp
Definition: glext.h:7133
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::img::CImage::colorImage
void colorImage(CImage &ret) const
Returns a RGB version of the grayscale image, or itself if it is already a RGB image.
Definition: CImage.cpp:2391
mrpt::img::CImage::copyFastFrom
void copyFastFrom(CImage &o)
Moves an image from another object, erasing the origin image in the process (this is much faster than...
Definition: CImage.cpp:168
mrpt::img::CImage::DISABLE_JPEG_COMPRESSION
static bool DISABLE_JPEG_COMPRESSION
By default, when storing images through the CSerializable interface, RGB images are JPEG-compressed t...
Definition: img/CImage.h:240
mrpt::img::CImage::isColor
bool isColor() const
Returns true if the image is RGB, false if it is grayscale.
Definition: CImage.cpp:906
TCamera.h
mrpt::img::CImage::setFromMatrix
void setFromMatrix(const Eigen::MatrixBase< Derived > &m, bool matrix_is_normalized=true)
Set the image from a matrix, interpreted as grayscale intensity values, in the range [0,...
Definition: img/CImage.h:891
mrpt::img::CImage::getAsFloat
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,...
Definition: CImage.cpp:948
mrpt::img::CImage::setPixel
void setPixel(int x, int y, size_t color) override
Changes the value of the pixel (x,y).
Definition: CImage.cpp:1243
mrpt::img::CImage::swapRB
void swapRB()
Swaps red and blue channels.
Definition: CImage.cpp:2005
mrpt::img::CImage::rotateImage
void rotateImage(double angle_radians, unsigned int center_x, unsigned int center_y, double scale=1.0)
Rotates the image by the given angle around the given center point, with an optional scale factor.
Definition: CImage.cpp:2280
mrpt::img::CImage::forceLoad
void forceLoad() const
For external storage image objects only, this method makes sure the image is loaded in memory.
Definition: img/CImage.h:817
mrpt::img::CExceptionExternalImageNotFound
Used in mrpt::img::CImage.
Definition: img/CImage.h:55
mrpt::math::CMatrixTemplateNumeric
A matrix of dynamic size.
Definition: CMatrixTemplateNumeric.h:37
mrpt::img::CImage::img
void * img
The internal IplImage pointer to the actual image content.
Definition: img/CImage.h:1090
mrpt::img::CImage::scaleHalf
CImage scaleHalf() const
Returns a new image scaled down to half its original size.
Definition: img/CImage.h:348
mrpt::img::CImage::loadFromIplImage
void loadFromIplImage(void *iplImage)
Reads the image from a OpenCV IplImage object (making a COPY).
Definition: CImage.cpp:321
mrpt::img::IMG_INTERP_LINEAR
@ IMG_INTERP_LINEAR
Definition: img/CImage.h:37
mrpt::img::CImage::getMaxAsFloat
float getMaxAsFloat() const
Return the maximum pixel value of the image, as a float value in the range [0,1].
Definition: CImage.cpp:979
val
int val
Definition: mrpt_jpeglib.h:955
mrpt::img::TImageChannels
int TImageChannels
For use in mrpt::img::CImage.
Definition: img/CImage.h:43
pixels
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: glext.h:3602
mrpt::img::CImage::setFromIplImageReadOnly
void setFromIplImageReadOnly(void *iplImage)
Reads the image from a OpenCV IplImage object (WITHOUT making a copy) and from now on the image canno...
Definition: CImage.cpp:340
mrpt::img::CImage::loadFromMemoryBuffer
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:385
mrpt::img::CCanvas
This virtual class defines the interface of any object accepting drawing primitives on it.
Definition: CCanvas.h:42
mrpt::img::CImage::getAsRGBMatrices
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:1672
mrpt::math::CMatrix
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:24
mrpt::img::CImage::swap
void swap(CImage &o)
Very efficient swap of two images (just swap the internal pointers)
Definition: CImage.cpp:133
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::img::CImage::loadFromStreamAsJPEG
void loadFromStreamAsJPEG(mrpt::io::CStream &in)
Reads the image from a binary stream containing a binary jpeg file.
Definition: CImage_JPEG_streams.cpp:467
mrpt::img::CImage::isOriginTopLeft
bool isOriginTopLeft() const
Returns true if the coordinates origin is top-left, or false if it is bottom-left
Definition: CImage.cpp:934
mrpt::img::CImage::scaleDouble
CImage scaleDouble() const
Returns a new image scaled up to double its original size.
Definition: img/CImage.h:377
mrpt::img::TInterpolationMethod
TInterpolationMethod
Interpolation methods for images.
Definition: img/CImage.h:34
mrpt::img::CImage::~CImage
virtual ~CImage()
Destructor:
Definition: CImage.cpp:219
mrpt::img::CImage::CImage
CImage()
Default constructor: initialize an 1x1 RGB image.
Definition: CImage.cpp:81
mrpt::img::CImage::line
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:1296
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
CCanvas.h
mrpt::serialization::CSerializable
The virtual base class which provides a unified interface for all persistent objects in MRPT.
Definition: CSerializable.h:32
color
GLuint color
Definition: glext.h:8300
mrpt::img::CImage::get_unsafe
unsigned char * get_unsafe(unsigned int col, unsigned int row, unsigned int channel=0) const
Access to pixels without checking boundaries - Use normally the () operator better,...
Definition: CImage.cpp:491
mrpt::img::CImage::drawCircle
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:1315
mrpt::img::TCamera
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:29
mrpt::img::CImage::getImagesPathBase
static const std::string & getImagesPathBase()
By default, ".".
Definition: CImage.cpp:53
mrpt::img::CImage::joinImagesHorz
void joinImagesHorz(const CImage &im1, const CImage &im2)
Joins two images side-by-side horizontally.
Definition: CImage.cpp:2437
mrpt::img::IMG_INTERP_CUBIC
@ IMG_INTERP_CUBIC
Definition: img/CImage.h:38
eigen_frwds.h
height
GLenum GLsizei GLsizei height
Definition: glext.h:3554
mrpt::img::CImage::rectifyImageInPlace
void rectifyImageInPlace(const mrpt::img::TCamera &cameraParams)
Rectify (un-distort) the image according to a certain camera matrix and vector of distortion coeffici...
Definition: CImage.cpp:2050
DECLARE_MEXPLUS_FROM
#define DECLARE_MEXPLUS_FROM(complete_type)
This must be inserted if a custom conversion method for MEX API is implemented in the class.
Definition: CSerializable.h:140
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
mrpt::img::FAST_REF_OR_CONVERT_TO_GRAY
@ FAST_REF_OR_CONVERT_TO_GRAY
Definition: img/CImage.h:51
mrpt::img::CImage::grayscaleInPlace
void grayscaleInPlace()
Replaces the image with a grayscale version of it.
Definition: CImage.cpp:1055
mrpt::img::CImage::setChannelsOrder_RGB
void setChannelsOrder_RGB()
Marks the channel ordering in a color image as "RGB" (this doesn't actually modify the image data,...
Definition: CImage.cpp:2731
mrpt::img::CImage::cross_correlation_FFT
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:1738
mrpt::img::CImage::saveToFile
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:296
scale
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:6503
mrpt::img::CImage::getAsMatrixTiled
void getAsMatrixTiled(math::CMatrix &outMatrix) const
Returns the image as a matrix, where the image is "tiled" (repeated) the required number of times to ...
Definition: CImage.cpp:1831
mrpt::img::CImage::unload
void unload() const noexcept
For external storage image objects only, this method unloads the image from memory (or does nothing i...
Definition: CImage.cpp:1897
mrpt::img::CImage::setFromIplImage
void setFromIplImage(void *iplImage)
Reads the image from a OpenCV IplImage object (WITHOUT making a copy).
Definition: CImage.cpp:363
mrpt::img::CImage::getHeight
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:892
img
GLint GLvoid * img
Definition: glext.h:3763
row
GLenum GLenum GLvoid * row
Definition: glext.h:3576
DEFINE_SERIALIZABLE
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
Definition: CSerializable.h:102
mrpt::img::CImage::setExternalStorage
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:1887
width
GLenum GLsizei width
Definition: glext.h:3531
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::img::CImage::update_patch
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:1332
blue
GLclampf GLclampf blue
Definition: glext.h:3525
mrpt::img::CImage::rectifyImage
void rectifyImage(CImage &out_img, const mrpt::img::TCamera &cameraParams) const
Rectify (un-distort) the image according to some camera parameters, and returns an output un-distorte...
Definition: CImage.cpp:2082
mrpt::img::UNINITIALIZED_IMAGE
@ UNINITIALIZED_IMAGE
Definition: img/CImage.h:50
in
GLuint in
Definition: glext.h:7274
mrpt::img::CImage::equalizeHist
void equalizeHist(CImage &outImg) const
Equalize the image histogram, saving the new image in the given output object.
Definition: CImage.cpp:2478
mrpt::img::CImage::KLT_response
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:2593
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::img::CImage::filterGaussianInPlace
void filterGaussianInPlace(int W=3, int H=3)
Filter the image with a Gaussian filter with a window size WxH, returning the filtered image in out_i...
Definition: CImage.cpp:2193
mrpt::img::CImage::setChannelsOrder_BGR
void setChannelsOrder_BGR()
Marks the channel ordering in a color image as "BGR" (this doesn't actually modify the image data,...
Definition: CImage.cpp:2742
mrpt::img::CImage::releaseIpl
void releaseIpl(bool thisIsExternalImgUnload=false) noexcept
Release the internal IPL image, if not nullptr or read-only.
Definition: CImage.cpp:1907
mrpt::img::TPixelCoord
A pair (x,y) of pixel coordinates (integer resolution).
Definition: TPixelCoord.h:39
mrpt::img::CImage::filterGaussian
void filterGaussian(CImage &out_img, int W=3, int H=3) const
Filter the image with a Gaussian filter with a window size WxH, replacing "this" image by the filtere...
Definition: CImage.cpp:2167
Eigen::MatrixBase
Definition: eigen_frwds.h:20
mrpt::img::CImage::loadTGA
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:2757
mrpt::img::CImage::changeSize
void changeSize(unsigned int width, unsigned int height, TImageChannels nChannels, bool originTopLeft)
Resize the buffers in "img" to accomodate a new image size and/or format.
Definition: CImage.cpp:223
CSerializable.h
TPixelCoord.h
mrpt::img::CImage::setFromRGBMatrices
void setFromRGBMatrices(const Eigen::MatrixBase< Derived > &m_r, const Eigen::MatrixBase< Derived > &m_g, const Eigen::MatrixBase< Derived > &m_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:927
mrpt::img::CImage::loadFromFile
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:271
mrpt::img::CImage::equalizeHistInPlace
void equalizeHistInPlace()
Equalize the image histogram, replacing the original image.
Definition: CImage.cpp:2517
mrpt::img::CImage::getChannelsOrder
const char * getChannelsOrder() const
Returns a string of the form "BGR","RGB" or "GRAY" indicating the channels ordering.
Definition: CImage.cpp:1180
mrpt::img::CImage::drawChessboardCorners
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:2327
mrpt::img::IMG_INTERP_NN
@ IMG_INTERP_NN
Definition: img/CImage.h:36
mrpt::img::IMG_INTERP_AREA
@ IMG_INTERP_AREA
Definition: img/CImage.h:39
mrpt::img::CImage::extract_patch
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:1359
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::img::CCanvas::TPenStyle
TPenStyle
Definition of pen styles.
Definition: CCanvas.h:56
DECLARE_MEX_CONVERSION
#define DECLARE_MEX_CONVERSION
This must be inserted if a custom conversion method for MEX API is implemented in the class.
Definition: CSerializable.h:131
mrpt::img::CImage::normalize
void normalize()
Optimize the brightness range of an image without using histogram Only for one channel images.
Definition: CImage.cpp:1571
mrpt::img::CImage::CImage
CImage(TConstructorFlags_CImage)
Fast constructor that leaves the image uninitialized (the internal IplImage pointer set to nullptr).
Definition: img/CImage.h:170
x
GLenum GLint x
Definition: glext.h:3538
mrpt::img::CImage::getExternalStorageFileAbsolutePath
std::string getExternalStorageFileAbsolutePath() const
Only if isExternallyStored() returns true.
Definition: img/CImage.h:805
mrpt::img::CImage::loadFromXPM
bool loadFromXPM(const char *const *xpm_array, bool swap_rb=true)
Loads the image from an XPM array, as #include'd from a ".xpm" file.
Definition: CImage_loadXPM.cpp:482
mrpt::img::CImage::isExternallyStored
bool isExternallyStored() const noexcept
See setExternalStorage().
Definition: img/CImage.h:793
mrpt::img::CImage::getAs
T * getAs()
Returns a pointer to a T* containing the image - the idea is to call like "img.getAs<IplImage>()" so ...
Definition: img/CImage.h:608
mrpt::img::CImage::setOriginTopLeft
void setOriginTopLeft(bool val)
Changes the property of the image stating if the top-left corner (vs.
Definition: CImage.cpp:1231
mrpt::io::CStream
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:30



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST