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. A series of problems and solutions in Java calling Python
  2. python自动化爬取淘宝商品数据导入execl表格
  3. Using Python to automatically punch in the pin / enterprise wechat
  4. 【分享】python+requests接口测试基础
  5. Python automatically crawls Taobao product data and imports it into excel table
  6. C++/Python描述 628. 三个数的最大乘积
  7. Python的容器有哪些?分别有什么作用?
  8. python+requests接口测试基础
  9. 20 行代码:Serverless 架构下用 Python 轻松搞定图像分类和预测
  10. python+requests接口测试基础
  11. [share] Python + requests interface test foundation
  12. C + + / Python description 628. Maximum product of three numbers
  13. What are Python containers and what are their functions?
  14. Testing foundation of Python + requests interface
  15. 20 lines of code: easy to do image classification and prediction with Python under serverless architecture
  16. Python爬取优质高清壁纸网站:彼岸
  17. Testing foundation of Python + requests interface
  18. 【人生苦短,我学 Python】基础篇——列表(Day8)
  19. Python crawls high quality HD Wallpaper website: the other side
  20. Python图像增强与特效-利用百度AI进行黑白图像上色
  21. 【七天搞定Python】day01.Python环境配置、pip、IDE、注释、变量,数据类型、标识符/关键字、输出、输入
  22. Life is short, I learn Python
  23. Python image enhancement and special effects - using Baidu AI to color black and white images
  24. Python environment configuration, Pip, IDE, comment, variable, data type, identifier / keyword, output, input
  25. 为什么说Python是最伟大的语言?看图就知道了 - 知乎
  26. Why is Python the greatest language? Just look at the picture. - Zhihu
  27. 通过创建视频游戏来学习 Python
  28. Learn Python by creating video games
  29. Python3版本下创建计算给定日期范围内工作日方法
  30. Creating a method to calculate working days within a given date range in Python 3
  31. 图解爬虫,用几个最简单的例子带你入门Python爬虫
  32. Graphical crawler, with a few of the simplest examples to take you to the introduction of Python crawler
  33. python+requests基础知识
  34. Basic knowledge of Python + requests
  35. python自定义windowsr日志支持文件分割
  36. python+requests基础知识
  37. Python custom Windowsr log supports file segmentation
  38. Basic knowledge of Python + requests
  39. 高级测试 | Python笔试题
  40. 火了!开源的 Python 抢票神器,过年回家就看这一波了!
  41. Python 爬虫进阶 - 前后端分离有什么了不起,过程超详细!
  42. 【python】使用pip提示ModuleNotFoundError
  43. 【python】虚拟环境搭建
  44. Advanced test | Python written test questions
  45. Fire! Open source Python ticket grabbing artifact, come home to see this wave of New Year!
  46. Python crawler advanced - before and after the end of the separation of what great, super detailed process!
  47. [Python] prompt modulenotfounderror with PIP
  48. Building a virtual environment
  49. Serverless 架构下用 Python 轻松搞定图像分类和预测
  50. Easy image classification and prediction with Python under serverless architecture
  51. python协程爬取某网站的老赖数据
  52. Python coroutine crawls Laolai data of a website
  53. 使用Python分析姿态估计数据集COCO的教程
  54. Using Python to analyze the data set coco of attitude estimation
  55. win环境 python3 flask 上手整理 环境搭建(一)
  56. Getting started with win environment python3 flash
  57. Python实现一个论文下载器,赶紧收藏
  58. win环境 python3 flask 上手整理 快速上手-基础操作(二)
  59. Python 中常见的配置文件写法
  60. Python to achieve a paper Downloader, quickly collect