I'm trying to find the count of horizontal columns in an ordered image (a sprite image that is used to generate an animated image):

Sprited dino

From this image, I'm trying to generate a meta-data file like so:

"frames": 12, 
"cols": 6, 
"width": 102, 
"height": 89

Frames would be the total count of "elements" in the image, columns is the horizontal column count, and width/height is for the individual "element" and can be calculated, once you know the column count.

What I Have Already

I've been using numpy/scipy/python mainly since I am a Python guy, but not too experienced with scipy.

This answer showed me how to get the total count of elements in an image and got me this far ipython:

ipython count

Where I'm Stuck

On most simple images, the ndimage.label works well, but once the image gets too dense, it seems to fall apart.

I've tried using a kind of line-by-line average of the file: subtract 1 horizontal line, get the ndimage.label count of total elements in the picture, then keeping subtract the whole image to build buckets and calc the average std deviation for the image; which would be the horizontal count. But this still isn't accurate on dense images like this: http://asktherelic.com/priv/npc_bundle_of_joy__x1_talk_png_1354839566.png

Would anyone have suggestions for a better algorithm or counting method? Thanks!

For ease of hacking I have a zip with the ipython notebook and sample images available here: http://asktherelic.com/priv/sprite_sample.zip

And it looks something like this currently: http://asktherelic.com/priv/sprite_sample.html


Using any image processing library, here is the simple step

  1. Threshold invert with high value will give you each object as a contour.

  2. Now process each contour and find centre of mass.

  3. Then sort contour by co-ordinates.

  • 1
    $\begingroup$ Or just look for vertical lines of empty pixels and count how many of those there are $\endgroup$ – Aaron May 20 '14 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.