32 #ifndef GSLAM_UNDISTORTER_H 33 #define GSLAM_UNDISTORTER_H 47 unsigned char data[Size];
53 : camera_in(in),camera_out(out),remapX(NULL),remapY(NULL),remapFast(NULL),
54 remapIdx(NULL),remapCoef(NULL),
valid(
true)
61 if( remapX != NULL )
delete[] remapX;
62 if( remapY != NULL )
delete[] remapY;
63 if( remapFast != NULL )
delete[] remapFast;
65 if( remapIdx != NULL )
delete[] remapIdx;
66 if( remapCoef != NULL)
delete[] remapCoef;
101 if(!undistort(image,result))
return GImage();
107 if(!undistortFast(image,result))
return GImage();
117 std::shared_ptr<UndistorterImpl> impl;
120 inline bool UndistorterImpl::prepareReMap()
124 if( !(camera_in.isValid() && camera_out.isValid()) )
131 cout <<
"Undistorter:\n";
132 cout <<
" Camera IN : " << camera_in.info() << endl;
133 cout <<
" Camera OUT: " << camera_out.info() << endl << endl;
134 int size=camera_out.width() * camera_out.height();
135 remapX =
new float[size];
136 remapY =
new float[size];
137 remapFast =
new int[size];
139 remapIdx =
new int [size*4];
140 remapCoef =
new float[size*4];
142 int w_in=camera_in.width(),h_in=camera_in.height();
143 for(
int y=0,yend=camera_out.height(); y<yend; y++)
144 for(
int x=0,xend=camera_out.width(); x<xend; x++)
148 world_pose=camera_out.UnProject(
Point2d(x,y));
149 im_pose = camera_in.Project(world_pose);
151 if(im_pose.x<0 || im_pose.y<0 ||
152 im_pose.x>=w_in||im_pose.y>=h_in)
163 remapCoef[i*4+0] = 0;
164 remapCoef[i*4+1] = 0;
165 remapCoef[i*4+2] = 0;
166 remapCoef[i*4+3] = 0;
171 remapX[i] = im_pose.x;
172 remapY[i] = im_pose.y;
173 remapFast[i] = (int)im_pose.x+w_in*(
int)im_pose.y;
178 float xx = remapX[i];
179 float yy = remapY[i];
181 if( xx < 0.0 )
continue;
190 remapIdx[i*4+0] = yyi*w_in + xxi;
191 remapIdx[i*4+1] = yyi*w_in + xxi + 1;
192 remapIdx[i*4+2] = (yyi+1)*w_in + xxi;
193 remapIdx[i*4+3] = (yyi+1)*w_in + xxi + 1;
195 remapCoef[i*4+0] = 1-xx-yy+xxyy;
196 remapCoef[i*4+1] = xx-xxyy;
197 remapCoef[i*4+2] = yy-xxyy;
198 remapCoef[i*4+3] = xxyy;
206 inline bool UndistorterImpl::undistortFast(
const GImage& image,
GImage& result)
214 int width_out=camera_out.width();
215 int height_out=camera_out.height();
217 if (image.rows != camera_in.height() || image.cols != camera_in.width())
219 cerr<<(
"input image size differs from expected input size! Not undistorting.\n");
224 int wh=width_out*height_out;
225 int c=image.channels();
227 result=
GImage(height_out,width_out,image.type());
231 Byte<1>* p_out=(Byte<1>*)result.data;
232 Byte<1>* p_img=(Byte<1>*)image.data;
234 for(
int i=0;i<wh;i++)
238 p_out[i] = p_img[remapFast[i]];
244 Byte<3>* p_out=(Byte<3>*)result.data;
245 Byte<3>* p_img=(Byte<3>*)image.data;
247 for(
int i=0;i<wh;i++)
249 p_out[i] = p_img[remapFast[i]];
254 int eleSize=image.elemSize();
255 Byte<1>* p_out=(Byte<1>*)result.data;
256 Byte<1>* p_img=(Byte<1>*)image.data;
258 for(
int i=0;i<wh;i++)
262 memcpy(p_out+eleSize*i,p_img+eleSize*remapFast[i],eleSize);
271 inline bool UndistorterImpl::undistort(
const GImage& image,
GImage& result)
279 int width_out=camera_out.width();
280 int height_out=camera_out.height();
282 if (image.rows != camera_in.height() || image.cols != camera_in.width())
284 cerr<<(
"input image size differs from expected input size! Not undistorting.\n");
289 int wh=width_out*height_out;
290 int c=image.channels();
292 result=
GImage(height_out,width_out,image.type());
296 uchar* p_out=(uchar*)result.data;
297 uchar* p_img=(uchar*)image.data;
299 int *pIdx = remapIdx;
300 float *pCoef = remapCoef;
303 for(
int i = 0; i<wh; i++)
306 float xx = remapX[i];
312 p_out[i] = p_img[pIdx[0]]*pCoef[0] +
313 p_img[pIdx[1]]*pCoef[1] +
314 p_img[pIdx[2]]*pCoef[2] +
315 p_img[pIdx[3]]*pCoef[3];
324 uchar* p_out = (uchar*)result.data;
325 uchar* p_img = (uchar*)image.data;
327 int *pIdx = remapIdx;
328 float *pCoef = remapCoef;
331 for(
int i=0; i<wh; i++)
335 for(
int j=0; j<c; j++)
336 p_out[i*3+j] = p_img[pIdx[0]*c+j]*pCoef[0] +
337 p_img[pIdx[1]*c+j]*pCoef[1] +
338 p_img[pIdx[2]*c+j]*pCoef[2] +
339 p_img[pIdx[3]*c+j]*pCoef[3];
355 inline bool Undistorter::undistort(
const GImage& image,
GImage& result)
357 return impl->undistort(image,result);
360 inline bool Undistorter::undistortFast(
const GImage& image,
GImage& result)
362 return impl->undistortFast(image,result);
365 inline Camera Undistorter::cameraIn(){
return impl->camera_in;}
366 inline Camera Undistorter::cameraOut(){
return impl->camera_out;}
368 inline bool Undistorter::prepareReMap()
370 return impl->prepareReMap();
373 inline bool Undistorter::valid()
bool valid
Is true if the undistorter object is valid (has been initialized with a valid configuration) ...
Definition: Undistorter.h:87
Definition: Undistorter.h:90
The GImage class is a tiny implementation of image for removing dependency of opencv.
Definition: GImage.h:160
Definition: Undistorter.h:41
Definition: Undistorter.h:45