您可以简单地使用一种非常简单的分割技术,称为Color Segmentation,您可以在其中对给定的 RGB 图像进行阈值化以获得二进制图像:
img = cv2.imread('/Users/anmoluppal/Desktop/cAMDX.jpg')
img_thresholded = cv2.inRange(img, (60, 60, 60), (140, 140, 140))
二值图像的噪声可以通过对二值图像的开运算来去除:
kernel = np.ones((10,10),np.uint8)
opening = cv2.morphologyEx(img_thresholded, cv2.MORPH_OPEN, kernel)
现在你对弹孔有了一些清晰的了解,最后一部分是找到这些轮廓并在它们周围画一些圆形/矩形以突出前景区域:
contours, hierarchy = cv2.findContours(opening.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print len(contours)
for contour in contours:
(x,y),radius = cv2.minEnclosingCircle(contour)
center = (int(x),int(y))
radius = int(radius)
cv2.circle(img,center,radius,(0,255,0),2)
# labelling the circles around the centers, in no particular order.
position = (center[0] - 10, center[1] + 10)
text_color = (0, 0, 255)
cv2.putText(img, str(i + 1), position, cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 3)