Home > OS >  Is there any algorithm that can automatically detect the rows and columns of sprite sheets?
Is there any algorithm that can automatically detect the rows and columns of sprite sheets?

Time:09-28

sprite sheet sample

Is there any algorithm that can automatically detect the rows and columns of sprite sheets like the one above? To make it easier for me to know the size and position of each grid for the animation display.

CodePudding user response:

Yes! This is an ideal use case for the Fourier transform. If you squish the image down to 1 pixel height, take just the alpha channel, and do a Fourier transform on it, you will see a frequency peak at a wavelength corresponding to the number of pixels per sprite. You can do the same thing vertically. This will be robust even with closely packed sprites, as long as they have some amount of outline to extract.

If the sprites have irregular width/height but are separated by transparent regions and are in distinct bounding boxes, you can instead pick a random non-transparent pixel and greedily grow a bounding box outward from it until the bounding box has only transient pixels at its outline, and then mark those pixels as “visited” and do the same with an unvisited pixels until you’ve visited all non-transparent pixels.

  • Related