1.获取usb摄像头
import cv2
cam = cv2.VideoCapture()
cam.open( 0, apiPreference = cv2.CAP_V4L2)
cam.set(cv2.CAP_PROP_FRAME_WIDTH,640)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG'))
cam.set(cv2.CAP_PROP_FPS,60)
while True:
ret,frame = cam.read()
if ret == False:
break
cv2.imshow('USB Camera' , frame)
if cv2.waitKey(1) == ord('q'):
break
print(cam.get(3),cam.get(4),cam.get(5),cam.get(6))
cam.release()
cv2.destroyAllWindows()
2.灰度识别 和 窗口设置位置设置
import cv2
cam = cv2.VideoCapture()
cam.open( 0, apiPreference = cv2.CAP_V4L2)
cam.set(cv2.CAP_PROP_FRAME_WIDTH,640)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG'))
cam.set(cv2.CAP_PROP_FPS,60)
while True:
ret,frame = cam.read()
if ret == False:
break
cv2.imshow('USB Camera' , frame)
cv2.moveWindow('USB Camera',0,0)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray Video',gray)
cv2.moveWindow('gray Video',0,540)
LAB = cv2.cvtColor(frame,cv2.COLOR_BGR2LAB)
cv2.imshow('LAB Video',LAB)
cv2.moveWindow('LAB Video',700,0)
HLS = cv2.cvtColor(frame,cv2.COLOR_BGR2HLS)
cv2.imshow('HLS Video',HLS)
cv2.moveWindow('HLS Video',700,540)
HSV = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
cv2.imshow('HSV Video',HSV)
cv2.moveWindow('HSV Video',1440,540)
XYZ = cv2.cvtColor(frame,cv2.COLOR_BGR2XYZ)
cv2.imshow('XYZ Video',XYZ)
cv2.moveWindow('XYZ Video',1440,0)
if cv2.waitKey(1) == ord('q'):
break
print(cam.get(3),cam.get(4),cam.get(5),cam.get(6))
cam.release()
cv2.destroyAllWindows()
3.窗口大小设置
import cv2
cam = cv2.VideoCapture()
cam.open( 0, apiPreference = cv2.CAP_V4L2)
cam.set(cv2.CAP_PROP_FRAME_WIDTH,640)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG'))
#cam.set(cv2.CAP_PROP_FPS,60)
while True:
ret,frame = cam.read()
if ret == False:
break
cv2.imshow('USB Camera' , frame)
cv2.moveWindow('USB Camera',0 , 0)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frameSmall = cv2.resize(gray,(320,240))
cv2.imshow('gray' , frameSmall)
cv2.moveWindow('gray' , 720, 0)
if cv2.waitKey(1) == ord('q'):
break
print(cam.get(3),cam.get(4),cam.get(5),cam.get(6))
cam.release()
cv2.destroyAllWindows()