## 10. Picture effect ground glass

1_ bit 2021-01-20 21:06:04
picture effect ground glass

# One 、 Learning goals

1. Learn how to use Gaussian blur
2. Learn about the picture effect of ground glass
3. Learn how to make a noisy picture yourself

If there is any mistake, please point out ~

# Two 、 Understand fuzziness and beauty

## 2.1 Noise reduction using Gaussian Blur

Because many small partner reaction, put aside the principle or theory to explain the use of usage for beginners will be very comfortable , From the beginning of this section will be explained in a relatively simple way API Application .

First of all, we have a picture ： There's a lot of noise in this picture , How to reduce the noise of the modified image ？ Students who have learned the previous sections may know a lot about noise reduction . Maybe some students will say mean fuzzy and median fuzzy . Let's try median ambiguity first ：
Get pictures first ：

``````import cv2
dst=cv2.medianBlur(img,5)
``````

And then median ambiguity , And wait for ：

``````cv2.imshow("img", dst)
cv2.waitKey (0)
cv2.destroyAllWindows()
``````

give the result as follows ： It can be seen from the results that , The noise reduction effect is really good , But there's a certain amount of paste . Now let's try mean ambiguity again . Change the median fuzzy code to ：

``````dst=cv2.blur(img,(2,24))
``````

give the result as follows ： It feels even worse , Now let's test the water with our Gauss blur . Gaussian blur uses API GaussianBlur, Gaussian blur uses the weighted average method for the radius 、 The scope is blurred .
GaussianBlur The prototype of the method is as follows ：

``````cv2.GaussianBlur（ SRC,ksize,sigmaX ）
``````

We can pass the following parameters in the current code ：

``````dst=cv2.GaussianBlur(img,(5,5),0)
``````

among img It's an image to blur ,(5,5) It's the size of the Gaussian kernel , Generally, the core size is odd , The last one is the standard deviation , Let's take 0 that will do .
If I (5,5) It's written in (1,1) The original image is not manipulated . The larger the kernel, the more blurred the image is . The appropriate matching value can make the image blur not too blurred . If filtering is needed, when the size of the image is equal , So average (5,5) Both values of are the same , In fact, it can be seen as a scale . Let's add the original image and compare it with the effect of Gaussian blur , The complete code is as follows ：

``````import cv2
dst=cv2.GaussianBlur(img,(5,5),0)
cv2.namedWindow("Image",cv2.WINDOW_NORMAL)
cv2.imshow("Image", img)
cv2.imshow("Gaussian", dst)
cv2.waitKey (0)
cv2.destroyAllWindows()
``````

give the result as follows ： From the picture, Gaussian blur has little effect on the whole contour . In fact, Gaussian blur should be very familiar to us , Gaussian blur is used in some image processing software to add ground glass effect to the image . We just need to set the core size to zero , Then change the variance value , This is the ground glass effect . The code is as follows ：

``````dst=cv2.GaussianBlur(img,(0,0),20)
`````` ## 2.2 Write your own noisy pictures

Actually 2.1 The noise image in the video is edited by myself , Then how to write a similar noisy image ？ It's very simple. Just go through the size of the image . Traverse the size of the image to increase the random value of the pixel . How to traverse images ？ Just get the width and height of the image , Just traverse the picture .
First of all, we read the image and get the width and height of the image ：

``````import cv2
h,w,c=img.shape
``````

Then traverse each pixel ：

``````for row in range(h):
for col in range(w):
b=img[row,col,0]
g=img[row,col,1]
r=img[row,col,2]
``````

Traversing high elements in the outer loop above , Then the inner loop iterates through the width elements of each high element ; Get in the width element 3 Values for channels . Some partners may say , Why acquire 3 The values of channels do not use loops ？ That's because using loops can increase complexity , It makes the program run more slowly , So to reduce the complexity , Here I use more “ stupid ” Read directly from 3 Values for channels .
And then randomly generate a 3 Bit random number , Add pixels to the picture ：

``````srand=np.random.normal(0,30,3)
img[row,col,0]=b+srand
img[row,col,1]=g+srand
img[row,col,2]=r+srand
``````

give the result as follows ： Why ？ Then why is it like this ？ Don't worry , Let's use Gaussian blur for noise reduction ：

``````dst=cv2.GaussianBlur(img,(5,5),0)
``````

give the result as follows ： Why ？ I think the noise reduction effect is pretty good , At this time, we should understand ,“ Gaussian blur uses the weighted average method for the radius 、 The scope is blurred ”, We can clearly see from the result graph that the noise gets a certain degree around the color value “ Restore ”.

Okay , Now let's explore why some of the random values in this graph will be white . That's because we didn't do “ overflow ” Calculation ; When we calculate the increase or decrease of the value, we should consider whether the value exceeds 255 Or is it less than 0, If less than 0 Then put 0, If more than 255 It is equal to 255. Because if we add a value , Less than 0, So it means that this value is dark in the province , Put... Directly 0 The bias has not changed ; If more than 255 It means light , Directly equal to 255 It doesn't change the color bias , So there's no problem doing this , The basic appearance of the original picture is retained to a great extent . The complete code at this time is as follows ：

``````import cv2
import numpy as np
h,w,c=img.shape
for row in range(h):
for col in range(w):
srand=np.random.normal(0,30,3)
b=img[row,col,0]
g=img[row,col,1]
r=img[row,col,2]
if b+srand>255:
img[row,col,0]=255
elif b+srand<0:
img[row,col,0]=0
else:
img[row,col,0]=b+srand
if g+srand>255:
img[row,col,1]=255
elif g+srand<0:
img[row,col,1]=0
else:
img[row,col,1]=g+srand
if r+srand>255:
img[row,col,2]=255
elif r+srand<0:
img[row,col,2]=0
else:
img[row,col,2]=r+srand
#dst=cv2.blur(img,(2,24))
dst=cv2.GaussianBlur(img,(0,0),20)
cv2.namedWindow("Image",cv2.WINDOW_NORMAL)
cv2.imshow("Image", img)
cv2.imshow("Gaussian", dst)
cv2.waitKey (0)
cv2.destroyAllWindows()
``````

give the result as follows ： The series was first published in ebaina

# 3、 ... and 、 summary

1. Understand the precautions of making noise pictures , The light and dark values need to be given equal value
2. Understand the use of Gaussian blur and the characteristics of contour preservation

https://pythonmana.com/2021/01/20210120210426688k.html