From 930eca198d154799be57c5918ed3d13cefe8f154 Mon Sep 17 00:00:00 2001 From: TT <45615081+tsukada-cs@users.noreply.github.com> Date: Fri, 23 Oct 2020 02:03:11 +0900 Subject: [PATCH] 3.4/ximgproc: Added edge input feature to fast_line_detector backport of commit: d8197c6ad64468c8b199dcd2343184a51a366f92 --- .../opencv2/ximgproc/fast_line_detector.hpp | 5 ++-- modules/ximgproc/samples/fld_lines.cpp | 5 ++-- modules/ximgproc/src/fast_line_detector.cpp | 23 +++++++++------ modules/ximgproc/test/test_fld.cpp | 28 +++++++++++++++++++ 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/fast_line_detector.hpp b/modules/ximgproc/include/opencv2/ximgproc/fast_line_detector.hpp index 78ec43a61..7de0a8ba0 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/fast_line_detector.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/fast_line_detector.hpp @@ -65,8 +65,9 @@ public: 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 */ diff --git a/modules/ximgproc/samples/fld_lines.cpp b/modules/ximgproc/samples/fld_lines.cpp index acb4e32ac..87edf6242 100644 --- a/modules/ximgproc/samples/fld_lines.cpp +++ b/modules/ximgproc/samples/fld_lines.cpp @@ -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; diff --git a/modules/ximgproc/src/fast_line_detector.cpp b/modules/ximgproc/src/fast_line_detector.cpp index 33560184d..4217d9b33 100644 --- a/modules/ximgproc/src/fast_line_detector.cpp +++ b/modules/ximgproc/src/fast_line_detector.cpp @@ -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, @@ -80,7 +81,7 @@ class FastLineDetectorImpl : public FastLineDetector double distPointLine(const Mat& p, Mat& l); - void extractSegments(const std::vector& points, std::vector& segments ); + void extractSegments(const std::vector& points, std::vector& segments); void lineDetection(const Mat& src, std::vector& segments_all); @@ -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) @@ -344,7 +345,7 @@ template pt = T(pt_tmp); } -void FastLineDetectorImpl::extractSegments(const std::vector& points, std::vector& segments ) +void FastLineDetectorImpl::extractSegments(const std::vector& points, std::vector& segments) { bool is_line; @@ -544,8 +545,14 @@ void FastLineDetectorImpl::lineDetection(const Mat& src, std::vector& s std::vector points; std::vector 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; diff --git a/modules/ximgproc/test/test_fld.cpp b/modules/ximgproc/test/test_fld.cpp index 71f7fd260..d2dc84215 100644 --- a/modules/ximgproc/test/test_fld.cpp +++ b/modules/ximgproc/test/test_fld.cpp @@ -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(); @@ -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))); @@ -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))); @@ -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 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)