opencv video operation

  • Read video content from the camera
import numpy as np
import cv2

# 从摄像头获取图像数据
cap = cv2.VideoCapture(0)

while(True):
    # ret 读取成功True或失败False
    # frame读取到的图像的内容
    # 读取一帧数据
    ret,frame = cap.read()
    # 变为灰度图
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray)
    # waitKey功能是不断刷新图像,单位ms,返回值是当前键盘按键值  
    # ord返回对应的ASCII数值
    if cv2.waitKey(100) & 0xff == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
  • The explanation about cv2.waitKey(100) & 0xff == ord('q') is at https://blog.csdn.net/weixin_44049693/article/details/106271643
  • Read the video file
    cap.read() returns a Boolean value (True/False). If the frame read is correct, it is True. So in the end, you can check whether the video file has reached the end by checking its return value.
    Sometimes cap may not be able to successfully initialize the camera device. In this case, the above code will report an error. You can use cap.isOpened() to check whether the initialization is successful. If the return value is True, there is no problem. Otherwise, use the function cap.open().
    You can use the function cap.get(propId) to get some parameter information of the video. Here propId can be any integer between 0 and 18. Each number represents an attribute of the video.

Some of these values ​​can be modified using cap.set(propId,value), and value is the new value you want to set.
For example, I can use cap.get(3) and cap.get(4) to see the width and height of each frame. The value obtained by default is 640X480. But we can use ret=cap.set(3,320) and ret=cap.set(4,240) to change the width and height to 320X240.
Insert picture description here

  • Read local video files
import numpy as np
import cv2
# 从文件读取视频内容
cap = cv2.VideoCapture('videos/cats.mp4')
# 视频每秒传输帧数
fps = cap.get(cv2.CAP_PROP_FPS)
# 视频图像的宽度
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
# 视频图像的长度
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(fps)
print(frame_width)
print(frame_height)

Insert picture description here

  • Local video file display
while(True):
    # ret 读取成功True或失败False
    # frame读取到的图像的内容
    # 读取一帧数据
    ret,frame = cap.read()
    if ret!=True:
        break
    cv2.imshow('frame',frame)
    # waitKey功能是不断刷新图像,单位ms,返回值是当前键盘按键值
    # ord返回对应的ASCII数值
    if cv2.waitKey(33) & 0xff == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Insert picture description here

Video writing

After we capture the video and process each frame, we want to save the video. For pictures, it is very simple, just use cv2.imwrite(). But for video, more work must be done.
This time we are going to create a VideoWriter object. We should determine the name of an output file. Next, specify the FourCC code (described below). The playback frequency and frame size also need to be determined. The last one is the isColor tag. If it is True, each frame is a color image, otherwise it is a grayscale image.
FourCC is a 4-byte code used to determine the encoding format of the video. The list of available codes can be found at fourcc.org.
• In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)

# 从文件读取视频内容
cap = cv2.VideoCapture('videos/cats.mp4')
# 视频每秒传输帧数
fps = cap.get(cv2.CAP_PROP_FPS)
# 视频图像的宽度
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
# 视频图像的长度
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(fps)
print(frame_width)
print(frame_height)

Insert picture description here

#指定视频编码格式
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('videos/output11.avi',fourcc,fps,(frame_width,frame_height))

while(True):
    ret, frame = cap.read()
    if ret==True:
        # 水平翻转
        frame = cv2.flip(frame,1)
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(25) & 0xff == ord('q'):
            break
    else:
        break
out.release()
cap.release()
cv2.destroyAllWindows()

Insert picture description here

Guess you like

Origin blog.csdn.net/cyj5201314/article/details/115056185