How to install OpenCV in Python using pip

WBOY
Release: 2024-01-18 08:57:17
Original
1143 people have browsed it

How to install OpenCV in Python using pip

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
Copy after login

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
Copy after login

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
Copy after login

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()
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!