Skip to content

Commit 5409d5a

Browse files
committed
Merge pull request #474 from mshabunin:normals_linemod_fix
2 parents 070e89e + 0b348ea commit 5409d5a

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

modules/rgbd/include/opencv2/rgbd.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ namespace rgbd
127127
/** Constructor
128128
* @param rows the number of rows of the depth image normals will be computed on
129129
* @param cols the number of cols of the depth image normals will be computed on
130-
* @param depth the depth of the normals (only CV_32F or CV_64F for FALS and SRI, CV_16U for LINEMOD)
130+
* @param depth the depth of the normals (only CV_32F or CV_64F)
131131
* @param K the calibration matrix to use
132132
* @param window_size the window size to compute the normals: can only be 1,3,5 or 7
133133
* @param method one of the methods to use: RGBD_NORMALS_METHOD_SRI, RGBD_NORMALS_METHOD_FALS
@@ -234,7 +234,7 @@ namespace rgbd
234234
}
235235

236236
/** Constructor
237-
* @param depth the depth of the normals (only CV_32F or CV_64F for FALS and SRI, CV_16U for LINEMOD)
237+
* @param depth the depth of the normals (only CV_32F or CV_64F)
238238
* @param window_size the window size to compute the normals: can only be 1,3,5 or 7
239239
* @param method one of the methods to use: RGBD_NORMALS_METHOD_SRI, RGBD_NORMALS_METHOD_FALS
240240
*/

modules/rgbd/src/normal.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
namespace cv
3939
{
4040
namespace rgbd
41-
{
41+
{
4242
/** Just compute the norm of a vector
4343
* @param vec a vector of size 3 and any type T
4444
* @return
@@ -133,6 +133,7 @@ namespace rgbd
133133
res = -normal_in / norm_vec(normal_in);
134134
else
135135
res = normal_in / norm_vec(normal_in);
136+
136137
normal_out[0] = res[0];
137138
normal_out[1] = res[1];
138139
normal_out[2] = res[2];
@@ -434,6 +435,7 @@ multiply_by_K_inv(const Matx<T, 3, 3> & K_inv, U a, U b, U c, Vec<T, 3> &res)
434435
Vec3T X1_minus_X, X2_minus_X;
435436

436437
ContainerDepth difference_threshold = 50;
438+
normals.setTo(std::numeric_limits<DepthDepth>::quiet_NaN());
437439
for (int y = r; y < rows_ - r - 1; ++y)
438440
{
439441
const DepthDepth * p_line = reinterpret_cast<const DepthDepth*>(depth.ptr(y, r));
@@ -525,7 +527,7 @@ multiply_by_K_inv(const Matx<T, 3, 3> & K_inv, U a, U b, U c, Vec<T, 3> &res)
525527

526528
// Get the mapping function for SRI
527529
float min_theta = (float)std::asin(sin_theta(0, 0)), max_theta = (float)std::asin(sin_theta(0, cols_ - 1));
528-
float min_phi = (float)std::asin(sin_phi(0, cols_/2-1)), max_phi = (float) std::asin(sin_phi(rows_ - 1, cols_/2-1));
530+
float min_phi = (float)std::asin(sin_phi(0, cols_/2-1)), max_phi = (float)std::asin(sin_phi(rows_ - 1, cols_/2-1));
529531

530532
std::vector<Point3f> points3d(cols_ * rows_);
531533
R_hat_.create(rows_, cols_);

modules/rgbd/test/test_normal.cpp

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include "test_precomp.hpp"
3737
#include <opencv2/rgbd.hpp>
38+
#include <opencv2/calib3d.hpp>
3839

3940
namespace cv
4041
{
@@ -77,7 +78,7 @@ void TickMeter::stop()
7778
return;
7879

7980
++counter;
80-
81+
8182
sumTime += ( time - startTime );
8283
startTime = 0;
8384
}
@@ -129,6 +130,35 @@ float cy = H / 2.f + 0.5f;
129130
Mat K = (Mat_<double>(3, 3) << focal_length, 0, cx, 0, focal_length, cy, 0, 0, 1);
130131
Mat Kinv = K.inv();
131132

133+
void points3dToDepth16U(const Mat_<Vec3f>& points3d, Mat& depthMap);
134+
135+
void points3dToDepth16U(const Mat_<Vec3f>& points3d, Mat& depthMap)
136+
{
137+
std::vector<Point3f> points3dvec;
138+
for(int i = 0; i < H; i++)
139+
for(int j = 0; j < W; j++)
140+
points3dvec.push_back(Point3f(points3d(i,j)[0], points3d(i,j)[1], points3d(i,j)[2]));
141+
142+
std::vector<Point2f> img_points;
143+
depthMap = Mat::zeros(H, W, CV_32F);
144+
Vec3f R(0.0,0.0,0.0);
145+
Vec3f T(0.0,0.0,0.0);
146+
cv::projectPoints(points3dvec, R, T, K, Mat(), img_points);
147+
148+
int index = 0;
149+
for(int i = 0; i < H; i++)
150+
{
151+
152+
for(int j = 0; j < W; j++)
153+
{
154+
float value = (points3d.at<Vec3f>(i, j))[2]; // value is the z
155+
depthMap.at<float>(cvRound(img_points[index].y), cvRound(img_points[index].x)) = value;
156+
index++;
157+
}
158+
}
159+
depthMap.convertTo(depthMap, CV_16U, 1000);
160+
}
161+
132162
static RNG rng;
133163
struct Plane
134164
{
@@ -224,8 +254,8 @@ class CV_RgbdNormalsTest: public cvtest::BaseTest
224254
// inner vector: whether it's 1 plane or 3 planes
225255
// outer vector: float or double
226256
std::vector<std::vector<float> > errors(2);
227-
errors[0].resize(2);
228-
errors[1].resize(2);
257+
errors[0].resize(4);
258+
errors[1].resize(4);
229259
switch (i)
230260
{
231261
case 0:
@@ -241,8 +271,13 @@ class CV_RgbdNormalsTest: public cvtest::BaseTest
241271
std::cout << std::endl << "*** LINEMOD" << std::endl;
242272
errors[0][0] = 0.04f;
243273
errors[0][1] = 0.07f;
274+
errors[0][2] = 0.04f; // depth 16U 1 plane
275+
errors[0][3] = 0.07f; // depth 16U 3 planes
276+
244277
errors[1][0] = 0.05f;
245278
errors[1][1] = 0.08f;
279+
errors[1][2] = 0.05f; // depth 16U 1 plane
280+
errors[1][3] = 0.08f; // depth 16U 3 planes
246281
break;
247282
case 2:
248283
method = RgbdNormals::RGBD_NORMALS_METHOD_SRI;
@@ -252,9 +287,9 @@ class CV_RgbdNormalsTest: public cvtest::BaseTest
252287
errors[1][0] = 0.02f;
253288
errors[1][1] = 0.04f;
254289
break;
255-
default:
256-
method = (RgbdNormals::RGBD_NORMALS_METHOD)-1;
257-
CV_Error(0, "");
290+
default:
291+
method = (RgbdNormals::RGBD_NORMALS_METHOD)-1;
292+
CV_Error(0, "");
258293
}
259294

260295
for (unsigned char j = 0; j < 2; ++j)
@@ -271,7 +306,7 @@ class CV_RgbdNormalsTest: public cvtest::BaseTest
271306
std::vector<Plane> plane_params;
272307
Mat points3d, ground_normals;
273308
// 1 plane, continuous scene, very low error..
274-
std::cout << "1 plane" << std::endl;
309+
std::cout << "1 plane - input 3d points" << std::endl;
275310
float err_mean = 0;
276311
for (int ii = 0; ii < 5; ++ii)
277312
{
@@ -291,6 +326,34 @@ class CV_RgbdNormalsTest: public cvtest::BaseTest
291326
}
292327
std::cout << "mean diff: " << (err_mean / 5) << std::endl;
293328
EXPECT_LE(err_mean/5, errors[j][1])<< "mean diff: " << (err_mean/5) << " thresh: " << errors[j][1] << std::endl;
329+
330+
if(method == RgbdNormals::RGBD_NORMALS_METHOD_LINEMOD)
331+
{
332+
// depth 16U test
333+
std::cout << "** depth 16U - 1 plane" << std::endl;
334+
err_mean = 0;
335+
for (int ii = 0; ii < 5; ++ii)
336+
{
337+
gen_points_3d(plane_params, plane_mask, points3d, ground_normals, 1);
338+
Mat depthMap;
339+
points3dToDepth16U(points3d, depthMap);
340+
err_mean += testit(depthMap, ground_normals, normals_computer);
341+
}
342+
std::cout << "mean diff: " << (err_mean / 5) << std::endl;
343+
EXPECT_LE(err_mean/5, errors[j][2])<< " thresh: " << errors[j][2] << std::endl;
344+
345+
std::cout << "** depth 16U - 3 plane" << std::endl;
346+
err_mean = 0;
347+
for (int ii = 0; ii < 5; ++ii)
348+
{
349+
gen_points_3d(plane_params, plane_mask, points3d, ground_normals, 3);
350+
Mat depthMap;
351+
points3dToDepth16U(points3d, depthMap);
352+
err_mean += testit(depthMap, ground_normals, normals_computer);
353+
}
354+
std::cout << "mean diff: " << (err_mean / 5) << std::endl;
355+
EXPECT_LE(err_mean/5, errors[j][3])<< "mean diff: " << (err_mean/5) << " thresh: " << errors[j][3] << std::endl;
356+
}
294357
}
295358
}
296359

@@ -309,7 +372,7 @@ class CV_RgbdNormalsTest: public cvtest::BaseTest
309372
TickMeter tm;
310373
tm.start();
311374
Mat in_normals;
312-
if (normals_computer.getMethod() == RgbdNormals::RGBD_NORMALS_METHOD_LINEMOD)
375+
if (normals_computer.getMethod() == RgbdNormals::RGBD_NORMALS_METHOD_LINEMOD && points3d.channels() == 3)
313376
{
314377
std::vector<Mat> channels;
315378
split(points3d, channels);
@@ -450,7 +513,7 @@ class CV_RgbdPlaneTest: public cvtest::BaseTest
450513

451514
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
452515

453-
TEST(DISABLED_Rgbd_Normals, compute)
516+
TEST(Rgbd_Normals, compute)
454517
{
455518
cv::rgbd::CV_RgbdNormalsTest test;
456519
test.safe_run();

0 commit comments

Comments
 (0)