Technology Sharing

C: A brief overview of the filter2D function

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

OpenCVfilter2DThe convolution function is a very powerful tool for applying various linear filters to images. This function is able to process each pixel in the image and modify the characteristics of the image by convolving it with the specified convolution kernel (or filter). The following is aboutfilter2DFunction details:

Function prototype

In OpenCV,filter2DThe prototype of the function is as follows:

void cv::filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel,  
                  Point anchor = Point(-1,-1), double delta = 0,  
                  int borderType = BORDER_DEFAULT);

Parameter Description

  • src: input image.
  • dst: Output image, same as input imagesrcHave the same size and number of channels.
  • ddepth: The desired depth of the output image (data type). When it is -1, it means the output image has the same depth as the input image.
  • kernel: The convolution kernel (or filter) is a single-channel floating-point matrix. This matrix defines how to perform convolution operations on the input image.
  • anchor: The anchor point of the kernel, which indicates the relative position of the filter point within the kernel. The anchor point should be inside the kernel; the default value (-1,-1) means the anchor point is at the center of the kernel.
  • delta: An optional value that will be used when storing the filtered pixels intodstPreviously added to the filtered pixel.
  • borderType: Pixel extrapolation method used to process pixels outside the image boundary. It determines how to deal with these boundary pixels when the convolution kernel exceeds the image boundary. Common options includeBORDER_CONSTANTBORDER_REPLICATEBORDER_REFLECTwait.

working principle

filter2DThe function actually performs a convolution operation (although technically, it computes a correlation rather than a strict convolution, since there is no flipping of the kernel involved). The convolution operation involves "sliding" the kernel over the input image and multiplying the corresponding elements of each submatrix that is the same size as the kernel, and then adding the results. This process generates a new pixel value that is placed at the corresponding position in the output image. This process is repeated as the kernel slides over the entire input image until the full output image is generated.

Application Scenario

By changing the convolution kernel,filter2DThe function can achieve a variety of image processing effects, including but not limited to:

  • Image Sharpening: Using a specific sharpening convolution kernel can make the edges of the image clearer.
  • Mean Filter: Using an average convolution kernel can reduce image noise, but may make the image blurry.
  • Gaussian filtering:Gaussian filtering is a commonly used image smoothing technique that can reduce image noise while maintaining edge information.filter2DThe function itself does not directly provide the generation of Gaussian kernels, but it can be done throughgetGaussianKernelThe function generates a Gaussian kernel and usesfilter2DPerform Gaussian filtering.
  • Edge Detection: By designing specific convolution kernels (such as Sobel operator, Laplacian operator, etc.), edges in images can be detected.

Sample Code

The following is a usefilter2DSample code for mean filtering using the function (Python version):

#include