opencv median filtering

 

Median filtering is a commonly used image filtering algorithm. It performs median filtering on multiple points around a pixel, averages the gray value of the point based on the gray value of the surrounding pixels, and makes the gray value of these points The degree values ​​are similar to achieve the purpose of smooth denoising. Median filtering is widely used in image processing and has been widely used in image filtering and image enhancement processing. The principle of median filtering is as follows: (1) Median filtering is an algorithm that achieves denoising by calculating the mean of the gray value of each pixel in a grayscale image. It uses gray-scale mean instead of gray-scale variance to filter out noise while ensuring that the basic information of the gray-scale image remains unchanged. The essence of median filtering is to determine the optimal smoothing window size and direction by performing local statistics and analysis on the image. (2) Median filtering In image processing, using the gray level mean to replace the gray level variance is the simplest method. Although this algorithm is simple and easy to use, it is not effective in noise suppression. The advantage of using gray-scale mean instead of gray-scale variance is that it can filter out noise and retain detailed information. (3) Median filtering is to calculate the median of multiple points for one point, and then average the grayscale values ​​​​of the pixels around this point to achieve the purpose of denoising.

  • 1. Algorithmic thinking

    Median filtering is an image processing technology based on statistical analysis. Its basic idea is to use gray-scale mean instead of gray-scale variance for image denoising. Its basic principle is to divide the image into several equal-sized windows in the gray value space, and calculate the gray value of each pixel in each window and the gray value of the pixel in the neighbor window. Ratio, use this ratio as a grayscale feature vector of the pixel, then perform median filtering on this feature vector, and weight the average of it with the ratio of the grayscale values ​​of all pixels in the neighborhood window to obtain smoothing image after. In the filtering process, noise is often added to the area where noise needs to be filtered out to achieve better filtering effects. In this way, some pixels with less obvious grayscale features and more noise will appear in the areas where noise filtering is not required, thus affecting the image quality. Therefore, the median filter algorithm not only has the ability to filter out noise, but also has good edge preservation capabilities.

  • 2. Algorithm process

    (1) Grayscale of the image: For each pixel, its grayscale (grayscale value) is described by a brightness (brightness value), and a maximum brightness (maximum value), a minimum brightness (minimum value) and a medium value are used. value to represent. (2) Find the mean of the gray value: For each pixel, calculate the mean using the difference between the gray values ​​of several adjacent pixels and the gray value of the pixel. (3) Determine the median: Determine which pixels are the current pixels by comparing the averages. (4) Select a smoothing window: In order to suppress noise, a smoothing window of appropriate size can be selected based on the grayscale characteristics of the image and the characteristics of the noise. (5) Calculate the average: Compare the current pixel with other adjacent pixels. If the gray level of the current pixel is similar to that of other adjacent pixels, then the pixel is regarded as the current pixel. (6) Output result: Use the obtained median value as the output result. (7) Parameter setting: Set the size of the smoothing window so that all pixels in the smoothing window can obtain the smoothing effect, thereby achieving the purpose of removing noise.

  • 3. Window size

    The window size determines the number of pixels in the window, which determines the effect of filtering. If the window is too large, the noise points will be submerged during the filtering process and the filtering effect will become worse; if the window is too small, the noise points will be submerged and the filtering effect will become worse. Therefore, it is important to choose the appropriate window size. The commonly used principle for selecting the size of the median filter window is: taking the image pixel as the unit, the difference between the median values ​​of two adjacent pixels cannot exceed half of the gray value of the pixel. In general, choose smaller for small image windows; choose larger for large image windows. For example: for a 300×300 image, you can choose: (1) When the window size is 4, a maximum of 50 pixels can be processed for noise points;

  • 4. Threshold setting

    Median filtering is a simple and effective image filtering algorithm. It has the following advantages: (1) Effectively suppresses noise, retains image details, and has a better filtering effect on grayscale images. (2) The edges and contours of the image are maintained, and spots caused by sudden changes in the gray value of the image are eliminated. (3) It has strong robustness to noise. Generally, when there is a lot of noise, the filtering effect is good. (4) Avoid stretching the image and avoid stripes and spots caused by stretching the image. (5) Good filtering effects can be obtained and information such as edges, texture, shape, etc. in the image can be maintained. If you need to enhance the image, you can add a step of median enhancement before median filtering to achieve better enhancement effects. (6) Median filtering can be performed on multiple images and the threshold can be automatically set. This is one of the most important parameters in median filtering. (7) The median filter algorithm is simple, easy to implement, and has a small amount of calculation, so it has been widely used in real-time processing and real-time imaging. (8) The algorithm has fast processing speed and fast operation speed, and is suitable for real-time processing.

  • 5. Median calculation

    Median filtering is a commonly used image processing technique that effectively suppresses noise to a certain extent, but the noise may be retained. In order to eliminate noise, people have designed many median filtering methods. Common methods include average method, median average method and adaptive median method. These methods can achieve smooth denoising of images to a certain extent, but their effects are not very ideal. In order to obtain better results, people introduce a parameter in the median filtering method - the weighted median, which reflects the information required for image processing. The weighted median is to select several points around the pixel and calculate their mean value for these points. The calculation formula is: where: i is a pixel point; s is a point adjacent to i, and its distance from i is h (s); h (s) represents the mean value of the k-th pixel point; a, b, c represents the mean, j, k of the k-th pixel point i respectively. The biggest advantage of the median filtering algorithm is that it can smooth noise and improve image quality, and the filtering results are clear and stable, which to a certain extent reduces the interference caused by noise on subsequent processing.

  • 6. Smoothing

    In image processing, median filtering is usually used to smooth images. Median filtering is a basic image smoothing technique that can eliminate interference caused by noise while maintaining image characteristics. It is mainly used to solve problems such as image noise, edge blur, and texture distortion. (1) Smooth window size. Traditional median filtering uses a fixed window size method. It continuously changes the window size according to certain rules during the processing. When the window size is proportional to the noise intensity, the best denoising effect can be achieved. The median filter uses a fixed window size, which does not consider the influence of noise intensity during the processing, so that it can obtain the best denoising effect. (2) Filtering direction. Traditional median filtering generally uses clockwise or counterclockwise filtering, while median filtering can be selected based on the location of the noise in the image, thereby filtering out the noise more effectively.

  • 7. Output results

    (1) During the execution of the algorithm, it is necessary to consider whether the pixel values ​​are uniform, otherwise local peaks will appear. When there are local peaks, possible noise points in the image can be determined. For grayscale images, there is usually no need to perform median filtering on them because the grayscale distribution of the image is uneven and it is difficult to find noise points. But for color images, if the pixel values ​​are evenly distributed, the noise points present in the image can be directly determined. (2) There are three main cases of algorithm output results: The first case is to output the results directly (that is, after median filtering): The second case is to perform median filtering after outputting the results and then output the results, that is, the median filtering window is used as the original Part of the window is processed. This method works very well for noise suppression. Although this method is not as effective as the first method, it has the advantage that it can suppress noise very well. (3) The algorithm usually involves the selection of the filter window size. Generally, a small window can be selected, which ensures the noise suppression effect and achieves better image quality. However, if the window is too small, image details may be destroyed; if the window is too large, information may be lost.

The following is the commonly used OpenCV median filtering code:

1. Perform median filtering on grayscale images```python import cv2
# Read grayscale images img = cv2.imread('image.jpg', 0) # Median filtering img_median = cv2.medianBlur(img, 5) # Display the original image and the median filtered image cv2.imshow('Original Image', img) cv2.imshow('Median Filtered Image', img_median) cv2.waitKey(0) cv2.destroyAllWindows() ```` 2. Perform median filtering on color images```python import cv2 # Read color images img = cv2.imread('image.jpg') # Median filtering img_median = cv2.medianBlur(img, 5) # Display the original image and the middle image Value filtered image cv2.imshow('Original Image', img) cv2.imshow('Median Filtered Image', img_median) cv2.waitKey(0) cv2.destroyAllWindows() ```
























3. Perform real-time median filtering on the video```python import cv2
# Open the camera cap = cv2.VideoCapture(0) while True: # Read the video frame ret, frame = cap.read() # Median filter frame_median = cv2 .medianBlur(frame, 5) # Display the original video and the median filtered video cv2.imshow('Original Video', frame) cv2.imshow('Median Filtered Video', frame_median) # Press the q key to exit the loop if cv2 .waitKey(1) & 0xFF == ord('q'): break # Release the camera and close all windows cap.release() cv2.destroyAllWindows() ```

















Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130790557