根据经验,图像顺序变化的原因取决于您使用的加载图像的框架。特别是OpenCV以BGR格式排序通道,因为大多数历史原因现在已过时。因此,我们不幸地被困在这个设计选择中。常规RGB格式的图像可以使用scikit-image、matplotlib和Pillow查看。
事实上,如果您查看load_video
函数,它使用OpenCV打开视频,因此传入的帧是BGR格式。因此,强制交换通道以获取RGB格式是必需的:
def load_video(path, max_frames=0):
cap = cv2.VideoCapture(path)
frames = []
try:
while True:
ret, frame = cap.read()
if not ret:
break
frame = crop_center(frame)
frame = frame[:, :, [2, 1, 0]]
frames.append(frame)
if len(frames) == max_frames:
break
finally:
cap.release()
return np.array(frames)