@@ -141,6 +141,108 @@ TEST_F(fisheyeTest, undistortImage)
141
141
}
142
142
}
143
143
144
+ TEST_F (fisheyeTest, undistortAndDistortImage)
145
+ {
146
+ cv::Matx33d K_src = this ->K ;
147
+ cv::Mat D_src = cv::Mat (this ->D );
148
+ std::string file = combine (datasets_repository_path, " /calib-3_stereo_from_JY/left/stereo_pair_014.jpg" );
149
+ cv::Matx33d K_dst = K_src;
150
+ cv::Mat image = cv::imread (file), image_projected;
151
+ cv::Vec4d D_dst_vec (-1.0 , 0.0 , 0.0 , 0.0 );
152
+ cv::Mat D_dst = cv::Mat (D_dst_vec);
153
+
154
+ int imageWidth = (int )this ->imageSize .width ;
155
+ int imageHeight = (int )this ->imageSize .height ;
156
+
157
+ cv::Mat imagePoints (imageHeight, imageWidth, CV_32FC2), undPoints, distPoints;
158
+ cv::Vec2f* pts = imagePoints.ptr <cv::Vec2f>();
159
+
160
+ for (int y = 0 , k = 0 ; y < imageHeight; ++y)
161
+ {
162
+ for (int x = 0 ; x < imageWidth; ++x)
163
+ {
164
+ cv::Vec2f point ((float )x, (float )y);
165
+ pts[k++] = point;
166
+ }
167
+ }
168
+
169
+ cv::fisheye::undistortPoints (imagePoints, undPoints, K_dst, D_dst);
170
+ cv::fisheye::distortPoints (undPoints, distPoints, K_src, D_src);
171
+ cv::remap (image, image_projected, distPoints, cv::noArray (), cv::INTER_LINEAR);
172
+
173
+ float dx, dy, r_sq;
174
+ float R_MAX = 250 ;
175
+ float imageCenterX = (float )imageWidth / 2 ;
176
+ float imageCenterY = (float )imageHeight / 2 ;
177
+
178
+ cv::Mat undPointsGt (imageHeight, imageWidth, CV_32FC2);
179
+ cv::Mat imageGt (imageHeight, imageWidth, CV_8UC3);
180
+
181
+ for (int y = 0 , k = 0 ; y < imageHeight; ++y)
182
+ {
183
+ for (int x = 0 ; x < imageWidth; ++x)
184
+ {
185
+ dx = x - imageCenterX;
186
+ dy = y - imageCenterY;
187
+ r_sq = dy * dy + dx * dx;
188
+
189
+ Vec2f & und_vec = undPoints.at <Vec2f>(y,x);
190
+ Vec3b & pixel = image_projected.at <Vec3b>(y,x);
191
+
192
+ Vec2f & undist_vec_gt = undPointsGt.at <Vec2f>(y,x);
193
+ Vec3b & pixel_gt = imageGt.at <Vec3b>(y,x);
194
+
195
+ if (r_sq > R_MAX * R_MAX)
196
+ {
197
+
198
+ undist_vec_gt[0 ] = -1e6 ;
199
+ undist_vec_gt[1 ] = -1e6 ;
200
+
201
+ pixel_gt[0 ] = 0 ;
202
+ pixel_gt[1 ] = 0 ;
203
+ pixel_gt[2 ] = 0 ;
204
+ }
205
+ else
206
+ {
207
+ undist_vec_gt[0 ] = und_vec[0 ];
208
+ undist_vec_gt[1 ] = und_vec[1 ];
209
+
210
+ pixel_gt[0 ] = pixel[0 ];
211
+ pixel_gt[1 ] = pixel[1 ];
212
+ pixel_gt[2 ] = pixel[2 ];
213
+ }
214
+
215
+ k++;
216
+ }
217
+ }
218
+
219
+ EXPECT_MAT_NEAR (undPoints, undPointsGt, 1e-10 );
220
+ EXPECT_MAT_NEAR (image_projected, imageGt, 1e-10 );
221
+
222
+ Vec2f dist_point_1 = distPoints.at <Vec2f>(400 , 640 );
223
+ Vec2f dist_point_1_gt (640 .044f , 400 .041f );
224
+
225
+ Vec2f dist_point_2 = distPoints.at <Vec2f>(400 , 440 );
226
+ Vec2f dist_point_2_gt (409 .731f , 403 .029f );
227
+
228
+ Vec2f dist_point_3 = distPoints.at <Vec2f>(200 , 640 );
229
+ Vec2f dist_point_3_gt (643 .341f , 168 .896f );
230
+
231
+ Vec2f dist_point_4 = distPoints.at <Vec2f>(300 , 480 );
232
+ Vec2f dist_point_4_gt (463 .402f , 290 .317f );
233
+
234
+ Vec2f dist_point_5 = distPoints.at <Vec2f>(550 , 750 );
235
+ Vec2f dist_point_5_gt (797 .51f , 611 .637f );
236
+
237
+ EXPECT_MAT_NEAR (dist_point_1, dist_point_1_gt, 1e-2 );
238
+ EXPECT_MAT_NEAR (dist_point_2, dist_point_2_gt, 1e-2 );
239
+ EXPECT_MAT_NEAR (dist_point_3, dist_point_3_gt, 1e-2 );
240
+ EXPECT_MAT_NEAR (dist_point_4, dist_point_4_gt, 1e-2 );
241
+ EXPECT_MAT_NEAR (dist_point_5, dist_point_5_gt, 1e-2 );
242
+
243
+ CV_Assert (cv::imwrite (combine (datasets_repository_path, " new_distortion.png" ), image_projected));
244
+ }
245
+
144
246
TEST_F (fisheyeTest, jacobians)
145
247
{
146
248
int n = 10 ;
0 commit comments