Main MRPT website > C++ reference for MRPT 1.9.9
CFeatureExtraction_polarImg.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 "vision-precomp.h" // Precompiled headers
11 
13 
14 // Universal include for all versions of OpenCV
15 #include <mrpt/otherlibs/do_opencv_includes.h>
16 
17 using namespace mrpt;
18 using namespace mrpt::vision;
19 using namespace mrpt::img;
20 using namespace mrpt::system;
21 using namespace std;
22 
23 /****************************************************************************************
24  Linear-Polar Transform
25  J.L. Blanco, Apr 2009
26 ****************************************************************************************/
27 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM < 0x111
29  const CvArr* srcarr, CvArr* dstarr, CvPoint2D32f center, double maxRadius,
30  int flags)
31 {
32  CvMat* mapx = 0;
33  CvMat* mapy = 0;
34  float* buf = 0;
35 
36  CV_FUNCNAME("cvLinPolar");
37 
38  __BEGIN__;
39 
40  CvMat srcstub, *src = (CvMat *)srcarr;
41  CvMat dststub, *dst = (CvMat *)dstarr;
42  CvSize ssize, dsize;
43 
44  CV_CALL(src = cvGetMat(srcarr, &srcstub, 0, 0));
45  CV_CALL(dst = cvGetMat(dstarr, &dststub, 0, 0));
46 
47  if (!CV_ARE_TYPES_EQ(src, dst)) CV_ERROR(CV_StsUnmatchedFormats, "");
48 
49  ssize.width = src->cols;
50  ssize.height = src->rows;
51  dsize.width = dst->cols;
52  dsize.height = dst->rows;
53 
54  CV_CALL(mapx = cvCreateMat(dsize.height, dsize.width, CV_32F));
55  CV_CALL(mapy = cvCreateMat(dsize.height, dsize.width, CV_32F));
56 
57  if (!(flags & CV_WARP_INVERSE_MAP))
58  {
59  int phi, rho;
60 
61  for (phi = 0; phi < dsize.height; phi++)
62  {
63  double cp = cos(phi * 2 * CV_PI / (dsize.height - 1));
64  double sp = sin(phi * 2 * CV_PI / (dsize.height - 1));
65  float* mx = (float*)(mapx->data.ptr + phi * mapx->step);
66  float* my = (float*)(mapy->data.ptr + phi * mapy->step);
67 
68  for (rho = 0; rho < dsize.width; rho++)
69  {
70  double r = maxRadius * (rho + 1) / double(dsize.width - 1);
71  double x = r * cp + center.x;
72  double y = r * sp + center.y;
73 
74  mx[rho] = (float)x;
75  my[rho] = (float)y;
76  }
77  }
78  }
79  else
80  {
81  int x, y;
82  CvMat bufx, bufy, bufp, bufa;
83  const double ascale = (ssize.height - 1) / (2 * CV_PI);
84  const double pscale = (ssize.width - 1) / maxRadius;
85 
86  CV_CALL(buf = (float*)cvAlloc(4 * dsize.width * sizeof(buf[0])));
87 
88  bufx = cvMat(1, dsize.width, CV_32F, buf);
89  bufy = cvMat(1, dsize.width, CV_32F, buf + dsize.width);
90  bufp = cvMat(1, dsize.width, CV_32F, buf + dsize.width * 2);
91  bufa = cvMat(1, dsize.width, CV_32F, buf + dsize.width * 3);
92 
93  for (x = 0; x < dsize.width; x++) bufx.data.fl[x] = (float)x - center.x;
94 
95  for (y = 0; y < dsize.height; y++)
96  {
97  float* mx = (float*)(mapx->data.ptr + y * mapx->step);
98  float* my = (float*)(mapy->data.ptr + y * mapy->step);
99 
100  for (x = 0; x < dsize.width; x++)
101  bufy.data.fl[x] = (float)y - center.y;
102 
103  cvCartToPolar(&bufx, &bufy, &bufp, &bufa, 0);
104 
105  for (x = 0; x < dsize.width; x++) bufp.data.fl[x] += 1.f;
106 
107  for (x = 0; x < dsize.width; x++)
108  {
109  double rho = bufp.data.fl[x] * pscale;
110  double phi = bufa.data.fl[x] * ascale;
111  mx[x] = (float)rho;
112  my[x] = (float)phi;
113  }
114  }
115  }
116 
117  cvRemap(src, dst, mapx, mapy, flags, cvScalarAll(0));
118 
119  __END__;
120 
121  cvFree(&buf);
122  cvReleaseMat(&mapx);
123  cvReleaseMat(&mapy);
124 }
125 #endif
126 
128  const mrpt::img::CImage& in_img, CFeatureList& in_features) const
129 {
130  MRPT_START
131 #if MRPT_HAS_OPENCV
132 
133  ASSERT_(options.PolarImagesOptions.radius > 1);
134  ASSERT_(options.PolarImagesOptions.bins_angle > 1);
135  ASSERT_(options.PolarImagesOptions.bins_distance > 1);
136 
137  const unsigned int radius = options.PolarImagesOptions.radius;
138  const unsigned int patch_w = options.PolarImagesOptions.bins_distance;
139  const unsigned int patch_h = options.PolarImagesOptions.bins_angle;
140 
141  CImage linpolar_frame(patch_w, patch_h, in_img.getChannelCount());
142 
143  // Compute intensity-domain spin images
144  for (CFeatureList::iterator it = in_features.begin();
145  it != in_features.end(); ++it)
146  {
147  // Overwrite scale with the descriptor scale:
148  (*it)->scale = radius;
149 
150 // Use OpenCV to convert:
151 #if MRPT_OPENCV_VERSION_NUM < 0x111
152  my_cvLinearPolar( // Use version embedded above in this file
153 #else
154  cvLinearPolar( // Use version sent to OpenCV
155 #endif
156  in_img.getAs<IplImage>(),
157  linpolar_frame.getAs<IplImage>(),
158  cvPoint2D32f( (*it)->x,(*it)->y ),
159  radius,
160  CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
161 
162  // Get the image as a matrix and save as patch:
163  linpolar_frame.getAsMatrix((*it)->descriptors.PolarImg);
164 
165  } // end for it
166 
167 #else
168  THROW_EXCEPTION("This method needs MRPT compiled with OpenCV support");
169 #endif
170  MRPT_END
171 }
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img.getAs<IplImage>()" so we can avoid here including OpenCV&#39;s headers.
Definition: img/CImage.h:599
#define MRPT_START
Definition: exceptions.h:262
TInternalFeatList::iterator iterator
Definition: CFeature.h:365
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:25
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
#define CV_PI
TImageChannels getChannelCount() const
Returns the number of channels, typically 1 (GRAY) or 3 (RGB)
Definition: CImage.cpp:920
STL namespace.
GLuint src
Definition: glext.h:7278
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
GLuint dst
Definition: glext.h:7135
void my_cvLinearPolar(const CvArr *srcarr, CvArr *dstarr, CvPoint2D32f center, double maxRadius, int flags)
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:20
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:304
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
void internal_computePolarImageDescriptors(const mrpt::img::CImage &in_img, CFeatureList &in_features) const
Compute a polar-image descriptor of the provided features into the input image.
#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



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