如何将 rgb 视频转换为灰度并保存?
How to convert a rgb video to grayscale and save it?
我是 python 的新手,我想将彩色视频转为灰度,然后保存。
我试过这段代码让它变成灰度,但我无法保存它。有什么想法吗?
import cv2
source = cv2.VideoCapture('video.mp4')
while True:
ret, img = source.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Live', gray)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.destroyAllWindows()
source.release()
这是将RGB视频文件写入灰度视频的方法
# importing the module
import cv2
import numpy as np
# reading the vedio
source = cv2.VideoCapture('input.avi')
# We need to set resolutions.
# so, convert them from float to integer.
frame_width = int(source.get(3))
frame_height = int(source.get(4))
size = (frame_width, frame_height)
result = cv2.VideoWriter('gray.avi',
cv2.VideoWriter_fourcc(*'MJPG'),
10, size, 0)
# running the loop
while True:
# extracting the frames
ret, img = source.read()
# converting to gray-scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# write to gray-scale
result.write(gray)
# displaying the video
cv2.imshow("Live", gray)
# exiting the loop
key = cv2.waitKey(1)
if key == ord("q"):
break
# closing the window
cv2.destroyAllWindows()
source.release()
如果这对你有帮助就给
我是 python 的新手,我想将彩色视频转为灰度,然后保存。 我试过这段代码让它变成灰度,但我无法保存它。有什么想法吗?
import cv2
source = cv2.VideoCapture('video.mp4')
while True:
ret, img = source.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Live', gray)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.destroyAllWindows()
source.release()
这是将RGB视频文件写入灰度视频的方法
# importing the module
import cv2
import numpy as np
# reading the vedio
source = cv2.VideoCapture('input.avi')
# We need to set resolutions.
# so, convert them from float to integer.
frame_width = int(source.get(3))
frame_height = int(source.get(4))
size = (frame_width, frame_height)
result = cv2.VideoWriter('gray.avi',
cv2.VideoWriter_fourcc(*'MJPG'),
10, size, 0)
# running the loop
while True:
# extracting the frames
ret, img = source.read()
# converting to gray-scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# write to gray-scale
result.write(gray)
# displaying the video
cv2.imshow("Live", gray)
# exiting the loop
key = cv2.waitKey(1)
if key == ord("q"):
break
# closing the window
cv2.destroyAllWindows()
source.release()
如果这对你有帮助就给