How to the get the coordinates of pixels?

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
etrader
Posts: 13
Joined: 2012-09-29T06:53:04-07:00
Authentication code: 67789

How to the get the coordinates of pixels?

Post by etrader »

I want to get the coordinates of the points in a line from an image to re-draw it.
Image

I tried

Code: Select all

convert photo.png txt:- | sed -n 's/^\(.*\):.*black$/\1/p'
but it didn't work. I also tried to create white edges as

Code: Select all

convert photo.png -edge 1 -threshold 0 photo.png
convert photo2.png txt:- | sed -n 's/^\(.*\):.*white$/\1/p'
but it didn't work either. Where did I do wrong?

In any case, my ideal solution is to find a set of approximate points (probably from the center of the line) to have the full line coordinates.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to the get the coordinates of pixels?

Post by fmw42 »

try

Code: Select all

convert xpd2F2A.png -threshold 50% -type bilevel txt:- | tail -n +2 | sed -n 's/^\(.*\):.*gray(0)$/\1/p'
or

Code: Select all

convert xpd2F2A.png -threshold 50% -depth 1 txt:- | tail -n +2 | sed -n 's/^\(.*\):.*black$/\1/p'
or

Code: Select all

convert xpd2F2A.png -threshold 50% -depth 1 txt:- | tail -n +2 | grep "black" | sed -n 's/^\(.*\):.*$/\1/p'
etrader
Posts: 13
Joined: 2012-09-29T06:53:04-07:00
Authentication code: 67789

Re: How to the get the coordinates of pixels?

Post by etrader »

WOW, it worked like a charm. The key is that my points are not completely black and I have to use gray instead, right?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to the get the coordinates of pixels?

Post by fmw42 »

See my edits above. That is part of it. You need to threshold to make it binary. But different -type or different -depth will result in txt showing the color as gray(0) or black. You should always test your command up to the txt: to see what format you get for the color before filtering.

Note I corrected my first line. I had accidentally put gray(255) which is white and you wanted black or gray(0). I also forgot the -n and have added that.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to the get the coordinates of pixels?

Post by fmw42 »

If you only want the truly black pixels and not the near black ones, then you can do

Code: Select all

convert xpd2F2A.png txt:- | tail -n +2 | sed -n 's/^\(.*\):.*gray(0)$/\1/p'
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to the get the coordinates of pixels?

Post by snibgo »

IM version? Platform?
etrader wrote:In any case, my ideal solution is to find a set of approximate points (probably from the center of the line) to have the full line coordinates.
The centreline can be traced, and the line expressed as a series of Bezier curves, which IM can draw, or you can create SVG.

See my Lines, points and curves page. I show how to do it, and give away the essential software, but not the complete software.

Code: Select all

%IM%convert oneLine.png -negate -threshold 50%% x.png

call %PICTBAT%lns2pts x.png x.txt

pts2bez /ix.txt /ob.txt

%IM%convert ^
  -size 300x300 xc:White ^
  -fill None ^
  -draw @b.txt ^
  redcurve.png


pts2bez /ix.txt /ob2.txt /e5

%IM%convert ^
  -size 300x300 xc:White ^
  -fill None ^
  -draw @b2.txt ^
  redcurve2.png
Image

Image
snibgo's IM pages: im.snibgo.com
etrader
Posts: 13
Joined: 2012-09-29T06:53:04-07:00
Authentication code: 67789

Re: How to the get the coordinates of pixels?

Post by etrader »

snibgo wrote: 2017-08-09T11:28:12-07:00 IM version? Platform?
ImageMagick 6.8.9-9
Ubuntu 16.04
Post Reply