使用 cv2.inRange 后,帧未写入输出视频

发布于 2025-01-14 18:06:13 字数 1811 浏览 1 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷离° 2025-01-21 18:06:13

您以彩色模式打开了 VideoWriter (isColor=真的)
但 inRange() 的结果只有一个通道,
所以它不会被写入视频。
在 inRange() 之后添加一个

cv2.cvtColor(frame,frame,cv2.COLOR_GRAY2BGR)

,因此您尝试编写的帧具有必要的通道数

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

cv2.cvtColor(frame,frame,cv2.COLOR_GRAY2BGR)

after the inRange(), so the frame you try to write has the nessecary number of channels

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文