python-opencv在图像处理中很重要,那么用OpenCV读摄像头或视频,取出的帧是一种什么样的数据呢?
实验代码如下:
import cv2cap = cv2.VideoCapture(r'I:\test.mp4')if cap.isOpened():ret, frame = cap.read()print(frame)cap.release()
运行结果:
[[[2 2 2][2 2 2][1 1 1]...[0 0 0][0 0 0][0 0 0]][[2 2 2][2 2 2][1 1 1]...[0 0 0][0 0 0][0 0 0]][[1 1 1][1 1 1][1 1 1]...[0 0 0][0 0 0][0 0 0]]...[[0 0 0][0 0 0][0 0 0]...[0 0 0][0 0 0][0 0 0]][[0 0 0][0 0 0][0 0 0]...[0 0 0][0 0 0][0 0 0]][[0 0 0][0 0 0][0 0 0]...[0 0 0][0 0 0][0 0 0]]]Process finished with exit code 0
检查一下它属于什么类型:
import cv2cap = cv2.VideoCapture(r'I:\test.mp4')if cap.isOpened():ret, frame = cap.read()print(type(frame))cap.release()
运行的结果:
<class 'numpy.ndarray'>Process finished with exit code 0
很显然,帧取出来后,在Python中以多维数组的形式呈现。类型是numpy.ndarray。