简体   繁体   English

如何在 OpenCV ( Python ) 视频的同一帧中显示图像?

[英]How can I show an Image in the same frame of a video in OpenCV ( Python )?

I am trying to show a video on cv2 and when the video is over I want to show and image.我正在尝试在 cv2 上显示视频,当视频结束时我想显示和图像。 Right now I have this code and it runs well.The video starts and when it's over a new frame it's opened to show the image.But what I want is the image to show on the same frame as the video when the video it's over.现在我有这个代码并且它运行良好。视频开始,当它在一个新的帧上时它被打开以显示图像。但我想要的是当视频结束时图像与视频显示在同一帧上。 How can I achieve this?我怎样才能做到这一点?

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()

You want to show img in the same window, then pass in the same name:你想在同一个窗口中显示 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM