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

Last one :[python opencv Computer vision zero basis to actual combat ] Nine 、 Fuzzy

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
img = cv2.imread(r'C:\Users\mx\Desktop\111.png')
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
img = cv2.imread(r'C:\Users\mx\Desktop\111.png')
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
img = cv2.imread(r'C:\Users\mx\Desktop\111.png')
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[0]
img[row,col,1]=g+srand[1]
img[row,col,2]=r+srand[2]

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
img = cv2.imread(r'C:\Users\mx\Desktop\111.png')
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[0]>255:
img[row,col,0]=255
elif b+srand[0]<0:
img[row,col,0]=0
else:
img[row,col,0]=b+srand[0]
if g+srand[1]>255:
img[row,col,1]=255
elif g+srand[1]<0:
img[row,col,1]=0
else:
img[row,col,1]=g+srand[1]
if r+srand[2]>255:
img[row,col,2]=255
elif r+srand[2]<0:
img[row,col,2]=0
else:
img[row,col,2]=r+srand[2]
#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
版权声明
本文为[1_ bit]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/01/20210120210426688k.html

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database