# 更新apt-get
sudo apt-get update && sudo apt-get upgrade
# 安装此次必需的软件
sudo apt-get install -y build-essential cmake make pkg-config
# 多种图片格式支持包
sudo apt-get install -y libjpeg-dev libtiff5-dev libjasper-dev libpng-dev
# 视频支持包(支持视频文件 & 视频串流)
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
# OpenCV的子包highgui(用于图像处理)所必需的GTK development library相关包
$ sudo apt-get install -y libfontconfig1-dev libcairo2-dev libgdk-pixbuf2.0-dev libpango1.0-dev libgtk2.0-dev libgtk-3-dev
# 加速opencv矩阵运算的包
sudo apt-get install -y libatlas-base-dev gfortran
# 编译opencv+python时所需的python头文件
sudo apt-get install -y python3-dev
# HDF5 数据库 和 Qt GUIs (仅限pip安装opencv,手动编译完整版opencv4的话不需要这步)
sudo apt-get install -y libhdf5-dev libhdf5-serial-dev libhdf5-103 python3-pyqt5
步骤2/4:安装python虚拟环境(可选)
首先下载pip
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo python3 get-pip.py
sudo rm -rf ~/.cache/pip
步骤3/4:安装opencv
安装OpenCV
pip install opencv-contrib-python==4.5.5.62
python # 运行python3
>>> import cv2
>>> cv2.__version__
'4.5.5'
新建一个 test1.py 文件,写入如下内容并保存 (用于显示灰度图)
import cv2
img = cv2.imread("lena.jpg", 0)
# print(img)
cv2.namedWindow('img', cv2.WINDOW_AUTOSIZE)
cv2.imshow('img', img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
注意修改为自己的图片名称和路径,按 esc 键退出程序
新建一个 test2.py 文件,写入如下内容并保存(用于显示视频)
import cv2
cap = cv2.VideoCapture('/home/pi/18.mp4')
cv2.namedWindow('frame', 0)
cv2.resizeWindow("frame", 400, 400)
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
注意修改为自己的视频名称和路径,按 q 键退出程序