Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Extracting pixels coordinates from a bitmap

Credits go to Thomas Fjellstrom and verthex for helping out!
This thread is locked; no one can reply to it. rss feed Print
Extracting pixels coordinates from a bitmap
Paul whoknows
Member #5,081
September 2004
avatar

From time to time I find myself drawing trajectories in MS Paint and then writting down every pixel coordinates in the code of my games, doing this by hand really pisses me off, so before I start to write a little tool to automatize this task I wanted to know if someone knows if there is an application already written which I could use for this purpose, or some advice to write my own.

For example, the program should read the yellow pixels and write a text file with points coordinates, this points should be sorted (from left to right, or top to bottom).

{"name":"599509","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/0\/103020fea8dfc5b24ec9fc4bb55c0e73.png","w":500,"h":296,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/0\/103020fea8dfc5b24ec9fc4bb55c0e73"}599509

I am sure there must be some application already written for things like these!!! I don't want to reinvent it!

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

verthex
Member #11,340
September 2009
avatar

This is the approach I would take assuming there is no software for it, basically you can extrapolate all the yellow points that are the same color, within a given area of pixels (tolerance).

So the steps would be,

1) import image into allegro
2) use a function that allows the user to click on that yellow line ( anywhere on it)
3) create another function that can check if there are contiguous points of the same color, basically it would check the perimeter of the point (based on tolerance,which means the number of square pixels around the point, I'd say 3 at the most), and save that coordinate into a vector. The function would need to keep track of directions that are found also. Like the first point would notice there are two directions to run across (if you choose the center of the yellow line as the starting point).
*You could ideally just figure out the coordinate of the left most yellow pixel and not worry about that though, I'd take that approach.

4) you created a vector that has the points extrapolated and you go into GNUplot and fit the thing with a polynomial. But you can just Google for a gnuplot polynomial fit and you'll find the formula.

Thomas Fjellstrom
Member #476
June 2000
avatar

For example, the program should read the yellow pixels and write a text file with points coordinates, this points should be sorted (from left to right, or top to bottom).

That would be pretty simple to whip up if all you want is all pixels with a given color.

Though taking a look at your example image, you probably want to flatten that line a bit ;)

I personally don't know of any pre made tools to do this. Though if one existed, and could actually create a "path" of the lines found in the image, that would be better than just the pixel coordinates. Shape/Line extraction tends to hurt my brain, so I haven't managed to code it fully yet.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

verthex
Member #11,340
September 2009
avatar

I used The Gimp to make the image I attached, its a gif.
to make it
1) Import your image into The Gimp.
2) Zoom to about 200% under the view toolbar.
3) Under select toolbar use select by color. Shift+O.
4) Select the yellow line.
5) Once its highlighted, CTRL+C,and and paste as a new image or just press SHIFT+CTRL+V

and save as somename.gif

This should make it easier to extrapolate the coordinates cause now you should have a transparent gif.

EDIT: You need to find ImageMagick and download it, you can convert the gif I uploaded into a jpeg and follow these instructions

#SelectExpand
1Here is an example: 2 3#create heart shaped image: 4convert -font WebDings -pointsize 24 label:Y \ 5+trim +repage -negate -bordercolor black -border 10 heart_mask.gif 6 7 8#create binary edge 9convert heart_mask.gif -edge 1 -threshold 0 heart_mask_edge.gif 10 11 12# list coordinates of only white edge pixels 13convert heart_mask_edge.gif txt:- | sed -n 's/^\(.*\):.*white$/\1/p' 1413,10 1514,10 1615,10 1722,10 1823,10 1924,10 2011,11 2112,11 2213,11 2314,11 2415,11 2516,11 2617,11 2720,11 2821,11 2922,11 3023,11 3124,11 3225,11 3326,11 3411,12 3512,12 3616,12 3717,12 3818,12 3920,12 4021,12 4125,12 4226,12 4310,13 4411,13 4517,13 4618,13 4719,13 4820,13 4926,13 5027,13 5110,14 5211,14 5326,14 5427,14 5510,15 5611,15 5726,15 5827,15 5910,16 6011,16 6126,16 6227,16 6311,17 6412,17 6525,17 6626,17 6711,18 6812,18 6913,18 7024,18 7125,18 7226,18 7312,19 7413,19 7514,19 7623,19 7724,19 7825,19 7913,20 8014,20 8115,20 8222,20 8323,20 8424,20 8514,21 8615,21 8721,21 8822,21 8923,21 9015,22 9116,22 9220,22 9321,22 9422,22 9515,23 9616,23 9718,23 9819,23 9920,23 10021,23 10115,24 10216,24 10317,24 10418,24 10519,24 10615,25 10716,25 10817,25 10918,25 11015,26 11116,26

Paul whoknows
Member #5,081
September 2004
avatar

Thanks verthex, that application is really useful, however, I just found this ImageJ application which has a Save XY coordinates, exactly what I have been looking for, just in case here is the site:

http://rsbweb.nih.gov/ij/

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Go to: