Adaptive filtering algorithms and routines

The adaptive filtering algorithm is a method that automatically adjusts filters according to the local characteristics of the image to achieve better image enhancement effects. This algorithm is able to dynamically select filter parameters based on the characteristics of different regions of the image.

Common adaptive filtering algorithms include the following:

  1. Adaptive Mean Filtering: This algorithm performs filtering based on the mean value of pixels in a local area. For each pixel, the mean of pixels in a fixed-size neighborhood around it is calculated and this mean is used to replace the original pixel value.

  2. Adaptive Median Filtering: This algorithm first sorts the pixels in a local area, and then determines the filter size based on the pixel sorting results. For each pixel, the maximum and minimum grayscale values ​​are calculated based on the sorted pixel values. If the current pixel is between the maximum and minimum values, it is judged to be a noise pixel, and the median value of the area is used instead.

  3. Bilateral Filtering: This algorithm considers the difference between the spatial distance of pixels and the grayscale of pixels during the filtering process. It uses weighted average processing to retain edge information while suppressing noise.

These algorithms can be used in different image enhancement tasks, such as image denoising, edge preservation, and detail enhancement. Choosing a suitable adaptive filtering algorithm depends on the specific application scenario and requirements.

In practical applications, image processing libraries (such as OpenCV) can be used to implement these adaptive filtering algorithms, and parameters can be adjusted according to specific situations to achieve the best results.

1. The following is a routine of a common adaptive filtering algorithm - Adaptive Mean Filtering:

import cv2

def adaptive_mean_filter(image, block_size, c):
    # 将图像转换为灰度图像
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # 应用自适应均值滤波
    result = cv2.adaptiveThreshold(gray,
                                   255,
                                   cv2.ADAPTIVE_THRESH_MEAN_C,
                                   cv2.THRESH_BINARY,
                                   block_size,
                                   c)
    
    return result

# 读取图像
image = cv2.imread('input.jpg')

# 进行自适应均值滤波
filtered_image = adaptive_mean_filter(image, 11, 2)

# 显示原始图像和滤波后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Filtered Image', filtered_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we have defined a function named adaptive_mean_filter which accepts an image, block size and constant c as parameters and Returns the image processed by adaptive mean filtering.

Inside the function, we first convert the color image to grayscale. Then, apply adaptive mean filtering using the cv2.adaptiveThreshold function. Among them, cv2.ADAPTIVE_THRESH_MEAN_C means using the mean as the threshold calculation method, cv2.THRESH_BINARY means setting the pixel value greater than the threshold to 255, and setting the pixel value less than the threshold to 0. block_size represents the filtering area size, c is a constant used to adjust the threshold.

It should be noted that the adaptive filtering algorithm can be modified and optimized according to different application scenarios. For example, another common adaptive filtering algorithm is Adaptive Median Filtering, which can better handle salt and pepper noise in images. You can choose to use an appropriate adaptive filtering algorithm based on your specific needs.

2. The adaptive median filtering algorithm is an adaptive filtering method used for image denoising. It can effectively process images containing salt and pepper noise. Here is a routine to implement adaptive median filtering using Python and OpenCV:

import cv2
import numpy as np

def adaptive_median_filter(image, max_filter_size):
    # 创建与输入图像相同大小的输出图像
    filtered_image = np.copy(image)
    
    # 获取图像的高度和宽度
    height, width = image.shape[:2]
    
    # 对每个像素进行遍历
    for i in range(height):
        for j in range(width):
            output_pixel = apply_adaptive_median(image, i, j, 3, max_filter_size)
            filtered_image[i, j] = output_pixel
    
    return filtered_image

def apply_adaptive_median(image, center_i, center_j, filter_size, max_filter_size):
    # 获取图像的高度和宽度
    height, width = image.shape[:2]
    
    # 计算过滤器中心像素的边界
    border = (filter_size - 1) // 2
    
    # 获取过滤器中心像素的灰度值
    center_pixel = image[center_i, center_j]
    
    # 获取过滤器中心像素的邻域
    neighborhood = get_neighborhood(image, center_i, center_j, border)
    
    # 计算邻域中像素值的最小值和最大值
    min_pixel = np.min(neighborhood)
    max_pixel = np.max(neighborhood)
    
    # 计算邻域中像素值的中值和中值偏差
    median_pixel = np.median(neighborhood)
    median_deviation = median_pixel - center_pixel
    
    # 判断当前滤波器是否能处理噪声
    if min_pixel < center_pixel < max_pixel:
        # 若可以,则返回中值
        return center_pixel
    elif filter_size < max_filter_size:
        # 若滤波器大小小于最大滤波器大小,则扩大滤波器大小并递归应用自适应中值滤波
        return apply_adaptive_median(image, center_i, center_j, filter_size + 2, max_filter_size)
    else:
        # 若无法处理噪声,则返回中值加上中值偏差
        return center_pixel + median_deviation

def get_neighborhood(image, center_i, center_j, border):
    # 获取图像的高度和宽度
    height, width = image.shape[:2]
    
    # 计算邻域的边界
    top = max(center_i - border, 0)
    bottom = min(center_i + border, height - 1)
    left = max(center_j - border, 0)
    right = min(center_j + border, width - 1)
    
    # 提取邻域矩阵
    neighborhood = image[top:bottom+1, left:right+1]
    
    return neighborhood

# 读取图像
image = cv2.imread('input.jpg', 0)  # 以灰度图像方式读取

# 进行自适应中值滤波
filtered_image = adaptive_median_filter(image, 3)

# 显示原始图像和滤波后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Filtered Image', filtered_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we defined a function namedadaptive_median_filter which accepts a grayscale image and the maximum filter size as parameters and returns the adaptive median filtered Processed image.

Inside the function, we iterate through each pixel of the image and apply theapply_adaptive_median function to each pixel to calculate the output pixel value. apply_adaptive_medianThe function first obtains the information of the center pixel and its neighbors, and then compares the gray value of the center pixel with the minimum and maximum values ​​of the neighborhood pixels to determine whether the noise can be processed. If it is possible, the median value is returned; if it is not possible, it is decided whether to expand the filter size and apply adaptive median filtering recursively based on whether the filter size is smaller than the maximum filter size; if the noise cannot be processed, the median value plus Median deviation.

Finally, we read the input image and call the adaptive_median_filter function to perform adaptive median filtering and display the original image and the filtered image.

おすすめ

転載: blog.csdn.net/wangjiaweiwei/article/details/131669368