opencv filtering technology


Preface

In OpenCV, there are various filtering techniques that can be used for image processing and image enhancement. Below I will introduce five common filtering techniques, including mean filtering, median filtering, Gaussian filtering, bilateral filtering and adaptive filtering, and provide corresponding functions and usage methods.

1. Mean filtering

Mean Filtering:
Mean filtering is the simplest filtering method. It uses the average value of pixels in the neighborhood to replace the value of the central pixel to achieve image smoothing.

cv::blur(inputImage, outputImage, cv::Size(ksize, ksize));

inputImage: input image
outputImage: output image
ksize: size of the filter, such as 3x3 or 5x5.

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat srcImage = cv::imread("input.jpg", cv::IMREAD_COLOR);
    
    // 均值滤波
    cv::Mat result;
    cv::blur(srcImage, result, cv::Size(3, 3));
    
    cv::imshow("Result", result);
    cv::waitKey(0);
    
    return 0;
}

2. Median filtering

Median Filtering:
Median filtering replaces the value of the center pixel by calculating the median of pixels in the neighborhood, thereby effectively removing outliers such as salt and pepper noise.

cv::medianBlur(inputImage, outputImage, ksize);

inputImage: input image
outputImage: output image
ksize: the size of the filter, which must be a positive odd number, such as 3, 5, 7.

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat srcImage = cv::imread("input.jpg", cv::IMREAD_COLOR);
    
    // 中值滤波
    cv::Mat result;
    cv::medianBlur(srcImage, result, 3);
    
    cv::imshow("Result", result);
    cv::waitKey(0);
    
    return 0;
}

3. Gaussian filter

Gaussian Filtering:
Gaussian filtering is a linear smoothing filtering method that calculates new pixel values ​​based on the weighted average of the distance between the pixel value and its neighbor pixel values. It preserves the edge details of the image while smoothing it.

cv::GaussianBlur(inputImage, outputImage, cv::Size(ksize, ksize), sigmaX, sigmaY);

inputImage: input image
outputImage: output image
ksize: the size of the filter, which must be a positive odd number, such as 3, 5, 7.
sigmaX and sigmaY: standard deviation in the X and Y directions, controlling the shape of the filter.

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat srcImage = cv::imread("input.jpg", cv::IMREAD_COLOR);
    
    // 高斯滤波
    cv::Mat result;
    cv::GaussianBlur(srcImage, result, cv::Size(3, 3), 0);
    
    cv::imshow("Result", result);
    cv::waitKey(0);
    
    return 0;
}

4. Bilateral filtering

Bilateral Filtering:
Bilateral filtering is a nonlinear filtering method that preserves edge information while smoothing the image. It not only considers similarity in spatial distance, but also considers pixel value similarity.

cv::bilateralFilter(inputImage, outputImage, d, sigmaColor, sigmaSpace);

inputImage: input image
outputImage: output image
d: neighborhood diameter of the bilateral filter, usually 5.
sigmaColor: The standard deviation of the pixel value space, used to control the color similarity measure.
sigmaSpace: The standard deviation of spatial distance, used to control the spatial similarity measure.

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat srcImage = cv::imread("input.jpg", cv::IMREAD_COLOR);
    
    // 双边滤波
    cv::Mat result;
    cv::bilateralFilter(srcImage, result, 9, 75, 75);
    
    cv::imshow("Result", result);
    cv::waitKey(0);
    
    return 0;
}

5. Adaptive filtering

Adaptive Filtering:
Adaptive filtering dynamically adjusts the weight of the filter based on the statistical information of neighborhood pixels to adapt to the image characteristics of different areas.

cv::adaptiveBilateralFilter(inputImage, outputImage, cv::Size(ksize, ksize), sigmaSpace);

inputImage: input image
outputImage: output image
ksize: filter size, must be a positive odd number.
sigmaSpace: The standard deviation of the spatial distance, used to control the adaptability of the filter.

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat srcImage = cv::imread("input.jpg", cv::IMREAD_COLOR);
    
    // 自适应滤波
    cv::Mat result;
    cv::adaptiveBilateralFilter(srcImage, result, cv::Size(3, 3), 75);
    
    cv::imshow("Result", result);
    cv::waitKey(0);
    
    return 0;
}

6. Filter size

Filter size refers to the size of the filter or window size, which is a parameter applied to a local area on the image for filtering operations. The choice of filter size has an important impact on filtering effect and computing performance.

滤波器大小的作用有以下几个方面:

1. Smoothness: A larger filter size produces a smoother image because it involves a wider area of ​​pixels. As the filter size increases, the smoothing effect increases and details and noise in the image are blurred or smoothed out more. Therefore, if a stronger smoothing effect is required, a larger filter size can be selected.

2. Edge preservation: Smaller filter size can better preserve the edge information of the image. This is because smaller filters only involve localized areas of pixels and are better able to capture details and edges. If you wish to preserve edge information while smoothing the image, you can choose a smaller filter size.

3. Computational complexity: Larger filter sizes require more pixels to be processed, so the computational complexity increases. In real-time applications or situations with high computational resource requirements, choosing a smaller filter size can reduce computational costs.

Summarize

This article mainly explains the filtering technology in opencv. You can try to use filtering technology to process images.

おすすめ

転載: blog.csdn.net/m0_49476241/article/details/133062836