Main MRPT website > C++ reference for MRPT 1.5.6
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-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 "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::system;
20 using namespace std;
21 
22 
23 /****************************************************************************************
24  Linear-Polar Transform
25  J.L. Blanco, Apr 2009
26 ****************************************************************************************/
27 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM < 0x111
28 void my_cvLinearPolar( const CvArr* srcarr, CvArr* dstarr,
29  CvPoint2D32f center, double maxRadius, int flags )
30 {
31  CvMat* mapx = 0;
32  CvMat* mapy = 0;
33  float* buf = 0;
34 
35  CV_FUNCNAME( "cvLinPolar" );
36 
37  __BEGIN__;
38 
39  CvMat srcstub, *src = (CvMat*)srcarr;
40  CvMat dststub, *dst = (CvMat*)dstarr;
41  CvSize ssize, dsize;
42 
43  CV_CALL( src = cvGetMat( srcarr, &srcstub,0,0 ));
44  CV_CALL( dst = cvGetMat( dstarr, &dststub,0,0 ));
45 
46  if( !CV_ARE_TYPES_EQ( src, dst ))
47  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++ )
94  bufx.data.fl[x] = (float)x - center.x;
95 
96  for( y = 0; y < dsize.height; y++ )
97  {
98  float* mx = (float*)(mapx->data.ptr + y*mapx->step);
99  float* my = (float*)(mapy->data.ptr + y*mapy->step);
100 
101  for( x = 0; x < dsize.width; x++ )
102  bufy.data.fl[x] = (float)y - center.y;
103 
104  cvCartToPolar( &bufx, &bufy, &bufp, &bufa, 0 );
105 
106  for( x = 0; x < dsize.width; x++ )
107  bufp.data.fl[x] += 1.f;
108 
109  for( x = 0; x < dsize.width; x++ )
110  {
111  double rho = bufp.data.fl[x]*pscale;
112  double phi = bufa.data.fl[x]*ascale;
113  mx[x] = (float)rho;
114  my[x] = (float)phi;
115  }
116  }
117  }
118 
119  cvRemap( src, dst, mapx, mapy, flags, cvScalarAll(0) );
120 
121  __END__;
122 
123  cvFree( &buf );
124  cvReleaseMat( &mapx );
125  cvReleaseMat( &mapy );
126 }
127 #endif
128 
129 
130 /************************************************************************************************
131  computePolarImageDescriptors
132 ************************************************************************************************/
134  const mrpt::utils::CImage &in_img,
135  CFeatureList &in_features) const
136 {
137  MRPT_START
138 #if MRPT_HAS_OPENCV
139 
140  ASSERT_(options.PolarImagesOptions.radius>1)
141  ASSERT_(options.PolarImagesOptions.bins_angle>1)
142  ASSERT_(options.PolarImagesOptions.bins_distance>1)
143 
144  const unsigned int radius = options.PolarImagesOptions.radius;
145  const unsigned int patch_w = options.PolarImagesOptions.bins_distance;
146  const unsigned int patch_h = options.PolarImagesOptions.bins_angle;
147 
148  CImage linpolar_frame( patch_w, patch_h, in_img.getChannelCount() );
149 
150  // Compute intensity-domain spin images
151  for (CFeatureList::iterator it=in_features.begin();it!=in_features.end();++it)
152  {
153  // Overwrite scale with the descriptor scale:
154  (*it)->scale = radius;
155 
156  // Use OpenCV to convert:
157 #if MRPT_OPENCV_VERSION_NUM < 0x111
158  my_cvLinearPolar( // Use version embedded above in this file
159 #else
160  cvLinearPolar( // Use version sent to OpenCV
161 #endif
162  in_img.getAs<IplImage>(),
163  linpolar_frame.getAs<IplImage>(),
164  cvPoint2D32f( (*it)->x,(*it)->y ),
165  radius,
166  CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
167 
168  // Get the image as a matrix and save as patch:
169  linpolar_frame.getAsMatrix( (*it)->descriptors.PolarImg );
170 
171  } // end for it
172 
173 #else
174  THROW_EXCEPTION("This method needs MRPT compiled with OpenCV support")
175 #endif
176  MRPT_END
177 }
178 
TImageChannels getChannelCount() const
Returns the number of channels, typically 1 (GRAY) or 3 (RGB)
Definition: CImage.cpp:912
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
#define CV_PI
#define THROW_EXCEPTION(msg)
STL namespace.
GLuint src
Definition: glext.h:6303
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: CImage.h:517
GLuint dst
Definition: glext.h:6198
#define MRPT_END
void my_cvLinearPolar(const CvArr *srcarr, CvArr *dstarr, CvPoint2D32f center, double maxRadius, int flags)
Classes for computer vision, detectors, features, etc.
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:211
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
#define ASSERT_(f)
TInternalFeatList::iterator iterator
Definition: CFeature.h:261
GLenum GLint GLint y
Definition: glext.h:3516
void internal_computePolarImageDescriptors(const mrpt::utils::CImage &in_img, CFeatureList &in_features) const
Compute a polar-image descriptor of the provided features into the input image.
GLenum GLint x
Definition: glext.h:3516



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019