Detailed operation of the camera opencv

Open the camera and capture photos

To use the camera, you need to use cv2.VideoCapture (0) create VideoCapture object parameter 0 refers to the number of the camera, if there are two cameras, then on your computer, visit the second camera can be passed 1, and so on.

# 打开摄像头并灰度化显示
import cv2 as cv 
# 0表示摄像头的编号
capture = cv.VideoCapture(0)

while(True):
    # 获取一帧
    # 第1个参数ret(return value缩写)是一个布尔值,表示当前这一帧是否获取正确
    ret, frame = capture.read()
    # 将这帧转换为灰度图
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break

The camera captured image:
The camera captured image

Obtain and modify the attribute values ​​of the camera

Some attributes may be acquired by the camera capture.get (propId), such as the capture resolution, brightness and contrast. propId is a number from 0 to 18, representing the different properties. To modify the attribute values ​​of the camera can be used capture.set (propId, value). For example, we add the following code before the while, you can capture with respect to the above two times the resolution of the video.

# 获取捕获的分辨率
# propId可以直接写数字,也可以用OpenCV的符号表示
width, height = capture.get(3), capture.get(4)
print(width, height)

# 以原分辨率的一倍来捕获
capture.set(cv.CAP_PROP_FRAME_WIDTH, width * 2)
capture.set(cv.CAP_PROP_FRAME_HEIGHT, height * 2)

2 times the resolution of the captured image to the original:
2 times the resolution of the captured image to the original

Local video playback

Like turning on the camera, if the number of video cameras into the path you can play local videos. Recall cv.waitKey (), which parameter indicates the pause time, so the higher the value, the slower the speed of video playback, on the contrary, the faster playback speed is usually set to 25 or 30.

# opencv播放本地视频
import cv2 as cv

capture = cv.VideoCapture('E:/1.mp4')

while(capture.isOpened()):
    ret, frame = capture.read()
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    cv.imshow('frame', gray)
    if cv.waitKey(30) == ord('q'):
        break

Play in E: /1.mp4:
Successfully play the video

Record videos and save

Before we save a picture using a cv.imwrite (), to save the video, we need to create an object of a VideoWriter, it needs to pass four parameters:
the file name 1. output such as 'output.avi'
2. coding FourCC code embodiment
3. framerate the FPS
4. to save the resolution size
FourCC video encoding is used to specify the four-byte codes. As MJPG encoding may be written: cv.VideoWriter_fourcc (* 'MJPG') or cv.VideoWriter_fourcc ( 'M', 'J ', 'P', 'G')

import cv2 as cv

capture = cv.VideoCapture(0)

# 定义编码方式并创建VideoWriter对象
fourcc = cv.VideoWriter_fourcc(*'MJPG')
outfile = cv.VideoWriter('output.avi', fourcc, 25., (640, 480))

while(capture.isOpened()):
    ret, frame = capture.read()

    if ret:
        outfile.write(frame)  # 写入文件
        cv.imshow('frame', frame)
        if cv.waitKey(1) == ord('q'):
            break
    else:
        break

Output.avi got his wish to become swells in the current path:
Got his wish generated output.avi
have seen here, do not point a praise, thank you (happy .jpg)

Guess you like

Origin www.cnblogs.com/wojianxin/p/12585417.html