Skip to content

Use texture Object to make cv::cuda::HoughSegmentDetectorImpl::detect() thread-safe #3185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions modules/cudaimgproc/src/cuda/hough_segments.cu
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@

#include "opencv2/core/cuda/common.hpp"
#include "opencv2/core/cuda/vec_math.hpp"
#include "opencv2/cudev.hpp"

namespace cv { namespace cuda { namespace device
{
namespace hough_segments
{
texture<uchar, cudaTextureType2D, cudaReadModeElementType> tex_mask(false, cudaFilterModePoint, cudaAddressModeClamp);

__global__ void houghLinesProbabilistic(const PtrStepSzi accum,
__global__ void houghLinesProbabilistic(cv::cudev::Texture<uchar> src, const PtrStepSzi accum,
int4* out, const int maxSize,
const float rho, const float theta,
const int lineGap, const int lineLength,
Expand Down Expand Up @@ -157,7 +156,7 @@ namespace cv { namespace cuda { namespace device

for (;;)
{
if (tex2D(tex_mask, p1.x, p1.y))
if (src(p1.y, p1.x))
{
gap = 0;

Expand Down Expand Up @@ -213,16 +212,17 @@ namespace cv { namespace cuda { namespace device
}
}

int houghLinesProbabilistic_gpu(PtrStepSzb mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int* counterPtr, cudaStream_t stream)
int houghLinesProbabilistic_gpu(GpuMat &mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int* counterPtr, cudaStream_t stream)
{
cudaSafeCall( cudaMemsetAsync(counterPtr, 0, sizeof(int), stream) );

const dim3 block(32, 8);
const dim3 grid(divUp(accum.cols - 2, block.x), divUp(accum.rows - 2, block.y));

cv::cudev::GpuMat_<uchar> src_(mask);
cv::cudev::Texture<uchar> tex(src_, false, cudaFilterModePoint, cudaAddressModeClamp);

bindTexture(&tex_mask, mask);

houghLinesProbabilistic<<<grid, block, 0, stream>>>(accum,
houghLinesProbabilistic<<<grid, block, 0, stream>>>(tex, accum,
out, maxSize,
rho, theta,
lineGap, lineLength,
Expand Down
2 changes: 1 addition & 1 deletion modules/cudaimgproc/src/hough_segments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace cv { namespace cuda { namespace device

namespace hough_segments
{
int houghLinesProbabilistic_gpu(PtrStepSzb mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int* counterPtr, cudaStream_t stream);
int houghLinesProbabilistic_gpu(GpuMat &mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int* counterPtr, cudaStream_t stream);
}
}}}

Expand Down
62 changes: 62 additions & 0 deletions modules/cudaimgproc/test/test_hough.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,68 @@ INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, HoughLines, testing::Combine(
DIFFERENT_SIZES,
WHOLE_SUBMAT));

///////////////////////////////////////////////////////////////////////////////////////////////////////
// HoughLines Probabilistic
PARAM_TEST_CASE(HoughLinesProbabilistic, cv::cuda::DeviceInfo, cv::Size, UseRoi)
{
static void generateLines(cv::Mat& img)
{
img.setTo(cv::Scalar::all(0));

cv::line(img, cv::Point(10, 0), cv::Point(10, 20), cv::Scalar::all(255));
cv::line(img, cv::Point(20, 50), cv::Point(40, 50), cv::Scalar::all(255));
}

static void drawLines(cv::Mat& dst, const std::vector<cv::Vec4i>& linesSegment)
{
dst.setTo(cv::Scalar::all(0));

for (size_t i = 0; i < linesSegment.size(); ++i)
{
Vec4i l = linesSegment[i];
cv::line(dst, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar::all(255));
}

}
};

CUDA_TEST_P(HoughLinesProbabilistic, Accuracy)
{
const cv::cuda::DeviceInfo devInfo = GET_PARAM(0);
cv::cuda::setDevice(devInfo.deviceID());
const cv::Size size = GET_PARAM(1);
const bool useRoi = GET_PARAM(2);

const float rho = 1.0f;
const float theta = (float) (1.0 * CV_PI / 180.0);
const int minLineLength = 15;
const int maxLineGap = 8;

cv::Mat src(size, CV_8UC1);
generateLines(src);

Ptr<cv::cuda::HoughSegmentDetector> hough = cv::cuda::createHoughSegmentDetector(rho, theta, minLineLength, maxLineGap);

cv::cuda::GpuMat d_lines;
hough->detect(loadMat(src, useRoi), d_lines);

std::vector<cv::Vec4i> linesSegment;
std::vector<cv::Vec2f> lines;
d_lines.download(linesSegment);

cv::Mat dst(size, CV_8UC1);
drawLines(dst, linesSegment);

ASSERT_MAT_NEAR(src, dst, 0.0);

}

INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, HoughLinesProbabilistic, testing::Combine(
ALL_DEVICES,
DIFFERENT_SIZES,
WHOLE_SUBMAT));


///////////////////////////////////////////////////////////////////////////////////////////////////////
// HoughCircles

Expand Down