Home > Article > Backend Development > OpenCV Quick Installation Guide: Install using pip
Using pip to quickly install OpenCV tutorial
Introduction:
OpenCV (Open Source Computer Vision) is an open source computer vision library that provides a rich set of images and video processing functions, which can be used to implement various computer vision tasks, such as face recognition, target tracking, image segmentation, etc. In this tutorial, we will introduce how to quickly install OpenCV using pip, and provide some specific code examples to help readers understand how to use OpenCV for basic image processing tasks.
Step 1: Install pip
Before you start, you first need to make sure that pip is installed on your computer. pip is a package management tool in Python that can help us quickly install and manage third-party libraries. Enter the following command on the command line to check whether pip has been installed:
pip --version
If pip has been installed, the version number of pip will be displayed; otherwise, pip needs to be installed first.
Step 2: Install OpenCV
Enter the following command on the command line to install OpenCV using pip:
pip install opencv-python
This command will download the latest version from PyPI (Python Package Index) OpenCV and automatically installed into your Python environment. After the installation is complete, you can verify whether the installation is successful by running the following command:
import cv2 print(cv2.__version__)
This code snippet will import the OpenCV library and print out the OpenCV version number. If no errors occur and the version number is successfully printed, OpenCV has been successfully installed.
Step 3: Use OpenCV for image processing
Below, we will use some specific code examples to show how to use OpenCV for basic image processing tasks.
Reading and displaying images
import cv2 # 读取图像 image = cv2.imread('image.jpg') # 显示图像 cv2.imshow('image', image) cv2.waitKey(0) cv2.destroyAllWindows()
This code will read the image named 'image.jpg' and display it in in a window. After pressing any key in the window, the window will close.
Image grayscale
import cv2 # 读取图像 image = cv2.imread('image.jpg') # 将图像转为灰度图 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 显示灰度图 cv2.imshow('gray image', gray_image) cv2.waitKey(0) cv2.destroyAllWindows()
This code will read the image named 'image.jpg' and convert it is a grayscale image. Then, display the grayscale image in a window.
Gaussian Blur
import cv2 # 读取图像 image = cv2.imread('image.jpg') # 高斯模糊 blurred_image = cv2.GaussianBlur(image, (5, 5), 0) # 显示模糊后的图像 cv2.imshow('blurred image', blurred_image) cv2.waitKey(0) cv2.destroyAllWindows()
This code will read the image named 'image.jpg' and perform Gaussian blur on it deal with. Then, display the blurred image in a window.
Summary:
Through this tutorial, we learned how to quickly install OpenCV using pip, and provided some specific code examples to demonstrate the basic usage of OpenCV. It is hoped that readers can master basic image processing skills by studying this tutorial, and flexibly use OpenCV to solve various image processing problems in actual projects.
The above is the detailed content of OpenCV Quick Installation Guide: Install using pip. For more information, please follow other related articles on the PHP Chinese website!