1. Install Python
1. Download Python from the Python official website. Download Python | Python.org
The download is a bit slow, please be patient. (It is very fast to download with Thunder)
2. After downloading, install it step by step. I installed it locally in D:\Python\, and the path can be customized. Checked Add Environment Variables during installation, and you can use python commands directly in cmd. If you didn’t check Add Environment Variables during installation, you need to add them manually.
3.win+r to run the cmd command, input: python, you can check whether the installation is successful, and check the python version.
2. Install OpenCV
Here we use the directly compiled files and install them through the Python pip command.
1. Set the pip source of Python: Before installing OpenCV, we can set the pip source, so that the download will be faster, and the official download speed is too slow. Use cmd to enter the Scripts directory under the Python installation directory, and pip.exe is in this directory. enter:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
The global.index-url option indicates that the global mirror source of pip is set to the mirror source of Tsinghua University, and the download speed is faster.
2. Install opencv-python: or use cmd to enter the Scripts directory under the Python installation directory, enter:
pip install opencv-python
If the version number is not specified, the latest OpenCV will be downloaded (currently the latest is 4.7.0.72), and the running results are roughly as follows:
3. Install pip install opencv-contrib-python: Enter:
pip install opencv-contrib-python
The result of the operation is roughly as follows:
4. Install install matplotlib: This installation has a little more dependencies.
pip install matplotlib
operation result
3. Write a Hello Python and test the environment
I use vs code here to write and run Python files.
1. Create a new file hello.py and write:
import cv2
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread('C:\\Users\\admin\\Desktop\\1.png') #读取图像
cv2.imshow('image',img) #显示图像
cv2.waitKey(0) # 等待时间,毫秒级,0表示任意键终止
cv2.destroyAllWindows()
2. Running effect
At this point, the operating environment of OpenCV+Python is configured.