Request: Vignette Correction

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
Jim

Request: Vignette Correction

Post by Jim »

Vignette correction (or flat-field correction) is a common photographic manipulation that must be done before images are stitched together in a panorama or seamless montage. Basically, the edges of an image get less illumination because optical systems have less efficiency as you move off-axis, and, this has to be corrected.

A correction image is just an image of a white object taken under the same conditions (same objective & lighting) as the image to be processed. The following command currently works to correct an image but is *VERY* slow:
convert input.tif \( "correction_image.tif" -colorspace Gray \) -fx "u/(v/v.p{w/2,h/2})" output.tif

It multiplies each pixel of the input image by a floating point factor which is calculated as the pixel value at the some position in a image divided by the value at the same position in a correction image normalized to the central pixel value in the correction image.

Basically, this operation multiplies two images together then divides by a fixed value. The reason this requires a new feature is that you can't use "composite -compose multiply a b c" followed by division because you can easily overflow the max value of the image. Another higher-order problem is that vignette correction should really be done for each color channel independently, and not the simpler way I'm doing is using grayscale compression of all the color channels in the correction image. And, the extremem slowness of this -fx operation compared with the high speed I've seen from commercial packages makes me think this should be a new feature.

So, my request is a new feature in composite:
composite -devignette_gray <images ...> -correction_image <image> <output>
composite -devignette_color <images ...> -correction_image <image> <output>

Where each image in the list is corrected using the designated correction image. The _gray uses the algorithm above, and, the _color would correct each color channel using the corresponding color channel of the correction_image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Request: Vignette Correction

Post by fmw42 »

Jim wrote:Vignette correction (or flat-field correction) is a common photographic manipulation that must be done before images are stitched together in a panorama or seamless montage. Basically, the edges of an image get less illumination because optical systems have less efficiency as you move off-axis, and, this has to be corrected.

A correction image is just an image of a white object taken under the same conditions (same objective & lighting) as the image to be processed. The following command currently works to correct an image but is *VERY* slow:
convert input.tif \( "correction_image.tif" -colorspace Gray \) -fx "u/(v/v.p{w/2,h/2})" output.tif

It multiplies each pixel of the input image by a floating point factor which is calculated as the pixel value at the some position in a image divided by the value at the same position in a correction image normalized to the central pixel value in the correction image.

Basically, this operation multiplies two images together then divides by a fixed value. The reason this requires a new feature is that you can't use "composite -compose multiply a b c" followed by division because you can easily overflow the max value of the image. Another higher-order problem is that vignette correction should really be done for each color channel independently, and not the simpler way I'm doing is using grayscale compression of all the color channels in the correction image. And, the extremem slowness of this -fx operation compared with the high speed I've seen from commercial packages makes me think this should be a new feature.

So, my request is a new feature in composite:
composite -devignette_gray <images ...> -correction_image <image> <output>
composite -devignette_color <images ...> -correction_image <image> <output>

Where each image in the list is corrected using the designated correction image. The _gray uses the algorithm above, and, the _color would correct each color channel using the corresponding color channel of the correction_image.
You can correct for vignette using -compose divide -composite.
See
http://www.imagemagick.org/Usage/compose/#divide
and
http://www.imagemagick.org/Usage/photos/#vignettation
Jim

Re: Request: Vignette Correction

Post by Jim »

Yes, I can see that. I avoided the -compose divide solution because the web page suggested to avoid it. If you look right below that, you can see the command I'm using. Also, if you look at my post, you'll see that I changed the order of operations from Martin's solution to avoid what might be roundoff error, depending upon how imagemagick casts variable types.

The fact remains that the -fx solution proposed is very slow and less than ideal. That's why I'm requesting a new feature. I think that's completely legitimate.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Request: Vignette Correction

Post by fmw42 »

Jim wrote:Yes, I can see that. I avoided the -compose divide solution because the web page suggested to avoid it. If you look right below that, you can see the command I'm using. Also, if you look at my post, you'll see that I changed the order of operations from Martin's solution to avoid what might be roundoff error, depending upon how imagemagick casts variable types.

The fact remains that the -fx solution proposed is very slow and less than ideal. That's why I'm requesting a new feature. I think that's completely legitimate.

I still suggest you TRY the -compose divide -composite approach. It is a lot faster than -fx.

(I don't see where the web page says to avoid it. It says JPEGs are not recommended.) Hopefully Anthony will respond and clarify.

The fact remains that you have nothing to lose to try -compose divide -composite and tweak that as needed, just as the -fx solution was tweaked.

The history is that when the -fx solution was suggested, the -compose divide option was not recognized as being available. I was the one that found it was available. I needed it for other things, but Anthony suggested that it then could also be used for removing vignette, as he saw the potential to replace the fx solution.

Anyway, he can comment further.

I would suggest that you post an example of your image(s). Then we can see what might be the issue. I would be willing to take a stab at implementing some command lines or a script.

From your -fx code, I would not expect that to be hard to do with -compose divide -composite. You just need to preprocess the normalized denominator image. The normalization ought be be achievable with -evaluate multiply after finding the value in the center of the normalization image, which is easily found. An example would be most useful to test with.
Last edited by fmw42 on 2008-10-03T15:56:59-07:00, edited 3 times in total.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Request: Vignette Correction

Post by magick »

Agreed, we'll see if Anthony can solve this with existing fast operators. If not, we can create a new fast operator for devignetting as you suggested.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Request: Vignette Correction

Post by anthony »

The only reason the divide soltion was not correct is that the reference image may need to be adjusted so that the center of the image is a proper white.

I am not a photographer, and I am not certian on the best way to 'fix' the reference image so a simple division can be used.

That is all.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Request: Vignette Correction

Post by el_supremo »

If you use the images in Anthony's example (http://www.imagemagick.org/Usage/photos/#vignettation) this command will partially correct the vignetting.

Code: Select all

convert vegas_orig.jpg ( vegas_nikon18-70dx_18mm_f3.5.jpg -colorspace gray -negate ) -compose color-dodge -composite vegas_dodge.jpg
The dodge operation brightens the photo based on the intensity of the reference image - black will produce no change, which is why the -negate is required so that the bright central portion of the reference does not change the vegas photo.
The reason it is only a partial correction is that the reference photo does not accurately represent the amount of vignetting in the original image. The reference was probably taken with a different focal length and/or aperture than the vegas photo.
If you add

Code: Select all

-modulate 160
immediately before the ")" it will reduce the vignetting even more - but only for this specific image.
I haven't got a lens which vignettes so I can't test this further.

Pete
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Request: Vignette Correction

Post by fmw42 »

Does anyone have a proper pair of images -- a vignetted image and a reference image? I would like to experiment on a script or technique, but can not find anything suitable by searching Google! If you are willing to provide such a pair, you can link here or email me directly (fmw at alink dot net)
Post Reply