opencv learning eight: Gaussian blur and edge preservation filtering EPF

1. Gaussian Blur

1.1 numpy implements Gaussian blur

code show as below:

import cv2 as cv
import numpy as np

#截断函数
def clamp(pv):
    if pv > 255:
        return 255
    else:
        return pv

#高斯滤波 
def gaussian_noise(image):
    h, w, c = image.shape
    for row in range(h):
        for col in range(w):
            s = np.random.normal(0, 20, 3)#0是矩阵中所有随机数的平均值,20是标准差,3是形状(1行3列)
            b = image[row, col, 0] #blue
            g = image[row, col, 1]  # green
            r = image[row, col, 2]  # red
            image[row, col, 0] = clamp(b + s[0])
            image[row, col, 1] = clamp(g + s[0])
            image[row, col, 2] = clamp(r + s[0])
    cv.imshow("gaussian_noise", image)


src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/quzao.jpg")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
t1 = cv.getTickCount()
gaussian_noise(src)
t2 = cv.getTickCount()
time = (t2 - t1)/cv.getTickFrequency()
print(time)
cv.waitKey(0)
cv.destroyAllWindows()

The implementation time of numpy is slow, about 5,6 seconds.
Run screenshot:
Insert picture description herenp.random.normal() normal distribution
Gaussian distribution probability density function
Insert picture description here

The
meaning of the numpy.random.normal(loc=0.0, scale=1.0, size=None)

parameter in numpy is:
  loc: the mean value of the float probability distribution, corresponding to the center of the entire distribution center
  scale: the standard deviation of the float probability distribution, corresponding For the width of the distribution, the larger the scale, theshorter and fatter, the smaller the scale, thethinner and tallerthe shape outputted by
  size:int or tuple of ints
, the default is None, and only one value is output.
We often use np.random.randn(size ) The so-called standard normal distribution (μ=0, σ=1) corresponds to np.random.normal(loc=0, scale=1, size)

1.2 Opencv realizes Gaussian blur

Implementation method: GaussianBlur
processing result = cv2.GaussianBlur (original image src, kernel function size ksize, sigmaX)

Kernel function size ksize: (N, N) must be an odd number
sigmaX: control the variance in the x direction, control the weight, generally take 0, it calculates the variance by itself. The y-axis variance and the x-consistent
Insert picture description herecode are as follows:

import cv2 as cv

src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/quzao.jpg")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
dst = cv.GaussianBlur(src, (5, 5), 0)
cv.imshow("GaussianBlur", dst)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description heredst = cv.GaussianBlur(src, (0, 0), 15)

import cv2 as cv

src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/quzao.jpg")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
dst = cv.GaussianBlur(src, (5, 5), 0)
cv.imshow("GaussianBlur", dst)
cv.waitKey(0)
cv.destroyAllWindows()

Running screenshot: In
Insert picture description herethis case, we call it "frosted glass", its outline is still there, but it is very fuzzy, because it considers the weight of each pixel, its current weight is the largest and has not been averaged. It also retains the basic characteristics of the image, which is better than the mean blur. (The mean value is fuzzy means back pot)

2. Edge preservation filtering EPF

It was introduced that both the mean and the Gaussian belong to fuzzy convolution. They all have a common feature that the edge information of the image no longer exists after the blur and is destroyed. There is a filtering method that has the ability to achieve image blurring through convolution processing without causing damage to the edge of the image. The output after filtering completely preserves the overall edge (contour) information of the image. This type of convolution filtering algorithm is called edge preservation. Filtering algorithm (EPF).
Realization method: Gaussian bilateral, mean shift.
Use scenario: Make image filter.

Insert picture description hereExplain the principle in vernacular: First, perform Gaussian filtering on the left side (black and white image). The
Gaussian filter is as shown in the figure below. The value range can be called the spatial domain.
Insert picture description here
If the current point and the surrounding pixel values ​​differ greatly as shown in the figure below, the range can be called the value domain
Insert picture description here
. Truncated. Get the
Insert picture description here
picture on the right (Black Pakse).
Gaussian filtering parameters are artificially added, and the pixel difference is cut off artificially.

2.1 Gaussian bilateral fuzzy principle

Gaussian filtering (spatial proximity) is to place a two-dimensional Gaussian normal distribution on the image matrix for convolution operation. What is considered is the spatial distance relationship of pixel values ​​in the neighborhood. Within the kernel size range, the corresponding weights are calculated from the spatial proximity of each point to the center point, and the calculated kernel is convolved with the image matrix. Finally, the image is filtered to achieve a smooth effect, and the edges on the image will also have a certain degree of smoothness, which makes the entire image blurred and the edges cannot be saved.
The basic idea of ​​bilateral filtering : optimize each weight calculated from the spatial proximity of each point to the center point in Gaussian filtering (spatial proximity), and optimize it into the weight of spatial proximity calculation and the weight of pixel value similarity calculation The product of values, and the optimized weights are convolved with the image. So as to achieve the effect of edge preservation and denoising.

Gaussian bilateral blur
dst=cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]])
src: input image
d: diameter of each pixel area around it
during filtering sigmaColor: filter sigma in the color space. The larger the parameter, the further away the neighboring pixels will be mixed.
sigmaSpace: filter sigma in coordinate space. The larger the parameter, the greater the influence of colors that are sufficiently similar.
The two variance sigma of the value range and the airspace can be simply set equal, less than 10, without much effect, more than 150, the effect is too strong, like a cartoon.
Explain
filter size d: the slower greater than 5 (5 forreal-time) d is a pixel neighborhood "diameter."
If the Sigma_color is larger, the pixels with larger pixel values ​​in the neighborhood will also be averaged.
Larger Sigma_space means farther away, but as long as the values ​​are similar, they will affect each other.
Set sigma_sapce to a larger value and sigma_color to a smaller value to obtain better results (salt and pepper noise).
The inner idea of ​​bilateral filtering is to do the work done in the domain of the traditional filter in the range of the image. Spatial filtering is a weighted average of neighboring points in space, and the weighting coefficient decreases with the increase of distance; value domain filtering is a weighted average of points with similar pixel values, and the weighting coefficient decreases as the value difference increases. At the same time, consider the significance of filtering the spatial domain and the value domain: limit the smoothing range of the image to the smaller part of the value domain (that is, the part with small luminosity/color difference), so that the purpose of edge preservation can be achieved . (The text part is borrowed from others)

import cv2 as cv
import numpy as np

#高斯双边 更接近于磨皮效果
def bi_demo(image):
    dst = cv.bilateralFilter(image, 0, 100, 15)
    cv.imshow("bi_demo", dst)

src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/quzao.jpg")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
bi_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here

2.2 Mean shift

The meanShfit mean shift algorithm is a general clustering algorithm. Its basic principle is: for a given number of samples, choose one of the samples, delimit a circular area with the sample as the center point, and find the circle The centroid of the sample in the shape area, that is, the point where the density is the highest, and then continue to perform the above iterative process with this point as the center, until the final convergence.
dst = cv2.pyrMeanShiftFiltering(image, sp, sr, maxLevel, termcrit)
sp: the radius of the drifting physical space;
sr: the radius of the drifting color space;
maxLevel: defines the maximum number of layers of the pyramid
termcrit: the termination condition of the drift iteration, which can be set as The number of iterations meets termination, the deviation between the iteration target and the center point meets termination, or a combination of the two

step:

  1. Iterative space construction:
    taking any point P0 on the src on the input image as the center of the circle, establish a spherical space with a radius of sp in the physical space and a radius of sr in the color space, 2 coordinates in the physical space—x, y, and 3 coordinates in the color space —R, G, B (or HSV), forming a 5-dimensional space sphere.
    The range x and y of the physical space are the length and width of the image, and the range R, G, and B of the color space are 0~255 respectively.
  2. Find the vector of the iterative space and move the sphere of the iterative space and recalculate the vector until convergence:
    in the spherical space constructed in 1, after finding the sum of the color vectors of all points relative to the center point, move the center point of the iterative space to The end of the vector, and calculate the vector sum of all points in the spherical space again, and iterate until the end of the vector sum obtained in the last space sphere is the center point Pn of the space sphere, and the iteration ends.
  3. The color value of the initial origin P0 corresponding to the updated output image dst is the color value of the end point Pn of the current iteration, thus completing the color average shift of one point.
  4. For other points on the input image src, perform steps 1, 2, 2, and 3 in sequence. After traversing all the points, the entire mean shift color filter is completed.
import cv2 as cv
import numpy as np

#均值迁移 可能会过度模糊,有种油画的效果
def shift_demo(image):
    dst = cv.pyrMeanShiftFiltering(image, 10, 50)
    cv.imshow("bi_demo", dst)

src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/quzao.jpg")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
shift_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44145452/article/details/112465769