Functions and classes described in this section are used to perform various linear or non-linear filtering operations on 2D images (represented as Mat‘s), that is, for each pixel location
in the source image some its (normally rectangular) neighborhood is considered and used to compute the response. In case of a linear filter it is a weighted sum of pixel values, in case of morphological operations it is the minimum or maximum etc. The computed response is stored to the destination image at the same location
. It means, that the output image will be of the same size as the input image. Normally, the functions supports multi-channel arrays, in which case every channel is processed independently, therefore the output image will also have the same number of channels as the input one.
Another common feature of the functions and classes described in this section is that, unlike simple arithmetic functions, they need to extrapolate values of some non-existing pixels. For example, if we want to smooth an image using a Gaussian
filter, then during the processing of the left-most pixels in each row we need pixels to the left of them, i.e. outside of the image. We can let those pixels be the same as the left-most image pixels (i.e. use “replicated border” extrapolation method), or assume that all the non-existing pixels are zeros (“contant border” extrapolation method) etc.
:ref:``
An IplConvKernel is a rectangular convolution kernel, created by functionCreateStructuringElementEx.CopyMakeBorderCopies an image and makes a border around it.
param src: The source image
param dst: The destination image
param offset: Coordinates of the top-left corner (or bottom-left in the case of images with bottom-left origin) of the destination image rectangle where the source image (or its ROI) is copied. Size of the rectanlge matches the source image size/ROI size
param bordertype: Type of the border to create around the copied source image rectangle; types include:
- IPL_BORDER_CONSTANT - border is filled with the fixed value, passed as last parameter of the function.
- IPL_BORDER_REPLICATE - the pixels from the top and bottom rows, the left-most and right-most columns are replicated to fill the border.
| param value: | Value of the border pixels if bordertype is IPL_BORDER_CONSTANT |
|---|
The function copies the source 2D array into the interior of the destination array and makes a border of the specified type around the copied area. The function is useful when one needs to emulate border type that is different from the one embedded into a specific algorithm implementation. For example, morphological functions, as well as most of other filtering functions in OpenCV, internally use replication border type, while the user may need a zero border or a border, filled with 1’s or 255’s.
Creates a structuring element.
| Parameters: |
|
|---|
The function CreateStructuringElementEx allocates and fills the structure IplConvKernel, which can be used as a structuring element in the morphological operations.
Dilates an image by using a specific structuring element.
| Parameters: |
|
|---|
The function dilates the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the maximum is taken:

The function supports the in-place mode. Dilation can be applied several (iterations) times. For color images, each channel is processed independently.
Erodes an image by using a specific structuring element.
| Parameters: |
|
|---|
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:

The function supports the in-place mode. Erosion can be applied several (iterations) times. For color images, each channel is processed independently.
Convolves an image with the kernel.
| Parameters: |
|
|---|
The function applies an arbitrary linear filter to the image. In-place operation is supported. When the aperture is partially outside the image, the function interpolates outlier pixel values from the nearest pixels that are inside the image.
Calculates the Laplacian of an image.
| Parameters: |
|
|---|
The function calculates the Laplacian of the source image by adding up the second x and y derivatives calculated using the Sobel operator:

Setting apertureSize = 1 gives the fastest variant that is equal to convolving the image with the following kernel:

Similar to the Sobel function, no scaling is done and the same combinations of input and output formats are supported.
Performs advanced morphological transformations.
| Parameters: |
|
|---|
The function can perform advanced morphological transformations using erosion and dilation as basic operations.
Opening:

Closing:

Morphological gradient:

“Top hat”:

“Black hat”:

The temporary image temp is required for a morphological gradient and, in the case of in-place operation, for “top hat” and “black hat”.
Downsamples an image.
| Parameters: |
|
|---|
The function performs the downsampling step of the Gaussian pyramid decomposition. First it convolves the source image with the specified filter and then downsamples the image by rejecting even rows and columns.
Deletes a structuring element.
| Parameter: | element – Pointer to the deleted structuring element |
|---|
The function releases the structure IplConvKernel that is no longer needed. If *element is NULL, the function has no effect.
Smooths the image in one of several ways.
| Parameters: |
|
|---|
Using standard sigma for small kernels (
to
) gives better speed. If param3 is not zero, while param1 and param2 are zeros, the kernel size is calculated from the sigma (to provide accurate enough operation).
The function smooths an image using one of several methods. Every of the methods has some features and restrictions listed below
Blur with no scaling works with single-channel images only and supports accumulation of 8-bit to 16-bit format (similar to Sobel and Laplace) and 32-bit floating point to 32-bit floating-point format.
Simple blur and Gaussian blur support 1- or 3-channel, 8-bit and 32-bit floating point images. These two methods can process images in-place.
Median and bilateral filters work with 1- or 3-channel 8-bit images and can not process images in-place.
Calculates the first, second, third or mixed image derivatives using an extended Sobel operator.
| Parameters: |
|
|---|
In all cases except 1, an
separable kernel will be used to calculate the derivative. For
a
or
a kernel is used (Gaussian smoothing is not done). There is also the special value CV_SCHARR (-1) that corresponds to a
Scharr filter that may give more accurate results than a
Sobel. Scharr aperture is

for the x-derivative or transposed for the y-derivative.
The function calculates the image derivative by convolving the image with the appropriate kernel:

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, apertureSize = 3) or (xorder = 0, yorder = 1, apertureSize = 3) to calculate the first x- or y- image derivative. The first case corresponds to a kernel of:

and the second one corresponds to a kernel of:

or a kernel of:

depending on the image origin (origin field of IplImage structure). No scaling is done, so the destination image usually has larger numbers (in absolute values) than the source image does. To avoid overflow, the function requires a 16-bit destination image if the source image is 8-bit. The result can be converted back to 8-bit using the ConvertScale or the ConvertScaleAbs function. Besides 8-bit images the function can process 32-bit floating-point images. Both the source and the destination must be single-channel images of equal size or equal ROI size.