如何在Python中使用cv2显示图像 [英] How can one display an image using cv2 in Python
问题描述
我一直在使用代码来显示电影中的帧.代码的基本内容如下:
I've been working with code to display frames from a movie. The bare bones of the code is as follows:
import cv2
import matplotlib.pyplot as plt
# Read single frame avi
cap = cv2.VideoCapture('singleFrame.avi')
rval, frame = cap.read()
# Attempt to display using cv2 (doesn't work)
cv2.namedWindow("Input")
cv2.imshow("Input", frame)
#Display image using matplotlib (Works)
b,g,r = cv2.split(frame)
frame_rgb = cv2.merge((r,g,b))
plt.imshow(frame_rgb)
plt.title('Matplotlib') #Give this plot a title,
#so I know it's from matplotlib and not cv2
plt.show()
因为我可以使用matplotlib显示图像,所以我知道我已经成功读取了图像.
Because I can display the image using matplotlib, I know that I'm successfully reading it in.
我不明白为什么我创建窗口并尝试使用cv2显示图像不起作用.从未出现过cv2窗口.奇怪的是,如果我创建第二个cv2窗口,则会出现输入"窗口,但它只是一个空白/白色窗口.
I don't understand why my creation of a window and attempt to show an image using cv2 doesn't work. No cv2 window ever appears. Oddly though, if I create a second cv2 window, the 'input' window appears, but it is only a blank/white window.
我在这里想念什么?
推荐答案
据我所知,您做的差不多.缺少一件事:
As far as I can see, you are doing it almost good. There is one thing missing:
cv2.imshow('image',img)
cv2.waitKey(0)
所以您的窗口可能出现了,但是很快关闭了.
So probably your window appears but is closed very very fast.
这篇关于如何在Python中使用cv2显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!