- Thickness of lines the contours are drawn with. If it is
negative (for example, thickness=CV_FILLED
), the contour
interiors are drawn.lineType
- Line connectivity. See "line" for details.hierarchy
- Optional information about hierarchy. It is only needed if
you want to draw only some of the contours (see maxLevel
).maxLevel
- Maximal level for drawn contours. If it is 0, only the
specified contour is drawn. If it is 1, the function draws the contour(s) and
all the nested contours. If it is 2, the function draws the contours, all the
nested contours, all the nested-to-nested contours, and so on. This parameter
is only taken into account when there is hierarchy
available.offset
- Optional contour shift parameter. Shift all the drawn contours
by the specified offset=(dx,dy).public static void equalizeHist(Mat src, Mat dst)
Equalizes the histogram of a grayscale image.
The function equalizes the histogram of the input image using the following algorithm:
src
.
H'_i = sum(by: 0 <= j < i) H(j)
Transform the image using H' as a look-up table: dst(x,y) = H'(src(x,y))
The algorithm normalizes the brightness and increases the contrast of the image.
src
- Source 8-bit single channel image.dst
- Destination image of the same size and type as src
.public static void erode(Mat src, Mat dst, Mat kernel)
Erodes an image by using a specific structuring element.
The function erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken:
dst(x,y) = min _((x',y'): element(x',y') != 0) src(x+x',y+y')
The function supports the in-place mode. Erosion can be applied several
(iterations
) times. In case of multi-channel images, each
channel is processed independently.
Note:
src
- input image; the number of channels can be arbitrary, but the
depth should be one of CV_8U
, CV_16U
,
CV_16S
, CV_32F" or
CV_64F".dst
- output image of the same size and type as src
.kernel
- a kernelmorphologyEx(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
,
dilate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
public static void erode(Mat src, Mat dst, Mat kernel, Point anchor, int iterations)
Erodes an image by using a specific structuring element.
The function erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken:
dst(x,y) = min _((x',y'): element(x',y') != 0) src(x+x',y+y')
The function supports the in-place mode. Erosion can be applied several
(iterations
) times. In case of multi-channel images, each
channel is processed independently.
Note:
src
- input image; the number of channels can be arbitrary, but the
depth should be one of CV_8U
, CV_16U
,
CV_16S
, CV_32F" or
CV_64F".dst
- output image of the same size and type as src
.kernel
- a kernelanchor
- position of the anchor within the element; default value
(-1, -1)
means that the anchor is at the element center.iterations
- number of times erosion is applied.morphologyEx(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
,
dilate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
public static void erode(Mat src, Mat dst, Mat kernel, Point anchor, int iterations, int borderType, Scalar borderValue)
Erodes an image by using a specific structuring element.
The function erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken:
dst(x,y) = min _((x',y'): element(x',y') != 0) src(x+x',y+y')
The function supports the in-place mode. Erosion can be applied several
(iterations
) times. In case of multi-channel images, each
channel is processed independently.
Note:
src
- input image; the number of channels can be arbitrary, but the
depth should be one of CV_8U
, CV_16U
,
CV_16S
, CV_32F" or
CV_64F".dst
- output image of the same size and type as src
.kernel
- a kernelanchor
- position of the anchor within the element; default value
(-1, -1)
means that the anchor is at the element center.iterations
- number of times erosion is applied.borderType
- pixel extrapolation method (see "borderInterpolate" for
details).borderValue
- border value in case of a constant border (see
"createMorphologyFilter" for details).morphologyEx(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
,
dilate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
public static void filter2D(Mat src, Mat dst, int ddepth, Mat kernel)
Convolves an image with the kernel.
The function applies an arbitrary linear filter to an image. In-place operation is supported. When the aperture is partially outside the image, the function interpolates outlier pixel values according to the specified border mode.
The function does actually compute correlation, not the convolution:
dst(x,y) = sum(by: 0 <= x' < kernel.cols, 0 <= y' < kernel.rows) kernel(x',y')* src(x+x'- anchor.x,y+y'- anchor.y)
That is, the kernel is not mirrored around the anchor point. If you need a
real convolution, flip the kernel using "flip" and set the new anchor to
(kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1)
.
The function uses the DFT-based algorithm in case of sufficiently large
kernels (~11 x 11
or larger) and the direct algorithm (that uses
the engine retrieved by "createLinearFilter") for small kernels.
src
- input image.dst
- output image of the same size and the same number of channels as
src
.ddepth
- desired depth of the destination image; if it is negative, it
will be the same as src.depth()
; the following combinations of
src.depth()
and ddepth
are supported:
src.depth()
= CV_8U
, ddepth
=
-1/CV_16S
/CV_32F
/CV_64F
src.depth()
= CV_16U
/CV_16S
,
ddepth
= -1/CV_32F
/CV_64F
src.depth()
= CV_32F
, ddepth
=
-1/CV_32F
/CV_64F
src.depth()
= CV_64F
, ddepth
=
-1/CV_64F
when ddepth=-1
, the output image will have the same depth as the
source.
kernel
- convolution kernel (or rather a correlation kernel), a
single-channel floating point matrix; if you want to apply different kernels
to different channels, split the image into separate color planes using
"split" and process them individually.matchTemplate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, int)
,
Core.dft(org.opencv.core.Mat, org.opencv.core.Mat, int, int)
,
sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
public static void filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)
Convolves an image with the kernel.
The function applies an arbitrary linear filter to an image. In-place operation is supported. When the aperture is partially outside the image, the function interpolates outlier pixel values according to the specified border mode.
The function does actually compute correlation, not the convolution:
dst(x,y) = sum(by: 0 <= x' < kernel.cols, 0 <= y' < kernel.rows) kernel(x',y')* src(x+x'- anchor.x,y+y'- anchor.y)
That is, the kernel is not mirrored around the anchor point. If you need a
real convolution, flip the kernel using "flip" and set the new anchor to
(kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1)
.
The function uses the DFT-based algorithm in case of sufficiently large
kernels (~11 x 11
or larger) and the direct algorithm (that uses
the engine retrieved by "createLinearFilter") for small kernels.
src
- input image.dst
- output image of the same size and the same number of channels as
src
.ddepth
- desired depth of the destination image; if it is negative, it
will be the same as src.depth()
; the following combinations of
src.depth()
and ddepth
are supported:
src.depth()
= CV_8U
, ddepth
=
-1/CV_16S
/CV_32F
/CV_64F
src.depth()
= CV_16U
/CV_16S
,
ddepth
= -1/CV_32F
/CV_64F
src.depth()
= CV_32F
, ddepth
=
-1/CV_32F
/CV_64F
src.depth()
= CV_64F
, ddepth
=
-1/CV_64F
when ddepth=-1
, the output image will have the same depth as the
source.
kernel
- convolution kernel (or rather a correlation kernel), a
single-channel floating point matrix; if you want to apply different kernels
to different channels, split the image into separate color planes using
"split" and process them individually.anchor
- anchor of the kernel that indicates the relative position of a
filtered point within the kernel; the anchor should lie within the kernel;
default value (-1,-1) means that the anchor is at the kernel center.delta
- optional value added to the filtered pixels before storing them
in dst
.matchTemplate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, int)
,
Core.dft(org.opencv.core.Mat, org.opencv.core.Mat, int, int)
,
sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
public static void filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta, int borderType)
Convolves an image with the kernel.
The function applies an arbitrary linear filter to an image. In-place operation is supported. When the aperture is partially outside the image, the function interpolates outlier pixel values according to the specified border mode.
The function does actually compute correlation, not the convolution:
dst(x,y) = sum(by: 0 <= x' < kernel.cols, 0 <= y' < kernel.rows) kernel(x',y')* src(x+x'- anchor.x,y+y'- anchor.y)
That is, the kernel is not mirrored around the anchor point. If you need a
real convolution, flip the kernel using "flip" and set the new anchor to
(kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1)
.
The function uses the DFT-based algorithm in case of sufficiently large
kernels (~11 x 11
or larger) and the direct algorithm (that uses
the engine retrieved by "createLinearFilter") for small kernels.
src
- input image.dst
- output image of the same size and the same number of channels as
src
.ddepth
- desired depth of the destination image; if it is negative, it
will be the same as src.depth()
; the following combinations of
src.depth()
and ddepth
are supported:
src.depth()
= CV_8U
, ddepth
=
-1/CV_16S
/CV_32F
/CV_64F
src.depth()
= CV_16U
/CV_16S
,
ddepth
= -1/CV_32F
/CV_64F
src.depth()
= CV_32F
, ddepth
=
-1/CV_32F
/CV_64F
src.depth()
= CV_64F
, ddepth
=
-1/CV_64F
when ddepth=-1
, the output image will have the same depth as the
source.
kernel
- convolution kernel (or rather a correlation kernel), a
single-channel floating point matrix; if you want to apply different kernels
to different channels, split the image into separate color planes using
"split" and process them individually.anchor
- anchor of the kernel that indicates the relative position of a
filtered point within the kernel; the anchor should lie within the kernel;
default value (-1,-1) means that the anchor is at the kernel center.delta
- optional value added to the filtered pixels before storing them
in dst
.borderType
- pixel extrapolation method (see "borderInterpolate" for
details).matchTemplate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, int)
,
Core.dft(org.opencv.core.Mat, org.opencv.core.Mat, int, int)
,
sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
public static void findContours(Mat image, java.util.List<MatOfPoint> contours, Mat hierarchy, int mode, int method)
Finds contours in a binary image.
The function retrieves contours from the binary image using the algorithm
[Suzuki85]. The contours are a useful tool for shape analysis and object
detection and recognition. See squares.c
in the OpenCV sample
directory.
Note: Source image
is modified by this function. Also, the
function does not take into account 1-pixel border of the image (it's filled
with 0's and used for neighbor analysis in the algorithm), therefore the
contours touching the image border will be clipped.
Note: If you use the new Python interface then the CV_
prefix
has to be omitted in contour retrieval mode and contour approximation method
parameters (for example, use cv2.RETR_LIST
and cv2.CHAIN_APPROX_NONE
parameters). If you use the old Python interface then these parameters have
the CV_
prefix (for example, use cv.CV_RETR_LIST
and cv.CV_CHAIN_APPROX_NONE
).
Note:
image
- Source, an 8-bit single-channel image. Non-zero pixels are
treated as 1's. Zero pixels remain 0's, so the image is treated as
binary
. You can use "compare", "inRange", "threshold",
"adaptiveThreshold", "Canny", and others to create a binary image out of a
grayscale or color one. The function modifies the image
while
extracting the contours. If mode equals to CV_RETR_CCOMP
or
CV_RETR_FLOODFILL
, the input can also be a 32-bit integer image
of labels (CV_32SC1
).contours
- Detected contours. Each contour is stored as a vector of
points.hierarchy
- Optional output vector, containing information about the
image topology. It has as many elements as the number of contours. For each
i-th contour contours[i]
, the elements hierarchy[i][0]
,
hiearchy[i][1]
, hiearchy[i][2]
, and
hiearchy[i][3]
are set to 0-based indices in contours
of the next and previous contours at the same hierarchical level, the first
child contour and the parent contour, respectively. If for the contour
i
there are no next, previous, parent, or nested contours, the
corresponding elements of hierarchy[i]
will be negative.mode
- Contour retrieval mode (if you use Python see also a note below).
hierarchy[i][2]=hierarchy[i][3]=-1
for all the contours.
contours.c
demo.
method
- Contour approximation method (if you use Python see also a note
below).
(x1,y1)
and (x2,y2)
of
the contour will be either horizontal, vertical or diagonal neighbors, that
is, max(abs(x1-x2),abs(y2-y1))==1
.
public static void findContours(Mat image, java.util.List<MatOfPoint> contours, Mat hierarchy, int mode, int method, Point offset)
Finds contours in a binary image.
The function retrieves contours from the binary image using the algorithm
[Suzuki85]. The contours are a useful tool for shape analysis and object
detection and recognition. See squares.c
in the OpenCV sample
directory.
Note: Source image
is modified by this function. Also, the
function does not take into account 1-pixel border of the image (it's filled
with 0's and used for neighbor analysis in the algorithm), therefore the
contours touching the image border will be clipped.
Note: If you use the new Python interface then the CV_
prefix
has to be omitted in contour retrieval mode and contour approximation method
parameters (for example, use cv2.RETR_LIST
and cv2.CHAIN_APPROX_NONE
parameters). If you use the old Python interface then these parameters have
the CV_
prefix (for example, use cv.CV_RETR_LIST
and cv.CV_CHAIN_APPROX_NONE
).
Note:
image
- Source, an 8-bit single-channel image. Non-zero pixels are
treated as 1's. Zero pixels remain 0's, so the image is treated as
binary
. You can use "compare", "inRange", "threshold",
"adaptiveThreshold", "Canny", and others to create a binary image out of a
grayscale or color one. The function modifies the image
while
extracting the contours. If mode equals to CV_RETR_CCOMP
or
CV_RETR_FLOODFILL
, the input can also be a 32-bit integer image
of labels (CV_32SC1
).contours
- Detected contours. Each contour is stored as a vector of
points.hierarchy
- Optional output vector, containing information about the
image topology. It has as many elements as the number of contours. For each
i-th contour contours[i]
, the elements hierarchy[i][0]
,
hiearchy[i][1]
, hiearchy[i][2]
, and
hiearchy[i][3]
are set to 0-based indices in contours
of the next and previous contours at the same hierarchical level, the first
child contour and the parent contour, respectively. If for the contour
i
there are no next, previous, parent, or nested contours, the
corresponding elements of hierarchy[i]
will be negative.mode
- Contour retrieval mode (if you use Python see also a note below).
hierarchy[i][2]=hierarchy[i][3]=-1
for all the contours.
contours.c
demo.
method
- Contour approximation method (if you use Python see also a note
below).
(x1,y1)
and (x2,y2)
of
the contour will be either horizontal, vertical or diagonal neighbors, that
is, max(abs(x1-x2),abs(y2-y1))==1
.
offset
- Optional offset by which every contour point is shifted. This
is useful if the contours are extracted from the image ROI and then they
should be analyzed in the whole image context.public static RotatedRect fitEllipse(MatOfPoint2f points)
Fits an ellipse around a set of 2D points.
The function calculates the ellipse that fits (in a least-squares sense) a set of 2D points best of all. It returns the rotated rectangle in which the ellipse is inscribed. The algorithm [Fitzgibbon95] is used. Developer should keep in mind that it is possible that the returned ellipse/rotatedRect data contains negative indices, due to the data points being close to the border of the containing Mat element.
Note:
points
- Input 2D point set, stored in:
std.vector<>
or Mat
(C++ interface)
CvSeq*
or CvMat*
(C interface)
public static void fitLine(Mat points, Mat line, int distType, double param, double reps, double aeps)
Fits a line to a 2D or 3D point set.
The function fitLine
fits a line to a 2D or 3D point set by
minimizing sum_i rho(r_i) where r_i is a distance between
the i^(th) point, the line and rho(r) is a distance
function, one of the following:
rho(r) = r^2/2(the simplest and the fastest least-squares method)
rho(r) = r
rho(r) = 2 * (sqrt(1 + frac(r^2)2) - 1)
rho(r) = C^2 * ((r)/(C) - log((1 + (r)/(C)))) where C=1.3998
rho(r) = (C^2)/2 * (1 - exp((-((r)/(C))^2))) where C=2.9846
rho(r) = r^2/2 if r < C; C * (r-C/2) otherwise where C=1.345
The algorithm is based on the M-estimator (http://en.wikipedia.org/wiki/M-estimator) technique that iteratively fits the line using the weighted least-squares algorithm. After each iteration the weights w_i are adjusted to be inversely proportional to rho(r_i)... Sample code:
points
- Input vector of 2D or 3D points, stored in std.vector<>
or Mat
.line
- Output line parameters. In case of 2D fitting, it should be a
vector of 4 elements (like Vec4f
) - (vx, vy, x0,
y0)
, where (vx, vy)
is a normalized vector collinear to
the line and (x0, y0)
is a point on the line. In case of 3D
fitting, it should be a vector of 6 elements (like Vec6f
) -
(vx, vy, vz, x0, y0, z0)
, where (vx, vy, vz)
is a
normalized vector collinear to the line and (x0, y0, z0)
is a
point on the line.distType
- Distance used by the M-estimator (see the discussion below).param
- Numerical parameter (C
) for some types of
distances. If it is 0, an optimal value is chosen.reps
- Sufficient accuracy for the radius (distance between the
coordinate origin and the line).aeps
- Sufficient accuracy for the angle. 0.01 would be a good default
value for reps
and aeps
.public static int floodFill(Mat image, Mat mask, Point seedPoint, Scalar newVal)
Fills a connected component with the given color.
The functions floodFill
fill a connected component starting from
the seed point with the specified color. The connectivity is determined by
the color/brightness closeness of the neighbor pixels. The pixel at
(x,y) is considered to belong to the repainted domain if:
in case of a grayscale image and floating range
in case of a grayscale image and fixed range
src(x',y')_g- loDiff _g <= src(x,y)_g <= src(x',y')_g+ upDiff _g
and
src(x',y')_b- loDiff _b <= src(x,y)_b <= src(x',y')_b+ upDiff _b
in case of a color image and floating range
src(seedPoint.x, seedPoint.y)_g- loDiff _g <= src(x,y)_g <= src(seedPoint.x, seedPoint.y)_g+ upDiff _g
and
src(seedPoint.x, seedPoint.y)_b- loDiff _b <= src(x,y)_b <= src(seedPoint.x, seedPoint.y)_b+ upDiff _b
in case of a color image and fixed range
where src(x',y') is the value of one of pixel neighbors that is already known to belong to the component. That is, to be added to the connected component, a color/brightness of the pixel should be close enough to:
Use these functions to either mark a connected component with the specified color in-place, or build a mask and then extract the contour, or copy the region to another image, and so on.
Note:
image
- Input/output 1- or 3-channel, 8-bit, or floating-point image. It
is modified by the function unless the FLOODFILL_MASK_ONLY
flag
is set in the second variant of the function. See the details below.mask
- Operation mask that should be a single-channel 8-bit image, 2
pixels wider and 2 pixels taller than image
. Since this is both
an input and output parameter, you must take responsibility of initializing
it. Flood-filling cannot go across non-zero pixels in the input mask. For
example, an edge detector output can be used as a mask to stop filling at
edges. On output, pixels in the mask corresponding to filled pixels in the
image are set to 1 or to the a value specified in flags
as
described below. It is therefore possible to use the same mask in multiple
calls to the function to make sure the filled areas do not overlap.
Note: Since the mask is larger than the filled image, a pixel (x, y)
in image
corresponds to the pixel (x+1, y+1) in the
mask
.
seedPoint
- Starting point.newVal
- New value of the repainted domain pixels.findContours(org.opencv.core.Mat, java.util.List<org.opencv.core.MatOfPoint>, org.opencv.core.Mat, int, int, org.opencv.core.Point)
public static int floodFill(Mat image, Mat mask, Point seedPoint, Scalar newVal, Rect rect, Scalar loDiff, Scalar upDiff, int flags)
Fills a connected component with the given color.
The functions floodFill
fill a connected component starting from
the seed point with the specified color. The connectivity is determined by
the color/brightness closeness of the neighbor pixels. The pixel at
(x,y) is considered to belong to the repainted domain if:
in case of a grayscale image and floating range
in case of a grayscale image and fixed range
src(x',y')_g- loDiff _g <= src(x,y)_g <= src(x',y')_g+ upDiff _g
and
src(x',y')_b- loDiff _b <= src(x,y)_b <= src(x',y')_b+ upDiff _b
in case of a color image and floating range
src(seedPoint.x, seedPoint.y)_g- loDiff _g <= src(x,y)_g <= src(seedPoint.x, seedPoint.y)_g+ upDiff _g
and
src(seedPoint.x, seedPoint.y)_b- loDiff _b <= src(x,y)_b <= src(seedPoint.x, seedPoint.y)_b+ upDiff _b
in case of a color image and fixed range
where src(x',y') is the value of one of pixel neighbors that is already known to belong to the component. That is, to be added to the connected component, a color/brightness of the pixel should be close enough to:
Use these functions to either mark a connected component with the specified color in-place, or build a mask and then extract the contour, or copy the region to another image, and so on.
Note:
image
- Input/output 1- or 3-channel, 8-bit, or floating-point image. It
is modified by the function unless the FLOODFILL_MASK_ONLY
flag
is set in the second variant of the function. See the details below.mask
- Operation mask that should be a single-channel 8-bit image, 2
pixels wider and 2 pixels taller than image
. Since this is both
an input and output parameter, you must take responsibility of initializing
it. Flood-filling cannot go across non-zero pixels in the input mask. For
example, an edge detector output can be used as a mask to stop filling at
edges. On output, pixels in the mask corresponding to filled pixels in the
image are set to 1 or to the a value specified in flags
as
described below. It is therefore possible to use the same mask in multiple
calls to the function to make sure the filled areas do not overlap.
Note: Since the mask is larger than the filled image, a pixel (x, y)
in image
corresponds to the pixel (x+1, y+1) in the
mask
.
seedPoint
- Starting point.newVal
- New value of the repainted domain pixels.rect
- Optional output parameter set by the function to the minimum
bounding rectangle of the repainted domain.loDiff
- Maximal lower brightness/color difference between the currently
observed pixel and one of its neighbors belonging to the component, or a seed
pixel being added to the component.upDiff
- Maximal upper brightness/color difference between the currently
observed pixel and one of its neighbors belonging to the component, or a seed
pixel being added to the component.flags
- Operation flags. The first 8 bits contain a connectivity value.
The default value of 4 means that only the four nearest neighbor pixels
(those that share an edge) are considered. A connectivity value of 8 means
that the eight nearest neighbor pixels (those that share a corner) will be
considered. The next 8 bits (8-16) contain a value between 1 and 255 with
which to fill the mask
(the default value is 1). For example,
4 | (255 << 8)
will consider 4 nearest neighbours and fill the
mask with a value of 255. The following additional options occupy higher bits
and therefore may be further combined with the connectivity and mask fill
values using bit-wise or (|
):
newVal
is ignored), and only fills the mask with the value
specified in bits 8-16 of flags
as described above. This option
only make sense in function variants that have the mask
parameter.
findContours(org.opencv.core.Mat, java.util.List<org.opencv.core.MatOfPoint>, org.opencv.core.Mat, int, int, org.opencv.core.Point)
public static void GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)
Blurs an image using a Gaussian filter.
The function convolves the source image with the specified Gaussian kernel. In-place filtering is supported.
src
- input image; the image can have any number of channels, which are
processed independently, but the depth should be CV_8U
,
CV_16U
, CV_16S
, CV_32F
or
CV_64F
.dst
- output image of the same size and type as src
.ksize
- Gaussian kernel size. ksize.width
and
ksize.height
can differ but they both must be positive and odd.
Or, they can be zero's and then they are computed from sigma*
.sigmaX
- Gaussian kernel standard deviation in X direction.sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
medianBlur(org.opencv.core.Mat, org.opencv.core.Mat, int)
,
boxFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Size, org.opencv.core.Point, boolean, int)
,
blur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, org.opencv.core.Point, int)
,
filter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
bilateralFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, double, double, int)
public static void GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX, double sigmaY)
Blurs an image using a Gaussian filter.
The function convolves the source image with the specified Gaussian kernel. In-place filtering is supported.
src
- input image; the image can have any number of channels, which are
processed independently, but the depth should be CV_8U
,
CV_16U
, CV_16S
, CV_32F
or
CV_64F
.dst
- output image of the same size and type as src
.ksize
- Gaussian kernel size. ksize.width
and
ksize.height
can differ but they both must be positive and odd.
Or, they can be zero's and then they are computed from sigma*
.sigmaX
- Gaussian kernel standard deviation in X direction.sigmaY
- Gaussian kernel standard deviation in Y direction; if
sigmaY
is zero, it is set to be equal to sigmaX
, if
both sigmas are zeros, they are computed from ksize.width
and
ksize.height
, respectively (see "getGaussianKernel" for
details); to fully control the result regardless of possible future
modifications of all this semantics, it is recommended to specify all of
ksize
, sigmaX
, and sigmaY
.sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
medianBlur(org.opencv.core.Mat, org.opencv.core.Mat, int)
,
boxFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Size, org.opencv.core.Point, boolean, int)
,
blur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, org.opencv.core.Point, int)
,
filter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
bilateralFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, double, double, int)
public static void GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX, double sigmaY, int borderType)
Blurs an image using a Gaussian filter.
The function convolves the source image with the specified Gaussian kernel. In-place filtering is supported.
src
- input image; the image can have any number of channels, which are
processed independently, but the depth should be CV_8U
,
CV_16U
, CV_16S
, CV_32F
or
CV_64F
.dst
- output image of the same size and type as src
.ksize
- Gaussian kernel size. ksize.width
and
ksize.height
can differ but they both must be positive and odd.
Or, they can be zero's and then they are computed from sigma*
.sigmaX
- Gaussian kernel standard deviation in X direction.sigmaY
- Gaussian kernel standard deviation in Y direction; if
sigmaY
is zero, it is set to be equal to sigmaX
, if
both sigmas are zeros, they are computed from ksize.width
and
ksize.height
, respectively (see "getGaussianKernel" for
details); to fully control the result regardless of possible future
modifications of all this semantics, it is recommended to specify all of
ksize
, sigmaX
, and sigmaY
.borderType
- pixel extrapolation method (see "borderInterpolate" for
details).sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
medianBlur(org.opencv.core.Mat, org.opencv.core.Mat, int)
,
boxFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Size, org.opencv.core.Point, boolean, int)
,
blur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, org.opencv.core.Point, int)
,
filter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
bilateralFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, double, double, int)
public static Mat getAffineTransform(MatOfPoint2f src, MatOfPoint2f dst)
Calculates an affine transform from three pairs of the corresponding points.
The function calculates the 2 x 3 matrix of an affine transform so that:
x'_i y'_i = map_matrix * x_i y_i 1
where
dst(i)=(x'_i,y'_i),<BR>src(i)=(x_i, y_i),<BR>i=0,1,2
src
- Coordinates of triangle vertices in the source image.dst
- Coordinates of the corresponding triangle vertices in the
destination image.warpAffine(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
,
Core.transform(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat)
public static Mat getDefaultNewCameraMatrix(Mat cameraMatrix)
Returns the default new camera matrix.
The function returns the camera matrix that is either an exact copy of the
input cameraMatrix
(when centerPrinicipalPoint=false
),
or the modified one (when centerPrincipalPoint=true
).
In the latter case, the new camera matrix will be:
f_x 0(imgSize.width -1)*0.5 0 f_y(imgSize.height -1)*0.5 0 0 1,
where f_x and f_y are (0,0) and (1,1)
elements of cameraMatrix
, respectively.
By default, the undistortion functions in OpenCV (see "initUndistortRectifyMap", "undistort") do not move the principal point. However, when you work with stereo, it is important to move the principal points in both views to the same y-coordinate (which is required by most of stereo correspondence algorithms), and may be to the same x-coordinate too. So, you can form the new camera matrix for each view where the principal points are located at the center.
cameraMatrix
- Input camera matrix.public static Mat getDefaultNewCameraMatrix(Mat cameraMatrix, Size imgsize, boolean centerPrincipalPoint)
Returns the default new camera matrix.
The function returns the camera matrix that is either an exact copy of the
input cameraMatrix
(when centerPrinicipalPoint=false
),
or the modified one (when centerPrincipalPoint=true
).
In the latter case, the new camera matrix will be:
f_x 0(imgSize.width -1)*0.5 0 f_y(imgSize.height -1)*0.5 0 0 1,
where f_x and f_y are (0,0) and (1,1)
elements of cameraMatrix
, respectively.
By default, the undistortion functions in OpenCV (see "initUndistortRectifyMap", "undistort") do not move the principal point. However, when you work with stereo, it is important to move the principal points in both views to the same y-coordinate (which is required by most of stereo correspondence algorithms), and may be to the same x-coordinate too. So, you can form the new camera matrix for each view where the principal points are located at the center.
cameraMatrix
- Input camera matrix.imgsize
- Camera view image size in pixels.centerPrincipalPoint
- Location of the principal point in the new camera
matrix. The parameter indicates whether this location should be at the image
center or not.public static void getDerivKernels(Mat kx, Mat ky, int dx, int dy, int ksize)
Returns filter coefficients for computing spatial image derivatives.
The function computes and returns the filter coefficients for spatial image
derivatives. When ksize=CV_SCHARR
, the Scharr 3 x 3
kernels are generated (see "Scharr"). Otherwise, Sobel kernels are generated
(see "Sobel"). The filters are normally passed to "sepFilter2D" or to
"createSeparableLinearFilter".
kx
- Output matrix of row filter coefficients. It has the type
ktype
.ky
- Output matrix of column filter coefficients. It has the type
ktype
.dx
- Derivative order in respect of x.dy
- Derivative order in respect of y.ksize
- Aperture size. It can be CV_SCHARR
, 1, 3, 5, or 7.public static void getDerivKernels(Mat kx, Mat ky, int dx, int dy, int ksize, boolean normalize, int ktype)
Returns filter coefficients for computing spatial image derivatives.
The function computes and returns the filter coefficients for spatial image
derivatives. When ksize=CV_SCHARR
, the Scharr 3 x 3
kernels are generated (see "Scharr"). Otherwise, Sobel kernels are generated
(see "Sobel"). The filters are normally passed to "sepFilter2D" or to
"createSeparableLinearFilter".
kx
- Output matrix of row filter coefficients. It has the type
ktype
.ky
- Output matrix of column filter coefficients. It has the type
ktype
.dx
- Derivative order in respect of x.dy
- Derivative order in respect of y.ksize
- Aperture size. It can be CV_SCHARR
, 1, 3, 5, or 7.normalize
- Flag indicating whether to normalize (scale down) the filter
coefficients or not. Theoretically, the coefficients should have the
denominator =2^(ksize*2-dx-dy-2). If you are going to filter
floating-point images, you are likely to use the normalized kernels. But if
you compute derivatives of an 8-bit image, store the results in a 16-bit
image, and wish to preserve all the fractional bits, you may want to set
normalize=false
.ktype
- Type of filter coefficients. It can be CV_32f
or
CV_64F
.public static Mat getGaborKernel(Size ksize, double sigma, double theta, double lambd, double gamma)
public static Mat getGaborKernel(Size ksize, double sigma, double theta, double lambd, double gamma, double psi, int ktype)
public static Mat getGaussianKernel(int ksize, double sigma)
Returns Gaussian filter coefficients.
The function computes and returns the ksize x 1 matrix of Gaussian filter coefficients:
G_i= alpha *e^(-(i-(ksize -1)/2)^2/(2* sigma^2)),
where i=0..ksize-1 and alpha is the scale factor chosen so that sum_i G_i=1.
Two of such generated kernels can be passed to "sepFilter2D" or to "createSeparableLinearFilter". Those functions automatically recognize smoothing kernels (a symmetrical kernel with sum of weights equal to 1) and handle them accordingly. You may also use the higher-level "GaussianBlur".
ksize
- Aperture size. It should be odd (ksize mod 2 = 1) and
positive.sigma
- Gaussian standard deviation. If it is non-positive, it is
computed from ksize
as sigma = 0.3*((ksize-1)*0.5 - 1) +
0.8
.GaussianBlur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, double, double, int)
,
sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
getStructuringElement(int, org.opencv.core.Size, org.opencv.core.Point)
,
getDerivKernels(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, boolean, int)
public static Mat getGaussianKernel(int ksize, double sigma, int ktype)
Returns Gaussian filter coefficients.
The function computes and returns the ksize x 1 matrix of Gaussian filter coefficients:
G_i= alpha *e^(-(i-(ksize -1)/2)^2/(2* sigma^2)),
where i=0..ksize-1 and alpha is the scale factor chosen so that sum_i G_i=1.
Two of such generated kernels can be passed to "sepFilter2D" or to "createSeparableLinearFilter". Those functions automatically recognize smoothing kernels (a symmetrical kernel with sum of weights equal to 1) and handle them accordingly. You may also use the higher-level "GaussianBlur".
ksize
- Aperture size. It should be odd (ksize mod 2 = 1) and
positive.sigma
- Gaussian standard deviation. If it is non-positive, it is
computed from ksize
as sigma = 0.3*((ksize-1)*0.5 - 1) +
0.8
.ktype
- Type of filter coefficients. It can be CV_32f
or
CV_64F
.GaussianBlur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, double, double, int)
,
sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
getStructuringElement(int, org.opencv.core.Size, org.opencv.core.Point)
,
getDerivKernels(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, boolean, int)
public static Mat getPerspectiveTransform(Mat src, Mat dst)
Calculates a perspective transform from four pairs of the corresponding points.
The function calculates the 3 x 3 matrix of a perspective transform so that:
t_i x'_i t_i y'_i t_i = map_matrix * x_i y_i 1
where
dst(i)=(x'_i,y'_i),<BR>src(i)=(x_i, y_i),<BR>i=0,1,2,3
src
- Coordinates of quadrangle vertices in the source image.dst
- Coordinates of the corresponding quadrangle vertices in the
destination image.Calib3d.findHomography(org.opencv.core.MatOfPoint2f, org.opencv.core.MatOfPoint2f, int, double, org.opencv.core.Mat)
,
Core.perspectiveTransform(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat)
,
warpPerspective(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
public static void getRectSubPix(Mat image, Size patchSize, Point center, Mat patch)
Retrieves a pixel rectangle from an image with sub-pixel accuracy.
The function getRectSubPix
extracts pixels from src
dst(x, y) = src(x + center.x - (dst.cols -1)*0.5, y + center.y - (dst.rows -1)*0.5)
where the values of the pixels at non-integer coordinates are retrieved using bilinear interpolation. Every channel of multi-channel images is processed independently. While the center of the rectangle must be inside the image, parts of the rectangle may be outside. In this case, the replication border mode (see "borderInterpolate") is used to extrapolate the pixel values outside of the image.
image
- a imagepatchSize
- Size of the extracted patch.center
- Floating point coordinates of the center of the extracted
rectangle within the source image. The center must be inside the image.patch
- a patchwarpAffine(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
,
warpPerspective(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
public static void getRectSubPix(Mat image, Size patchSize, Point center, Mat patch, int patchType)
Retrieves a pixel rectangle from an image with sub-pixel accuracy.
The function getRectSubPix
extracts pixels from src
dst(x, y) = src(x + center.x - (dst.cols -1)*0.5, y + center.y - (dst.rows -1)*0.5)
where the values of the pixels at non-integer coordinates are retrieved using bilinear interpolation. Every channel of multi-channel images is processed independently. While the center of the rectangle must be inside the image, parts of the rectangle may be outside. In this case, the replication border mode (see "borderInterpolate") is used to extrapolate the pixel values outside of the image.
image
- a imagepatchSize
- Size of the extracted patch.center
- Floating point coordinates of the center of the extracted
rectangle within the source image. The center must be inside the image.patch
- a patchpatchType
- Depth of the extracted pixels. By default, they have the
same depth as src
.warpAffine(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
,
warpPerspective(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
public static Mat getRotationMatrix2D(Point center, double angle, double scale)
Calculates an affine matrix of 2D rotation.
The function calculates the following matrix:
alpha beta(1- alpha) * center.x - beta * center.y - beta alpha beta * center.x + (1- alpha) * center.y
where
alpha = scale * cos angle, beta = scale * sin angle
The transformation maps the rotation center to itself. If this is not the target, adjust the shift.
center
- Center of the rotation in the source image.angle
- Rotation angle in degrees. Positive values mean
counter-clockwise rotation (the coordinate origin is assumed to be the
top-left corner).scale
- Isotropic scale factor.warpAffine(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
,
getAffineTransform(org.opencv.core.MatOfPoint2f, org.opencv.core.MatOfPoint2f)
,
Core.transform(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat)
public static Mat getStructuringElement(int shape, Size ksize)
Returns a structuring element of the specified size and shape for morphological operations.
The function constructs and returns the structuring element that can be further passed to "createMorphologyFilter", "erode", "dilate" or "morphologyEx". But you can also construct an arbitrary binary mask yourself and use it as the structuring element.
Note: When using OpenCV 1.x C API, the created structuring element
IplConvKernel* element
must be released in the end using
cvReleaseStructuringElement(&element)
.
shape
- Element shape that could be one of the following:
E_(ij)=1
Rect(0, 0, esize.width,
0.esize.height)
E_(ij) = 1 if i=anchor.y or j=anchor.x; 0 otherwise
ksize
- Size of the structuring element.public static Mat getStructuringElement(int shape, Size ksize, Point anchor)
Returns a structuring element of the specified size and shape for morphological operations.
The function constructs and returns the structuring element that can be further passed to "createMorphologyFilter", "erode", "dilate" or "morphologyEx". But you can also construct an arbitrary binary mask yourself and use it as the structuring element.
Note: When using OpenCV 1.x C API, the created structuring element
IplConvKernel* element
must be released in the end using
cvReleaseStructuringElement(&element)
.
shape
- Element shape that could be one of the following:
E_(ij)=1
Rect(0, 0, esize.width,
0.esize.height)
E_(ij) = 1 if i=anchor.y or j=anchor.x; 0 otherwise
ksize
- Size of the structuring element.anchor
- Anchor position within the element. The default value (-1,
-1) means that the anchor is at the center. Note that only the shape of
a cross-shaped element depends on the anchor position. In other cases the
anchor just regulates how much the result of the morphological operation is
shifted.public static void goodFeaturesToTrack(Mat image, MatOfPoint corners, int maxCorners, double qualityLevel, double minDistance)
Determines strong corners on an image.
The function finds the most prominent corners in the image or in the specified image region, as described in [Shi94]:
maxDistance
.
The function can be used to initialize a point-based tracker of an object.
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
.
image
- Input 8-bit or floating-point 32-bit, single-channel image.corners
- Output vector of detected corners.maxCorners
- Maximum number of corners to return. If there are more
corners than are found, the strongest of them is returned.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 "cornerMinEigenVal") or the
Harris function response (see "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.minDistance
- Minimum possible Euclidean distance between the returned
corners.cornerHarris(org.opencv.core.Mat, org.opencv.core.Mat, int, int, double, int)
,
Video.estimateRigidTransform(org.opencv.core.Mat, org.opencv.core.Mat, boolean)
,
cornerMinEigenVal(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int)
,
Video.calcOpticalFlowPyrLK(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.MatOfPoint2f, org.opencv.core.MatOfPoint2f, org.opencv.core.MatOfByte, org.opencv.core.MatOfFloat, org.opencv.core.Size, int, org.opencv.core.TermCriteria, int, double)
public static void goodFeaturesToTrack(Mat image, MatOfPoint corners, int maxCorners, double qualityLevel, double minDistance, Mat mask, int blockSize, boolean useHarrisDetector, double k)
Determines strong corners on an image.
The function finds the most prominent corners in the image or in the specified image region, as described in [Shi94]:
maxDistance
.
The function can be used to initialize a point-based tracker of an object.
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
.
image
- Input 8-bit or floating-point 32-bit, single-channel image.corners
- Output vector of detected corners.maxCorners
- Maximum number of corners to return. If there are more
corners than are found, the strongest of them is returned.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 "cornerMinEigenVal") or the
Harris function response (see "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.minDistance
- Minimum possible Euclidean distance between the returned
corners.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.blockSize
- Size of an average block for computing a derivative
covariation matrix over each pixel neighborhood. See "cornerEigenValsAndVecs".useHarrisDetector
- Parameter indicating whether to use a Harris
detector (see "cornerHarris") or "cornerMinEigenVal".k
- Free parameter of the Harris detector.cornerHarris(org.opencv.core.Mat, org.opencv.core.Mat, int, int, double, int)
,
Video.estimateRigidTransform(org.opencv.core.Mat, org.opencv.core.Mat, boolean)
,
cornerMinEigenVal(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int)
,
Video.calcOpticalFlowPyrLK(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.MatOfPoint2f, org.opencv.core.MatOfPoint2f, org.opencv.core.MatOfByte, org.opencv.core.MatOfFloat, org.opencv.core.Size, int, org.opencv.core.TermCriteria, int, double)
public static void grabCut(Mat img, Mat mask, Rect rect, Mat bgdModel, Mat fgdModel, int iterCount)
Runs the GrabCut algorithm.
The function implements the GrabCut image segmentation algorithm
(http://en.wikipedia.org/wiki/GrabCut).
See the sample grabcut.cpp
to learn how to use the function.
Note:
img
- Input 8-bit 3-channel image.mask
- Input/output 8-bit single-channel mask. The mask is initialized
by the function when mode
is set to GC_INIT_WITH_RECT
.
Its elements may have one of following values:
rect
- ROI containing a segmented object. The pixels outside of the ROI
are marked as "obvious background". The parameter is only used when
mode==GC_INIT_WITH_RECT
.bgdModel
- Temporary array for the background model. Do not modify it
while you are processing the same image.fgdModel
- Temporary arrays for the foreground model. Do not modify it
while you are processing the same image.iterCount
- Number of iterations the algorithm should make before
returning the result. Note that the result can be refined with further calls
with mode==GC_INIT_WITH_MASK
or mode==GC_EVAL
.public static void grabCut(Mat img, Mat mask, Rect rect, Mat bgdModel, Mat fgdModel, int iterCount, int mode)
Runs the GrabCut algorithm.
The function implements the GrabCut image segmentation algorithm
(http://en.wikipedia.org/wiki/GrabCut).
See the sample grabcut.cpp
to learn how to use the function.
Note:
img
- Input 8-bit 3-channel image.mask
- Input/output 8-bit single-channel mask. The mask is initialized
by the function when mode
is set to GC_INIT_WITH_RECT
.
Its elements may have one of following values:
rect
- ROI containing a segmented object. The pixels outside of the ROI
are marked as "obvious background". The parameter is only used when
mode==GC_INIT_WITH_RECT
.bgdModel
- Temporary array for the background model. Do not modify it
while you are processing the same image.fgdModel
- Temporary arrays for the foreground model. Do not modify it
while you are processing the same image.iterCount
- Number of iterations the algorithm should make before
returning the result. Note that the result can be refined with further calls
with mode==GC_INIT_WITH_MASK
or mode==GC_EVAL
.mode
- Operation mode that could be one of the following:
iterCount
iterations of the algorithm.
GC_INIT_WITH_RECT
and GC_INIT_WITH_MASK
can be combined. Then, all the pixels outside of the ROI are automatically
initialized with GC_BGD
.
public static void HoughCircles(Mat image, Mat circles, int method, double dp, double minDist)
Finds circles in a grayscale image using the Hough transform.
The function finds circles in a grayscale image using a modification of the
Hough transform.
Example:
// C++ code:
#include
#include
#include
using namespace cv;
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
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;
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.
Note:
image
- 8-bit, single-channel, grayscale input image.circles
- Output vector of found circles. Each vector is encoded as a
3-element floating-point vector (x, y, radius).method
- Detection method to use. Currently, the only implemented method
is CV_HOUGH_GRADIENT
, which is basically *21HT*, described in
[Yuen90].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.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.minEnclosingCircle(org.opencv.core.MatOfPoint2f, org.opencv.core.Point, float[])
,
fitEllipse(org.opencv.core.MatOfPoint2f)
public static void HoughCircles(Mat image, Mat circles, int method, double dp, double minDist, double param1, double param2, int minRadius, int maxRadius)
Finds circles in a grayscale image using the Hough transform.
The function finds circles in a grayscale image using a modification of the
Hough transform.
Example:
// C++ code:
#include
#include
#include
using namespace cv;
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
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;
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.
Note:
image
- 8-bit, single-channel, grayscale input image.circles
- Output vector of found circles. Each vector is encoded as a
3-element floating-point vector (x, y, radius).method
- Detection method to use. Currently, the only implemented method
is CV_HOUGH_GRADIENT
, which is basically *21HT*, described in
[Yuen90].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.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.param1
- First method-specific parameter. In case of CV_HOUGH_GRADIENT
,
it is the higher threshold of the two passed to the "Canny" edge detector
(the lower one is twice smaller).param2
- Second method-specific parameter. In case of CV_HOUGH_GRADIENT
,
it is the accumulator threshold for the circle centers at the detection
stage. The smaller it is, the more false circles may be detected. Circles,
corresponding to the larger accumulator values, will be returned first.minRadius
- Minimum circle radius.maxRadius
- Maximum circle radius.minEnclosingCircle(org.opencv.core.MatOfPoint2f, org.opencv.core.Point, float[])
,
fitEllipse(org.opencv.core.MatOfPoint2f)
public static void HoughLines(Mat image, Mat lines, double rho, double theta, int threshold)
Finds lines in a binary image using the standard Hough transform.
The function implements the standard or standard multi-scale Hough transform algorithm for line detection. See http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm for a good explanation of Hough transform. See also the example in "HoughLinesP" description.
Note:
image
- 8-bit, single-channel binary source image. The image may be
modified by the function.lines
- Output vector of lines. Each line is represented by a
two-element vector (rho, theta). rho is the distance from
the coordinate origin (0,0) (top-left corner of the image).
theta is the line rotation angle in radians (0 ~ vertical line,
pi/2 ~ horizontal line).rho
- Distance resolution of the accumulator in pixels.theta
- Angle resolution of the accumulator in radians.threshold
- Accumulator threshold parameter. Only those lines are
returned that get enough votes (>threshold).public static void HoughLines(Mat image, Mat lines, double rho, double theta, int threshold, double srn, double stn)
Finds lines in a binary image using the standard Hough transform.
The function implements the standard or standard multi-scale Hough transform algorithm for line detection. See http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm for a good explanation of Hough transform. See also the example in "HoughLinesP" description.
Note:
image
- 8-bit, single-channel binary source image. The image may be
modified by the function.lines
- Output vector of lines. Each line is represented by a
two-element vector (rho, theta). rho is the distance from
the coordinate origin (0,0) (top-left corner of the image).
theta is the line rotation angle in radians (0 ~ vertical line,
pi/2 ~ horizontal line).rho
- Distance resolution of the accumulator in pixels.theta
- Angle resolution of the accumulator in radians.threshold
- Accumulator threshold parameter. Only those lines are
returned that get enough votes (>threshold).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.stn
- For the multi-scale Hough transform, it is a divisor for the
distance resolution theta
.public static void HoughLinesP(Mat image, Mat lines, double rho, double theta, int threshold)
Finds line segments in a binary image using the probabilistic Hough transform.
The function implements the probabilistic Hough transform algorithm for line
detection, described in[Matas00]. See the line detection example below:
// C++ code:
/ * This is a standalone program. Pass an image name as the first parameter
of the program. Switch between standard and probabilistic Hough transform
by changing "#if 1" to "#if 0" and back * /
#include
#include
#include
using namespace cv;
int main(int argc, char argv)
Mat src, dst, color_dst;
if(argc != 2 || !(src=imread(argv[1], 0)).data)
return -1;
Canny(src, dst, 50, 200, 3);
cvtColor(dst, color_dst, CV_GRAY2BGR);
#if 0
vector
HoughLines(dst, lines, 1, CV_PI/180, 100);
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
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);
namedWindow("Detected Lines", 1);
imshow("Detected Lines", color_dst);
waitKey(0);
return 0;
This is a sample picture the function parameters have been tuned for:
And this is the output of the above program in case of the probabilistic Hough transform:
image
- 8-bit, single-channel binary source image. The image may be
modified by the function.lines
- Output vector of lines. Each line is represented by a 4-element
vector (x_1, y_1, x_2, y_2), where (x_1,y_1) and (x_2,
y_2) are the ending points of each detected line segment.rho
- Distance resolution of the accumulator in pixels.theta
- Angle resolution of the accumulator in radians.threshold
- Accumulator threshold parameter. Only those lines are
returned that get enough votes (>threshold).public static void HoughLinesP(Mat image, Mat lines, double rho, double theta, int threshold, double minLineLength, double maxLineGap)
Finds line segments in a binary image using the probabilistic Hough transform.
The function implements the probabilistic Hough transform algorithm for line
detection, described in[Matas00]. See the line detection example below:
// C++ code:
/ * This is a standalone program. Pass an image name as the first parameter
of the program. Switch between standard and probabilistic Hough transform
by changing "#if 1" to "#if 0" and back * /
#include
#include
#include
using namespace cv;
int main(int argc, char argv)
Mat src, dst, color_dst;
if(argc != 2 || !(src=imread(argv[1], 0)).data)
return -1;
Canny(src, dst, 50, 200, 3);
cvtColor(dst, color_dst, CV_GRAY2BGR);
#if 0
vector
HoughLines(dst, lines, 1, CV_PI/180, 100);
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
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);
namedWindow("Detected Lines", 1);
imshow("Detected Lines", color_dst);
waitKey(0);
return 0;
This is a sample picture the function parameters have been tuned for:
And this is the output of the above program in case of the probabilistic Hough transform:
image
- 8-bit, single-channel binary source image. The image may be
modified by the function.lines
- Output vector of lines. Each line is represented by a 4-element
vector (x_1, y_1, x_2, y_2), where (x_1,y_1) and (x_2,
y_2) are the ending points of each detected line segment.rho
- Distance resolution of the accumulator in pixels.theta
- Angle resolution of the accumulator in radians.threshold
- Accumulator threshold parameter. Only those lines are
returned that get enough votes (>threshold).minLineLength
- Minimum line length. Line segments shorter than that are
rejected.maxLineGap
- Maximum allowed gap between points on the same line to link
them.public static void HuMoments(Moments m, Mat hu)
Calculates seven Hu invariants.
The function calculates seven Hu invariants (introduced in [Hu62]; see also http://en.wikipedia.org/wiki/Image_moment) defined as:
hu[0]= eta _20+ eta _02 hu[1]=(eta _20- eta _02)^2+4 eta _11^2 hu[2]=(eta _30-3 eta _12)^2+ (3 eta _21- eta _03)^2 hu[3]=(eta _30+ eta _12)^2+ (eta _21+ eta _03)^2 hu[4]=(eta _30-3 eta _12)(eta _30+ eta _12)[(eta _30+ eta _12)^2-3(eta _21+ eta _03)^2]+(3 eta _21- eta _03)(eta _21+ eta _03)[3(eta _30+ eta _12)^2-(eta _21+ eta _03)^2] hu[5]=(eta _20- eta _02)[(eta _30+ eta _12)^2- (eta _21+ eta _03)^2]+4 eta _11(eta _30+ eta _12)(eta _21+ eta _03) hu[6]=(3 eta _21- eta _03)(eta _21+ eta _03)[3(eta _30+ eta _12)^2-(eta _21+ eta _03)^2]-(eta _30-3 eta _12)(eta _21+ eta _03)[3(eta _30+ eta _12)^2-(eta _21+ eta _03)^2]
where eta_(ji) stands for Moments.nu_(ji).
These values are proved to be invariants to the image scale, rotation, and reflection except the seventh one, whose sign is changed by reflection. This invariance is proved with the assumption of infinite image resolution. In case of raster images, the computed Hu invariants for the original and transformed images are a bit different.
m
- a mhu
- Output Hu invariants.matchShapes(org.opencv.core.Mat, org.opencv.core.Mat, int, double)
public static void initUndistortRectifyMap(Mat cameraMatrix, Mat distCoeffs, Mat R, Mat newCameraMatrix, Size size, int m1type, Mat map1, Mat map2)
Computes the undistortion and rectification transformation map.
The function computes the joint undistortion and rectification transformation
and represents the result in the form of maps for "remap". The undistorted
image looks like original, as if it is captured with a camera using the
camera matrix =newCameraMatrix
and zero distortion. In case of a
monocular camera, newCameraMatrix
is usually equal to
cameraMatrix
, or it can be computed by "getOptimalNewCameraMatrix"
for a better control over scaling. In case of a stereo camera,
newCameraMatrix
is normally set to P1
or
P2
computed by "stereoRectify".
Also, this new camera is oriented differently in the coordinate space,
according to R
. That, for example, helps to align two heads of a
stereo camera so that the epipolar lines on both images become horizontal and
have the same y- coordinate (in case of a horizontally aligned stereo
camera).
The function actually builds the maps for the inverse mapping algorithm that is used by "remap". That is, for each pixel (u, v) in the destination (corrected and rectified) image, the function computes the corresponding coordinates in the source image (that is, in the original image from camera). The following process is applied:
x <- (u - (c')_x)/(f')_x y <- (v - (c')_y)/(f')_y ([X Y W]) ^T <- R^(-1)*[x y 1]^T x' <- X/W y' <- Y/W x" <- x' (1 + k_1 r^2 + k_2 r^4 + k_3 r^6) + 2p_1 x' y' + p_2(r^2 + 2 x'^2) y" <- y' (1 + k_1 r^2 + k_2 r^4 + k_3 r^6) + p_1(r^2 + 2 y'^2) + 2 p_2 x' y' map_x(u,v) <- x" f_x + c_x map_y(u,v) <- y" f_y + c_y
where (k_1, k_2, p_1, p_2[, k_3]) are the distortion coefficients.
In case of a stereo camera, this function is called twice: once for each
camera head, after "stereoRectify", which in its turn is called after
"stereoCalibrate". But if the stereo camera was not calibrated, it is still
possible to compute the rectification transformations directly from the
fundamental matrix using "stereoRectifyUncalibrated". For each camera, the
function computes homography H
as the rectification
transformation in a pixel domain, not a rotation matrix R
in 3D
space. R
can be computed from H
as
R = cameraMatrix ^(-1) * H * cameraMatrix
where cameraMatrix
can be chosen arbitrarily.
cameraMatrix
- Input camera matrix A=
|f_x 0 c_x| |0 f_y c_y| |0 0 1|
.distCoeffs
- Input vector of distortion coefficients (k_1, k_2, p_1,
p_2[, k_3[, k_4, k_5, k_6]]) of 4, 5, or 8 elements. If the vector is
NULL/empty, the zero distortion coefficients are assumed.R
- Optional rectification transformation in the object space (3x3
matrix). R1
or R2
, computed by "stereoRectify" can
be passed here. If the matrix is empty, the identity transformation is
assumed. In cvInitUndistortMap
R assumed to be an identity
matrix.newCameraMatrix
- New camera matrix A'=
|f_x' 0 c_x'| |0 f_y' c_y'| |0 0 1|
.size
- Undistorted image size.m1type
- Type of the first output map that can be CV_32FC1
or CV_16SC2
. See "convertMaps" for details.map1
- The first output map.map2
- The second output map.public static float initWideAngleProjMap(Mat cameraMatrix, Mat distCoeffs, Size imageSize, int destImageWidth, int m1type, Mat map1, Mat map2)
public static float initWideAngleProjMap(Mat cameraMatrix, Mat distCoeffs, Size imageSize, int destImageWidth, int m1type, Mat map1, Mat map2, int projType, double alpha)
public static void integral(Mat src, Mat sum)
Calculates the integral of an image.
The functions calculate one or more integral images for the source image as follows:
sum(X,Y) = sum(by: x<X,y<Y) image(x,y)
sqsum(X,Y) = sum(by: x<X,y<Y) image(x,y)^2
tilted(X,Y) = sum(by: y<Y,abs(x-X+1) <= Y-y-1) image(x,y)
Using these integral images, you can calculate sum, mean, and standard deviation over a specific up-right or rotated rectangular region of the image in a constant time, for example:
sum(by: x_1 <= x < x_2, y_1 <= y < y_2) image(x,y) = sum(x_2,y_2)- sum(x_1,y_2)- sum(x_2,y_1)+ sum(x_1,y_1)
It makes possible to do a fast blurring or fast block correlation with a variable window size, for example. In case of multi-channel images, sums for each channel are accumulated independently.
As a practical example, the next figure shows the calculation of the integral
of a straight rectangle Rect(3,3,3,2)
and of a tilted rectangle
Rect(5,1,2,3)
. The selected pixels in the original
image
are shown, as well as the relative pixels in the integral
images sum
and tilted
.
src
- a srcsum
- integral image as (W+1)x(H+1), 32-bit integer or
floating-point (32f or 64f).public static void integral(Mat src, Mat sum, int sdepth)
Calculates the integral of an image.
The functions calculate one or more integral images for the source image as follows:
sum(X,Y) = sum(by: x<X,y<Y) image(x,y)
sqsum(X,Y) = sum(by: x<X,y<Y) image(x,y)^2
tilted(X,Y) = sum(by: y<Y,abs(x-X+1) <= Y-y-1) image(x,y)
Using these integral images, you can calculate sum, mean, and standard deviation over a specific up-right or rotated rectangular region of the image in a constant time, for example:
sum(by: x_1 <= x < x_2, y_1 <= y < y_2) image(x,y) = sum(x_2,y_2)- sum(x_1,y_2)- sum(x_2,y_1)+ sum(x_1,y_1)
It makes possible to do a fast blurring or fast block correlation with a variable window size, for example. In case of multi-channel images, sums for each channel are accumulated independently.
As a practical example, the next figure shows the calculation of the integral
of a straight rectangle Rect(3,3,3,2)
and of a tilted rectangle
Rect(5,1,2,3)
. The selected pixels in the original
image
are shown, as well as the relative pixels in the integral
images sum
and tilted
.
src
- a srcsum
- integral image as (W+1)x(H+1), 32-bit integer or
floating-point (32f or 64f).sdepth
- desired depth of the integral and the tilted integral images,
CV_32S
, CV_32F
, or CV_64F
.public static void integral2(Mat src, Mat sum, Mat sqsum)
Calculates the integral of an image.
The functions calculate one or more integral images for the source image as follows:
sum(X,Y) = sum(by: x<X,y<Y) image(x,y)
sqsum(X,Y) = sum(by: x<X,y<Y) image(x,y)^2
tilted(X,Y) = sum(by: y<Y,abs(x-X+1) <= Y-y-1) image(x,y)
Using these integral images, you can calculate sum, mean, and standard deviation over a specific up-right or rotated rectangular region of the image in a constant time, for example:
sum(by: x_1 <= x < x_2, y_1 <= y < y_2) image(x,y) = sum(x_2,y_2)- sum(x_1,y_2)- sum(x_2,y_1)+ sum(x_1,y_1)
It makes possible to do a fast blurring or fast block correlation with a variable window size, for example. In case of multi-channel images, sums for each channel are accumulated independently.
As a practical example, the next figure shows the calculation of the integral
of a straight rectangle Rect(3,3,3,2)
and of a tilted rectangle
Rect(5,1,2,3)
. The selected pixels in the original
image
are shown, as well as the relative pixels in the integral
images sum
and tilted
.
src
- a srcsum
- integral image as (W+1)x(H+1), 32-bit integer or
floating-point (32f or 64f).sqsum
- integral image for squared pixel values; it is (W+1)x(H+1),
double-precision floating-point (64f) array.public static void integral2(Mat src, Mat sum, Mat sqsum, int sdepth)
Calculates the integral of an image.
The functions calculate one or more integral images for the source image as follows:
sum(X,Y) = sum(by: x<X,y<Y) image(x,y)
sqsum(X,Y) = sum(by: x<X,y<Y) image(x,y)^2
tilted(X,Y) = sum(by: y<Y,abs(x-X+1) <= Y-y-1) image(x,y)
Using these integral images, you can calculate sum, mean, and standard deviation over a specific up-right or rotated rectangular region of the image in a constant time, for example:
sum(by: x_1 <= x < x_2, y_1 <= y < y_2) image(x,y) = sum(x_2,y_2)- sum(x_1,y_2)- sum(x_2,y_1)+ sum(x_1,y_1)
It makes possible to do a fast blurring or fast block correlation with a variable window size, for example. In case of multi-channel images, sums for each channel are accumulated independently.
As a practical example, the next figure shows the calculation of the integral
of a straight rectangle Rect(3,3,3,2)
and of a tilted rectangle
Rect(5,1,2,3)
. The selected pixels in the original
image
are shown, as well as the relative pixels in the integral
images sum
and tilted
.
src
- a srcsum
- integral image as (W+1)x(H+1), 32-bit integer or
floating-point (32f or 64f).sqsum
- integral image for squared pixel values; it is (W+1)x(H+1),
double-precision floating-point (64f) array.sdepth
- desired depth of the integral and the tilted integral images,
CV_32S
, CV_32F
, or CV_64F
.public static void integral3(Mat src, Mat sum, Mat sqsum, Mat tilted)
Calculates the integral of an image.
The functions calculate one or more integral images for the source image as follows:
sum(X,Y) = sum(by: x<X,y<Y) image(x,y)
sqsum(X,Y) = sum(by: x<X,y<Y) image(x,y)^2
tilted(X,Y) = sum(by: y<Y,abs(x-X+1) <= Y-y-1) image(x,y)
Using these integral images, you can calculate sum, mean, and standard deviation over a specific up-right or rotated rectangular region of the image in a constant time, for example:
sum(by: x_1 <= x < x_2, y_1 <= y < y_2) image(x,y) = sum(x_2,y_2)- sum(x_1,y_2)- sum(x_2,y_1)+ sum(x_1,y_1)
It makes possible to do a fast blurring or fast block correlation with a variable window size, for example. In case of multi-channel images, sums for each channel are accumulated independently.
As a practical example, the next figure shows the calculation of the integral
of a straight rectangle Rect(3,3,3,2)
and of a tilted rectangle
Rect(5,1,2,3)
. The selected pixels in the original
image
are shown, as well as the relative pixels in the integral
images sum
and tilted
.
src
- a srcsum
- integral image as (W+1)x(H+1), 32-bit integer or
floating-point (32f or 64f).sqsum
- integral image for squared pixel values; it is (W+1)x(H+1),
double-precision floating-point (64f) array.tilted
- integral for the image rotated by 45 degrees; it is
(W+1)x(H+1) array with the same data type as sum
.public static void integral3(Mat src, Mat sum, Mat sqsum, Mat tilted, int sdepth)
Calculates the integral of an image.
The functions calculate one or more integral images for the source image as follows:
sum(X,Y) = sum(by: x<X,y<Y) image(x,y)
sqsum(X,Y) = sum(by: x<X,y<Y) image(x,y)^2
tilted(X,Y) = sum(by: y<Y,abs(x-X+1) <= Y-y-1) image(x,y)
Using these integral images, you can calculate sum, mean, and standard deviation over a specific up-right or rotated rectangular region of the image in a constant time, for example:
sum(by: x_1 <= x < x_2, y_1 <= y < y_2) image(x,y) = sum(x_2,y_2)- sum(x_1,y_2)- sum(x_2,y_1)+ sum(x_1,y_1)
It makes possible to do a fast blurring or fast block correlation with a variable window size, for example. In case of multi-channel images, sums for each channel are accumulated independently.
As a practical example, the next figure shows the calculation of the integral
of a straight rectangle Rect(3,3,3,2)
and of a tilted rectangle
Rect(5,1,2,3)
. The selected pixels in the original
image
are shown, as well as the relative pixels in the integral
images sum
and tilted
.
src
- a srcsum
- integral image as (W+1)x(H+1), 32-bit integer or
floating-point (32f or 64f).sqsum
- integral image for squared pixel values; it is (W+1)x(H+1),
double-precision floating-point (64f) array.tilted
- integral for the image rotated by 45 degrees; it is
(W+1)x(H+1) array with the same data type as sum
.sdepth
- desired depth of the integral and the tilted integral images,
CV_32S
, CV_32F
, or CV_64F
.public static float intersectConvexConvex(Mat _p1, Mat _p2, Mat _p12, boolean handleNested)
public static void invertAffineTransform(Mat M, Mat iM)
Inverts an affine transformation.
The function computes an inverse affine transformation represented by 2 x
3 matrix M
:
a_11 a_12 b_1 a_21 a_22 b_2
The result is also a 2 x 3 matrix of the same type as
M
.
M
- Original affine transformation.iM
- Output reverse affine transformation.public static boolean isContourConvex(MatOfPoint contour)
Tests a contour convexity.
The function tests whether the input contour is convex or not. The contour must be simple, that is, without self-intersections. Otherwise, the function output is undefined.
contour
- Input vector of 2D points, stored in:
std.vector<>
or Mat
(C++ interface)
CvSeq*
or CvMat*
(C interface)
public static void Laplacian(Mat src, Mat dst, int ddepth)
Calculates the Laplacian of an image.
The function calculates the Laplacian of the source image by adding up the second x and y derivatives calculated using the Sobel operator:
dst = Delta src = (d^2 src)/(dx^2) + (d^2 src)/(dy^2)
This is done when ksize > 1
. When ksize == 1
, the
Laplacian is computed by filtering the image with the following 3 x
3 aperture:
vecthreethree 0101(-4)1010
Note:
src
- Source image.dst
- Destination image of the same size and the same number of channels
as src
.ddepth
- Desired depth of the destination image.Scharr(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, double, double, int)
,
Sobel(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, int, double, double, int)
public static void Laplacian(Mat src, Mat dst, int ddepth, int ksize, double scale, double delta)
Calculates the Laplacian of an image.
The function calculates the Laplacian of the source image by adding up the second x and y derivatives calculated using the Sobel operator:
dst = Delta src = (d^2 src)/(dx^2) + (d^2 src)/(dy^2)
This is done when ksize > 1
. When ksize == 1
, the
Laplacian is computed by filtering the image with the following 3 x
3 aperture:
vecthreethree 0101(-4)1010
Note:
src
- Source image.dst
- Destination image of the same size and the same number of channels
as src
.ddepth
- Desired depth of the destination image.ksize
- Aperture size used to compute the second-derivative filters. See
"getDerivKernels" for details. The size must be positive and odd.scale
- Optional scale factor for the computed Laplacian values. By
default, no scaling is applied. See "getDerivKernels" for details.delta
- Optional delta value that is added to the results prior to
storing them in dst
.Scharr(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, double, double, int)
,
Sobel(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, int, double, double, int)
public static void Laplacian(Mat src, Mat dst, int ddepth, int ksize, double scale, double delta, int borderType)
Calculates the Laplacian of an image.
The function calculates the Laplacian of the source image by adding up the second x and y derivatives calculated using the Sobel operator:
dst = Delta src = (d^2 src)/(dx^2) + (d^2 src)/(dy^2)
This is done when ksize > 1
. When ksize == 1
, the
Laplacian is computed by filtering the image with the following 3 x
3 aperture:
vecthreethree 0101(-4)1010
Note:
src
- Source image.dst
- Destination image of the same size and the same number of channels
as src
.ddepth
- Desired depth of the destination image.ksize
- Aperture size used to compute the second-derivative filters. See
"getDerivKernels" for details. The size must be positive and odd.scale
- Optional scale factor for the computed Laplacian values. By
default, no scaling is applied. See "getDerivKernels" for details.delta
- Optional delta value that is added to the results prior to
storing them in dst
.borderType
- Pixel extrapolation method. See "borderInterpolate" for
details.Scharr(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, double, double, int)
,
Sobel(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, int, double, double, int)
public static double matchShapes(Mat contour1, Mat contour2, int method, double parameter)
Compares two shapes.
The function compares two shapes. All three implemented methods use the Hu
invariants (see "HuMoments") as follows (A denotes object1
,B
denotes object2
):
I_1(A,B) = sum(by: i=1...7) <= ft|1/(m^A_i) - 1/(m^B_i) right|
I_2(A,B) = sum(by: i=1...7) <= ft|m^A_i - m^B_i right|
I_3(A,B) = max _(i=1...7)(<= ft| m^A_i - m^B_i right|)/(<= ft| m^A_i right|)
where
m^A_i = sign(h^A_i) * log(h^A_i) m^B_i = sign(h^B_i) * log(h^B_i)
and h^A_i, h^B_i are the Hu moments of A and B, respectively.
contour1
- a contour1contour2
- a contour2method
- Comparison method: CV_CONTOURS_MATCH_I1
,
CV_CONTOURS_MATCH_I2
\
or CV_CONTOURS_MATCH_I3
(see the details below).
parameter
- Method-specific parameter (not supported now).public static void matchTemplate(Mat image, Mat templ, Mat result, int method)
Compares a template against overlapped image regions.
The function slides through image
, compares the overlapped
patches of size w x h against templ
using the specified
method and stores the comparison results in result
. Here are the
formulae for the available comparison methods (I denotes
image
, T template
, R
result
). The summation is done over template and/or the image
patch: x' = 0...w-1, y' = 0...h-1
R(x,y)= sum(by: x',y')(T(x',y')-I(x+x',y+y'))^2
R(x,y)= (sum_(x',y')(T(x',y')-I(x+x',y+y'))^2)/(sqrt(sum_(x',y')T(x',y')^2 * sum_(x',y') I(x+x',y+y')^2))
R(x,y)= sum(by: x',y')(T(x',y') * I(x+x',y+y'))
R(x,y)= (sum_(x',y')(T(x',y') * I(x+x',y+y')))/(sqrt(sum_(x',y')T(x',y')^2 * sum_(x',y') I(x+x',y+y')^2))
R(x,y)= sum(by: x',y')(T'(x',y') * I'(x+x',y+y'))
where
T'(x',y')=T(x',y') - 1/(w * h) * sum(by: x'',y'') T(x'',y'') I'(x+x',y+y')=I(x+x',y+y') - 1/(w * h) * sum(by: x'',y'') I(x+x'',y+y'')
R(x,y)= (sum_(x',y')(T'(x',y') * I'(x+x',y+y')))/(sqrt(sum_(x',y')T'(x',y')^2 * sum_(x',y') I'(x+x',y+y')^2))
After the function finishes the comparison, the best matches can be found as
global minimums (when CV_TM_SQDIFF
was used) or maximums (when
CV_TM_CCORR
or CV_TM_CCOEFF
was used) using the
"minMaxLoc" function. In case of a color image, template summation in the
numerator and each sum in the denominator is done over all of the channels
and separate mean values are used for each channel. That is, the function can
take a color template and a color image. The result will still be a
single-channel image, which is easier to analyze.
Note:
image
- Image where the search is running. It must be 8-bit or 32-bit
floating-point.templ
- Searched template. It must be not greater than the source image
and have the same data type.result
- Map of comparison results. It must be single-channel 32-bit
floating-point. If image
is W x H and templ
is w x h, then result
is (W-w+1) x(H-h+1).method
- Parameter specifying the comparison method (see below).public static void medianBlur(Mat src, Mat dst, int ksize)
Blurs an image using the median filter.
The function smoothes an image using the median filter with the ksize x ksize aperture. Each channel of a multi-channel image is processed independently. In-place operation is supported.
src
- input 1-, 3-, or 4-channel image; when ksize
is 3 or
5, the image depth should be CV_8U
, CV_16U
, or
CV_32F
, for larger aperture sizes, it can only be
CV_8U
.dst
- destination array of the same size and type as src
.ksize
- aperture linear size; it must be odd and greater than 1, for
example: 3, 5, 7...boxFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Size, org.opencv.core.Point, boolean, int)
,
GaussianBlur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, double, double, int)
,
bilateralFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, double, double, int)
,
blur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, org.opencv.core.Point, int)
public static RotatedRect minAreaRect(MatOfPoint2f points)
Finds a rotated rectangle of the minimum area enclosing the input 2D point set.
The function calculates and returns the minimum-area bounding rectangle
(possibly rotated) for a specified point set. See the OpenCV sample
minarea.cpp
.
Developer should keep in mind that the returned rotatedRect can contain
negative indices when data is close the the containing Mat element boundary.
points
- Input vector of 2D points, stored in:
std.vector<>
or Mat
(C++ interface)
CvSeq*
or CvMat*
(C interface)
public static void minEnclosingCircle(MatOfPoint2f points, Point center, float[] radius)
Finds a circle of the minimum area enclosing a 2D point set.
The function finds the minimal enclosing circle of a 2D point set using an
iterative algorithm. See the OpenCV sample minarea.cpp
.
points
- Input vector of 2D points, stored in:
std.vector<>
or Mat
(C++ interface)
CvSeq*
or CvMat*
(C interface)
center
- Output center of the circle.radius
- Output radius of the circle.public static Moments moments(Mat array)
Calculates all of the moments up to the third order of a polygon or rasterized shape.
The function computes moments, up to the 3rd order, of a vector shape or a
rasterized shape. The results are returned in the structure Moments
defined as:
// C++ code:
class Moments
public:
Moments();
Moments(double m00, double m10, double m01, double m20, double m11,
double m02, double m30, double m21, double m12, double m03);
Moments(const CvMoments& moments);
operator CvMoments() const;
// spatial moments
double m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;
// central moments
double mu20, mu11, mu02, mu30, mu21, mu12, mu03;
// central normalized moments
double nu20, nu11, nu02, nu30, nu21, nu12, nu03;
In case of a raster image, the spatial moments Moments.m_(ji) are computed as:
m _(ji)= sum(by: x,y)(array(x,y) * x^j * y^i)
The central moments Moments.mu_(ji) are computed as:
mu _(ji)= sum(by: x,y)(array(x,y) * (x - x")^j * (y - y")^i)
where (x", y") is the mass center:
x" = (m_10)/(m_(00)), y" = (m_01)/(m_(00))
The normalized central moments Moments.nu_(ij) are computed as:
nu _(ji)= (mu_(ji))/(m_(00)^((i+j)/2+1)).
Note:
mu_00=m_00, nu_00=1 nu_10=mu_10=mu_01=mu_10=0, hence the values are not stored.
The moments of a contour are defined in the same way but computed using the Green's formula (see http://en.wikipedia.org/wiki/Green_theorem). So, due to a limited raster resolution, the moments computed for a contour are slightly different from the moments computed for the same rasterized contour.
Note:
Since the contour moments are computed using Green formula, you may get
seemingly odd results for contours with self-intersections, e.g. a zero area
(m00
) for butterfly-shaped contours.
array
- Raster image (single-channel, 8-bit or floating-point 2D array)
or an array (1 x N or N x 1) of 2D points (Point
or Point2f
).contourArea(org.opencv.core.Mat, boolean)
,
arcLength(org.opencv.core.MatOfPoint2f, boolean)
public static Moments moments(Mat array, boolean binaryImage)
Calculates all of the moments up to the third order of a polygon or rasterized shape.
The function computes moments, up to the 3rd order, of a vector shape or a
rasterized shape. The results are returned in the structure Moments
defined as:
// C++ code:
class Moments
public:
Moments();
Moments(double m00, double m10, double m01, double m20, double m11,
double m02, double m30, double m21, double m12, double m03);
Moments(const CvMoments& moments);
operator CvMoments() const;
// spatial moments
double m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;
// central moments
double mu20, mu11, mu02, mu30, mu21, mu12, mu03;
// central normalized moments
double nu20, nu11, nu02, nu30, nu21, nu12, nu03;
In case of a raster image, the spatial moments Moments.m_(ji) are computed as:
m _(ji)= sum(by: x,y)(array(x,y) * x^j * y^i)
The central moments Moments.mu_(ji) are computed as:
mu _(ji)= sum(by: x,y)(array(x,y) * (x - x")^j * (y - y")^i)
where (x", y") is the mass center:
x" = (m_10)/(m_(00)), y" = (m_01)/(m_(00))
The normalized central moments Moments.nu_(ij) are computed as:
nu _(ji)= (mu_(ji))/(m_(00)^((i+j)/2+1)).
Note:
mu_00=m_00, nu_00=1 nu_10=mu_10=mu_01=mu_10=0, hence the values are not stored.
The moments of a contour are defined in the same way but computed using the Green's formula (see http://en.wikipedia.org/wiki/Green_theorem). So, due to a limited raster resolution, the moments computed for a contour are slightly different from the moments computed for the same rasterized contour.
Note:
Since the contour moments are computed using Green formula, you may get
seemingly odd results for contours with self-intersections, e.g. a zero area
(m00
) for butterfly-shaped contours.
array
- Raster image (single-channel, 8-bit or floating-point 2D array)
or an array (1 x N or N x 1) of 2D points (Point
or Point2f
).binaryImage
- If it is true, all non-zero image pixels are treated as
1's. The parameter is used for images only.contourArea(org.opencv.core.Mat, boolean)
,
arcLength(org.opencv.core.MatOfPoint2f, boolean)
public static void morphologyEx(Mat src, Mat dst, int op, Mat kernel)
Performs advanced morphological transformations.
The function can perform advanced morphological transformations using an erosion and dilation as basic operations.
Opening operation:
dst = open(src, element)= dilate(erode(src, element))
Closing operation:
dst = close(src, element)= erode(dilate(src, element))
Morphological gradient:
dst = morph_grad(src, element)= dilate(src, element)- erode(src, element)
"Top hat":
dst = tophat(src, element)= src - open(src, element)
"Black hat":
dst = blackhat(src, element)= close(src, element)- src
"Hit and Miss": Only supported for CV_8UC1 binary images. Tutorial can be found in this page: https://web.archive.org/web/20160316070407/http://opencv-code.com/tutorials/hit-or-miss-transform-in-opencv/
Any of the operations can be done in-place. In case of multi-channel images, each channel is processed independently.
Note:
src
- Source image. The number of channels can be arbitrary. The depth
should be one of CV_8U
, CV_16U
, CV_16S
,
CV_32F
or CV_64F
.dst
- Destination image of the same size and type as src
.op
- Type of a morphological operation that can be one of the following:
kernel
- a kernelerode(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
,
dilate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
public static void morphologyEx(Mat src, Mat dst, int op, Mat kernel, Point anchor, int iterations)
Performs advanced morphological transformations.
The function can perform advanced morphological transformations using an erosion and dilation as basic operations.
Opening operation:
dst = open(src, element)= dilate(erode(src, element))
Closing operation:
dst = close(src, element)= erode(dilate(src, element))
Morphological gradient:
dst = morph_grad(src, element)= dilate(src, element)- erode(src, element)
"Top hat":
dst = tophat(src, element)= src - open(src, element)
"Black hat":
dst = blackhat(src, element)= close(src, element)- src
"Hit and Miss": Only supported for CV_8UC1 binary images. Tutorial can be found in this page: https://web.archive.org/web/20160316070407/http://opencv-code.com/tutorials/hit-or-miss-transform-in-opencv/
Any of the operations can be done in-place. In case of multi-channel images, each channel is processed independently.
Note:
src
- Source image. The number of channels can be arbitrary. The depth
should be one of CV_8U
, CV_16U
, CV_16S
,
CV_32F
or CV_64F
.dst
- Destination image of the same size and type as src
.op
- Type of a morphological operation that can be one of the following:
kernel
- a kernelanchor
- a anchoriterations
- Number of times erosion and dilation are applied.erode(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
,
dilate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
public static void morphologyEx(Mat src, Mat dst, int op, Mat kernel, Point anchor, int iterations, int borderType, Scalar borderValue)
Performs advanced morphological transformations.
The function can perform advanced morphological transformations using an erosion and dilation as basic operations.
Opening operation:
dst = open(src, element)= dilate(erode(src, element))
Closing operation:
dst = close(src, element)= erode(dilate(src, element))
Morphological gradient:
dst = morph_grad(src, element)= dilate(src, element)- erode(src, element)
"Top hat":
dst = tophat(src, element)= src - open(src, element)
"Black hat":
dst = blackhat(src, element)= close(src, element)- src
"Hit and Miss": Only supported for CV_8UC1 binary images. Tutorial can be found in this page: https://web.archive.org/web/20160316070407/http://opencv-code.com/tutorials/hit-or-miss-transform-in-opencv/
Any of the operations can be done in-place. In case of multi-channel images, each channel is processed independently.
Note:
src
- Source image. The number of channels can be arbitrary. The depth
should be one of CV_8U
, CV_16U
, CV_16S
,
CV_32F
or CV_64F
.dst
- Destination image of the same size and type as src
.op
- Type of a morphological operation that can be one of the following:
kernel
- a kernelanchor
- a anchoriterations
- Number of times erosion and dilation are applied.borderType
- Pixel extrapolation method. See "borderInterpolate" for
details.borderValue
- Border value in case of a constant border. The default
value has a special meaning. See "createMorphologyFilter" for details.erode(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
,
dilate(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, int, int, org.opencv.core.Scalar)
public static Point phaseCorrelate(Mat src1, Mat src2)
The function is used to detect translational shifts that occur between two images. The operation takes advantage of the Fourier shift theorem for detecting the translational shift in the frequency domain. It can be used for fast image registration as well as motion estimation. For more information please see http://en.wikipedia.org/wiki/Phase_correlation.
Calculates the cross-power spectrum of two supplied source arrays. The arrays are padded if needed with "getOptimalDFTSize".
Return value: detected phase shift (sub-pixel) between the two arrays.
The function performs the following equations
mathbf(G)_a = mathcal(F)(src_1), mathbf(G)_b = mathcal(F)(src_2)
where mathcal(F) is the forward DFT.
R = (mathbf(G)_a mathbf(G)_b^*)/(|mathbf(G)_a mathbf(G)_b^*|)
r = mathcal(F)^(-1)(R)
(Delta x, Delta y) = weightedCentroid (arg max_((x, y))(r))
src1
- Source floating point array (CV_32FC1 or CV_64FC1)src2
- Source floating point array (CV_32FC1 or CV_64FC1)createHanningWindow(org.opencv.core.Mat, org.opencv.core.Size, int)
,
Core.dft(org.opencv.core.Mat, org.opencv.core.Mat, int, int)
,
Core.mulSpectrums(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, int, boolean)
,
Core.getOptimalDFTSize(int)
,
Core.idft(org.opencv.core.Mat, org.opencv.core.Mat, int, int)
public static Point phaseCorrelate(Mat src1, Mat src2, Mat window)
The function is used to detect translational shifts that occur between two images. The operation takes advantage of the Fourier shift theorem for detecting the translational shift in the frequency domain. It can be used for fast image registration as well as motion estimation. For more information please see http://en.wikipedia.org/wiki/Phase_correlation.
Calculates the cross-power spectrum of two supplied source arrays. The arrays are padded if needed with "getOptimalDFTSize".
Return value: detected phase shift (sub-pixel) between the two arrays.
The function performs the following equations
mathbf(G)_a = mathcal(F)(src_1), mathbf(G)_b = mathcal(F)(src_2)
where mathcal(F) is the forward DFT.
R = (mathbf(G)_a mathbf(G)_b^*)/(|mathbf(G)_a mathbf(G)_b^*|)
r = mathcal(F)^(-1)(R)
(Delta x, Delta y) = weightedCentroid (arg max_((x, y))(r))
src1
- Source floating point array (CV_32FC1 or CV_64FC1)src2
- Source floating point array (CV_32FC1 or CV_64FC1)window
- Floating point array with windowing coefficients to reduce edge
effects (optional).createHanningWindow(org.opencv.core.Mat, org.opencv.core.Size, int)
,
Core.dft(org.opencv.core.Mat, org.opencv.core.Mat, int, int)
,
Core.mulSpectrums(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, int, boolean)
,
Core.getOptimalDFTSize(int)
,
Core.idft(org.opencv.core.Mat, org.opencv.core.Mat, int, int)
public static Point phaseCorrelateRes(Mat src1, Mat src2, Mat window, double[] response)
public static double pointPolygonTest(MatOfPoint2f contour, Point pt, boolean measureDist)
Performs a point-in-contour test.
The function determines whether the point is inside a contour, outside, or
lies on an edge (or coincides with a vertex). It returns positive (inside),
negative (outside), or zero (on an edge) value, correspondingly. When
measureDist=false
, the return value is +1, -1, and 0,
respectively. Otherwise, the return value is a signed distance between the
point and the nearest contour edge.
See below a sample output of the function where each image pixel is tested against the contour.
contour
- Input contour.pt
- Point tested against the contour.measureDist
- If true, the function estimates the signed distance from
the point to the nearest contour edge. Otherwise, the function only checks if
the point is inside a contour or not.public static void preCornerDetect(Mat src, Mat dst, int ksize)
Calculates a feature map for corner detection.
The function calculates the complex spatial derivative-based function of the source image
dst = (D_x src)^2 * D_(yy) src + (D_y src)^2 * D_(xx) src - 2 D_x src * D_y src * D_(xy) src
where D_x,D_y are the first image derivatives,
D_(xx),D_(yy) are the second image derivatives, and
D_(xy) is the mixed derivative.
The corners can be found as local maximums of the functions, as shown below:
// C++ code:
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;
src
- Source single-channel 8-bit of floating-point image.dst
- Output image that has the type CV_32F
and the same
size as src
.ksize
- Aperture size of the "Sobel".public static void preCornerDetect(Mat src, Mat dst, int ksize, int borderType)
Calculates a feature map for corner detection.
The function calculates the complex spatial derivative-based function of the source image
dst = (D_x src)^2 * D_(yy) src + (D_y src)^2 * D_(xx) src - 2 D_x src * D_y src * D_(xy) src
where D_x,D_y are the first image derivatives,
D_(xx),D_(yy) are the second image derivatives, and
D_(xy) is the mixed derivative.
The corners can be found as local maximums of the functions, as shown below:
// C++ code:
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;
src
- Source single-channel 8-bit of floating-point image.dst
- Output image that has the type CV_32F
and the same
size as src
.ksize
- Aperture size of the "Sobel".borderType
- Pixel extrapolation method. See "borderInterpolate".public static void pyrDown(Mat src, Mat dst)
Blurs an image and downsamples it.
By default, size of the output image is computed as Size((src.cols+1)/2,
(src.rows+1)/2)
, but in any case, the following conditions should be
satisfied:
ltBR gt| dstsize.width *2-src.cols| <= 2 |dstsize.height *2-src.rows| <= 2
The function performs the downsampling step of the Gaussian pyramid construction. First, it convolves the source image with the kernel:
1/256 1 4 6 4 1 4 16 24 16 4 6 24 36 24 6 4 16 24 16 4 1 4 6 4 1
Then, it downsamples the image by rejecting even rows and columns.
src
- input image.dst
- output image; it has the specified size and the same type as
src
.public static void pyrDown(Mat src, Mat dst, Size dstsize)
Blurs an image and downsamples it.
By default, size of the output image is computed as Size((src.cols+1)/2,
(src.rows+1)/2)
, but in any case, the following conditions should be
satisfied:
ltBR gt| dstsize.width *2-src.cols| <= 2 |dstsize.height *2-src.rows| <= 2
The function performs the downsampling step of the Gaussian pyramid construction. First, it convolves the source image with the kernel:
1/256 1 4 6 4 1 4 16 24 16 4 6 24 36 24 6 4 16 24 16 4 1 4 6 4 1
Then, it downsamples the image by rejecting even rows and columns.
src
- input image.dst
- output image; it has the specified size and the same type as
src
.dstsize
- size of the output image.public static void pyrDown(Mat src, Mat dst, Size dstsize, int borderType)
Blurs an image and downsamples it.
By default, size of the output image is computed as Size((src.cols+1)/2,
(src.rows+1)/2)
, but in any case, the following conditions should be
satisfied:
ltBR gt| dstsize.width *2-src.cols| <= 2 |dstsize.height *2-src.rows| <= 2
The function performs the downsampling step of the Gaussian pyramid construction. First, it convolves the source image with the kernel:
1/256 1 4 6 4 1 4 16 24 16 4 6 24 36 24 6 4 16 24 16 4 1 4 6 4 1
Then, it downsamples the image by rejecting even rows and columns.
src
- input image.dst
- output image; it has the specified size and the same type as
src
.dstsize
- size of the output image.borderType
- Pixel extrapolation method (BORDER_CONSTANT don't
supported). See "borderInterpolate" for details.public static void pyrMeanShiftFiltering(Mat src, Mat dst, double sp, double sr)
Performs initial step of meanshift segmentation of an image.
The function implements the filtering stage of meanshift segmentation, that
is, the output of the function is the filtered "posterized" image with color
gradients and fine-grain texture flattened. At every pixel (X,Y)
of the input image (or down-sized input image, see below) the function
executes meanshift iterations, that is, the pixel (X,Y)
neighborhood in the joint space-color hyperspace is considered:
(x,y): X- sp <= x <= X+ sp, Y- sp <= y <= Y+ sp, ||(R,G,B)-(r,g,b)|| <= sr
where (R,G,B)
and (r,g,b)
are the vectors of color
components at (X,Y)
and (x,y)
, respectively
(though, the algorithm does not depend on the color space used, so any
3-component color space can be used instead). Over the neighborhood the
average spatial value (X',Y')
and average color vector
(R',G',B')
are found and they act as the neighborhood center on
the next iteration:
(X,Y)~(X',Y'), (R,G,B)~(R',G',B').
After the iterations over, the color components of the initial pixel (that is, the pixel from where the iterations started) are set to the final value (average color at the last iteration):
I(X,Y) <- (R*,G*,B*)
When maxLevel > 0
, the gaussian pyramid of maxLevel+1
levels is built, and the above procedure is run on the smallest layer first.
After that, the results are propagated to the larger layer and the iterations
are run again only on those pixels where the layer colors differ by more than
sr
from the lower-resolution layer of the pyramid. That makes
boundaries of color regions sharper. Note that the results will be actually
different from the ones obtained by running the meanshift procedure on the
whole original image (i.e. when maxLevel==0
).
Note:
src
- The source 8-bit, 3-channel image.dst
- The destination image of the same format and the same size as the
source.sp
- The spatial window radius.sr
- The color window radius.public static void pyrMeanShiftFiltering(Mat src, Mat dst, double sp, double sr, int maxLevel, TermCriteria termcrit)
Performs initial step of meanshift segmentation of an image.
The function implements the filtering stage of meanshift segmentation, that
is, the output of the function is the filtered "posterized" image with color
gradients and fine-grain texture flattened. At every pixel (X,Y)
of the input image (or down-sized input image, see below) the function
executes meanshift iterations, that is, the pixel (X,Y)
neighborhood in the joint space-color hyperspace is considered:
(x,y): X- sp <= x <= X+ sp, Y- sp <= y <= Y+ sp, ||(R,G,B)-(r,g,b)|| <= sr
where (R,G,B)
and (r,g,b)
are the vectors of color
components at (X,Y)
and (x,y)
, respectively
(though, the algorithm does not depend on the color space used, so any
3-component color space can be used instead). Over the neighborhood the
average spatial value (X',Y')
and average color vector
(R',G',B')
are found and they act as the neighborhood center on
the next iteration:
(X,Y)~(X',Y'), (R,G,B)~(R',G',B').
After the iterations over, the color components of the initial pixel (that is, the pixel from where the iterations started) are set to the final value (average color at the last iteration):
I(X,Y) <- (R*,G*,B*)
When maxLevel > 0
, the gaussian pyramid of maxLevel+1
levels is built, and the above procedure is run on the smallest layer first.
After that, the results are propagated to the larger layer and the iterations
are run again only on those pixels where the layer colors differ by more than
sr
from the lower-resolution layer of the pyramid. That makes
boundaries of color regions sharper. Note that the results will be actually
different from the ones obtained by running the meanshift procedure on the
whole original image (i.e. when maxLevel==0
).
Note:
src
- The source 8-bit, 3-channel image.dst
- The destination image of the same format and the same size as the
source.sp
- The spatial window radius.sr
- The color window radius.maxLevel
- Maximum level of the pyramid for the segmentation.termcrit
- Termination criteria: when to stop meanshift iterations.public static void pyrUp(Mat src, Mat dst)
Upsamples an image and then blurs it.
By default, size of the output image is computed as Size(src.cols*2,
(src.rows*2)
, but in any case, the following conditions should be
satisfied:
ltBR gt| dstsize.width -src.cols*2| <= (dstsize.width mod 2) |dstsize.height -src.rows*2| <= (dstsize.height mod 2)
The function performs the upsampling step of the Gaussian pyramid construction, though it can actually be used to construct the Laplacian pyramid. First, it upsamples the source image by injecting even zero rows and columns and then convolves the result with the same kernel as in "pyrDown" multiplied by 4.
Note:
src
- input image.dst
- output image. It has the specified size and the same type as
src
.public static void pyrUp(Mat src, Mat dst, Size dstsize)
Upsamples an image and then blurs it.
By default, size of the output image is computed as Size(src.cols*2,
(src.rows*2)
, but in any case, the following conditions should be
satisfied:
ltBR gt| dstsize.width -src.cols*2| <= (dstsize.width mod 2) |dstsize.height -src.rows*2| <= (dstsize.height mod 2)
The function performs the upsampling step of the Gaussian pyramid construction, though it can actually be used to construct the Laplacian pyramid. First, it upsamples the source image by injecting even zero rows and columns and then convolves the result with the same kernel as in "pyrDown" multiplied by 4.
Note:
src
- input image.dst
- output image. It has the specified size and the same type as
src
.dstsize
- size of the output image.public static void pyrUp(Mat src, Mat dst, Size dstsize, int borderType)
Upsamples an image and then blurs it.
By default, size of the output image is computed as Size(src.cols*2,
(src.rows*2)
, but in any case, the following conditions should be
satisfied:
ltBR gt| dstsize.width -src.cols*2| <= (dstsize.width mod 2) |dstsize.height -src.rows*2| <= (dstsize.height mod 2)
The function performs the upsampling step of the Gaussian pyramid construction, though it can actually be used to construct the Laplacian pyramid. First, it upsamples the source image by injecting even zero rows and columns and then convolves the result with the same kernel as in "pyrDown" multiplied by 4.
Note:
src
- input image.dst
- output image. It has the specified size and the same type as
src
.dstsize
- size of the output image.borderType
- Pixel extrapolation method (only BORDER_DEFAULT supported).
See "borderInterpolate" for details.public static void remap(Mat src, Mat dst, Mat map1, Mat map2, int interpolation)
Applies a generic geometrical transformation to an image.
The function remap
transforms the source image using the
specified map:
dst(x,y) = src(map_x(x,y),map_y(x,y))
where values of pixels with non-integer coordinates are computed using one of
available interpolation methods.
map_x and map_y can be encoded as separate floating-point
maps in map_1 and map_2 respectively, or interleaved
floating-point maps of (x,y) in map_1, or fixed-point maps
created by using "convertMaps". The reason you might want to convert from
floating to fixed-point representations of a map is that they can yield much
faster (~2x) remapping operations. In the converted case, map_1
contains pairs (cvFloor(x), cvFloor(y))
and map_2
contains indices in a table of interpolation coefficients.
This function cannot operate in-place.
src
- Source image.dst
- Destination image. It has the same size as map1
and
the same type as src
.map1
- The first map of either (x,y)
points or just
x
values having the type CV_16SC2
,
CV_32FC1
, or CV_32FC2
. See "convertMaps" for
details on converting a floating point representation to fixed-point for
speed.map2
- The second map of y
values having the type
CV_16UC1
, CV_32FC1
, or none (empty map if
map1
is (x,y)
points), respectively.interpolation
- Interpolation method (see "resize"). The method
INTER_AREA
is not supported by this function.public static void remap(Mat src, Mat dst, Mat map1, Mat map2, int interpolation, int borderMode, Scalar borderValue)
Applies a generic geometrical transformation to an image.
The function remap
transforms the source image using the
specified map:
dst(x,y) = src(map_x(x,y),map_y(x,y))
where values of pixels with non-integer coordinates are computed using one of
available interpolation methods.
map_x and map_y can be encoded as separate floating-point
maps in map_1 and map_2 respectively, or interleaved
floating-point maps of (x,y) in map_1, or fixed-point maps
created by using "convertMaps". The reason you might want to convert from
floating to fixed-point representations of a map is that they can yield much
faster (~2x) remapping operations. In the converted case, map_1
contains pairs (cvFloor(x), cvFloor(y))
and map_2
contains indices in a table of interpolation coefficients.
This function cannot operate in-place.
src
- Source image.dst
- Destination image. It has the same size as map1
and
the same type as src
.map1
- The first map of either (x,y)
points or just
x
values having the type CV_16SC2
,
CV_32FC1
, or CV_32FC2
. See "convertMaps" for
details on converting a floating point representation to fixed-point for
speed.map2
- The second map of y
values having the type
CV_16UC1
, CV_32FC1
, or none (empty map if
map1
is (x,y)
points), respectively.interpolation
- Interpolation method (see "resize"). The method
INTER_AREA
is not supported by this function.borderMode
- Pixel extrapolation method (see "borderInterpolate"). When
borderMode=BORDER_TRANSPARENT
, it means that the pixels in the
destination image that corresponds to the "outliers" in the source image are
not modified by the function.borderValue
- Value used in case of a constant border. By default, it is
0.public static void resize(Mat src, Mat dst, Size dsize)
Resizes an image.
The function resize
resizes the image src
down to
or up to the specified size.Note that the initial dst
type or
size are not taken into account. Instead, the size and type are derived from
the src
,dsize
,fx
, and fy
.
If you want to resize src
so that it fits the pre-created
dst
, you may call the function as follows:
// C++ code:
// explicitly specify dsize=dst.size(); fx and fy will be computed from that.
resize(src, dst, dst.size(), 0, 0, interpolation);
If you want to decimate the image by factor of 2 in each direction, you can call the function this way:
// specify fx and fy and let the function compute the destination image size.
resize(src, dst, Size(), 0.5, 0.5, interpolation);
To shrink an image, it will generally look best with CV_INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with CV_INTER_CUBIC (slow) or CV_INTER_LINEAR (faster but still looks OK).
src
- input image.dst
- output image; it has the size dsize
(when it is
non-zero) or the size computed from src.size()
, fx
,
and fy
; the type of dst
is the same as of
src
.dsize
- output image size; if it equals zero, it is computed as:
dsize = Size(round(fx*src.cols), round(fy*src.rows))
Either dsize
or both fx
and fy
must be
non-zero.
warpAffine(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
,
remap(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, int, int, org.opencv.core.Scalar)
,
warpPerspective(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
public static void resize(Mat src, Mat dst, Size dsize, double fx, double fy, int interpolation)
Resizes an image.
The function resize
resizes the image src
down to
or up to the specified size.Note that the initial dst
type or
size are not taken into account. Instead, the size and type are derived from
the src
,dsize
,fx
, and fy
.
If you want to resize src
so that it fits the pre-created
dst
, you may call the function as follows:
// C++ code:
// explicitly specify dsize=dst.size(); fx and fy will be computed from that.
resize(src, dst, dst.size(), 0, 0, interpolation);
If you want to decimate the image by factor of 2 in each direction, you can call the function this way:
// specify fx and fy and let the function compute the destination image size.
resize(src, dst, Size(), 0.5, 0.5, interpolation);
To shrink an image, it will generally look best with CV_INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with CV_INTER_CUBIC (slow) or CV_INTER_LINEAR (faster but still looks OK).
src
- input image.dst
- output image; it has the size dsize
(when it is
non-zero) or the size computed from src.size()
, fx
,
and fy
; the type of dst
is the same as of
src
.dsize
- output image size; if it equals zero, it is computed as:
dsize = Size(round(fx*src.cols), round(fy*src.rows))
Either dsize
or both fx
and fy
must be
non-zero.
fx
- scale factor along the horizontal axis; when it equals 0, it is
computed as
(double)dsize.width/src.cols
fy
- scale factor along the vertical axis; when it equals 0, it is
computed as
(double)dsize.height/src.rows
interpolation
- interpolation method:
INTER_NEAREST
method.
warpAffine(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
,
remap(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, int, int, org.opencv.core.Scalar)
,
warpPerspective(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, int, int, org.opencv.core.Scalar)
public static void Scharr(Mat src, Mat dst, int ddepth, int dx, int dy)
Calculates the first x- or y- image derivative using Scharr operator.
The function computes the first x- or y- spatial image derivative using the Scharr operator. The call
Scharr(src, dst, ddepth, dx, dy, scale, delta, borderType)
is equivalent to
Sobel(src, dst, ddepth, dx, dy, CV_SCHARR, scale, delta, borderType).
src
- input image.dst
- output image of the same size and the same number of channels as
src
.ddepth
- output image depth (see "Sobel" for the list of supported
combination of src.depth()
and ddepth
).dx
- order of the derivative x.dy
- order of the derivative y.Core.cartToPolar(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, boolean)
public static void Scharr(Mat src, Mat dst, int ddepth, int dx, int dy, double scale, double delta)
Calculates the first x- or y- image derivative using Scharr operator.
The function computes the first x- or y- spatial image derivative using the Scharr operator. The call
Scharr(src, dst, ddepth, dx, dy, scale, delta, borderType)
is equivalent to
Sobel(src, dst, ddepth, dx, dy, CV_SCHARR, scale, delta, borderType).
src
- input image.dst
- output image of the same size and the same number of channels as
src
.ddepth
- output image depth (see "Sobel" for the list of supported
combination of src.depth()
and ddepth
).dx
- order of the derivative x.dy
- order of the derivative y.scale
- optional scale factor for the computed derivative values; by
default, no scaling is applied (see "getDerivKernels" for details).delta
- optional delta value that is added to the results prior to
storing them in dst
.Core.cartToPolar(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, boolean)
public static void Scharr(Mat src, Mat dst, int ddepth, int dx, int dy, double scale, double delta, int borderType)
Calculates the first x- or y- image derivative using Scharr operator.
The function computes the first x- or y- spatial image derivative using the Scharr operator. The call
Scharr(src, dst, ddepth, dx, dy, scale, delta, borderType)
is equivalent to
Sobel(src, dst, ddepth, dx, dy, CV_SCHARR, scale, delta, borderType).
src
- input image.dst
- output image of the same size and the same number of channels as
src
.ddepth
- output image depth (see "Sobel" for the list of supported
combination of src.depth()
and ddepth
).dx
- order of the derivative x.dy
- order of the derivative y.scale
- optional scale factor for the computed derivative values; by
default, no scaling is applied (see "getDerivKernels" for details).delta
- optional delta value that is added to the results prior to
storing them in dst
.borderType
- pixel extrapolation method (see "borderInterpolate" for
details).Core.cartToPolar(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, boolean)
public static void sepFilter2D(Mat src, Mat dst, int ddepth, Mat kernelX, Mat kernelY)
Applies a separable linear filter to an image.
The function applies a separable linear filter to the image. That is, first,
every row of src
is filtered with the 1D kernel
kernelX
. Then, every column of the result is filtered with the
1D kernel kernelY
. The final result shifted by delta
is stored in dst
.
src
- Source image.dst
- Destination image of the same size and the same number of channels
as src
.ddepth
- Destination image depth. The following combination of
src.depth()
and ddepth
are supported:
src.depth()
= CV_8U
, ddepth
=
-1/CV_16S
/CV_32F
/CV_64F
src.depth()
= CV_16U
/CV_16S
,
ddepth
= -1/CV_32F
/CV_64F
src.depth()
= CV_32F
, ddepth
=
-1/CV_32F
/CV_64F
src.depth()
= CV_64F
, ddepth
=
-1/CV_64F
when ddepth=-1
, the destination image will have the same depth
as the source.
kernelX
- Coefficients for filtering each row.kernelY
- Coefficients for filtering each column.GaussianBlur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, double, double, int)
,
Sobel(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, int, double, double, int)
,
boxFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Size, org.opencv.core.Point, boolean, int)
,
blur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, org.opencv.core.Point, int)
,
filter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, double, int)
public static void sepFilter2D(Mat src, Mat dst, int ddepth, Mat kernelX, Mat kernelY, Point anchor, double delta)
Applies a separable linear filter to an image.
The function applies a separable linear filter to the image. That is, first,
every row of src
is filtered with the 1D kernel
kernelX
. Then, every column of the result is filtered with the
1D kernel kernelY
. The final result shifted by delta
is stored in dst
.
src
- Source image.dst
- Destination image of the same size and the same number of channels
as src
.ddepth
- Destination image depth. The following combination of
src.depth()
and ddepth
are supported:
src.depth()
= CV_8U
, ddepth
=
-1/CV_16S
/CV_32F
/CV_64F
src.depth()
= CV_16U
/CV_16S
,
ddepth
= -1/CV_32F
/CV_64F
src.depth()
= CV_32F
, ddepth
=
-1/CV_32F
/CV_64F
src.depth()
= CV_64F
, ddepth
=
-1/CV_64F
when ddepth=-1
, the destination image will have the same depth
as the source.
kernelX
- Coefficients for filtering each row.kernelY
- Coefficients for filtering each column.anchor
- Anchor position within the kernel. The default value
(-1,-1) means that the anchor is at the kernel center.delta
- Value added to the filtered results before storing them.GaussianBlur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, double, double, int)
,
Sobel(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, int, double, double, int)
,
boxFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Size, org.opencv.core.Point, boolean, int)
,
blur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, org.opencv.core.Point, int)
,
filter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, double, int)
public static void sepFilter2D(Mat src, Mat dst, int ddepth, Mat kernelX, Mat kernelY, Point anchor, double delta, int borderType)
Applies a separable linear filter to an image.
The function applies a separable linear filter to the image. That is, first,
every row of src
is filtered with the 1D kernel
kernelX
. Then, every column of the result is filtered with the
1D kernel kernelY
. The final result shifted by delta
is stored in dst
.
src
- Source image.dst
- Destination image of the same size and the same number of channels
as src
.ddepth
- Destination image depth. The following combination of
src.depth()
and ddepth
are supported:
src.depth()
= CV_8U
, ddepth
=
-1/CV_16S
/CV_32F
/CV_64F
src.depth()
= CV_16U
/CV_16S
,
ddepth
= -1/CV_32F
/CV_64F
src.depth()
= CV_32F
, ddepth
=
-1/CV_32F
/CV_64F
src.depth()
= CV_64F
, ddepth
=
-1/CV_64F
when ddepth=-1
, the destination image will have the same depth
as the source.
kernelX
- Coefficients for filtering each row.kernelY
- Coefficients for filtering each column.anchor
- Anchor position within the kernel. The default value
(-1,-1) means that the anchor is at the kernel center.delta
- Value added to the filtered results before storing them.borderType
- Pixel extrapolation method. See "borderInterpolate" for
details.GaussianBlur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, double, double, int)
,
Sobel(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, int, double, double, int)
,
boxFilter(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Size, org.opencv.core.Point, boolean, int)
,
blur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, org.opencv.core.Point, int)
,
filter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, double, int)
public static void Sobel(Mat src, Mat dst, int ddepth, int dx, int dy)
Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.
In all cases except one, the ksize x<BR>ksize separable kernel
is used to calculate the derivative. When ksize = 1, the 3 x
1 or 1 x 3 kernel is used (that is, no Gaussian smoothing is
done). ksize = 1
can only be used for the first or the second x-
or y- derivatives.
There is also the special value ksize = CV_SCHARR
(-1) that
corresponds to the 3x3 Scharr filter that may give more accurate
results than the 3x3 Sobel. The Scharr aperture is
|-3 0 3| |-10 0 10| |-3 0 3|
for the x-derivative, or transposed for the y-derivative.
The function calculates an image derivative by convolving the image with the appropriate kernel:
dst = (d^(xorder+yorder) src)/(dx^(xorder) dy^(yorder))
The Sobel operators combine Gaussian smoothing and differentiation, so the
result is more or less resistant to the noise. 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. The
first case corresponds to a kernel of:
|-1 0 1| |-2 0 2| |-1 0 1|
The second case corresponds to a kernel of:
|-1 -2 -1| |0 0 0| |1 2 1|
src
- input image.dst
- output image of the same size and the same number of channels as
src
.ddepth
- output image depth; the following combinations of
src.depth()
and ddepth
are supported:
src.depth()
= CV_8U
, ddepth
=
-1/CV_16S
/CV_32F
/CV_64F
src.depth()
= CV_16U
/CV_16S
,
ddepth
= -1/CV_32F
/CV_64F
src.depth()
= CV_32F
, ddepth
=
-1/CV_32F
/CV_64F
src.depth()
= CV_64F
, ddepth
=
-1/CV_64F
when ddepth=-1
, the destination image will have the same depth
as the source; in the case of 8-bit input images it will result in truncated
derivatives.
dx
- a dxdy
- a dyGaussianBlur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, double, double, int)
,
Core.cartToPolar(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, boolean)
,
sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
Laplacian(org.opencv.core.Mat, org.opencv.core.Mat, int, int, double, double, int)
,
Scharr(org.opencv.core.Mat, org.opencv.core.Mat, int, int, int, double, double, int)
,
filter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Point, double, int)
public static void Sobel(Mat src, Mat dst, int ddepth, int dx, int dy, int ksize, double scale, double delta)
Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.
In all cases except one, the ksize x<BR>ksize separable kernel
is used to calculate the derivative. When ksize = 1, the 3 x
1 or 1 x 3 kernel is used (that is, no Gaussian smoothing is
done). ksize = 1
can only be used for the first or the second x-
or y- derivatives.
There is also the special value ksize = CV_SCHARR
(-1) that
corresponds to the 3x3 Scharr filter that may give more accurate
results than the 3x3 Sobel. The Scharr aperture is
|-3 0 3| |-10 0 10| |-3 0 3|
for the x-derivative, or transposed for the y-derivative.
The function calculates an image derivative by convolving the image with the appropriate kernel:
dst = (d^(xorder+yorder) src)/(dx^(xorder) dy^(yorder))
The Sobel operators combine Gaussian smoothing and differentiation, so the
result is more or less resistant to the noise. 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. The
first case corresponds to a kernel of:
|-1 0 1| |-2 0 2| |-1 0 1|
The second case corresponds to a kernel of:
|-1 -2 -1| |0 0 0| |1 2 1|
src
- input image.dst
- output image of the same size and the same number of channels as
src
.ddepth
- output image depth; the following combinations of
src.depth()
and ddepth
are supported:
src.depth()
= CV_8U
, ddepth
=
-1/CV_16S
/CV_32F
/CV_64F
src.depth()
= CV_16U
/CV_16S
,
ddepth
= -1/CV_32F
/CV_64F
src.depth()
= CV_32F
, ddepth
=
-1/CV_32F
/CV_64F
src.depth()
= CV_64F
, ddepth
=
-1/CV_64F
when ddepth=-1
, the destination image will have the same depth
as the source; in the case of 8-bit input images it will result in truncated
derivatives.
dx
- a dxdy
- a dyksize
- size of the extended Sobel kernel; it must be 1, 3, 5, or 7.scale
- optional scale factor for the computed derivative values; by
default, no scaling is applied (see "getDerivKernels" for details).delta
- optional delta value that is added to the results prior to
storing them in dst
.GaussianBlur(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Size, double, double, int)
,
Core.cartToPolar(org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Mat, boolean)
,
sepFilter2D(org.opencv.core.Mat, org.opencv.core.Mat, int, org.opencv.core.Mat, org.opencv.core.Mat, org.opencv.core.Point, double, int)
,
Laplacian(org.opencv.core.Mat, org.opencv.core.Mat, int, int, double, double, int)
,