Main MRPT website > C++ reference for MRPT 1.9.9
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-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "img-precomp.h" // Precompiled headers
11 
12 #include <mrpt/img/CMappedImage.h>
13 #include <mrpt/core/round.h>
14 
15 using namespace mrpt;
16 using namespace mrpt::img;
17 using namespace mrpt::math;
18 
19 /*---------------------------------------------------------------
20  Constructor
21  ---------------------------------------------------------------*/
23  CImage::Ptr img, double x0, double x1, double y0, double y1,
24  TInterpolationMethod method)
25  : m_img(img),
26  m_x0(x0),
27  m_x1(x1),
28  m_y0(y0),
29  m_y1(y1),
30  m_pixel_size(0),
31  m_method(method)
32 {
33  m_img->grayscale();
34  if (m_img->isColor())
35  {
36  CImage* new_img = new CImage();
37  m_img->grayscale(*new_img);
38  m_img = CImage::Ptr(new_img);
39  }
40  changeCoordinates(x0, x1, y0, y1);
41 }
42 
43 /*---------------------------------------------------------------
44  changeCoordinates
45  ---------------------------------------------------------------*/
46 void CMappedImage::changeCoordinates(double x0, double x1, double y0, double y1)
47 {
49  ASSERT_(x0 != x1);
50  ASSERT_(y0 != y1);
51 
52  m_x0 = x0;
53  m_x1 = x1;
54  m_y0 = y0;
55  m_y1 = y1;
56 
57  if (y1 < 0 || x1 < 0)
58  {
59  m_x1 = m_img->getWidth() - 1;
60  m_y1 = m_img->getHeight() - 1;
61  }
62 
63  ASSERT_(m_img->getWidth() > 0 && m_img->getHeight());
64 
65  m_pixel_size = (m_x1 - m_x0) / m_img->getWidth();
66 
67  MRPT_END
68 }
69 
70 /*---------------------------------------------------------------
71  getPixel
72  ---------------------------------------------------------------*/
73 double CMappedImage::getPixel(double x, double y) const
74 {
75  // Image size:
76  const size_t W = m_img->getWidth();
77  const size_t H = m_img->getHeight();
78 
79  // the sub-pixel pixel coordinates:
80  const double px = (x - m_x0) / m_pixel_size;
81  const double py = (y - m_y0) / m_pixel_size;
82 
83  if (px < 0 || py < 0 || px > W || py > H)
84  {
85  return 0;
86  } // Out of image
87 
88  switch (m_method)
89  {
90  case IMG_INTERP_NN:
91  {
92  // The closest pixel:
93  const int px0 = mrpt::round(px);
94  const int py0 = mrpt::round(py);
95  return static_cast<double>(*m_img->get_unsafe(px0, py0));
96  }
97  break;
98  case IMG_INTERP_LINEAR:
99  {
100  // See: http://en.wikipedia.org/wiki/Bilinear_interpolation
101 
102  // The four pixels around:
103  const int px0 = (int)floor(px);
104  const int px1 = (int)ceil(px);
105  const int py0 = (int)floor(py);
106  const int py1 = (int)ceil(py);
107 
108  const double P11 =
109  static_cast<double>(*m_img->get_unsafe(px0, py0));
110  const double P12 =
111  static_cast<double>(*m_img->get_unsafe(px0, py1));
112  const double P21 =
113  static_cast<double>(*m_img->get_unsafe(px1, py0));
114  const double P22 =
115  static_cast<double>(*m_img->get_unsafe(px1, py1));
116 
117  const double R1 = P11 * (px1 - px) /* /(px1-px0)*/ +
118  P21 * (px - px0) /* /(px1-px0) */;
119  const double R2 = P12 * (px1 - px) /* /(px1-px0)*/ +
120  P22 * (px - px0) /* /(px1-px0) */;
121 
122  return R1 * (py1 - py) + R2 * (py - py0);
123  }
124  break;
125 
126  case IMG_INTERP_CUBIC:
127  {
128  THROW_EXCEPTION("TO DO!");
129  }
130  break;
131 
132  case IMG_INTERP_AREA:
133  default:
135  "The selected interpolation method is not supported in this "
136  "method.");
137  };
138 }
void changeCoordinates(double x0, double x1, double y0, double y1)
Changes the coordinates of the image (see constructor for the meaning)
#define MRPT_START
Definition: exceptions.h:262
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 THROW_EXCEPTION(msg)
Definition: exceptions.h:41
double m_pixel_size
width * pixel_size = (x1-x0)
Definition: CMappedImage.h:31
CMappedImage(CImage::Ptr 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.
std::shared_ptr< CImage > Ptr
Definition: img/CImage.h:132
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::img::CImage CImage
Definition: utils/CImage.h:7
This base provides a set of functions for maths stuff.
GLint GLvoid * img
Definition: glext.h:3763
TInterpolationMethod
Interpolation methods for images.
Definition: img/CImage.h:34
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
TInterpolationMethod m_method
Definition: CMappedImage.h:32
#define MRPT_END
Definition: exceptions.h:266
GLenum GLint GLint y
Definition: glext.h:3538
GLenum GLint x
Definition: glext.h:3538
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019