test_sgm_funcs.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Author: The "adaskit Team" at Fixstars Corporation
  6. #include "test_precomp.hpp"
  7. #ifdef HAVE_CUDA
  8. #ifdef _WIN32
  9. #define popcnt64 __popcnt64
  10. #else
  11. #define popcnt64 __builtin_popcountll
  12. #endif
  13. #include "opencv2/core/cuda.hpp"
  14. namespace cv { namespace cuda { namespace device {
  15. namespace stereosgm
  16. {
  17. namespace census_transform
  18. {
  19. void censusTransform(const GpuMat& src, GpuMat& dest, cv::cuda::Stream& stream);
  20. }
  21. namespace path_aggregation
  22. {
  23. namespace horizontal
  24. {
  25. template <unsigned int MAX_DISPARITY>
  26. void aggregateLeft2RightPath(
  27. const GpuMat& left,
  28. const GpuMat& right,
  29. GpuMat& dest,
  30. unsigned int p1,
  31. unsigned int p2,
  32. int min_disp,
  33. Stream& stream);
  34. template <unsigned int MAX_DISPARITY>
  35. void aggregateRight2LeftPath(
  36. const GpuMat& left,
  37. const GpuMat& right,
  38. GpuMat& dest,
  39. unsigned int p1,
  40. unsigned int p2,
  41. int min_disp,
  42. Stream& stream);
  43. }
  44. namespace vertical
  45. {
  46. template <unsigned int MAX_DISPARITY>
  47. void aggregateUp2DownPath(
  48. const GpuMat& left,
  49. const GpuMat& right,
  50. GpuMat& dest,
  51. unsigned int p1,
  52. unsigned int p2,
  53. int min_disp,
  54. Stream& stream);
  55. template <unsigned int MAX_DISPARITY>
  56. void aggregateDown2UpPath(
  57. const GpuMat& left,
  58. const GpuMat& right,
  59. GpuMat& dest,
  60. unsigned int p1,
  61. unsigned int p2,
  62. int min_disp,
  63. Stream& stream);
  64. }
  65. namespace oblique
  66. {
  67. template <unsigned int MAX_DISPARITY>
  68. void aggregateUpleft2DownrightPath(
  69. const GpuMat& left,
  70. const GpuMat& right,
  71. GpuMat& dest,
  72. unsigned int p1,
  73. unsigned int p2,
  74. int min_disp,
  75. Stream& stream);
  76. template <unsigned int MAX_DISPARITY>
  77. void aggregateUpright2DownleftPath(
  78. const GpuMat& left,
  79. const GpuMat& right,
  80. GpuMat& dest,
  81. unsigned int p1,
  82. unsigned int p2,
  83. int min_disp,
  84. Stream& stream);
  85. template <unsigned int MAX_DISPARITY>
  86. void aggregateDownright2UpleftPath(
  87. const GpuMat& left,
  88. const GpuMat& right,
  89. GpuMat& dest,
  90. unsigned int p1,
  91. unsigned int p2,
  92. int min_disp,
  93. Stream& stream);
  94. template <unsigned int MAX_DISPARITY>
  95. void aggregateDownleft2UprightPath(
  96. const GpuMat& left,
  97. const GpuMat& right,
  98. GpuMat& dest,
  99. unsigned int p1,
  100. unsigned int p2,
  101. int min_disp,
  102. Stream& stream);
  103. }
  104. } // namespace path_aggregation
  105. namespace winner_takes_all
  106. {
  107. template <size_t MAX_DISPARITY>
  108. void winnerTakesAll(const GpuMat& src, GpuMat& left, GpuMat& right, float uniqueness, bool subpixel, int mode, cv::cuda::Stream& stream);
  109. }
  110. } // namespace stereosgm
  111. }}} // namespace cv { namespace cuda { namespace device {
  112. namespace opencv_test { namespace {
  113. void census_transform(const cv::Mat& src, cv::Mat& dst)
  114. {
  115. const int hor = 9 / 2, ver = 7 / 2;
  116. dst.create(src.size(), CV_32SC1);
  117. dst = 0;
  118. for (int y = ver; y < static_cast<int>(src.rows) - ver; ++y) {
  119. for (int x = hor; x < static_cast<int>(src.cols) - hor; ++x) {
  120. int32_t value = 0;
  121. for (int dy = -ver; dy <= 0; ++dy) {
  122. for (int dx = -hor; dx <= (dy == 0 ? -1 : hor); ++dx) {
  123. const auto a = src.at<uint8_t>(y + dy, x + dx);
  124. const auto b = src.at<uint8_t>(y - dy, x - dx);
  125. value <<= 1;
  126. if (a > b) { value |= 1; }
  127. }
  128. }
  129. dst.at<int32_t>(y, x) = value;
  130. }
  131. }
  132. }
  133. PARAM_TEST_CASE(StereoSGM_CensusTransformImage, cv::cuda::DeviceInfo, std::string, UseRoi)
  134. {
  135. cv::cuda::DeviceInfo devInfo;
  136. std::string path;
  137. bool useRoi;
  138. virtual void SetUp()
  139. {
  140. devInfo = GET_PARAM(0);
  141. path = GET_PARAM(1);
  142. useRoi = GET_PARAM(2);
  143. cv::cuda::setDevice(devInfo.deviceID());
  144. }
  145. };
  146. CUDA_TEST_P(StereoSGM_CensusTransformImage, Image)
  147. {
  148. cv::Mat image = readImage(path, cv::IMREAD_GRAYSCALE);
  149. cv::Mat dst_gold;
  150. census_transform(image, dst_gold);
  151. cv::cuda::GpuMat g_dst;
  152. g_dst.create(image.size(), CV_32SC1);
  153. cv::cuda::device::stereosgm::census_transform::censusTransform(loadMat(image, useRoi), g_dst, cv::cuda::Stream::Null());
  154. cv::Mat dst;
  155. g_dst.download(dst);
  156. EXPECT_MAT_NEAR(dst_gold, dst, 0);
  157. }
  158. INSTANTIATE_TEST_CASE_P(CUDA_StereoSGM_funcs, StereoSGM_CensusTransformImage, testing::Combine(
  159. ALL_DEVICES,
  160. testing::Values("stereobm/aloe-L.png", "stereobm/aloe-R.png"),
  161. WHOLE_SUBMAT));
  162. PARAM_TEST_CASE(StereoSGM_CensusTransformRandom, cv::cuda::DeviceInfo, cv::Size, UseRoi)
  163. {
  164. cv::cuda::DeviceInfo devInfo;
  165. cv::Size size;
  166. bool useRoi;
  167. virtual void SetUp()
  168. {
  169. devInfo = GET_PARAM(0);
  170. size = GET_PARAM(1);
  171. useRoi = GET_PARAM(2);
  172. cv::cuda::setDevice(devInfo.deviceID());
  173. }
  174. };
  175. CUDA_TEST_P(StereoSGM_CensusTransformRandom, Random)
  176. {
  177. cv::Mat image = randomMat(size, CV_8UC1);
  178. cv::Mat dst_gold;
  179. census_transform(image, dst_gold);
  180. cv::cuda::GpuMat g_dst;
  181. g_dst.create(image.size(), CV_32SC1);
  182. cv::cuda::device::stereosgm::census_transform::censusTransform(loadMat(image, useRoi), g_dst, cv::cuda::Stream::Null());
  183. cv::Mat dst;
  184. g_dst.download(dst);
  185. EXPECT_MAT_NEAR(dst_gold, dst, 0);
  186. }
  187. INSTANTIATE_TEST_CASE_P(CUDA_StereoSGM_funcs, StereoSGM_CensusTransformRandom, testing::Combine(
  188. ALL_DEVICES,
  189. DIFFERENT_SIZES,
  190. WHOLE_SUBMAT));
  191. static void path_aggregation(
  192. const cv::Mat& left,
  193. const cv::Mat& right,
  194. cv::Mat& dst,
  195. int max_disparity, int min_disparity, int p1, int p2,
  196. int dx, int dy)
  197. {
  198. const int width = left.cols;
  199. const int height = left.rows;
  200. dst.create(cv::Size(width * height * max_disparity, 1), CV_8UC1);
  201. std::vector<int> before(max_disparity);
  202. for (int i = (dy < 0 ? height - 1 : 0); 0 <= i && i < height; i += (dy < 0 ? -1 : 1)) {
  203. for (int j = (dx < 0 ? width - 1 : 0); 0 <= j && j < width; j += (dx < 0 ? -1 : 1)) {
  204. const int i2 = i - dy, j2 = j - dx;
  205. const bool inside = (0 <= i2 && i2 < height && 0 <= j2 && j2 < width);
  206. for (int k = 0; k < max_disparity; ++k) {
  207. before[k] = inside ? dst.at<uint8_t>(0, k + (j2 + i2 * width) * max_disparity) : 0;
  208. }
  209. const int min_cost = *min_element(before.begin(), before.end());
  210. for (int k = 0; k < max_disparity; ++k) {
  211. const auto l = left.at<int32_t>(i, j);
  212. const auto r = (k + min_disparity > j ? 0 : right.at<int32_t>(i, j - k - min_disparity));
  213. int cost = std::min(before[k] - min_cost, p2);
  214. if (k > 0) {
  215. cost = std::min(cost, before[k - 1] - min_cost + p1);
  216. }
  217. if (k + 1 < max_disparity) {
  218. cost = std::min(cost, before[k + 1] - min_cost + p1);
  219. }
  220. cost += static_cast<int>(popcnt64(l ^ r));
  221. dst.at<uint8_t>(0, k + (j + i * width) * max_disparity) = static_cast<uint8_t>(cost);
  222. }
  223. }
  224. }
  225. }
  226. static constexpr size_t DISPARITY = 128;
  227. static constexpr int P1 = 10;
  228. static constexpr int P2 = 120;
  229. PARAM_TEST_CASE(StereoSGM_PathAggregation, cv::cuda::DeviceInfo, cv::Size, UseRoi, int)
  230. {
  231. cv::cuda::DeviceInfo devInfo;
  232. cv::Size size;
  233. bool useRoi;
  234. int minDisp;
  235. virtual void SetUp()
  236. {
  237. devInfo = GET_PARAM(0);
  238. size = GET_PARAM(1);
  239. useRoi = GET_PARAM(2);
  240. minDisp = GET_PARAM(3);
  241. cv::cuda::setDevice(devInfo.deviceID());
  242. }
  243. template<typename T>
  244. void test_path_aggregation(T func, int dx, int dy)
  245. {
  246. cv::Mat left_image = randomMat(size, CV_32SC1, 0.0, static_cast<double>(std::numeric_limits<int32_t>::max()));
  247. cv::Mat right_image = randomMat(size, CV_32SC1, 0.0, static_cast<double>(std::numeric_limits<int32_t>::max()));
  248. cv::Mat dst_gold;
  249. path_aggregation(left_image, right_image, dst_gold, DISPARITY, minDisp, P1, P2, dx, dy);
  250. cv::cuda::GpuMat g_dst;
  251. g_dst.create(cv::Size(left_image.cols * left_image.rows * DISPARITY, 1), CV_8UC1);
  252. func(loadMat(left_image, useRoi), loadMat(right_image, useRoi), g_dst, P1, P2, minDisp, cv::cuda::Stream::Null());
  253. cv::Mat dst;
  254. g_dst.download(dst);
  255. EXPECT_MAT_NEAR(dst_gold, dst, 0);
  256. }
  257. };
  258. CUDA_TEST_P(StereoSGM_PathAggregation, RandomLeft2Right)
  259. {
  260. test_path_aggregation(cv::cuda::device::stereosgm::path_aggregation::horizontal::aggregateLeft2RightPath<DISPARITY>, 1, 0);
  261. }
  262. CUDA_TEST_P(StereoSGM_PathAggregation, RandomRight2Left)
  263. {
  264. test_path_aggregation(cv::cuda::device::stereosgm::path_aggregation::horizontal::aggregateRight2LeftPath<DISPARITY>, -1, 0);
  265. }
  266. CUDA_TEST_P(StereoSGM_PathAggregation, RandomUp2Down)
  267. {
  268. test_path_aggregation(cv::cuda::device::stereosgm::path_aggregation::vertical::aggregateUp2DownPath<DISPARITY>, 0, 1);
  269. }
  270. CUDA_TEST_P(StereoSGM_PathAggregation, RandomDown2Up)
  271. {
  272. test_path_aggregation(cv::cuda::device::stereosgm::path_aggregation::vertical::aggregateDown2UpPath<DISPARITY>, 0, -1);
  273. }
  274. CUDA_TEST_P(StereoSGM_PathAggregation, RandomUpLeft2DownRight)
  275. {
  276. test_path_aggregation(cv::cuda::device::stereosgm::path_aggregation::oblique::aggregateUpleft2DownrightPath<DISPARITY>, 1, 1);
  277. }
  278. CUDA_TEST_P(StereoSGM_PathAggregation, RandomUpRight2DownLeft)
  279. {
  280. test_path_aggregation(cv::cuda::device::stereosgm::path_aggregation::oblique::aggregateUpright2DownleftPath<DISPARITY>, -1, 1);
  281. }
  282. CUDA_TEST_P(StereoSGM_PathAggregation, RandomDownRight2UpLeft)
  283. {
  284. test_path_aggregation(cv::cuda::device::stereosgm::path_aggregation::oblique::aggregateDownright2UpleftPath<DISPARITY>, -1, -1);
  285. }
  286. CUDA_TEST_P(StereoSGM_PathAggregation, RandomDownLeft2UpRight)
  287. {
  288. test_path_aggregation(cv::cuda::device::stereosgm::path_aggregation::oblique::aggregateDownleft2UprightPath<DISPARITY>, 1, -1);
  289. }
  290. INSTANTIATE_TEST_CASE_P(CUDA_StereoSGM_funcs, StereoSGM_PathAggregation, testing::Combine(
  291. ALL_DEVICES,
  292. DIFFERENT_SIZES,
  293. WHOLE_SUBMAT,
  294. testing::Values(0, 1, 10)));
  295. void winner_takes_all_left(
  296. const cv::Mat& src,
  297. cv::Mat& dst,
  298. int width, int height, int disparity, int num_paths,
  299. float uniqueness, bool subpixel)
  300. {
  301. dst.create(cv::Size(width, height), CV_16UC1);
  302. for (int i = 0; i < height; ++i) {
  303. for (int j = 0; j < width; ++j) {
  304. std::vector<std::pair<int, int>> v;
  305. for (int k = 0; k < disparity; ++k) {
  306. int cost_sum = 0;
  307. for (int p = 0; p < num_paths; ++p) {
  308. cost_sum += static_cast<int>(src.at<uint8_t>(0,
  309. p * disparity * width * height +
  310. i * disparity * width +
  311. j * disparity +
  312. k));
  313. }
  314. v.emplace_back(cost_sum, static_cast<int>(k));
  315. }
  316. const auto ite = std::min_element(v.begin(), v.end());
  317. assert(ite != v.end());
  318. const auto best = *ite;
  319. const int best_cost = best.first;
  320. int best_disp = best.second;
  321. int ans = best_disp;
  322. if (subpixel) {
  323. ans <<= StereoMatcher::DISP_SHIFT;
  324. if (0 < best_disp && best_disp < static_cast<int>(disparity) - 1) {
  325. const int left = v[best_disp - 1].first;
  326. const int right = v[best_disp + 1].first;
  327. const int numer = left - right;
  328. const int denom = left - 2 * best_cost + right;
  329. ans += ((numer << StereoMatcher::DISP_SHIFT) + denom) / (2 * denom);
  330. }
  331. }
  332. for (const auto& p : v) {
  333. const int cost = p.first;
  334. const int disp = p.second;
  335. if (cost * uniqueness < best_cost && abs(disp - best_disp) > 1) {
  336. ans = -1;
  337. break;
  338. }
  339. }
  340. dst.at<uint16_t>(i, j) = static_cast<uint16_t>(ans);
  341. }
  342. }
  343. }
  344. PARAM_TEST_CASE(StereoSGM_WinnerTakesAll, cv::cuda::DeviceInfo, cv::Size, bool, int)
  345. {
  346. cv::cuda::DeviceInfo devInfo;
  347. cv::Size size;
  348. bool subpixel;
  349. int mode;
  350. virtual void SetUp()
  351. {
  352. devInfo = GET_PARAM(0);
  353. size = GET_PARAM(1);
  354. subpixel = GET_PARAM(2);
  355. mode = GET_PARAM(3);
  356. cv::cuda::setDevice(devInfo.deviceID());
  357. }
  358. };
  359. CUDA_TEST_P(StereoSGM_WinnerTakesAll, RandomLeft)
  360. {
  361. int num_paths = mode == cv::cuda::StereoSGM::MODE_HH4 ? 4 : 8;
  362. cv::Mat aggregated = randomMat(cv::Size(size.width * size.height * DISPARITY * num_paths, 1), CV_8UC1, 0.0, 32.0);
  363. cv::Mat dst_gold;
  364. winner_takes_all_left(aggregated, dst_gold, size.width, size.height, DISPARITY, num_paths, 0.95f, subpixel);
  365. cv::cuda::GpuMat g_src, g_dst, g_dst_right;
  366. g_src.upload(aggregated);
  367. g_dst.create(size, CV_16UC1);
  368. g_dst_right.create(size, CV_16UC1);
  369. cv::cuda::device::stereosgm::winner_takes_all::winnerTakesAll<DISPARITY>(g_src, g_dst, g_dst_right, 0.95f, subpixel, mode, cv::cuda::Stream::Null());
  370. cv::Mat dst;
  371. g_dst.download(dst);
  372. EXPECT_MAT_NEAR(dst_gold, dst, 0);
  373. }
  374. INSTANTIATE_TEST_CASE_P(CUDA_StereoSGM_funcs, StereoSGM_WinnerTakesAll, testing::Combine(
  375. ALL_DEVICES,
  376. DIFFERENT_SIZES,
  377. testing::Values(false, true),
  378. testing::Values(cv::cuda::StereoSGM::MODE_HH4, cv::cuda::StereoSGM::MODE_HH)));
  379. }} // namespace
  380. #endif // HAVE_CUDA