Commit ed207d79 authored by yuki takehara's avatar yuki takehara Committed by Alexander Alekhin

Merge pull request #11108 from take1014:hough_4303

* Added accumulator value to the output of HoughLines and HoughCircles

* imgproc: refactor Hough patch

- eliminate code duplication
- fix type handling, fix OpenCL code
- fix test data generation
- re-generated test data in debug mode via plain CPU code path
parent 2d5d98ec
......@@ -1977,10 +1977,11 @@ detection. See <http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm> for a good ex
transform.
@param image 8-bit, single-channel binary source image. The image may be modified by the function.
@param lines Output vector of lines. Each line is represented by a two-element vector
\f$(\rho, \theta)\f$ . \f$\rho\f$ is the distance from the coordinate origin \f$(0,0)\f$ (top-left corner of
@param lines Output vector of lines. Each line is represented by a 2 or 3 element vector
\f$(\rho, \theta)\f$ or \f$(\rho, \theta, \votes)\f$ . \f$\rho\f$ is the distance from the coordinate origin \f$(0,0)\f$ (top-left corner of
the image). \f$\theta\f$ is the line rotation angle in radians (
\f$0 \sim \textrm{vertical line}, \pi/2 \sim \textrm{horizontal line}\f$ ).
\f$\votes\f$ is the value of accumulator.
@param rho Distance resolution of the accumulator in pixels.
@param theta Angle resolution of the accumulator in radians.
@param threshold Accumulator threshold parameter. Only those lines are returned that get enough
......@@ -2155,8 +2156,8 @@ you know it. Or, you may set maxRadius to a negative number to return centers on
search, and find the correct radius using an additional procedure.
@param image 8-bit, single-channel, grayscale input image.
@param circles Output vector of found circles. Each vector is encoded as a 3-element
floating-point vector \f$(x, y, radius)\f$ .
@param circles Output vector of found circles. Each vector is encoded as 3 or 4 element
floating-point vector \f$(x, y, radius)\f$ or \f$(x, y, radius, votes)\f$ .
@param method Detection method, see #HoughModes. Currently, the only implemented method is #HOUGH_GRADIENT
@param dp Inverse ratio of the accumulator resolution to the image resolution. For example, if
dp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has
......
......@@ -56,4 +56,30 @@ PERF_TEST(PerfHoughCircles2, ManySmallCircles)
SANITY_CHECK_NOTHING();
}
PERF_TEST(PerfHoughCircles4f, Basic)
{
string filename = getDataPath("cv/imgproc/stuff.jpg");
const double dp = 1.0;
double minDist = 20;
double edgeThreshold = 20;
double accumThreshold = 30;
int minRadius = 20;
int maxRadius = 200;
Mat img = imread(filename, IMREAD_GRAYSCALE);
ASSERT_FALSE(img.empty()) << "Unable to load source image " << filename;
GaussianBlur(img, img, Size(9, 9), 2, 2);
vector<Vec4f> circles;
declare.in(img);
TEST_CYCLE()
{
HoughCircles(img, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
}
SANITY_CHECK_NOTHING();
}
} // namespace
......@@ -69,4 +69,47 @@ PERF_TEST_P(Image_RhoStep_ThetaStep_Threshold, HoughLines,
SANITY_CHECK_NOTHING();
}
PERF_TEST_P(Image_RhoStep_ThetaStep_Threshold, HoughLines3f,
testing::Combine(
testing::Values( "cv/shared/pic5.png", "stitching/a1.png" ),
testing::Values( 1, 10 ),
testing::Values( 0.01, 0.1 ),
testing::Values( 0.5, 1.1 )
)
)
{
string filename = getDataPath(get<0>(GetParam()));
double rhoStep = get<1>(GetParam());
double thetaStep = get<2>(GetParam());
double threshold_ratio = get<3>(GetParam());
Mat image = imread(filename, IMREAD_GRAYSCALE);
if (image.empty())
FAIL() << "Unable to load source image" << filename;
Canny(image, image, 32, 128);
// add some syntetic lines:
line(image, Point(0, 0), Point(image.cols, image.rows), Scalar::all(255), 3);
line(image, Point(image.cols, 0), Point(image.cols/2, image.rows), Scalar::all(255), 3);
vector<Vec3f> lines;
declare.time(60);
int hough_threshold = (int)(std::min(image.cols, image.rows) * threshold_ratio);
TEST_CYCLE() HoughLines(image, lines, rhoStep, thetaStep, hough_threshold);
printf("%dx%d: %d lines\n", image.cols, image.rows, (int)lines.size());
if (threshold_ratio < 1.0)
{
EXPECT_GE(lines.size(), 2u);
}
EXPECT_LT(lines.size(), 3000u);
SANITY_CHECK_NOTHING();
}
} // namespace
This diff is collapsed.
......@@ -49,6 +49,8 @@ namespace opencv_test { namespace {
#define DEBUG_IMAGES 0
#endif
//#define GENERATE_DATA // generate data in debug mode via CPU code path (without IPP / OpenCL and other accelerators)
using namespace cv;
using namespace std;
......@@ -109,7 +111,8 @@ public:
{
}
void run_test()
template <typename CircleType>
void run_test(const char* xml_name)
{
string test_case_name = getTestCaseName(picture_name, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
......@@ -118,7 +121,7 @@ public:
GaussianBlur(src, src, Size(9, 9), 2, 2);
vector<Vec3f> circles;
vector<CircleType> circles;
const double dp = 1.0;
HoughCircles(src, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
......@@ -127,31 +130,37 @@ public:
highlightCircles(filename, circles, imgProc + test_case_name + ".png");
#endif
string xml = imgProc + "HoughCircles.xml";
FileStorage fs(xml, FileStorage::READ);
FileNode node = fs[test_case_name];
if (node.empty())
string xml = imgProc + xml_name;
#ifdef GENERATE_DATA
{
fs.release();
fs.open(xml, FileStorage::APPEND);
FileStorage fs(xml, FileStorage::READ);
ASSERT_TRUE(!fs.isOpened() || fs[test_case_name].empty());
}
{
FileStorage fs(xml, FileStorage::APPEND);
EXPECT_TRUE(fs.isOpened()) << "Cannot open sanity data file: " << xml;
fs << test_case_name << circles;
fs.release();
fs.open(xml, FileStorage::READ);
EXPECT_TRUE(fs.isOpened()) << "Cannot open sanity data file: " << xml;
}
vector<Vec3f> exp_circles;
read(fs[test_case_name], exp_circles, vector<Vec3f>());
#else
FileStorage fs(xml, FileStorage::READ);
FileNode node = fs[test_case_name];
ASSERT_FALSE(node.empty()) << "Missing test data: " << test_case_name << std::endl << "XML: " << xml;
vector<CircleType> exp_circles;
read(fs[test_case_name], exp_circles, vector<CircleType>());
fs.release();
EXPECT_EQ(exp_circles.size(), circles.size());
#endif
}
};
TEST_P(HoughCirclesTestFixture, regression)
{
run_test();
run_test<Vec3f>("HoughCircles.xml");
}
TEST_P(HoughCirclesTestFixture, regression4f)
{
run_test<Vec4f>("HoughCircles4f.xml");
}
INSTANTIATE_TEST_CASE_P(ImgProc, HoughCirclesTestFixture, testing::Combine(
......@@ -186,7 +195,9 @@ TEST(HoughCirclesTest, DefaultMaxRadius)
GaussianBlur(src, src, Size(9, 9), 2, 2);
vector<Vec3f> circles;
vector<Vec4f> circles4f;
HoughCircles(src, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
HoughCircles(src, circles4f, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
#if DEBUG_IMAGES
string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
......@@ -220,7 +231,9 @@ TEST(HoughCirclesTest, CentersOnly)
GaussianBlur(src, src, Size(9, 9), 2, 2);
vector<Vec3f> circles;
vector<Vec4f> circles4f;
HoughCircles(src, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
HoughCircles(src, circles4f, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
#if DEBUG_IMAGES
string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
......@@ -231,6 +244,9 @@ TEST(HoughCirclesTest, CentersOnly)
for (size_t i = 0; i < circles.size(); ++i)
{
EXPECT_EQ(circles[i][2], 0.0f) << "Did not ask for radius";
EXPECT_EQ(circles[i][0], circles4f[i][0]);
EXPECT_EQ(circles[i][1], circles4f[i][1]);
EXPECT_EQ(circles[i][2], circles4f[i][2]);
}
}
......@@ -249,7 +265,9 @@ TEST(HoughCirclesTest, ManySmallCircles)
EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
vector<Vec3f> circles;
vector<Vec4f> circles4f;
HoughCircles(src, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
HoughCircles(src, circles4f, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
#if DEBUG_IMAGES
string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
......@@ -258,6 +276,7 @@ TEST(HoughCirclesTest, ManySmallCircles)
#endif
EXPECT_GT(circles.size(), size_t(3000)) << "Should find a lot of circles";
EXPECT_EQ(circles.size(), circles4f.size());
}
}} // namespace
......@@ -43,6 +43,8 @@
#include "test_precomp.hpp"
//#define GENERATE_DATA // generate data in debug mode via CPU code path (without IPP / OpenCL and other accelerators)
namespace opencv_test { namespace {
template<typename T>
......@@ -52,30 +54,36 @@ struct SimilarWith
float theta_eps;
float rho_eps;
SimilarWith<T>(T val, float e, float r_e): value(val), theta_eps(e), rho_eps(r_e) { };
bool operator()(T other);
bool operator()(const T& other);
};
template<>
bool SimilarWith<Vec2f>::operator()(Vec2f other)
bool SimilarWith<Vec2f>::operator()(const Vec2f& other)
{
return std::abs(other[0] - value[0]) < rho_eps && std::abs(other[1] - value[1]) < theta_eps;
}
template<>
bool SimilarWith<Vec3f>::operator()(const Vec3f& other)
{
return std::abs(other[0] - value[0]) < rho_eps && std::abs(other[1] - value[1]) < theta_eps;
}
template<>
bool SimilarWith<Vec4i>::operator()(Vec4i other)
bool SimilarWith<Vec4i>::operator()(const Vec4i& other)
{
return cv::norm(value, other) < theta_eps;
}
template <typename T>
int countMatIntersection(Mat expect, Mat actual, float eps, float rho_eps)
int countMatIntersection(const Mat& expect, const Mat& actual, float eps, float rho_eps)
{
int count = 0;
if (!expect.empty() && !actual.empty())
{
for (MatIterator_<T> it=expect.begin<T>(); it!=expect.end<T>(); it++)
for (MatConstIterator_<T> it=expect.begin<T>(); it!=expect.end<T>(); it++)
{
MatIterator_<T> f = std::find_if(actual.begin<T>(), actual.end<T>(), SimilarWith<T>(*it, eps, rho_eps));
MatConstIterator_<T> f = std::find_if(actual.begin<T>(), actual.end<T>(), SimilarWith<T>(*it, eps, rho_eps));
if (f != actual.end<T>())
count++;
}
......@@ -99,7 +107,8 @@ class BaseHoughLineTest
public:
enum {STANDART = 0, PROBABILISTIC};
protected:
void run_test(int type);
template<typename LinesType, typename LineType>
void run_test(int type, const char* xml_name);
string picture_name;
double rhoStep;
......@@ -162,60 +171,63 @@ public:
}
};
void BaseHoughLineTest::run_test(int type)
template<typename LinesType, typename LineType>
void BaseHoughLineTest::run_test(int type, const char* xml_name)
{
string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
Mat src = imread(filename, IMREAD_GRAYSCALE);
EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
ASSERT_FALSE(src.empty()) << "Invalid test image: " << filename;
string xml;
if (type == STANDART)
xml = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/HoughLines.xml";
else if (type == PROBABILISTIC)
xml = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/HoughLinesP.xml";
string xml = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/" + xml_name;
Mat dst;
Canny(src, dst, 100, 150, 3);
EXPECT_FALSE(dst.empty()) << "Failed Canny edge detector";
ASSERT_FALSE(dst.empty()) << "Failed Canny edge detector";
Mat lines;
LinesType lines;
if (type == STANDART)
HoughLines(dst, lines, rhoStep, thetaStep, threshold, 0, 0);
else if (type == PROBABILISTIC)
HoughLinesP(dst, lines, rhoStep, thetaStep, threshold, minLineLength, maxGap);
String test_case_name = format("lines_%s_%.0f_%.2f_%d_%d_%d", picture_name.c_str(), rhoStep, thetaStep,
threshold, minLineLength, maxGap);
threshold, minLineLength, maxGap);
test_case_name = getTestCaseName(test_case_name);
FileStorage fs(xml, FileStorage::READ);
FileNode node = fs[test_case_name];
if (node.empty())
#ifdef GENERATE_DATA
{
fs.release();
fs.open(xml, FileStorage::APPEND);
EXPECT_TRUE(fs.isOpened()) << "Cannot open sanity data file: " << xml;
fs << test_case_name << lines;
fs.release();
fs.open(xml, FileStorage::READ);
FileStorage fs(xml, FileStorage::READ);
ASSERT_TRUE(!fs.isOpened() || fs[test_case_name].empty());
}
{
FileStorage fs(xml, FileStorage::APPEND);
EXPECT_TRUE(fs.isOpened()) << "Cannot open sanity data file: " << xml;
fs << test_case_name << Mat(lines);
}
#else
FileStorage fs(xml, FileStorage::READ);
FileNode node = fs[test_case_name];
ASSERT_FALSE(node.empty()) << "Missing test data: " << test_case_name << std::endl << "XML: " << xml;
Mat exp_lines;
read( fs[test_case_name], exp_lines, Mat() );
Mat exp_lines_;
read(fs[test_case_name], exp_lines_, Mat());
fs.release();
LinesType exp_lines;
exp_lines_.copyTo(exp_lines);
int count = -1;
if (type == STANDART)
count = countMatIntersection<Vec2f>(exp_lines, lines, (float) thetaStep + FLT_EPSILON, (float) rhoStep + FLT_EPSILON);
count = countMatIntersection<LineType>(Mat(exp_lines), Mat(lines), (float) thetaStep + FLT_EPSILON, (float) rhoStep + FLT_EPSILON);
else if (type == PROBABILISTIC)
count = countMatIntersection<Vec4i>(exp_lines, lines, 1e-4f, 0.f);
count = countMatIntersection<LineType>(Mat(exp_lines), Mat(lines), 1e-4f, 0.f);
#if defined HAVE_IPP && IPP_VERSION_X100 >= 810 && !IPP_DISABLE_HOUGH
EXPECT_GE( count, (int) (exp_lines.total() * 0.8) );
EXPECT_LE(std::abs((double)count - Mat(exp_lines).total()), Mat(exp_lines).total() * 0.25)
<< "count=" << count << " expected=" << Mat(exp_lines).total();
#else
EXPECT_EQ( count, (int) exp_lines.total());
EXPECT_EQ(count, (int)Mat(exp_lines).total());
#endif
#endif // GENERATE_DATA
}
void HoughLinesPointSetTest::run_test(void)
......@@ -264,12 +276,22 @@ void HoughLinesPointSetTest::run_test(void)
TEST_P(StandartHoughLinesTest, regression)
{
run_test(STANDART);
run_test<Mat, Vec2f>(STANDART, "HoughLines.xml");
}
TEST_P(ProbabilisticHoughLinesTest, regression)
{
run_test(PROBABILISTIC);
run_test<Mat, Vec4i>(PROBABILISTIC, "HoughLinesP.xml");
}
TEST_P(StandartHoughLinesTest, regression_Vec2f)
{
run_test<std::vector<Vec2f>, Vec2f>(STANDART, "HoughLines2f.xml");
}
TEST_P(StandartHoughLinesTest, regression_Vec3f)
{
run_test<std::vector<Vec3f>, Vec3f>(STANDART, "HoughLines3f.xml");
}
TEST_P(HoughLinesPointSetTest, regression)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment