[OpenCV Practical Combat] 4. OpenCV five filtering practices (mean, box, median, Gaussian, bilateral)

OpenCV five filtering practices (mean, box, median, Gaussian, bilateral)

  • 〇. Coding practical content
  • 1. Filtering, kernel and convolution
    • 1.1 Filtering
    • 1.2 Kernels & Filters
    • 1.3 Formula
    • 1.4 Example
  • 2. Actual combat of picture border filling
    • 2.1 Solve the problem
    • 2.2 Related OpenCV functions
    • 2.3 Code
  • 3. Practical practice of mean filtering
    • 3.1 Theory
    • 3.2 Blur
    • 3.3 Code
  • 4. Box filtering practice
    • 4.1 Theory
    • 4.2 Practical combat: Implementing Sobel filter
  • 5. Median filtering practice
    • 5.1 Concept
    • 5.2 Code
  • 6. Gaussian filtering practice
    • 6.1 Concept
    • 6.2 Code
  • 7. Bilateral filtering practice
    • 7.1 Concept
    • 7.2 Code

〇、Coding practical content

  1. Filtering, Kernels and Convolutions
  2. Picture border filling practice
  3. Practical use of mean filter
  4. Practical use of Box filtering
  5. Practical use of median filter
  6. Practical use of Gaussian filter
  7. Practical use of bilateral filtering

1. Filtering, kernel and convolution

1.1 Filtering

1. Formula: Picture A => Filter => Picture B

2. Popular concepts:
1. A picture is processed into another picture through the filtering process.
2. Use neighborhood information to update its own information at each position on the image.

3. Application scenarios:
1. Image enhancement: denoising, sharpening, blurring
2. Extract information: texture, edge detection, etc.

1.2 Kernel & Filter

1. Filter: The algorithm used in the green wave process is: filter
The filter is an algorithm that calculates a new image I’(x, y) from an image I(x, y) based on the area near the pixel point x, y.

2. Kernel: Color(x,y) => Array[w,h] – Kernel => Color’(x,y)

illustrate:

  1. The kernel is a multidimensional array
  2. There are width and height attributes. If width is equal to height, it is BoxFilter.
  3. The difference between A and B is that B has been normalized. If not normalized, the filtered color value will overflow.

1.3 Formula

For any box of shape, take the value K(i, j) of its (i, j) and offset it with the value I(x + i) of the offset (i, j) relative to the pixel point (x, y) in the original image. , y + j) for dot multiplication. Finally, the sum can be used to obtain the filtered new value I’(x, y) of the original I(x, y).

1.4 Example

(0 * 1/9 + 0 * 1/9 + 0 * 1/9 + 0 * 1/9 + 0 * 1/9 + 0 * 1/9 + 0 * 1/9 + 90 * 1/9 + 90 * 1/9) = 20

2. Picture border filling practice

2.1 Solving problems

  1. The new image [w, h] obtained by passing the original image [w + p, h + q] through the filter [2p + 1, 2q + 1]
  2. Therefore, if you want the output image to have the same size as the original image, you need to add virtual pixels to the original image.

2.2 Related OpenCV functions

/**
The function copies the source image into the middle of the destination image. The areas to the
left, to the right, above and below the copied source image will be filled with extrapolated
pixels. This is not what filtering functions based on it do (they extrapolate pixels on-fly), but
what other more complex functions, including your own, may do to simplify image boundary handling.
*/
CV_EXPORTS_W void copyMakeBorder(InputArray src, OutputArray dst,
                                 int top, int bottom, int left, int right,
                                 int borderType, const Scalar & amp; value = Scalar() );
                     
//Commonly used BorderTypes
enum BorderTypes {<!-- -->
    BORDER_CONSTANT = 0, //!< `iiiiii|abcdefgh|iiiiiii` with some `i`
    BORDER_REPLICATE = 1, //!< `aaaaaa|abcdefgh|hhhhhhh`
    BORDER_REFLECT = 2, //!< `fedcba|abcdefgh|hgfedcb`
    BORDER_WRAP = 3, //!< `cdefgh|abcdefgh|abcdefg`
};

2.3 Code

int main(int argc, char *argv[])
{<!-- -->
      // root Path
    std::string filePath = std::string(__FILE__);
    size_t pos = filePath.find_last_of("/");
    std::string rootPath = filePath.substr(0, pos); // string path = string(__BASE_FILE__) + "/img.webp";
    cout << rootPath;
    Mat image = imread(rootPath + "/img.webp",IMREAD_COLOR);
    
 
    //Fill in the black border, BORDER_CONSTANT
    Mat borderImageBlack;
    copyMakeBorder(image,borderImageBlack,200,200,200,200,BORDER_CONSTANT,Scalar(0,0,0));


    Mat borderReplicate;
    copyMakeBorder(image,borderReplicate,200,200,200,200,BORDER_REPLICATE);


    Mat borderReflect;
    copyMakeBorder(image,borderReflect,400,400,400,400,BORDER_REFLECT);

    Mat borderWrap;
    copyMakeBorder(image,borderWrap,200,200,200,200,BORDER_WRAP);

}

BORDER_CONSTANT

3. Mean filtering practice

3.1 Theory

3.2 Blur

This can be achieved using the blur() method in OpenCV

CV_EXPORTS_W void blur( InputArray src, OutputArray dst,
                        Size ksize, Point anchor = Point(-1,-1),
                        int borderType = BORDER_DEFAULT );

Parameter Description:

  1. src original image
  2. dst target image
  3. ksize: core size
  4. Anchor: The position of the core center point. The default is -1. -1 is at the center of the core. The default value is used in the first version.
  5. borderType: border filling type, just use the default value

3.3 Code

int main(int argc, char *argv[])
{<!-- -->
      // root Path
    std::string filePath = std::string(__FILE__);
    size_t pos = filePath.find_last_of("/");
    std::string rootPath = filePath.substr(0, pos); // string path = string(__BASE_FILE__) + "/img.webp";
    cout << rootPath;
    Mat image = imread(rootPath + "/img_1.jpeg",IMREAD_COLOR);
    
    Mat blurImg;
    blur(image,blurImg,Size(5,5));
    
 namedWindow("originImg");
    imshow("originImg", image);
    waitKey(0);
    destroyWindow("originImg");

 namedWindow("blurImg");
    imshow("blurImg", blurImg);
    waitKey(0);
    destroyWindow("blurImg");
    return 0;

< img src="//i2.wp.com/img-blog.csdnimg.cn/ce26cb539bd8460b80b9566b45a08c25.png?x" width="200">

illustrate:

  1. Mean filtering is to calculate the mean value of the pixels surrounding the image, which is used to blur the image.
  2. The above shows the original image, 33, 55, and 7*7 mean filtering.

4. Box filtering practice

4.1 Theory

  1. Mean filtering requires that the mean values of the boxes add up to 1
  2. Boxed filtering does not have this requirement.

4.2 Practical combat: Implementing Sobel filter

int main(int argc, char *argv[])
{<!-- -->
      // root Path
    std::string filePath = std::string(__FILE__);
    size_t pos = filePath.find_last_of("/");
    std::string rootPath = filePath.substr(0, pos); // string path = string(__BASE_FILE__) + "/img.webp";
    cout << rootPath;
    
    Mat dstImage;
    //
    Mat sobel(3,3,CV_32S);
    sobel.at<int>(0,0) = 1;
    sobel.at<int>(0,1) = 0;
    sobel.at<int>(0,2) = -1;

    sobel.at<int>(1,0) = 2;
    sobel.at<int>(1,1) = 0;
    sobel.at<int>(1,2) = -2;

    sobel.at<int>(2,0) = 1;
    sobel.at<int>(2,1) = 0;
    sobel.at<int>(2,2) = -1;
    
    Mat sobel_horizion(3,3,CV_32S);
    sobel_horizion.at<int>(0,0) = 1;
    sobel_horizion.at<int>(0,1) = 2;
    sobel_horizion.at<int>(0,2) = 1;

    sobel_horizion.at<int>(1,0) = 0;
    sobel_horizion.at<int>(1,1) = 0;
    sobel_horizion.at<int>(1,2) = 0;

    sobel_horizion.at<int>(2,0) = -1;
    sobel_horizion.at<int>(2,1) = -2;
    sobel_horizion.at<int>(2,2) = -1;

    filter2D(image,dstImage,CV_8UC3,sobel);
    namedWindow("sobel"); // Create a window titled "hello"
    imshow("sobel", dstImage); // Display the image in the window "hello"
    waitKey(0); // Wait for the user to press the keyboard
    destroyWindow("sobel"); // Destroy window "hello"
    return 0;
}

5. Median filtering in practice

5.1 Concept

Suitable for denoising, this filter selects the final value in the image corresponding to the kernel

Original image

5.2 Code

 Mat mediaImage;
    medianBlur(image,mediaImage,3);


The above are the effects of median filters with kernels 33, 55, 77, and 99 respectively.

6. Gaussian filtering practice

6.1 Concept

Idea: The farther away the pixel is from (x, y), the lower the correlation with (x, y)

/** @brief Blurs an image using a Gaussian filter.

The function convolves the source image with the specified Gaussian kernel. In-place filtering is
supported.

@param src input image;

@param dst output image of the same size and type as src.

@param ksize Gaussian kernel size. ksize.width and ksize.height can differ but they both must be
positive and odd.

@param sigmaX Gaussian kernel standard deviation in X direction.

@param sigmaY Gaussian kernel standard deviation in Y direction; if
 */
CV_EXPORTS_W void GaussianBlur( InputArray src, OutputArray dst, Size ksize,
                                double sigmaX, double sigmaY = 0,
                                int borderType = BORDER_DEFAULT );

6.2 Code

    Mat gauss;
    GaussianBlur(image,gauss,Size(7,7),2,1.5);

< img src="//i2.wp.com/img-blog.csdnimg.cn/9db324bc789b4ed2943246157b40cbce.png?x" width="200">

7. Bilateral filtering in practice

7.1 Concept

CV_EXPORTS_W void bilateralFilter( InputArray src, OutputArray dst, int d,
                                   double sigmaColor, double sigmaSpace,
                                   int borderType = BORDER_DEFAULT );

Gaussian filter:

  1. When doing blur processing, obvious boundaries will also be blurred, but bilateral filtering will not
  2. Slow down the spatial changes of pixels.

Bilateral filtering:

  1. Edges remain smooth
  2. A weighted average is performed on each pixel and the pixels within its domain.
  3. Instead of spatial distance based on other filters, smoothing is calculated based on color intensity. That is, the sigmaColor parameter.
  4. The main three parameters:
    d: Domain diameter distance, set to -1, it will be automatically calculated based on sigmaColor
    sigmaColor: Similar to Gaussian filter. The larger the finger, the greater the smooth color intensity.
    sigmaSpace: sigma index of the filter in coordinate space

7.2 Code

 Mat bilateral;
    bilateralFilter(image,bilateral,-1,100,11);

There is no obvious difference between before and after treatment, maybe the picture selection is not very consistent.