[Learning] Raspberry Pi six Raspberry Pi OpenCV video / camera Basic Operations

I. Introduction

Raspberry Pi comes with multiple USB ports, we can be an external USB camera. If the drive supports, by default in the system's / dev, directly out of the virtual device (video0, video1 ...). Raspberry Pi opencv directly video0 this data, video, and process (program execution processing, recording preservation function, because the program locks the virtual video device, plug the camera, the serial number may change, such as the default video0 , will become video1).

Second, video / camera Basic operation functions

1. Create an object VideoCapture
cap = cv2.VideoCapture(0) # 创建一个 VideoCapture 对象, 0 是指摄像头video0, 也可以改成文件名来引入视频文件。
2, release the camera or video file
cap.release() #释放对象, 再操作完成之后需要释放, 否则其他程序无法再次获取摄像头或者视频文件。
3, a data read
ret, frame = cap.read() #一帧帧读取数据给frame, 并返回一个布尔值( True/False) 并赋值给 ret。 

Pay attention to you, if the reading frame is correct, retis True. So you can finally return to his value by checking to see if the video file has reached the end.

Sometimes cap may not be successful initialization of camera equipment. In this case, the above code error.
You can use cap.isOpened()to check if a successful initialization. If the return value is True, then there is no problem.
Otherwise, it will use the function cap.open().

4, access, modify video parameters
ret = cap.get(propId) #获取视频对应 ID 的参数

ret = cap.set(propId,value) #修改视频对应 ID 的参数

propID names and the corresponding meanings indicated in the following table:

propID name meaning
0 CV_CAP_PROP_POS_MSEC The current location of the video file (ms)
1 CV_CAP_PROP_POS_FRAMES Next, based on the index to be decoded / captured frames 0
2 CV_CAP_PROP_POS_AVI_RATIO The relative position of the video file: Start 0- video, ending 1- video.
3 Width of the frames in the video stream Width of the video stream of frames
4 CV_CAP_PROP_FRAME_HEIGHT Height of the video stream of frames
5 CV_CAP_PROP_FPS Frame rate
6 CV_CAP_PROP_FOURCC 4 characters codec code
7 CV_CAP_PROP_FRAME_COUNT The number of frames in the video file
8 CV_CAP_PROP_FORMAT The retrieve()format of the returned object Mat
9 CV_CAP_PROP_MODE Values ​​specific to the rear end of the capture mode indicates the current
10 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras)
11 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras)
12 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras)
13 CV_CAP_PROP_HUE Hue Pictures (only for cameras)
14 CV_CAP_PROP_GAIN Image gain (only for cameras)
15 CV_CAP_PROP_EXPOSURE Exposure (only for cameras)
16 CV_CAP_PROP_CONVERT_RGB Whether a Boolean flag that indicates whether images should be converted to RGB
17 CV_CAP_PROP_WHITE_BALANCE Not currently supported
18 CV_CAP_PROP_RECTIFICATION Rectification sign a stereo camera (Note: Only the back-end support by DC1394 v 2.x)
5, taken grayscale image
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #从帧图像 frame 中截取灰度图并赋值给 gray
6, set the video format
fourcc = cv.FOURCC('P','I','M','1') 

MJPG set coding format corresponding to the format is as follows:

cv.FOURCC('P','I','M','1') = MPEG-1 codec
cv.FOURCC('M','J','P','G') = motion-jpeg codec (does not work well)
cv.FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
cv.FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
cv.FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
cv.FOURCC('U', '2', '6', '3') = H263 codec
cv.FOURCC('I', '2', '6', '3') = H263I codec
cv.FOURCC('F', 'L', 'V', '1') = FLV1 codec
 #定义输出属性: 将一帧图像保存到名为 output.avi 的文件中
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

out.write(frame) #将帧文件 frame 按照 out 属性保存为视频文件
7, image rotation
frame = cv2.flip(frame,0) #0:垂直反转; 1:水平翻转; -1:水平垂直翻转

Third, the sample code

1, a camera to capture color video, and to achieve on the keyboard is pressed qto exit the function. code show as below
# -*- coding: utf-8 -*-
import cv2

cap = cv2.VideoCapture(0)
while (cap.isOpened()):
	ret,frame = cap.read()	#capture frame_by_frame
	if ret == True:
		cv2.imshow('color',frame)#显示彩色视图
		if cv2.waitKey(1)&0xFF == ord('q'):
			break
	else:
		break
cap.release()
cv2.destroyAllWindows()

Results are as follows:
Here Insert Picture Description

2, a camera to capture color images and save!
# -*- coding: utf-8 -*-
import cv2

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi',fourcc,20.0,(640,480))
while (cap.isOpened()):
	ret,frame = cap.read()	#capture frame_by_frame
	if ret == True:
		gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
		out.write(frame)#保存彩色视图
		cv2.imshow('color',frame)#显示彩色视图
		if cv2.waitKey(1)&0xFF == ord('q'):
			break
	else:
		break
cap.release()
cv2.destroyAllWindows()
3, while the display color and grayscale images!
# -*- coding: utf-8 -*-
import cv2

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi',fourcc,20.0,(640,480))
while (cap.isOpened()):
	ret,frame = cap.read()	#capture frame_by_frame
	if ret == True:
		gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
		#out.write(frame)#保存彩色视图
		cv2.imshow('frame',gray)#显示灰度视图
		cv2.imshow('color',frame)#显示彩色视图
		if cv2.waitKey(1)&0xFF == ord('q'):
			break
	else:
		break
cap.release()
cv2.destroyAllWindows()
Published 653 original articles · won praise 1016 · Views 730,000 +

Guess you like

Origin blog.csdn.net/ReCclay/article/details/103665060