Get position of min luminosity along a line

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
dargaud
Posts: 31
Joined: 2006-08-06T01:58:58-07:00

Get position of min luminosity along a line

Post by dargaud »

Hello all,
Say I have two points. Along the line between them most pixels are white but at a certain point there's a dark patch. How can I get the position of the peak of this dark patch ? Either in cartesian coordinates or expressed as a % along the segment.
All I can think of is to get many pixel values, convert them to text, then feed that to an external prog with a peak search algorithm.
Any pure IM idea ?!?
Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get position of min luminosity along a line

Post by snibgo »

There are a number of possibilities. If the line is known to be vertical or horizontal, the process can be fairly simple. We crop to the line, auto-level so the darkest becomes black, make non-black pixels transparent, and list the non-transparent pixels by sparse-colors. These will be all the black ones. Just choose the first. A bit more work will find the middle of the darkest.

If all the input pixels were the same colour, not black, then auto-level will make none black, and sparse-colors will list none. Your script should check for this condition.

Code: Select all

convert in.png -crop WxH+X+Y -auto-level +transparent Black sparse-color:
EDIT: Should be "sparse-color:" not "sparse-colors:". I assumed your input was greyscale. If it isn't, then we also need "-channel RGB" so that each channel auto-levels independently.

Code: Select all

convert in.png -crop WxH+X+Y -channel RGB -auto-level +channel +transparent Black sparse-color:
My suggestion will find the darkest pixel (or one of the darkest pixels) within a given rectangle. If your line is vertical or horizontal, that defines a thin rectangle. But if your line is at an angle, the problem is harder. One possibility is to rotate the image until the line is horizontal (or vertical), then apply the above method.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get position of min luminosity along a line

Post by fmw42 »

What is your background color for the rest of the image.

If you can make the background color white. Then negate the image so that the background is black and your region that you want to find is bright, then you can use a feature of identify that will find the brightest value

identify -precision 5 -define identify:locate=maximum -define identify:limit=3 image.png

This will find up to 3 coordinates that all have the same maximum value. The limit can be changed.

see http://www.imagemagick.org/script/identify.php

But you need to be on IM 6.8.8-6 or higher, since this is a relatively new addition to identify
dargaud
Posts: 31
Joined: 2006-08-06T01:58:58-07:00

Re: Get position of min luminosity along a line

Post by dargaud »

Thanks for the ideas. I don't actually have a sample image yet ! I'm waiting to get one (it'll be a webcam image of a dial, and I need to find the position of the needle, a call to DePolar does the 1st part of the trick).
Post Reply