Main MRPT website > C++ reference for MRPT 1.5.6
CMappedImage.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/utils/round.h>
14 
15 using namespace mrpt;
16 using namespace mrpt::utils;
17 using namespace mrpt::math;
18 
19 
20 /*---------------------------------------------------------------
21  Constructor
22  ---------------------------------------------------------------*/
23 CMappedImage::CMappedImage( CImagePtr img, double x0, double x1, double y0, double y1, TInterpolationMethod method ) :
24  m_img( img ),
25  m_x0 (x0),
26  m_x1 (x1),
27  m_y0 (y0),
28  m_y1 (y1),
29  m_pixel_size(0),
30  m_method( method )
31 {
32  m_img->grayscale();
33  if (m_img->isColor())
34  {
35  CImage *new_img = new CImage();
36  m_img->grayscale(*new_img);
37  m_img = CImagePtr( new_img );
38  }
39  changeCoordinates(x0,x1,y0,y1);
40 }
41 
42 /*---------------------------------------------------------------
43  changeCoordinates
44  ---------------------------------------------------------------*/
45 void CMappedImage::changeCoordinates(double x0, double x1, double y0, double y1)
46 {
48  ASSERT_(x0!=x1);
49  ASSERT_(y0!=y1);
50 
51  m_x0 =x0; m_x1 =x1;
52  m_y0 =y0; m_y1 =y1;
53 
54  if (y1<0 || x1<0)
55  {
56  m_x1 = m_img->getWidth()-1;
57  m_y1 = m_img->getHeight()-1;
58  }
59 
60  ASSERT_( m_img->getWidth()>0 && m_img->getHeight() );
61 
62  m_pixel_size = (m_x1-m_x0) / m_img->getWidth();
63 
64  MRPT_END
65 }
66 
67 /*---------------------------------------------------------------
68  getPixel
69  ---------------------------------------------------------------*/
70 double CMappedImage::getPixel(double x,double y ) const
71 {
72  // Image size:
73  const size_t W = m_img->getWidth();
74  const size_t H = m_img->getHeight();
75 
76  // the sub-pixel pixel coordinates:
77  const double px = (x-m_x0)/m_pixel_size;
78  const double py = (y-m_y0)/m_pixel_size;
79 
80  if (px<0 || py<0 || px>W || py>H) { return 0; } // Out of image
81 
82  switch (m_method)
83  {
84  case IMG_INTERP_NN:
85  {
86  // The closest pixel:
87  const int px0 = mrpt::utils::round(px);
88  const int py0 = mrpt::utils::round(py);
89  return static_cast<double>(*m_img->get_unsafe(px0, py0));
90  }
91  break;
92  case IMG_INTERP_LINEAR:
93  {
94  // See: http://en.wikipedia.org/wiki/Bilinear_interpolation
95 
96  // The four pixels around:
97  const int px0 = (int)floor(px);
98  const int px1 = (int)ceil(px);
99  const int py0 = (int)floor(py);
100  const int py1 = (int)ceil(py);
101 
102  const double P11 = static_cast<double>(*m_img->get_unsafe(px0, py0));
103  const double P12 = static_cast<double>(*m_img->get_unsafe(px0, py1));
104  const double P21 = static_cast<double>(*m_img->get_unsafe(px1, py0));
105  const double P22 = static_cast<double>(*m_img->get_unsafe(px1, py1));
106 
107  const double R1 = P11*(px1-px) /* /(px1-px0)*/ + P21*(px-px0) /* /(px1-px0) */;
108  const double R2 = P12*(px1-px) /* /(px1-px0)*/ + P22*(px-px0) /* /(px1-px0) */;
109 
110  return R1 * (py1-py) + R2 * (py-py0);
111  }
112  break;
113 
114  case IMG_INTERP_CUBIC:
115  {
116  THROW_EXCEPTION("TO DO!");
117  }
118  break;
119 
120  case IMG_INTERP_AREA:
121  default:
122  THROW_EXCEPTION("The selected interpolation method is not supported in this method.");
123  };
124 
125 }
126 
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1166
double m_pixel_size
width * pixel_size = (x1-x0)
Definition: CMappedImage.h:29
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
#define THROW_EXCEPTION(msg)
void changeCoordinates(double x0, double x1, double y0, double y1)
Changes the coordinates of the image (see constructor for the meaning)
GLint GLvoid * img
Definition: glew.h:1290
#define MRPT_END
GLint GLint GLint GLint GLint x
Definition: glew.h:1166
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
TInterpolationMethod
Interpolation methods for images.
Definition: CImage.h:31
#define MRPT_START
double getPixel(double x, double y) const
Returns the interpolated pixel at the coordinates (x,y), in the range [0,255] (grayscale) If the poin...
#define ASSERT_(f)
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:26
CMappedImage(CImagePtr img, double x0=0, double x1=-1, double y0=0, double y1=-1, TInterpolationMethod method=IMG_INTERP_LINEAR)
Constructor: Must pass an image (as a smart pointer) and the coordinates of the border.
TInterpolationMethod m_method
Definition: CMappedImage.h:30



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018