How to find the diameter of objects using image processing in Python?

前端 未结 2 516
生来不讨喜
生来不讨喜 2021-01-05 21:44

Given an image with some irregular objects in it, I want to find their individual diameter.

Thanks to this answer, I know how to identify the objects. Howeve

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-05 22:18

    You could use skimage.measure.regionprops to determine the bounding box of all the regions in your image. For roughly circular blobs the diameter of the minimum enclosing circle can be approximated by the largest side of the bounding box. To do so you just need to add the following snippet at the end of your script:

    from skimage.measure import regionprops
    
    properties = regionprops(labels)
    print 'Label \tLargest side'
    for p in properties:
        min_row, min_col, max_row, max_col = p.bbox
        print '%5d %14.3f' % (p.label, max(max_row - min_row, max_col - min_col))
    
    fig = plt.figure()
    ax = fig.add_subplot(111)    
    ax.imshow(np.ma.masked_array(labels, ~blobs), cmap=plt.cm.gist_rainbow) 
    ax.set_title('Labeled objects')
    plt.xticks([])
    plt.yticks([])
    for ri, ci, li in zip(r, c, range(1, nlabels+1)):
        ax.annotate(li, xy=(ci, ri), fontsize=24)
    plt.show()
    

    And this is the output you get:

    Label   Largest side
        1        106.000
        2         75.000
        3         79.000
        4         56.000
        5        161.000
        6         35.000
        7         47.000  
    

    Labeled objects

提交回复
热议问题