Detect objects on a white background in Python

后端 未结 1 486
眼角桃花
眼角桃花 2021-01-03 08:47

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,

1条回答
  •  囚心锁ツ
    2021-01-03 09:27

    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.

    0 讨论(0)
提交回复
热议问题