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 }
mrpt::img::CMappedImage::m_x1
double m_x1
Definition: CMappedImage.h:29
mrpt::img::CMappedImage::m_y0
double m_y0
Definition: CMappedImage.h:29
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::img::CMappedImage::m_method
TInterpolationMethod m_method
Definition: CMappedImage.h:32
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
CMappedImage.h
mrpt::img::IMG_INTERP_LINEAR
@ IMG_INTERP_LINEAR
Definition: img/CImage.h:37
mrpt::utils::CImage
mrpt::img::CImage CImage
Definition: utils/CImage.h:7
mrpt::img
Definition: CCanvas.h:17
mrpt::img::CMappedImage::m_x0
double m_x0
Definition: CMappedImage.h:29
mrpt::img::CMappedImage::CMappedImage
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.
Definition: CMappedImage.cpp:22
mrpt::img::CMappedImage::changeCoordinates
void changeCoordinates(double x0, double x1, double y0, double y1)
Changes the coordinates of the image (see constructor for the meaning)
Definition: CMappedImage.cpp:46
mrpt::round
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::img::CMappedImage::m_y1
double m_y1
Definition: CMappedImage.h:29
mrpt::img::TInterpolationMethod
TInterpolationMethod
Interpolation methods for images.
Definition: img/CImage.h:34
round.h
img-precomp.h
mrpt::img::IMG_INTERP_CUBIC
@ IMG_INTERP_CUBIC
Definition: img/CImage.h:38
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
mrpt::img::CImage::Ptr
std::shared_ptr< CImage > Ptr
Definition: img/CImage.h:132
img
GLint GLvoid * img
Definition: glext.h:3763
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::img::CMappedImage::m_pixel_size
double m_pixel_size
width * pixel_size = (x1-x0)
Definition: CMappedImage.h:31
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::img::CMappedImage::m_img
CImage::Ptr m_img
Definition: CMappedImage.h:28
mrpt::img::IMG_INTERP_NN
@ IMG_INTERP_NN
Definition: img/CImage.h:36
mrpt::img::IMG_INTERP_AREA
@ IMG_INTERP_AREA
Definition: img/CImage.h:39
y
GLenum GLint GLint y
Definition: glext.h:3538
x
GLenum GLint x
Definition: glext.h:3538
mrpt::img::CMappedImage::getPixel
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...
Definition: CMappedImage.cpp:73



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