fitting a circle to a binary image

前端 未结 5 964
Happy的楠姐
Happy的楠姐 2020-12-15 12:48

I have been using skim age\'s thresholding algorithms to get some binary mask. For example, I obtain binary images like this:

5条回答
  •  青春惊慌失措
    2020-12-15 13:40

    For someone looking to code Mark's suggestion in python, it is quite easy.

    collapsed = np.sum(binary_array, axis=0)
    # These indices will be already sorted
    indices = np.where(collapsed == collapsed.max())[0]
    c = indices[int(round((len(indices) - 1) / 2))]
    
    # Same for rows
    collapsed = np.sum(binary_array, axis=1)
    # These indices will be already sorted
    indices = np.where(collapsed == collapsed.max())[0]
    r = indices[int(round((len(indices) - 1) / 2))]
    
    # circle center is (r, c)
    

    This code takes care when your shape is not spherical and the collapsing along an axes can have multiple maxima. In that case, it takes the middle one (the one that can give you the largest radius when you fit the circle).

提交回复
热议问题