首页 > 解决方案 > 在 Python 中使用 regionprops

问题描述

我正在尝试分析灰度 TIFF 堆栈,其中给定的帧看起来像这样。我对其进行过滤(使用高斯模糊),然后对其进行二值化(使用 Otsu 的阈值方法)。

MATLAB 代码,效果很好:

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

示例图像中的每个白点都被拾取,其体积(以像素为单位)非常准确地由 给出object_size

Python代码:

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)

Python 代码似乎工作得很好……除了大多数检测到的对象将有object_size1 - 200 个,然后几个将有几千个像素的大小。

这些功能有什么不同?我很乐意在 Python 中尝试另一种方法来计算对象大小,但我很难找到另一种方法。如果我能找到一个很好的 Matlabbwconncomp函数替代品,那么拥有这段代码的 Python 版本会很棒。

在此处输入图像描述

标签: pythonmatlabimage-processingscikit-imagescipy.ndimage

解决方案


像这样的东西?

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))

输出:

情节

Label: 1 >> Object size: 37
Label: 2 >> Object size: 66
Label: 3 >> Object size: 1

推荐阅读