简体   繁体   English

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

[英]Detect objects on a white background in Python

I'm trying to use Python to detect how many objects are on a white surface. 我正在尝试使用Python来检测白色表面上有多少个对象。 An example image is found at the end of this post. 在这篇文章的最后找到了一个示例图像。

I'm wondering how I should do this, mainly because the background is white and most of the time it gets detected as foreground. 我想知道我应该怎么做,主要是因为背景是白色的,大多数时候它被检测为前景。

What I have now in Python based on this tutorial ( http://pythonvision.org/basic-tutorial ) uses several libraries and detects the white as the object so count is 1, the tools get detected as background and thus are ignored: 基于本教程( http://pythonvision.org/basic-tutorial )我现在在Python中使用了几个库,并将白色检测为对象,因此count为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()

Are there any options for getting the white part as background and the tools as foreground? 是否有任何选项可以将白色部分作为背景,将工具作为前景?

Thanks in advance! 提前致谢!

Example image: 示例图片: 在此输入图像描述

The segmented image where red is foreground and blue background (the few tools merging is not a problem): 分割图像,其中红色是前景和蓝色背景(合并的几个工具不是问题):

在此输入图像描述

If the shadow is not a problem 如果阴影不是问题

You can label the images in mahotas ( http://mahotas.readthedocs.org/en/latest/labeled.html ) given this binary image. 在给定此二进制图像的情况下,您可以在mahotas( http://mahotas.readthedocs.org/en/latest/labeled.html )中标记图像。 You can also use skimage.morphology (which uses ndlabel as was mentioned in comments). 您还可以使用skimage.morphology(使用评论中提到的ndlabel)。 http://scikit-image.org/docs/dev/auto_examples/plot_label.html http://scikit-image.org/docs/dev/auto_examples/plot_label.html

These are examples of connect-component algorithms and are standard in any general image processing package. 这些是连接组件算法的示例,并且是任何通用图像处理包中的标准。 ImageJ also makes this quite simple. ImageJ也很简单。

If the shadow is a problem 如果阴影是个问题

Otsu thresholding returns a single value: a pixel brightness, and all you're doing is keeping all pixels that are dimmer than this threshold. Otsu阈值返回单个值:像素亮度,而您所做的只是保持所有像素都比此阈值更暗。 This method is getting tripped up by your shadows, so you need to try another segmentation algorithm, preferably one that does local segmentation (IE it segments small regions of the image individually.) 这种方法会被你的阴影绊倒,所以你需要尝试另一种分割算法,最好是一种进行局部分割的算法(IE可以单独分割图像的小区域)。

Adaptive or local methods don't have this problem and would be really well-suited to your image's shadows: 自适应或本地方法没有这个问题,并且非常适合您的图像阴影:

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

In mahotas there should be other segmentation methods but I'm only knowledgeable about scikit-image. 在mahotas应该有其他分割方法,但我只知道scikit-image。 If you want a serious overview on segmentation, check out this paper: https://peerj.com/preprints/671/ 如果您需要对分段进行认真的概述,请查看以下文章: https//peerj.com/preprints/671/

Full disclosure, it's my paper. 完全披露,这是我的论文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM