Page 1 of 2

Normalize only extreme cases?

Posted: 2014-01-30T10:44:23-07:00
by Elapido
I know I can use the normalize command or contrast-stretch, etc. to correct the levels of all my images in a folder. However, I'd like that Imagemagick does not process all images, but only those that have severe problems of levels. I suppose there must be a way to make it analize the picture by generating its histogram and then process or not the image acording to a predefined values.

Re: Normalize only extreme cases?

Posted: 2014-01-30T11:18:04-07:00
by fmw42
Simplest case is to use -auto-level to stretch the dynamic range so that any empty bins on either end of the histogram are stretched to full black/white.

Another option is to use -auto-gamma, but the IM implementation has a fixed value. My scripts, autogamma and autolevel and autocolor and autotone, at the link below allows a user choice for the midgraylevel argument.

I know of no automatic way to determine a "best case" scenario. You will likely have to process the histogram and make your own determination of when contrast is too low.

Re: Normalize only extreme cases?

Posted: 2014-01-31T02:38:12-07:00
by Elapido
There must be a way to copy or move all images that, for instance, do not have any value from 0 to 50.

Image

Re: Normalize only extreme cases?

Posted: 2014-01-31T02:51:11-07:00
by snibgo
For example:

Code: Select all

convert x.jpg -format "%[fx:minima>0.25]" info:
This returns 0 or 1 according to whether the minimum exceeds a threshold. It could be used in a script to do whatever you want.

Re: Normalize only extreme cases?

Posted: 2014-01-31T06:03:42-07:00
by Elapido
I will test it. Thanx.

I had already figure out a silly way to do what I needed. This is my test script. It creates a histogram of the image and then crops the histogram to a txt file. The crop points to a low point to the left and to the right. So, if it's white, it means there's data and there's no need for adjustment. If if's black, it means, it needs adjustments, either in shadows or in lights.

Code: Select all

runwait, %comspec% /c d:\imagemagick\convert.exe z:\1-3.jpg -colorspace Gray -define histogram:unique-colors=false histogram:z:\histo.jpg ,,hide
runwait, %comspec% /c d:\imagemagick\convert.exe z:\histo.jpg -crop 1x1+20+192 txt: >shadow.txt ,,hide
runwait, %comspec% /c d:\imagemagick\convert.exe z:\histo.jpg -crop 1x1+236+195 txt: >light.txt ,,hide
filedelete, z:\histo.jpg
filereadline, shadow, shadow.txt, 2
FileReadLine, light, light.txt, 2
filedelete, z:\shadow.txt
filedelete, z:\light.txt
IfInString, shadow, #000000
msgbox, the image needs shadow correction
IfInString, light, #000000
msgbox, the image needs light correction
exitapp

Re: Normalize only extreme cases?

Posted: 2014-01-31T06:10:54-07:00
by Elapido
snibgo wrote:For example:

Code: Select all

convert x.jpg -format "%[fx:minima>0.25]" info:
This returns 0 or 1 according to whether the minimum exceeds a threshold. It could be used in a script to do whatever you want.
Can you explain the synthax? I'm trying it and I always get 0.

Re: Normalize only extreme cases?

Posted: 2014-01-31T06:21:30-07:00
by Elapido
Ok, I've reduced the value to 0.01 and now it's working. However, it seems this is only for shadows. I've tried with some images and I only get 1 with images that lack blacks, but with images which are dark and lack in the lights end, I always get 0. Any idea?

Re: Normalize only extreme cases?

Posted: 2014-01-31T06:23:51-07:00
by snibgo

Code: Select all

F:\web\im>%IM%convert -size 100x100 xc:gray75 p.png

F:\web\im>%IM%convert p.png -format "%[fx:minima>0.25]" info:
1
F:\web\im>%IM%convert -size 100x100 xc:gray10 p.png

F:\web\im>%IM%convert p.png -format "%[fx:minima>0.25]" info:
0
F:\web\im>
Does that explain it?

Re: Normalize only extreme cases?

Posted: 2014-01-31T06:25:47-07:00
by snibgo
For the light end, examine "maxima".
runwait, %comspec% /c d:\imagemagick\convert.exe z:\1-3.jpg -colorspace Gray -define histogram:unique-colors=false histogram:z:\histo.jpg ,,hide
Don't use jpg. Jpg uses LOSSY compression. This means it will change the values. Png won't change the values.

Re: Normalize only extreme cases?

Posted: 2014-01-31T06:27:42-07:00
by Elapido
Reading the docs, I've found the maxima parameter. Now, what's the max value? 255?

Re: Normalize only extreme cases?

Posted: 2014-01-31T06:30:57-07:00
by snibgo
Value is between 0 and 1, inclusive.

Re: Normalize only extreme cases?

Posted: 2014-01-31T07:05:21-07:00
by Elapido
I don't get it to work :(

Look, with minima I get good results. If you try with these images, you can see how it reports 0 with correct.jpg, contrast.jpg and lightcorrection.jpg, but it reports 1 with darkcorrection.jpg and bothcorrection.jpg, because they lack blacks, so that's fine.
However, with maxima, something must be incorrect with the values I'm trying. Any idea what value to use so that it reports 1 with lightcorrection.jpg and bothcorrection.jpg and 0 with the rest?

Code: Select all

impath=d:\imagemagick
image= z:\darkcorrection.jpg

runwait, %comspec% /c %impath%\convert.exe %image% -colorspace Gray -define histogram:unique-colors=false histogram:z:\histo.jpg ,,hide ; just to see the histogram...
runwait, %comspec% /c %impath%\convert.exe %image% -format "`%[fx:minima>0.01]" info: >minima.txt ,,hide
runwait, %comspec% /c %impath%\convert.exe %image% -format "`%[fx:maxima>0.99]" info: >maxima.txt ,,hide
FileReadLine, minima, minima.txt, 1
FileReadLine, maxima, maxima.txt, 1
filedelete, minima.txt
filedelete, maxima.txt
msgbox, correct darks?: %minima%, correct lights?: %maxima%
return
correct.jpg
Image

darkcorrection.jpg
Image

lightcorrection.jpg
Image

contrast.jpg
Image

bothcorrection.jpg
Image

Re: Normalize only extreme cases?

Posted: 2014-01-31T07:26:08-07:00
by snibgo
It returns 0 meaning "false" and 1 meaning "true".

So "fx:maxima<0.99" returns 0 if you have a light value and 1 if you don't.

Re: Normalize only extreme cases?

Posted: 2014-01-31T07:35:50-07:00
by Elapido
Oh, I see. Now I understand. Tested and working :) Thanx.

Code: Select all

impath=d:\imagemagick
image= z:\lightcorrection.jpg

runwait, %comspec% /c %impath%\convert.exe %image% -colorspace Gray -define histogram:unique-colors=false histogram:z:\histo.png ,,hide
runwait, %comspec% /c %impath%\convert.exe %image% -format "`%[fx:minima>0.001]" info: >minima.txt ,,hide
runwait, %comspec% /c %impath%\convert.exe %image% -format "`%[fx:maxima>0.99]" info: >maxima.txt ,,hide
FileReadLine, minima, minima.txt, 1
FileReadLine, maxima, maxima.txt, 1
filedelete, minima.txt
filedelete, maxima.txt

if minima = 1
minima=yes
else
minima=no

if maxima=1
maxima=no
else
maxima=yes

msgbox, correct darks?: %minima%, correct lights?: %maxima%
exitapp

Re: Normalize only extreme cases?

Posted: 2014-01-31T11:43:53-07:00
by fmw42
Seems like you are trying to stretch the histogram to full dynamic range. That is done automatically by

convert image -auto-level result

However, if you want to use -normalize or -contrast-stretch with other values (normalize is just a fixed set of values for -contrast-stretch), then just check the min and max values from the image. You can use -fx calculations to return values between 0 and 1 or string formats to return values between 0 and Quantumrange depending upon your IM compile. So Q8 is 0 to 255 and Q16 is 0 to 65535.

convert image -format "%[fx:minima]" info:
convert image -format "%[fx:maxima]" info:

or

convert image -format "%[minima]" info:
convert image -format "%[maxima]" info:

These can be put into a variable and tested further against some thresholds that you want to use.