test_plane.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2015, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. const string STRUCTURED_LIGHT_DIR = "structured_light";
  44. const string FOLDER_DATA = "data";
  45. /****************************************************************************************\
  46. * Plane test *
  47. \****************************************************************************************/
  48. class CV_PlaneTest : public cvtest::BaseTest
  49. {
  50. public:
  51. CV_PlaneTest();
  52. ~CV_PlaneTest();
  53. //////////////////////////////////////////////////////////////////////////////////////////////////
  54. // From rgbd module: since I needed the distance method of plane class, I copied the class from rgb module
  55. // it will be made a pull request to make Plane class public
  56. /** Structure defining a plane. The notations are from the second paper */
  57. class PlaneBase
  58. {
  59. public:
  60. PlaneBase(const Vec3f & m, const Vec3f &n_in, int index) :
  61. index_(index),
  62. n_(n_in),
  63. m_sum_(Vec3f(0, 0, 0)),
  64. m_(m),
  65. Q_(Matx33f::zeros()),
  66. mse_(0),
  67. K_(0)
  68. {
  69. UpdateD();
  70. }
  71. virtual ~PlaneBase()
  72. {
  73. }
  74. /** Compute the distance to the plane. This will be implemented by the children to take into account different
  75. * sensor models
  76. * @param p_j
  77. * @return
  78. */
  79. virtual
  80. float
  81. distance(const Vec3f& p_j) const = 0;
  82. /** The d coefficient in the plane equation ax+by+cz+d = 0
  83. * @return
  84. */
  85. inline float d() const
  86. {
  87. return d_;
  88. }
  89. /** The normal to the plane
  90. * @return the normal to the plane
  91. */
  92. const Vec3f &
  93. n() const
  94. {
  95. return n_;
  96. }
  97. /** Update the different coefficients of the plane, based on the new statistics
  98. */
  99. void UpdateParameters()
  100. {
  101. if( empty() )
  102. return;
  103. m_ = m_sum_ / K_;
  104. // Compute C
  105. Matx33f C = Q_ - m_sum_ * m_.t();
  106. // Compute n
  107. SVD svd(C);
  108. n_ = Vec3f(svd.vt.at<float>(2, 0), svd.vt.at<float>(2, 1), svd.vt.at<float>(2, 2));
  109. mse_ = svd.w.at<float>(2) / K_;
  110. UpdateD();
  111. }
  112. /** Update the different sum of point and sum of point*point.t()
  113. */
  114. void UpdateStatistics(const Vec3f & point, const Matx33f & Q_local)
  115. {
  116. m_sum_ += point;
  117. Q_ += Q_local;
  118. ++K_;
  119. }
  120. inline size_t empty() const
  121. {
  122. return K_ == 0;
  123. }
  124. inline int K() const
  125. {
  126. return K_;
  127. }
  128. /** The index of the plane */
  129. int index_;
  130. protected:
  131. /** The 4th coefficient in the plane equation ax+by+cz+d = 0 */
  132. float d_;
  133. /** Normal of the plane */
  134. Vec3f n_;
  135. private:
  136. inline void UpdateD()
  137. {
  138. // Hessian form (d = nc . p_plane (centroid here) + p)
  139. //d = -1 * n.dot (xyz_centroid);//d =-axP+byP+czP
  140. d_ = -m_.dot(n_);
  141. }
  142. /** The sum of the points */
  143. Vec3f m_sum_;
  144. /** The mean of the points */
  145. Vec3f m_;
  146. /** The sum of pi * pi^\top */
  147. Matx33f Q_;
  148. /** The different matrices we need to update */
  149. Matx33f C_;
  150. float mse_;
  151. /** the number of points that form the plane */
  152. int K_;
  153. };
  154. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. /** Basic planar child, with no sensor error model
  156. */
  157. class Plane : public PlaneBase
  158. {
  159. public:
  160. Plane(const Vec3f & m, const Vec3f &n_in, int index) :
  161. PlaneBase(m, n_in, index)
  162. {
  163. }
  164. /** The computed distance is perfect in that case
  165. * @param p_j the point to compute its distance to
  166. * @return
  167. */
  168. float distance(const Vec3f& p_j) const
  169. {
  170. return std::abs(float(p_j.dot(n_) + d_));
  171. }
  172. };
  173. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  174. protected:
  175. void run( int );
  176. };
  177. CV_PlaneTest::CV_PlaneTest(){}
  178. CV_PlaneTest::~CV_PlaneTest(){}
  179. void CV_PlaneTest::run( int )
  180. {
  181. string folder = cvtest::TS::ptr()->get_data_path() + "/" + STRUCTURED_LIGHT_DIR + "/" + FOLDER_DATA + "/";
  182. structured_light::GrayCodePattern::Params params;
  183. params.width = 1280;
  184. params.height = 800;
  185. // Set up GraycodePattern with params
  186. Ptr<structured_light::GrayCodePattern> graycode = structured_light::GrayCodePattern::create( params );
  187. size_t numberOfPatternImages = graycode->getNumberOfPatternImages();
  188. FileStorage fs( folder + "calibrationParameters.yml", FileStorage::READ );
  189. if( !fs.isOpened() )
  190. {
  191. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  192. }
  193. FileStorage fs2( folder + "gt_plane.yml", FileStorage::READ );
  194. if( !fs.isOpened() )
  195. {
  196. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  197. }
  198. // Loading ground truth plane parameters
  199. Vec4f plane_coefficients;
  200. Vec3f m;
  201. fs2["plane_coefficients"] >> plane_coefficients;
  202. fs2["m"] >> m;
  203. // Loading calibration parameters
  204. Mat cam1intrinsics, cam1distCoeffs, cam2intrinsics, cam2distCoeffs, R, T;
  205. fs["cam1_intrinsics"] >> cam1intrinsics;
  206. fs["cam2_intrinsics"] >> cam2intrinsics;
  207. fs["cam1_distorsion"] >> cam1distCoeffs;
  208. fs["cam2_distorsion"] >> cam2distCoeffs;
  209. fs["R"] >> R;
  210. fs["T"] >> T;
  211. // Loading white and black images
  212. vector<Mat> blackImages;
  213. vector<Mat> whiteImages;
  214. blackImages.resize( 2 );
  215. whiteImages.resize( 2 );
  216. whiteImages[0] = imread( folder + "pattern_cam1_im43.jpg", 0 );
  217. whiteImages[1] = imread( folder + "pattern_cam2_im43.jpg", 0 );
  218. blackImages[0] = imread( folder + "pattern_cam1_im44.jpg", 0 );
  219. blackImages[1] = imread( folder + "pattern_cam2_im44.jpg", 0 );
  220. Size imagesSize = whiteImages[0].size();
  221. if( ( !cam1intrinsics.data ) || ( !cam2intrinsics.data ) || ( !cam1distCoeffs.data ) || ( !cam2distCoeffs.data ) || ( !R.data )
  222. || ( !T.data ) || ( !whiteImages[0].data ) || ( !whiteImages[1].data ) || ( !blackImages[0].data )
  223. || ( !blackImages[1].data ) )
  224. {
  225. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  226. }
  227. // Computing stereo rectify parameters
  228. Mat R1, R2, P1, P2, Q;
  229. Rect validRoi[2];
  230. stereoRectify( cam1intrinsics, cam1distCoeffs, cam2intrinsics, cam2distCoeffs, imagesSize, R, T, R1, R2, P1, P2, Q, 0,
  231. -1, imagesSize, &validRoi[0], &validRoi[1] );
  232. Mat map1x, map1y, map2x, map2y;
  233. initUndistortRectifyMap( cam1intrinsics, cam1distCoeffs, R1, P1, imagesSize, CV_32FC1, map1x, map1y );
  234. initUndistortRectifyMap( cam2intrinsics, cam2distCoeffs, R2, P2, imagesSize, CV_32FC1, map2x, map2y );
  235. vector<vector<Mat> > captured_pattern;
  236. captured_pattern.resize( 2 );
  237. captured_pattern[0].resize( numberOfPatternImages );
  238. captured_pattern[1].resize( numberOfPatternImages );
  239. // Loading and rectifying pattern images
  240. for( size_t i = 0; i < numberOfPatternImages; i++ )
  241. {
  242. std::ostringstream name1;
  243. name1 << "pattern_cam1_im" << i + 1 << ".jpg";
  244. captured_pattern[0][i] = imread( folder + name1.str(), 0 );
  245. std::ostringstream name2;
  246. name2 << "pattern_cam2_im" << i + 1 << ".jpg";
  247. captured_pattern[1][i] = imread( folder + name2.str(), 0 );
  248. if( (!captured_pattern[0][i].data) || (!captured_pattern[1][i].data) )
  249. {
  250. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  251. }
  252. remap( captured_pattern[0][i], captured_pattern[0][i], map2x, map2y, INTER_NEAREST, BORDER_CONSTANT, Scalar() );
  253. remap( captured_pattern[1][i], captured_pattern[1][i], map1x, map1y, INTER_NEAREST, BORDER_CONSTANT, Scalar() );
  254. }
  255. // Rectifying white and black images
  256. remap( whiteImages[0], whiteImages[0], map2x, map2y, INTER_NEAREST, BORDER_CONSTANT, Scalar() );
  257. remap( whiteImages[1], whiteImages[1], map1x, map1y, INTER_NEAREST, BORDER_CONSTANT, Scalar() );
  258. remap( blackImages[0], blackImages[0], map2x, map2y, INTER_NEAREST, BORDER_CONSTANT, Scalar() );
  259. remap( blackImages[1], blackImages[1], map1x, map1y, INTER_NEAREST, BORDER_CONSTANT, Scalar() );
  260. // Setting up threshold parameters to reconstruct only the plane in foreground
  261. graycode->setBlackThreshold( 55 );
  262. graycode->setWhiteThreshold( 10 );
  263. // Computing the disparity map
  264. Mat disparityMap;
  265. bool decoded = graycode->decode( captured_pattern, disparityMap, blackImages, whiteImages,
  266. structured_light::DECODE_3D_UNDERWORLD );
  267. EXPECT_TRUE( decoded );
  268. // Computing the point cloud
  269. Mat pointcloud;
  270. disparityMap.convertTo( disparityMap, CV_32FC1 );
  271. reprojectImageTo3D( disparityMap, pointcloud, Q, true, -1 );
  272. // from mm (unit of calibration) to m
  273. pointcloud = pointcloud / 1000;
  274. // Setting up plane with ground truth plane values
  275. Vec3f normal( plane_coefficients.val[0], plane_coefficients.val[1], plane_coefficients.val[2] );
  276. Ptr<PlaneBase> plane = Ptr<PlaneBase>( new Plane( m, normal, 0 ) );
  277. // Computing the distance of every point of the pointcloud from ground truth plane
  278. float sum_d = 0;
  279. int cont = 0;
  280. for( int i = 0; i < disparityMap.rows; i++ )
  281. {
  282. for( int j = 0; j < disparityMap.cols; j++ )
  283. {
  284. float value = disparityMap.at<float>( i, j );
  285. if( value != 0 )
  286. {
  287. Vec3f point = pointcloud.at<Vec3f>( i, j );
  288. sum_d += plane->distance( point );
  289. cont++;
  290. }
  291. }
  292. }
  293. sum_d /= cont;
  294. // test pass if the mean of points distance from ground truth plane is lower than 3 mm
  295. EXPECT_LE( sum_d, 0.003 );
  296. }
  297. /****************************************************************************************\
  298. * Test registration *
  299. \****************************************************************************************/
  300. TEST( GrayCodePattern, plane_reconstruction )
  301. {
  302. CV_PlaneTest test;
  303. test.safe_run();
  304. }
  305. }} // namespace