diff --git a/modules/imgproc/doc/feature_detection.rst b/modules/imgproc/doc/feature_detection.rst index 1c5d29c16..a6d5817dd 100644 --- a/modules/imgproc/doc/feature_detection.rst +++ b/modules/imgproc/doc/feature_detection.rst @@ -496,6 +496,110 @@ And this is the output of the above program in case of the probabilistic Hough t .. image:: pics/houghp.png +.. seealso:: + + :ocv:class:`LineSegmentDetector` + + + +LineSegmentDetector +------------------- +Line segment detector class, following the algorithm described at [Rafael12]_. + +.. ocv:class:: LineSegmentDetector : public Algorithm + + +createLineSegmentDetectorPtr +---------------------------- +Creates a smart pointer to a LineSegmentDetector object and initializes it. + +.. ocv:function:: Ptr<LineSegmentDetector> createLineSegmentDetectorPtr(int _refine = LSD_REFINE_STD, double _scale = 0.8, double _sigma_scale = 0.6, double _quant = 2.0, double _ang_th = 22.5, double _log_eps = 0, double _density_th = 0.7, int _n_bins = 1024) + + :param _refine: The way found lines will be refined: + + * **LSD_REFINE_NONE** - No refinement applied. + + * **LSD_REFINE_STD** - Standard refinement is applied. E.g. breaking arches into smaller straighter line approximations. + + * **LSD_REFINE_ADV** - Advanced refinement. Number of false alarms is calculated, lines are refined through increase of precision, decrement in size, etc. + + :param scale: The scale of the image that will be used to find the lines. Range (0..1]. + + :param sigma_scale: Sigma for Gaussian filter. It is computed as sigma = _sigma_scale/_scale. + + :param quant: Bound to the quantization error on the gradient norm. + + :param ang_th: Gradient angle tolerance in degrees. + + :param log_eps: Detection threshold: -log10(NFA) > log_eps. Used only when advancent refinement is chosen. + + :param density_th: Minimal density of aligned region points in the enclosing rectangle. + + :param n_bins: Number of bins in pseudo-ordering of gradient modulus. + +The LineSegmentDetector algorithm is defined using the standard values. Only advanced users may want to edit those, as to tailor it for their own application. + + +LineSegmentDetector::detect +--------------------------- +Finds lines in the input image. See the lsd_lines.cpp sample for possible usage. + +.. ocv:function:: void LineSegmentDetector::detect(const InputArray _image, OutputArray _lines, OutputArray width = noArray(), OutputArray prec = noArray(), OutputArray nfa = noArray()) + + :param _image A grayscale (CV_8UC1) input image. + If only a roi needs to be selected, use :: + lsd_ptr->detect(image(roi), lines, ...); + lines += Scalar(roi.x, roi.y, roi.x, roi.y); + + :param lines: A vector of Vec4i elements specifying the beginning and ending point of a line. Where Vec4i is (x1, y1, x2, y2), point 1 is the start, point 2 - end. Returned lines are strictly oriented depending on the gradient. + + :param width: Vector of widths of the regions, where the lines are found. E.g. Width of line. + + :param prec: Vector of precisions with which the lines are found. + + :param nfa: Vector containing number of false alarms in the line region, with precision of 10%. The bigger the value, logarithmically better the detection. + + * -1 corresponds to 10 mean false alarms + + * 0 corresponds to 1 mean false alarm + + * 1 corresponds to 0.1 mean false alarms + + This vector will be calculated only when the objects type is LSD_REFINE_ADV. + +This is the output of the default parameters of the algorithm on the above shown image. + +.. image:: pics/building_lsd.png + +.. note:: + + * An example using the LineSegmentDetector can be found at opencv_source_code/samples/cpp/lsd_lines.cpp + +LineSegmentDetector::drawSegments +--------------------------------- +Draws the line segments on a given image. + +.. ocv:function:: void LineSegmentDetector::drawSegments(InputOutputArray _image, InputArray lines) + + :param image: The image, where the liens will be drawn. Should be bigger or equal to the image, where the lines were found. + + :param lines: A vector of the lines that needed to be drawn. + + +LineSegmentDetector::compareSegments +------------------------------------ +Draws two groups of lines in blue and red, counting the non overlapping (mismatching) pixels. + +.. ocv:function:: int LineSegmentDetector::compareSegments(const Size& size, InputArray lines1, InputArray lines2, InputOutputArray _image = noArray()) + + :param size: The size of the image, where lines1 and lines2 were found. + + :param lines1: The first group of lines that needs to be drawn. It is visualized in blue color. + + :param lines2: The second group of lines. They visualized in red color. + + :param image: Optional image, where the lines will be drawn. The image should be color in order for lines1 and lines2 to be drawn in the above mentioned colors. + preCornerDetect @@ -542,3 +646,5 @@ The corners can be found as local maximums of the functions, as shown below: :: .. [Shi94] J. Shi and C. Tomasi. *Good Features to Track*. Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pages 593-600, June 1994. .. [Yuen90] Yuen, H. K. and Princen, J. and Illingworth, J. and Kittler, J., *Comparative study of Hough transform methods for circle finding*. Image Vision Comput. 8 1, pp 71–77 (1990) + +.. [Rafael12] Rafael Grompone von Gioi, Jérémie Jakubowicz, Jean-Michel Morel, and Gregory Randall, LSD: a Line Segment Detector, Image Processing On Line, vol. 2012. http://dx.doi.org/10.5201/ipol.2012.gjmr-lsd diff --git a/modules/imgproc/doc/pics/building_lsd.png b/modules/imgproc/doc/pics/building_lsd.png new file mode 100644 index 000000000..747029a65 Binary files /dev/null and b/modules/imgproc/doc/pics/building_lsd.png differ diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index 55816ccb7..00bdaa18c 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -904,7 +904,7 @@ class LineSegmentDetector : public Algorithm { public: /** - * Detect lines in the input image with the specified ROI. + * Detect lines in the input image. * * @param _image A grayscale(CV_8UC1) input image. * If only a roi needs to be selected, use @@ -913,8 +913,6 @@ public: * @param _lines Return: A vector of Vec4i elements specifying the beginning and ending point of a line. * Where Vec4i is (x1, y1, x2, y2), point 1 is the start, point 2 - end. * Returned lines are strictly oriented depending on the gradient. - * @param _roi Return: ROI of the image, where lines are to be found. If specified, the returning - * lines coordinates are image wise. * @param width Return: Vector of widths of the regions, where the lines are found. E.g. Width of line. * @param prec Return: Vector of precisions with which the lines are found. * @param nfa Return: Vector containing number of false alarms in the line region, with precision of 10%. @@ -935,18 +933,19 @@ public: * Should have the size of the image, where the lines were found * @param lines The lines that need to be drawn */ - virtual void drawSegments(InputOutputArray image, InputArray lines) = 0; + virtual void drawSegments(InputOutputArray _image, InputArray lines) = 0; /** * Draw both vectors on the image canvas. Uses blue for lines 1 and red for lines 2. * - * @param image The image, where lines will be drawn. - * Should have the size of the image, where the lines were found + * @param size The size of the image, where lines were found. * @param lines1 The first lines that need to be drawn. Color - Blue. * @param lines2 The second lines that need to be drawn. Color - Red. + * @param image Optional image, where lines will be drawn. + * Should have the size of the image, where the lines were found * @return The number of mismatching pixels between lines1 and lines2. */ - virtual int compareSegments(const Size& size, InputArray lines1, InputArray lines2, Mat* image = 0) = 0; + virtual int compareSegments(const Size& size, InputArray lines1, InputArray lines2, InputOutputArray _image = noArray()) = 0; virtual ~LineSegmentDetector() {}; }; diff --git a/modules/imgproc/src/lsd.cpp b/modules/imgproc/src/lsd.cpp index f468c6187..bb3895448 100644 --- a/modules/imgproc/src/lsd.cpp +++ b/modules/imgproc/src/lsd.cpp @@ -1,5 +1,6 @@ /*M/////////////////////////////////////////////////////////////////////////////////////// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, @@ -9,8 +10,7 @@ // License Agreement // For Open Source Computer Vision Library // -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2008-2011, Willow Garage Inc., all rights reserved. +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, @@ -185,7 +185,7 @@ public: double _log_eps = 0, double _density_th = 0.7, int _n_bins = 1024); /** - * Detect lines in the input image with the specified ROI. + * Detect lines in the input image. * * @param _image A grayscale(CV_8UC1) input image. * If only a roi needs to be selected, use @@ -194,8 +194,6 @@ public: * @param _lines Return: A vector of Vec4i elements specifying the beginning and ending point of a line. * Where Vec4i is (x1, y1, x2, y2), point 1 is the start, point 2 - end. * Returned lines are strictly oriented depending on the gradient. - * @param _roi Return: ROI of the image, where lines are to be found. If specified, the returning - * lines coordinates are image wise. * @param width Return: Vector of widths of the regions, where the lines are found. E.g. Width of line. * @param prec Return: Vector of precisions with which the lines are found. * @param nfa Return: Vector containing number of false alarms in the line region, with precision of 10%. @@ -216,18 +214,19 @@ public: * Should have the size of the image, where the lines were found * @param lines The lines that need to be drawn */ - void drawSegments(InputOutputArray image, InputArray lines); + void drawSegments(InputOutputArray _image, InputArray lines); /** * Draw both vectors on the image canvas. Uses blue for lines 1 and red for lines 2. * - * @param image The image, where lines will be drawn. - * Should have the size of the image, where the lines were found + * @param size The size of the image, where lines1 and lines2 were found. * @param lines1 The first lines that need to be drawn. Color - Blue. * @param lines2 The second lines that need to be drawn. Color - Red. + * @param image An optional image, where lines will be drawn. + * Should have the size of the image, where the lines were found * @return The number of mismatching pixels between lines1 and lines2. */ - int compareSegments(const Size& size, InputArray lines1, InputArray lines2, Mat* image = 0); + int compareSegments(const Size& size, InputArray lines1, InputArray lines2, InputOutputArray _image = noArray()); private: Mat image; @@ -336,7 +335,7 @@ private: * @param rec Return: The generated rectangle. */ void region2rect(const std::vector<RegionPoint>& reg, const int reg_size, const double reg_angle, - const double prec, const double p, rect& rec) const; + const double prec, const double p, rect& rec) const; /** * Compute region's angle as the principal inertia axis of the region. @@ -410,7 +409,7 @@ LineSegmentDetectorImpl::LineSegmentDetectorImpl(int _refine, double _scale, dou _n_bins > 0); } -void LineSegmentDetectorImpl::detect(const InputArray _image, OutputArray _lines, +void LineSegmentDetectorImpl::detect(InputArray _image, OutputArray _lines, OutputArray _width, OutputArray _prec, OutputArray _nfa) { Mat_<double> img = _image.getMat(); @@ -1150,7 +1149,7 @@ inline bool LineSegmentDetectorImpl::isAligned(const int& address, const double& } -void LineSegmentDetectorImpl::drawSegments(InputOutputArray _image, const InputArray lines) +void LineSegmentDetectorImpl::drawSegments(InputOutputArray _image, InputArray lines) { CV_Assert(!_image.empty() && (_image.channels() == 1 || _image.channels() == 3)); @@ -1186,10 +1185,10 @@ void LineSegmentDetectorImpl::drawSegments(InputOutputArray _image, const InputA } -int LineSegmentDetectorImpl::compareSegments(const Size& size, const InputArray lines1, const InputArray lines2, Mat* _image) +int LineSegmentDetectorImpl::compareSegments(const Size& size, InputArray lines1, InputArray lines2, InputOutputArray _image) { Size sz = size; - if (_image && _image->size() != size) sz = _image->size(); + if (_image.needed() && _image.size() != size) sz = _image.size(); CV_Assert(sz.area()); Mat_<uchar> I1 = Mat_<uchar>::zeros(sz); @@ -1219,14 +1218,11 @@ int LineSegmentDetectorImpl::compareSegments(const Size& size, const InputArray bitwise_xor(I1, I2, Ixor); int N = countNonZero(Ixor); - if (_image) + if (_image.needed()) { - Mat Ig; - if (_image->channels() == 1) - { - cvtColor(*_image, *_image, CV_GRAY2BGR); - } - CV_Assert(_image->isContinuous() && I1.isContinuous() && I2.isContinuous()); + CV_Assert(_image.channels() == 3); + Mat img = _image.getMatRef(); + CV_Assert(img.isContinuous() && I1.isContinuous() && I2.isContinuous()); for (unsigned int i = 0; i < I1.total(); ++i) { @@ -1234,11 +1230,12 @@ int LineSegmentDetectorImpl::compareSegments(const Size& size, const InputArray uchar i2 = I2.data[i]; if (i1 || i2) { - _image->data[3*i + 1] = 0; - if (i1) _image->data[3*i] = 255; - else _image->data[3*i] = 0; - if (i2) _image->data[3*i + 2] = 255; - else _image->data[3*i + 2] = 0; + unsigned int base_idx = i * 3; + if (i1) img.data[base_idx] = 255; + else img.data[base_idx] = 0; + img.data[base_idx + 1] = 0; + if (i2) img.data[base_idx + 2] = 255; + else img.data[base_idx + 2] = 0; } } }