【发布时间】: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)
这是具有给定轮廓和样本的示例输入图像和输出图像
原图:
输出图像:
如您所见,我得到的是边界框的轮廓和中心,而不是形状的轮廓和中心。
知道如何解决这个问题吗?
【问题讨论】: