Make writing a habit together! This is the 11th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

0. Preparation

Right click on the new project, select Python File, create a new Python File, and start with theimport cv2Import the CV2 library.

1. Transform into grayscale image
  1. Call imread() to get the image Lena.png from our resource folder
  2. The cvtColor() method allows us to convert our image to any color. The first parameter is the image we want to convert, and the second parameter is the color space we want to convert.cv2.COLOR_BGR2GRAYFrom BGR to GRAY, we have RGB three-channel order in everyday life, but in OpenCV it is BGR order.
  3. Use the imshow() method to display the Image with the window name Gray Image
  4. WaitKey (0) keeps the window open, but if you remove it, the window will flash by
img=cv2.imread("Resources/lena.png")
imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray Image",imgGray)
cv2.waitKey(0)
Copy the code

Let’s take a look at the effect:

2. Gaussian blur
  • GaussianBlur() is a method to blur images with GaussianBlur(also known as gaussian smoothing).
  • The first parameter is the image, the second parameter (7,7) is the size of the convolution kernel, which can only be an odd-length matrix, and the third parameter is Sigma X, which defaults to 0
  • Imshow () shows raw and grayscale images
img=cv2.imread("Resources/lena.png")
imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgBlur=cv2.GaussianBlur(imgGray,(7.7),0)
cv2.imshow("Gray Image",imgGray)
cv2.imshow("Blur Image",imgBlur)
cv2.waitKey(0)
Copy the code

Let’s take a look at the effect, with the original image on the left and gaussian blur on the right:

3. Edge detection

Edge detection In Canny() method, the first parameter is the image, the second parameter is the threshold value 1, and the third parameter is the threshold value 2, which is used to display the edge line with gray value within this range.

img=cv2.imread("Resources/lena.png")
imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgBlur=cv2.GaussianBlur(imgGray,(7.7),0)
imgCanny=cv2.Canny(img,150.200)

cv2.imshow("Gray Image",imgGray)
cv2.imshow("Blur Image",imgBlur)
cv2.imshow("Canny Image",imgCanny)
cv2.waitKey(0)
Copy the code

Let’s run it to see what it looks like:

4. Expansion operation

Kernel =np.ones((5,5),np.uint8), the value type is unsigned integer

kernel=np.ones((5.5),np.uint8)
img=cv2.imread("Resources/lena.png")
imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgBlur=cv2.GaussianBlur(imgGray,(7.7),0)
imgCanny=cv2.Canny(img,150.200)
imgDialation=cv2.dilate(imgCanny,kernel,iterations=1)

cv2.imshow("Gray Image",imgGray)
cv2.imshow("Blur Image",imgBlur)
cv2.imshow("Canny Image",imgCanny)
cv2.imshow("Dialation Image",imgDialation)
cv2.waitKey(0)
Copy the code

Let’s see what happens. The expansion makes the edge lines thicker, all joined together.

5. Corrosion operation

The closed edge image can be obtained by corroding the image that has just expanded.

kernel=np.ones((5.5),np.uint8)
img=cv2.imread("Resources/lena.png")
imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgBlur=cv2.GaussianBlur(imgGray,(7.7),0)
imgCanny=cv2.Canny(img,150.200)
imgDialation=cv2.dilate(imgCanny,kernel,iterations=1)

cv2.imshow("Gray Image",imgGray)
cv2.imshow("Blur Image",imgBlur)
cv2.imshow("Canny Image",imgCanny)
cv2.imshow("Dialation Image",imgDialation)
cv2.waitKey(0)
Copy the code

From left to right are expansion image, corrosion image and edge detection image

OK, the second section is also very simple, mainly to familiarize you with the most basic application of OpenCV. Eyes over a thousand times is not as good as a hand over, go quickly to begin to knock again ~💘