Normalize only extreme cases?

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?".
Elapido
Posts: 42
Joined: 2011-06-10T14:27:28-07:00
Authentication code: 8675308

Normalize only extreme cases?

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Normalize only extreme cases?

Post 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.
Elapido
Posts: 42
Joined: 2011-06-10T14:27:28-07:00
Authentication code: 8675308

Re: Normalize only extreme cases?

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Normalize only extreme cases?

Post 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.
snibgo's IM pages: im.snibgo.com
Elapido
Posts: 42
Joined: 2011-06-10T14:27:28-07:00
Authentication code: 8675308

Re: Normalize only extreme cases?

Post 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
Elapido
Posts: 42
Joined: 2011-06-10T14:27:28-07:00
Authentication code: 8675308

Re: Normalize only extreme cases?

Post 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.
Elapido
Posts: 42
Joined: 2011-06-10T14:27:28-07:00
Authentication code: 8675308

Re: Normalize only extreme cases?

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Normalize only extreme cases?

Post 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?
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Normalize only extreme cases?

Post 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.
snibgo's IM pages: im.snibgo.com
Elapido
Posts: 42
Joined: 2011-06-10T14:27:28-07:00
Authentication code: 8675308

Re: Normalize only extreme cases?

Post by Elapido »

Reading the docs, I've found the maxima parameter. Now, what's the max value? 255?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Normalize only extreme cases?

Post by snibgo »

Value is between 0 and 1, inclusive.
snibgo's IM pages: im.snibgo.com
Elapido
Posts: 42
Joined: 2011-06-10T14:27:28-07:00
Authentication code: 8675308

Re: Normalize only extreme cases?

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Normalize only extreme cases?

Post 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.
snibgo's IM pages: im.snibgo.com
Elapido
Posts: 42
Joined: 2011-06-10T14:27:28-07:00
Authentication code: 8675308

Re: Normalize only extreme cases?

Post 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
Last edited by Elapido on 2014-02-01T02:07:11-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Normalize only extreme cases?

Post 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.
Post Reply