Skip to content

Commit 31c657e

Browse files
committed
Merge pull request #2912 from sturkmen72:update_fast_line_detector
2 parents cc36f58 + 14b4301 commit 31c657e

File tree

2 files changed

+46
-105
lines changed

2 files changed

+46
-105
lines changed

modules/ximgproc/include/opencv2/ximgproc/fast_line_detector.hpp

+20-23
Original file line numberDiff line numberDiff line change
@@ -33,48 +33,45 @@ class CV_EXPORTS_W FastLineDetector : public Algorithm
3333
3434
![image](pics/corridor_fld.jpg)
3535
36-
@param _image A grayscale (CV_8UC1) input image. If only a roi needs to be
36+
@param image A grayscale (CV_8UC1) input image. If only a roi needs to be
3737
selected, use: `fld_ptr-\>detect(image(roi), lines, ...);
3838
lines += Scalar(roi.x, roi.y, roi.x, roi.y);`
39-
@param _lines A vector of Vec4f elements specifying the beginning
39+
@param lines A vector of Vec4f elements specifying the beginning
4040
and ending point of a line. Where Vec4f is (x1, y1, x2, y2), point
4141
1 is the start, point 2 - end. Returned lines are directed so that the
4242
brighter side is on their left.
4343
*/
44-
CV_WRAP virtual void detect(InputArray _image, OutputArray _lines) = 0;
44+
CV_WRAP virtual void detect(InputArray image, OutputArray lines) = 0;
4545

4646
/** @brief Draws the line segments on a given image.
47-
@param _image The image, where the lines will be drawn. Should be bigger
47+
@param image The image, where the lines will be drawn. Should be bigger
4848
or equal to the image, where the lines were found.
4949
@param lines A vector of the lines that needed to be drawn.
5050
@param draw_arrow If true, arrow heads will be drawn.
51-
*/
52-
CV_WRAP virtual void drawSegments(InputOutputArray _image, InputArray lines,
53-
bool draw_arrow = false) = 0;
51+
@param linecolor Line color.
52+
@param linethickness Line thickness.
53+
*/
54+
CV_WRAP virtual void drawSegments(InputOutputArray image, InputArray lines,
55+
bool draw_arrow = false, Scalar linecolor = Scalar(0, 0, 255), int linethickness = 1) = 0;
5456

5557
virtual ~FastLineDetector() { }
5658
};
5759

5860
/** @brief Creates a smart pointer to a FastLineDetector object and initializes it
5961
60-
@param _length_threshold 10 - Segment shorter than this will be discarded
61-
@param _distance_threshold 1.41421356 - A point placed from a hypothesis line
62-
segment farther than this will be
63-
regarded as an outlier
64-
@param _canny_th1 50 - First threshold for
65-
hysteresis procedure in Canny()
66-
@param _canny_th2 50 - Second threshold for
67-
hysteresis procedure in Canny()
68-
@param _canny_aperture_size 3 - Aperturesize for the sobel operator in Canny().
69-
If zero, Canny() is not applied and the input
70-
image is taken as an edge image.
71-
@param _do_merge false - If true, incremental merging of segments
72-
will be performed
62+
@param length_threshold Segment shorter than this will be discarded
63+
@param distance_threshold A point placed from a hypothesis line
64+
segment farther than this will be regarded as an outlier
65+
@param canny_th1 First threshold for hysteresis procedure in Canny()
66+
@param canny_th2 Second threshold for hysteresis procedure in Canny()
67+
@param canny_aperture_size Aperturesize for the sobel operator in Canny().
68+
If zero, Canny() is not applied and the input image is taken as an edge image.
69+
@param do_merge If true, incremental merging of segments will be performed
7370
*/
7471
CV_EXPORTS_W Ptr<FastLineDetector> createFastLineDetector(
75-
int _length_threshold = 10, float _distance_threshold = 1.414213562f,
76-
double _canny_th1 = 50.0, double _canny_th2 = 50.0, int _canny_aperture_size = 3,
77-
bool _do_merge = false);
72+
int length_threshold = 10, float distance_threshold = 1.414213562f,
73+
double canny_th1 = 50.0, double canny_th2 = 50.0, int canny_aperture_size = 3,
74+
bool do_merge = false);
7875

7976
//! @} ximgproc_fast_line_detector
8077
}

modules/ximgproc/src/fast_line_detector.cpp

+26-82
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,14 @@ namespace ximgproc{
2121
class FastLineDetectorImpl : public FastLineDetector
2222
{
2323
public:
24-
/**
25-
* @param _length_threshold 10 - Segment shorter than this will be discarded
26-
* @param _distance_threshold 1.41421356 - A point placed from a hypothesis line segment
27-
* farther than this will be regarded as an outlier
28-
* @param _canny_th1 50 - First threshold for
29-
* _ hysteresis procedure in Canny()
30-
* @param _canny_th2 50 - Second threshold for
31-
* _ hysteresis procedure in Canny()
32-
* @param _canny_aperture_size 3 - Aperturesize for the sobel operator in Canny().
33-
* If zero, Canny() is not applied and the input
34-
* image is taken as an edge image.
35-
* @param _do_merge false - If true, incremental merging of segments
36-
* will be performed
37-
*/
24+
3825
FastLineDetectorImpl(int _length_threshold = 10, float _distance_threshold = 1.414213562f,
3926
double _canny_th1 = 50.0, double _canny_th2 = 50.0, int _canny_aperture_size = 3,
4027
bool _do_merge = false);
4128

42-
/**
43-
* Detect lines in the input image.
44-
*
45-
* @param _image A grayscale(CV_8UC1) input image.
46-
* If only a roi needs to be selected, use
47-
* lsd_ptr->detect(image(roi), ..., lines);
48-
* lines += Scalar(roi.x, roi.y, roi.x, roi.y);
49-
* @param _lines Return: A vector of Vec4f elements specifying the beginning and ending point of
50-
* a line. Where Vec4f is (x1, y1, x2, y2), point 1 is the start, point 2 is the end.
51-
* Returned lines are directed so that the brighter side is placed on left.
52-
*/
53-
void detect(InputArray _image, OutputArray _lines) CV_OVERRIDE;
54-
55-
/**
56-
* Draw lines on the given canvas.
57-
*
58-
* @param image The image, where lines will be drawn
59-
* Should have the size of the image, where the lines were found
60-
* @param lines The lines that need to be drawn
61-
* @param draw_arrow If true, arrow heads will be drawn
62-
*/
63-
void drawSegments(InputOutputArray _image, InputArray lines, bool draw_arrow = false) CV_OVERRIDE;
29+
void detect(InputArray image, OutputArray lines) CV_OVERRIDE;
30+
31+
void drawSegments(InputOutputArray image, InputArray lines, bool draw_arrow = false, Scalar linecolor = Scalar(0, 0, 255), int linethickness = 1) CV_OVERRIDE;
6432

6533
private:
6634
int imagewidth, imageheight, threshold_length;
@@ -85,25 +53,24 @@ class FastLineDetectorImpl : public FastLineDetector
8553

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

88-
void pointInboardTest(const Mat& src, Point2i& pt);
56+
void pointInboardTest(const Size srcSize, Point2i& pt);
8957

9058
inline void getAngle(SEGMENT& seg);
9159

9260
void additionalOperationsOnSegment(const Mat& src, SEGMENT& seg);
9361

94-
void drawSegment(Mat& mat, const SEGMENT& seg, Scalar bgr = Scalar(0,255,0),
95-
int thickness = 1, bool directed = true);
62+
void drawSegment(InputOutputArray image, const SEGMENT& seg, Scalar bgr = Scalar(0,255,0), int thickness = 1, bool directed = true);
9663
};
9764

9865
/////////////////////////////////////////////////////////////////////////////////////////
9966

10067
CV_EXPORTS Ptr<FastLineDetector> createFastLineDetector(
101-
int _length_threshold, float _distance_threshold,
102-
double _canny_th1, double _canny_th2, int _canny_aperture_size, bool _do_merge)
68+
int length_threshold, float distance_threshold,
69+
double canny_th1, double canny_th2, int canny_aperture_size, bool do_merge)
10370
{
10471
return makePtr<FastLineDetectorImpl>(
105-
_length_threshold, _distance_threshold,
106-
_canny_th1, _canny_th2, _canny_aperture_size, _do_merge);
72+
length_threshold, distance_threshold,
73+
canny_th1, canny_th2, canny_aperture_size, do_merge);
10774
}
10875

10976
/////////////////////////////////////////////////////////////////////////////////////////
@@ -136,30 +103,23 @@ void FastLineDetectorImpl::detect(InputArray _image, OutputArray _lines)
136103
Mat(lines).copyTo(_lines);
137104
}
138105

139-
void FastLineDetectorImpl::drawSegments(InputOutputArray _image, InputArray lines, bool draw_arrow)
106+
void FastLineDetectorImpl::drawSegments(InputOutputArray image, InputArray lines, bool draw_arrow, Scalar linecolor, int linethickness)
140107
{
141108
CV_INSTRUMENT_REGION();
142109

143-
CV_Assert(!_image.empty() && (_image.channels() == 1 || _image.channels() == 3));
110+
int cn = image.channels();
111+
CV_Assert(!image.empty() && ( cn == 1 || cn == 3 || cn == 4));
144112

145-
Mat gray;
146-
if (_image.channels() == 1)
113+
if (cn == 1)
147114
{
148-
gray = _image.getMatRef();
115+
cvtColor(image, image, COLOR_GRAY2BGR);
149116
}
150-
else if (_image.channels() == 3)
117+
else
151118
{
152-
cvtColor(_image, gray, COLOR_BGR2GRAY);
119+
cvtColor(image, image, COLOR_BGRA2GRAY);
120+
cvtColor(image, image, cn == 3 ? COLOR_GRAY2BGR : COLOR_GRAY2BGRA);
153121
}
154122

155-
// Create a 3 channel image in order to draw colored lines
156-
std::vector<Mat> planes;
157-
planes.push_back(gray);
158-
planes.push_back(gray);
159-
planes.push_back(gray);
160-
161-
merge(planes, _image);
162-
163123
double gap = 10.0;
164124
double arrow_angle = 30.0;
165125

@@ -172,7 +132,7 @@ void FastLineDetectorImpl::drawSegments(InputOutputArray _image, InputArray line
172132
const Vec4f& v = _lines.at<Vec4f>(i);
173133
Point2f b(v[0], v[1]);
174134
Point2f e(v[2], v[3]);
175-
line(_image.getMatRef(), b, e, Scalar(0, 0, 255), 1);
135+
line(image, b, e, linecolor, linethickness);
176136
if(draw_arrow)
177137
{
178138
SEGMENT seg;
@@ -185,8 +145,8 @@ void FastLineDetectorImpl::drawSegments(InputOutputArray _image, InputArray line
185145
Point2i p1;
186146
p1.x = cvRound(seg.x2 - gap*cos(arrow_angle * CV_PI / 180.0 + ang));
187147
p1.y = cvRound(seg.y2 - gap*sin(arrow_angle * CV_PI / 180.0 + ang));
188-
pointInboardTest(_image.getMatRef(), p1);
189-
line(_image.getMatRef(), Point(cvRound(seg.x2), cvRound(seg.y2)), p1, Scalar(0,0,255), 1);
148+
pointInboardTest(image.size(), p1);
149+
line(image, Point(cvRound(seg.x2), cvRound(seg.y2)), p1, linecolor, linethickness);
190150
}
191151
}
192152
}
@@ -477,10 +437,10 @@ void FastLineDetectorImpl::extractSegments(const std::vector<Point2i>& points, s
477437
}
478438
}
479439

480-
void FastLineDetectorImpl::pointInboardTest(const Mat& src, Point2i& pt)
440+
void FastLineDetectorImpl::pointInboardTest(const Size srcSize, Point2i& pt)
481441
{
482-
pt.x = pt.x <= 5 ? 5 : pt.x >= src.cols - 5 ? src.cols - 5 : pt.x;
483-
pt.y = pt.y <= 5 ? 5 : pt.y >= src.rows - 5 ? src.rows - 5 : pt.y;
442+
pt.x = pt.x <= 5 ? 5 : pt.x >= srcSize.width - 5 ? srcSize.width - 5 : pt.x;
443+
pt.y = pt.y <= 5 ? 5 : pt.y >= srcSize.height - 5 ? srcSize.height - 5 : pt.y;
484444
}
485445

486446
bool FastLineDetectorImpl::getPointChain(const Mat& img, Point pt,
@@ -692,8 +652,8 @@ void FastLineDetectorImpl::additionalOperationsOnSegment(const Mat& src, SEGMENT
692652
points_right[i].y = cvRound(points[i].y + gap*sin(90.0 * CV_PI / 180.0 + ang));
693653
points_left[i].x = cvRound(points[i].x - gap*cos(90.0 * CV_PI / 180.0 + ang));
694654
points_left[i].y = cvRound(points[i].y - gap*sin(90.0 * CV_PI / 180.0 + ang));
695-
pointInboardTest(src, points_right[i]);
696-
pointInboardTest(src, points_left[i]);
655+
pointInboardTest(src.size(), points_right[i]);
656+
pointInboardTest(src.size(), points_left[i]);
697657
}
698658

699659
int iR = 0, iL = 0;
@@ -717,21 +677,5 @@ void FastLineDetectorImpl::additionalOperationsOnSegment(const Mat& src, SEGMENT
717677
return;
718678
}
719679

720-
void FastLineDetectorImpl::drawSegment(Mat& mat, const SEGMENT& seg, Scalar bgr, int thickness, bool directed)
721-
{
722-
double gap = 10.0;
723-
double ang = (double)seg.angle;
724-
double arrow_angle = 30.0;
725-
726-
Point2i p1;
727-
p1.x = cvRound(seg.x2 - gap*cos(arrow_angle * CV_PI / 180.0 + ang));
728-
p1.y = cvRound(seg.y2 - gap*sin(arrow_angle * CV_PI / 180.0 + ang));
729-
pointInboardTest(mat, p1);
730-
731-
line(mat, Point(cvRound(seg.x1), cvRound(seg.y1)),
732-
Point(cvRound(seg.x2), cvRound(seg.y2)), bgr, thickness, 1);
733-
if(directed)
734-
line(mat, Point(cvRound(seg.x2), cvRound(seg.y2)), p1, bgr, thickness, 1);
735-
}
736680
} // namespace cv
737681
} // namespace ximgproc

0 commit comments

Comments
 (0)