Identify a shape inside another shape using AForge.Net
Question: I am using the AForge.Net library to do some basic image processing stuff as
part of a project. I have found it trivial to identify individual geometric
shapes in an image (like a square, circle etc.,). However when I have an image
like ![image](http://i.imgur.com/crs0Y.jpg) , the program can only identify
the outer circle. I would want it to be recognized as a circle and a line.

Similarly another example is, ![image1](http://i.imgur.com/Vp81V.jpg) , where
the program identifies only a square but I need it to recognize it as a square
and a circle.

I guess this library itself is outdated and no longer supported, but I will
really appreciate some help! I have found this to be a really easy library to
use but if my requirement cannot be met with it I am open to other libraries
as well. (in Java, C# or Python). Thanks!


Answer: That's an easy task using `Python`  
You'll need libraries like `numpy` and `scipy.ndimage` to be installed,and
with only `scipy.ndimage` you can extract any shape on a black background .  
So if your image is in white background you need to invert it first which is
an easy job.

    
    
    import scipy.ndimage  
    from scipy.misc import imread           # so I can read the image as a numpy array
    
    img=imread('image.png')        # I assume your image is a grayscale image with a black bg.  
    labeled,y=scipy.ndimage.label(img)   # this will label all connected areas(shapes).  
                                             #y returns how many shapes??  
    
    shapes=scipy.ndimage.find_objects(labeled)         
    # shapes returns indexing slices for ever shape  
    # So if you have 2 shapes in your image,then y=2.
    # to extract the 1st shape you do like this.  
    first_shape=img[shapes[0]]          # that's is a numpy feature if you are familiar with numpy .
    second_shape=img[shapes[1]]  
    

After extracting your individual shapes you can actually do some math work to
identify **what it is?** (e.g circularity ratio >> you can google it , it's
helpful in your case)

