gpt4 book ai didi

python - 在 Python 中检测白色背景上的对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:11:37 24 4
gpt4 key购买 nike

我正在尝试使用 Python 来检测白色表面上有多少对象。在这篇文章的末尾可以找到一个示例图片。

我想知道我应该怎么做,主要是因为背景是白色的,而且大多数时候它被检测为前景。

基于本教程 (http://pythonvision.org/basic-tutorial),我现在在 Python 中拥有的内容使用多个库并将白色检测为对象,因此计数为 1,工具被检测为背景,因此被忽略:

dna = mahotas.imread('dna.jpeg')
dna = dna.squeeze()
dna = pymorph.to_gray(dna)


print dna.shape
print dna.dtype
print dna.max()
print dna.min()

dnaf = ndimage.gaussian_filter(dna, 8)
T = mahotas.thresholding.otsu(dnaf)
labeled, nr_objects = ndimage.label(dnaf > T)
print nr_objects
pylab.imshow(labeled)
pylab.jet()
pylab.show()

是否有任何选项可以将白色部分作为背景并将工具作为前景?

提前致谢!

示例图片: enter image description here

红前景蓝背景的分割图(少数工具合并没问题):

enter image description here

最佳答案

如果阴影不是问题

给定这个二进制图像,您可以在 mahotas ( http://mahotas.readthedocs.org/en/latest/labeled.html) 中标记图像。您还可以使用 skimage.morphology (它使用评论中提到的 ndlabel )。 http://scikit-image.org/docs/dev/auto_examples/plot_label.html

这些是连接组件算法的示例,并且是任何通用图像处理包中的标准。 ImageJ 也让这一切变得非常简单。

如果阴影有问题

Otsu 阈值处理返回一个值:像素亮度,您所做的就是保留所有比该阈值暗的像素。这种方法被你的阴影绊倒了,所以你需要尝试另一种分割算法,最好是进行局部分割的算法(即它单独分割图像的小区域。)

自适应或局部方法没有这个问题,并且非常适合您图像的阴影:

http://scikit-image.org/docs/dev/auto_examples/plot_threshold_adaptive.html#example-plot-threshold-adaptive-py

在 mahotas 中应该有其他分割方法,但我只了解 scikit-image。如果您想认真了解分割,请查看本文:https://peerj.com/preprints/671/

完全公开,这是我的论文。

关于python - 在 Python 中检测白色背景上的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28032722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com