使用 cv2.inRange 后未写入输出视频的帧
Frames not written in output video after cv2.inRange is used
我正在尝试通过 BGR 颜色阈值处理(在我录制的视频中)执行对象检测 space 并将结果保存在输出视频中。
我使用 cv2.imshow
得到的预览是正确的,我得到的对象位置的二值图是正确的。
但是,输出视频中缺少显示二进制映射(我使用 cv2.inRange()
处理的那些)的这些帧。
其余视频帧已正确写入输出视频。
有谁知道可能导致此问题的原因是什么?
谢谢!
这是我的代码:
import cv2
# helper function to change what you do based on video seconds
# arguments "lower: int" and "upper: int" are measured in milliseconds
def between(cap, lower: int, upper: int) -> bool:
return lower <= int(cap.get(cv2.CAP_PROP_POS_MSEC)) < upper
cap = cv2.VideoCapture(input_video_file_path)
fps = int(round(cap.get(5)))
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # saving output video as .mp4
out = cv2.VideoWriter(output_video_file_path, fourcc, fps, (frame_width, frame_height)) # to create a VideoWriter object
# while loop where the real work happens
while cap.isOpened():
ret, frame = cap.read()
if ret:
if cv2.waitKey(28) & 0xFF == ord('q'):
break
if between(cap, 1000, 4000):
lower_blue = (82,0,0) #BGR
upper_blue = (255,143,61)
frame = cv2.inRange(frame,lower_blue,upper_blue)
frame = cv2.putText(frame, 'Grab in RBG', (50,50),cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255,255,255), 2, cv2.LINE_AA)
# write frame that you processed to output
out.write(frame)
# (optional) display the resulting frame
cv2.imshow('Frame', frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture and writing object
cap.release()
out.release()
# Closes all the frames
cv2.destroyAllWindows()
您在彩色模式下打开了 VideoWriter (isColor=True)
但是 inRange() 的结果只有一个通道,
所以它不会被写入视频。
添加一个
cv2.cvtColor(frame,frame,cv2.COLOR_GRAY2BGR)
在 inRange() 之后,因此您尝试写入的帧具有必要的通道数
我正在尝试通过 BGR 颜色阈值处理(在我录制的视频中)执行对象检测 space 并将结果保存在输出视频中。
我使用 cv2.imshow
得到的预览是正确的,我得到的对象位置的二值图是正确的。
但是,输出视频中缺少显示二进制映射(我使用 cv2.inRange()
处理的那些)的这些帧。
其余视频帧已正确写入输出视频。
有谁知道可能导致此问题的原因是什么?
谢谢!
这是我的代码:
import cv2
# helper function to change what you do based on video seconds
# arguments "lower: int" and "upper: int" are measured in milliseconds
def between(cap, lower: int, upper: int) -> bool:
return lower <= int(cap.get(cv2.CAP_PROP_POS_MSEC)) < upper
cap = cv2.VideoCapture(input_video_file_path)
fps = int(round(cap.get(5)))
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # saving output video as .mp4
out = cv2.VideoWriter(output_video_file_path, fourcc, fps, (frame_width, frame_height)) # to create a VideoWriter object
# while loop where the real work happens
while cap.isOpened():
ret, frame = cap.read()
if ret:
if cv2.waitKey(28) & 0xFF == ord('q'):
break
if between(cap, 1000, 4000):
lower_blue = (82,0,0) #BGR
upper_blue = (255,143,61)
frame = cv2.inRange(frame,lower_blue,upper_blue)
frame = cv2.putText(frame, 'Grab in RBG', (50,50),cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255,255,255), 2, cv2.LINE_AA)
# write frame that you processed to output
out.write(frame)
# (optional) display the resulting frame
cv2.imshow('Frame', frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture and writing object
cap.release()
out.release()
# Closes all the frames
cv2.destroyAllWindows()
您在彩色模式下打开了 VideoWriter (isColor=True) 但是 inRange() 的结果只有一个通道, 所以它不会被写入视频。 添加一个
cv2.cvtColor(frame,frame,cv2.COLOR_GRAY2BGR)
在 inRange() 之后,因此您尝试写入的帧具有必要的通道数