程序:
#!/usr/bin/env python3
# -*-coding:GBK -*-
# author: diyue time:2019/10/22
# 功能:调用摄像头,并实现图像灰度化,‘q’键退出
import cv2
cap = cv2.VideoCapture(0)
if cap.isOpened():
# check the cap is open
print('yes')
# ret = cap.set(3, 400)
# w = cap.get(3)
# h = cap.get(4)
# print(w)
# print(h)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.resizeWindow('demo', 500, 500) # create one window ,w=h=500
# get one image and change to another form image,such as color to gray
cv2.imshow('demo', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
注释:
# author: diyue time:2019/10/22
1.键盘输入判定
import keyboard
while True:
if keyboard.is_pressed('k'):
print('oojj')
break
2.按键判定,关闭图像显示
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#'q'键退出,原理还没有理解透
3.cap.get()
功能:获取窗口参数,get()参数值从0-18代表不同参数,如3、4表示每一帧的宽高,百度具体内容
该函数应用于,设备获取之后,即cap = cv2.VideoCapture(0) 之后
4.cap.set(3,320)
功能:设置摄像头参数,(3,320)表示将摄像头宽度分辨率设置为320;(4,320)为将高度的分辨率设置为320
5.cv2.resizeWindow('demo', 500, 500) # create one window ,w=h=500
功能:新建窗口
参考:
https://blog.csdn.net/Danta_psq/article/details/88560172
http://www.voidcn.com/article/p-gxnwjbth-eo.html