# Finding horizontal count of items in an image

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): 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: # 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: 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

## 1 Answer

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.

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