python - 在 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_size
1 - 200 个,然后几个将有几千个像素的大小。
这些功能有什么不同?我很乐意在 Python 中尝试另一种方法来计算对象大小,但我很难找到另一种方法。如果我能找到一个很好的 Matlabbwconncomp
函数替代品,那么拥有这段代码的 Python 版本会很棒。
解决方案
像这样的东西?
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
推荐阅读
- python - Discord.py 每个用户的冷却时间不同
- python - 如何在 Python 3 中获取结果后重复此代码
- bamboo - 使用 REST API 从 Bamboo 拉取给定版本的构建细节
- javascript - for...of 循环奇怪的行为
- ios - iOS检测设备是否用作wifi热点
- javascript - D3.js,全球叶绿素/热图。使图例水平并在国家周围添加黑色边框
- reactjs - 日期过滤在反应表列过滤器中不起作用
- google-cloud-platform - 需要通过google cloud中的数据流作业访问项目A中项目B的存储桶(数据流作业失败)
- c - Dev C++ ld 返回 1 个退出状态(C 语言)
- gcc - 未找到命令“gcc”,但仍已安装