Skip to content

Commit 03407a9

Browse files
take1014alalek
authored andcommitted
Merge pull request opencv#10646 from take1014:master
* Add a new interface for hough transform * Fixed warning code * Fix HoughLinesUsingSetOfPoints based on HoughLinesStandard * Delete memset * Rename HoughLinesUsingSetOfPoints and add common function * Fix test error * Change static function name * Change using CV_Assert instead of if-block and add integer test case * I solve the conflict and delete 'std :: tr1' and changed it to use 'tuple' * I deleted std::tr1::get and changed int to use 'get' * Fixed sample code * revert test_main.cpp * Delete sample code in comment and add snippets * Change file name * Delete static function * Fixed build error
1 parent d56b2b5 commit 03407a9

File tree

4 files changed

+235
-20
lines changed

4 files changed

+235
-20
lines changed

modules/imgproc/include/opencv2/imgproc.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,27 @@ CV_EXPORTS_W void HoughLinesP( InputArray image, OutputArray lines,
20722072
double rho, double theta, int threshold,
20732073
double minLineLength = 0, double maxLineGap = 0 );
20742074

2075+
/** @brief Finds lines in a set of points using the standard Hough transform.
2076+
2077+
The function finds lines in a set of points using a modification of the Hough transform.
2078+
@include snippets/imgproc_HoughLinesPointSet.cpp
2079+
@param _point Input vector of points. Each vector must be encoded as a Point vector \f$(x,y)\f$. Type must be CV_32FC2 or CV_32SC2.
2080+
@param _lines Output vector of found lines. Each vector is encoded as a vector<Vec3d> \f$(votes, rho, theta)\f$.
2081+
The larger the value of 'votes', the higher the reliability of the Hough line.
2082+
@param lines_max Max count of hough lines.
2083+
@param threshold Accumulator threshold parameter. Only those lines are returned that get enough
2084+
votes ( \f$>\texttt{threshold}\f$ )
2085+
@param min_rho Minimum Distance value of the accumulator in pixels.
2086+
@param max_rho Maximum Distance value of the accumulator in pixels.
2087+
@param rho_step Distance resolution of the accumulator in pixels.
2088+
@param min_theta Minimum angle value of the accumulator in radians.
2089+
@param max_theta Maximum angle value of the accumulator in radians.
2090+
@param theta_step Angle resolution of the accumulator in radians.
2091+
*/
2092+
CV_EXPORTS_W void HoughLinesPointSet( InputArray _point, OutputArray _lines, int lines_max, int threshold,
2093+
double min_rho, double max_rho, double rho_step,
2094+
double min_theta, double max_theta, double theta_step );
2095+
20752096
/** @example houghcircles.cpp
20762097
An example using the Hough circle detector
20772098
*/

modules/imgproc/src/hough.cpp

Lines changed: 103 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ struct hough_cmp_gt
6868
const int* aux;
6969
};
7070

71+
static void
72+
createTrigTable( int numangle, double min_theta, double theta_step,
73+
float irho, float *tabSin, float *tabCos )
74+
{
75+
float ang = static_cast<float>(min_theta);
76+
for(int n = 0; n < numangle; ang += (float)theta_step, n++ )
77+
{
78+
tabSin[n] = (float)(sin((double)ang) * irho);
79+
tabCos[n] = (float)(cos((double)ang) * irho);
80+
}
81+
}
82+
83+
static void
84+
findLocalMaximums( int numrho, int numangle, int threshold,
85+
const int *accum, std::vector<int>& sort_buf )
86+
{
87+
for(int r = 0; r < numrho; r++ )
88+
for(int n = 0; n < numangle; n++ )
89+
{
90+
int base = (n+1) * (numrho+2) + r+1;
91+
if( accum[base] > threshold &&
92+
accum[base] > accum[base - 1] && accum[base] >= accum[base + 1] &&
93+
accum[base] > accum[base - numrho - 2] && accum[base] >= accum[base + numrho + 2] )
94+
sort_buf.push_back(base);
95+
}
96+
}
7197

7298
/*
7399
Here image is an input raster;
@@ -125,21 +151,17 @@ HoughLinesStandard( const Mat& img, float rho, float theta,
125151
}
126152
#endif
127153

128-
AutoBuffer<int> _accum((numangle+2) * (numrho+2));
154+
155+
Mat _accum = Mat::zeros( (numangle+2), (numrho+2), CV_32SC1 );
129156
std::vector<int> _sort_buf;
130157
AutoBuffer<float> _tabSin(numangle);
131158
AutoBuffer<float> _tabCos(numangle);
132-
int *accum = _accum;
159+
int *accum = _accum.ptr<int>();
133160
float *tabSin = _tabSin, *tabCos = _tabCos;
134161

135-
memset( accum, 0, sizeof(accum[0]) * (numangle+2) * (numrho+2) );
136-
137-
float ang = static_cast<float>(min_theta);
138-
for(int n = 0; n < numangle; ang += theta, n++ )
139-
{
140-
tabSin[n] = (float)(sin((double)ang) * irho);
141-
tabCos[n] = (float)(cos((double)ang) * irho);
142-
}
162+
// create sin and cos table
163+
createTrigTable( numangle, min_theta, theta,
164+
irho, tabSin, tabCos );
143165

144166
// stage 1. fill accumulator
145167
for( i = 0; i < height; i++ )
@@ -155,15 +177,7 @@ HoughLinesStandard( const Mat& img, float rho, float theta,
155177
}
156178

157179
// stage 2. find local maximums
158-
for(int r = 0; r < numrho; r++ )
159-
for(int n = 0; n < numangle; n++ )
160-
{
161-
int base = (n+1) * (numrho+2) + r+1;
162-
if( accum[base] > threshold &&
163-
accum[base] > accum[base - 1] && accum[base] >= accum[base + 1] &&
164-
accum[base] > accum[base - numrho - 2] && accum[base] >= accum[base + numrho + 2] )
165-
_sort_buf.push_back(base);
166-
}
180+
findLocalMaximums( numrho, numangle, threshold, accum, _sort_buf );
167181

168182
// stage 3. sort the detected lines by accumulator value
169183
std::sort(_sort_buf.begin(), _sort_buf.end(), hough_cmp_gt(accum));
@@ -883,6 +897,76 @@ void HoughLinesP(InputArray _image, OutputArray _lines,
883897
Mat(lines).copyTo(_lines);
884898
}
885899

900+
void HoughLinesPointSet( InputArray _point, OutputArray _lines, int lines_max, int threshold,
901+
double min_rho, double max_rho, double rho_step,
902+
double min_theta, double max_theta, double theta_step )
903+
{
904+
std::vector<Vec3d> lines;
905+
std::vector<Point2f> point;
906+
_point.copyTo(point);
907+
908+
CV_Assert( _point.type() == CV_32FC2 || _point.type() == CV_32SC2 );
909+
if( lines_max <= 0 ) {
910+
CV_Error( Error::StsBadArg, "lines_max must be greater than 0" );
911+
}
912+
if( threshold < 0) {
913+
CV_Error( Error::StsBadArg, "threshold must be greater than 0" );
914+
}
915+
if( ((max_rho - min_rho) <= 0) || ((max_theta - min_theta) <= 0) ) {
916+
CV_Error( Error::StsBadArg, "max must be greater than min" );
917+
}
918+
if( ((rho_step <= 0)) || ((theta_step <= 0)) ) {
919+
CV_Error( Error::StsBadArg, "step must be greater than 0" );
920+
}
921+
922+
int i;
923+
float irho = 1 / (float)rho_step;
924+
float irho_min = ((float)min_rho * irho);
925+
int numangle = cvRound((max_theta - min_theta) / theta_step);
926+
int numrho = cvRound((max_rho - min_rho + 1) / rho_step);
927+
928+
Mat _accum = Mat::zeros( (numangle+2), (numrho+2), CV_32SC1 );
929+
std::vector<int> _sort_buf;
930+
AutoBuffer<float> _tabSin(numangle);
931+
AutoBuffer<float> _tabCos(numangle);
932+
int *accum = _accum.ptr<int>();
933+
float *tabSin = _tabSin, *tabCos = _tabCos;
934+
935+
// create sin and cos table
936+
createTrigTable( numangle, min_theta, theta_step,
937+
irho, tabSin, tabCos );
938+
939+
// stage 1. fill accumlator
940+
for( i = 0; i < (int)point.size(); i++ )
941+
for(int n = 0; n < numangle; n++ )
942+
{
943+
int r = cvRound( point.at(i).x * tabCos[n] + point.at(i).y * tabSin[n] - irho_min);
944+
accum[(n+1) * (numrho+2) + r+1]++;
945+
}
946+
947+
// stage 2. find local maximums
948+
findLocalMaximums( numrho, numangle, threshold, accum, _sort_buf );
949+
950+
// stage 3. sort the detected lines by accumulator value
951+
std::sort(_sort_buf.begin(), _sort_buf.end(), hough_cmp_gt(accum));
952+
953+
// stage 4. store the first min(total,linesMax) lines to the output buffer
954+
lines_max = std::min(lines_max, (int)_sort_buf.size());
955+
double scale = 1./(numrho+2);
956+
for( i = 0; i < lines_max; i++ )
957+
{
958+
LinePolar line;
959+
int idx = _sort_buf[i];
960+
int n = cvFloor(idx*scale) - 1;
961+
int r = idx - (n+1)*(numrho+2) - 1;
962+
line.rho = static_cast<float>(min_rho) + r * (float)rho_step;
963+
line.angle = static_cast<float>(min_theta) + n * (float)theta_step;
964+
lines.push_back(Vec3d((double)accum[idx], (double)line.rho, (double)line.angle));
965+
}
966+
967+
Mat(lines).copyTo(_lines);
968+
}
969+
886970
/****************************************************************************************\
887971
* Circle Detection *
888972
\****************************************************************************************/
@@ -1611,7 +1695,6 @@ void HoughCircles( InputArray _image, OutputArray _circles,
16111695
{
16121696
HoughCircles(_image, _circles, method, dp, minDist, param1, param2, minRadius, maxRadius, -1, 3);
16131697
}
1614-
16151698
} // \namespace cv
16161699

16171700

modules/imgproc/test/test_houghlines.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ class ProbabilisticHoughLinesTest : public BaseHoughLineTest, public testing::Te
139139
}
140140
};
141141

142+
typedef tuple<double, double, double, double> HoughLinesPointSetInput_t;
143+
class HoughLinesPointSetTest : public testing::TestWithParam<HoughLinesPointSetInput_t>
144+
{
145+
protected:
146+
void run_test();
147+
double Rho;
148+
double Theta;
149+
double rhoMin, rhoMax, rhoStep;
150+
double thetaMin, thetaMax, thetaStep;
151+
public:
152+
HoughLinesPointSetTest()
153+
{
154+
rhoMin = get<0>(GetParam());
155+
rhoMax = get<1>(GetParam());
156+
rhoStep = (rhoMax - rhoMin) / 360.0f;
157+
thetaMin = get<2>(GetParam());
158+
thetaMax = get<3>(GetParam());
159+
thetaStep = CV_PI / 180.0f;
160+
Rho = 320.00000;
161+
Theta = 1.04719;
162+
}
163+
};
164+
142165
void BaseHoughLineTest::run_test(int type)
143166
{
144167
string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
@@ -195,6 +218,50 @@ void BaseHoughLineTest::run_test(int type)
195218
#endif
196219
}
197220

221+
void HoughLinesPointSetTest::run_test(void)
222+
{
223+
Mat lines_f, lines_i;
224+
vector<Point2f> pointf;
225+
vector<Point2i> pointi;
226+
vector<Vec3d> line_polar_f, line_polar_i;
227+
const float Points[20][2] = {
228+
{ 0.0f, 369.0f }, { 10.0f, 364.0f }, { 20.0f, 358.0f }, { 30.0f, 352.0f },
229+
{ 40.0f, 346.0f }, { 50.0f, 341.0f }, { 60.0f, 335.0f }, { 70.0f, 329.0f },
230+
{ 80.0f, 323.0f }, { 90.0f, 318.0f }, { 100.0f, 312.0f }, { 110.0f, 306.0f },
231+
{ 120.0f, 300.0f }, { 130.0f, 295.0f }, { 140.0f, 289.0f }, { 150.0f, 284.0f },
232+
{ 160.0f, 277.0f }, { 170.0f, 271.0f }, { 180.0f, 266.0f }, { 190.0f, 260.0f }
233+
};
234+
235+
// Float
236+
for (int i = 0; i < 20; i++)
237+
{
238+
pointf.push_back(Point2f(Points[i][0],Points[i][1]));
239+
}
240+
241+
HoughLinesPointSet(pointf, lines_f, 20, 1,
242+
rhoMin, rhoMax, rhoStep,
243+
thetaMin, thetaMax, thetaStep);
244+
245+
lines_f.copyTo( line_polar_f );
246+
247+
// Integer
248+
for( int i = 0; i < 20; i++ )
249+
{
250+
pointi.push_back( Point2i( (int)Points[i][0], (int)Points[i][1] ) );
251+
}
252+
253+
HoughLinesPointSet( pointi, lines_i, 20, 1,
254+
rhoMin, rhoMax, rhoStep,
255+
thetaMin, thetaMax, thetaStep );
256+
257+
lines_i.copyTo( line_polar_i );
258+
259+
EXPECT_EQ((int)(line_polar_f.at(0).val[1] * 100000.0f), (int)(Rho * 100000.0f));
260+
EXPECT_EQ((int)(line_polar_f.at(0).val[2] * 100000.0f), (int)(Theta * 100000.0f));
261+
EXPECT_EQ((int)(line_polar_i.at(0).val[1] * 100000.0f), (int)(Rho * 100000.0f));
262+
EXPECT_EQ((int)(line_polar_i.at(0).val[2] * 100000.0f), (int)(Theta * 100000.0f));
263+
}
264+
198265
TEST_P(StandartHoughLinesTest, regression)
199266
{
200267
run_test(STANDART);
@@ -205,6 +272,11 @@ TEST_P(ProbabilisticHoughLinesTest, regression)
205272
run_test(PROBABILISTIC);
206273
}
207274

275+
TEST_P(HoughLinesPointSetTest, regression)
276+
{
277+
run_test();
278+
}
279+
208280
INSTANTIATE_TEST_CASE_P( ImgProc, StandartHoughLinesTest, testing::Combine(testing::Values( "shared/pic5.png", "../stitching/a1.png" ),
209281
testing::Values( 1, 10 ),
210282
testing::Values( 0.05, 0.1 ),
@@ -219,4 +291,10 @@ INSTANTIATE_TEST_CASE_P( ImgProc, ProbabilisticHoughLinesTest, testing::Combine(
219291
testing::Values( 0, 4 )
220292
));
221293

294+
INSTANTIATE_TEST_CASE_P( Imgproc, HoughLinesPointSetTest, testing::Combine(testing::Values( 0.0f, 120.0f ),
295+
testing::Values( 360.0f, 480.0f ),
296+
testing::Values( 0.0f, (CV_PI / 18.0f) ),
297+
testing::Values( (CV_PI / 2.0f), (CV_PI * 5.0f / 12.0f) )
298+
));
299+
222300
}} // namespace
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <opencv2/opencv.hpp>
2+
3+
using namespace cv;
4+
using namespace std;
5+
6+
int main()
7+
{
8+
Mat lines;
9+
vector<Vec3d> line3d;
10+
vector<Point2f> point;
11+
const static float Points[20][2] = {
12+
{ 0.0f, 369.0f }, { 10.0f, 364.0f }, { 20.0f, 358.0f }, { 30.0f, 352.0f },
13+
{ 40.0f, 346.0f }, { 50.0f, 341.0f }, { 60.0f, 335.0f }, { 70.0f, 329.0f },
14+
{ 80.0f, 323.0f }, { 90.0f, 318.0f }, { 100.0f, 312.0f }, { 110.0f, 306.0f },
15+
{ 120.0f, 300.0f }, { 130.0f, 295.0f }, { 140.0f, 289.0f }, { 150.0f, 284.0f },
16+
{ 160.0f, 277.0f }, { 170.0f, 271.0f }, { 180.0f, 266.0f }, { 190.0f, 260.0f }
17+
};
18+
19+
for (int i = 0; i < 20; i++)
20+
{
21+
point.push_back(Point2f(Points[i][0],Points[i][1]));
22+
}
23+
24+
double rhoMin = 0.0f, rhoMax = 360.0f, rhoStep = 1;
25+
double thetaMin = 0.0f, thetaMax = CV_PI / 2.0f, thetaStep = CV_PI / 180.0f;
26+
27+
HoughLinesPointSet(point, lines, 20, 1,
28+
rhoMin, rhoMax, rhoStep,
29+
thetaMin, thetaMax, thetaStep);
30+
31+
lines.copyTo(line3d);
32+
printf("votes:%d, rho:%.7f, theta:%.7f\n",(int)line3d.at(0).val[0], line3d.at(0).val[1], line3d.at(0).val[2]);
33+
}

0 commit comments

Comments
 (0)