Using OpenCV in Python can help us with image processing, computer vision and other aspects of work. Pip is a very important package management tool in Python, which allows us to easily install, upgrade or uninstall Python packages. In this article, I will introduce how to install OpenCV using pip, with specific code examples.
Step One: Install pip
If you haven’t installed pip yet, you need to install it first. Under Linux and macOS systems, you can use the following command to install:
sudo apt-get install python-pip
Under Windows systems, you can download the pip installation package from the Python official website and install it.
Step 2: Install OpenCV
Once pip is installed, you can use pip to install OpenCV. Before installing OpenCV, you need to choose a Python version (Python 2.x or Python 3.x), and then execute the following command:
pip install opencv-python
If you want to install OpenCV’s Contrib module (including SIFT and SURF etc.), you can use the following command:
pip install opencv-contrib-python
Step 3: Test OpenCV
In order to ensure that OpenCV has been installed correctly, we can use the following code to test:
import cv2 img = cv2.imread('test.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imshow('image',gray) cv2.waitKey(0) cv2.destroyAllWindows()
After saving the above code as a .py file, place an image named test.jpg in the same directory and run this file. You should be able to see the grayscale version of the image.
At this point, you have successfully installed OpenCV and can use it in Python. Hope this article helps you!
The above is the detailed content of How to install OpenCV in Python using pip. For more information, please follow other related articles on the PHP Chinese website!