Skip to content

Commit 6a71751

Browse files
mitul93airtop-bastdeadprogram
authored
Merge pull request #3185 from northvolt:hough_cuda_texture_obj
Use texture Object to make cv::cuda::HoughSegmentDetectorImpl::detect() thread-safe * Use texture Object to make HoughSegmentDetectorImpl::detect() thread-safe * Use parameter useRoi correctly in HoughLinesProbabilistic cuda test Signed-off-by: Ron Evans <[email protected]> Co-authored-by: airtop-bast <[email protected]> Co-authored-by: Ron Evans <[email protected]>
1 parent a5b2490 commit 6a71751

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

modules/cudaimgproc/src/cuda/hough_segments.cu

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@
4444

4545
#include "opencv2/core/cuda/common.hpp"
4646
#include "opencv2/core/cuda/vec_math.hpp"
47+
#include "opencv2/cudev.hpp"
4748

4849
namespace cv { namespace cuda { namespace device
4950
{
5051
namespace hough_segments
5152
{
52-
texture<uchar, cudaTextureType2D, cudaReadModeElementType> tex_mask(false, cudaFilterModePoint, cudaAddressModeClamp);
53-
54-
__global__ void houghLinesProbabilistic(const PtrStepSzi accum,
53+
__global__ void houghLinesProbabilistic(cv::cudev::Texture<uchar> src, const PtrStepSzi accum,
5554
int4* out, const int maxSize,
5655
const float rho, const float theta,
5756
const int lineGap, const int lineLength,
@@ -157,7 +156,7 @@ namespace cv { namespace cuda { namespace device
157156

158157
for (;;)
159158
{
160-
if (tex2D(tex_mask, p1.x, p1.y))
159+
if (src(p1.y, p1.x))
161160
{
162161
gap = 0;
163162

@@ -213,16 +212,17 @@ namespace cv { namespace cuda { namespace device
213212
}
214213
}
215214

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

220219
const dim3 block(32, 8);
221220
const dim3 grid(divUp(accum.cols - 2, block.x), divUp(accum.rows - 2, block.y));
221+
222+
cv::cudev::GpuMat_<uchar> src_(mask);
223+
cv::cudev::Texture<uchar> tex(src_, false, cudaFilterModePoint, cudaAddressModeClamp);
222224

223-
bindTexture(&tex_mask, mask);
224-
225-
houghLinesProbabilistic<<<grid, block, 0, stream>>>(accum,
225+
houghLinesProbabilistic<<<grid, block, 0, stream>>>(tex, accum,
226226
out, maxSize,
227227
rho, theta,
228228
lineGap, lineLength,

modules/cudaimgproc/src/hough_segments.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace cv { namespace cuda { namespace device
6565

6666
namespace hough_segments
6767
{
68-
int houghLinesProbabilistic_gpu(PtrStepSzb mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int* counterPtr, cudaStream_t stream);
68+
int houghLinesProbabilistic_gpu(GpuMat &mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int* counterPtr, cudaStream_t stream);
6969
}
7070
}}}
7171

modules/cudaimgproc/test/test_hough.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,68 @@ INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, HoughLines, testing::Combine(
113113
DIFFERENT_SIZES,
114114
WHOLE_SUBMAT));
115115

116+
///////////////////////////////////////////////////////////////////////////////////////////////////////
117+
// HoughLines Probabilistic
118+
PARAM_TEST_CASE(HoughLinesProbabilistic, cv::cuda::DeviceInfo, cv::Size, UseRoi)
119+
{
120+
static void generateLines(cv::Mat& img)
121+
{
122+
img.setTo(cv::Scalar::all(0));
123+
124+
cv::line(img, cv::Point(10, 0), cv::Point(10, 20), cv::Scalar::all(255));
125+
cv::line(img, cv::Point(20, 50), cv::Point(40, 50), cv::Scalar::all(255));
126+
}
127+
128+
static void drawLines(cv::Mat& dst, const std::vector<cv::Vec4i>& linesSegment)
129+
{
130+
dst.setTo(cv::Scalar::all(0));
131+
132+
for (size_t i = 0; i < linesSegment.size(); ++i)
133+
{
134+
Vec4i l = linesSegment[i];
135+
cv::line(dst, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar::all(255));
136+
}
137+
138+
}
139+
};
140+
141+
CUDA_TEST_P(HoughLinesProbabilistic, Accuracy)
142+
{
143+
const cv::cuda::DeviceInfo devInfo = GET_PARAM(0);
144+
cv::cuda::setDevice(devInfo.deviceID());
145+
const cv::Size size = GET_PARAM(1);
146+
const bool useRoi = GET_PARAM(2);
147+
148+
const float rho = 1.0f;
149+
const float theta = (float) (1.0 * CV_PI / 180.0);
150+
const int minLineLength = 15;
151+
const int maxLineGap = 8;
152+
153+
cv::Mat src(size, CV_8UC1);
154+
generateLines(src);
155+
156+
Ptr<cv::cuda::HoughSegmentDetector> hough = cv::cuda::createHoughSegmentDetector(rho, theta, minLineLength, maxLineGap);
157+
158+
cv::cuda::GpuMat d_lines;
159+
hough->detect(loadMat(src, useRoi), d_lines);
160+
161+
std::vector<cv::Vec4i> linesSegment;
162+
std::vector<cv::Vec2f> lines;
163+
d_lines.download(linesSegment);
164+
165+
cv::Mat dst(size, CV_8UC1);
166+
drawLines(dst, linesSegment);
167+
168+
ASSERT_MAT_NEAR(src, dst, 0.0);
169+
170+
}
171+
172+
INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, HoughLinesProbabilistic, testing::Combine(
173+
ALL_DEVICES,
174+
DIFFERENT_SIZES,
175+
WHOLE_SUBMAT));
176+
177+
116178
///////////////////////////////////////////////////////////////////////////////////////////////////////
117179
// HoughCircles
118180

0 commit comments

Comments
 (0)