【发布时间】:2014-03-03 02:09:12
【问题描述】:
我按照this page 的教程进行操作,但在执行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
没有错误消息。 star.jpg 是上述网页中的星星。 我正在使用 opencv 版本 2.4.8 和 Python 2.7。
drawContours 是否应该在我的屏幕上显示图像?如果是这样,我做错了什么?如果没有,如何显示图像?
谢谢
编辑:
添加以下行将显示图像:
cv2.imshow("window title", im)
cv2.waitKey()
waitKey() 是必需的,否则窗口将只显示灰色背景。根据this post,这是因为 waitKey() 告诉它开始处理 WM_PAINT 事件。
【问题讨论】:
-
drawContours(.) 将轮廓绘制到图像上。它不会在屏幕上显示。调用 drawContours(.) 后,您必须显示图像
im才能看到它。