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-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  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::utils::round(px);
94  const int py0 = mrpt::utils::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 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
double m_pixel_size
width * pixel_size = (x1-x0)
Definition: CMappedImage.h:32
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:118
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.
#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)
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...
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_END
GLint GLvoid * img
Definition: glext.h:3763
std::shared_ptr< CImage > Ptr
Definition: CImage.h:120
TInterpolationMethod
Interpolation methods for images.
Definition: CImage.h:32
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_(f)
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25
GLenum GLint GLint y
Definition: glext.h:3538
TInterpolationMethod m_method
Definition: CMappedImage.h:33
GLenum GLint x
Definition: glext.h:3538



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