MCPcopy Create free account
hub / github.com/boostorg/compute / calculateHistogramUsingCL

Function calculateHistogramUsingCL

example/opencv_histogram.cpp:125–179  ·  view source on GitHub ↗

Get the device context Create GPU array/vector Copy the image & set up the kernel Execute the kernel Copy GPU data back to CPU cv::Mat data pointer OpenCV conversion for convienient display

Source from the content-addressed store, hash-verified

123//Copy GPU data back to CPU cv::Mat data pointer
124//OpenCV conversion for convienient display
125void calculateHistogramUsingCL(cv::Mat src, compute::command_queue &queue)
126{
127 compute::context context = queue.get_context();
128
129 // Convert image to BGRA (OpenCL requires 16-byte aligned data)
130 cv::cvtColor(src, src, CV_BGR2BGRA);
131
132 //3 channels & 256 bins : alpha channel is ignored
133 compute::vector<int> gpu_b_hist(histSize, context);
134 compute::vector<int> gpu_g_hist(histSize, context);
135 compute::vector<int> gpu_r_hist(histSize, context);
136
137 // Transfer image to gpu
138 compute::image2d gpu_src =
139 compute::opencv_create_image2d_with_mat(
140 src, compute::image2d::read_only,
141 queue
142 );
143
144 compute::program histogram_program =
145 compute::program::create_with_source(source, context);
146 histogram_program.build();
147
148 // create histogram kernel and set arguments
149 compute::kernel histogram_kernel(histogram_program, "histogram");
150 histogram_kernel.set_arg(0, gpu_src);
151 histogram_kernel.set_arg(1, gpu_b_hist.get_buffer());
152 histogram_kernel.set_arg(2, gpu_g_hist.get_buffer());
153 histogram_kernel.set_arg(3, gpu_r_hist.get_buffer());
154
155 // run histogram kernel
156 // each kernel thread updating red, green & blue bins
157 size_t origin[2] = { 0, 0 };
158 size_t region[2] = { gpu_src.width(),
159 gpu_src.height() };
160
161 queue.enqueue_nd_range_kernel(histogram_kernel, 2, origin, region, 0);
162
163 //Make sure kernel get executed and data copied back
164 queue.finish();
165
166 //create Mat and copy GPU bins to CPU memory
167 cv::Mat b_hist(256, 1, CV_32SC1);
168 compute::copy(gpu_b_hist.begin(), gpu_b_hist.end(), b_hist.data, queue);
169 cv::Mat g_hist(256, 1, CV_32SC1);
170 compute::copy(gpu_g_hist.begin(), gpu_g_hist.end(), g_hist.data, queue);
171 cv::Mat r_hist(256, 1, CV_32SC1);
172 compute::copy(gpu_r_hist.begin(), gpu_r_hist.end(), r_hist.data, queue);
173
174 b_hist.convertTo(b_hist, CV_32FC1); //converted for displaying
175 g_hist.convertTo(g_hist, CV_32FC1);
176 r_hist.convertTo(r_hist, CV_32FC1);
177
178 showHistogramWindow(b_hist, g_hist, r_hist, "Histogram");
179}
180
181int main( int argc, char** argv )
182{

Callers 1

mainFunction · 0.85

Calls 12

copyFunction · 0.85
showHistogramWindowFunction · 0.85
buildMethod · 0.80
widthMethod · 0.80
heightMethod · 0.80
get_contextMethod · 0.45
set_argMethod · 0.45
finishMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected