Learn how to install OpenCV using pip
OpenCV is a popular computer vision library that provides many powerful image processing and computer vision functions. Before you start using OpenCV, you need to make sure that Python and pip are installed correctly. In this article, we will introduce in detail how to use the pip command to install OpenCV and give specific code examples.
Make sure Python and pip have been installed correctly
Enter the following command in the terminal window to check the version of Python and pip:
python --version pip --version
If you see something like this The output indicates that Python and pip have been successfully installed:
Python 3.9.2 pip - 21.0.1
Install OpenCV
Enter the following command in the terminal window to install OpenCV using pip:
pip install opencv-python
This command will automatically download and install the latest version of OpenCV from the Python Package Index (PyPI).
Verify whether OpenCV installation is successful
You can use the following code to verify whether OpenCV has been successfully installed:
import cv2 print(cv2.__version__)
If you see output similar to this, it means OpenCV It has been successfully installed and can be used normally:
4.5.1
Using OpenCV for image processing
Now that you have successfully installed OpenCV, you can start using it for image processing. The following is a simple sample code that shows how to read an image and display it on the screen:
import cv2 # 读取图片 img = cv2.imread('image.jpg', cv2.IMREAD_COLOR) # 显示图片 cv2.imshow('Image', img) # 等待按键输入 cv2.waitKey(0) # 关闭窗口 cv2.destroyAllWindows()
In this example, we use the cv2.imread()
function An image named 'image.jpg' is read and displayed on the screen using the cv2.imshow()
function. Then we use cv2.waitKey(0)
to wait for the user to press any key, and finally use cv2.destroyAllWindows()
to close the window.
Summary:
Through this article, you learned how to use the pip command to install OpenCV and gave specific code examples. By installing OpenCV and running the sample code, you have begun the journey of learning OpenCV. I hope you can learn more about OpenCV and make breakthroughs in the fields of image processing and computer vision!
The above is the detailed content of Master how to install OpenCV using pip. For more information, please follow other related articles on the PHP Chinese website!