Functions and classes described in this section are used to perform various linear or nonlinear 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 multichannel 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 nonexisting pixels. For example, if we want to smooth an image using a Gaussian filter, then during the processing of the leftmost 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 leftmost image pixels (i.e. use “replicated border” extrapolation method), or assume that all the nonexisting pixels are zeros (“contant border” extrapolation method) etc.
An IplConvKernel is a rectangular convolution kernel, created by function CreateStructuringElementEx .
Copies an image and makes a border around it.
Parameters: 


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 inplace 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 inplace 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. Inplace 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 inplace 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.
Smooths the image in one of several ways.
Parameters: 


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 singlechannel images only and supports accumulation of 8bit to 16bit format (similar to Sobel and Laplace ) and 32bit floating point to 32bit floatingpoint format.
Simple blur and Gaussian blur support 1 or 3channel, 8bit and 32bit floating point images. These two methods can process images inplace.
Median and bilateral filters work with 1 or 3channel 8bit images and can not process images inplace.
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 xderivative or transposed for the yderivative.
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 16bit destination image if the source image is 8bit. The result can be converted back to 8bit using the ConvertScale or the ConvertScaleAbs function. Besides 8bit images the function can process 32bit floatingpoint images. Both the source and the destination must be singlechannel images of equal size or equal ROI size.