如何在 Python 中使用 cv2 显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34966541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can one display an image using cv2 in Python
提问by user1245262
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()
Because I can display the image using matplotlib, I know that I'm successfully reading it in.
因为我可以使用 matplotlib 显示图像,所以我知道我已经成功读取了它。
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.
我不明白为什么我创建的窗口并尝试使用 cv2 显示图像不起作用。从未出现过 cv2 窗口。奇怪的是,如果我创建第二个 cv2 窗口,则会出现“输入”窗口,但它只是一个空白/白色窗口。
What am I missing here?
我在这里缺少什么?
采纳答案by phev8
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.
因此,您的窗口可能会出现,但关闭速度非常快。