MRPT  2.0.1
CMappedImage.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "img-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/round.h>
13 #include <mrpt/img/CMappedImage.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  auto* 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 unsigned int px0 = mrpt::round(px);
94  const unsigned int py0 = mrpt::round(py);
95  return static_cast<double>((*m_img).at<uint8_t>(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 auto P11 =
109  static_cast<double>((*m_img).at<uint8_t>(px0, py0));
110  const auto P12 =
111  static_cast<double>((*m_img).at<uint8_t>(px0, py1));
112  const auto P21 =
113  static_cast<double>((*m_img).at<uint8_t>(px1, py0));
114  const auto P22 =
115  static_cast<double>((*m_img).at<uint8_t>(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  case IMG_INTERP_AREA:
128  default:
130  "The selected interpolation method is not supported in this "
131  "method.");
132  };
133 }
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:241
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:67
double m_pixel_size
width * pixel_size = (x1-x0)
Definition: CMappedImage.h:29
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 ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::img::CImage CImage
Definition: utils/CImage.h:5
This base provides a set of functions for maths stuff.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::shared_ptr< mrpt::img ::CImage > Ptr
Definition: img/CImage.h:150
TInterpolationMethod
Interpolation methods for images.
Definition: img/CImage.h:50
TInterpolationMethod m_method
Definition: CMappedImage.h:30
#define MRPT_END
Definition: exceptions.h:245
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:24



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020