test_interop_opencv.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #define BOOST_TEST_MODULE TestInteropOpenCV
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/container/vector.hpp>
  14. #include <boost/compute/algorithm/reverse.hpp>
  15. #include <boost/compute/interop/opencv.hpp>
  16. #include <opencv2/imgproc/imgproc.hpp>
  17. #include "check_macros.hpp"
  18. #include "context_setup.hpp"
  19. namespace bcl = boost::compute;
  20. BOOST_AUTO_TEST_CASE(opencv_mat_to_buffer)
  21. {
  22. // create opencv mat
  23. cv::Mat mat(1, 4, CV_32F);
  24. mat.at<float>(0, 0) = 0.0f;
  25. mat.at<float>(0, 1) = 2.5f;
  26. mat.at<float>(0, 2) = 4.1f;
  27. mat.at<float>(0, 3) = 5.6f;
  28. // copy mat to gpu vector
  29. bcl::vector<float> vector(4, context);
  30. bcl::opencv_copy_mat_to_buffer(mat, vector.begin(), queue);
  31. CHECK_RANGE_EQUAL(float, 4, vector, (0.0f, 2.5f, 4.1f, 5.6f));
  32. // reverse gpu vector and copy back to mat
  33. bcl::reverse(vector.begin(), vector.end(), queue);
  34. bcl::opencv_copy_buffer_to_mat(vector.begin(), mat, queue);
  35. BOOST_CHECK_EQUAL(mat.at<float>(0), 5.6f);
  36. BOOST_CHECK_EQUAL(mat.at<float>(1), 4.1f);
  37. BOOST_CHECK_EQUAL(mat.at<float>(2), 2.5f);
  38. BOOST_CHECK_EQUAL(mat.at<float>(3), 0.0f);
  39. }
  40. BOOST_AUTO_TEST_CASE(opencv_image_format)
  41. {
  42. // 8-bit uchar BGRA
  43. BOOST_CHECK(
  44. bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_8UC4)) ==
  45. bcl::image_format(CL_BGRA, CL_UNORM_INT8)
  46. );
  47. // 32-bit float
  48. BOOST_CHECK(
  49. bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_32F)) ==
  50. bcl::image_format(CL_INTENSITY, CL_FLOAT)
  51. );
  52. // 32-bit float RGBA
  53. BOOST_CHECK(
  54. bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_32FC4)) ==
  55. bcl::image_format(CL_RGBA, CL_FLOAT)
  56. );
  57. // 16-bit uchar BGRA
  58. BOOST_CHECK(
  59. bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_16UC4)) ==
  60. bcl::image_format(CL_BGRA, CL_UNORM_INT16)
  61. );
  62. // 8-bit uchar
  63. BOOST_CHECK(
  64. bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_8UC1)) ==
  65. bcl::image_format(CL_INTENSITY, CL_UNORM_INT8)
  66. );
  67. }
  68. BOOST_AUTO_TEST_CASE(opencv_float_mat_image2d)
  69. {
  70. REQUIRES_OPENCL_VERSION(1,2);
  71. cv::Vec4f pixel;
  72. // create opencv mat
  73. cv::Mat mat(2, 2, CV_32FC4, cv::Scalar(100, 150, 200, 255));
  74. // transfer image to gpu
  75. bcl::image2d image =
  76. bcl::opencv_create_image2d_with_mat(
  77. mat,
  78. bcl::image2d::read_only,
  79. queue
  80. );
  81. // copy the data back to cpu
  82. bcl::opencv_copy_image_to_mat(image, mat, queue);
  83. pixel = mat.at<cv::Vec4f>(1,1);
  84. BOOST_CHECK_EQUAL(pixel[0], 100.0f);
  85. BOOST_CHECK_EQUAL(pixel[1], 150.0f);
  86. BOOST_CHECK_EQUAL(pixel[2], 200.0f);
  87. BOOST_CHECK_EQUAL(pixel[3], 255.0f);
  88. }
  89. BOOST_AUTO_TEST_SUITE_END()