Today I’m going to learn a little bit about fuzzy. send it out to record.
The fuzzy
Image blur is one of the more common and simple processing methods in image processing. Then the reason for using this method is to denoise the image for subsequent processing. The principle behind obfuscation is actually convolution computation. # Simply, take a small square and slide it over the image square so that we can get a new image. There are many ways to blur, depending on the principle. # (baidu YYds)Copy the code
The mean of fuzzy
Mean filtering literally means taking the average, that is, the coefficients on the yellow square are all1, multiplied by the value of the pixel covered by the yellow form and then removed by9(Convolution and magnitude are3*3), get the average value and assign it to # center pixel. # Its disadvantage is that it can not retain image details well while denoising, because all mean values are replaced.Copy the code
def blur_demo(image):# mean fuzzy DST= cv.blur(image,(100.300CV. Imshow ()# CV. Imshow ()" blur_demo",dst)
Copy the code
The median fuzzy
# Take the middle value instead of the center pixel. It's similar to mean filtering, except the mean filtering takes the average, the median filtering takes the middle value. The median filter can effectively remove salt-and-pepper noise. Corresponding to the image, on a black image, there are many small white dots, these are the salt and pepper noise. # (Suddenly craving a barbecue...)Copy the code
def median_blur_demo(image): # median fuzzy det= cv.medianBlur(image,5)
cv.imshow("Median_Blur",det)
Copy the code
Custom ambiguity
Suitable for image enhancement and sharpening
def custom_demo(image)# define fuzzy kernel= np.ones([5.5], np.float32)/25# Ensure that the value does not overflow# kernel = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], np.float32)/9
# the kernel = np. Array ([[0, 1, 0], [1, 5, 1], [0, 1, 0]], np. Float32) # a sharpening effect this parameter
dst = cv.filter2D(image, - 1, kernel=kernel) #- 1CV. Imshow ("custom_demo", dst)
Copy the code
Gaussian blur
# Gaussian filter is a linear smoothing filter, suitable for eliminating gaussian noise, widely used in image processing noise reduction process. Generally speaking, filtering Gaussian is the process of weighted average of the whole image. The value of each pixel is obtained by weighted average of its own and other pixel values in the neighborhood. Compared with mean filtering, image details can be retained better. Because of the nature of the Gaussian function, there is a certain ratio of weights. #** Manually add Gaussian noise **Copy the code
def gaussian_noise(image): # Gaussian noise H, W, C= image.shape # image.shape[0], image vertical size; Image.shape [1], image horizontal size; Image.shape [2], number of image channels
for row in range(h):
for col in range(w):
s = np.random.normal(0.20.3) # Produces a normal distribution' ' '
numpy.random.normal(loc=0,scale=1.0, size = parameter loc (shape)float) : The mean of the normal distribution corresponds to the center of the distribution. loc=0It indicates that the normal distribution parameter Scale (float) : Standard deviation of the normal distribution, corresponding to the width of the distribution, the larger the scale, the fatter the normal distribution curve, the smaller the scale, the higher the curve.intOr integer tuples) : Output shape, default to None, set here to3, represents the output3A value' ' '
b = image[row, col, 0] # blue; Row height, COL width, 0 indicates channel 0, blue
g = image[row, col, 1] # green; 1 indicates channel 1. Green
r = image[row, col, 2] # red
image[row, col, 0] = clamp(b + s[0] # in the channel0Add a gaussian noise image[row, col,1] = clamp(g + s[1] # in the channel1Add a gaussian noise image[row, col,2] = clamp(r + s[2] # in the channel2Add a gaussian noise CV. Imshow ("gaussian_noise image", image)
def clamp(pv):
if pv >255:
return 255
if pv <255:
return 0
else :
return pv**
Copy the code
Opencv Gaussian blur
Gaussian_noise (SRC) # cv.GaussianBlur(SRC, (5.5), 0) # Gaussian blur API for OpencV
' ' 'def GaussianBlur(src: Any, ksize: Any, sigmaX: Any, dst: Any = None, sigmaY: Any = None, borderType: Any = None) SRC: Enter the image, can be Any number of channels, this function is independent of the number of channels ksize: Gaussian kernel size. Width and ksize. Height can be different but they must be positive odd numbers. Or equal to0Is determined by the parameter sigma. SigmaX: Standard deviation of the Gaussian kernel in the X direction. SigmaY: The standard deviation of the Gaussian kernel in the Y direction. If sigmaY for0, it will have the same value as sigmaX if they are both0, then they are calculated from ksize. Width and ksize. Height. BorderType: The pattern used to determine image boundaries.' ' '
Copy the code
The Ground-Glass Effect
Bilateral fuzzy
# Bilateral filtering is a nonlinear filtering method. It is a compromise processing combining spatial proximity and similarity of pixel values of images, and at the same time considering spatial information and similarity of gray scale, so as to achieve the purpose of edge preservation and denoising. # Bilateral filtering avoids the loss of edge information and keeps the contour of the image unchanged.Copy the code
def bi_demo(image): # double blur DST= cv.bilateralFilter(image, 0.100.15)
' ' '
def bilateralFilter(SRC: Any, # Original image data D: Any, # represents the diameter range of each pixel neighborhood in the filtering process. If the value is non-positive, the function computes it from the fifth argument, sigmaSpace. SigmaColor: Any, # The sigma value of the color space filter. The larger the value of this parameter, the wider the range of colors in the pixel neighborhood will be mixed together, resulting in a larger semi-equal color region. SigmaSpace: Any, # The sigma value of the filter in the coordinate space. If this value is large, it means that the more distant pixels will influence each other so that similar colors in a larger area are sufficient to obtain the same color. When d >0, d specifies the neighborhood size and is independent of sigmaSpace, otherwise d is proportional to sigmaSpace DST: Any = None, ## some boundary mode "" CV for inferring external pixels of the image.imshow("bi_demo image", dst)
Copy the code
(Baidu Zhendixiang)