# Perspective correction

## Intro

This tutorial has no direct relevance to panorama creation. However, perspective correction is an issue for panorama creation as well, if you shoot your images tilted up or down. But please note that it is not necessary to correct the images prior to stitching - this is done in one go with alignment and stitching.

Falling lines can be annoying not only in architectural photography. In former times photographers used shift lenses (some times called Perspective Correction[*] lenses) to correct for that. These lenses usually are pretty expensive, not available in all needed focal lengths and limited to a certain amount of correction.

Panotools are able to simulate a shift lens in terms of geometry (obviously they can't simulate it in terms of Depth of Field). There are some easy and straightforward techniques to achieve perspective correction depending on the direction the camera was tilted.

## Prerequisites

This tutorial uses PTGui[*], but any of the GUI front-ends[*] would do. The usage of these programs is more or less identical although names differ sometimes a bit and some fields are in different locations. For hugin see tutorials on Simulating an architectural projection[*] and Perspective correction[*]

In all cases you start a new project and add the image you want to correct by pressing the Add button.

Go to Lens parameters tab and choose your lens type (presumable normal (rectilinear)). Enter the approximate Field of View if not already read from EXIF data (it's not necessary to have it exact). If you already have lens correction parameters enter them in the a, b and c fields else enter 0.0 for each.

In Panorama settings tab choose Rectilinear ('flat') as output.

### Warning

Newer PTGui versions require you to allow optimization for Roll and Pitch when using the internal optimizer together with horizontal or vertical control points. Use the Panorama Tools Optimizer in this case.

## Camera tilted up or down

Using a shift lens this would have been shot with camera level and the lens shifted up or down. The respective parameter to simulate this in panotools is Pitch.

### Camera level to the horizon

[*] [*] Falling lines example © Daniela Dürbeck | [*] [*] corrected image in Panorama Editor

If the camera was level to the horizon, one pair of vertical control points is enough to correct for falling lines. Go to Control points tab and set one point to the top of something vertical in your image in the left pane and one point at the bottom of the same vertical structure in the right pane. This vertical structure must not go through or near the image center. Be sure to choose Vertical line as control point type.

On Optimizer tab check Interface: Advanced. Check Pitch and Use control points of: for Image 0. All other fields are unchecked. Then press Run Optimizer and confirm the following dialog.

Once you're done proceed as described under Adjust output.

In the example image I set one point at the middle light at the top of the tower, another approximately in the middle as far down as possible. Click the thumbs to enlarge.

### Camera tilted and rotated

[*] [*] Falling lines example 2 © Erik Krause | [*] [*] Image in Control Point Editor
[*] [*] Corrected image in Panorama Editor

If the camera was rotated you need at least two vertical lines control point pairs. Go to Control points tab and set two point pairs on some vertical structures well apart from each other. For each line set a point in one pane at the bottom and in the other pane at the top of the same vertical structure. Be sure to choose Vertical line as control point type.

If there is no second vertical structure you can use a horizontal one with a horizontal control points pair instead.

In order to correct for perspective and image rotation you must optimize Pitch and Roll.

On Optimizer tab check Interface: Advanced. Check Pitch Roll and Use control points of: for Image 0. All other fields are unchecked. Then press Run Optimizer and confirm the following dialog.

Once you're done proceed as described under Adjust output.

In the example image I set one point pair at top and bottom of the leftmost edge of the altar and one point pair at the rightmost edge. Click the thumbs to enlarge.

## Camera panned sideways

[*] [*] Panned camera example © Tom! Striewisch | [*] [*] corrected image

Using a shift lens this would have been shot with camera film or sensor parallel to the subject and the lens shifted right or left. The respective parameter to simulate this in panotools is Yaw.

It is not often necessary to correct for a panned camera since we are used to horizontal perspective distortion. One use is to shoot a mirror without the camera beeing mirrored.

If the camera was level to the horizon, one pair of horizontal control points is enough to correct for horizontal perspective distortion. Go to Control points tab and set one point to the left of something horizontal in your image in the left pane and one point at the rigth of the same horizontal structure in the rigth pane. This horizontal structure must not go through or near the image center. Be sure to choose Horizontal line as control point type.

On Optimizer tab check Interface: Advanced. Check Yaw and Use control points of: for Image 0. All other fields are unchecked. Then press Run Optimizer and confirm the following dialog.

Once you're done proceed as described under Adjust output.

In the example image I set one point at the top border of the mirror at the left and right end. Click the thumbs to enlarge.

### Camera panned and rotated

If the camera was rotated during shooting you need at least two horizontal control point pairs or one horizontal and one vertical. Proceed in an analogous manner as for rotated camera if tilted upwards

## Camera panned, tilted and rotated

[*] [*] Tight spaces example © Bruno Postle | [*] [*] corrected image

Using a shift lens (provided you get one with a short enough focal length) this would have been shot with camera level and film or sensor parallel to the subject and the lens shifted diagonally. The respective parameters to simulate this in panotools are Yaw, Pitch and Roll.

Sometimes you have to shoot in very tight spaces but would like to show the image in a frontal view.

In this case two vertical pairs and two horizontal pairs are needed. Go to Control points tab and set two point pairs to the left and right of something horizontal in your image. Be sure to choose Horizontal line as control point type for these two and to set the lines as distant as possible.

Set two point pairs to the top and bottom of some vertical structure in your image. Be sure to choose Vertical line as control point type for these two. These two lines should be as distant as possible, too. There is no problem if horizontal and vertical lines cross.

On Optimizer tab check Interface: Advanced. Check Yaw, Pitch, Roll and eventually Field of View and Use control points of: for Image 0. In this case it is possibly necessary to optimize Field of View as well, because the optimizer won't find a good solution if it is wrong. All other fields are unchecked. Then press Run Optimizer and confirm the following dialog.

Once you're done proceed as described under Adjust output.

In the example image I set the lines at the vertical and horizontal edges between the slabs. Click the thumbs to enlarge. There is an excellent hugin tutorial available on how to correct this image: [1][*]

## Summary

Only few control point pairs are needed to correct for perspective distortion.

- Camera level. To correct for
  - falling lines (vertical perspective distortion) - set one pair to a vertical structure off center. Optimize for Pitch only.
  - horizontal perspective distortion - set one pair to a horizontal structure off center. Optimize for Yaw only.
- Camera rotated. To correct for
  - falling lines (vertical perspective distortion) - set at least one pair of vertical points to a vertical structure plus either one vertical pair apart from the other vertical one or a horizontal pair on a horizontal structure. Optimize for Pitch and Roll only.
  - horizontal perspective distortion - set at least one pair of horizontal points to a horizontal structure plus either one horizontal pair apart from the other horizontal one or a vertical pair on a vertical structure. Optimize for Yaw and Roll only.
- Camera tilted, panned and rotated - set two pairs to two different vertical structures apart from each other and two pairs to different horizontal structures apart from each other. Optimize for Yaw, Pitch, Roll and eventually Field of View.

## Adjust output

In any case you should get an average control point distance of almost 0.0. And in any case the image will be shifted in some direction, hence the output image will be larger. The width and height of the output image can be adjusted, but it is not possible to crop either side individually in PTGui.

Open Panorama Editor (Ctrl+E) and press the Fit panorama (Ctrl+F or menu: Edit -> Fit panorama) button. This will automatically adjust the output area to fit the corrected image.

You should see the image shifted in some direction with the empty space filled with black. You can adjust the width and heigth of the frame using the small sliders at the bottom and right side of the window (actually you adjust the output Field of View). The additional black space must be cropped in some image editor.

## Save

As a last step go to Create Panorama tab page and press Set optimum size. Choose either TIFF, JPG (and quality) or PNG as output and enter an appropriate file name. There should be a checkmark under Include Images. Then press Create Panorama.

## Batch process

If you want to batch process some equally sized images with the same correction values (only possible if you shot them all from a tripod without moving the camera) you can simply add them to the project on Source Images tab.

To correct them all for same Pitch and Roll (falling lines) you simply check the Link box on Optimizer tab for either parameter.

If you want to correct them all for Yaw too select the Yaw field on Image parameters tab and press Ctrl+C twice. Then hold down the shift key and press the down arrow until all Yaw field are selected. Press Ctrl+V twice to paste the value to all Yaw fields (same works for Pitch and Roll fields, too)

On Create Panorama tab page select Multi image TIFF for output. You get the images saved with the base name and a four digit number appended.

--Erik Krause[*] 18:38, 23 Jul 2005 (EDT)

Retrieved from "http://wiki.panotools.org/Perspective_correction"