Skip to content

Commit 853144e

Browse files
authored
Merge pull request #3465 from 747:hough-segment-fix
Add `threshold` parameter to cuda::HoughSegmentDetector
2 parents fd411e7 + 04ae7db commit 853144e

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

modules/cudaimgproc/include/opencv2/cudaimgproc.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ class CV_EXPORTS_W HoughSegmentDetector : public Algorithm
425425

426426
CV_WRAP virtual void setMaxLines(int maxLines) = 0;
427427
CV_WRAP virtual int getMaxLines() const = 0;
428+
429+
CV_WRAP virtual void setThreshold(int threshold) = 0;
430+
CV_WRAP virtual int getThreshold() const = 0;
428431
};
429432

430433
/** @brief Creates implementation for cuda::HoughSegmentDetector .
@@ -434,8 +437,10 @@ class CV_EXPORTS_W HoughSegmentDetector : public Algorithm
434437
@param minLineLength Minimum line length. Line segments shorter than that are rejected.
435438
@param maxLineGap Maximum allowed gap between points on the same line to link them.
436439
@param maxLines Maximum number of output lines.
440+
@param threshold %Accumulator threshold parameter. Only those lines are returned that get enough
441+
votes ( \f$>\texttt{threshold}\f$ ).
437442
*/
438-
CV_EXPORTS_W Ptr<HoughSegmentDetector> createHoughSegmentDetector(float rho, float theta, int minLineLength, int maxLineGap, int maxLines = 4096);
443+
CV_EXPORTS_W Ptr<HoughSegmentDetector> createHoughSegmentDetector(float rho, float theta, int minLineLength, int maxLineGap, int maxLines = 4096, int threshold = -1);
439444

440445
//////////////////////////////////////
441446
// HoughCircles

modules/cudaimgproc/src/cuda/hough_segments.cu

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ namespace cv { namespace cuda { namespace device
5555
int4* out, const int maxSize,
5656
const float rho, const float theta,
5757
const int lineGap, const int lineLength,
58+
const int threshold,
5859
const int rows, const int cols,
5960
int* counterPtr)
6061
{
@@ -65,8 +66,9 @@ namespace cv { namespace cuda { namespace device
6566
return;
6667

6768
const int curVotes = accum(n + 1, r + 1);
69+
const int threshold_ = (threshold > 0) ? threshold : lineLength;
6870

69-
if (curVotes >= lineLength &&
71+
if (curVotes >= threshold_ &&
7072
curVotes > accum(n, r) &&
7173
curVotes > accum(n, r + 1) &&
7274
curVotes > accum(n, r + 2) &&
@@ -213,7 +215,7 @@ namespace cv { namespace cuda { namespace device
213215
}
214216
}
215217

216-
int houghLinesProbabilistic_gpu(GpuMat &mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int* counterPtr, cudaStream_t stream)
218+
int houghLinesProbabilistic_gpu(GpuMat &mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int threshold, int* counterPtr, cudaStream_t stream)
217219
{
218220
cudaSafeCall( cudaMemsetAsync(counterPtr, 0, sizeof(int), stream) );
219221

@@ -225,11 +227,11 @@ namespace cv { namespace cuda { namespace device
225227
mask.locateROI(wholeSize, ofs);
226228
if (ofs.x || ofs.y) {
227229
cv::cudev::TextureOff<uchar> texMask(wholeSize.height, wholeSize.width, mask.datastart, mask.step, ofs.y, ofs.x);
228-
houghLinesProbabilistic<cv::cudev::TextureOffPtr<uchar>><<<grid, block, 0, stream>>>(texMask, accum, out, maxSize, rho, theta, lineGap, lineLength, mask.rows, mask.cols, counterPtr);
230+
houghLinesProbabilistic<cv::cudev::TextureOffPtr<uchar>><<<grid, block, 0, stream>>>(texMask, accum, out, maxSize, rho, theta, lineGap, lineLength, threshold, mask.rows, mask.cols, counterPtr);
229231
}
230232
else {
231233
cv::cudev::Texture<uchar> texMask(mask);
232-
houghLinesProbabilistic<cv::cudev::TexturePtr<uchar>><<<grid, block, 0, stream>>>(texMask, accum, out, maxSize, rho, theta, lineGap, lineLength, mask.rows, mask.cols, counterPtr);
234+
houghLinesProbabilistic<cv::cudev::TexturePtr<uchar>><<<grid, block, 0, stream>>>(texMask, accum, out, maxSize, rho, theta, lineGap, lineLength, threshold, mask.rows, mask.cols, counterPtr);
233235
}
234236

235237
cudaSafeCall( cudaGetLastError() );

modules/cudaimgproc/src/hough_segments.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ using namespace cv::cuda;
4747

4848
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
4949

50-
Ptr<cuda::HoughSegmentDetector> cv::cuda::createHoughSegmentDetector(float, float, int, int, int) { throw_no_cuda(); return Ptr<HoughSegmentDetector>(); }
50+
Ptr<cuda::HoughSegmentDetector> cv::cuda::createHoughSegmentDetector(float, float, int, int, int, int) { throw_no_cuda(); return Ptr<HoughSegmentDetector>(); }
5151

5252
#else /* !defined (HAVE_CUDA) */
5353

@@ -65,7 +65,7 @@ namespace cv { namespace cuda { namespace device
6565

6666
namespace hough_segments
6767
{
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);
68+
int houghLinesProbabilistic_gpu(GpuMat &mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int threshold, int* counterPtr, cudaStream_t stream);
6969
}
7070
}}}
7171

@@ -74,7 +74,7 @@ namespace
7474
class HoughSegmentDetectorImpl : public HoughSegmentDetector
7575
{
7676
public:
77-
HoughSegmentDetectorImpl(float rho, float theta, int minLineLength, int maxLineGap, int maxLines);
77+
HoughSegmentDetectorImpl(float rho, float theta, int minLineLength, int maxLineGap, int maxLines, int threshold);
7878
~HoughSegmentDetectorImpl();
7979

8080
void detect(InputArray src, OutputArray lines, Stream& stream);
@@ -94,6 +94,9 @@ namespace
9494
void setMaxLines(int maxLines) { maxLines_ = maxLines; }
9595
int getMaxLines() const { return maxLines_; }
9696

97+
void setThreshold(int threshold) { threshold_ = threshold; }
98+
int getThreshold() const { return threshold_; }
99+
97100
void write(FileStorage& fs) const
98101
{
99102
writeFormat(fs);
@@ -102,7 +105,8 @@ namespace
102105
<< "theta" << theta_
103106
<< "minLineLength" << minLineLength_
104107
<< "maxLineGap" << maxLineGap_
105-
<< "maxLines" << maxLines_;
108+
<< "maxLines" << maxLines_
109+
<< "threshold" << threshold_;
106110
}
107111

108112
void read(const FileNode& fn)
@@ -113,6 +117,7 @@ namespace
113117
minLineLength_ = (int)fn["minLineLength"];
114118
maxLineGap_ = (int)fn["maxLineGap"];
115119
maxLines_ = (int)fn["maxLines"];
120+
threshold_ = (int)fn["threshold"];
116121
}
117122

118123
private:
@@ -121,6 +126,7 @@ namespace
121126
int minLineLength_;
122127
int maxLineGap_;
123128
int maxLines_;
129+
int threshold_;
124130

125131
GpuMat accum_;
126132
GpuMat list_;
@@ -129,8 +135,8 @@ namespace
129135
int* counterPtr_;
130136
};
131137

132-
HoughSegmentDetectorImpl::HoughSegmentDetectorImpl(float rho, float theta, int minLineLength, int maxLineGap, int maxLines) :
133-
rho_(rho), theta_(theta), minLineLength_(minLineLength), maxLineGap_(maxLineGap), maxLines_(maxLines)
138+
HoughSegmentDetectorImpl::HoughSegmentDetectorImpl(float rho, float theta, int minLineLength, int maxLineGap, int maxLines, int threshold) :
139+
rho_(rho), theta_(theta), minLineLength_(minLineLength), maxLineGap_(maxLineGap), maxLines_(maxLines), threshold_(threshold)
134140
{
135141
cudaSafeCall(cudaMalloc(&counterPtr_, sizeof(int)));
136142
}
@@ -178,7 +184,7 @@ namespace
178184

179185
ensureSizeIsEnough(1, maxLines_, CV_32SC4, result_);
180186

181-
int linesCount = houghLinesProbabilistic_gpu(src, accum_, result_.ptr<int4>(), maxLines_, rho_, theta_, maxLineGap_, minLineLength_, counterPtr_, cudaStream);
187+
int linesCount = houghLinesProbabilistic_gpu(src, accum_, result_.ptr<int4>(), maxLines_, rho_, theta_, maxLineGap_, minLineLength_, threshold_, counterPtr_, cudaStream);
182188

183189
if (linesCount == 0)
184190
{
@@ -191,9 +197,9 @@ namespace
191197
}
192198
}
193199

194-
Ptr<HoughSegmentDetector> cv::cuda::createHoughSegmentDetector(float rho, float theta, int minLineLength, int maxLineGap, int maxLines)
200+
Ptr<HoughSegmentDetector> cv::cuda::createHoughSegmentDetector(float rho, float theta, int minLineLength, int maxLineGap, int maxLines, int threshold)
195201
{
196-
return makePtr<HoughSegmentDetectorImpl>(rho, theta, minLineLength, maxLineGap, maxLines);
202+
return makePtr<HoughSegmentDetectorImpl>(rho, theta, minLineLength, maxLineGap, maxLines, threshold);
197203
}
198204

199205
#endif /* !defined (HAVE_CUDA) */

0 commit comments

Comments
 (0)