test_reg.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, Alfonso Sanchez-Beato, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #include "test_precomp.hpp"
  44. namespace opencv_test { namespace {
  45. #define REG_DEBUG_OUTPUT 0
  46. class RegTest : public testing::Test
  47. {
  48. public:
  49. void loadImage(int dstDataType = CV_32FC3);
  50. void testShift();
  51. void testEuclidean();
  52. void testSimilarity();
  53. void testAffine();
  54. void testProjective();
  55. private:
  56. Mat img1;
  57. };
  58. void RegTest::testShift()
  59. {
  60. Mat img2;
  61. // Warp original image
  62. Vec<double, 2> shift(5., 5.);
  63. MapShift mapTest(shift);
  64. mapTest.warp(img1, img2);
  65. // Register
  66. Ptr<Mapper> mapper = makePtr<MapperGradShift>();
  67. MapperPyramid mappPyr(mapper);
  68. Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
  69. // Print result
  70. Ptr<MapShift> mapShift = MapTypeCaster::toShift(mapPtr);
  71. #if REG_DEBUG_OUTPUT
  72. cout << endl << "--- Testing shift mapper ---" << endl;
  73. cout << Mat(shift) << endl;
  74. cout << Mat(mapShift->getShift()) << endl;
  75. #endif
  76. // Check accuracy
  77. Ptr<Map> mapInv(mapShift->inverseMap());
  78. mapTest.compose(mapInv);
  79. double shNorm = cv::norm(mapTest.getShift());
  80. EXPECT_LE(shNorm, 0.1);
  81. }
  82. void RegTest::testEuclidean()
  83. {
  84. Mat img2;
  85. // Warp original image
  86. double theta = 3*CV_PI/180;
  87. double cosT = cos(theta);
  88. double sinT = sin(theta);
  89. Matx<double, 2, 2> linTr(cosT, -sinT, sinT, cosT);
  90. Vec<double, 2> shift(5., 5.);
  91. MapAffine mapTest(linTr, shift);
  92. mapTest.warp(img1, img2);
  93. // Register
  94. Ptr<Mapper> mapper = makePtr<MapperGradEuclid>();
  95. MapperPyramid mappPyr(mapper);
  96. Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
  97. // Print result
  98. Ptr<MapAffine> mapAff = MapTypeCaster::toAffine(mapPtr);
  99. #if REG_DEBUG_OUTPUT
  100. cout << endl << "--- Testing Euclidean mapper ---" << endl;
  101. cout << Mat(linTr) << endl;
  102. cout << Mat(shift) << endl;
  103. cout << Mat(mapAff->getLinTr()) << endl;
  104. cout << Mat(mapAff->getShift()) << endl;
  105. #endif
  106. // Check accuracy
  107. Ptr<Map> mapInv(mapAff->inverseMap());
  108. mapTest.compose(mapInv);
  109. double shNorm = cv::norm(mapTest.getShift());
  110. EXPECT_LE(shNorm, 0.1);
  111. double linTrNorm = cv::norm(mapTest.getLinTr());
  112. EXPECT_LE(linTrNorm, sqrt(2.) + 0.01);
  113. EXPECT_GE(linTrNorm, sqrt(2.) - 0.01);
  114. }
  115. void RegTest::testSimilarity()
  116. {
  117. Mat img2;
  118. // Warp original image
  119. double theta = 3*CV_PI/180;
  120. double scale = 0.95;
  121. double a = scale*cos(theta);
  122. double b = scale*sin(theta);
  123. Matx<double, 2, 2> linTr(a, -b, b, a);
  124. Vec<double, 2> shift(5., 5.);
  125. MapAffine mapTest(linTr, shift);
  126. mapTest.warp(img1, img2);
  127. // Register
  128. Ptr<Mapper> mapper = makePtr<MapperGradSimilar>();
  129. MapperPyramid mappPyr(mapper);
  130. Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
  131. // Print result
  132. Ptr<MapAffine> mapAff = MapTypeCaster::toAffine(mapPtr);
  133. #if REG_DEBUG_OUTPUT
  134. cout << endl << "--- Testing similarity mapper ---" << endl;
  135. cout << Mat(linTr) << endl;
  136. cout << Mat(shift) << endl;
  137. cout << Mat(mapAff->getLinTr()) << endl;
  138. cout << Mat(mapAff->getShift()) << endl;
  139. #endif
  140. // Check accuracy
  141. Ptr<Map> mapInv(mapAff->inverseMap());
  142. mapTest.compose(mapInv);
  143. double shNorm = cv::norm(mapTest.getShift());
  144. EXPECT_LE(shNorm, 0.1);
  145. double linTrNorm = cv::norm(mapTest.getLinTr());
  146. EXPECT_LE(linTrNorm, sqrt(2.) + 0.01);
  147. EXPECT_GE(linTrNorm, sqrt(2.) - 0.01);
  148. }
  149. void RegTest::testAffine()
  150. {
  151. Mat img2;
  152. // Warp original image
  153. Matx<double, 2, 2> linTr(1., 0.1, -0.01, 1.);
  154. Vec<double, 2> shift(1., 1.);
  155. MapAffine mapTest(linTr, shift);
  156. mapTest.warp(img1, img2);
  157. // Register
  158. Ptr<Mapper> mapper = makePtr<MapperGradAffine>();
  159. MapperPyramid mappPyr(mapper);
  160. Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
  161. // Print result
  162. Ptr<MapAffine> mapAff = MapTypeCaster::toAffine(mapPtr);
  163. #if REG_DEBUG_OUTPUT
  164. cout << endl << "--- Testing affine mapper ---" << endl;
  165. cout << Mat(linTr) << endl;
  166. cout << Mat(shift) << endl;
  167. cout << Mat(mapAff->getLinTr()) << endl;
  168. cout << Mat(mapAff->getShift()) << endl;
  169. #endif
  170. // Check accuracy
  171. Ptr<Map> mapInv(mapAff->inverseMap());
  172. mapTest.compose(mapInv);
  173. double shNorm = cv::norm(mapTest.getShift());
  174. EXPECT_LE(shNorm, 0.1);
  175. double linTrNorm = cv::norm(mapTest.getLinTr());
  176. EXPECT_LE(linTrNorm, sqrt(2.) + 0.01);
  177. EXPECT_GE(linTrNorm, sqrt(2.) - 0.01);
  178. }
  179. void RegTest::testProjective()
  180. {
  181. Mat img2;
  182. // Warp original image
  183. Matx<double, 3, 3> projTr(1., 0., 0., 0., 1., 0., 0.0001, 0.0001, 1);
  184. MapProjec mapTest(projTr);
  185. mapTest.warp(img1, img2);
  186. // Register
  187. Ptr<Mapper> mapper = makePtr<MapperGradProj>();
  188. MapperPyramid mappPyr(mapper);
  189. Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
  190. // Print result
  191. Ptr<MapProjec> mapProj = MapTypeCaster::toProjec(mapPtr);
  192. mapProj->normalize();
  193. #if REG_DEBUG_OUTPUT
  194. cout << endl << "--- Testing projective transformation mapper ---" << endl;
  195. cout << Mat(projTr) << endl;
  196. cout << Mat(mapProj->getProjTr()) << endl;
  197. #endif
  198. // Check accuracy
  199. Ptr<Map> mapInv(mapProj->inverseMap());
  200. mapTest.compose(mapInv);
  201. double projNorm = cv::norm(mapTest.getProjTr());
  202. EXPECT_LE(projNorm, sqrt(3.) + 0.01);
  203. EXPECT_GE(projNorm, sqrt(3.) - 0.01);
  204. }
  205. void RegTest::loadImage(int dstDataType)
  206. {
  207. const string imageName = cvtest::TS::ptr()->get_data_path() + "reg/home.png";
  208. img1 = imread(imageName, -1);
  209. ASSERT_TRUE(!img1.empty());
  210. img1.convertTo(img1, dstDataType);
  211. }
  212. TEST_F(RegTest, shift)
  213. {
  214. loadImage();
  215. testShift();
  216. }
  217. TEST_F(RegTest, euclidean)
  218. {
  219. loadImage();
  220. testEuclidean();
  221. }
  222. TEST_F(RegTest, similarity)
  223. {
  224. loadImage();
  225. testSimilarity();
  226. }
  227. TEST_F(RegTest, affine)
  228. {
  229. loadImage();
  230. testAffine();
  231. }
  232. TEST_F(RegTest, projective)
  233. {
  234. loadImage();
  235. testProjective();
  236. }
  237. TEST_F(RegTest, projective_dt64fc3)
  238. {
  239. loadImage(CV_64FC3);
  240. testProjective();
  241. }
  242. TEST_F(RegTest, projective_dt64fc1)
  243. {
  244. loadImage(CV_64FC1);
  245. testProjective();
  246. }
  247. }} // namespace