Skip to main content
This module provides functions for detecting various features in images, including edges, corners, lines, and circles.

Edge Detection

Canny

Finds edges in an image using the Canny algorithm.
void Canny(InputArray image, OutputArray edges,
           double threshold1, double threshold2,
           int apertureSize = 3, bool L2gradient = false);

void Canny(InputArray dx, InputArray dy,
           OutputArray edges,
           double threshold1, double threshold2,
           bool L2gradient = false);
image
InputArray
8-bit input image.
dx
InputArray
16-bit x derivative of input image (CV_16SC1 or CV_16SC3).
dy
InputArray
16-bit y derivative of input image (same type as dx).
edges
OutputArray
Output edge map; single channels 8-bit image, which has the same size as image.
threshold1
double
First threshold for the hysteresis procedure.
threshold2
double
Second threshold for the hysteresis procedure.
apertureSize
int
default:"3"
Aperture size for the Sobel operator.
L2gradient
bool
default:"false"
A flag, indicating whether a more accurate L2 norm should be used to calculate the image gradient magnitude (L2gradient=true), or whether the default L1 norm is enough (L2gradient=false).
The function finds edges in the input 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.
Mat src = imread("image.jpg", IMREAD_GRAYSCALE);
Mat edges;
Canny(src, edges, 50, 150);

Derivatives and Gradients

Sobel

Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.
void Sobel(InputArray src, OutputArray dst, int ddepth,
           int dx, int dy, int ksize = 3,
           double scale = 1, double delta = 0,
           int borderType = BORDER_DEFAULT);
src
InputArray
Input image.
dst
OutputArray
Output image of the same size and the same number of channels as src.
ddepth
int
Output image depth. In the case of 8-bit input images it will result in truncated derivatives.
dx
int
Order of the derivative x.
dy
int
Order of the derivative y.
ksize
int
default:"3"
Size of the extended Sobel kernel; it must be 1, 3, 5, or 7.
scale
double
default:"1"
Optional scale factor for the computed derivative values; by default, no scaling is applied.
delta
double
default:"0"
Optional delta value that is added to the results prior to storing them in dst.
borderType
int
default:"BORDER_DEFAULT"
Pixel extrapolation method. BORDER_WRAP is not supported.
The Sobel operators combine Gaussian smoothing and differentiation. Most often, the function is called with (xorder = 1, yorder = 0, ksize = 3) or (xorder = 0, yorder = 1, ksize = 3) to calculate the first x- or y- image derivative.

Scharr

Calculates the first x- or y- image derivative using Scharr operator.
void Scharr(InputArray src, OutputArray dst, int ddepth,
            int dx, int dy, double scale = 1, double delta = 0,
            int borderType = BORDER_DEFAULT);
src
InputArray
Input image.
dst
OutputArray
Output image of the same size and the same number of channels as src.
ddepth
int
Output image depth.
dx
int
Order of the derivative x.
dy
int
Order of the derivative y.
scale
double
default:"1"
Optional scale factor for the computed derivative values.
delta
double
default:"0"
Optional delta value that is added to the results prior to storing them in dst.
borderType
int
default:"BORDER_DEFAULT"
Pixel extrapolation method. BORDER_WRAP is not supported.
The Scharr operator may give more accurate results than the 3×3 Sobel. The Scharr aperture is: [30310010303]\begin{bmatrix} -3 & 0 & 3 \\ -10 & 0 & 10 \\ -3 & 0 & 3 \end{bmatrix} for the x-derivative, or transposed for the y-derivative.

Laplacian

Calculates the Laplacian of an image.
void Laplacian(InputArray src, OutputArray dst, int ddepth,
               int ksize = 1, double scale = 1, double delta = 0,
               int borderType = BORDER_DEFAULT);
src
InputArray
Source image.
dst
OutputArray
Destination image of the same size and the same number of channels as src.
ddepth
int
Desired depth of the destination image.
ksize
int
default:"1"
Aperture size used to compute the second-derivative filters. The size must be positive and odd.
scale
double
default:"1"
Optional scale factor for the computed Laplacian values.
delta
double
default:"0"
Optional delta value that is added to the results prior to storing them in dst.
borderType
int
default:"BORDER_DEFAULT"
Pixel extrapolation method. BORDER_WRAP is not supported.
The function calculates the Laplacian of the source image by adding up the second x and y derivatives calculated using the Sobel operator: Δsrc=2srcx2+2srcy2\Delta \text{src} = \frac{\partial^2 \text{src}}{\partial x^2} + \frac{\partial^2 \text{src}}{\partial y^2}

Corner Detection

cornerHarris

Harris corner detector.
void cornerHarris(InputArray src, OutputArray dst, int blockSize,
                  int ksize, double k,
                  int borderType = BORDER_DEFAULT);
src
InputArray
Input single-channel 8-bit or floating-point image.
dst
OutputArray
Image to store the Harris detector responses. It has the type CV_32FC1 and the same size as src.
blockSize
int
Neighborhood size.
ksize
int
Aperture parameter for the Sobel operator.
k
double
Harris detector free parameter.
borderType
int
default:"BORDER_DEFAULT"
Pixel extrapolation method. BORDER_WRAP is not supported.
The function runs the Harris corner detector on the image. Corners in the image can be found as the local maxima of this response map.

cornerMinEigenVal

Calculates the minimal eigenvalue of gradient matrices for corner detection.
void cornerMinEigenVal(InputArray src, OutputArray dst,
                       int blockSize, int ksize = 3,
                       int borderType = BORDER_DEFAULT);
src
InputArray
Input single-channel 8-bit or floating-point image.
dst
OutputArray
Image to store the minimal eigenvalues. It has the type CV_32FC1 and the same size as src.
blockSize
int
Neighborhood size.
ksize
int
default:"3"
Aperture parameter for the Sobel operator.
borderType
int
default:"BORDER_DEFAULT"
Pixel extrapolation method. BORDER_WRAP is not supported.
The function is similar to cornerEigenValsAndVecs but it calculates and stores only the minimal eigenvalue of the covariance matrix of derivatives.

goodFeaturesToTrack

Determines strong corners on an image.
void goodFeaturesToTrack(InputArray image, OutputArray corners,
                         int maxCorners, double qualityLevel, double minDistance,
                         InputArray mask = noArray(), int blockSize = 3,
                         bool useHarrisDetector = false, double k = 0.04);
image
InputArray
Input 8-bit or floating-point 32-bit, single-channel image.
corners
OutputArray
Output vector of detected corners.
maxCorners
int
Maximum number of corners to return. If there are more corners than are found, the strongest of them is returned. maxCorners <= 0 implies that no limit on the maximum is set.
qualityLevel
double
Parameter characterizing the minimal accepted quality of image corners. The parameter value is multiplied by the best corner quality measure.
minDistance
double
Minimum possible Euclidean distance between the returned corners.
mask
InputArray
default:"noArray()"
Optional region of interest.
blockSize
int
default:"3"
Size of an average block for computing a derivative covariation matrix over each pixel neighborhood.
useHarrisDetector
bool
default:"false"
Parameter indicating whether to use a Harris detector or cornerMinEigenVal.
k
double
default:"0.04"
Free parameter of the Harris detector.
The function finds the most prominent corners in the image or in the specified image region.

Hough Transform

HoughLines

Finds lines in a binary image using the standard Hough transform.
void HoughLines(InputArray image, OutputArray lines,
                double rho, double theta, int threshold,
                double srn = 0, double stn = 0,
                double min_theta = 0, double max_theta = CV_PI,
                bool use_edgeval = false);
image
InputArray
8-bit, single-channel binary source image. The image may be modified by the function.
lines
OutputArray
Output vector of lines. Each line is represented by a 2 or 3 element vector (ρ, θ) or (ρ, θ, votes).
rho
double
Distance resolution of the accumulator in pixels.
theta
double
Angle resolution of the accumulator in radians.
threshold
int
Accumulator threshold parameter. Only those lines are returned that get enough votes (>threshold).
srn
double
default:"0"
For the multi-scale Hough transform, it is a divisor for the distance resolution rho.
stn
double
default:"0"
For the multi-scale Hough transform, it is a divisor for the distance resolution theta.
min_theta
double
default:"0"
Minimum angle to check for lines. Must fall between 0 and max_theta.
max_theta
double
default:"CV_PI"
Upper bound for the angle. Must fall between min_theta and CV_PI.
The function implements the standard or standard multi-scale Hough transform algorithm for line detection.

HoughLinesP

Finds line segments in a binary image using the probabilistic Hough transform.
void HoughLinesP(InputArray image, OutputArray lines,
                 double rho, double theta, int threshold,
                 double minLineLength = 0, double maxLineGap = 0);
image
InputArray
8-bit, single-channel binary source image. The image may be modified by the function.
lines
OutputArray
Output vector of lines. Each line is represented by a 4-element vector (x₁, y₁, x₂, y₂), where (x₁,y₁) and (x₂, y₂) are the ending points of each detected line segment.
rho
double
Distance resolution of the accumulator in pixels.
theta
double
Angle resolution of the accumulator in radians.
threshold
int
Accumulator threshold parameter. Only those lines are returned that get enough votes (>threshold).
minLineLength
double
default:"0"
Minimum line length. Line segments shorter than that are rejected.
maxLineGap
double
default:"0"
Maximum allowed gap between points on the same line to link them.
The function implements the probabilistic Hough transform algorithm for line detection.

HoughCircles

Finds circles in a grayscale image using the Hough transform.
void HoughCircles(InputArray image, OutputArray circles,
                  int method, double dp, double minDist,
                  double param1 = 100, double param2 = 100,
                  int minRadius = 0, int maxRadius = 0);
image
InputArray
8-bit, single-channel, grayscale input image.
circles
OutputArray
Output vector of found circles. Each vector is encoded as 3 or 4 element floating-point vector (x, y, radius) or (x, y, radius, votes).
method
int
Detection method. The available methods are HOUGH_GRADIENT and HOUGH_GRADIENT_ALT.
dp
double
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.
minDist
double
Minimum distance between the centers of the detected circles.
param1
double
default:"100"
First method-specific parameter. In case of HOUGH_GRADIENT and HOUGH_GRADIENT_ALT, it is the higher threshold of the two passed to the Canny edge detector.
param2
double
default:"100"
Second method-specific parameter. In case of HOUGH_GRADIENT, it is the accumulator threshold for the circle centers at the detection stage.
minRadius
int
default:"0"
Minimum circle radius.
maxRadius
int
default:"0"
Maximum circle radius. If <= 0, uses the maximum image dimension.
The function finds circles in a grayscale image using a modification of the Hough transform.
Mat src = imread("image.jpg", IMREAD_GRAYSCALE);
Mat blurred;
GaussianBlur(src, blurred, Size(9, 9), 2, 2);

vector<Vec3f> circles;
HoughCircles(blurred, circles, HOUGH_GRADIENT, 1, src.rows/8, 200, 100);
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.

Enumerations

HoughModes

Variants of Hough transform:
  • HOUGH_STANDARD - Classical or standard Hough transform
  • HOUGH_PROBABILISTIC - Probabilistic Hough transform (more efficient)
  • HOUGH_MULTI_SCALE - Multi-scale variant of the classical Hough transform
  • HOUGH_GRADIENT - 21HT for circles
  • HOUGH_GRADIENT_ALT - Variation of HOUGH_GRADIENT to get better accuracy