Shan
Shan

Reputation: 19243

connected components attributes in python

I want to compute statistics on the connected components of a binary image. In matlab we have

Shape Measurements

    'Area'              'EulerNumber'       'Orientation'               
    'BoundingBox'       'Extent'            'Perimeter'          
    'Centroid'          'Extrema'           'PixelIdxList' 
    'ConvexArea'        'FilledArea'        'PixelList'
    'ConvexHull'        'FilledImage'       'Solidity' 
    'ConvexImage'       'Image'             'SubarrayIdx'            
    'Eccentricity'      'MajorAxisLength' 
    'EquivDiameter'     'MinorAxisLength' 

Is there any equivalent in python?

Thanks

Upvotes: 1

Views: 2168

Answers (3)

masoud anaraki
masoud anaraki

Reputation: 67

as I know when Matlab try to find 'PixelIdxList' consider the array as a 1D array and then count the indexes but in regionprops[i].coords bring back an array of arrays(2D array) that each inner arrays have two-element and by using them you can find the index values.

sth like this x = [[x1,y1],[x2,y2],....,[xn,yn]].I try to implement the exact code of Matlab you can use if you want:

import skimage.measure as skme
import numpy as np

 
state = skme.regionprops(image)
for j in range(np.size(state)):
    s1 = state[j].coords
    flg = 0
    if np.size(s1,axis=0)>1:
        flg = 1
        InrList = []
        for k in range(np.size(s1,axis=0)):
            x1, y1 = state[j].coords[k]
            InrList.append(image[x1,y1])
        PixelIdxList.append(InrList)
    if flg == 0:
        x1, y1 = state2[j].coords[0]
        PixelIdxList.append(iamge[x1,y1])

# PixelIdxList is exact out put in Matlab
#STATS = regionprops(image,'PixelIdxList');          matlab
''' EACH ONE TEACH ONE '''

Upvotes: 0

George Liu
George Liu

Reputation: 216

Just answered a similar question. Use the regionprops function in scikit-image to get the CC properties in Python.

from scipy.ndimage.measurements import label
from skimage.measure import regionprops
label = label(img)
props = regionprops(label)
# get centroid of second object
centroid = props[1].centroid
# get eccentricity of first object
ecc = props[0].eccentricity

The shape measurements output by regionprops include all the features listed above in the question. The 'PixelIdxList' equivalent in Python is the coords property output by regionprops.

Upvotes: 2

TheBlackCat
TheBlackCat

Reputation: 10298

I think openCV's cv2 interface is what you are probably looking for.

Upvotes: 0

Related Questions