小编Ham*_*msi的帖子

如何在 OpenCV - Python 中获得没有冗余的内部轮廓点

我是 OpenCV 新手,问题是我需要获取所有轮廓点。在 findContours 方法中设置 cv2.RETR_TREE 模式很容易。问题是这样会返回多余的坐标。因此,例如,在这个多边形中,我不想得到这样的轮廓点:

绿色是找到的轮廓(3),红色是根据这些轮廓找到的点

但像这样: 在此输入图像描述

因此,根据第一张图像,绿色是使用 RETR_TREE 模式检测到的轮廓,而点 1-2、3-5、4-6... 是多余的,因为它们彼此非常接近。我需要将这些冗余点组合成一个,并将其附加到 customContours 数组中。目前,我只有第一张图片的代码,设置点之间的距离和点坐标:

def getContours(img, minArea=20000, cThr=[100, 100]):
  font = cv2.FONT_HERSHEY_COMPLEX
  imgColor = img
  imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1)
  imgCanny = cv2.Canny(imgBlur, cThr[0], cThr[1])
  kernel = np.ones((5, 5))
  imgDial = cv2.dilate(imgCanny, kernel, iterations=3)
  imgThre = cv2.erode(imgDial, kernel, iterations=2)
  cv2.imshow('threshold', imgThre)
  contours, hierachy = cv2.findContours(imgThre, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

  customContours = []
  for cnt in contours:
    area = cv2.contourArea(cnt)
    if area > minArea:
        peri = cv2.arcLength(cnt, …
Run Code Online (Sandbox Code Playgroud)

opencv computer-vision python-3.x canny-operator

2
推荐指数
1
解决办法
1352
查看次数