使用 cv2.inRange 后,帧未写入输出视频
我正在尝试通过 BGR 颜色空间中的阈值执行对象检测(在我录制的视频中)并将结果保存在输出视频中。
我使用 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()
I'm trying to perform object detection (in a video I recorded) through thresholding in BGR color space and save the result in an output video.
The preview I get using cv2.imshow
is correct, the binary map I get for the object location is the right one.
However, these frames showing the binary map (the ones I process using cv2.inRange()
) are missing from the output video.
The rest of the video frames are properly written in the output video.
Does anyone know what could be causing this issue?
Thanks!
This is my code:
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您以彩色模式打开了 VideoWriter (isColor=真的)
但 inRange() 的结果只有一个通道,
所以它不会被写入视频。
在 inRange() 之后添加一个
,因此您尝试编写的帧具有必要的通道数
you opened the VideoWriter in color mode (isColor=True)
but the result from inRange() has only a single channel,
so it wont get written to the video.
add a
after the inRange(), so the frame you try to write has the nessecary number of channels