From ad86b80375b43d6dcc420a187966d48cd2e6ed1e Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Thu, 13 Dec 2012 02:33:21 -0500 Subject: [PATCH 01/10] finished buildPointList --- modules/ocl/src/hough.cpp | 383 +++++++++++++++++++++++++++++++ modules/ocl/src/kernels/hough.cl | 307 +++++++++++++++++++++++++ 2 files changed, 690 insertions(+) create mode 100644 modules/ocl/src/hough.cpp create mode 100644 modules/ocl/src/kernels/hough.cl diff --git a/modules/ocl/src/hough.cpp b/modules/ocl/src/hough.cpp new file mode 100644 index 0000000000..dd4db84a47 --- /dev/null +++ b/modules/ocl/src/hough.cpp @@ -0,0 +1,383 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Modified by Seunghoon Park(pclove1@gmail.com) +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "precomp.hpp" + +using namespace std; +using namespace cv; +using namespace cv::ocl; + +#if !defined (HAVE_OPENCL) + +// void cv::ocl::HoughLines(const oclMat&, oclMat&, float, float, int, bool, int) { throw_nogpu(); } +// void cv::ocl::HoughLines(const oclMat&, oclMat&, HoughLinesBuf&, float, float, int, bool, int) { throw_nogpu(); } +// void cv::ocl::HoughLinesDownload(const oclMat&, OutputArray, OutputArray) { throw_nogpu(); } + +void cv::ocl::HoughCircles(const oclMat&, oclMat&, int, float, float, int, int, int, int, int) { throw_nogpu(); } +void cv::ocl::HoughCircles(const oclMat&, oclMat&, HoughCirclesBuf&, int, float, float, int, int, int, int, int) { throw_nogpu(); } +void cv::ocl::HoughCirclesDownload(const oclMat&, OutputArray) { throw_nogpu(); } + +// Ptr cv::ocl::GeneralizedHough_GPU::create(int) { throw_nogpu(); return Ptr(); } +// cv::ocl::GeneralizedHough_GPU::~GeneralizedHough_GPU() {} +// void cv::ocl::GeneralizedHough_GPU::setTemplate(const oclMat&, int, Point) { throw_nogpu(); } +// void cv::ocl::GeneralizedHough_GPU::setTemplate(const oclMat&, const oclMat&, const oclMat&, Point) { throw_nogpu(); } +// void cv::ocl::GeneralizedHough_GPU::detect(const oclMat&, oclMat&, int) { throw_nogpu(); } +// void cv::ocl::GeneralizedHough_GPU::detect(const oclMat&, const oclMat&, const oclMat&, oclMat&) { throw_nogpu(); } +// void cv::ocl::GeneralizedHough_GPU::download(const oclMat&, OutputArray, OutputArray) { throw_nogpu(); } +// void cv::ocl::GeneralizedHough_GPU::release() {} + +#else /* !defined (HAVE_OPENCL) */ + +namespace cv { namespace ocl +{ + int buildPointList_gpu(const oclMat& src, unsigned int* list); + + ///////////////////////////OpenCL kernel strings/////////////////////////// + extern const char *hough; +}} + + + +////////////////////////////////////////////////////////// +// common functions + +namespace cv { namespace ocl +{ + int buildPointList_gpu(const oclMat& src, unsigned int* list) + { + const int PIXELS_PER_THREAD = 16; + + // void* counterPtr; + // cudaSafeCall( cudaGetSymbolAddress(&counterPtr, g_counter) ); + // cudaSafeCall( cudaMemset(counterPtr, 0, sizeof(int)) ); + + int totalCount = 0; + int err = CL_SUCCESS; + cl_mem counter = clCreateBuffer(src.clCxt->impl->clContext, + CL_MEM_COPY_HOST_PTR, // CL_MEM_READ_WRITE, + sizeof(int), + &totalCount, // NULL, + &err); + openCLSafeCall(err); + // openCLSafeCall(clEnqueueWriteBuffer(src.clCxt->impl->clCmdQueue, counter, CL_TRUE, 0, sizeof(int), &totalCount, 0, 0, 0)); + + // const dim3 block(32, 4); + // const dim3 grid(divUp(src.cols, block.x * PIXELS_PER_THREAD), divUp(src.rows, block.y)); + + const size_t blkSizeX = 32; + const size_t blkSizeY = 4; + size_t localThreads[3] = { blkSizeX, blkSizeY, 1 }; + + const int PIXELS_PER_BLOCK = blkSizeX * PIXELS_PER_THREAD; + const size_t glbSizeX = src.cols % (PIXELS_PER_BLOCK) == 0 ? src.cols : (src.cols / PIXELS_PER_BLOCK + 1) * PIXELS_PER_BLOCK; + const size_t glbSizeY = src.rows % blkSizeY == 0 ? src.rows : (src.rows / blkSizeY + 1) * blkSizeY; + size_t globalThreads[3] = { glbSizeX, glbSizeY, 1 }; + + // cudaSafeCall( cudaFuncSetCacheConfig(buildPointList, cudaFuncCachePreferShared) ); + + // buildPointList<<>>(src, list); + // cudaSafeCall( cudaGetLastError() ); + // cudaSafeCall( cudaDeviceSynchronize() ); + vector > args; + args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&list )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&counter )); + + openCLExecuteKernel(src.clCxt, &hough, "buildPointList", globalThreads, localThreads, args, -1, -1); + // int totalCount; + // cudaSafeCall( cudaMemcpy(&totalCount, counterPtr, sizeof(int), cudaMemcpyDeviceToHost) ); + openCLSafeCall(clEnqueueReadBuffer(src.clCxt->impl->clCmdQueue, counter, CL_TRUE, 0, sizeof(int), &totalCount, 0, NULL, NULL)); + openCLSafeCall(clReleaseMemObject(counter)); + + return totalCount; + } +}} + +////////////////////////////////////////////////////////// +// HoughLines + +// namespace cv { namespace ocl { namespace device +// { +// namespace hough +// { +// void linesAccum_gpu(const unsigned int* list, int count, PtrStepSzi accum, float rho, float theta, size_t sharedMemPerBlock, bool has20); +// int linesGetResult_gpu(PtrStepSzi accum, float2* out, int* votes, int maxSize, float rho, float theta, int threshold, bool doSort); +// } +// }}} + +// void cv::ocl::HoughLines(const oclMat& src, oclMat& lines, float rho, float theta, int threshold, bool doSort, int maxLines) +// { +// HoughLinesBuf buf; +// HoughLines(src, lines, buf, rho, theta, threshold, doSort, maxLines); +// } + +// void cv::ocl::HoughLines(const oclMat& src, oclMat& lines, HoughLinesBuf& buf, float rho, float theta, int threshold, bool doSort, int maxLines) +// { +// using namespace cv::ocl::device::hough; + +// CV_Assert(src.type() == CV_8UC1); +// CV_Assert(src.cols < std::numeric_limits::max()); +// CV_Assert(src.rows < std::numeric_limits::max()); + +// ensureSizeIsEnough(1, src.size().area(), CV_32SC1, buf.list); +// unsigned int* srcPoints = buf.list.ptr(); + +// const int pointsCount = buildPointList_gpu(src, srcPoints); +// if (pointsCount == 0) +// { +// lines.release(); +// return; +// } + +// const int numangle = cvRound(CV_PI / theta); +// const int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho); +// CV_Assert(numangle > 0 && numrho > 0); + +// ensureSizeIsEnough(numangle + 2, numrho + 2, CV_32SC1, buf.accum); +// buf.accum.setTo(Scalar::all(0)); + +// DeviceInfo devInfo; +// linesAccum_gpu(srcPoints, pointsCount, buf.accum, rho, theta, devInfo.sharedMemPerBlock(), devInfo.supports(FEATURE_SET_COMPUTE_20)); + +// ensureSizeIsEnough(2, maxLines, CV_32FC2, lines); + +// int linesCount = linesGetResult_gpu(buf.accum, lines.ptr(0), lines.ptr(1), maxLines, rho, theta, threshold, doSort); +// if (linesCount > 0) +// lines.cols = linesCount; +// else +// lines.release(); +// } + +// void cv::ocl::HoughLinesDownload(const oclMat& d_lines, OutputArray h_lines_, OutputArray h_votes_) +// { +// if (d_lines.empty()) +// { +// h_lines_.release(); +// if (h_votes_.needed()) +// h_votes_.release(); +// return; +// } + +// CV_Assert(d_lines.rows == 2 && d_lines.type() == CV_32FC2); + +// h_lines_.create(1, d_lines.cols, CV_32FC2); +// Mat h_lines = h_lines_.getMat(); +// d_lines.row(0).download(h_lines); + +// if (h_votes_.needed()) +// { +// h_votes_.create(1, d_lines.cols, CV_32SC1); +// Mat h_votes = h_votes_.getMat(); +// oclMat d_votes(1, d_lines.cols, CV_32SC1, const_cast(d_lines.ptr(1))); +// d_votes.download(h_votes); +// } +// } + +////////////////////////////////////////////////////////// +// HoughCircles + +// namespace cv { namespace ocl +// { +// namespace hough +// { +// void circlesAccumCenters_gpu(const unsigned int* list, int count, PtrStepi dx, PtrStepi dy, PtrStepSzi accum, int minRadius, int maxRadius, float idp); +// int buildCentersList_gpu(PtrStepSzi accum, unsigned int* centers, int threshold); +// int circlesAccumRadius_gpu(const unsigned int* centers, int centersCount, const unsigned int* list, int count, +// float3* circles, int maxCircles, float dp, int minRadius, int maxRadius, int threshold, bool has20); +// } +// }} + +void cv::ocl::HoughCircles(const oclMat& src, oclMat& circles, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles) +{ + HoughCirclesBuf buf; + HoughCircles(src, circles, buf, method, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius, maxCircles); +} + +void cv::ocl::HoughCircles(const oclMat& src, oclMat& circles, HoughCirclesBuf& buf, int method, + float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles) +{ + CV_Assert(src.type() == CV_8UC1); + CV_Assert(src.cols < std::numeric_limits::max()); + CV_Assert(src.rows < std::numeric_limits::max()); + CV_Assert(method == CV_HOUGH_GRADIENT); + CV_Assert(dp > 0); + CV_Assert(minRadius > 0 && maxRadius > minRadius); + CV_Assert(cannyThreshold > 0); + CV_Assert(votesThreshold > 0); + CV_Assert(maxCircles > 0); + + const float idp = 1.0f / dp; + + cv::ocl::Canny(src, buf.cannyBuf, buf.edges, std::max(cannyThreshold / 2, 1), cannyThreshold); + + ensureSizeIsEnough(2, src.size().area(), CV_32SC1, buf.list); + // unsigned int* srcPoints = buf.list.ptr(0); + unsigned int* srcPoints = (unsigned int*)buf.list.data; + // unsigned int* centers = buf.list.ptr(1); + unsigned int* centers = (unsigned int*)buf.list.data + buf.list.step; + + const int pointsCount = buildPointList_gpu(buf.edges, srcPoints); + //std::cout << "pointsCount: " << pointsCount << std::endl; + if (pointsCount == 0) + { + circles.release(); + return; + } + + // ensureSizeIsEnough(cvCeil(src.rows * idp) + 2, cvCeil(src.cols * idp) + 2, CV_32SC1, buf.accum); + // buf.accum.setTo(Scalar::all(0)); + + // circlesAccumCenters_gpu(srcPoints, pointsCount, buf.cannyBuf.dx, buf.cannyBuf.dy, buf.accum, minRadius, maxRadius, idp); + + // int centersCount = buildCentersList_gpu(buf.accum, centers, votesThreshold); + // if (centersCount == 0) + // { + // circles.release(); + // return; + // } + + // if (minDist > 1) + // { + // cv::AutoBuffer oldBuf_(centersCount); + // cv::AutoBuffer newBuf_(centersCount); + // int newCount = 0; + + // ushort2* oldBuf = oldBuf_; + // ushort2* newBuf = newBuf_; + + // cudaSafeCall( cudaMemcpy(oldBuf, centers, centersCount * sizeof(ushort2), cudaMemcpyDeviceToHost) ); + + // const int cellSize = cvRound(minDist); + // const int gridWidth = (src.cols + cellSize - 1) / cellSize; + // const int gridHeight = (src.rows + cellSize - 1) / cellSize; + + // std::vector< std::vector > grid(gridWidth * gridHeight); + + // const float minDist2 = minDist * minDist; + + // for (int i = 0; i < centersCount; ++i) + // { + // ushort2 p = oldBuf[i]; + + // bool good = true; + + // int xCell = static_cast(p.x / cellSize); + // int yCell = static_cast(p.y / cellSize); + + // int x1 = xCell - 1; + // int y1 = yCell - 1; + // int x2 = xCell + 1; + // int y2 = yCell + 1; + + // // boundary check + // x1 = std::max(0, x1); + // y1 = std::max(0, y1); + // x2 = std::min(gridWidth - 1, x2); + // y2 = std::min(gridHeight - 1, y2); + + // for (int yy = y1; yy <= y2; ++yy) + // { + // for (int xx = x1; xx <= x2; ++xx) + // { + // vector& m = grid[yy * gridWidth + xx]; + + // for(size_t j = 0; j < m.size(); ++j) + // { + // float dx = (float)(p.x - m[j].x); + // float dy = (float)(p.y - m[j].y); + + // if (dx * dx + dy * dy < minDist2) + // { + // good = false; + // goto break_out; + // } + // } + // } + // } + + // break_out: + + // if(good) + // { + // grid[yCell * gridWidth + xCell].push_back(p); + + // newBuf[newCount++] = p; + // } + // } + + // cudaSafeCall( cudaMemcpy(centers, newBuf, newCount * sizeof(unsigned int), cudaMemcpyHostToDevice) ); + // centersCount = newCount; + // } + + // ensureSizeIsEnough(1, maxCircles, CV_32FC3, circles); + + // DeviceInfo devInfo; + // const int circlesCount = circlesAccumRadius_gpu(centers, centersCount, srcPoints, pointsCount, circles.ptr(), maxCircles, + // dp, minRadius, maxRadius, votesThreshold, devInfo.supports(FEATURE_SET_COMPUTE_20)); + + // if (circlesCount > 0) + // circles.cols = circlesCount; + // else + // circles.release(); +} + +void cv::ocl::HoughCirclesDownload(const oclMat& d_circles, cv::OutputArray h_circles_) +{ + if (d_circles.empty()) + { + h_circles_.release(); + return; + } + + CV_Assert(d_circles.rows == 1 && d_circles.type() == CV_32FC3); + + h_circles_.create(1, d_circles.cols, CV_32FC3); + Mat h_circles = h_circles_.getMat(); + d_circles.download(h_circles); +} + +#endif /* !defined (HAVE_OPENCL) */ diff --git a/modules/ocl/src/kernels/hough.cl b/modules/ocl/src/kernels/hough.cl new file mode 100644 index 0000000000..e4eabc62d8 --- /dev/null +++ b/modules/ocl/src/kernels/hough.cl @@ -0,0 +1,307 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or bpied warranties, including, but not limited to, the bpied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable +#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable + +//////////////////////////////////////////////////////////////////////// +// buildPointList + +#define PIXELS_PER_THREAD 16 + +__kernel void buildPointList(__global const uchar* src, + int cols, + int rows, + int step, + __global unsigned int* list, + __global int* counter) +{ + __local unsigned int s_queues[4][32 * PIXELS_PER_THREAD]; + __local int s_qsize[4]; + __local int s_globStart[4]; + + const int x = get_group_id(0) * get_local_size(0) * PIXELS_PER_THREAD + get_local_id(0); + const int y = get_global_id(1); + + if (get_local_id(0) == 0) + s_qsize[get_local_id(1)] = 0; + barrier(CLK_LOCAL_MEM_FENCE); + + if (y < rows) + { + // fill the queue + __global const uchar* srcRow = &src[y * step]; + for (int i = 0, xx = x; i < PIXELS_PER_THREAD && xx < cols; ++i, xx += get_local_size(0)) + { + if (srcRow[xx]) + { + const unsigned int val = (y << 16) | xx; + const int qidx = atomic_add(&s_qsize[get_local_id(1)], 1); + s_queues[get_local_id(1)][qidx] = val; + } + } + } + + barrier(CLK_LOCAL_MEM_FENCE); + + // let one work-item reserve the space required in the global list + if (get_local_id(0) == 0 && get_local_id(1) == 0) + { + // find how many items are stored in each list + int totalSize = 0; + for (int i = 0; i < get_local_size(1); ++i) + { + s_globStart[i] = totalSize; + totalSize += s_qsize[i]; + } + + // calculate the offset in the global list + const int globalOffset = atomic_add(counter, totalSize); + for (int i = 0; i < get_local_size(1); ++i) + s_globStart[i] += globalOffset; + } + + barrier(CLK_GLOBAL_MEM_FENCE); + + // copy local queues to global queue + const int qsize = s_qsize[get_local_id(1)]; + int gidx = s_globStart[get_local_id(1)] + get_local_id(0); + for(int i = get_local_id(0); i < qsize; i += get_local_size(0), gidx += get_local_size(0)) + list[gidx] = s_queues[get_local_id(1)][i]; +} + +//////////////////////////////////////////////////////////////////////// +// circlesAccumCenters + +// __global__ void circlesAccumCenters(const unsigned int* list, const int count, const PtrStepi dx, const PtrStepi dy, +// PtrStepi accum, const int width, const int height, const int minRadius, const int maxRadius, const float idp) +// { +// const int SHIFT = 10; +// const int ONE = 1 << SHIFT; + +// const int tid = blockIdx.x * blockDim.x + threadIdx.x; + +// if (tid >= count) +// return; + +// const unsigned int val = list[tid]; + +// const int x = (val & 0xFFFF); +// const int y = (val >> 16) & 0xFFFF; + +// const int vx = dx(y, x); +// const int vy = dy(y, x); + +// if (vx == 0 && vy == 0) +// return; + +// const float mag = ::sqrtf(vx * vx + vy * vy); + +// const int x0 = __float2int_rn((x * idp) * ONE); +// const int y0 = __float2int_rn((y * idp) * ONE); + +// int sx = __float2int_rn((vx * idp) * ONE / mag); +// int sy = __float2int_rn((vy * idp) * ONE / mag); + +// // Step from minRadius to maxRadius in both directions of the gradient +// for (int k1 = 0; k1 < 2; ++k1) +// { +// int x1 = x0 + minRadius * sx; +// int y1 = y0 + minRadius * sy; + +// for (int r = minRadius; r <= maxRadius; x1 += sx, y1 += sy, ++r) +// { +// const int x2 = x1 >> SHIFT; +// const int y2 = y1 >> SHIFT; + +// if (x2 < 0 || x2 >= width || y2 < 0 || y2 >= height) +// break; + +// ::atomicAdd(accum.ptr(y2 + 1) + x2 + 1, 1); +// } + +// sx = -sx; +// sy = -sy; +// } +// } + +// void circlesAccumCenters_gpu(const unsigned int* list, int count, PtrStepi dx, PtrStepi dy, PtrStepSzi accum, int minRadius, int maxRadius, float idp) +// { +// const dim3 block(256); +// const dim3 grid(divUp(count, block.x)); + +// cudaSafeCall( cudaFuncSetCacheConfig(circlesAccumCenters, cudaFuncCachePreferL1) ); + +// circlesAccumCenters<<>>(list, count, dx, dy, accum, accum.cols - 2, accum.rows - 2, minRadius, maxRadius, idp); +// cudaSafeCall( cudaGetLastError() ); + +// cudaSafeCall( cudaDeviceSynchronize() ); +// } + +// //////////////////////////////////////////////////////////////////////// +// // buildCentersList + +// __global__ void buildCentersList(const PtrStepSzi accum, unsigned int* centers, const int threshold) +// { +// const int x = blockIdx.x * blockDim.x + threadIdx.x; +// const int y = blockIdx.y * blockDim.y + threadIdx.y; + +// if (x < accum.cols - 2 && y < accum.rows - 2) +// { +// const int top = accum(y, x + 1); + +// const int left = accum(y + 1, x); +// const int cur = accum(y + 1, x + 1); +// const int right = accum(y + 1, x + 2); + +// const int bottom = accum(y + 2, x + 1); + +// if (cur > threshold && cur > top && cur >= bottom && cur > left && cur >= right) +// { +// const unsigned int val = (y << 16) | x; +// const int idx = ::atomicAdd(&g_counter, 1); +// centers[idx] = val; +// } +// } +// } + +// int buildCentersList_gpu(PtrStepSzi accum, unsigned int* centers, int threshold) +// { +// void* counterPtr; +// cudaSafeCall( cudaGetSymbolAddress(&counterPtr, g_counter) ); + +// cudaSafeCall( cudaMemset(counterPtr, 0, sizeof(int)) ); + +// const dim3 block(32, 8); +// const dim3 grid(divUp(accum.cols - 2, block.x), divUp(accum.rows - 2, block.y)); + +// cudaSafeCall( cudaFuncSetCacheConfig(buildCentersList, cudaFuncCachePreferL1) ); + +// buildCentersList<<>>(accum, centers, threshold); +// cudaSafeCall( cudaGetLastError() ); + +// cudaSafeCall( cudaDeviceSynchronize() ); + +// int totalCount; +// cudaSafeCall( cudaMemcpy(&totalCount, counterPtr, sizeof(int), cudaMemcpyDeviceToHost) ); + +// return totalCount; +// } + +// //////////////////////////////////////////////////////////////////////// +// // circlesAccumRadius + +// __global__ void circlesAccumRadius(const unsigned int* centers, const unsigned int* list, const int count, +// float3* circles, const int maxCircles, const float dp, +// const int minRadius, const int maxRadius, const int histSize, const int threshold) +// { +// int* smem = DynamicSharedMem(); + +// for (int i = threadIdx.x; i < histSize + 2; i += blockDim.x) +// smem[i] = 0; +// __syncthreads(); + +// unsigned int val = centers[blockIdx.x]; + +// float cx = (val & 0xFFFF); +// float cy = (val >> 16) & 0xFFFF; + +// cx = (cx + 0.5f) * dp; +// cy = (cy + 0.5f) * dp; + +// for (int i = threadIdx.x; i < count; i += blockDim.x) +// { +// val = list[i]; + +// const int x = (val & 0xFFFF); +// const int y = (val >> 16) & 0xFFFF; + +// const float rad = ::sqrtf((cx - x) * (cx - x) + (cy - y) * (cy - y)); +// if (rad >= minRadius && rad <= maxRadius) +// { +// const int r = __float2int_rn(rad - minRadius); + +// Emulation::smem::atomicAdd(&smem[r + 1], 1); +// } +// } + +// __syncthreads(); + +// for (int i = threadIdx.x; i < histSize; i += blockDim.x) +// { +// const int curVotes = smem[i + 1]; + +// if (curVotes >= threshold && curVotes > smem[i] && curVotes >= smem[i + 2]) +// { +// const int ind = ::atomicAdd(&g_counter, 1); +// if (ind < maxCircles) +// circles[ind] = make_float3(cx, cy, i + minRadius); +// } +// } +// } + +// int circlesAccumRadius_gpu(const unsigned int* centers, int centersCount, const unsigned int* list, int count, +// float3* circles, int maxCircles, float dp, int minRadius, int maxRadius, int threshold, bool has20) +// { +// void* counterPtr; +// cudaSafeCall( cudaGetSymbolAddress(&counterPtr, g_counter) ); + +// cudaSafeCall( cudaMemset(counterPtr, 0, sizeof(int)) ); + +// const dim3 block(has20 ? 1024 : 512); +// const dim3 grid(centersCount); + +// const int histSize = maxRadius - minRadius + 1; +// size_t smemSize = (histSize + 2) * sizeof(int); + +// circlesAccumRadius<<>>(centers, list, count, circles, maxCircles, dp, minRadius, maxRadius, histSize, threshold); +// cudaSafeCall( cudaGetLastError() ); + +// cudaSafeCall( cudaDeviceSynchronize() ); + +// int totalCount; +// cudaSafeCall( cudaMemcpy(&totalCount, counterPtr, sizeof(int), cudaMemcpyDeviceToHost) ); + +// totalCount = ::min(totalCount, maxCircles); + +// return totalCount; +// } From 3a04cfedab1ac5a085e788b0745d64104ffd392c Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Thu, 13 Dec 2012 02:35:31 -0500 Subject: [PATCH 02/10] added HoughCircles in ocl.hpp --- modules/ocl/include/opencv2/ocl/ocl.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/ocl/include/opencv2/ocl/ocl.hpp b/modules/ocl/include/opencv2/ocl/ocl.hpp index 419b05b635..db24b38ec6 100644 --- a/modules/ocl/include/opencv2/ocl/ocl.hpp +++ b/modules/ocl/include/opencv2/ocl/ocl.hpp @@ -827,6 +827,21 @@ namespace cv }; + ///////////////////////////////////////// Hough Transform ///////////////////////////////////////// + //! HoughCircles + struct HoughCirclesBuf + { + oclMat edges; + oclMat accum; + oclMat list; + CannyBuf cannyBuf; + }; + + CV_EXPORTS void HoughCircles(const oclMat& src, oclMat& circles, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096); + CV_EXPORTS void HoughCircles(const oclMat& src, oclMat& circles, HoughCirclesBuf& buf, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096); + CV_EXPORTS void HoughCirclesDownload(const oclMat& d_circles, OutputArray h_circles); + + ///////////////////////////////////////// clAmdFft related ///////////////////////////////////////// //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix. //! Param dft_size is the size of DFT transform. From 0656f13107688974ca0283d5370e8595b234dd14 Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Thu, 13 Dec 2012 02:43:13 -0500 Subject: [PATCH 03/10] removed useless comments in buildPointList_gpu() --- modules/ocl/src/hough.cpp | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/modules/ocl/src/hough.cpp b/modules/ocl/src/hough.cpp index dd4db84a47..2adf20ea6b 100644 --- a/modules/ocl/src/hough.cpp +++ b/modules/ocl/src/hough.cpp @@ -88,22 +88,14 @@ namespace cv { namespace ocl { const int PIXELS_PER_THREAD = 16; - // void* counterPtr; - // cudaSafeCall( cudaGetSymbolAddress(&counterPtr, g_counter) ); - // cudaSafeCall( cudaMemset(counterPtr, 0, sizeof(int)) ); - int totalCount = 0; int err = CL_SUCCESS; cl_mem counter = clCreateBuffer(src.clCxt->impl->clContext, - CL_MEM_COPY_HOST_PTR, // CL_MEM_READ_WRITE, + CL_MEM_COPY_HOST_PTR, sizeof(int), - &totalCount, // NULL, + &totalCount, &err); openCLSafeCall(err); - // openCLSafeCall(clEnqueueWriteBuffer(src.clCxt->impl->clCmdQueue, counter, CL_TRUE, 0, sizeof(int), &totalCount, 0, 0, 0)); - - // const dim3 block(32, 4); - // const dim3 grid(divUp(src.cols, block.x * PIXELS_PER_THREAD), divUp(src.rows, block.y)); const size_t blkSizeX = 32; const size_t blkSizeY = 4; @@ -114,11 +106,6 @@ namespace cv { namespace ocl const size_t glbSizeY = src.rows % blkSizeY == 0 ? src.rows : (src.rows / blkSizeY + 1) * blkSizeY; size_t globalThreads[3] = { glbSizeX, glbSizeY, 1 }; - // cudaSafeCall( cudaFuncSetCacheConfig(buildPointList, cudaFuncCachePreferShared) ); - - // buildPointList<<>>(src, list); - // cudaSafeCall( cudaGetLastError() ); - // cudaSafeCall( cudaDeviceSynchronize() ); vector > args; args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data )); args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols )); @@ -128,8 +115,6 @@ namespace cv { namespace ocl args.push_back( make_pair( sizeof(cl_mem) , (void *)&counter )); openCLExecuteKernel(src.clCxt, &hough, "buildPointList", globalThreads, localThreads, args, -1, -1); - // int totalCount; - // cudaSafeCall( cudaMemcpy(&totalCount, counterPtr, sizeof(int), cudaMemcpyDeviceToHost) ); openCLSafeCall(clEnqueueReadBuffer(src.clCxt->impl->clCmdQueue, counter, CL_TRUE, 0, sizeof(int), &totalCount, 0, NULL, NULL)); openCLSafeCall(clReleaseMemObject(counter)); From 13c44dd3189d36eb83c8734cba876164073935fb Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Fri, 14 Dec 2012 03:25:46 -0500 Subject: [PATCH 04/10] finished ocl::HoughCircles --- modules/ocl/include/opencv2/ocl/ocl.hpp | 3 +- modules/ocl/src/hough.cpp | 470 +++++++++++++---------- modules/ocl/src/kernels/hough.cl | 307 --------------- modules/ocl/src/kernels/imgproc_hough.cl | 282 ++++++++++++++ 4 files changed, 544 insertions(+), 518 deletions(-) delete mode 100644 modules/ocl/src/kernels/hough.cl create mode 100644 modules/ocl/src/kernels/imgproc_hough.cl diff --git a/modules/ocl/include/opencv2/ocl/ocl.hpp b/modules/ocl/include/opencv2/ocl/ocl.hpp index db24b38ec6..ee5e7849db 100644 --- a/modules/ocl/include/opencv2/ocl/ocl.hpp +++ b/modules/ocl/include/opencv2/ocl/ocl.hpp @@ -833,7 +833,8 @@ namespace cv { oclMat edges; oclMat accum; - oclMat list; + oclMat srcPoints; + oclMat centers; CannyBuf cannyBuf; }; diff --git a/modules/ocl/src/hough.cpp b/modules/ocl/src/hough.cpp index 2adf20ea6b..9c51804566 100644 --- a/modules/ocl/src/hough.cpp +++ b/modules/ocl/src/hough.cpp @@ -50,31 +50,29 @@ using namespace cv::ocl; #if !defined (HAVE_OPENCL) -// void cv::ocl::HoughLines(const oclMat&, oclMat&, float, float, int, bool, int) { throw_nogpu(); } -// void cv::ocl::HoughLines(const oclMat&, oclMat&, HoughLinesBuf&, float, float, int, bool, int) { throw_nogpu(); } -// void cv::ocl::HoughLinesDownload(const oclMat&, OutputArray, OutputArray) { throw_nogpu(); } - void cv::ocl::HoughCircles(const oclMat&, oclMat&, int, float, float, int, int, int, int, int) { throw_nogpu(); } void cv::ocl::HoughCircles(const oclMat&, oclMat&, HoughCirclesBuf&, int, float, float, int, int, int, int, int) { throw_nogpu(); } void cv::ocl::HoughCirclesDownload(const oclMat&, OutputArray) { throw_nogpu(); } -// Ptr cv::ocl::GeneralizedHough_GPU::create(int) { throw_nogpu(); return Ptr(); } -// cv::ocl::GeneralizedHough_GPU::~GeneralizedHough_GPU() {} -// void cv::ocl::GeneralizedHough_GPU::setTemplate(const oclMat&, int, Point) { throw_nogpu(); } -// void cv::ocl::GeneralizedHough_GPU::setTemplate(const oclMat&, const oclMat&, const oclMat&, Point) { throw_nogpu(); } -// void cv::ocl::GeneralizedHough_GPU::detect(const oclMat&, oclMat&, int) { throw_nogpu(); } -// void cv::ocl::GeneralizedHough_GPU::detect(const oclMat&, const oclMat&, const oclMat&, oclMat&) { throw_nogpu(); } -// void cv::ocl::GeneralizedHough_GPU::download(const oclMat&, OutputArray, OutputArray) { throw_nogpu(); } -// void cv::ocl::GeneralizedHough_GPU::release() {} - #else /* !defined (HAVE_OPENCL) */ -namespace cv { namespace ocl -{ - int buildPointList_gpu(const oclMat& src, unsigned int* list); +#define MUL_UP(a, b) ((a)/(b)+1)*(b) +namespace cv { namespace ocl { ///////////////////////////OpenCL kernel strings/////////////////////////// - extern const char *hough; + extern const char *imgproc_hough; + + namespace hough + { + int buildPointList_gpu(const oclMat& src, oclMat& list); + void circlesAccumCenters_gpu(const unsigned int* list, int count, const oclMat& dx, const oclMat& dy, oclMat& accum, int minRadius, int maxRadius, float idp); + int buildCentersList_gpu(const oclMat& accum, oclMat& centers, int threshold); + + int circlesAccumRadius_gpu(const oclMat& centers, int centersCount, + const oclMat& list, int count, + oclMat& circles, int maxCircles, + float dp, int minRadius, int maxRadius, int threshold); + } }} @@ -82,9 +80,9 @@ namespace cv { namespace ocl ////////////////////////////////////////////////////////// // common functions -namespace cv { namespace ocl +namespace cv { namespace ocl { namespace hough { - int buildPointList_gpu(const oclMat& src, unsigned int* list) + int buildPointList_gpu(const oclMat& src, oclMat& list) { const int PIXELS_PER_THREAD = 16; @@ -102,8 +100,8 @@ namespace cv { namespace ocl size_t localThreads[3] = { blkSizeX, blkSizeY, 1 }; const int PIXELS_PER_BLOCK = blkSizeX * PIXELS_PER_THREAD; - const size_t glbSizeX = src.cols % (PIXELS_PER_BLOCK) == 0 ? src.cols : (src.cols / PIXELS_PER_BLOCK + 1) * PIXELS_PER_BLOCK; - const size_t glbSizeY = src.rows % blkSizeY == 0 ? src.rows : (src.rows / blkSizeY + 1) * blkSizeY; + const size_t glbSizeX = src.cols % (PIXELS_PER_BLOCK) == 0 ? src.cols : MUL_UP(src.cols, PIXELS_PER_BLOCK); + const size_t glbSizeY = src.rows % blkSizeY == 0 ? src.rows : MUL_UP(src.rows, blkSizeY); size_t globalThreads[3] = { glbSizeX, glbSizeY, 1 }; vector > args; @@ -111,110 +109,141 @@ namespace cv { namespace ocl args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols )); args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows )); args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step )); - args.push_back( make_pair( sizeof(cl_mem) , (void *)&list )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&list.data )); args.push_back( make_pair( sizeof(cl_mem) , (void *)&counter )); - openCLExecuteKernel(src.clCxt, &hough, "buildPointList", globalThreads, localThreads, args, -1, -1); + openCLExecuteKernel(src.clCxt, &imgproc_hough, "buildPointList", globalThreads, localThreads, args, -1, -1); openCLSafeCall(clEnqueueReadBuffer(src.clCxt->impl->clCmdQueue, counter, CL_TRUE, 0, sizeof(int), &totalCount, 0, NULL, NULL)); openCLSafeCall(clReleaseMemObject(counter)); return totalCount; } -}} - -////////////////////////////////////////////////////////// -// HoughLines - -// namespace cv { namespace ocl { namespace device -// { -// namespace hough -// { -// void linesAccum_gpu(const unsigned int* list, int count, PtrStepSzi accum, float rho, float theta, size_t sharedMemPerBlock, bool has20); -// int linesGetResult_gpu(PtrStepSzi accum, float2* out, int* votes, int maxSize, float rho, float theta, int threshold, bool doSort); -// } -// }}} - -// void cv::ocl::HoughLines(const oclMat& src, oclMat& lines, float rho, float theta, int threshold, bool doSort, int maxLines) -// { -// HoughLinesBuf buf; -// HoughLines(src, lines, buf, rho, theta, threshold, doSort, maxLines); -// } - -// void cv::ocl::HoughLines(const oclMat& src, oclMat& lines, HoughLinesBuf& buf, float rho, float theta, int threshold, bool doSort, int maxLines) -// { -// using namespace cv::ocl::device::hough; - -// CV_Assert(src.type() == CV_8UC1); -// CV_Assert(src.cols < std::numeric_limits::max()); -// CV_Assert(src.rows < std::numeric_limits::max()); - -// ensureSizeIsEnough(1, src.size().area(), CV_32SC1, buf.list); -// unsigned int* srcPoints = buf.list.ptr(); - -// const int pointsCount = buildPointList_gpu(src, srcPoints); -// if (pointsCount == 0) -// { -// lines.release(); -// return; -// } - -// const int numangle = cvRound(CV_PI / theta); -// const int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho); -// CV_Assert(numangle > 0 && numrho > 0); - -// ensureSizeIsEnough(numangle + 2, numrho + 2, CV_32SC1, buf.accum); -// buf.accum.setTo(Scalar::all(0)); - -// DeviceInfo devInfo; -// linesAccum_gpu(srcPoints, pointsCount, buf.accum, rho, theta, devInfo.sharedMemPerBlock(), devInfo.supports(FEATURE_SET_COMPUTE_20)); - -// ensureSizeIsEnough(2, maxLines, CV_32FC2, lines); - -// int linesCount = linesGetResult_gpu(buf.accum, lines.ptr(0), lines.ptr(1), maxLines, rho, theta, threshold, doSort); -// if (linesCount > 0) -// lines.cols = linesCount; -// else -// lines.release(); -// } - -// void cv::ocl::HoughLinesDownload(const oclMat& d_lines, OutputArray h_lines_, OutputArray h_votes_) -// { -// if (d_lines.empty()) -// { -// h_lines_.release(); -// if (h_votes_.needed()) -// h_votes_.release(); -// return; -// } - -// CV_Assert(d_lines.rows == 2 && d_lines.type() == CV_32FC2); - -// h_lines_.create(1, d_lines.cols, CV_32FC2); -// Mat h_lines = h_lines_.getMat(); -// d_lines.row(0).download(h_lines); - -// if (h_votes_.needed()) -// { -// h_votes_.create(1, d_lines.cols, CV_32SC1); -// Mat h_votes = h_votes_.getMat(); -// oclMat d_votes(1, d_lines.cols, CV_32SC1, const_cast(d_lines.ptr(1))); -// d_votes.download(h_votes); -// } -// } +}}} ////////////////////////////////////////////////////////// // HoughCircles -// namespace cv { namespace ocl -// { -// namespace hough -// { -// void circlesAccumCenters_gpu(const unsigned int* list, int count, PtrStepi dx, PtrStepi dy, PtrStepSzi accum, int minRadius, int maxRadius, float idp); -// int buildCentersList_gpu(PtrStepSzi accum, unsigned int* centers, int threshold); -// int circlesAccumRadius_gpu(const unsigned int* centers, int centersCount, const unsigned int* list, int count, -// float3* circles, int maxCircles, float dp, int minRadius, int maxRadius, int threshold, bool has20); -// } -// }} +namespace cv { namespace ocl { namespace hough +{ + void circlesAccumCenters_gpu(const oclMat& list, int count, const oclMat& dx, const oclMat& dy, oclMat& accum, int minRadius, int maxRadius, float idp) + { + const size_t blkSizeX = 256; + size_t localThreads[3] = { 256, 1, 1 }; + + const size_t glbSizeX = count % blkSizeX == 0 ? count : MUL_UP(count, blkSizeX); + size_t globalThreads[3] = { glbSizeX, 1, 1 }; + + const int width = accum.cols - 2; + const int height = accum.rows - 2; + + vector > args; + args.push_back( make_pair( sizeof(cl_mem) , (void *)&list.data )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&count )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&dx.data )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&dx.step )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&dy.data )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&dy.step )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&accum.data )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&accum.step )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&width )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&height )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&minRadius)); + args.push_back( make_pair( sizeof(cl_int) , (void *)&maxRadius)); + args.push_back( make_pair( sizeof(cl_float), (void *)&idp)); + + openCLExecuteKernel(accum.clCxt, &imgproc_hough, "circlesAccumCenters", globalThreads, localThreads, args, -1, -1); + } + + int buildCentersList_gpu(const oclMat& accum, oclMat& centers, int threshold) + { + int totalCount = 0; + int err = CL_SUCCESS; + cl_mem counter = clCreateBuffer(accum.clCxt->impl->clContext, + CL_MEM_COPY_HOST_PTR, + sizeof(int), + &totalCount, + &err); + openCLSafeCall(err); + + const size_t blkSizeX = 32; + const size_t blkSizeY = 8; + size_t localThreads[3] = { blkSizeX, blkSizeY, 1 }; + + const size_t glbSizeX = (accum.cols - 2) % blkSizeX == 0 ? accum.cols - 2 : MUL_UP(accum.cols - 2, blkSizeX); + const size_t glbSizeY = (accum.rows - 2) % blkSizeY == 0 ? accum.rows - 2 : MUL_UP(accum.rows - 2, blkSizeY); + size_t globalThreads[3] = { glbSizeX, glbSizeY, 1 }; + + vector > args; + args.push_back( make_pair( sizeof(cl_mem) , (void *)&accum.data )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&accum.cols )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&accum.rows )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&accum.step )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)¢ers.data )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&threshold )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&counter )); + + openCLExecuteKernel(accum.clCxt, &imgproc_hough, "buildCentersList", globalThreads, localThreads, args, -1, -1); + + openCLSafeCall(clEnqueueReadBuffer(accum.clCxt->impl->clCmdQueue, counter, CL_TRUE, 0, sizeof(int), &totalCount, 0, NULL, NULL)); + openCLSafeCall(clReleaseMemObject(counter)); + + return totalCount; + } + + int circlesAccumRadius_gpu(const oclMat& centers, int centersCount, + const oclMat& list, int count, + oclMat& circles, int maxCircles, + float dp, int minRadius, int maxRadius, int threshold) + { + int totalCount = 0; + int err = CL_SUCCESS; + cl_mem counter = clCreateBuffer(circles.clCxt->impl->clContext, + CL_MEM_COPY_HOST_PTR, + sizeof(int), + &totalCount, + &err); + openCLSafeCall(err); + + const size_t blkSizeX = circles.clCxt->impl->maxWorkGroupSize; + size_t localThreads[3] = { blkSizeX, 1, 1 }; + + const size_t glbSizeX = centersCount * blkSizeX; + size_t globalThreads[3] = { glbSizeX, 1, 1 }; + + const int histSize = maxRadius - minRadius + 1; + size_t smemSize = (histSize + 2) * sizeof(int); + + vector > args; + args.push_back( make_pair( sizeof(cl_mem) , (void *)¢ers.data )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&list.data )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&count )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&circles.data )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&maxCircles )); + args.push_back( make_pair( sizeof(cl_float), (void *)&dp )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&minRadius )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&maxRadius )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&histSize )); + args.push_back( make_pair( sizeof(cl_int) , (void *)&threshold )); + args.push_back( make_pair( smemSize , (void *)NULL )); + args.push_back( make_pair( sizeof(cl_mem) , (void *)&counter )); + + CV_Assert(circles.offset == 0); + + openCLExecuteKernel(circles.clCxt, &imgproc_hough, "circlesAccumRadius", globalThreads, localThreads, args, -1, -1); + + openCLSafeCall(clEnqueueReadBuffer(circles.clCxt->impl->clCmdQueue, counter, CL_TRUE, 0, sizeof(int), &totalCount, 0, NULL, NULL)); + + openCLSafeCall(clReleaseMemObject(counter)); + + totalCount = ::min(totalCount, maxCircles); + + return totalCount; + } + + +}}} // namespace cv { namespace ocl { namespace hough + + void cv::ocl::HoughCircles(const oclMat& src, oclMat& circles, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles) { @@ -239,119 +268,140 @@ void cv::ocl::HoughCircles(const oclMat& src, oclMat& circles, HoughCirclesBuf& cv::ocl::Canny(src, buf.cannyBuf, buf.edges, std::max(cannyThreshold / 2, 1), cannyThreshold); - ensureSizeIsEnough(2, src.size().area(), CV_32SC1, buf.list); - // unsigned int* srcPoints = buf.list.ptr(0); - unsigned int* srcPoints = (unsigned int*)buf.list.data; - // unsigned int* centers = buf.list.ptr(1); - unsigned int* centers = (unsigned int*)buf.list.data + buf.list.step; - - const int pointsCount = buildPointList_gpu(buf.edges, srcPoints); - //std::cout << "pointsCount: " << pointsCount << std::endl; + ensureSizeIsEnough(1, src.size().area(), CV_32SC1, buf.srcPoints); + const int pointsCount = hough::buildPointList_gpu(buf.edges, buf.srcPoints); if (pointsCount == 0) { circles.release(); return; } - // ensureSizeIsEnough(cvCeil(src.rows * idp) + 2, cvCeil(src.cols * idp) + 2, CV_32SC1, buf.accum); - // buf.accum.setTo(Scalar::all(0)); - - // circlesAccumCenters_gpu(srcPoints, pointsCount, buf.cannyBuf.dx, buf.cannyBuf.dy, buf.accum, minRadius, maxRadius, idp); - - // int centersCount = buildCentersList_gpu(buf.accum, centers, votesThreshold); - // if (centersCount == 0) - // { - // circles.release(); - // return; - // } + ensureSizeIsEnough(cvCeil(src.rows * idp) + 2, cvCeil(src.cols * idp) + 2, CV_32SC1, buf.accum); + buf.accum.setTo(Scalar::all(0)); - // if (minDist > 1) - // { - // cv::AutoBuffer oldBuf_(centersCount); - // cv::AutoBuffer newBuf_(centersCount); - // int newCount = 0; + hough::circlesAccumCenters_gpu(buf.srcPoints, pointsCount, buf.cannyBuf.dx, buf.cannyBuf.dy, buf.accum, minRadius, maxRadius, idp); - // ushort2* oldBuf = oldBuf_; - // ushort2* newBuf = newBuf_; - - // cudaSafeCall( cudaMemcpy(oldBuf, centers, centersCount * sizeof(ushort2), cudaMemcpyDeviceToHost) ); - - // const int cellSize = cvRound(minDist); - // const int gridWidth = (src.cols + cellSize - 1) / cellSize; - // const int gridHeight = (src.rows + cellSize - 1) / cellSize; - - // std::vector< std::vector > grid(gridWidth * gridHeight); - - // const float minDist2 = minDist * minDist; - - // for (int i = 0; i < centersCount; ++i) - // { - // ushort2 p = oldBuf[i]; - - // bool good = true; - - // int xCell = static_cast(p.x / cellSize); - // int yCell = static_cast(p.y / cellSize); - - // int x1 = xCell - 1; - // int y1 = yCell - 1; - // int x2 = xCell + 1; - // int y2 = yCell + 1; - - // // boundary check - // x1 = std::max(0, x1); - // y1 = std::max(0, y1); - // x2 = std::min(gridWidth - 1, x2); - // y2 = std::min(gridHeight - 1, y2); - - // for (int yy = y1; yy <= y2; ++yy) - // { - // for (int xx = x1; xx <= x2; ++xx) - // { - // vector& m = grid[yy * gridWidth + xx]; - - // for(size_t j = 0; j < m.size(); ++j) - // { - // float dx = (float)(p.x - m[j].x); - // float dy = (float)(p.y - m[j].y); - - // if (dx * dx + dy * dy < minDist2) - // { - // good = false; - // goto break_out; - // } - // } - // } - // } - - // break_out: - - // if(good) - // { - // grid[yCell * gridWidth + xCell].push_back(p); + ensureSizeIsEnough(1, src.size().area(), CV_32SC1, buf.centers); + int centersCount = hough::buildCentersList_gpu(buf.accum, buf.centers, votesThreshold); + if (centersCount == 0) + { + circles.release(); + return; + } - // newBuf[newCount++] = p; - // } - // } + if (minDist > 1) + { + cv::AutoBuffer oldBuf_(centersCount); + cv::AutoBuffer newBuf_(centersCount); + int newCount = 0; + + unsigned int* oldBuf = oldBuf_; + unsigned int* newBuf = newBuf_; + + openCLSafeCall(clEnqueueReadBuffer(buf.centers.clCxt->impl->clCmdQueue, + (cl_mem)buf.centers.data, + CL_TRUE, + 0, + centersCount * sizeof(unsigned int), + oldBuf, + 0, + NULL, + NULL)); - // cudaSafeCall( cudaMemcpy(centers, newBuf, newCount * sizeof(unsigned int), cudaMemcpyHostToDevice) ); - // centersCount = newCount; - // } + + const int cellSize = cvRound(minDist); + const int gridWidth = (src.cols + cellSize - 1) / cellSize; + const int gridHeight = (src.rows + cellSize - 1) / cellSize; + + std::vector< std::vector > grid(gridWidth * gridHeight); + + const float minDist2 = minDist * minDist; + + for (int i = 0; i < centersCount; ++i) + { + unsigned int p = oldBuf[i]; + const int px = p & 0xFFFF; + const int py = (p >> 16) & 0xFFFF; + + bool good = true; + + int xCell = static_cast(px / cellSize); + int yCell = static_cast(py / cellSize); + + int x1 = xCell - 1; + int y1 = yCell - 1; + int x2 = xCell + 1; + int y2 = yCell + 1; + + // boundary check + x1 = std::max(0, x1); + y1 = std::max(0, y1); + x2 = std::min(gridWidth - 1, x2); + y2 = std::min(gridHeight - 1, y2); + + for (int yy = y1; yy <= y2; ++yy) + { + for (int xx = x1; xx <= x2; ++xx) + { + vector& m = grid[yy * gridWidth + xx]; + + for(size_t j = 0; j < m.size(); ++j) + { + const int val = m[j]; + const int jx = val & 0xFFFF; + const int jy = (val >> 16) & 0xFFFF; + + float dx = (float)(px - jx); + float dy = (float)(py - jy); + + if (dx * dx + dy * dy < minDist2) + { + good = false; + goto break_out; + } + } + } + } + + break_out: + + if(good) + { + grid[yCell * gridWidth + xCell].push_back(p); + newBuf[newCount++] = p; + } + } + + openCLSafeCall(clEnqueueWriteBuffer(buf.centers.clCxt->impl->clCmdQueue, + (cl_mem)buf.centers.data, + CL_TRUE, + 0, + newCount * sizeof(unsigned int), + newBuf, + 0, + 0, + 0)); + centersCount = newCount; + } - // ensureSizeIsEnough(1, maxCircles, CV_32FC3, circles); + ensureSizeIsEnough(1, maxCircles, CV_32FC3, circles); - // DeviceInfo devInfo; - // const int circlesCount = circlesAccumRadius_gpu(centers, centersCount, srcPoints, pointsCount, circles.ptr(), maxCircles, - // dp, minRadius, maxRadius, votesThreshold, devInfo.supports(FEATURE_SET_COMPUTE_20)); + const int circlesCount = hough::circlesAccumRadius_gpu(buf.centers, centersCount, + buf.srcPoints, pointsCount, + circles, maxCircles, + dp, minRadius, maxRadius, votesThreshold); - // if (circlesCount > 0) - // circles.cols = circlesCount; - // else - // circles.release(); + if (circlesCount > 0) + circles.cols = circlesCount; + else + circles.release(); } void cv::ocl::HoughCirclesDownload(const oclMat& d_circles, cv::OutputArray h_circles_) { + // FIX ME: garbage values are copied! + CV_Error(CV_StsNotImplemented, "HoughCirclesDownload is not implemented"); + if (d_circles.empty()) { h_circles_.release(); diff --git a/modules/ocl/src/kernels/hough.cl b/modules/ocl/src/kernels/hough.cl deleted file mode 100644 index e4eabc62d8..0000000000 --- a/modules/ocl/src/kernels/hough.cl +++ /dev/null @@ -1,307 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or bpied warranties, including, but not limited to, the bpied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable -#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable - -//////////////////////////////////////////////////////////////////////// -// buildPointList - -#define PIXELS_PER_THREAD 16 - -__kernel void buildPointList(__global const uchar* src, - int cols, - int rows, - int step, - __global unsigned int* list, - __global int* counter) -{ - __local unsigned int s_queues[4][32 * PIXELS_PER_THREAD]; - __local int s_qsize[4]; - __local int s_globStart[4]; - - const int x = get_group_id(0) * get_local_size(0) * PIXELS_PER_THREAD + get_local_id(0); - const int y = get_global_id(1); - - if (get_local_id(0) == 0) - s_qsize[get_local_id(1)] = 0; - barrier(CLK_LOCAL_MEM_FENCE); - - if (y < rows) - { - // fill the queue - __global const uchar* srcRow = &src[y * step]; - for (int i = 0, xx = x; i < PIXELS_PER_THREAD && xx < cols; ++i, xx += get_local_size(0)) - { - if (srcRow[xx]) - { - const unsigned int val = (y << 16) | xx; - const int qidx = atomic_add(&s_qsize[get_local_id(1)], 1); - s_queues[get_local_id(1)][qidx] = val; - } - } - } - - barrier(CLK_LOCAL_MEM_FENCE); - - // let one work-item reserve the space required in the global list - if (get_local_id(0) == 0 && get_local_id(1) == 0) - { - // find how many items are stored in each list - int totalSize = 0; - for (int i = 0; i < get_local_size(1); ++i) - { - s_globStart[i] = totalSize; - totalSize += s_qsize[i]; - } - - // calculate the offset in the global list - const int globalOffset = atomic_add(counter, totalSize); - for (int i = 0; i < get_local_size(1); ++i) - s_globStart[i] += globalOffset; - } - - barrier(CLK_GLOBAL_MEM_FENCE); - - // copy local queues to global queue - const int qsize = s_qsize[get_local_id(1)]; - int gidx = s_globStart[get_local_id(1)] + get_local_id(0); - for(int i = get_local_id(0); i < qsize; i += get_local_size(0), gidx += get_local_size(0)) - list[gidx] = s_queues[get_local_id(1)][i]; -} - -//////////////////////////////////////////////////////////////////////// -// circlesAccumCenters - -// __global__ void circlesAccumCenters(const unsigned int* list, const int count, const PtrStepi dx, const PtrStepi dy, -// PtrStepi accum, const int width, const int height, const int minRadius, const int maxRadius, const float idp) -// { -// const int SHIFT = 10; -// const int ONE = 1 << SHIFT; - -// const int tid = blockIdx.x * blockDim.x + threadIdx.x; - -// if (tid >= count) -// return; - -// const unsigned int val = list[tid]; - -// const int x = (val & 0xFFFF); -// const int y = (val >> 16) & 0xFFFF; - -// const int vx = dx(y, x); -// const int vy = dy(y, x); - -// if (vx == 0 && vy == 0) -// return; - -// const float mag = ::sqrtf(vx * vx + vy * vy); - -// const int x0 = __float2int_rn((x * idp) * ONE); -// const int y0 = __float2int_rn((y * idp) * ONE); - -// int sx = __float2int_rn((vx * idp) * ONE / mag); -// int sy = __float2int_rn((vy * idp) * ONE / mag); - -// // Step from minRadius to maxRadius in both directions of the gradient -// for (int k1 = 0; k1 < 2; ++k1) -// { -// int x1 = x0 + minRadius * sx; -// int y1 = y0 + minRadius * sy; - -// for (int r = minRadius; r <= maxRadius; x1 += sx, y1 += sy, ++r) -// { -// const int x2 = x1 >> SHIFT; -// const int y2 = y1 >> SHIFT; - -// if (x2 < 0 || x2 >= width || y2 < 0 || y2 >= height) -// break; - -// ::atomicAdd(accum.ptr(y2 + 1) + x2 + 1, 1); -// } - -// sx = -sx; -// sy = -sy; -// } -// } - -// void circlesAccumCenters_gpu(const unsigned int* list, int count, PtrStepi dx, PtrStepi dy, PtrStepSzi accum, int minRadius, int maxRadius, float idp) -// { -// const dim3 block(256); -// const dim3 grid(divUp(count, block.x)); - -// cudaSafeCall( cudaFuncSetCacheConfig(circlesAccumCenters, cudaFuncCachePreferL1) ); - -// circlesAccumCenters<<>>(list, count, dx, dy, accum, accum.cols - 2, accum.rows - 2, minRadius, maxRadius, idp); -// cudaSafeCall( cudaGetLastError() ); - -// cudaSafeCall( cudaDeviceSynchronize() ); -// } - -// //////////////////////////////////////////////////////////////////////// -// // buildCentersList - -// __global__ void buildCentersList(const PtrStepSzi accum, unsigned int* centers, const int threshold) -// { -// const int x = blockIdx.x * blockDim.x + threadIdx.x; -// const int y = blockIdx.y * blockDim.y + threadIdx.y; - -// if (x < accum.cols - 2 && y < accum.rows - 2) -// { -// const int top = accum(y, x + 1); - -// const int left = accum(y + 1, x); -// const int cur = accum(y + 1, x + 1); -// const int right = accum(y + 1, x + 2); - -// const int bottom = accum(y + 2, x + 1); - -// if (cur > threshold && cur > top && cur >= bottom && cur > left && cur >= right) -// { -// const unsigned int val = (y << 16) | x; -// const int idx = ::atomicAdd(&g_counter, 1); -// centers[idx] = val; -// } -// } -// } - -// int buildCentersList_gpu(PtrStepSzi accum, unsigned int* centers, int threshold) -// { -// void* counterPtr; -// cudaSafeCall( cudaGetSymbolAddress(&counterPtr, g_counter) ); - -// cudaSafeCall( cudaMemset(counterPtr, 0, sizeof(int)) ); - -// const dim3 block(32, 8); -// const dim3 grid(divUp(accum.cols - 2, block.x), divUp(accum.rows - 2, block.y)); - -// cudaSafeCall( cudaFuncSetCacheConfig(buildCentersList, cudaFuncCachePreferL1) ); - -// buildCentersList<<>>(accum, centers, threshold); -// cudaSafeCall( cudaGetLastError() ); - -// cudaSafeCall( cudaDeviceSynchronize() ); - -// int totalCount; -// cudaSafeCall( cudaMemcpy(&totalCount, counterPtr, sizeof(int), cudaMemcpyDeviceToHost) ); - -// return totalCount; -// } - -// //////////////////////////////////////////////////////////////////////// -// // circlesAccumRadius - -// __global__ void circlesAccumRadius(const unsigned int* centers, const unsigned int* list, const int count, -// float3* circles, const int maxCircles, const float dp, -// const int minRadius, const int maxRadius, const int histSize, const int threshold) -// { -// int* smem = DynamicSharedMem(); - -// for (int i = threadIdx.x; i < histSize + 2; i += blockDim.x) -// smem[i] = 0; -// __syncthreads(); - -// unsigned int val = centers[blockIdx.x]; - -// float cx = (val & 0xFFFF); -// float cy = (val >> 16) & 0xFFFF; - -// cx = (cx + 0.5f) * dp; -// cy = (cy + 0.5f) * dp; - -// for (int i = threadIdx.x; i < count; i += blockDim.x) -// { -// val = list[i]; - -// const int x = (val & 0xFFFF); -// const int y = (val >> 16) & 0xFFFF; - -// const float rad = ::sqrtf((cx - x) * (cx - x) + (cy - y) * (cy - y)); -// if (rad >= minRadius && rad <= maxRadius) -// { -// const int r = __float2int_rn(rad - minRadius); - -// Emulation::smem::atomicAdd(&smem[r + 1], 1); -// } -// } - -// __syncthreads(); - -// for (int i = threadIdx.x; i < histSize; i += blockDim.x) -// { -// const int curVotes = smem[i + 1]; - -// if (curVotes >= threshold && curVotes > smem[i] && curVotes >= smem[i + 2]) -// { -// const int ind = ::atomicAdd(&g_counter, 1); -// if (ind < maxCircles) -// circles[ind] = make_float3(cx, cy, i + minRadius); -// } -// } -// } - -// int circlesAccumRadius_gpu(const unsigned int* centers, int centersCount, const unsigned int* list, int count, -// float3* circles, int maxCircles, float dp, int minRadius, int maxRadius, int threshold, bool has20) -// { -// void* counterPtr; -// cudaSafeCall( cudaGetSymbolAddress(&counterPtr, g_counter) ); - -// cudaSafeCall( cudaMemset(counterPtr, 0, sizeof(int)) ); - -// const dim3 block(has20 ? 1024 : 512); -// const dim3 grid(centersCount); - -// const int histSize = maxRadius - minRadius + 1; -// size_t smemSize = (histSize + 2) * sizeof(int); - -// circlesAccumRadius<<>>(centers, list, count, circles, maxCircles, dp, minRadius, maxRadius, histSize, threshold); -// cudaSafeCall( cudaGetLastError() ); - -// cudaSafeCall( cudaDeviceSynchronize() ); - -// int totalCount; -// cudaSafeCall( cudaMemcpy(&totalCount, counterPtr, sizeof(int), cudaMemcpyDeviceToHost) ); - -// totalCount = ::min(totalCount, maxCircles); - -// return totalCount; -// } diff --git a/modules/ocl/src/kernels/imgproc_hough.cl b/modules/ocl/src/kernels/imgproc_hough.cl new file mode 100644 index 0000000000..42210e39a4 --- /dev/null +++ b/modules/ocl/src/kernels/imgproc_hough.cl @@ -0,0 +1,282 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Modified by Seunghoon Park(pclove1@gmail.com) +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or bpied warranties, including, but not limited to, the bpied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable +#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable + +//////////////////////////////////////////////////////////////////////// +// buildPointList + +#define PIXELS_PER_THREAD 16 + +// TODO: add offset to support ROI +__kernel void buildPointList(__global const uchar* src, + int cols, + int rows, + int step, + __global unsigned int* list, + __global int* counter) +{ + __local unsigned int s_queues[4][32 * PIXELS_PER_THREAD]; + __local int s_qsize[4]; + __local int s_globStart[4]; + + const int x = get_group_id(0) * get_local_size(0) * PIXELS_PER_THREAD + get_local_id(0); + const int y = get_global_id(1); + + if (get_local_id(0) == 0) + s_qsize[get_local_id(1)] = 0; + barrier(CLK_LOCAL_MEM_FENCE); + + if (y < rows) + { + // fill the queue + __global const uchar* srcRow = &src[y * step]; + for (int i = 0, xx = x; i < PIXELS_PER_THREAD && xx < cols; ++i, xx += get_local_size(0)) + { + if (srcRow[xx]) + { + const unsigned int val = (y << 16) | xx; + const int qidx = atomic_add(&s_qsize[get_local_id(1)], 1); + s_queues[get_local_id(1)][qidx] = val; + } + } + } + + barrier(CLK_LOCAL_MEM_FENCE); + + // let one work-item reserve the space required in the global list + if (get_local_id(0) == 0 && get_local_id(1) == 0) + { + // find how many items are stored in each list + int totalSize = 0; + for (int i = 0; i < get_local_size(1); ++i) + { + s_globStart[i] = totalSize; + totalSize += s_qsize[i]; + } + + // calculate the offset in the global list + const int globalOffset = atomic_add(counter, totalSize); + for (int i = 0; i < get_local_size(1); ++i) + s_globStart[i] += globalOffset; + } + + barrier(CLK_GLOBAL_MEM_FENCE); + + // copy local queues to global queue + const int qsize = s_qsize[get_local_id(1)]; + int gidx = s_globStart[get_local_id(1)] + get_local_id(0); + for(int i = get_local_id(0); i < qsize; i += get_local_size(0), gidx += get_local_size(0)) + list[gidx] = s_queues[get_local_id(1)][i]; +} + +//////////////////////////////////////////////////////////////////////// +// circlesAccumCenters + +// TODO: add offset to support ROI +__kernel void circlesAccumCenters(__global const unsigned int* list, + const int count, + __global const int* dx, + const int dxStep, + __global const int* dy, + const int dyStep, + __global int* accum, + const int accumStep, + const int width, + const int height, + const int minRadius, + const int maxRadius, + const float idp) +{ + const int dxStepInPixel = dxStep / sizeof(int); + const int dyStepInPixel = dyStep / sizeof(int); + const int accumStepInPixel = accumStep / sizeof(int); + + const int SHIFT = 10; + const int ONE = 1 << SHIFT; + + // const int tid = blockIdx.x * blockDim.x + threadIdx.x; + const int wid = get_global_id(0); + + if (wid >= count) + return; + + const unsigned int val = list[wid]; + + const int x = (val & 0xFFFF); + const int y = (val >> 16) & 0xFFFF; + + const int vx = dx[mad24(y, dxStepInPixel, x)]; + const int vy = dy[mad24(y, dyStepInPixel, x)]; + + if (vx == 0 && vy == 0) + return; + + const float mag = sqrt(convert_float(vx * vx + vy * vy)); + + const int x0 = convert_int_rte((x * idp) * ONE); + const int y0 = convert_int_rte((y * idp) * ONE); + + int sx = convert_int_rte((vx * idp) * ONE / mag); + int sy = convert_int_rte((vy * idp) * ONE / mag); + + // Step from minRadius to maxRadius in both directions of the gradient + for (int k1 = 0; k1 < 2; ++k1) + { + int x1 = x0 + minRadius * sx; + int y1 = y0 + minRadius * sy; + + for (int r = minRadius; r <= maxRadius; x1 += sx, y1 += sy, ++r) + { + const int x2 = x1 >> SHIFT; + const int y2 = y1 >> SHIFT; + + if (x2 < 0 || x2 >= width || y2 < 0 || y2 >= height) + break; + + atomic_add(&accum[mad24(y2+1, accumStepInPixel, x2+1)], 1); + } + + sx = -sx; + sy = -sy; + } +} + +// //////////////////////////////////////////////////////////////////////// +// // buildCentersList + +// TODO: add offset to support ROI +__kernel void buildCentersList(__global const int* accum, + const int accumCols, + const int accumRows, + const int accumStep, + __global unsigned int* centers, + const int threshold, + __global int* counter) +{ + const int accumStepInPixel = accumStep/sizeof(int); + + const int x = get_global_id(0); + const int y = get_global_id(1); + + if (x < accumCols - 2 && y < accumRows - 2) + { + const int top = accum[mad24(y, accumStepInPixel, x + 1)]; + + const int left = accum[mad24(y + 1, accumStepInPixel, x)]; + const int cur = accum[mad24(y + 1, accumStepInPixel, x + 1)]; + const int right = accum[mad24(y + 1, accumStepInPixel, x + 2)]; + + const int bottom = accum[mad24(y + 2, accumStepInPixel, x + 1)];; + + if (cur > threshold && cur > top && cur >= bottom && cur > left && cur >= right) + { + const unsigned int val = (y << 16) | x; + const int idx = atomic_add(counter, 1); + centers[idx] = val; + } + } +} + + +// //////////////////////////////////////////////////////////////////////// +// // circlesAccumRadius + +// TODO: add offset to support ROI +__kernel void circlesAccumRadius(__global const unsigned int* centers, + __global const unsigned int* list, const int count, + __global float4* circles, const int maxCircles, + const float dp, + const int minRadius, const int maxRadius, + const int histSize, + const int threshold, + __local int* smem, + __global int* counter) +{ + for (int i = get_local_id(0); i < histSize + 2; i += get_local_size(0)) + smem[i] = 0; + barrier(CLK_LOCAL_MEM_FENCE); + + unsigned int val = centers[get_group_id(0)]; + + float cx = convert_float(val & 0xFFFF); + float cy = convert_float((val >> 16) & 0xFFFF); + + cx = (cx + 0.5f) * dp; + cy = (cy + 0.5f) * dp; + + for (int i = get_local_id(0); i < count; i += get_local_size(0)) + { + val = list[i]; + + const int x = (val & 0xFFFF); + const int y = (val >> 16) & 0xFFFF; + + const float rad = sqrt((cx - x) * (cx - x) + (cy - y) * (cy - y)); + if (rad >= minRadius && rad <= maxRadius) + { + const int r = convert_int_rte(rad - minRadius); + + atomic_add(&smem[r + 1], 1); + } + } + + barrier(CLK_LOCAL_MEM_FENCE); + + for (int i = get_local_id(0); i < histSize; i += get_local_size(0)) + { + const int curVotes = smem[i + 1]; + + if (curVotes >= threshold && curVotes > smem[i] && curVotes >= smem[i + 2]) + + { + const int ind = atomic_add(counter, 1); + if (ind < maxCircles) + { + circles[ind] = (float4)(cx, cy, convert_float(i + minRadius), 0.0f); + } + } + } +} From e1c6564d5ed10297796220eed86d2d1d9ce8a744 Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Mon, 24 Dec 2012 18:41:51 -0500 Subject: [PATCH 05/10] fixed a typo in ocl.hpp --- modules/ocl/include/opencv2/ocl/ocl.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ocl/include/opencv2/ocl/ocl.hpp b/modules/ocl/include/opencv2/ocl/ocl.hpp index ee5e7849db..73b963b987 100644 --- a/modules/ocl/include/opencv2/ocl/ocl.hpp +++ b/modules/ocl/include/opencv2/ocl/ocl.hpp @@ -41,8 +41,8 @@ // //M*/ -#ifndef __OPENCV_GPU_HPP__ -#define __OPENCV_GPU_HPP__ +#ifndef __OPENCV_OCL_HPP__ +#define __OPENCV_OCL_HPP__ #include #include @@ -1762,4 +1762,4 @@ namespace cv #if _MSC_VER >= 1200 #pragma warning( pop) #endif -#endif /* __OPENCV_GPU_HPP__ */ +#endif /* __OPENCV_OCL_HPP__ */ From 78202100df56b5c301d0fcbd1499a715e4ef5694 Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Mon, 24 Dec 2012 22:53:27 -0500 Subject: [PATCH 06/10] added unit test for HoughCircles --- modules/ocl/test/test_hough.cpp | 111 ++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 modules/ocl/test/test_hough.cpp diff --git a/modules/ocl/test/test_hough.cpp b/modules/ocl/test/test_hough.cpp new file mode 100644 index 0000000000..cdb6807f58 --- /dev/null +++ b/modules/ocl/test/test_hough.cpp @@ -0,0 +1,111 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// Intel License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000, Intel Corporation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of Intel Corporation may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "precomp.hpp" + +#ifdef HAVE_OPENCL + +/////////////////////////////////////////////////////////////////////////////////////////////////////// +// HoughCircles + +PARAM_TEST_CASE(HoughCircles, cv::Size) +{ + static void drawCircles(cv::Mat& dst, const std::vector& circles, bool fill) + { + dst.setTo(cv::Scalar::all(0)); + + for (size_t i = 0; i < circles.size(); ++i) + cv::circle(dst, cv::Point2f(circles[i][0], circles[i][1]), (int)circles[i][2], cv::Scalar::all(255), fill ? -1 : 1); + } +}; + +TEST_P(HoughCircles, Accuracy) +{ + const cv::Size size = GET_PARAM(0); + + const float dp = 2.0f; + const float minDist = 10.0f; + const int minRadius = 10; + const int maxRadius = 20; + const int cannyThreshold = 100; + const int votesThreshold = 15; + + std::vector circles_gold(4); + circles_gold[0] = cv::Vec3i(20, 20, minRadius); + circles_gold[1] = cv::Vec3i(90, 87, minRadius + 3); + circles_gold[2] = cv::Vec3i(30, 70, minRadius + 8); + circles_gold[3] = cv::Vec3i(80, 10, maxRadius); + + cv::Mat src(size, CV_8UC1); + drawCircles(src, circles_gold, true); + cv::ocl::oclMat d_src(src); + + cv::ocl::oclMat d_circles; + cv::ocl::HoughCircles(d_src, d_circles, CV_HOUGH_GRADIENT, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius); + ASSERT_TRUE(d_circles.rows > 0); + + cv::Mat circles; + d_circles.download(circles); + + for (int i = 0; i < circles.cols; ++i) + { + cv::Vec3f cur = circles.at(i); + + bool found = false; + + for (size_t j = 0; j < circles_gold.size(); ++j) + { + cv::Vec3f gold = circles_gold[j]; + + if (std::fabs(cur[0] - gold[0]) < minDist && std::fabs(cur[1] - gold[1]) < minDist && std::fabs(cur[2] - gold[2]) < minDist) + { + found = true; + break; + } + } + + ASSERT_TRUE(found); + } +} + +INSTANTIATE_TEST_CASE_P(Hough, HoughCircles, DIFFERENT_SIZES); + +#endif // HAVE_OPENCL From b1faa46d3ae8f9bc18f07aeb749b3d5727ffff39 Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Wed, 26 Dec 2012 16:49:58 -0500 Subject: [PATCH 07/10] added performance test --- modules/ocl/perf/perf_hough.cpp | 136 ++++++++++++++++++++++++++++++++ modules/ocl/test/test_hough.cpp | 2 + 2 files changed, 138 insertions(+) create mode 100644 modules/ocl/perf/perf_hough.cpp diff --git a/modules/ocl/perf/perf_hough.cpp b/modules/ocl/perf/perf_hough.cpp new file mode 100644 index 0000000000..8d2139488e --- /dev/null +++ b/modules/ocl/perf/perf_hough.cpp @@ -0,0 +1,136 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved. +// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// @Authors +// Fangfang Bai, fangfang@multicorewareinc.com +// Seunghoon Park, pclove1@gmail.com +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other oclMaterials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors as is and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "precomp.hpp" + +#ifdef HAVE_OPENCL + +using namespace cv; +using namespace perf; + +////////////////////////////////////////////////////////////////////// +// HoughCircles + +PARAM_TEST_CASE(HoughCircles_Perf, cv::Size, float, float) +{ + static void drawCircles(cv::Mat& dst, const std::vector& circles, bool fill) + { + dst.setTo(cv::Scalar::all(0)); + + for (size_t i = 0; i < circles.size(); ++i) + cv::circle(dst, cv::Point2f(circles[i][0], circles[i][1]), (int)circles[i][2], cv::Scalar::all(255), fill ? -1 : 1); + } +}; + +TEST_P(HoughCircles_Perf, Performance) +{ + const cv::Size size = GET_PARAM(0); + const float dp = GET_PARAM(1); + const float minDist = GET_PARAM(2); + + const int minRadius = 10; + const int maxRadius = 30; + const int cannyThreshold = 100; + const int votesThreshold = 15; + + cv::RNG rng(123456789); + + cv::Mat src(size, CV_8UC1, cv::Scalar::all(0)); + + const int numCircles = rng.uniform(50, 100); + for (int i = 0; i < numCircles; ++i) + { + cv::Point center(rng.uniform(0, src.cols), rng.uniform(0, src.rows)); + const int radius = rng.uniform(minRadius, maxRadius + 1); + + cv::circle(src, center, radius, cv::Scalar::all(255), -1); + } + + cv::ocl::oclMat d_circles; + + double totalgputick = 0; + double totalgputick_kernel = 0; + + double t1 = 0.0; + double t2 = 0.0; + for (int j = 0; j < LOOP_TIMES + 1; ++j) + { + + t1 = (double)cvGetTickCount();//gpu start1 + + cv::ocl::oclMat ocl_src = cv::ocl::oclMat(src);//upload + + t2 = (double)cvGetTickCount(); //kernel + cv::ocl::HoughCircles(ocl_src, d_circles, CV_HOUGH_GRADIENT, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius); + t2 = (double)cvGetTickCount() - t2;//kernel + + cv::Mat cpu_dst; + if (d_circles.rows > 0) + d_circles.download (cpu_dst);//download + + t1 = (double)cvGetTickCount() - t1;//gpu end1 + + if(j == 0) + continue; + + totalgputick = t1 + totalgputick; + + totalgputick_kernel = t2 + totalgputick_kernel; + } + + std::cout << "average gpu runtime is " << totalgputick / ((double)cvGetTickFrequency()* LOOP_TIMES * 1000.) << "ms" << std::endl; + std::cout << "average gpu runtime without data transfer is " << totalgputick_kernel / ((double)cvGetTickFrequency()* LOOP_TIMES * 1000.) << "ms" << std::endl; + +} + + +INSTANTIATE_TEST_CASE_P(Hough, HoughCircles_Perf, + testing::Combine( + testing::Values(perf::sz720p, perf::szSXGA, perf::sz1080p), + testing::Values(1.0f, 2.0f, 4.0f), + testing::Values(1.0f, 10.0f))); + +#endif // HAVE_OPENCL diff --git a/modules/ocl/test/test_hough.cpp b/modules/ocl/test/test_hough.cpp index cdb6807f58..8fb06cfa02 100644 --- a/modules/ocl/test/test_hough.cpp +++ b/modules/ocl/test/test_hough.cpp @@ -13,6 +13,8 @@ // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective owners. // +// Modified by Seunghoon Park(pclove1@gmail.com) +// // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // From 8a1d6a1bb28642fb804a10873984e61c7be3e2ab Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Wed, 26 Dec 2012 17:40:26 -0500 Subject: [PATCH 08/10] added documentation for ocl::HoughCircles --- modules/ocl/doc/image_processing.rst | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/modules/ocl/doc/image_processing.rst b/modules/ocl/doc/image_processing.rst index 461fb98a5d..7ea5741ff7 100644 --- a/modules/ocl/doc/image_processing.rst +++ b/modules/ocl/doc/image_processing.rst @@ -329,3 +329,38 @@ Interpolate frames (images) using provided optical flow (displacement field). :param newFrame: Output image. :param buf: Temporary buffer, will have width x 6*height size, CV_32FC1 type and contain 6 oclMat: occlusion masks for first frame, occlusion masks for second, interpolated forward horizontal flow, interpolated forward vertical flow, interpolated backward horizontal flow, interpolated backward vertical flow. + + +ocl::HoughCircles +----------------- +Finds circles in a grayscale image using the Hough transform. + +.. ocv:function:: void ocl::HoughCircles(const oclMat& src, oclMat& circles, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096) + +.. ocv:function:: void ocl::HoughCircles(const oclMat& src, oclMat& circles, HoughCirclesBuf& buf, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096) + + :param src: 8-bit, single-channel grayscale input image. + + :param circles: Output vector of found circles. Each vector is encoded as a 3-element floating-point vector :math:`(x, y, radius)` . + + :param method: Detection method to use. Currently, the only implemented method is ``CV_HOUGH_GRADIENT`` , which is basically *21HT* , described in [Yuen90]_. + + :param dp: Inverse ratio of the accumulator resolution to the image resolution. For example, if ``dp=1`` , the accumulator has the same resolution as the input image. If ``dp=2`` , the accumulator has half as big width and height. + + :param minDist: Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed. + + :param cannyThreshold: The higher threshold of the two passed to the :ocv:func:`ocl::Canny` edge detector (the lower one is twice smaller). + + :param votesThreshold: The accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected. + + :param minRadius: Minimum circle radius. + + :param maxRadius: Maximum circle radius. + + :param maxCircles: Maximum number of output circles. + + :param buf: Optional buffer to avoid extra memory allocations (for many calls with the same sizes). + +.. note:: Currently only non-ROI oclMat is supported for src. +.. seealso:: :ocv:func:`HoughCircles` + From 0afa9cede566708ee45b6eb2b8aa1db2e2b6f787 Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Fri, 28 Dec 2012 10:08:28 -0500 Subject: [PATCH 09/10] removed personal info from the license header --- modules/ocl/perf/perf_hough.cpp | 4 ---- modules/ocl/src/hough.cpp | 2 -- modules/ocl/src/kernels/imgproc_hough.cl | 2 -- modules/ocl/test/test_hough.cpp | 7 +++---- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/modules/ocl/perf/perf_hough.cpp b/modules/ocl/perf/perf_hough.cpp index 8d2139488e..de2bf42f30 100644 --- a/modules/ocl/perf/perf_hough.cpp +++ b/modules/ocl/perf/perf_hough.cpp @@ -14,10 +14,6 @@ // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved. // Third party copyrights are property of their respective owners. // -// @Authors -// Fangfang Bai, fangfang@multicorewareinc.com -// Seunghoon Park, pclove1@gmail.com -// // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // diff --git a/modules/ocl/src/hough.cpp b/modules/ocl/src/hough.cpp index 9c51804566..0d2e9b81fa 100644 --- a/modules/ocl/src/hough.cpp +++ b/modules/ocl/src/hough.cpp @@ -14,8 +14,6 @@ // Copyright (C) 2009, Willow Garage Inc., all rights reserved. // Third party copyrights are property of their respective owners. // -// Modified by Seunghoon Park(pclove1@gmail.com) -// // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // diff --git a/modules/ocl/src/kernels/imgproc_hough.cl b/modules/ocl/src/kernels/imgproc_hough.cl index 42210e39a4..06655dea31 100644 --- a/modules/ocl/src/kernels/imgproc_hough.cl +++ b/modules/ocl/src/kernels/imgproc_hough.cl @@ -14,8 +14,6 @@ // Copyright (C) 2009, Willow Garage Inc., all rights reserved. // Third party copyrights are property of their respective owners. // -// Modified by Seunghoon Park(pclove1@gmail.com) -// // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // diff --git a/modules/ocl/test/test_hough.cpp b/modules/ocl/test/test_hough.cpp index 8fb06cfa02..3a5cec5f95 100644 --- a/modules/ocl/test/test_hough.cpp +++ b/modules/ocl/test/test_hough.cpp @@ -7,14 +7,13 @@ // copy or use the software. // // -// Intel License Agreement +// License Agreement // For Open Source Computer Vision Library // -// Copyright (C) 2000, Intel Corporation, all rights reserved. +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2008-2011, Willow Garage Inc., all rights reserved. // Third party copyrights are property of their respective owners. // -// Modified by Seunghoon Park(pclove1@gmail.com) -// // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // From 4f965296fc0441b937c0555299d456ad7ffc8a20 Mon Sep 17 00:00:00 2001 From: Suenghoon Park Date: Fri, 28 Dec 2012 16:32:10 -0500 Subject: [PATCH 10/10] modified performance test for ocl::HoughCircles --- modules/ocl/perf/perf_hough.cpp | 70 +++++++++------------------------ 1 file changed, 18 insertions(+), 52 deletions(-) diff --git a/modules/ocl/perf/perf_hough.cpp b/modules/ocl/perf/perf_hough.cpp index de2bf42f30..d0d130e922 100644 --- a/modules/ocl/perf/perf_hough.cpp +++ b/modules/ocl/perf/perf_hough.cpp @@ -50,22 +50,18 @@ using namespace perf; ////////////////////////////////////////////////////////////////////// // HoughCircles -PARAM_TEST_CASE(HoughCircles_Perf, cv::Size, float, float) +typedef std::tr1::tuple Size_Dp_MinDist_t; +typedef perf::TestBaseWithParam Size_Dp_MinDist; + +PERF_TEST_P(Size_Dp_MinDist, OCL_HoughCircles, + testing::Combine( + testing::Values(perf::sz720p, perf::szSXGA, perf::sz1080p), + testing::Values(1.0f, 2.0f, 4.0f), + testing::Values(1.0f, 10.0f))) { - static void drawCircles(cv::Mat& dst, const std::vector& circles, bool fill) - { - dst.setTo(cv::Scalar::all(0)); - - for (size_t i = 0; i < circles.size(); ++i) - cv::circle(dst, cv::Point2f(circles[i][0], circles[i][1]), (int)circles[i][2], cv::Scalar::all(255), fill ? -1 : 1); - } -}; - -TEST_P(HoughCircles_Perf, Performance) -{ - const cv::Size size = GET_PARAM(0); - const float dp = GET_PARAM(1); - const float minDist = GET_PARAM(2); + const cv::Size size = std::tr1::get<0>(GetParam()); + const float dp = std::tr1::get<1>(GetParam()); + const float minDist = std::tr1::get<2>(GetParam()); const int minRadius = 10; const int maxRadius = 30; @@ -85,48 +81,18 @@ TEST_P(HoughCircles_Perf, Performance) cv::circle(src, center, radius, cv::Scalar::all(255), -1); } - cv::ocl::oclMat d_circles; + cv::ocl::oclMat ocl_src(src); + cv::ocl::oclMat ocl_circles; - double totalgputick = 0; - double totalgputick_kernel = 0; + declare.time(10.0).iterations(25); - double t1 = 0.0; - double t2 = 0.0; - for (int j = 0; j < LOOP_TIMES + 1; ++j) + TEST_CYCLE() { - - t1 = (double)cvGetTickCount();//gpu start1 - - cv::ocl::oclMat ocl_src = cv::ocl::oclMat(src);//upload - - t2 = (double)cvGetTickCount(); //kernel - cv::ocl::HoughCircles(ocl_src, d_circles, CV_HOUGH_GRADIENT, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius); - t2 = (double)cvGetTickCount() - t2;//kernel - - cv::Mat cpu_dst; - if (d_circles.rows > 0) - d_circles.download (cpu_dst);//download - - t1 = (double)cvGetTickCount() - t1;//gpu end1 - - if(j == 0) - continue; - - totalgputick = t1 + totalgputick; - - totalgputick_kernel = t2 + totalgputick_kernel; + cv::ocl::HoughCircles(ocl_src, ocl_circles, CV_HOUGH_GRADIENT, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius); } - - std::cout << "average gpu runtime is " << totalgputick / ((double)cvGetTickFrequency()* LOOP_TIMES * 1000.) << "ms" << std::endl; - std::cout << "average gpu runtime without data transfer is " << totalgputick_kernel / ((double)cvGetTickFrequency()* LOOP_TIMES * 1000.) << "ms" << std::endl; + cv::Mat circles(ocl_circles); + SANITY_CHECK(circles); } - -INSTANTIATE_TEST_CASE_P(Hough, HoughCircles_Perf, - testing::Combine( - testing::Values(perf::sz720p, perf::szSXGA, perf::sz1080p), - testing::Values(1.0f, 2.0f, 4.0f), - testing::Values(1.0f, 10.0f))); - #endif // HAVE_OPENCL