2011-02-23 04:43:26 +08:00
Feature Detection
=================
2011-04-19 19:41:12 +08:00
.. highlight :: cpp
2011-02-23 04:43:26 +08:00
.. index :: Canny
2011-03-09 06:22:24 +08:00
.. _Canny:
2011-03-01 05:26:43 +08:00
Canny
2011-02-23 04:43:26 +08:00
---------
2011-03-09 06:22:24 +08:00
.. c:function :: void Canny( const Mat& image, Mat& edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false )
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
Finds edges in an image using the Canny algorithm.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param image: Single-channel 8-bit input image.
2011-02-26 19:05:10 +08:00
2011-04-30 21:53:08 +08:00
:param edges: Output edge map. It has the same size and type as ``image`` .
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
:param threshold1: The first threshold for the hysteresis procedure.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param threshold2: The second threshold for the hysteresis procedure.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param apertureSize: Aperture size for the :func:`Sobel` operator.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param L2gradient: Flag indicating whether a more accurate :math:`L_2` norm :math:`=\sqrt{(dI/dx)^2 + (dI/dy)^2}` should be used to compute the image gradient magnitude ( ``L2gradient=true`` ), or a faster default :math:`L_1` norm :math:`=|dI/dx|+|dI/dy|` is enough ( ``L2gradient=false`` ).
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
The function finds edges in the input image `` image `` and marks them in the output map `` edges `` using the Canny algorithm. The smallest value between `` threshold1 `` and `` threshold2 `` is used for edge linking. The largest value is used to find initial segments of strong edges. See
2011-02-23 04:43:26 +08:00
http://en.wikipedia.org/wiki/Canny_edge_detector
.. index :: cornerEigenValsAndVecs
2011-03-09 06:22:24 +08:00
.. _cornerEigenValsAndVecs:
2011-03-01 05:26:43 +08:00
cornerEigenValsAndVecs
2011-03-09 06:22:24 +08:00
----------------------
.. c:function :: void cornerEigenValsAndVecs( const Mat& src, Mat& dst, int blockSize, int apertureSize, int borderType=BORDER_DEFAULT )
2011-02-23 04:43:26 +08:00
Calculates eigenvalues and eigenvectors of image blocks for corner detection.
2011-04-30 21:53:08 +08:00
:param src: Input single-channel 8-bit or floating-point image.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param dst: Image to store the results. It has the same size as ``src`` and the type ``CV_32FC(6)`` .
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
:param blockSize: Neighborhood size (see details below).
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param apertureSize: Aperture parameter for the :func:`Sobel` operator.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param boderType: Pixel extrapolation method. See :func:`borderInterpolate` .
2011-03-01 05:26:43 +08:00
2011-02-26 19:05:10 +08:00
For every pixel
:math: `p` , the function `` cornerEigenValsAndVecs `` considers a `` blockSize `` :math: `\times` `` blockSize `` neigborhood
:math: `S(p)` . It calculates the covariation matrix of derivatives over the neighborhood as:
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
M = \begin{bmatrix} \sum _{S(p)}(dI/dx)^2 & \sum _{S(p)}(dI/dx dI/dy)^2 \\ \sum _{S(p)}(dI/dx dI/dy)^2 & \sum _{S(p)}(dI/dy)^2 \end{bmatrix}
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
where the derivatives are computed using the
2011-02-26 19:05:10 +08:00
:func: `Sobel` operator.
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
After that it finds eigenvectors and eigenvalues of
2011-04-30 21:53:08 +08:00
:math: `M` and stores them in the destination image as
2011-02-26 19:05:10 +08:00
:math: `(\lambda_1, \lambda_2, x_1, y_1, x_2, y_2)` where
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
* :math: `\lambda_1, \lambda_2` are the non-sorted eigenvalues of :math: `M`
2011-03-01 05:26:43 +08:00
* :math: `x_1, y_1` are the eigenvectors corresponding to :math: `\lambda_1`
* :math: `x_2, y_2` are the eigenvectors corresponding to :math: `\lambda_2`
2011-02-23 04:43:26 +08:00
The output of the function can be used for robust edge or corner detection.
2011-04-30 21:53:08 +08:00
See Also:
:func: `cornerMinEigenVal` ,
:func: `cornerHarris` ,
:func: `preCornerDetect`
2011-03-09 06:22:24 +08:00
2011-02-23 04:43:26 +08:00
.. index :: cornerHarris
2011-03-09 06:22:24 +08:00
.. _cornerHarris:
2011-03-01 05:26:43 +08:00
cornerHarris
2011-03-09 06:22:24 +08:00
------------
.. c:function :: void cornerHarris( const Mat& src, Mat& dst, int blockSize, int apertureSize, double k, int borderType=BORDER_DEFAULT )
2011-02-23 04:43:26 +08:00
Harris edge detector.
2011-04-30 21:53:08 +08:00
:param src: Input single-channel 8-bit or floating-point image.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param dst: Image to store the Harris detector responses. It has the type ``CV_32FC1`` and the same size as ``src`` .
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
:param blockSize: Neighborhood size (see the details on :func:`cornerEigenValsAndVecs` ).
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param apertureSize: Aperture parameter for the :func:`Sobel` operator.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param k: Harris detector free parameter. See the formula below.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param boderType: Pixel extrapolation method. See :func:`borderInterpolate` .
2011-03-01 05:26:43 +08:00
2011-02-26 19:05:10 +08:00
The function runs the Harris edge detector on the image. Similarly to
:func: `cornerMinEigenVal` and
:func: `cornerEigenValsAndVecs` , for each pixel
:math: `(x, y)` it calculates a
:math: `2\times2` gradient covariation matrix
:math: `M^{(x,y)}` over a
:math: `\texttt{blockSize} \times \texttt{blockSize}` neighborhood. Then, it computes the following characteristic:
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\texttt{dst} (x,y) = \mathrm{det} M^{(x,y)} - k \cdot \left ( \mathrm{tr} M^{(x,y)} \right )^2
2011-02-23 04:43:26 +08:00
Corners in the image can be found as the local maxima of this response map.
.. index :: cornerMinEigenVal
2011-03-09 06:22:24 +08:00
.. _cornerMinEigenVal:
2011-03-01 05:26:43 +08:00
cornerMinEigenVal
2011-03-09 06:22:24 +08:00
-----------------
.. c:function :: void cornerMinEigenVal( const Mat& src, Mat& dst, int blockSize, int apertureSize=3, int borderType=BORDER_DEFAULT )
2011-02-23 04:43:26 +08:00
Calculates the minimal eigenvalue of gradient matrices for corner detection.
2011-04-30 21:53:08 +08:00
:param src: Input single-channel 8-bit or floating-point image.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param dst: Image to store the minimal eigenvalues. It has the type ``CV_32FC1`` and the same size as ``src`` .
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
:param blockSize: Neighborhood size (see the details on :func:`cornerEigenValsAndVecs` ).
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param apertureSize: Aperture parameter for the :func:`Sobel` operator.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param boderType: Pixel extrapolation method. See :func:`borderInterpolate` .
2011-03-01 05:26:43 +08:00
2011-02-26 19:05:10 +08:00
The function is similar to
2011-04-30 21:53:08 +08:00
:func: `cornerEigenValsAndVecs` but it calculates and stores only the minimal eigenvalue of the covariation matrix of derivatives, that is,
:math: `\min(\lambda_1, \lambda_2)` in terms of the formulae in the
2011-02-26 19:05:10 +08:00
:func: `cornerEigenValsAndVecs` description.
2011-02-23 04:43:26 +08:00
.. index :: cornerSubPix
2011-03-09 06:22:24 +08:00
.. _cornerSubPix:
2011-03-01 05:26:43 +08:00
cornerSubPix
2011-02-23 04:43:26 +08:00
----------------
2011-03-09 06:22:24 +08:00
.. c:function :: void cornerSubPix( const Mat& image, vector<Point2f>& corners, Size winSize, Size zeroZone, TermCriteria criteria )
2011-02-23 04:43:26 +08:00
Refines the corner locations.
2011-04-30 21:53:08 +08:00
:param image: Input image.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param corners: Initial coordinates of the input corners and refined coordinates provided for output.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param winSize: Half of the side length of the search window. For example, if ``winSize=Size(5,5)`` , then a :math:`5*2+1 \times 5*2+1 = 11 \times 11` search window is used.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param zeroZone: Half of the size of the dead region in the middle of the search zone over which the summation in the formula below is not done. It is used sometimes to avoid possible singularities of the autocorrelation matrix. The value of (-1,-1) indicates that there is no such a size.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param criteria: Criteria for termination of the iterative process of corner refinement. That is, the process of corner position refinement stops either after a certain number of iterations or when a required accuracy is achieved. The ``criteria`` may specify either the maximum number of iteration or the required accuracy, or both of them.??
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
The function iterates to find the sub-pixel accurate location of corners or radial saddle points, as shown on the picture below.
2011-02-23 04:43:26 +08:00
2011-03-06 05:26:13 +08:00
.. image :: pics/cornersubpix.png
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
Sub-pixel accurate corner locator is based on the observation that every vector from the center
:math: `q` to a point
:math: `p` located within a neighborhood of
:math: `q` is orthogonal to the image gradient at
:math: `p` subject to image and measurement noise. Consider the expression:
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\epsilon _i = {DI_{p_i}}^T \cdot (q - p_i)
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
where
2011-04-30 21:53:08 +08:00
:math: `{DI_{p_i}}` is an image gradient at one of the points
2011-02-26 19:05:10 +08:00
:math: `p_i` in a neighborhood of
:math: `q` . The value of
2011-04-30 21:53:08 +08:00
:math: `q` is to be found so that
2011-02-26 19:05:10 +08:00
:math: `\epsilon_i` is minimized. A system of equations may be set up with
:math: `\epsilon_i` set to zero:
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\sum _i(DI_{p_i} \cdot {DI_{p_i}}^T) - \sum _i(DI_{p_i} \cdot {DI_{p_i}}^T \cdot p_i)
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
where the gradients are summed within a neighborhood ("search window") of
:math: `q` . Calling the first gradient term
:math: `G` and the second gradient term
:math: `b` gives:
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
q = G^{-1} \cdot b
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
The algorithm sets the center of the neighborhood window at this new center
2011-04-30 21:53:08 +08:00
:math: `q` and then iterates until the center stays within a set threshold.
2011-02-23 04:43:26 +08:00
.. index :: goodFeaturesToTrack
2011-03-09 06:22:24 +08:00
.. _goodFeaturesToTrack:
2011-03-01 05:26:43 +08:00
goodFeaturesToTrack
2011-03-09 06:22:24 +08:00
-------------------
.. c:function :: void goodFeaturesToTrack( const Mat& image, vector<Point2f>& corners, int maxCorners, double qualityLevel, double minDistance, const Mat& mask=Mat(), int blockSize=3, bool useHarrisDetector=false, double k=0.04 )
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
Determines strong corners on an image.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param image: Input 8-bit or floating-point 32-bit, single-channel image.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param corners: Output vector of detected corners.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param maxCorners: Maximum number of corners to return. If there are more corners than are found, the strongest of them is returned.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param qualityLevel: Parameter characterizing the minimal accepted quality of image corners. The parameter value is multiplied by the best corner quality measure, which is the minimal eigenvalue (see :func:`cornerMinEigenVal` ) or the Harris function response (see :func:`cornerHarris` ). The corners with the quality measure less than the product are rejected. For example, if the best corner has the quality measure = 1500, and the ``qualityLevel=0.01`` , then all the corners with the quality measure less than 15 are rejected.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param minDistance: Minimum possible Euclidean distance between the returned corners.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param mask: Optional region of interest. If the image is not empty (it needs to have the type ``CV_8UC1`` and the same size as ``image`` ), it specifies the region in which the corners are detected.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param blockSize: Size of an average block for computing a derivative covariation matrix over each pixel neighborhood. See :func:`cornerEigenValsAndVecs` .
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
:param useHarrisDetector: Parameter indicating whether to use an operator or :func:`cornerMinEigenVal` .??
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
:param k: Free parameter of the Harris detector.
2011-02-23 04:43:26 +08:00
2011-03-01 05:26:43 +08:00
The function finds the most prominent corners in the image or in the specified image region, as described in Shi94:
2011-02-23 04:43:26 +08:00
#.
2011-04-30 21:53:08 +08:00
Function calculates the corner quality measure at every source image pixel using the
2011-02-26 19:05:10 +08:00
:func: `cornerMinEigenVal` or
2011-04-30 21:53:08 +08:00
:func: `cornerHarris` .
2011-03-01 05:26:43 +08:00
2011-02-23 04:43:26 +08:00
#.
2011-04-30 21:53:08 +08:00
Function performs a non-maximum?? suppression (the local maximums in
2011-02-26 19:05:10 +08:00
:math: `3\times 3` neighborhood
2011-02-23 04:43:26 +08:00
are retained).
#.
2011-04-30 21:53:08 +08:00
The corners with the minimal eigenvalue less than
:math: `\texttt{qualityLevel} \cdot \max_{x,y} qualityMeasureMap(x,y)` are rejected.
2011-02-23 04:43:26 +08:00
#.
2011-04-30 21:53:08 +08:00
The remaining corners are sorted by the quality measure in the descending order.
2011-02-23 04:43:26 +08:00
#.
2011-04-30 21:53:08 +08:00
Function throws away each corner
2011-02-26 19:05:10 +08:00
:math: `pt_j` if there is a stronger corner
:math: `pt_i` (
2011-04-30 21:53:08 +08:00
:math: `i < j` ) so that the distance between them is less than `` minDistance `` .
2011-03-01 05:26:43 +08:00
2011-02-23 04:43:26 +08:00
The function can be used to initialize a point-based tracker of an object.
2011-04-30 21:53:08 +08:00
**Note** : If the function is called with different values `` A `` and `` B `` of the parameter `` qualityLevel `` , and `` A `` > {B}, the vector of returned corners with `` qualityLevel=A `` will be the prefix of the output vector with `` qualityLevel=B `` .
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
See Also: :func: `cornerMinEigenVal` ,
:func: `cornerHarris` ,
:func: `calcOpticalFlowPyrLK` ,
:func: `estimateRigidMotion` ,
:func: `PlanarObjectDetector` ,
:func: `OneWayDescriptor`
2011-03-01 05:26:43 +08:00
2011-02-23 04:43:26 +08:00
.. index :: HoughCircles
2011-03-09 06:22:24 +08:00
.. _HoughCircles:
2011-03-01 05:26:43 +08:00
HoughCircles
2011-03-09 06:22:24 +08:00
------------
2011-03-01 05:26:43 +08:00
.. c:function :: void HoughCircles( Mat& image, vector<Vec3f>& circles, int method, double dp, double minDist, double param1=100, double param2=100, int minRadius=0, int maxRadius=0 )
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
Finds circles in a grayscale image using the Hough transform.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param image: 8-bit, single-channel, grayscale input image.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param circles: Output vector of found circles. Each vector is encoded as a 3-element floating-point vector :math:`(x, y, radius)` .
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
:param method: desc required?? Currently, the only implemented method is ``CV_HOUGH_GRADIENT`` , which is basically *21HT* , described in Yuen90 .
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
: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 half as big width and height.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param minDist: Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param param1: The first method-specific parameter. In case of ``CV_HOUGH_GRADIENT`` , it is the higher threshold of the two passed to the :func:`Canny` edge detector (the lower one is twice smaller).
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param param2: The second method-specific parameter. In case of ``CV_HOUGH_GRADIENT`` , it is the accumulator threshold at the center of ?? detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param minRadius: Minimum circle radius.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param maxRadius: Maximum circle radius.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
The function finds circles in a grayscale image using a modification of the Hough transform. Here is a short usage example: ::
2011-02-23 04:43:26 +08:00
#include <cv.h>
#include <highgui.h>
#include <math.h>
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
using namespace cv;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
int main(int argc, char** argv)
{
Mat img, gray;
if( argc != 2 && !(img=imread(argv[1], 1)).data)
return -1;
cvtColor(img, gray, CV_BGR2GRAY);
// smooth it, otherwise a lot of false circles may be detected
GaussianBlur( gray, gray, Size(9, 9), 2, 2 );
vector<Vec3f> circles;
HoughCircles(gray, circles, CV_HOUGH_GRADIENT,
2, gray->rows/4, 200, 100 );
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// draw the circle center
circle( img, center, 3, Scalar(0,255,0), -1, 8, 0 );
// draw the circle outline
circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
}
namedWindow( "circles", 1 );
imshow( "circles", img );
return 0;
}
2011-04-30 21:53:08 +08:00
**Note** : Usually the function detects the centers of circles well. However, it may fail to find correct radii. You can assist to the function by specifying the radius range ( `` minRadius `` and `` maxRadius `` ) if you know it. Or, you may ignore the returned radius, use only the center, and find the correct radius using an additional procedure.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
See Also:
:func: `fitEllipse` ,
:func: `minEnclosingCircle`
2011-03-09 06:22:24 +08:00
2011-02-23 04:43:26 +08:00
.. index :: HoughLines
2011-03-09 06:22:24 +08:00
.. _HoughLines:
2011-03-01 05:26:43 +08:00
HoughLines
2011-03-09 06:22:24 +08:00
----------
.. c:function :: void HoughLines( Mat& image, vector<Vec2f>& lines, double rho, double theta, int threshold, double srn=0, double stn=0 )
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
Finds lines in a binary image using the standard Hough transform.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param image: 8-bit, single-channel binary source image. The image may be modified by the function.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param lines: Output vector of lines. Each line is represented by a two-element vector :math:`(\rho, \theta)` . :math:`\rho` is the distance from the coordinate origin :math:`(0,0)` (top-left corner of the image). :math:`\theta` is the line rotation angle in radians ( :math:`0 \sim \textrm{vertical line}, \pi/2 \sim \textrm{horizontal line}` ).
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param rho: Distance resolution of the accumulator in pixels.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param theta: Angle resolution of the accumulator in radians.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param threshold: Accumulator threshold parameter. Only those lines are returned that get enough votes ( :math:`>\texttt{threshold}` ).
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param srn: For the multi-scale Hough transform, it is a divisor for the distance resolution ``rho`` . The coarse accumulator distance resolution is ``rho`` and the accurate accumulator resolution is ``rho/srn`` . If both ``srn=0`` and ``stn=0`` , the classical Hough transform is used. Otherwise, both these parameters should be positive.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param stn: For the multi-scale Hough transform, it is a divisor for the distance resolution ``theta`` .
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
The function implements the standard or standard multi-scale Hough transform algorithm for line detection. See
2011-02-26 19:05:10 +08:00
:func: `HoughLinesP` for the code example.
2011-02-23 04:43:26 +08:00
.. index :: HoughLinesP
2011-03-09 06:22:24 +08:00
.. _HoughLinesP:
2011-03-01 05:26:43 +08:00
HoughLinesP
2011-03-09 06:22:24 +08:00
-----------
.. c:function :: void HoughLinesP( Mat& image, vector<Vec4i>& lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0 )
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
Finds line segments in a binary image using the probabilistic Hough transform.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param image: 8-bit, single-channel binary source image. The image may be modified by the function.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param lines: Output vector of lines. Each line is represented by a 4-element vector :math:`(x_1, y_1, x_2, y_2)` , where :math:`(x_1,y_1)` and :math:`(x_2, y_2)` are the ending points of each detected line segment.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param rho: Distance resolution of the accumulator in pixels.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param theta: Angle resolution of the accumulator in radians.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param threshold: Accumulator threshold parameter. Only those lines are returned that get enough votes ( :math:`>\texttt{threshold}` ).
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param minLineLength: Minimum line length. Line segments shorter than that are rejected.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param maxLineGap: Maximum allowed gap between points on the same line to link them.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
The function implements the probabilistic Hough transform algorithm for line detection, described in
2011-02-26 19:05:10 +08:00
Matas00
2011-04-30 21:53:08 +08:00
. See the line detection example below: ::
2011-02-23 04:43:26 +08:00
/* This is a standalone program. Pass an image name as a first parameter
of the program. Switch between standard and probabilistic Hough transform
by changing "#if 1" to "#if 0" and back */
#include <cv.h>
#include <highgui.h>
#include <math.h>
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
using namespace cv;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
int main(int argc, char** argv)
{
Mat src, dst, color_dst;
if( argc != 2 || !(src=imread(argv[1], 0)).data)
return -1;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
Canny( src, dst, 50, 200, 3 );
2011-02-26 19:05:10 +08:00
cvtColor( dst, color_dst, CV_GRAY2BGR );
2011-02-23 04:43:26 +08:00
#if 0
vector<Vec2f> lines;
HoughLines( dst, lines, 1, CV_PI/180, 100 );
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0];
float theta = lines[i][1];
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b* rho;
Point pt1(cvRound(x0 + 1000*(-b)),
cvRound(y0 + 1000*(a)));
Point pt2(cvRound(x0 - 1000*(-b)),
cvRound(y0 - 1000*(a)));
line( color_dst, pt1, pt2, Scalar(0,0,255), 3, 8 );
}
#else
vector<Vec4i> lines;
HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
line( color_dst, Point(lines[i][0], lines[i][1]),
Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
}
#endif
namedWindow( "Source", 1 );
imshow( "Source", src );
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
namedWindow( "Detected Lines", 1 );
imshow( "Detected Lines", color_dst );
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
waitKey(0);
return 0;
}
2011-04-30 21:53:08 +08:00
This is a sample picture the function parameters have been tuned for:
2011-02-23 04:43:26 +08:00
2011-03-06 05:26:13 +08:00
.. image :: pics/building.jpg
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
And this is the output of the above program in case of the probabilistic Hough transform:
2011-02-23 04:43:26 +08:00
2011-03-06 05:26:13 +08:00
.. image :: pics/houghp.png
2011-02-23 04:43:26 +08:00
.. index :: preCornerDetect
2011-03-09 06:22:24 +08:00
.. _preCornerDetect:
2011-03-01 05:26:43 +08:00
preCornerDetect
2011-03-09 06:22:24 +08:00
---------------
.. c:function :: void preCornerDetect( const Mat& src, Mat& dst, int apertureSize, int borderType=BORDER_DEFAULT )
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
Calculates a feature map for corner detection.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param src: Source single-channel 8-bit of floating-point image.
2011-02-23 04:43:26 +08:00
2011-04-30 21:53:08 +08:00
:param dst: Output image that has the type ``CV_32F`` and the same size as ``src`` .
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
:param apertureSize: Aperture size of the :func:`Sobel` .
2011-03-01 05:26:43 +08:00
2011-04-30 21:53:08 +08:00
:param borderType: Pixel extrapolation method. See :func:`borderInterpolate` .
2011-03-01 05:26:43 +08:00
2011-02-23 04:43:26 +08:00
The function calculates the complex spatial derivative-based function of the source image
.. math ::
2011-02-26 19:05:10 +08:00
\texttt{dst} = (D_x \texttt{src} )^2 \cdot D_{yy} \texttt{src} + (D_y \texttt{src} )^2 \cdot D_{xx} \texttt{src} - 2 D_x \texttt{src} \cdot D_y \texttt{src} \cdot D_{xy} \texttt{src}
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
where
:math: `D_x` ,:math: `D_y` are the first image derivatives,
2011-04-30 21:53:08 +08:00
:math: `D_{xx}` ,:math: `D_{yy}` are the second image derivatives, and
2011-02-26 19:05:10 +08:00
:math: `D_{xy}` is the mixed derivative.
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
The corners can be found as local maximums of the functions, as shown below: ::
2011-02-23 04:43:26 +08:00
Mat corners, dilated_corners;
preCornerDetect(image, corners, 3);
// dilation with 3x3 rectangular structuring element
dilate(corners, dilated_corners, Mat(), 1);
Mat corner_mask = corners == dilated_corners;
2011-03-01 05:26:43 +08:00
2011-02-23 04:43:26 +08:00