在Python中使用regionprops [英] Using regionprops in Python
问题描述
我正在尝试分析灰度TIFF堆栈,其中给定的帧看起来像
像这样吗?
从skimage.io中的 导入imread,imshow从skimage.filters导入高斯,threshold_otsu从skimage导入度量导入matplotlib.pyplot作为plt原始= imread('https://i.stack.imgur.com/nkQpj.png')模糊=高斯(原始,sigma = .8)二进制=模糊>threshold_otsu(模糊)标签= measure.label(二进制)地块= {原始":原始,模糊":模糊,二进制":二进制,标签":标签}无花果,ax = plt.subplots(1,len(plots))对于n,(title,img)在enumerate(plots.items())中:cmap = plt.cm.gnuplot如果n == len(plots)-1 else plt.cm.grayax [n] .imshow(img,cmap = cmap)ax [n] .axis('off')ax [n] .set_title(title)plt.show(图)道具= measure.regionprops(标签)道具中的道具:print('Label:{}>>对象大小:{}'.format(prop.label,prop.area))
输出:
标签:1>>物件大小:37标签:2>>物件大小:66标签:3>>物件大小:1
I am trying to analyze greyscale TIFF stacks, in which a given frame will look like this. I filter it (using Gaussian blur), and then binarize it (using Otsu's method for threshold).
MATLAB code, which works great:
image_conncomp = bwconncomp(image_binary); # entire stack is held in image_binary
for i=1:image_conncomp.NumObjects
object_size = length(image_conncomp.PixelIdxList{i});
end
Each white spot in the example image is picked up, and its volume (in pixels) is pretty accurately given by object_size
.
Python code:
from skimage import measure
labels = measure.label(image_binary, background=1) # same image_binary as above
propsa = measure.regionprops(labels)
for label in propsa:
object_size = len(label.coords)
The Python code seems to work decently... except that most detected objects will have object_size
of 1 - 200, and then a couple will have a size of several thousand pixels.
What are these functions doing differently? I would be happy to try another approach in Python to get calculate object sizes, but I struggled to find another one. It'd be great to have a Python version of this code, if I could find a good substitute for Matlab's bwconncomp
function.
Something like this?
from skimage.io import imread, imshow
from skimage.filters import gaussian, threshold_otsu
from skimage import measure
import matplotlib.pyplot as plt
original = imread('https://i.stack.imgur.com/nkQpj.png')
blurred = gaussian(original, sigma=.8)
binary = blurred > threshold_otsu(blurred)
labels = measure.label(binary)
plots = {'Original': original, 'Blurred': blurred,
'Binary': binary, 'Labels': labels}
fig, ax = plt.subplots(1, len(plots))
for n, (title, img) in enumerate(plots.items()):
cmap = plt.cm.gnuplot if n == len(plots) - 1 else plt.cm.gray
ax[n].imshow(img, cmap=cmap)
ax[n].axis('off')
ax[n].set_title(title)
plt.show(fig)
props = measure.regionprops(labels)
for prop in props:
print('Label: {} >> Object size: {}'.format(prop.label, prop.area))
Output:
Label: 1 >> Object size: 37
Label: 2 >> Object size: 66
Label: 3 >> Object size: 1
这篇关于在Python中使用regionprops的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!