## Algorithmic vignetting correction for pinhole cameras?

- Mark Sirota
**Posts:**19**Joined:**2011-04-27T09:04:26-07:00**Authentication code:**8675308**Location:**Philadelphia, PA, USA

### Algorithmic vignetting correction for pinhole cameras?

After being so impressed by this community following the Correcting for a curved film plane? discussion, I've got a followup to ask...

Pinhole cameras generally need significant vignetting correction. They tend to have very wide angles of view, which means that the corners of the frame are much further from the pinhole than the center. Light falls off with the inverse square of the distance form the source; a common pinhole camera that has a frame three times as wide as its focal length will have about two stops of falloff at the corners (that is, the corners receive 1/4 as much light as the center). The formula is falloff = ((d/ƒ)^2, where d is the distance from image center and ƒ is the focal length of the camera.

I've written a tool to generate Adobe Lens Profiles to correct for this in Adobe Camera Raw and Lightroom:

... but due to the way Adobe's lens profiles are implemented, they are far from perfect. You can't separate the X axis from the Y (as you'd need to do for the curved film plane cameras discussed in the other thread), and they approximate the falloff using a limited 6th degree polynomial but using only the coefficients for the 2nd, 4th, and 6th terms, which approximates the actual curve poorly in many cases, as seen in the graphs in this feedback to Adobe.

While I know this correction can likely already be done in ImageMagick using maps, because the math is so simple it would be great to be able to do this in a straightforward way on the command line without going through that process. The user would only need to specify the frame/focal-length ratio in both the X and Y axes, then you calculate the falloff and correct algorithmically.

Is this already doable in a straightforward way? If not, can I beg for a command-line implementation? If so, then for pinhole photographers it becomes a simple pass through ImageMagick for vignetting and distortion correction before importing into Lightroom or Photoshop.

Thanks for considering this!

Pinhole cameras generally need significant vignetting correction. They tend to have very wide angles of view, which means that the corners of the frame are much further from the pinhole than the center. Light falls off with the inverse square of the distance form the source; a common pinhole camera that has a frame three times as wide as its focal length will have about two stops of falloff at the corners (that is, the corners receive 1/4 as much light as the center). The formula is falloff = ((d/ƒ)^2, where d is the distance from image center and ƒ is the focal length of the camera.

I've written a tool to generate Adobe Lens Profiles to correct for this in Adobe Camera Raw and Lightroom:

... but due to the way Adobe's lens profiles are implemented, they are far from perfect. You can't separate the X axis from the Y (as you'd need to do for the curved film plane cameras discussed in the other thread), and they approximate the falloff using a limited 6th degree polynomial but using only the coefficients for the 2nd, 4th, and 6th terms, which approximates the actual curve poorly in many cases, as seen in the graphs in this feedback to Adobe.

While I know this correction can likely already be done in ImageMagick using maps, because the math is so simple it would be great to be able to do this in a straightforward way on the command line without going through that process. The user would only need to specify the frame/focal-length ratio in both the X and Y axes, then you calculate the falloff and correct algorithmically.

Is this already doable in a straightforward way? If not, can I beg for a command-line implementation? If so, then for pinhole photographers it becomes a simple pass through ImageMagick for vignetting and distortion correction before importing into Lightroom or Photoshop.

Thanks for considering this!

### Re: Algorithmic vignetting correction for pinhole cameras?

Mark,

If I understand what you want --

For pinhole (frame) camera's, something like this is done with -compose divide. See http://www.imagemagick.org/Usage/compose/#divide (or possibly -compose minus)

You can make your radial map easily with the linear radial-gradient: and then apply a (d/f)^2 intensity transformation to adjust for the focal length using -fx.

see http://www.imagemagick.org/Usage/canvas ... l-gradient

So it seems a couple of lines of code should work and can probably be combined into one command line.

Are you needing this for the curved film? Perhaps I misunderstand about all the coefficients part. Can you explain in more detail?

Can you provide links to a (pinhole/frame camera) image and the f value that needs correcting so we can test out approaches.

Fred

If I understand what you want --

For pinhole (frame) camera's, something like this is done with -compose divide. See http://www.imagemagick.org/Usage/compose/#divide (or possibly -compose minus)

You can make your radial map easily with the linear radial-gradient: and then apply a (d/f)^2 intensity transformation to adjust for the focal length using -fx.

see http://www.imagemagick.org/Usage/canvas ... l-gradient

So it seems a couple of lines of code should work and can probably be combined into one command line.

Are you needing this for the curved film? Perhaps I misunderstand about all the coefficients part. Can you explain in more detail?

Can you provide links to a (pinhole/frame camera) image and the f value that needs correcting so we can test out approaches.

Fred

Last edited by fmw42 on 2011-06-16T11:20:03-07:00, edited 1 time in total.

### Re: Algorithmic vignetting correction for pinhole cameras?

Don't you mean the roll-off is (f/d)^2 if it is inverse square.Light falls off with the inverse square of the distance form the source; a common pinhole camera that has a frame three times as wide as its focal length will have about two stops of falloff at the corners (that is, the corners receive 1/4 as much light as the center). The formula is falloff = ((d/ƒ)^2, where d is the distance from image center and ƒ is the focal length of the camera.

This seems to work to give the corners at about 1/4 the center.

width=150 # width in mm

f=50 # focal length in mm (1/3 of width)

dim=150 # width=height in pixels for image

convert -size ${dim}x${dim} xc: -monitor -fx "rr=($width/$dim)*sqrt((i-w/2)^2+(j-h/2)^2); ($f/(rr+quantumscale))^2" rolloff.png

convert rolloff.png[1x1+0+0] -format "%[fx:u]" info:

0.222217

- Mark Sirota
**Posts:**19**Joined:**2011-04-27T09:04:26-07:00**Authentication code:**8675308**Location:**Philadelphia, PA, USA

### Re: Algorithmic vignetting correction for pinhole cameras?

Do be more clear, 1/((d/ƒ)^2) is the amount of light received at that location.

Your image doesn't look like real falloff. It's too uniform in the center, and the contrast is too high/sharp at the edge of your circle.

Your image doesn't look like real falloff. It's too uniform in the center, and the contrast is too high/sharp at the edge of your circle.

### Re: Algorithmic vignetting correction for pinhole cameras?

1/((d/f)^2) = (f/d)^2Mark Sirota wrote:Do be more clear, 1/((d/ƒ)^2) is the amount of light received at that location.

Your image doesn't look like real falloff. It's too uniform in the center, and the contrast is too high/sharp at the edge of your circle.

That is the formula that I programmed into the fx expression, where d=radial distance from the center. So perhaps there is some other scaling factor?

If this is not correct, we will have to understand the proper equation for the rolloff before we can test with fx. Then once we understand that, it may be possible to speed it up. If not, then Anthony will need to decide if he has time to implement anything. But the first step is understanding the rolloff. Then the correction.

### Re: Algorithmic vignetting correction for pinhole cameras?

Is it possible d is not the distance within the image from the image center to some pixel, but from the focal point to some pixel in the image?

If this is the case then you have:

width=150

f=50

dim=150

ff=`convert xc: -format "%[fx:$f*$dim/$width]" info:`

convert -size ${dim}x${dim} xc: -monitor -fx "rr=sqrt((i-w/2)^2+(j-h/2)^2); ($ff)^2/(rr^2+$ff^2))" rolloff2.png

convert rolloff2.png[1x1+0+0] -format "%[fx:u]" info:

0.181811

im_profile rolloff2.png[150x1+0+75] rolloff2_profile.gif

Looking for vignetting in pinhole cameras comes up with:

http://photo.net/alternative-cameras-forum/00RB8s

http://forums.adobe.com/thread/747149

this last one seems to show the same formula you expressed and a similar graph to mine above. further down they apply it to a midgray image to form the correction, which appears to be the image you showed in your original post.

So creating the correction image would be:

convert rolloff2.png \( -clone 0 -fill "gray(50%)" -colorize 100 \) -compose multiply -composite -negate rolloff2_correction.png

So here is a command line for doing this much and also using radial-gradient to speed it up.

width=150

f=50

dim=150

dim2=`convert xc: -format "%[fx:$dim*sqrt(2)]" info:`

dimi2=`convert xc: -format "%[fx:$dim/sqrt(2)]" info:`

ff=`convert xc: -format "%[fx:$f*$dim/$width]" info:`

convert -size ${dim2}x${dim2} radial-gradient: \

-gravity center -crop ${dim}x${dim}+0+0 +repage -negate \

-monitor -fx "($ff)^2/((u*$dimi2)^2+$ff^2))" +monitor \

\( -clone 0 -fill "gray(50%)" -colorize 100 \) \

-compose multiply -composite -negate rolloff2_correction2.png

If this is the case then you have:

width=150

f=50

dim=150

ff=`convert xc: -format "%[fx:$f*$dim/$width]" info:`

convert -size ${dim}x${dim} xc: -monitor -fx "rr=sqrt((i-w/2)^2+(j-h/2)^2); ($ff)^2/(rr^2+$ff^2))" rolloff2.png

convert rolloff2.png[1x1+0+0] -format "%[fx:u]" info:

0.181811

im_profile rolloff2.png[150x1+0+75] rolloff2_profile.gif

Looking for vignetting in pinhole cameras comes up with:

http://photo.net/alternative-cameras-forum/00RB8s

http://forums.adobe.com/thread/747149

this last one seems to show the same formula you expressed and a similar graph to mine above. further down they apply it to a midgray image to form the correction, which appears to be the image you showed in your original post.

So creating the correction image would be:

convert rolloff2.png \( -clone 0 -fill "gray(50%)" -colorize 100 \) -compose multiply -composite -negate rolloff2_correction.png

So here is a command line for doing this much and also using radial-gradient to speed it up.

width=150

f=50

dim=150

dim2=`convert xc: -format "%[fx:$dim*sqrt(2)]" info:`

dimi2=`convert xc: -format "%[fx:$dim/sqrt(2)]" info:`

ff=`convert xc: -format "%[fx:$f*$dim/$width]" info:`

convert -size ${dim2}x${dim2} radial-gradient: \

-gravity center -crop ${dim}x${dim}+0+0 +repage -negate \

-monitor -fx "($ff)^2/((u*$dimi2)^2+$ff^2))" +monitor \

\( -clone 0 -fill "gray(50%)" -colorize 100 \) \

-compose multiply -composite -negate rolloff2_correction2.png

- Mark Sirota
**Posts:**19**Joined:**2011-04-27T09:04:26-07:00**Authentication code:**8675308**Location:**Philadelphia, PA, USA

### Re: Algorithmic vignetting correction for pinhole cameras?

That looks more like it. How about separating horizontal from vertical? For example, a camera with a curved film plane would have vignetting in this pattern along the Y axis, but none on the X axis.

### Re: Algorithmic vignetting correction for pinhole cameras?

Mark Sirota wrote:That looks more like it. How about separating horizontal from vertical? For example, a camera with a curved film plane would have vignetting in this pattern along the Y axis, but none on the X axis.

That is even easier as it is basically one dimensional and so will be very fast:

height=150

f=50

dim=150

ff=`convert xc: -format "%[fx:$f*$dim/$height]" info:`

convert -size 1x${dim} xc: \

\-monitor -fx "yy=(j-h/2); ($ff)^2/((yy)^2+$ff^2))" +monitor \

-scale ${dim}x${dim}! \

\( -clone 0 -fill "gray(50%)" -colorize 100 \) \

-compose multiply -composite -negate rolloff2_correction3.png

And here it is applied to your P90 image all in one convert command. Note with IM there is no need to apply the rolloff to the gray image. When using -compose divide, you want the center to be pure white and rolloff appropriately as white is equivalent to 1 in the range 0 to 1 for the way IM treats images internally.

infile="P90achop.jpg"

inname=`convert $infile -format "%t" info:`

height=60

f=57

inname=`convert $infile -format "%t" info:`

dim=`convert $infile -format "%h" info:`

size=`convert $infile -format "%wx%h" info:`

ff=`convert xc: -format "%[fx:$f*$dim/$height]" info:`

convert -size 1x${dim} xc: \

-monitor -fx "yy=(j-h/2); ($ff)^2/((yy)^2+$ff^2))" +monitor \

-scale ${size}! $infile \

-compose divide -composite ${inname}_vignette_corrected.jpg

You will have to download each image and alternate them to compare them to see the difference or do -compose difference.

Last edited by fmw42 on 2011-06-16T16:59:47-07:00, edited 4 times in total.

- anthony
**Posts:**8858**Joined:**2004-05-31T19:27:03-07:00**Authentication code:**8675308**Location:**Brisbane, Australia

### Re: Algorithmic vignetting correction for pinhole cameras?

This is explored in a more practical way in IM examples, Photo Handling, Vignette Removalfmw42 wrote:For pinhole (frame) camera's, something like this is done with -compose divide. See http://www.imagemagick.org/Usage/compose/#divide (or possibly -compose minus)

http://www.imagemagick.org/Usage/photos/#vignettation

As to working out the formula for the vignettation image, you may also need to take into account the elliptical angle of the 'pin-hole', not just the distance.

At a 45 degree angle (the edge of a 90 degree FOV) any circle will have only 71% (sin of 45 degrees) of the area because of the angle of light passing through the pinhole. However it may be more than this as the material the pin hole is made in will also have some thickness.

The only true way to get a accurate viginatte image is to take a photo of uniformly illuminated white sheet of paper (actually any uniform color except black will work for this). The multiply that image by the average color of the center area (to take care of inconsistencies in film and pixels) and you have your divisor image (center white).

Things I can see effecting vignettation include

- Distance of film from lens, further away means more light spread.
- Area of the aperture 'circle' (lens or pinhole) due to angle of light.
- Arrangement of camera material around the aperture. For example the lens holder or pinhole thickness.

Anthony Thyssen -- Webmaster for ImageMagick Example Pages

http://www.imagemagick.org/Usage/

http://www.imagemagick.org/Usage/

### Re: Algorithmic vignetting correction for pinhole cameras?

Sorry Anthony,

I was just modifying my message above to do just that for the curved film P90 camera (I also pointed out earlier the divide method, though I pointed to your -compose divide example -- I was not aware of your vignette correction examples -- so thanks for pointing those out). Our messages crossed and you got yours in before I finished editing mine. So see above for my results.

You have some good points about the pinhole camera however. With the curved film, the pinhole will project to a vertical ellipse of varying sizes depending upon y coord relative to the center. But I am not sure how that affects vignetting as opposed to the distortion correction needed for the geometry. Perhaps it is a second order effect in relation to the 1/r^2 effect.

Fred

I was just modifying my message above to do just that for the curved film P90 camera (I also pointed out earlier the divide method, though I pointed to your -compose divide example -- I was not aware of your vignette correction examples -- so thanks for pointing those out). Our messages crossed and you got yours in before I finished editing mine. So see above for my results.

You have some good points about the pinhole camera however. With the curved film, the pinhole will project to a vertical ellipse of varying sizes depending upon y coord relative to the center. But I am not sure how that affects vignetting as opposed to the distortion correction needed for the geometry. Perhaps it is a second order effect in relation to the 1/r^2 effect.

Fred

- Mark Sirota
**Posts:**19**Joined:**2011-04-27T09:04:26-07:00**Authentication code:**8675308**Location:**Philadelphia, PA, USA

### Re: Algorithmic vignetting correction for pinhole cameras?

All true regarding other sources of vignetting for pinholes. The best pinholes are drilled with beveled edges rather than as cylinders to minimize this, and some people are making them out of clear film now, with a black coating except in the pinhole area. These don't work as well with very wide angles, but they keep dust out and are more reliable/consistent than laser-drilled or mechanically drilled pinholes in brass.

My goal is to handle the dominant source of distortion in wide-angle pinholes, which is the type caused by the varying distance from pinhole to film. The others can be minimized using high quality pinholes, and can be fully corrected as you say using reference images, though it's harder than you might think to have a perfectly evenly illuminated surface to shoot.

My goal is to handle the dominant source of distortion in wide-angle pinholes, which is the type caused by the varying distance from pinhole to film. The others can be minimized using high quality pinholes, and can be fully corrected as you say using reference images, though it's harder than you might think to have a perfectly evenly illuminated surface to shoot.

- anthony
**Posts:**8858**Joined:**2004-05-31T19:27:03-07:00**Authentication code:**8675308**Location:**Brisbane, Australia

### Re: Algorithmic vignetting correction for pinhole cameras?

Not just Y coordinate, X coordinate too! The film may be curving around, so the light striking the film is perpendicular, but the pinhole itself is no longer perpendicular! In the Y coordinate you get distance, pinhole angle, and light to film angle to take care of. That last two however is the same effect, just doubled, the effect the X coodinate gets.fmw42 wrote:You have some good points about the pinhole camera however. With the curved film, the pinhole will project to a vertical ellipse of varying sizes depending upon y coord relative to the center.

i would not discount the effect of light angle though the pinhole. that 71% reduction at the side edges of a P90 film is not to be taken lightly. And that effect is doubled for the Y direction. This is of course on top of the Y distance effect!Mark Sirota wrote:My goal is to handle the dominant source of distortion in wide-angle pinholes, which is the type caused by the varying distance from pinhole to film. The others can be minimized using high quality pinholes, and can be fully corrected as you say using reference images, though it's harder than you might think to have a perfectly evenly illuminated surface to shoot.

This was NOT taking into account any 'depth' that the pinhole has, only the circle area of the pinhole (no matter how big it is). Angle effects as I see it are a sin() function from maximum, times 1 for X and times 2 for Y, distance effect is also a sin() function of the angle. However with Y the angle never reaches 90 degrees and is non-linear, and with X it is linear, and reached 45 degrees at edges (still just as strong as Y just a different shape).

In summery...

X is 1 times sin() angle being linear with X/R

Y is 3 times sin() but with arctan() of Y/R (worked out using Pythagorean sqrt() functions)

Anthony Thyssen -- Webmaster for ImageMagick Example Pages

http://www.imagemagick.org/Usage/

http://www.imagemagick.org/Usage/

- Mark Sirota
**Posts:**19**Joined:**2011-04-27T09:04:26-07:00**Authentication code:**8675308**Location:**Philadelphia, PA, USA

### Re: Algorithmic vignetting correction for pinhole cameras?

Apologies for the long delay...

But it makes sense to include both factors when building vignetting correction into the workflow, whether or not the film plane was curved. I appreciate your analysis!

I've been giving this a lot of thought, and agree that this can be a significant factor. It's a greater factor in the P.90 than in similar cameras with flat film planes, because they already have significant vignetting in the horizontal which the P.90 avoids with its curved film plane.i would not discount the effect of light angle though the pinhole. that 71% reduction at the side edges of a P90 film is not to be taken lightly. And that effect is doubled for the Y direction. This is of course on top of the Y distance effect!

But it makes sense to include both factors when building vignetting correction into the workflow, whether or not the film plane was curved. I appreciate your analysis!

- anthony
**Posts:**8858**Joined:**2004-05-31T19:27:03-07:00**Authentication code:**8675308**Location:**Brisbane, Australia

### Re: Algorithmic vignetting correction for pinhole cameras?

It is only a rough analysis. And would appreciate any further development you may find if you take this any further.

Of particular interest would be a comparison of actual vignette image data (white paper) against the relative vignette determined by mathematics (against the central pixel).

A camera such as this is particularly useful as a check of the mathematics, as you do get a different equation for X and Y axis vignette, rather than just a radial vignette.

Of particular interest would be a comparison of actual vignette image data (white paper) against the relative vignette determined by mathematics (against the central pixel).

A camera such as this is particularly useful as a check of the mathematics, as you do get a different equation for X and Y axis vignette, rather than just a radial vignette.

Anthony Thyssen -- Webmaster for ImageMagick Example Pages

http://www.imagemagick.org/Usage/

http://www.imagemagick.org/Usage/