Please refer to this link first:
How to automatically detect and crop individual sprite bounds in sprite sheet?
In the example in the above link, the bounding boxes of the individual sprites do not overlap, so the code in the accepted answer works well, and all the sprites are extracted in individual files.
But supposed the sprites were packed differently such that their bounding boxes overlapped even thought the individual images did not overlap:
Then the above code would not work well at all, and just one file is output because the bounding boxes would intersect.
How would someone solve this problem using OpenCV?
Without using OpenCV, my approach would be to:
- Iterate through each sprite till I found a non-transparent sprite. Store it, and all connected non-transparent sprites as an individual set.
- Once no more connected pixels from the previous set, examine the remaining pixels in the rest of the image, ignoring the ones I have already seen and put in the above set. When I encounter a new non-transparent pixel I have not seen before, then repeat the same as the first step, but place these connected pixels in a new set.
- Like that repeat the process till there are no pixels left to examine.
- For each set of connected pixels, create a new image with the dimensions from a bounding box constructed by the farthermost pixels of that set. But include only the pixels from that connected set in the output image. Repeat for each set to create a new image for each connected set.
Instead of the approach I have described above, is there a method to use OpenCV to do this ? I can do it using my method, but I would like to learn more about OpenCV and what it is capable of, but I am just a beginner and still learning.


You can fill each contour with white and then generate an image with only that contour. After that, apply a regular cropping by getting the properties of the bounding rect. Here is the code:
And the results look nice as well, I'll just post the biggest one and smallest one as an example:
V2.0: make it work with both images
The first image, for some reason, has 255 on the green channel for all the background. Which is why only one big contour is detected. I added a few lines of code to pre-process the image and also to take into consideration the alpha channel. Here is the code:
I won't bother with adding images of the results again, as they are the same as before. However, here is an image of the first example with the green background I am talking about:
The code:
The image:
I went around this with
g[g==255] = 0