[Python opencv computer vision zero basis to actual combat] 9. Fuzzy

1_ bit 2021-01-20 21:06:14
python opencv vision zero basis

One 、 Learning goals

  1. Understand what convolution is
  2. Understand the use and application of fuzzy

If there is any mistake, please point out ~

Two 、 Understand the application of fuzziness

Last one :[python opencv Computer vision zero basis to actual combat ] 8、 ... and 、ROI Flood fill

2.1 Understand what convolution is

In this section , But we don't have much to explain , I have only a little understanding of convolution , I dare not write too much deep content . stay OpenCV In the fuzzy operation of , Fuzziness requires convolution , In this paper, we introduce teacher Jia Zhigang's view on OpenCV The basic convolution of .
Here is an illustration of a basic convolution , The first column is the value of each position in the one-dimensional array . among 111 We call it convolution kernel . Through convolution kernel and one-dimensional array calculation, we will get the value in the blue box at the bottom of the blue . First, let's write down , The blue area is 1, The second value in the blue square is 2, How did you get it ? It's simple , Multiply each number of convolution kernel by the light cyan content , That's it 11,21,15, Then add them up , Divided by the step size of the convolution kernel , That is to say 3. This can be written as (11+21+15)/3 be equal to 2, Remainder is 2, We just take the integral part . The same is true for later calculations , Including two-dimensional data calculation is also based on one-dimensional calculation principle .

2.2 The mean of fuzzy

stay OpenCV In the mean fuzzy use blur function ,blur Functions are generally used to receive 2 Parameters , One is src For the input image , One is ksize Is the convolution kernel size ; The convolution kernel size can give a matrix , As shown in the picture above 111 yes 1 That's ok 3 The convolution kernel of a column , Then it can be written as (1,3). Blur can be denoised , Different blurs have different effects on different noises . Mean blur can generally be used in images with random noise , It can remove the noise very well .

First, let's introduce a picture :

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\2.jpg")
cv2.imshow("img", img)

Subsequent use blur The mean blur function is used to denoise the image :

blur_img=cv2.blur(img,(2,24))# Image denoising 

blur The first argument to the function is img, For the pictures we're going to deal with , The second parameter is (2,24), Means to create a 2 That's ok 24 The convolution kernel of a column . Finally, show the picture and wait , The complete code is as follows :

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\3.jpg")
cv2.imshow("img", img)
blur_img=cv2.blur(img,(2,24))# Image denoising 
cv2.imshow("blur_img", blur_img)
cv2.waitKey (0)# Wait to close 

We can see from the picture , There is a certain amount of noise in the original image , But after the mean value is blurred, it will be much lighter , But there will be some blurring in the picture .

2.3 The median fuzzy

Median ambiguity uses medianBlur function ,medianBlur General reception 2 Parameters , One is the image to be processed , Another is the size of the nucleus , It is stipulated to be greater than 1 The odd number , for example 3、5、7…
Now I have a picture with salt and pepper noise :

Median blur is very effective for noise reduction of this type of image . Since most of the code has been explained , No more details here , Direct paste code :

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\2.jpg")
cv2.imshow("img", img)
cv2.imshow("median_blur_img", median_blur_img)
cv2.waitKey (0)# Wait to close 

The above code uses medianBlur Median fuzzy method , The photos came in , And given the core size value 5, The greater the value , The more fuzzy it is . give the result as follows :

2.4 sharpening

stay OpenCV We can customize the inner check image for convolution , The kernel also has several different standards , Can convolute the image to achieve some specified effect . Custom convolution of the kernel filter2D function . The function prototype is as follows :

cv.filter2D(src, ddepth, kernel)

src For the image to be processed ;ddepth The general usage is -1, It means that it has the same depth as the original image ;kernel Is the convolution kernel , Is a single channel floating point matrix ; Because of our preliminary use, we use the general convolution kernel directly , At this time, you can directly pass in the fixed data , So again, there is no in-depth explanation .

The sharpened convolution kernel is :


Incoming to filter2D The function is :

cv2.filter2D(blur_img, -1, kernel)

Now we're going to use an image that's mean blurred filter2D Function to sharpen , The complete code is as follows :

import cv2
import numpy as np
img = cv2.imread(r"C:\Users\Administrator\Desktop\4.jpg")
cv2.imshow("img", img)
blur_img=cv2.blur(img,(1,6))# Image denoising 
cv2.imshow("blur_img", blur_img)
dst = cv2.filter2D(blur_img, -1, kernel)
cv2.imshow("dst", dst)
cv2.waitKey (0)# Wait to close 

In the above code blur_img, It's the image after average blur , We will blur_img The incoming value filter2D Function uses the specified convolution kernel to sharpen , Finally get dst Image data . give the result as follows :
The mean of fuzzy :

Sharpening :

It can be seen from the results of the picture , After mean ambiguity , Then sharpen it , The compilation of this image will be deepened .

2.4 Relief

matrix :


It's a bit of a ghost animal , Don't laugh. .

2.5 The outline

matrix :


2.6 Laplace operator

matrix :


2.7 The original picture of separation

matrix :


 Insert picture description here
The series was first published in ebaina

3、 ... and 、 summary

  1. Understand the operation of convolution
  2. Learn how to use a variety of fuzziness
  3. Understand the median blur for salt and pepper noise has a good denoising effect
本文为[1_ bit]所创,转载请带上原文链接,感谢

  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