added gpu::HoughLinesP function
This commit is contained in:
@@ -293,6 +293,201 @@ namespace cv { namespace gpu { namespace device
|
||||
return totalCount;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// houghLinesProbabilistic
|
||||
|
||||
texture<uchar, cudaTextureType2D, cudaReadModeElementType> tex_mask(false, cudaFilterModePoint, cudaAddressModeClamp);
|
||||
|
||||
__global__ void houghLinesProbabilistic(const PtrStepSzi accum,
|
||||
int4* out, const int maxSize,
|
||||
const float rho, const float theta,
|
||||
const int lineGap, const int lineLength,
|
||||
const int rows, const int cols)
|
||||
{
|
||||
const int r = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
const int n = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
|
||||
if (r >= accum.cols - 2 || n >= accum.rows - 2)
|
||||
return;
|
||||
|
||||
const int curVotes = accum(n + 1, r + 1);
|
||||
|
||||
if (curVotes >= lineLength &&
|
||||
curVotes > accum(n, r) &&
|
||||
curVotes > accum(n, r + 1) &&
|
||||
curVotes > accum(n, r + 2) &&
|
||||
curVotes > accum(n + 1, r) &&
|
||||
curVotes > accum(n + 1, r + 2) &&
|
||||
curVotes > accum(n + 2, r) &&
|
||||
curVotes > accum(n + 2, r + 1) &&
|
||||
curVotes > accum(n + 2, r + 2))
|
||||
{
|
||||
const float radius = (r - (accum.cols - 2 - 1) * 0.5f) * rho;
|
||||
const float angle = n * theta;
|
||||
|
||||
float cosa;
|
||||
float sina;
|
||||
sincosf(angle, &sina, &cosa);
|
||||
|
||||
float2 p0 = make_float2(cosa * radius, sina * radius);
|
||||
float2 dir = make_float2(-sina, cosa);
|
||||
|
||||
float2 pb[4] = {make_float2(-1, -1), make_float2(-1, -1), make_float2(-1, -1), make_float2(-1, -1)};
|
||||
float a;
|
||||
|
||||
if (dir.x != 0)
|
||||
{
|
||||
a = -p0.x / dir.x;
|
||||
pb[0].x = 0;
|
||||
pb[0].y = p0.y + a * dir.y;
|
||||
|
||||
a = (cols - 1 - p0.x) / dir.x;
|
||||
pb[1].x = cols - 1;
|
||||
pb[1].y = p0.y + a * dir.y;
|
||||
}
|
||||
if (dir.y != 0)
|
||||
{
|
||||
a = -p0.y / dir.y;
|
||||
pb[2].x = p0.x + a * dir.x;
|
||||
pb[2].y = 0;
|
||||
|
||||
a = (rows - 1 - p0.y) / dir.y;
|
||||
pb[3].x = p0.x + a * dir.x;
|
||||
pb[3].y = rows - 1;
|
||||
}
|
||||
|
||||
if (pb[0].x == 0 && (pb[0].y >= 0 && pb[0].y < rows))
|
||||
{
|
||||
p0 = pb[0];
|
||||
if (dir.x < 0)
|
||||
dir = -dir;
|
||||
}
|
||||
else if (pb[1].x == cols - 1 && (pb[0].y >= 0 && pb[0].y < rows))
|
||||
{
|
||||
p0 = pb[1];
|
||||
if (dir.x > 0)
|
||||
dir = -dir;
|
||||
}
|
||||
else if (pb[2].y == 0 && (pb[2].x >= 0 && pb[2].x < cols))
|
||||
{
|
||||
p0 = pb[2];
|
||||
if (dir.y < 0)
|
||||
dir = -dir;
|
||||
}
|
||||
else if (pb[3].y == rows - 1 && (pb[3].x >= 0 && pb[3].x < cols))
|
||||
{
|
||||
p0 = pb[3];
|
||||
if (dir.y > 0)
|
||||
dir = -dir;
|
||||
}
|
||||
|
||||
float2 d;
|
||||
if (::fabsf(dir.x) > ::fabsf(dir.y))
|
||||
{
|
||||
d.x = dir.x > 0 ? 1 : -1;
|
||||
d.y = dir.y / ::fabsf(dir.x);
|
||||
}
|
||||
else
|
||||
{
|
||||
d.x = dir.x / ::fabsf(dir.y);
|
||||
d.y = dir.y > 0 ? 1 : -1;
|
||||
}
|
||||
|
||||
float2 line_end[2];
|
||||
int gap;
|
||||
bool inLine = false;
|
||||
|
||||
float2 p1 = p0;
|
||||
if (p1.x < 0 || p1.x >= cols || p1.y < 0 || p1.y >= rows)
|
||||
return;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (tex2D(tex_mask, p1.x, p1.y))
|
||||
{
|
||||
gap = 0;
|
||||
|
||||
if (!inLine)
|
||||
{
|
||||
line_end[0] = p1;
|
||||
line_end[1] = p1;
|
||||
inLine = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
line_end[1] = p1;
|
||||
}
|
||||
}
|
||||
else if (inLine)
|
||||
{
|
||||
if (++gap > lineGap)
|
||||
{
|
||||
bool good_line = ::abs(line_end[1].x - line_end[0].x) >= lineLength ||
|
||||
::abs(line_end[1].y - line_end[0].y) >= lineLength;
|
||||
|
||||
if (good_line)
|
||||
{
|
||||
const int ind = ::atomicAdd(&g_counter, 1);
|
||||
if (ind < maxSize)
|
||||
out[ind] = make_int4(line_end[0].x, line_end[0].y, line_end[1].x, line_end[1].y);
|
||||
}
|
||||
|
||||
gap = 0;
|
||||
inLine = false;
|
||||
}
|
||||
}
|
||||
|
||||
p1 = p1 + d;
|
||||
if (p1.x < 0 || p1.x >= cols || p1.y < 0 || p1.y >= rows)
|
||||
{
|
||||
if (inLine)
|
||||
{
|
||||
bool good_line = ::abs(line_end[1].x - line_end[0].x) >= lineLength ||
|
||||
::abs(line_end[1].y - line_end[0].y) >= lineLength;
|
||||
|
||||
if (good_line)
|
||||
{
|
||||
const int ind = ::atomicAdd(&g_counter, 1);
|
||||
if (ind < maxSize)
|
||||
out[ind] = make_int4(line_end[0].x, line_end[0].y, line_end[1].x, line_end[1].y);
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int houghLinesProbabilistic_gpu(PtrStepSzb mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength)
|
||||
{
|
||||
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));
|
||||
|
||||
bindTexture(&tex_mask, mask);
|
||||
|
||||
houghLinesProbabilistic<<<grid, block>>>(accum,
|
||||
out, maxSize,
|
||||
rho, theta,
|
||||
lineGap, lineLength,
|
||||
mask.rows, mask.cols);
|
||||
cudaSafeCall( cudaGetLastError() );
|
||||
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
|
||||
int totalCount;
|
||||
cudaSafeCall( cudaMemcpy(&totalCount, counterPtr, sizeof(int), cudaMemcpyDeviceToHost) );
|
||||
|
||||
totalCount = ::min(totalCount, maxSize);
|
||||
|
||||
return totalCount;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// circlesAccumCenters
|
||||
|
||||
|
@@ -52,6 +52,8 @@ void cv::gpu::HoughLines(const GpuMat&, GpuMat&, float, float, int, bool, int) {
|
||||
void cv::gpu::HoughLines(const GpuMat&, GpuMat&, HoughLinesBuf&, float, float, int, bool, int) { throw_nogpu(); }
|
||||
void cv::gpu::HoughLinesDownload(const GpuMat&, OutputArray, OutputArray) { throw_nogpu(); }
|
||||
|
||||
void cv::gpu::HoughLinesP(const GpuMat&, GpuMat&, HoughLinesBuf&, float, float, int, int, int) { throw_nogpu(); }
|
||||
|
||||
void cv::gpu::HoughCircles(const GpuMat&, GpuMat&, int, float, float, int, int, int, int, int) { throw_nogpu(); }
|
||||
void cv::gpu::HoughCircles(const GpuMat&, GpuMat&, HoughCirclesBuf&, int, float, float, int, int, int, int, int) { throw_nogpu(); }
|
||||
void cv::gpu::HoughCirclesDownload(const GpuMat&, OutputArray) { throw_nogpu(); }
|
||||
@@ -157,6 +159,57 @@ void cv::gpu::HoughLinesDownload(const GpuMat& d_lines, OutputArray h_lines_, Ou
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
// HoughLinesP
|
||||
|
||||
namespace cv { namespace gpu { namespace device
|
||||
{
|
||||
namespace hough
|
||||
{
|
||||
int houghLinesProbabilistic_gpu(PtrStepSzb mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength);
|
||||
}
|
||||
}}}
|
||||
|
||||
void cv::gpu::HoughLinesP(const GpuMat& src, GpuMat& lines, HoughLinesBuf& buf, float rho, float theta, int minLineLength, int maxLineGap, int maxLines)
|
||||
{
|
||||
using namespace cv::gpu::device::hough;
|
||||
|
||||
CV_Assert( src.type() == CV_8UC1 );
|
||||
CV_Assert( src.cols < std::numeric_limits<unsigned short>::max() );
|
||||
CV_Assert( src.rows < std::numeric_limits<unsigned short>::max() );
|
||||
|
||||
ensureSizeIsEnough(1, src.size().area(), CV_32SC1, buf.list);
|
||||
unsigned int* srcPoints = buf.list.ptr<unsigned int>();
|
||||
|
||||
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;
|
||||
cudaDeviceProp prop;
|
||||
cudaSafeCall(cudaGetDeviceProperties(&prop, devInfo.deviceID()));
|
||||
linesAccum_gpu(srcPoints, pointsCount, buf.accum, rho, theta, prop.sharedMemPerBlock, devInfo.supports(FEATURE_SET_COMPUTE_20));
|
||||
|
||||
ensureSizeIsEnough(1, maxLines, CV_32SC4, lines);
|
||||
|
||||
int linesCount = houghLinesProbabilistic_gpu(src, buf.accum, lines.ptr<int4>(), maxLines, rho, theta, maxLineGap, minLineLength);
|
||||
|
||||
if (linesCount > 0)
|
||||
lines.cols = linesCount;
|
||||
else
|
||||
lines.release();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
// HoughCircles
|
||||
|
||||
|
Reference in New Issue
Block a user