findContours()函数未返回适当数量的轮廓

问题描述 投票:-1回答:1

我正在使用Python 3.8.2openCV 4.3.0。我应该得到3时得到1个轮廓的答案。我尝试使用RETR_TREE而不是RETR_EXTERNAL。在这种情况下,它会给我8轮廓。

import cv2 as cv
def getCounters(img):
    contours,hierarchy=cv.findContours(img,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
    print("Number of Contours found = " + str(len(contours)))

这是输入图像:

https://i.stack.imgur.com/Pr9hp.png

这是输入的Canny图片:

https://i.stack.imgur.com/FhIyr.png

请提出必要的更改建议。

python-3.x opencv image-processing computer-vision opencv-contour
1个回答
0
投票

这是在Python / OpenCV中执行此操作的一种方法。您的问题是由于边缘的黑色轮廓,您正在从白色背景获取外部轮廓。一种方法是使用层次结构来获取第二级轮廓。我在这里使用的花药只是为了摆脱黑色边框。

  • 读取输入
  • 删除黑色边框并使其变为白色
  • 转换为灰色
  • 反转灰色
  • 阈值,因此对象在黑色背景上为白色
  • 获取轮廓并在输入以及黑色背景上绘制
  • 保存结果

输入:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread('shapes.png')
hh, ww = img.shape[:2]

# remove black border and add white border back
img2 = img[2:hh-2, 2:ww-2]
img2 = cv2.copyMakeBorder(img2, 2, 2, 2, 2, cv2.BORDER_CONSTANT, value=(255,255,255))

# convert to grayscale
gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

# invert
gray = 255 - gray

# threshold
thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)[1]

# get contours and draw on input and on black background
result1 = img.copy()
result2 = np.zeros_like(img)
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
for cntr in contours:
    cv2.drawContours(result1, [cntr], 0, (0,0,255), 1)
    cv2.drawContours(result2, [cntr], 0, (255,255,255), 1)

# save results
cv2.imwrite('shapes_modified.png',img2)
cv2.imwrite('shapes_thresh.png',thresh)
cv2.imwrite('shapes_result1.png',result1)
cv2.imwrite('shapes_result2.png',result2)

# show results  
cv2.imshow("img2", img2)
cv2.imshow("thresh", thresh)
cv2.imshow("result1", result1)
cv2.imshow("result2", result2)
cv2.waitKey(0)
cv2.destroyAllWindows()

黑色边框输入变成白色:

enter image description here

阈值图像:

enter image description here

输入轮廓:

enter image description here

黑色背景上的轮廓:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.