Python opencv drawContours 不显示任何内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22137511/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:20:34  来源:igfitidea点击:

Python opencv drawContours does not show anything

pythonopencv

提问by King Long Tse

I followed the tutorial at this pagebut nothing seems to happen when the line cv2.drawContours(im,contours,-1,(0,255,0),3)is executed. I was expecting to see star.jpg with a green outline, as shown in the tutorial. Here is my code:

我在此页面上遵循了教程,但在cv2.drawContours(im,contours,-1,(0,255,0),3)执行该行时似乎没有任何反应。我期待看到带有绿色轮廓的 star.jpg,如教程中所示。这是我的代码:

import numpy as np
import cv2

im = cv2.imread('C:\Temp\ip\star.jpg')
print im.shape #check if the image is loaded correctly
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,contours,-1,(0,255,0),3)
pass

There are no error messages. star.jpg is the star from the above mentioned webpage. I am using opencv version 2.4.8 and Python 2.7.

没有错误消息。star.jpg 是上述网页中的明星。我使用的是 opencv 2.4.8 版和 Python 2.7。

Is drawContours supposed to show an image on my screen? If so, what did I do wrong? If not, how do I show the image?

drawContours 是否应该在我的屏幕上显示图像?如果是这样,我做错了什么?如果没有,我如何显示图像?

Thanks

谢谢

Edit:

编辑:

Adding the following lines will show the image:

添加以下几行将显示图像:

cv2.imshow("window title", im)
cv2.waitKey()

waitKey() is needed otherwise the window will just show a gray background. According to this post, that's because waitKey() tells it to start handling the WM_PAINT event.

需要 waitKey() 否则窗口将只显示灰色背景。根据这篇文章,这是因为 waitKey() 告诉它开始处理 WM_PAINT 事件。

回答by patel deven

i too had the same problem. The thing is it shows, but too dark for our eyes to see. Solution: change the colour from (0,255,0) (for some weird reason, i too had give exactly the same color!) to (128,255,0) (or some better brighter colour)

我也有同样的问题。问题是它显示出来,但太暗了,我们的眼睛看不到。解决方案:将颜色从 (0,255,0)(出于某种奇怪的原因,我也给出了完全相同的颜色!)到 (128,255,0)(或一些更好的更亮的颜色)

回答by Daniel Andersen

I had the same issue. I believe the issue is that the underlying image is 1-channel rather than 3-channel. Therefore, you need to set the color so it's nonzero in the first element (e.g. (255,0,0)).

我遇到过同样的问题。我认为问题在于底层图像是 1 通道而不是 3 通道。因此,您需要设置颜色,使其在第一个元素中不为零(例如 (255,0,0))。

回答by Derek Janni

You have to do something to the effect of:

你必须做一些事情来达到以下效果:

cv2.drawContours(im,contours,-1,(255,255,0),3)
cv2.imshow("Keypoints", im)
cv2.waitKey(0)

回答by Shivam Sahil

I guess your original image is in gray bit plane. Since your bit plane is Gray instead of BGR and so the contour is not showing up. Because it's slightly black and grey which you cannot distinguish. Here's the simple solution [By converting the bit plane]:

我猜你的原始图像是灰色位平面。由于您的位平面是灰色而不是 BGR,因此轮廓未显示。因为它有点黑色和灰色,你无法区分。这是简单的解决方案[通过转换位平面]:

im=cv2.cvtColor(im,cv2.COLOR_GRAY2BGR)
cv2.drawContours(im,contours,-1,(0,255,0),3)