Home > other >  Detecting objects in images using SciKit with python
Detecting objects in images using SciKit with python

Time:10-08

I have an immage processing problem that im struggling to figure out a solution for the image. here is the image SourceImg Basically its a segmentation and counting problem using scikit image in python. Basically i have to write a psudeo code of how i would go about counting these "scissor" objects in a source image that i have. The scissors are surrounded by other different objects of different shape and size. Recently i have done a similar beginner problem to count the number of coins in an image. this one was much easier because all of the objects were of the same nature.

Could any of you help me with ideas of how to go about counting the scissors, seperating and isolating them from all of the other objects in the image. My thought process so far is to

  1. read in image
  2. convert to grayscale
  3. plot a histogram
  4. from this threshold preferqbley using otsu
  5. remove all unwanted objects that touc border using skimage clear_border

however unlike the coins which are simple and all nearly identical i dont know how to go about isolating the scissor objects. Is there any advanced segmentaqion techniques in skimage that could be used for this. Like i was thinking of blob but i dont think that will work here. If anyone could provide any insight please let me know i would be very grateful

CodePudding user response:

It depends how general you need your solution to be. In the image you showed, the scissors are the only objects that have two holes in them. We can use the skimage.measure.regionprops property euler_number, described in the documentation as:

Euler characteristic of the set of non-zero pixels. Computed as number of connected components subtracted by number of holes (input.ndim connectivity). In 3D, number of connected components plus number of holes subtracted by number of tunnels.

So, for scissors, that will be 1-2 = -1, whereas for solid objects it's 1 and for objects with 1 hole it's 1-1 = 0. So you can say:

from skimage import measure

objects = measure.label(borders_cleared)
props_list = measure.regionprops(objects)
num_scissors = 0
for props in props_list:  # one RegionProps object per region
    if props.euler_number == -1:
        num_scissors  = 1

When the segmentation itself is easy, as in the image you showed, then my strategy would always be to find a property or combination of properties in regionprops that allows me to distinguish the objects I'm interested from others. This could be size, elongation, roundness, ... Using the extra_properties= keyword argument, you can even compute other properties defined by any function you can imagine.

  • Related