如何在python的每个countour上绘制边界框

时间:2018-08-29 06:56:47

标签: python opencv3.0 bounding-box

我是python的新手,我想尝试的是读取图像并在其中找到轮廓列表。然后,我需要在图像上的所有轮廓上绘制一个边界矩形框,这是我找不到解决方案的地方。请帮助我在轮廓上绘制该框。

img = cv2.imread('E:/test images/2.jpg')

imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#ret,thresh = cv2.threshold(imgray,127,255,0)
thresh = cv2.adaptiveThreshold(imgray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,25,2)

cv2.imshow("New",thresh)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContours(img, contours, -1, (0,255,0), 1)

x,y,w,h = cv2.boundingRect(contours)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

cv2.imshow('answer',img)
cv2.waitKey(0)enter code here`

0 个答案:

没有答案