Search code examples
c#image-processingtransparencyphotoshop

How to split an image based on transparent areas


I have searched the internet thoroughly but couldn't find a solution

Here what i want

This is my image

enter image description here

This is how it looks with 0 transparency when i have selected

enter image description here

So programmatically i want to split image into 6 pieces. Each one containing one of the egg with 0 transparent area left

How can i do that?

My preferred solutions based on c# or photoshop script but i am open to all solutions

An example output

enter image description here enter image description here


Solution

  • To solve this problem for any image size, egg size, orientation, position, count I suggest to use the following approach:

    Load the image file.

    Extract the alpha channel (this contains the transparency information)

    find the egg blobs (blob search/analysis, region labelling, connected components, countless names for this method)

    get the bounding boxes of those blobs

    crop the sub images using those bounding boxes

    This can be achieved with most image processing librarys. If you prefer C#, give EmguCV a try. Or use websearch to find others.

    http://www.emgu.com/wiki/files/3.1.0/document/html/e13fa7a9-5eee-b46c-4b65-ff3e7e427719.htm