Page 1 of 1
Calculate white-point with histogram
Posted: 2014-09-28T08:08:42-07:00
by MitchMitchel23
Hi!
I have thousends of scans of old documents (all greyscale, all of different grey backgrounds). I would like to use convert-stretch on each file individually.
I thought I could use the histogram to calculate the white-point I have to use.
Sample histogram "convert Test_001.png -define histogram:unique-colors=true -format %c histogram:info:-"
Code: Select all
25289: ( 0, 0, 0) #000000 gray(0,0,0)
2986: ( 1, 1, 1) #010101 gray(1,1,1)
...
...
68148: (210,210,210) #D2D2D2 gray(210,210,210)
68784: (211,211,211) #D3D3D3 gray(211,211,211)
67654: (212,212,212) #D4D4D4 gray(212,212,212)
...
...
435: (254,254,254) #FEFEFE gray(254,254,254)
2491: (255,255,255) #FFFFFF gray(255,255,255)
The highest pixel count is at gray 211. This is 82,74 % of 255. The parameters for contrast-stretch should be therefore 0%x83%.
But how can I do this automatically on each file? Any ideas to do that?
My version: ImageMagick 6.7.7-10 2014-03-06 Q16
Thanks!
Re: Calculate white-point with histogram
Posted: 2014-09-28T10:41:51-07:00
by fmw42
use -auto-level rather than contrast-stretch. see
http://www.imagemagick.org/script/comma ... auto-level. It will stretch both white and black points. contrast-stretch 0 will do the same. If you do not want to stretch the black point, then use -linear-stretch. see
http://www.imagemagick.org/Usage/color_ ... ar-stretch and
http://www.imagemagick.org/script/comma ... ar-stretch
Re: Calculate white-point with histogram
Posted: 2014-09-28T11:34:06-07:00
by MitchMitchel23
Hi fmw42!
Here is an example:
HIstogram:
-auto-level seems to do nothing:
The result with -linear-stretch 0%x83%:
The result with -contrast-stretch 0%x83%:
The highest value on the right side of the histogram should be the white-point (here gray 211 should be 255). But this must be calculated for each file individually. All files are different!
Re: Calculate white-point with histogram
Posted: 2014-09-28T11:59:59-07:00
by fmw42
Is this doing what you want with -linear-stretch or -contrast-stretch?
What platform and version of IM are you using? If on Linux/MacOSX or Windows with Cygwin, you might try my script textcleaner at the link below. Alternately, you could use -lat with or without the morphology.
Code: Select all
convert T_001.png -negate -lat 20x20+10% -negate -morphology open octagon:1 -morphology close octagon:1 result.png
Re: Calculate white-point with histogram
Posted: 2014-09-28T12:34:01-07:00
by MitchMitchel23
I'm using ImageMagick 6.7.7-10 2014-03-06 Q16 on Ubuntu 14.04 LTS x64.
I already tried textcleaner.sh. Really nice script, but it's not what I want, because there is not only text. The result should be also greyscale, not black and white.
The best results without loosing to much information was with contrast-stretch, so I also could use a black-point (1% or 2%).
Re: Calculate white-point with histogram
Posted: 2014-09-28T13:28:07-07:00
by fmw42
I do not know if this will do better, but you could try my script omnistretch with option -c or -pc.
The -c option searches each end of the histogram until the count at some bin is larger than the given value.
The -pc option searches each end of the histogram until the count at some bin is larger some percentage of the maximum bin count.
Re: Calculate white-point with histogram
Posted: 2014-09-28T13:34:17-07:00
by snibgo
MitchMitchel23 wrote:The highest pixel count is at gray 211. This is 82,74 % of 255. The parameters for contrast-stretch should be therefore 0%x83%.
"-Contrast-stretch" will make a certain percentage of pixels white. Your histogram doesn't say what percentage of pixels should be white. Instead, it says what level should become white. Level 211 out of 255, or 82.74%. For that, use "-level".
Code: Select all
convert T_001.png -level 0,82.74% out.png
MitchMitchel23 wrote:But how can I do this automatically on each file? Any ideas to do that?
Write a script that loops through all the files.
Re: Calculate white-point with histogram
Posted: 2014-09-28T16:37:03-07:00
by fmw42
snibgo is correct about using -level. But I am not sure you really want to stretch from the max bin to white, as it is washed out. If that is really what you want, then this will do it
Code: Select all
infile="T_001.png"
inname=`convert $infile -format "%t" info:`
maxbin=`convert $infile -depth 8 -format "%c" histogram:info:- |\
sed 's/^[ ]*//' | tr -cs "0-9\n" " " |\
sort -g -r -k 1 | head -n 1 | cut -d" " -f2`
echo $maxbin
maxpct=`convert xc: -format "%[fx:$maxbin/255]" info:`
convert $infile -level 0,$maxpct% ${inname}_stretched.png
You just need to write a loop over all the images you want to process and do this.
Re: Calculate white-point with histogram
Posted: 2014-09-28T16:57:23-07:00
by fmw42
This looks much better, in my opinion, using my script trianglethresh to get the whitepoint percent at the left inflection point before the peak.
Code: Select all
infile="T_001.png"
inname=`convert $infile -format "%t" info:`
maxpct=`trianglethresh $infile null: | tr -cs "%.0-9\n" " " | sed 's/ *//g'`
echo $maxpct
convert $infile -level 0,$maxpct ${inname}_stretched.png
Re: Calculate white-point with histogram
Posted: 2014-09-29T07:22:50-07:00
by MitchMitchel23
Hi fmw42!
Script with level:
Code: Select all
infile="T_001.png"
inname=`convert $infile -format "%t" info:`
maxbin=`convert $infile -depth 8 -format "%c" histogram:info:- | sed 's/^[ ]*//' | tr -cs "0-9\n" " " | sort -g -r -k 1 | head -n 1 | cut -d" " -f2`
echo $maxbin
maxpct=`convert xc: -format "%[fx:$maxbin/255*100]" info:`
echo $maxpct
convert $infile -level 0,$maxpct% ${inname}_ready.png
Result:
Script with contrast-stretch:
Code: Select all
infile="T_001.png"
inname=`convert $infile -format "%t" info:`
maxbin=`convert $infile -depth 8 -format "%c" histogram:info:- | sed 's/^[ ]*//' | tr -cs "0-9\n" " " | sort -g -r -k 1 | head -n 1 | cut -d" " -f2`
echo $maxbin
maxpct=`convert xc: -format "%[fx:$maxbin/255*100]" info:`
echo $maxpct
convert $infile -contrast-stretch 0,$maxpct% ${inname}_ready_contrast.png
Result:
Had to change
Code: Select all
maxpct=`convert xc: -format "%[fx:$maxbin/255]" info:`
to
Code: Select all
maxpct=`convert xc: -format "%[fx:$maxbin/255*100]" info:`
to calculate percent.
Tried also the traingletresh script. But it calculates 73.7255%.
Thanks a lot!
Re: Calculate white-point with histogram
Posted: 2014-09-29T09:12:07-07:00
by fmw42
Tried also the traingletresh script. But it calculates 73.7255%.
That is correct. It gets a lower threshold.
In my earlier scripts, you were correct. I forgot to multiply by 100 to get percent.