【问题标题】:How can I show an Image in the same frame of a video in OpenCV ( Python )?如何在 OpenCV ( Python ) 中的视频的同一帧中显示图像?
【发布时间】:2019-12-05 19:10:06
【问题描述】:

我正在尝试在 cv2 上显示视频,当视频结束时,我想显示和图像。现在我有这个代码,它运行良好。视频开始,当它超过一个新的帧时,它会打开以显示图像。但我想要的是当视频结束时图像显示在与视频相同的帧上。我怎样才能做到这一点?

import cv2
cap = cv2.VideoCapture('video.mp4')
img = cv2.imread('test.jpg', -1)
if (cap.isOpened() == False):
    print("Error opening video stream or file")
while (cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        cv2.imshow('Frame', frame)
        if cv2.waitKey(25) == ord('q'):
            cv2.destroyAllWindows();
            break
    else:
        break
cap.release()
cv2.imshow('image', img)
k = cv2.waitKey(0) & 0xFF
if k == 27:
  cv2.destroyAllWindows()

【问题讨论】:

    标签: python opencv video


    【解决方案1】:

    你想在同一个窗口显示img,然后传入同名:

    import cv2
    cap = cv2.VideoCapture('video.mp4')
    img = cv2.imread('test.jpg', -1)
    if (cap.isOpened() == False):
        print("Error opening video stream or file")
    
    while (cap.isOpened()):
        ret, frame = cap.read()
        if ret == True:
            cv2.imshow('Frame', frame)
    
            # add waitKey for video to display
            cv2.waitKey(1)
    
            if cv2.waitKey(25) == ord('q'):
                # do not close window, you want to show the frame
                # cv2.destroyAllWindows();
                break
        else:
            break
    cap.release()
    cv2.imshow('Frame', img)   # note the difference
    k = cv2.waitKey(0) & 0xFF
    if k == 27:
      cv2.destroyAllWindows()
    

    【讨论】:

    • 工作就像一个魅力!谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-09-28
    • 2023-01-31
    • 1970-01-01
    • 2019-12-28
    • 2015-05-20
    • 2023-02-16
    • 2020-05-11
    • 1970-01-01
    相关资源
    最近更新 更多