In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the basic functions of Python OpenCV". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the basic functions of Python OpenCV.
Preparatory work
Right-click the new project, select Python File, create a new Python file, and then import the cv2 library into import cv2 at the beginning.
Convert to grayscale image
Call the imread () method to get the picture lena.png in our resource folder
CvtColor () method allows our pictures to be converted into any color, the first parameter is the picture we want to convert, the second parameter is the color space to be converted, cv2.COLOR_BGR2GRAY is from BGR to GRAY, our daily life is RGB three-channel order, and in OpenCV is the BGR order.
Use the imshow () method to display the picture, and the window name is Gray Image
The sentence waitKey (0) can keep the window forever. If you remove this sentence, 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)
Let's take a look at the effect:
Gaussian blur
GaussianBlur () is a Gaussian blur (also known as Gaussian smoothing) on the image, which is a method to remove image noise.
The first parameter is the image, the second parameter 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 () displays the original image and grayscale image
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)
Let's take a look at the effect. The left is the original image, and the right is Gaussian blur:
Edge detection
In the edge detection Canny () method, the first parameter is the image, the second parameter is threshold 1, and the third parameter is threshold 2, which is used to display the edge lines with grayscale values in 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,150200) cv2.imshow ("Gray Image", imgGray) cv2.imshow ("Blur Image", imgBlur) cv2.imshow ("Canny Image", imgCanny) cv2.waitKey (0)
Let's run it to see the effect:
Expansion operation
In the dilation operation, the numpy library will be used. Let's import it first: import numpy as np defines the convolution kernel with the size of 5x5: kernel=np.ones ((5L5), np.uint8). The numerical type is an unsigned integer.
Kernel=np.ones ((5je 5), np.uint8) img=cv2.imread ("Resources/lena.png") imgGray=cv2.cvtColor (img,cv2.COLOR_BGR2GRAY) imgBlur=cv2.GaussianBlur (imgGray, (7 kernel=np.ones 7), 0) imgCanny=cv2.Canny (img,150200) 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)
Let's look at the effect, the expansion makes the edge lines thicker, all connected to one piece.
Erosion operation
If we corrode the newly expanded image, we can get a closed edge image.
Kernel=np.ones ((5je 5), np.uint8) img=cv2.imread ("Resources/lena.png") imgGray=cv2.cvtColor (img,cv2.COLOR_BGR2GRAY) imgBlur=cv2.GaussianBlur (imgGray, (7 kernel=np.ones 7), 0) imgCanny=cv2.Canny (img,150200) 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)
From left to right are expansion image, corrosion image and edge detection image.
At this point, I believe you have a deeper understanding of "how to use the basic functions of Python OpenCV". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.