In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to install and use the OpenCV library. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Installation
OpenCV can be installed through pip by running the following command in the Jupyter Notebook unit:
! pip install-- upgrade opencv-python
Pip is the default package manager and stand-alone executable for Python, but running it in this way ensures that the package is installed in the Anaconda environment.
If the package is installed correctly, this Python code should work properly:
Import cv2
Because we want to display images in our Jupyter Notebook, we should also make sure that matplotlib is installed:
! pip install-- upgrade matplotlib reads and draws images
Reading images that can be processed by OpenCV is simple:
Import cv2im = cv2.imread ("path/to/image.jpg")
OpenCV supports multiple image formats. When parsing an image fails, the result imread will be None. Note that if the image file is not found, no error will be raised-the result will be the same None.
Suppose we find the image, and then we can use matplotlib. To do this, we will use this helper function:
From matplotlib import pyplotdef plot_img (img): rgb = cv2.cvtColor (img, cv2.COLOR_BGR2RGB) pyplot.imshow (rgb)
OpenCV reads images in BGR color format, but matplotlib wants them to be RGB, so first we have to convert the images. And then you can draw.
You can use this feature as follows:
Plot_img (im)
The OpenCV and matplotlib integration is so clean because the OpenCV image is really just a multidimensional NumPy array containing pixel values, and matplotlib can use it.
Object detection
There are a lot of OpenCV can do. We will pay special attention to object detection.
Object detection works with so-called cascading classifiers. This method uses machine learning: the classifier trains on the image containing the desired object (positive image) and the image that does not contain it (negative image). You can train your own classifier, but OpenCV also provides several pre-training models that can be downloaded from its GitHub.
Let's try the Russian license plate pre-training classifier haarcascade_russian_plate_number.xml. If you want to test the image, you can use the Lada Vesta image of Sergey Rodovnichenko.
We can read the image and draw it to see if everything is going well, just like before:
Car = cv2.imread ("Pictures/lada-vesta.jpg") plot_img (car)
Next, we create a classifier:
Python copy code
Classifier = cv2.CascadeClassifier () classifier.load ("path/to/haarcascade_russian_plate_number.xml")
The detection is done through the following detectMultiScale methods:
Plates = classifier.detectMultiScale (car)
This returns an array of NumPy. It is actually an array array, and each inner array is the rectangular boundary of the detected plate in the format [x, y, width, height]. To display them visually, we can draw a rectangle on the image and then draw the result:
With_indicators = carfor plate in plates: X, y, width, height = plate with_indicators = cv2.rectangle (with_indicators, (x, y), (x + width, y + height), (0,0,255), 5)
The rectangle function uses the image, upper-left coordinates, lower-right coordinates, color and thickness. It returns a new image with a rectangle. We can draw the results:
Plot_img (with_indicators)
This example shows OpenCV's pre-training classifier for Russian license plates, but this is not the only classifier available. There are also pre-training classifiers for faces, eyes, etc., which are used in exactly the same way as this classifier.
These are all the contents of the article "how to install and use the OpenCV Library". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.