cv2.drawContours为什么没有出现?

时间:2017-02-07 04:11:57

标签: python opencv

您好我是Python的新手,我正在尝试获取图像中的边界矩形。我做了以下事情:

import cv2 
from skimage import io
import numpy as np
img = cv2.imread('artelab.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv2.moments(cnt)
print M
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img,[box],0,(0,0,255),2)

没有显示但我没有收到错误消息。有什么问题?

1 个答案:

答案 0 :(得分:0)

添加for循环后,我获取了所有对象周围的边界框。谢谢

import cv2
from skimage import io
import numpy as np

img = cv2.imread('paper_artelab.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
numbContours=contours.__len__()

for x in range(0,numbContours):
    cnt = contours[x]
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(img,[box],0,(0,0,255),2)
io.imshow(img)