在图像上找到硬币

时间:2015-03-24 18:28:58

标签: opencv computer-vision geometry hough-transform scikit-image

我试图在不同的图像上找到硬币并标记它们的位置。硬币总是完美的圆圈(不是椭圆形),但它们可以触摸甚至重叠。 Here是一些示例图像,以及我尝试的结果(使用skimage及其输出的Python脚本),但它似乎表现不佳。

剧本:

def edges(img, t):
    @adapt_rgb(each_channel)
    def filter_rgb(image):
        sigma = 1
        return feature.canny(image, sigma=sigma, low_threshold=t/sigma/2, high_threshold=t/sigma)

    edges = color.rgb2hsv(filter_rgb(img))
    edges = edges[..., 2]
    return edges

images = io.ImageCollection('*.bmp', conserve_memory=True)

for i, im in enumerate(images):
    es = edges(im, t=220)
    output = im.copy()
    circles = cv2.HoughCircles((es*255).astype(np.uint8), cv2.cv.CV_HOUGH_GRADIENT, dp=1, minDist=50, param2=50, minRadius=0, maxRadius=0)

    if circles is not None:
        circles = np.round(circles[0, :]).astype("int")

        for (x, y, r) in circles:
            cv2.circle(output, (x, y), r, (0, 255, 0), 4)
            cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

    # now es is edges
    # and output is image with marked circles

一些示例图像,检测到边缘和圆圈:

我正在使用canny边缘检测&霍夫变换,这是检测圆圈的最常用方法。但是,使用相同的参数,它在某些照片上几乎找不到任何内容,并在其他照片上找到太多的圆圈。

你能否就如何做得更好给我任何指示和建议?

2 个答案:

答案 0 :(得分:2)

我最终使用dlib's object detector并且效果非常好。探测器可以很容易地应用于探测任何类型的物体。有关相关讨论,请参阅reddit上的the question topic

答案 1 :(得分:1)

嗯,我会在canny结果中做一些形态学操作,比如说 作为关闭和开放操作: http://en.wikipedia.org/wiki/Mathematical_morphology

我还建议你看看分水岭计划。应用的 直接进入图像渐变,然后对其进行Hough变换。 http://en.wikipedia.org/wiki/Watershed_%28image_processing%29

相关问题