第一步安装numpy pip install numpy 第二步安装opencv-contrib-python pip install opencv-contrib-python 添加灰色滤镜
import cv2import numpy as npdef show_view():cap cv2.VideoCapture(0)ret cap.set(3, 320) # 设置捕获窗口大小ret cap.set(4, 240)while(cap.isOpened()):ret, frame cap.read()gray cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 彩色图像用BGR2RGBcv2.imshow(frame, gray)if cv2.waitKey(1) ord(q):breakcap.release()cv2.destroyAllWindows()if __name__ __main__:show_view()
原图像输出
import cv2import numpy as npdef show_view():cap cv2.VideoCapture(0)ret cap.set(3, 320) # 设置捕获窗口大小ret cap.set(4, 240)while(cap.isOpened()):ret, frame cap.read()# gray cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 彩色图像用BGR2RGBcv2.imshow(frame, frame)if cv2.waitKey(1) ord(q):breakcap.release()cv2.destroyAllWindows()if __name__ __main__:show_view()
学习网址https://docs.opencv.org/master/