Grid based white balancing of images
Moderator: peterZ
-
- 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
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
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.
I decided to write a small command line tool for grid based white balancing of images with a calibrationg gray card.
The source code
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.
-
- 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
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
to obtain the color calibration information in the binary file color.bin from the picture of the gray card gray.ppm.
An application of
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.
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
An application of
Code: Select all
ppmwhitebalancing -d color.bin page_orig.ppm > page_corrected.ppm
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.
-
- 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
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.
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.
-
- 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
The picture of my simple gray card
in combination with the calibration grid
can be used to obtain color correction information and unwarping information with the following commands:
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:
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:
in combination with the calibration grid
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
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:
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:
-
- 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
Now the command
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:
After correction the picture looks like
Code: Select all
ppmunwarp -d deform.bin page_orig.ppm | ppmwhitebalancing -d color2.bin > page_corrected.ppm
After correction the picture looks like
-
- 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
And here is a picture of another laser printout
and the corrected version
and the corrected version
-
- 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
As a last example the picture of a flyer
and the corrected version
and the corrected version
-
- 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
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!
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!
-
- 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
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:
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:
-
- 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
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.
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.