Grid based white balancing of images

Share your software workflow. Write up your tips and tricks on how to scan, digitize, OCR, and bind ebooks.

Moderator: peterZ

Post Reply
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Grid based white balancing of images

Post by mhr »

After the discussion in the thread http://www.diybookscanner.org/forum/vie ... =19&t=2795,
I decided to write a small command line tool for grid based white balancing of images with a calibrationg gray card.
The source code
ppmwhitebalance.cc.txt
Version 1.0 of the program ppmwhitebalance
(36.98 KiB) Downloaded 485 times
is written in the same spirit as the unwarp utility ppmunwarp, which is discussed in
http://www.diybookscanner.org/forum/vie ... =19&t=2589.
Please read that thread for compilation issues. This program also restricts itself to the PPM picture format.
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Re: Grid based white balancing of images

Post by mhr »

The program has several options to control its behaviour.

The main goal is to color correct pictures by shooting an additional picture of a homogeneous gray card as color calibration information.
The program places a control point grid upon the calibration picture of nx x ny points (see options -nx and -ny)
and smoothes the gray information by cubic interpolation (linear interpolation is enforced, if option -g1 is given). If no grid size is specified, a
homogeneous white balancing is done for the following pictures as a special case.

The gathered information is saved and can afterwards be applied to the target pictures to correct the colors.
This is done channelwise (red, green, blue) or only on the brightness channel if option -gc is specified.

The target value for color scaling is the middle value 128 from the channel range 0-255. This can be changed by the option -gb.
If a zero value is given there, the mean value of the color calibration picture is used as the target value.

Sometines the color correction is too heavy, especially at the border if the gray card doesn't cover the complete picture.
To restrict the possible channel factors, an upper limit for the scaling -gm and the division -gd can be set. Per default the programm restricts the factor to 1/1.5<=scalefactor<=1.5. As an example this would allow channel values from 86 up to 192 to be scaled to the target value 128.

The gathering of color calibration information is done with a command like

Code: Select all

ppmwhitebalancing <options> gray.ppm > color.bin
to obtain the color calibration information in the binary file color.bin from the picture of the gray card gray.ppm.
An application of

Code: Select all

ppmwhitebalancing -d color.bin page_orig.ppm > page_corrected.ppm
takes the color calibration information to correct the colors of picture page_orig.ppm and writes the corrected picture into
page_corrected.ppm. If several pages are corrected with the same color correction information color.bin, obviously the
lighting conditions should be kept as fixed as possible.
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Re: Grid based white balancing of images

Post by mhr »

In the following I want to concretize the steps I have in mind to color correct and unwarp pictures with the two utilities ppmwhitebalancing and
ppmunwarp. This example is just rapid prototyping, the pictures are not totally in focus, the keystoning is quite large and the lighting conditions are far from optimal. It should just emphasize the general red line. To reduce server space, all photos are scaled from 10 Megapixels to 1024 x 768 pixels and coded as JPG, but the general idea should be understandable though.
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Re: Grid based white balancing of images

Post by mhr »

The picture of my simple gray card
The gray card calibration image gray.ppm
The gray card calibration image gray.ppm
in combination with the calibration grid
The calibration grid for unwarping grid.ppm, edited to make ppmunwarp happy due to missing points at the four corners
The calibration grid for unwarping grid.ppm, edited to make ppmunwarp happy due to missing points at the four corners
can be used to obtain color correction information and unwarping information with the following commands:

Code: Select all

  ppmunwarp -gm 0 -m check.ppm grid.ppm > deform.bin
  ppmwhitebalance -nx 40 gray.ppm > color1.bin
  ppmunwarp -d deform.bin gray.ppm | ppmwhitebalance -nx 40 > color2.bin
  ppmwhitebalance -d color1.bin > gray1.ppm
  ppmunwarp -d deform.bin gray.ppm | ppmwhitebalance -d color2.bin > gray2.ppm
The first line calculates the unwarping information in deform.bin. Note that I prefer here to have no margin around the calibration grid.
Therefore I used the option -gm 0. Due to a bug, this is only possible starting from version 1.2 of the program ppmunwarp.

The second line calculates color correction information in color1.bin for the unwarped pictures. This is done here only for illustration and not
recommended. It is used in the fourth line to illustrate the artifacts if the color correction is applied to the gray card picture itself. The abrupt color changes
at the border of the gray card produces large color distortions mainly at the borders and corners:
The distorted gray card picture itself with color artifacts gray1.ppm
The distorted gray card picture itself with color artifacts gray1.ppm
These effects can be avoided if the color correction is applied to the unwarped pictures as done in the third and fifth line of the code from above. The corrected gray card looks like:
Unwarping and color correction for the gray card picture itself gray2.ppm
Unwarping and color correction for the gray card picture itself gray2.ppm
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Re: Grid based white balancing of images

Post by mhr »

Now the command

Code: Select all

  ppmunwarp -d deform.bin page_orig.ppm | ppmwhitebalancing -d color2.bin > page_corrected.ppm
can be used to dewarp and color correct the pictures of interest. My first example page is a picture full of numbers, which I use to get the autofocus of my camera right:
Original number picture page1a.ppm
Original number picture page1a.ppm
After correction the picture looks like
Corrected number picture page1b.ppm
Corrected number picture page1b.ppm
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Re: Grid based white balancing of images

Post by mhr »

And here is a picture of another laser printout
Original picture of the GNU License page2a.ppm
Original picture of the GNU License page2a.ppm
and the corrected version
Corrected picture page2b.ppm
Corrected picture page2b.ppm
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Re: Grid based white balancing of images

Post by mhr »

As a last example the picture of a flyer
Original picture of flyer page3a.ppm
Original picture of flyer page3a.ppm
and the corrected version
Corrected flyer page3b.ppm
Corrected flyer page3b.ppm
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Re: Grid based white balancing of images

Post by mhr »

Obviously these examples suffer from bad influence of the page holders at the corners, which were used to
rapidly get some example pictures and the pictures have been truncated by the calibration grid as well.
In addition the large keystoning takes parts of the pictures out of focus.
By careful design this errors can all be avoided.

It would be quite interesting, if the color correction scheme in combination with the unwarping can be used
properly in some of the scanner setups of other users!

Have fun!
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Re: Grid based white balancing of images

Post by mhr »

I just decided to enable ppmwhitebalance to be able to convert 8 bit and 16 bit ppm files.
So this version 1.3 of ppmwhitebalance should recognize and convert all valid raw ppm files.
You can create 16 bit ppm files from raw camera dng files via a program like dcraw.
The target color depth can be specified with the new option -od:
ppmwhitebalance.cc.txt
Version 1.3 of ppmwhitebalance
(40.01 KiB) Downloaded 444 times
mhr
Posts: 37
Joined: 07 May 2012, 10:12
E-book readers owned: onyx-boox-m92 sony-trs-t1
Number of books owned: 500
Country: Germany

Re: Grid based white balancing of images

Post by mhr »

I joined common parts of ppmunwarp and ppmwhitebalance into a library to eliminate code duplicity.
Version 1.4 can be obtained in the thread http://www.diybookscanner.org/forum/vie ... =19&t=2589
as it is now bundled with ppmunwarp and the latter is discussed in the above thread.
Post Reply