Skip to content

(3.4) ximgproc: Added edge input feature to fast_line_detector #2915

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 1 commit into from
Apr 7, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ class CV_EXPORTS_W FastLineDetector : public Algorithm
hysteresis procedure in Canny()
@param _canny_th2 50 - Second threshold for
hysteresis procedure in Canny()
@param _canny_aperture_size 3 - Aperturesize for the sobel
operator in Canny()
@param _canny_aperture_size 3 - Aperturesize for the sobel operator in Canny().
If zero, Canny() is not applied and the input
image is taken as an edge image.
@param _do_merge false - If true, incremental merging of segments
will be performed
*/
Expand Down
5 changes: 3 additions & 2 deletions modules/ximgproc/samples/fld_lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ int main(int argc, char** argv)
// hysteresis procedure in Canny()
// canny_th2 50 - Second threshold for
// hysteresis procedure in Canny()
// canny_aperture_size 3 - Aperturesize for the sobel
// operator in Canny()
// canny_aperture_size 3 - Aperturesize for the sobel operator in Canny().
// If zero, Canny() is not applied and the input
// image is taken as an edge image.
// do_merge false - If true, incremental merging of segments
// will be performed
int length_threshold = 10;
Expand Down
23 changes: 15 additions & 8 deletions modules/ximgproc/src/fast_line_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ class FastLineDetectorImpl : public FastLineDetector
* _ hysteresis procedure in Canny()
* @param _canny_th2 50 - Second threshold for
* _ hysteresis procedure in Canny()
* @param _canny_aperture_size 3 - Aperturesize for the sobel
* _ operator in Canny()
* @param _canny_aperture_size 3 - Aperturesize for the sobel operator in Canny().
* If zero, Canny() is not applied and the input
* image is taken as an edge image.
* @param _do_merge false - If true, incremental merging of segments
will be perfomred
* will be performed
*/
FastLineDetectorImpl(int _length_threshold = 10, float _distance_threshold = 1.414213562f,
double _canny_th1 = 50.0, double _canny_th2 = 50.0, int _canny_aperture_size = 3,
Expand Down Expand Up @@ -80,7 +81,7 @@ class FastLineDetectorImpl : public FastLineDetector

double distPointLine(const Mat& p, Mat& l);

void extractSegments(const std::vector<Point2i>& points, std::vector<SEGMENT>& segments );
void extractSegments(const std::vector<Point2i>& points, std::vector<SEGMENT>& segments);

void lineDetection(const Mat& src, std::vector<SEGMENT>& segments_all);

Expand Down Expand Up @@ -113,7 +114,7 @@ FastLineDetectorImpl::FastLineDetectorImpl(int _length_threshold, float _distanc
canny_th1(_canny_th1), canny_th2(_canny_th2), canny_aperture_size(_canny_aperture_size), do_merge(_do_merge)
{
CV_Assert(_length_threshold > 0 && _distance_threshold > 0 &&
_canny_th1 > 0 && _canny_th2 > 0 && _canny_aperture_size > 0);
_canny_th1 > 0 && _canny_th2 > 0 && _canny_aperture_size >= 0);
}

void FastLineDetectorImpl::detect(InputArray _image, OutputArray _lines)
Expand Down Expand Up @@ -344,7 +345,7 @@ template<class T>
pt = T(pt_tmp);
}

void FastLineDetectorImpl::extractSegments(const std::vector<Point2i>& points, std::vector<SEGMENT>& segments )
void FastLineDetectorImpl::extractSegments(const std::vector<Point2i>& points, std::vector<SEGMENT>& segments)
{
bool is_line;

Expand Down Expand Up @@ -544,8 +545,14 @@ void FastLineDetectorImpl::lineDetection(const Mat& src, std::vector<SEGMENT>& s
std::vector<Point2i> points;
std::vector<SEGMENT> segments, segments_tmp;
Mat canny;
Canny(src, canny, canny_th1, canny_th2, canny_aperture_size);

if (canny_aperture_size == 0)
{
canny = src;
}
else
{
Canny(src, canny, canny_th1, canny_th2, canny_aperture_size);
}
canny.colRange(0,6).rowRange(0,6) = 0;
canny.colRange(src.cols-5,src.cols).rowRange(src.rows-5,src.rows) = 0;

Expand Down
28 changes: 28 additions & 0 deletions modules/ximgproc/test/test_fld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FLDBase : public testing::Test
void GenerateWhiteNoise(Mat& image);
void GenerateConstColor(Mat& image);
void GenerateLines(Mat& image, const unsigned int numLines);
void GenerateEdgeLines(Mat& image, const unsigned int numLines);
void GenerateBrokenLines(Mat& image, const unsigned int numLines);
void GenerateRotatedRect(Mat& image);
virtual void SetUp();
Expand Down Expand Up @@ -55,6 +56,7 @@ void FLDBase::GenerateConstColor(Mat& image)
image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 256)));
}


void FLDBase::GenerateLines(Mat& image, const unsigned int numLines)
{
image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 128)));
Expand All @@ -68,6 +70,19 @@ void FLDBase::GenerateLines(Mat& image, const unsigned int numLines)
}
}

void FLDBase::GenerateEdgeLines(Mat& image, const unsigned int numLines)
{
image = Mat(img_size, CV_8UC1, Scalar::all(0));

for(unsigned int i = 0; i < numLines; ++i)
{
int y = rng.uniform(10, img_size.width - 10);
Point p1(y, 10);
Point p2(y, img_size.height - 10);
line(image, p1, p2, Scalar(255), 1);
}
}

void FLDBase::GenerateBrokenLines(Mat& image, const unsigned int numLines)
{
image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 128)));
Expand Down Expand Up @@ -153,6 +168,19 @@ TEST_F(ximgproc_FLD, lines)
ASSERT_EQ(EPOCHS, passedtests);
}

TEST_F(ximgproc_FLD, edgeLines)
{
for (int i = 0; i < EPOCHS; ++i)
{
const unsigned int numOfLines = 1;
GenerateEdgeLines(test_image, numOfLines);
Ptr<FastLineDetector> detector = createFastLineDetector(10, 1.414213562f, 50, 50, 0);
detector->detect(test_image, lines);
if(numOfLines == lines.size()) ++passedtests;
}
ASSERT_EQ(EPOCHS, passedtests);
}

TEST_F(ximgproc_FLD, mergeLines)
{
for (int i = 0; i < EPOCHS; ++i)
Expand Down