5

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.

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?

Thanks in advance!

Example image: enter image description here

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

enter image description here

luispedro
  • 6,934
  • 4
  • 35
  • 45
E. V. d. B.
  • 815
  • 2
  • 13
  • 30

1 Answers1

5

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

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

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

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.

Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
  • Good information, even though the thresholding method was not in question. `ndimage.label` uses connected components algorithm i think +1 – M4rtini Jan 19 '15 at 20:33
  • Thanks, I updated the answer to talk about connected components rather than dwell on just thresholding. I didn't read carefully and see that he said shadows were not a problem. – Adam Hughes Jan 19 '15 at 20:47
  • I'm actually in the process of writing a paper myself about image processing in Python! Your info will greatly help, thanks a lot!! – E. V. d. B. Jan 19 '15 at 22:27
  • Awesome, good luck! You should check out the mahotas and scikit-image papers, they were both recently published in Journal of Open Research Software and PeerJ, respectively. – Adam Hughes Jan 19 '15 at 22:59
  • Thanks! I am struggling to get scikit-image installed at this moment on my Windows machine, can't seem to find what to do with the .whl file ... – E. V. d. B. Jan 20 '15 at 13:19
  • Got scikit-image to work, and the adaptive thresholding example worked perfectly, but when I try inputtin my own image it doesn't work as well, in fact the adaptive thresholding is just black, what could be the poroblem here? http://i.imgur.com/uYnj6af.png – E. V. d. B. Jan 20 '15 at 13:56
  • On windows, I always use enthought canopy as the primary python dist. because it makes installing a breeze. You might want to to contact their mailing list. Try the adaptive function with your gray-converted image- it might not like r,g,b images. If that doesn't work, you may have to contact them on the their mailing list. – Adam Hughes Jan 20 '15 at 16:38
  • Yerah I tried it with the grayscale image but it just turns all black :( – E. V. d. B. Jan 21 '15 at 13:39