Search code examples
pythonmatlabimage-processingcomputer-visionscikit-image

Skimage regionprops feature's(area,euler_number) dimensions not correct in Python


I have 1797 Mnist Images, for which I need to extract two features (FilledArea,EulerNumber). I know how to do it in Matlab. My feature matrix is having(and is correct) size of 1797*2 (1797 for each dimension) In Matlab

Code for matlab (working correctly)

for i = 1:2*N
    img = regionprops(BW(:,:,i),'FilledArea', 'Solidity');
    features(i, 1) = img.EulerNumber;
    features(i, 2) = img.FilledArea;
    clear img;
end

I want to do same thing in python with Skimage regionprops, but for 1797 images, I am getting 29350*2 features (29350 props for each features), which according to my understanding should be 1797*2

Code for python (not working correctly)

digits = datasets.load_digits()
label_img = digits.images
rps = regionprops(label_img, cache=False)
print(len([r.area for r in rps]))  #29350
print(len([r.euler_number for r in rps]))  #29350

What might be wrong with my approach? why am I having 29350 element for each feature instead of 1797?


Solution

  • Just like you need a for-loop in Matlab to compute the properties for each image, you need one in Python to do the same. Currently, you are computing the properties for a single 3D image of shape (1797, 8, 8), instead of 1797 2D images of shape (8, 8). Here is the somewhat equivalent Python code for what you are after:

    features = []
    for image in digits.images:
        labels = (image > 0).astype(int)  # only one object in the image
        props = regionprops(labels, image)[0]  # only one object
        features.append((props.euler_number, props.filled_area))