Page 1 of 1

Finding the first white pixel

Posted: 2012-04-18T04:00:07-07:00
by idevelop
Hie.

I'm trying to color balance a photo. I've learned from fred's excellent script (whitebalance) that I can do it if I specify the % or a pixel location of a white pixel.
I got that part working without a glitch but now I want to make it more robust and locate the position of the white pixel automagically without me telling him where it is.

I went through the posts and help files but couldn't solve it. I'm pretty sure i need to use the -format or -fx features but I didn't find the correct syntax.

I've managed to replace the white/close to white colors but I only need the coordinates of a "white" pixel to feed them to the next function.

Any help would be very much appreciated.
Shahar Zrihen

Re: Finding the first white pixel

Posted: 2012-04-18T09:32:49-07:00
by fmw42
Exactly white or close to white. If close to white, how close? What happens if there is no exactly white pixel?

This will locate the first occurrence of a pure white pixel in the gradient image.

convert -size 128x128 gradient: -rotate 90 grad_tmp.png
compare -metric rmse -subimage-search -dissimilarity-threshold 1 grad_tmp.png \( -size 1x1 xc:white \) null:
0 (0) @ 127,0

So it as x=127 and y=0 ( top right of the gradient as it should be )

see
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/

Alternately,

convert grad_tmp.png txt:- | grep "white" | head -n 1
127,0: (65535,65535,65535) #FFFFFFFFFFFF white


see
http://www.imagemagick.org/Usage/files/#txt

Re: Finding the first white pixel

Posted: 2012-04-28T03:52:49-07:00
by idevelop
Thanks for the quick reply.

I'm looking for the "closest to white" solution and if there's no white pixels I would love to get an error message.
I thought about doing it with a threshold number.

My idea is to use the whitebalance script with a script that would find the white marker used in the image instead of supplying it's X,Y coordinates or having to put the marker at the same spot every time.

You script will not help me since I need "closest to white". Sorry if I wasn't clear the first time.

Shahar

Re: Finding the first white pixel

Posted: 2012-04-28T10:07:39-07:00
by fmw42
add -fuzz to the compare


compare -metric rmse -fuzz XX% -subimage-search -dissimilarity-threshold 1 yourimage \( -size 1x1 xc:white \) null:

That will locate near white values within the fuzz % difference.

Alternately, and again simpler, you can auto-level the image to stretch near white to white and use the other method

convert yourimager -auto-level txt:- | grep "white" | head -n 1

This will report the location of the closest white value as it has now become white

Re: Finding the first white pixel

Posted: 2012-04-30T18:25:49-07:00
by anthony
I suggest converting the image to grayscale, and output 16 bit depth values to a txt: image
You can then sort, or simply go though the output list and look for the brightest pixel in the whole image.

You can also use 'max' function on the grayscale image to get the brightness level of the brigthest pixel.

I'll leave it to Fred (previous responder to get you the details.

Re: Finding the first white pixel

Posted: 2012-04-30T19:06:34-07:00
by fmw42
anthony wrote:
You can also use 'max' function on the grayscale image to get the brightness level of the brigthest pixel.
I am not sure you can get near-white with -fuzz that way? Also you still don't have the coordinates for the "max" value.