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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019