Coffee beans separation algorithm

后端 未结 5 757
日久生厌
日久生厌 2020-12-23 15:36

What is proper algorithm for separating (counting) coffee beans on binary image? Beans can touch and partially overlap.


(source: beucher at cmm.ensmp.fr)

5条回答
  •  一整个雨季
    2020-12-23 16:07

    Here is some code (in Python) that will give you a baseline. Count the number of black pixels and divide into the area accounting by how many circles of average size can be packed into a square of your size. The has the virtue of being the simplest possible thing you can do.

    If a given method is not on average more accurate than this, then you need a better method. BTW I'm getting around 85% accuracy, so your 95% is not out of the question.

    import Image
    
    im = Image.open('ex2a.gif').convert('RGB')
    (h,w) = im.size
    print h,w
    num_pixels = h*w
    print num_pixels
    black_pixels = 0
    for i in range(h):
        for j in range(w):
            q = im.getpixel((i,j)) 
            if q[0]<10 and q[1]<10 and q[2]<10:
                black_pixels = black_pixels + 1
                im.putpixel((i,j),(255,0,0))
    r = 15
    unpackable = (h/(2*r))*(w/(2*r))*((2*r)**2 - 3.14*r**2)
    print 'unpackable:',unpackable
    print 'num beans:',round((num_pixels-2*unpackable)/750.0)
    im.save('qq.jpg')
    

提交回复
热议问题