首页 文章

如何检测目标上的弹孔

提问于
浏览
10

我想知道如何使用Python和OpenCV检测目标上的弹孔 .

我无法画出周围的轮廓 . 到目前为止,我已经应用了一个阈值,我有以下结果(阈值后的图像和二进制AND):

这是原始图像:
enter image description here

我不知道应该采用哪种方法来检测弹孔并相应地计算得分 .

1 回答

  • 14

    您可以简单地使用一种非常简单的分割技术,称为 Color Segmentation ,您可以在其中对给定的RGB图像进行阈值处理,以获得二进制图像:

    img = cv2.imread('/Users/anmoluppal/Desktop/cAMDX.jpg')
    
    img_thresholded = cv2.inRange(img, (60, 60, 60), (140, 140, 140))
    

    enter image description here

    使用二进制图像上的打开操作可以删除二进制图像的噪声,如下所示:

    kernel = np.ones((10,10),np.uint8)
    opening = cv2.morphologyEx(img_thresholded, cv2.MORPH_OPEN, kernel)
    

    enter image description here

    现在你有一个清晰的弹孔图片,最后一部分是找到这些轮廓并在它们周围绘制一些圆/矩形以突出显示前景区域:

    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)
    

    enter image description here

相关问题