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



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014