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

2023-09-11 02:36:40 作者:分开不是尽头

我想使用Python来检测多少个对象都在一个白色的表面。一个例子图像在这篇文章的末尾找到。

I'm trying to use Python to detect how many objects are on a white surface. 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.

我现在已经在Python基于此教程( http://pythonvision.org/basic-tutorial)使用几个库并检测白色作为对象所以计数为1时,工具获得检测为背景,因而被忽略:

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:

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?

在此先感谢!

示例图片:

Example image:

的划分图像,其中红色是前景和蓝色背景(少数工具合并是不成问题的):

The segmented image where red is foreground and blue background (the few tools merging is not a problem):

推荐答案

您可以在mahotas标记的图像( http://mahotas.readthedocs.org/ EN /最新/ labeled.html )鉴于此二值图像。您也可以使用skimage.morphology(使用ndlabel作为中提到的评论)。 http://scikit-image.org/docs/dev/auto_examples/plot_label.html

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. You can also use skimage.morphology (which uses ndlabel as was mentioned in comments). http://scikit-image.org/docs/dev/auto_examples/plot_label.html

这些是连接组件算法的例子,并且在任何一般的图像处理包标准。 ImageJ的也使得这个很简单。

These are examples of connect-component algorithms and are standard in any general image processing package. ImageJ also makes this quite simple.

大津阈值返回一个值:一个像素的亮度,而你正在做的是保持所有比此阈值暗的像素。这个方法是越来越绊倒你的影子,所以你需要尝试另一种分割算法,preferably一个没有地方分割(即,它部分图像的小区域独立。)

Otsu thresholding returns a single value: a pixel brightness, and all you're doing is keeping all pixels that are dimmer than this threshold. 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.)

自适应或本地方法就没有这个问题,将是非常非常适合于图像的阴影:

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

在mahotas应该有其他的分割方法,但我只有熟知scikit形象。如果你想在分割严重的概述,请查看本文: https://peerj.com/preprints / 671 /

In mahotas there should be other segmentation methods but I'm only knowledgeable about scikit-image. If you want a serious overview on segmentation, check out this paper: https://peerj.com/preprints/671/

充分披露,这是我的论文。

Full disclosure, it's my paper.