Correcting non uniform light?

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
bugbear
Posts: 33
Joined: 2010-10-21T03:06:35-07:00
Authentication code: 8675308

Correcting non uniform light?

Post by bugbear »

I am currently preparing techniques to be used when visiting an archive
of historical documents, and will be taking photographs under some tight constraints.

I wish to capture some (old) maps. This will be done by taking multiple
overlapping images, and stitching them together. I have (with help
from the hugin mailing list) got this nailed.

My next step/concern is that a large map will not be uniformly
lit in the archive reading room.

So - if I have a non uniformly lit image, but with plenty of white areas
from which I can take local "whitepoint" samples, how do I proceed
to use this information to correct the whole image (ideally include the blacks)?

Here's a test image made at home.

Image

(I could easily have lit this uniformly at home, but then
it wouldn't be a good test)

If I had a reference black image, and a reference white image, correction would be simple,
using -fx; subtract the reference black from the pixel, and multiply by the difference of
reference white to reference black.

BugBear
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Correcting non uniform light?

Post by Bonzo »

I would have a go at setting the whitepoint on the images before joining them and see what the result was. It may be easier dealing with the smaller images than the one large one?
bugbear
Posts: 33
Joined: 2010-10-21T03:06:35-07:00
Authentication code: 8675308

Re: Correcting non uniform light?

Post by bugbear »

Bonzo wrote:I would have a go at setting the whitepoint on the images before joining them and see what the result was. It may be easier dealing with the smaller images than the one large one?
That probably reduces the problem, but the lighting is non-uniform even on the sub-images (there are only3 sub images here, so they're quite large).

BugBear
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Correcting non uniform light?

Post by snibgo »

If you are looking for good quality, don't start from a JPEG image. Use a good camera and start from a raw image.

In this image, the light intensity varies, and so does the colour of the light. It also varies between the folded sections. (A household iron, slighty warm, smooths paper well. But a museum might not approve!) I'll ignore the folded-section problem.

With Gimp, I took average readings from six "white" sections, each averaging 10x10 pixels. I took the four corners, also top-centre and left-centre. This command reconstructs a "white" background. Then it divides the image by this background, which make the background white at those six locations.

Windows BAT syntax.

Code: Select all

convert ^
  light.jpg ^
  ( +clone ^
    -sparse-color shepards "0,0 #bab5b1 %%[fx:w-1],0 #797471 0,%%[fx:h-1],#b7b6b3 %%[fx:w-1],%%[fx:h-1] #7c7876 57,403 #d6d2ce 576,58 #988d89" ^
  ) ^
  +swap ^
  -compose Divide -composite ^
  b.png
snibgo's IM pages: im.snibgo.com
bugbear
Posts: 33
Joined: 2010-10-21T03:06:35-07:00
Authentication code: 8675308

Re: Correcting non uniform light?

Post by bugbear »

Thanks for that - most helpful.

I've never used (and didn't even know about) -sparse-color

http://www.imagemagick.org/Usage/canvas/#sparse-color

which is clearly an excellent answer to my interpolation requirment!!

EDIT; the -sparse-color isn't working. I'm using
convert -version
Version: ImageMagick 6.7.7-10 2014-03-06 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP

And I get:

convert.im6: invalid argument for option `sparse-color': Invalid number of Arguments @ error/mogrify.c/SparseColorOption/521.

Edit; seems to be the %% syntax. Replacing those with the numbers makes it run OK.

BugBear
bugbear
Posts: 33
Joined: 2010-10-21T03:06:35-07:00
Authentication code: 8675308

Re: Correcting non uniform light?

Post by bugbear »

snibgo wrote: In this image, the light intensity varies, and so does the colour of the light.
I can (probably) explain that. I placed the map on the floor, near an East facing glass double door, at 8:00 AM, and then placed a incandescent lamp with its "back" towards the door.

So near the lamp the light was fairly "pure" incandescent, but the ambient light was sunlight.

BugBear
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Correcting non uniform light?

Post by snibgo »

Yes, that figures. If an adjacent wall was coloured, that would also affect the image.

I'll mention: a reference black sounds useful, but it actually isn't. It tells you far more about the nature of the black reference sample than about the light. For this work, we care only about the light.

If you have a large number of these to process, using Gimp or similar to get white samples would be tedious and error-prone. A fairly simple script with IM would find small areas without detail and extract white samples from those. My "Details, details" page shows various methods for this. On that page, I'm searching for areas of high detail. Exactly the same processes will find areas of low detail; just "-negate" the image before finding the lightest pixels.

You might want the paper to become white. However, for some purposes, you might prefer a neutral very light gray; a "-level" will do this. If you want to adjust the intensity but not the hue, "-colorspace gray" will do that.

Code: Select all

convert ^
  light.jpg ^
  ( +clone ^
    -sparse-color shepards "0,0 #bab5b1 %%[fx:w-1],0 #797471 0,%%[fx:h-1],#b7b6b3 %%[fx:w-1],%%[fx:h-1] #7c7876 57,403 #d6d2ce 576,58 #988d89" ^
    -level 0,90%% ^
    -colorspace gray ^
  ) ^
  +swap ^
  -compose Divide -composite ^
  b.png
Many variations are possible.
snibgo's IM pages: im.snibgo.com
bugbear
Posts: 33
Joined: 2010-10-21T03:06:35-07:00
Authentication code: 8675308

Re: Correcting non uniform light?

Post by bugbear »

snibgo wrote: If you have a large number of these to process, using Gimp or similar to get white samples would be tedious and error-prone. A fairly simple script with IM would find small areas without detail and extract white samples from those. My "Details, details" page shows various methods for this. On that page, I'm searching for areas of high detail. Exactly the same processes will find areas of low detail; just "-negate" the image before finding the lightest pixels.
Hmm. Perhaps a compromise would be to (manually) alpha-mask IN the areas of the original image you wish to use as samples (using Gimp), then use a script
to find the x,y <value> triples from this edited image.

In practise the masking need not be much more that simply clicking with a pen tool in the alpha channel.

BugBear
Post Reply