Reproduce Book Restorer lighting correction

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?".
Post Reply
pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Reproduce Book Restorer lighting correction

Post by pchun »

Questions about improving images of scanned book pages were posted here many times before, but please bare with me. I scan books with a V-cradle photo-scanner (Internet Archive - style design). Unfortunately the lighting is not uniform, so brightness is uneven across the scanned page. I am aware of the fact that problems of this type could be solved with Fred's whiteboard or textcleaner, but my employer doesn't want the end product to be b/w pages, my goal is to preserve color. So it appears the only option is to somehow reproduce "lighting corrrection" effect, available in Book Restorer software, which allows you to go from this

https://drive.google.com/file/d/1fx9GW9 ... sp=sharing

to this

https://drive.google.com/file/d/1-og--f ... sp=sharing

As far as I understand the solution was in general terms outlined here by Anthony viewtopic.php?t=21906 That is first create a mask removing text foreground from the page, then blur the image to fill the holes and even background, divide original image by the masked one.
Can anyone explain in more detail how do I go about this? Especially how do I mask the text out? Text-masking example in IM documentation here http://www.imagemagick.org/Usage/photos/#removing is not very helpful, because it is based on the idea that the operator knows in advance where the text is located and doesn't bother with the problem of finding it first. And how do I even the brightness of the blurred background image after masking?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reproduce Book Restorer lighting correction

Post by fmw42 »

Actually my textcleaner script has an argument -b bgcolor to set the color of the background. You can either specify some color that you separately measure from the image. Or as of today, a new version is available that will estimate the background color in the image. So using your image

Code: Select all

textcleaner -f 20 -o 10 -b image 1.jpg 1_proc.jpg
Image
pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Re: Reproduce Book Restorer lighting correction

Post by pchun »

Thank you very much, Fred. I didn't pay attention to the -b key in textcleaner description. It works great. Probably it's the easiest way to even background after all.
Though speaking of Book Restorer how do you think their specific effect was achieved? It somehow significantly reduces the difference in brightness perception across the page without flood filling background with one color, so the page keeps authentic feel of texture to it.
Is it really text-masking with subsequently dividing the image by blurred version of itself, like Anthony suggested? If so then will it be possible to somehow invert textcleaner'ed b/w image and use it as a mask? Sorry for amateurish suggestion.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reproduce Book Restorer lighting correction

Post by fmw42 »

I do not know "Book Restorer". Do you have a link to it?

I am not sure how to reproduce that effect. If I had more clue as to what they do, then I might be able to emulate it.

Comparing your two images, it looks like some anti-vignetting, since the shadow text from the backside is still showing and only the corners are cleaned. In my opinion, that is not very satisfying having the shadow text showing.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reproduce Book Restorer lighting correction

Post by fmw42 »

I just realized that the saturation default was 200 on my script. So the result above is over-saturated in background color. The proper command should have been

Code: Select all

textcleaner -f 20 -o 10 -b "image" -S 100 1.jpg 1_proc1.jpg
Image

An alternate is to crop out a swatch of image of the desired background color where it is not shaded.

Image

Then get that average color:

Code: Select all

convert 1_swatch.png -scale 1x1! -format "%[pixel:u.p{0,0}]" info:
srgb(203,197,143)
Then use that color:

Code: Select all

textcleaner -f 20 -o 10 -b "rgb(203,197,143)" -S 100 1.jpg 1_proc2.jpg
Image
pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Re: Reproduce Book Restorer lighting correction

Post by pchun »

Thanks again, Fred. Book Restorer is a rather antiquated piece of software, developed in early 2000s by the French company i2s http://www.i2s-digibook.com/. Their main line of business is producing professional book scanners, so they specifically designed Book Restorer for post-processing of scanned pages. It was since then discontinued and isn't commercially available anymore but still makes rounds on internet as a cracked installation package, because folks, who dabble in book scanning, value it for two reasons. First it has rather dependable algorithm for straightening distorted text lines, second is lighting correction effect we are talking about.
Below I post a screenshot of "lighting correction" controls.
https://drive.google.com/file/d/1hHVIv3 ... sp=sharing
There are only two of them, threshold (from 0 to 255, apparently controls overall brightness and is usually computed automatically, I tinkered with it in manual regime and found out that to reproduce the effect on my sample image it should be set somewhere between 180 and 190) and sensitivity (from 0 to 40, the higher the value the cruder effect you have, apparently filter radius? my sample value was set to 15)
Below I post two more versions of the sample image
First with sensitivity set to the default value of 5
https://drive.google.com/file/d/1OX31pS ... sp=sharing
Second with sensitivity set to the maximum value of 40
https://drive.google.com/file/d/11VRJNs ... sp=sharing

I can give a link to cracked installation package itself if you don't object on copyright grounds and aren't afraid of me being an evil Russian hacker trying to fish you for something. Though you can't go much beyond simple experimenting with it, it's just a bunch of dll's, underlying code is not available.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reproduce Book Restorer lighting correction

Post by fmw42 »

I will ask again. Why would you want the backside shadow text to still show through? To me that is distracting.
pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Re: Reproduce Book Restorer lighting correction

Post by pchun »

I don't want it to show through and would be glad to get rid of it altogether. The problem is sometimes I have to deal with much worse cases of bleed through effect when foreground text is not that different from background text. The whole problem with preserving background color of scanned pages in some form originates there. As a general rule you obviously would be better off with clean b/w page, but with old books you quiet often encounter heavy bleed through or bad print with lots of partially pressed characters or very low-grad paper produced from God knows what and which creates noise of its own, and quality of print and paper is not always consistent across a book. So to achieve consistent results with algorithms aimed at clearing background altogether and binarization you have to tinker a lot with thresholds, and there is a chance that no matter how you tinker you will arrive at a b/w result that would be visually less legible and aesthetically less appealing than the image you started with.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reproduce Book Restorer lighting correction

Post by fmw42 »

You can combine text cleaner with a vignette mask to composite with your original, so that it only affects the corners where it reduces the shading. Adjust the mask as desired. See also my scripts, vignette, vignette2, vignette3 for different shapes.

Image:
Image

Code: Select all

convert -size 489x756 xc:black -background white -vignette 0x30+2+2% 1_mask.png
Image

Code: Select all

textcleaner -f 20 -o 10 -b "image" -S 100 1.jpg 1_proc1.jpg
Image

Code: Select all

convert 1.jpg 1_proc1.jpg mask.png -compose over -composite 1_proc3.jpg
Image

Sorry, I do not know how to automatically create the mask image from your input. My mask image was trial and error until it looked close.
pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Re: Reproduce Book Restorer lighting correction

Post by pchun »

Thanks for your time, Fred. I will try it. To illustrate my point about complexities of working with book scans. Below is a sample page (low quality rough grainy paper and the print is smudged to boot). I textcleaner'ed it with textcleaner -f 20 -o 10 -b "image" -S 100. You can see it doesn't look nice.
Before
https://drive.google.com/file/d/1G2Oqjn ... sp=sharing
After
https://drive.google.com/file/d/1H9dMav ... sp=sharing
StevePM
Posts: 6
Joined: 2017-11-29T05:30:49-07:00
Authentication code: 1152

Re: Reproduce Book Restorer lighting correction

Post by StevePM »

To prevent the problem in the first place have you ever tried putting a black sheet behind the page when you scan it, i believe it stops the bleed through of text happening.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reproduce Book Restorer lighting correction

Post by fmw42 »

Your After result looks much better to me, since it is much more readable than the Before.
pchun
Posts: 12
Joined: 2017-11-02T22:53:16-07:00
Authentication code: 1152

Re: Reproduce Book Restorer lighting correction

Post by pchun »

I did some additional research on this problem and I think managed to make some progress though in a somewhat circuitous way. Generally I tried to move forward with the idea outlined by Anthony here viewtopic.php?t=21906, that is divide the image by a blurred version of itself.
In one of his replies Fred mentions that probably Book Restorer "lighting correction" includes some sort of anti-vignetting. I googled it and apparently the most common application of anti-vignetting is processing of astronomical images (flat field images). So I found this astronomy plugin for GIMP http://www.hennigbuam.de/georg/gimp.html
and processed my sample page image https://drive.google.com/file/d/1fx9GW9 ... sp=sharing
The plugin produced this grey image https://drive.google.com/file/d/1THrwAW ... sp=sharing
Note that background becomes much more uniform here, darker areas along page borders are gone. Then I subjected this new grey image to heavy gaussian blur (radius 150). And finally I run

Code: Select all

 convert blur.jpg page.jpg -compose Divide -composite divide.jpg

Here is the final image I got https://drive.google.com/file/d/13MZJO8 ... sp=sharing. It looks kind of promising to me because if you compare background in the areas around page borders (that is the most poorly lit parts of the original image) it looks very close to what you get after Book Restorer "lighting correction" (see here https://drive.google.com/file/d/1-og--f ... sp=sharing). But obviously it is too bright overall and especially in the middle, plus weird halos around character edges. Apparently Book Restorer subjects grey image to some additional transformations before blur, first of all, masking out of text (as was also suggested by Anthony), and probably some brightness adjustment as well.

So I would like to ask for forum’s help and advice on the following questions
1) Is it possible to get flat grey image with IM rather than with GIMP astronomy plugin? Source code for the plugin is available here http://www.hennigbuam.de/georg/download ... .9.tar.bz2 if anyone is interested in taking a peek (background_gradient.c file to be precise). Unfortunately I am not a programmer, so I don’t understand what it is actually doing. Also astronomy images processing with IM was recently discussed here viewtopic.php?f=1&t=32935
2) How do I mask out text from the page image (essentially punching holes in place of characters)?
3) Is it possible to calculate individual brightness adjustment for grey divisor image (apparently it should be much brighter to get reasonable brightness in the final image) as some kind of derivative from the known brightness of the divisible image we are processing?

Sorry for a lengthy post.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reproduce Book Restorer lighting correction

Post by fmw42 »

1) You can convert to grayscale using

Code: Select all

convert input -colorspace gray output
2) Assuming your text is near black, then

Code: Select all

convert input -fuzz XX% -transparent black output
where XX% is the percentage you want to consider near black

3) I do not know how to automatically compute the divisor you need other than trial an error. Perhaps I do not understand the issue.

But if you are trying to remove the vignette, it seems that my textcleaner script does what you need. The script allows you to convert to grayscale as an argument. It also allows you to set the output background color and will even out the vignetting.
Post Reply