一、安装所需要的库和依赖
sudo pip3 install numpy
sudo apt-get install build-essential git cmake pkg-config -y
sudo apt-get install libjpeg8-dev -y
sudo apt-get install libtiff5-dev -y
sudo apt-get install libjasper-dev -y
sudo apt-get install libpng12-dev -y
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y
sudo apt-get install libgtk2.0-dev -y
sudo apt-get install libatlas-base-dev gfortran -y
sudo pip3 install opencv-python
二、测试安装环境
在终端输入 :python3
然后 :import cv2
如果安装成功,则import不会有问题
下面就可以输出OpenCv的版本号
使用 :print(cv2.version)
退出python用: exit()
三、简单应用
使用OpenCv库读取usb摄像头,实时显示并拍照
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
# get a frame
ret, frame = cap.read()
# show a frame
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
#退出并拍照
cv2.imwrite("takephoto2.jpg", frame)
print("take Photo Ok")
break
cap.release()
cv2.destroyAllWindows()
|