x y coordinates of image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
bmacd615

x y coordinates of image

Post by bmacd615 »

To start simply, I would like to get the perimeter coordinates of an image. For example if I have a black and white image of a hand drawn circle, I would like to get back some sort of coordinates that can be used to accurately redraw the circle in say a spreadsheet.
I have not yet downloaded ImageMajick. Is there a script written to do something like this?
Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: x y coordinates of image

Post by fmw42 »

without seeing the image it is hard to tell. you can threshold the image to binary, then get the edge or outline pixels for the change from black to white. then you can have IM export that to text and filter only the white (edge pixels coordinates.

Here is an example:

#create heart shaped image:
convert -font WebDings -pointsize 24 label:Y \
+trim +repage -negate -bordercolor black -border 10 heart_mask.gif
Image

#create binary edge
convert heart_mask.gif -edge 1 -threshold 0 heart_mask_edge.gif
Image

# list coordinates of only white edge pixels
convert heart_mask_edge.gif txt:- | sed -n 's/^\(.*\):.*white$/\1/p'
13,10
14,10
15,10
22,10
23,10
24,10
11,11
12,11
13,11
14,11
15,11
16,11
17,11
20,11
21,11
22,11
23,11
24,11
25,11
26,11
11,12
12,12
16,12
17,12
18,12
20,12
21,12
25,12
26,12
10,13
11,13
17,13
18,13
19,13
20,13
26,13
27,13
10,14
11,14
26,14
27,14
10,15
11,15
26,15
27,15
10,16
11,16
26,16
27,16
11,17
12,17
25,17
26,17
11,18
12,18
13,18
24,18
25,18
26,18
12,19
13,19
14,19
23,19
24,19
25,19
13,20
14,20
15,20
22,20
23,20
24,20
14,21
15,21
21,21
22,21
23,21
15,22
16,22
20,22
21,22
22,22
15,23
16,23
18,23
19,23
20,23
21,23
15,24
16,24
17,24
18,24
19,24
15,25
16,25
17,25
18,25
15,26
16,26

Note these coords are listed by increasing Y, then increasing X and are NOT organized in any other special way so that one leads to the next in a boundary walking fashion. I don't know that IM has that kind of ability. But will defer to the IM experts.
bmacd615

Re: x y coordinates of image

Post by bmacd615 »

Okay, thanks! You have the idea and this is getting very close. The ordering would be very helpful, but MAYBE I could figure out something in Excel, though nothing jumps to mind yet.

Can all of what you said be done with just ImageMagick, or do I need something else like the perl interface. I haven't used this program yet. I didn't want to invest the time if it couldn't do what I am looking for.

Another way to solve this: Many graphical image editing programs have a magic wand that will select something if it stands out well. That portion of the image gets highlighted with a marquee. Is there a way to get the coordinates based on a magic wand selection, if there is one in ImageMagick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: x y coordinates of image

Post by fmw42 »

Can all of what you said be done with just ImageMagick, or do I need something else like the perl interface. I haven't used this program yet. I didn't want to invest the time if it couldn't do what I am looking for.
all was done with command line IM except the pipe to SED (unix function) to filter the output from txt:-, which lists the coordinates and colors of every pixel in the image. So it was filtered with SED to select only the white pixels and throw away the color values and just keep the coordinates.
Another way to solve this: Many graphical image editing programs have a magic wand that will select something if it stands out well. That portion of the image gets highlighted with a marquee. Is there a way to get the coordinates based on a magic wand selection, if there is one in ImageMagick?
IM has not special Magick Wand to my knowledge. I will defer to the IM experts on this.

However, I have a magicwand (bash unix) script that may or may not be helpful to you. See the link below. Go to the link for magickwand. It will have a page of examples as well as a link to download the script. But again the script will only allow you to color or get an edge outline of the region as above. It does not list out the coordinates and you will need to use the same output to txt: format and then filter the coordinates as I did above.

Note that IM is an image processing tool and does not do too much with textual information other than convert the image to text, get histograms and statistics and the like. Further processing might be needed to order your points, but I know of no other program to do that. Perhaps someone else on the list can suggest something further.
bmacd615

Re: x y coordinates of image

Post by bmacd615 »

fmw42,
Thanks, I see what you did.
In excel I was able to make the original data you supplied a little more to my liking (ideally only one set of coordinates at each step around the perimeter). An excel macro would speed up the task.

You have some fascinating scripts on your linked page. I expect to use some of them for some other things I have been trying.

Any other expert advice from others?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: x y coordinates of image

Post by anthony »

Are you actually wanting all the coordinates, or just a 'path'.

One solution may be covert your image to a bitmap and then filter it though a raster to vector program such as 'autotrace'. This will generate a SVG image of the shape which includes a SVG drawing path. This will contain fewer coordinates, but will have them in sequence around the edge.

For example using the IM Example output of "autotrace" from...
http://www.imagemagick.org/Usage/transform/#edge_vector
It generated this SVG vector image from a a 'curvy heart' bitmap image.

Code: Select all

<?xml version="1.0" standalone="yes"?>
<svg width="80" height="80">
<path style="fill:#ffffff; stroke:none;" d="M0 0L0 80L80 80L80 0L0 0z"/>
<path style="fill:#000000; stroke:none;" d="M28 71C44.0116 66.2902 69.8045 48.7318 68.9529 30C68.1674 12.7209 47.8992 13.9644 40 25C33.1425 11.9008 12.2146 14.0725 11.0972 30C10.3006 41.356 19.4577 46.4548 24.8665 55C28.0439 60.0198 27.9994 65.2766 28 71z"/>
</svg>
The first 'path' just colors the rectangle, while the second 'path' is the outline of the heart. From the you get the path (inserting a return before and a space after each letter for clarity)
M 28 71
C 44.0116 66.2902 69.8045 48.7318 68.9529 30
C 68.1674 12.7209 47.8992 13.9644 40 25
C 33.1425 11.9008 12.2146 14.0725 11.0972 30
C 10.3006 41.356 19.4577 46.4548 24.8665 55
C 28.0439 60.0198 27.9994 65.2766 28 71
z
Now in the SVG path specification the last two numbers of each 'segment' is a coodrinate somewhere along the edge of the shape, in sequence. As such the coodinates along the edge are (rounding to integers)
28 71
69 30
40 25
11 30
25 55
28 71
Of course the first and last points are the same to 'close' the curve.

Other options to autotrace can increase the number of points, and the style of output.

For details of SVG paths see
http://www.imagemagick.org/Usage/draw/#paths
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
bmacd615

Re: x y coordinates of image

Post by bmacd615 »

Anthony,
I am not familiar with this and will have to look into it, but at first glance it seems promising. But my day is ending now and the work week begins.
Post Reply