将RGB视频转换为灰度并保存,可以使用以下步骤:
完整代码示例:
import cv2
def convert_rgb_to_gray_video(rgb_video_path, output_path):
video = cv2.VideoCapture(rgb_video_path)
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = video.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output = cv2.VideoWriter(output_path, fourcc, fps, (width, height), 0)
while True:
ret, frame = video.read()
if not ret:
break
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
output.write(gray_frame)
video.release()
output.release()
# 使用示例
convert_rgb_to_gray_video('path/to/rgb/video.mp4', 'path/to/grayscale/video.mp4')
这段代码将会打开一个RGB视频文件并逐帧转换为灰度图像,最后保存为一个新的灰度视频文件。您可以将'path/to/rgb/video.mp4'
替换为您的RGB视频文件的实际路径,将'path/to/grayscale/video.mp4'
替换为您希望保存灰度视频的路径。请确保OpenCV库已正确安装。
推荐的腾讯云相关产品:
领取专属 10元无门槛券
手把手带您无忧上云