使用opencv+python读取图片和视频

import cv2

# 使用opencv+Python读取图片
# 创建img_show函数:读取并显示图片
def img_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# 调用cv_show()函数
img = cv2.imread("cat.jpg")
img_show('cat',img)


# 使用opencv+Python读取视频
vc = cv2.VideoCapture('test.mp4')
# 检查摄像头是否正确打开
if vc.isOpened():
    flag = 1
else:
    flag = 0
 # 若flag = 1   
if (flage):
	while True:
	    ret,frame = vc.read()  # 读取一帧
	    if ret == False:  # 读取帧失败
	        break
	    if ret == True:
	        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)  # 灰度转换
	        cv2.imshow('result',gray)
	        if cv2.waitKey(10) & 0xFF == 27:  # 每隔10秒读取下一帧图片,当按ESC键时退出程序
	            break
vc.release()
cv2.destroyAllWindows()

你可能感兴趣的:(科研)