test_utils.cpp 1.57 KB
Newer Older
1 2
#include "test_precomp.hpp"

Vincent Rabaud's avatar
Vincent Rabaud committed
3 4
#include <opencv2/calib3d.hpp>

5 6 7 8
namespace cv
{
namespace rgbd
{
Vincent Rabaud's avatar
Vincent Rabaud committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

class CV_RgbdDepthTo3dTest: public cvtest::BaseTest
{
public:
  CV_RgbdDepthTo3dTest()
  {
  }
  ~CV_RgbdDepthTo3dTest()
  {
  }
protected:
  void
  run(int)
  {
    try
    {
      // K from a VGA Kinect
26
      Mat K = (Mat_<float>(3, 3) << 525., 0., 319.5, 0., 525., 239.5, 0., 0., 1.);
Vincent Rabaud's avatar
Vincent Rabaud committed
27 28

      // Create a random depth image
29 30 31
      RNG rng;
      Mat_<float> depth(480, 640);
      rng.fill(depth, RNG::UNIFORM, 0, 100);
Vincent Rabaud's avatar
Vincent Rabaud committed
32 33 34

      // Create some 3d points on the plane
      int rows = depth.rows, cols = depth.cols;
35 36
      Mat_<Vec3f> points3d;
      depthTo3d(depth, K, points3d);
Vincent Rabaud's avatar
Vincent Rabaud committed
37 38

      // Make sure the points belong to the plane
39 40 41 42 43 44
      Mat points = points3d.reshape(1, rows*cols);
      Mat image_points;
      Mat rvec;
      Rodrigues(Mat::eye(3,3,CV_32F),rvec);
      Mat tvec = (Mat_<float>(1,3) << 0, 0, 0);
      projectPoints(points, rvec, tvec, K, Mat(), image_points);
Vincent Rabaud's avatar
Vincent Rabaud committed
45 46 47 48 49
      image_points = image_points.reshape(2, rows);

      float avg_diff = 0;
      for (int y = 0; y < rows; ++y)
        for (int x = 0; x < cols; ++x)
50
          avg_diff += (float)norm(image_points.at<Vec2f>(y,x) - Vec2f((float)x,(float)y));
Vincent Rabaud's avatar
Vincent Rabaud committed
51 52 53 54 55 56 57 58 59 60 61

      // Verify the function works
      ASSERT_LE(avg_diff/rows/cols, 1e-4) << "Average error for ground truth is: " << (avg_diff / rows / cols);
    } catch (...)
    {
      ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
    }
    ts->set_failed_test_info(cvtest::TS::OK);
  }
};

62 63 64
}
}

Vincent Rabaud's avatar
Vincent Rabaud committed
65 66
TEST(Rgbd_DepthTo3d, compute)
{
67
  cv::rgbd::CV_RgbdDepthTo3dTest test;
Vincent Rabaud's avatar
Vincent Rabaud committed
68 69
  test.safe_run();
}