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 
11 #define BOOST_TEST_MODULE TestInteropOpenCV
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/container/vector.hpp>
16 #include <boost/compute/algorithm/reverse.hpp>
17 #include <boost/compute/interop/opencv.hpp>
18 #include <opencv2/imgproc/imgproc.hpp>
19 
20 #include "check_macros.hpp"
21 #include "context_setup.hpp"
22 
23 namespace bcl = boost::compute;
24 
BOOST_AUTO_TEST_CASE(opencv_mat_to_buffer)25 BOOST_AUTO_TEST_CASE(opencv_mat_to_buffer)
26 {
27     // create opencv mat
28     cv::Mat mat(1, 4, CV_32F);
29     mat.at<float>(0, 0) = 0.0f;
30     mat.at<float>(0, 1) = 2.5f;
31     mat.at<float>(0, 2) = 4.1f;
32     mat.at<float>(0, 3) = 5.6f;
33 
34     // copy mat to gpu vector
35     bcl::vector<float> vector(4, context);
36     bcl::opencv_copy_mat_to_buffer(mat, vector.begin(), queue);
37     CHECK_RANGE_EQUAL(float, 4, vector, (0.0f, 2.5f, 4.1f, 5.6f));
38 
39     // reverse gpu vector and copy back to mat
40     bcl::reverse(vector.begin(), vector.end(), queue);
41     bcl::opencv_copy_buffer_to_mat(vector.begin(), mat, queue);
42     BOOST_CHECK_EQUAL(mat.at<float>(0), 5.6f);
43     BOOST_CHECK_EQUAL(mat.at<float>(1), 4.1f);
44     BOOST_CHECK_EQUAL(mat.at<float>(2), 2.5f);
45     BOOST_CHECK_EQUAL(mat.at<float>(3), 0.0f);
46 }
47 
BOOST_AUTO_TEST_CASE(opencv_image_format)48 BOOST_AUTO_TEST_CASE(opencv_image_format)
49 {
50     // 8-bit uchar BGRA
51     BOOST_CHECK(
52         bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_8UC4)) ==
53         bcl::image_format(CL_BGRA, CL_UNORM_INT8)
54     );
55 
56     // 32-bit float
57     BOOST_CHECK(
58         bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_32F)) ==
59         bcl::image_format(CL_INTENSITY, CL_FLOAT)
60     );
61 
62     // 32-bit float RGBA
63     BOOST_CHECK(
64         bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_32FC4)) ==
65         bcl::image_format(CL_RGBA, CL_FLOAT)
66     );
67 
68     // 16-bit uchar BGRA
69     BOOST_CHECK(
70         bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_16UC4)) ==
71         bcl::image_format(CL_BGRA, CL_UNORM_INT16)
72     );
73 
74     // 8-bit uchar
75     BOOST_CHECK(
76         bcl::opencv_get_mat_image_format(cv::Mat(32, 32, CV_8UC1)) ==
77         bcl::image_format(CL_INTENSITY, CL_UNORM_INT8)
78     );
79 }
80 
BOOST_AUTO_TEST_CASE(opencv_float_mat_image2d)81 BOOST_AUTO_TEST_CASE(opencv_float_mat_image2d)
82 {
83     REQUIRES_OPENCL_VERSION(1,2);
84 
85     cv::Vec4f pixel;
86     // create opencv mat
87     cv::Mat mat(2, 2, CV_32FC4, cv::Scalar(100, 150, 200, 255));
88     // transfer image to gpu
89     bcl::image2d image =
90             bcl::opencv_create_image2d_with_mat(
91                 mat,
92                 bcl::image2d::read_only,
93                 queue
94                 );
95     // copy the data back to cpu
96     bcl::opencv_copy_image_to_mat(image, mat, queue);
97 
98     pixel = mat.at<cv::Vec4f>(1,1);
99     BOOST_CHECK_EQUAL(pixel[0], 100.0f);
100     BOOST_CHECK_EQUAL(pixel[1], 150.0f);
101     BOOST_CHECK_EQUAL(pixel[2], 200.0f);
102     BOOST_CHECK_EQUAL(pixel[3], 255.0f);
103 }
104 
105 BOOST_AUTO_TEST_SUITE_END()
106