Computing only a specific property in python regionprops

I use the measure.regionprops method, available in scikit-image, to measure the properties of connected components. It computes a bunch of properties ( Python-regionprops ). However, I just need the area of ​​each connected component. Is there a way to calculate only one property and save the calculations?

+6
source share
3 answers

There seems to be a more direct way to do the same using regionpropsc cache=False. I generated tags using skimage.segmentation.slicc n_segments=10000. Then:

rps = regionprops(labels, cache=False)
[r.area for r in rps]

regionprops documentation , cache=False , . %%time Jupyter, 166 cache=False 247 cache=True, , , .

.

%%time
ard = np.empty(10000, dtype=int)
for i in range(10000):
   ard[i] = size(np.where(labels==0)[1])

34,3 .

, skimage , slic:

import numpy as np
import skimage
from skimage.segmentation import slic
from skimage.data import astronaut

img = astronaut()
# `+ 1` is added to avoid a region with the label of `0`
# zero is considered unlabeled so isn't counted by regionprops
# but would be counted by the other method.
segments = slic(img, n_segments=1000, compactness=10) + 1

# This is just to make it more like the original poster 
# question.
labels, num = skimage.measure.label(segments, return_num=True)

OP , :

%%time
area = {}
for i in range(1,num + 1):
    area[i + 1] = np.size(np.where(labels==i)[1])

CPU times: user 512 ms, sys: 0 ns, total: 512 ms Wall time: 506 ms

regionprops:

%%time
rps = skimage.measure.regionprops(labels, cache=False)
area2 = [r.area for r in rps]

CPU times: user 16.6 ms, sys: 0 ns, total: 16.6 ms Wall time: 16.2 ms

, :

np.equal(area.values(), area2).all()

True

, , , , regionprops .

+6

regionprops , , , . label, , . ,

labels,num=label(image, return_num=True)
for i in range(num):
    area[i]=size(np.where(labels==i)[1])

.

+1

@optimist

non-regionprops . -

import numpy as np
from skimage.measure import label, regionprops
import matplotlib.pyplot as plt

arr = np.array([[1,0,1,0,0,0,1],
                [1,1,1,0,0,0,1],
                [0,1,1,0,0,0,1],
                [0,1,1,0,0,1,1],
                [0,0,0,0,1,1,1],
                [0,0,0,1,1,1,1],
                [1,0,0,1,1,1,1],
                [1,0,0,1,1,1,1],
                [1,0,0,1,1,1,1]])

area = {}
labels, num = label(arr, return_num=True)
for i in range(num):
    print(i)
    area[i]=np.size(np.where(labels==i)[1])
    print(area[i])

plt.imshow(labels)
plt.show();

enter image description here

rps = regionprops(labels, cache=False)
[r.area for r in rps]

Out: [9, 24, 3]
0

All Articles