【问题标题】:How to accurately get the contour of a shape in an image?如何准确获取图像中形状的轮廓?
【发布时间】:2019-06-23 08:32:20
【问题描述】:

我有黑白 93x94 像素的图像,其中包含几何形状。我正在尝试找到形状轮廓以及它们的中心

这是我目前尝试过的

import cv2
import imutils

img = cv2.imread("input.jpg", cv2.IMREAD_GRAYSCALE)
blurred = cv2.GaussianBlur(img, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
for c in cnts:
    # compute the center of the contour
    M = cv2.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])

    # draw the contour and center of the shape on the image
    cv2.drawContours(img, [c], -1, (0, 255, 0), 2)
    cv2.circle(img, (cX, cY), 7, (0, 0, 0), -1)
    #cv2.putText(img, "center", (cX - 20, cY - 20),
        #cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)

    # show the image
    cv2.imshow('output image',img)
    cv2.waitKey(0)

这是具有给定轮廓和样本的示例输入图像和输出图像

原图:

输出图像:

如您所见,我得到的是边界框的轮廓和中心,而不是形状的轮廓和中心。

知道如何解决这个问题吗?

【问题讨论】:

    标签: python image contour


    【解决方案1】:

    经过模糊和阈值处理后,图像几乎是全白的。

    我不确定我会从中得到什么。在这里使用 160 的值会更好

    thresh = cv2.threshold(blurred, 160, 255, cv2.THRESH_BINARY)[1]
    

    下一个问题是使用RETR_EXTERNAL,因为

    仅检索极端外部轮廓。

    使用RETR_TREE 似乎是正确的

    检索所有轮廓并重建嵌套轮廓的完整层次结构。

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 2021-09-06
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多