## [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

## 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
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
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
cv2.destroyAllWindows()#destroy
``````

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
cv2.imshow("img", img)
median_blur_img=cv2.medianBlur(img,5)
cv2.imshow("median_blur_img", median_blur_img)
cv2.waitKey (0)# Wait to close
cv2.destroyAllWindows()#destroy
``````

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 ：

``````kernel=np.array([[0,-1,0],
[-1,5,-1],
[0,-1,0]],np.float32)
``````

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
cv2.imshow("img", img)
blur_img=cv2.blur(img,(1,6))# Image denoising
cv2.imshow("blur_img", blur_img)
kernel=np.array([[0,-1,0],[-1,5,-1],[0,-1,0]],np.float32)
dst = cv2.filter2D(blur_img, -1, kernel)
cv2.imshow("dst", dst)
cv2.waitKey (0)# Wait to close
cv2.destroyAllWindows()#destroy
``````

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 ：

``````kernel=np.array([[-2,-1,0],
[-1,1,1],
[0,1,2]],np.float32)
``````

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

## 2.5 The outline

matrix ：

``````kernel=np.array([[-1,-1,-1],
[-1,8,-1],
[-1,-1,-1]],np.float32)
``````

## 2.6 Laplace operator

matrix ：

``````kernel=np.array([[0,1,0],
[1,-4,1],
[0,1,0]],np.float32)
``````

## 2.7 The original picture of separation

matrix ：

``````kernel=np.array([[0,0,0],
[0,1,0],
[0,0,0]],np.float32)
``````

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

https://pythonmana.com/2021/01/20210120210426692r.html