Mean filtering, box filtering, and Gaussian filtering in OPENCV are all linear filtering methods. Since the result of linear filtering is a linear combination of all pixel values, pixels containing noise will also be taken into account, and noise will not be eliminated. exists in a softer way. If this noise needs to be removed, it may be better to use nonlinear filtering. Median filtering uses the median value of all pixel values in the neighborhood to replace the pixel value of the current pixel.
1. Principle introduction
The median filter will take the pixel values of the current pixel and its surrounding pixels (there are odd pixels in total), sort these pixel values, and then use the pixel value in the middle position as the pixel value of the current pixel.
1.1. Function syntax
In OpenCV, the function that implements median filtering is medianBlur, and its syntax format is as follows: medianBlur(src,dst,ksize)
● dst is the processed image obtained after median filtering
● src is the source image to be processed. It can have any number of channels and can process each channel independently. Image depth should be one of CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
● ksize is the size of the filter kernel. The filter kernel size refers to the height and width of its neighborhood image during the filtering process. It should be noted that the core size must be an odd number greater than 1, such as 3, 5, 7, 9, 11, etc.
1.2. Examples
原数据
97 95 94
93 78 90
66 91 101
中值滤波后的数据
97 95 94
93 93 90
66 91 101
If ksize is set to 3, its neighborhood is 3×3. Sort the pixel values of the pixels in the 3×3 neighborhood, and sort in ascending order to get the sequence value: [66, 78, 90, 91, 93, 94, 95, 97, 101]. In this sequence, the value at the center position (also called the center point or median point) is "93", so replace the pixel value 78 with 93 as the new pixel value for the current point.
1.3 Others
- Median filtering is better at eliminating noise than linear filtering;
- As the filter kernel increases, the image will also become blurred, and the processing time will be higher;