Author Topic: Stitching images together  (Read 3214 times)

PeterC

  • Administrator
  • Member
  • *****
  • Posts: 483
  • Passionate about my one-place study
    • View Profile
    • Holywell-cum-Needingworth History
Stitching images together
« on: 19 December 2016, 20:43:33 »
Assuming we have photographs of a map, using the principles contained in Using photography to get a good enough quality image of a map, then they will be able to be stitched together to create a full image of the map. Note that it is best not to crop the photographs as this loses the EXIF data.

(Note that this procedure has not yet been tested on very large, or crumpled maps)

To stitch images we use the FOSS panorama stitching system, Hugin. (From there we can see a manual, which includes the section on the Panorama Editor window and tutorials.)

The most pertinent tutorial is about Stitching flat scanned images, on which the following is based (but assuming we have taken photoraphs with a camera rather than used a scanner).

The basic process for stitching overlapping images together is:
  • Put copies of the images of the map into a working directory (do not delete or alter them after we have started with Hugin)
  • Check each of them for suitability (focus, exposure etc), and ensure we have sufficient (?10-30%) overlaps
  • Open Hugin (I am using v 2016.2.0)
  • Menu option Interface/ Expert - thereby opening the Panorama Editor window as well as the Fast Panorama Preview
  • Either
    • Select just one (corner) image, set vertical and horizontal control points as accurately as possible on this image, and optimise just that one image for just the Lens Parameter b (barrel), or
    • Use a known value of the Lens Parameter b (eg from a previous stitch with same camera/lens)
  • From here on fix the Lens Parameter b (barrel) at that value, until the final optimisation
  • Under the photos tab click on Add Images, and select all the rest of your photographs (although for large numbers of images, consider adding images only a few at a time, processing, then adding a few more)
  • (if necessary drag the images to reorder them into a logical ordering)
  • If the EXIF data is missing from a file just enter 40 in the HFOV(v) - the value (5-40) doesn't matter since the picture is the same regardless
  • If we want to change the anchor image choose it (perhaps the one in the middle) - right click on it and select Anchor this image for position, and then again Anchor this image for exposure
  • If we had EXIF data, for each image after the first, right cick on it and select Lens/ new lens (this tells Hugin that it should  not assume its default behaviour of assuming everything was taken by the same camera from the same point) - no need to do this
  • Use a strategy for fixing control points:
    • Automatic
      • Under Feature Matching (generating control points) select CPFind multirow/stacked as the tool to use (in fact the normal CPFind seems to work just as well)
      • Make sure no images are selected and click on Create control points - this creates points on images that match up exactly with points on other images (it should have done all pairs!)
      • Under the Photos tab right click on any image and click on Control points/ Clean Control points then any duplicates in the whole project will be deleted - we may need to do this more than once, but it is best not to do it again after we have started adding control points manually
      • Go through all the control points and remove any obviously spurious ones (this happens quite a lot with maps because of standard symbols and lettering), as well as the vertical and horizontal control points added previously
      • Also go through the pairs of images above each other and add control points if necessary (the CPFind tool is not good at doing this unless pairwise)
    • By row (because the horizontal generation of control points works better than the vertical)
      • Under Feature Matching (generating control points) select CPFind as the tool to use
      • Select all the images in one row and click on Create control points - this creates points on images that match up exactly with points on other images across the row
      • Repeat the above for each row in turn, making sure that other images are not selected
      • Go through all the images, and consider each of the images after it
      • Manually remove any obviously spurious control points
      • For the image verticaly above or below the given image use the CPFind tool on just that pair to generate control points between them
  • Still on the photos tab at the bottom by Optimise Geometric select Custom parameters
  • There will now have appeared an Optimiser tab up the top - click on this
  • Right click on Yaw(y) and Unselect all - this has the effect of not allowing the y parameter to be altered by the optimiser when we run it
  • Do the same for Pitch(p) and Roll(r)
  • Right click on X(TrX) and Select all, and repeat for Y(TrY) - thereby telling the optimiser it is allowed to alter the X and Y parameters (ie this is like shuffling the images around on a table, but not adjusting them in any other way)
  • Note that if you have added images since the last Optimisation you will need to do the Select All process on X and Y again
  • The optimiser now knows what it is allowed to vary to get as near a match as possible, so Click on Optimise Now!(top right) - this gets the photos in roughly the right place as a starting point
  • Assuming the images are taken regularly (eg left to right then bottom to top) the X and Y parameters should vary regularly, so adjust any manually that are way out, and then optimise again - the results should be better
  • We are aiming for average control point distance of under 1 pixel but at this point, for various reasons, it may only be 40-100
  • From now on frequently save the project (a pto file containing text instructions for Hugin) usually in the same folder as the images
  • Use the View menu, or button, to open the Fast Panorama preview
  • Leave the lens type as normal(Rectilinear) but go to the Projection tab and set the projection to Rectilinear
  • Click on the Fit button (top left) to fit the panorama to the window
  • NEVER touch the center icon - per Marius - Why?
  • If we do need to move the panorama then ensure we use mosaic drag mode
  • Manipulate the control points (see below) and repeat the Optimisation until the average distance does not change much (probably about 10)
  • It is not worth adding in vertical and horizontal control points to ensure the map is aligned correctly, which helps any future trimming and stitching with other maps, as it can corrupt the stitching - we will cope with this after the stitching
  • If we are adding more images, it may be worth fixing some parameters for the old ones so they do not get messed up
  • Refine the Optimisation step by step under the Optimiser tab (the average distance should come down at each step):
    • Right click on Roll(r) and Select all, then Optimise Now!
    • Right click on Z(TrZ) and Select all, then Optimise Now!
    • Right click on Yaw(y) and Select all, do the same for Pitch(p), then Optimise Now!
    • Right click on Lens Parameter b (barrel) and Select all, then Optimise Now!
    • Check the Control Point table for any remaining large distances and delete or fine tune
    • If the average distance is down to under 1 pixel that is good
  • (Note that Hugin needs such careful optimisation only because we are trying to do something with it that it is not primarily designed for)
  • Under the Photos tab/ Optimise Photometric leave it as low dynamic range, and click Calculate - this evens out the exposure
  • Go to the Fast Panorama preview, and the Crop tab, click on Autocrop (and adjust cropping rectangle if us want)
  • Under the Stitcher tab, do not Calculate Field of View as this would change the cropping
  • Click on Calculate optimal size (but we can change this if we want - keeping the same aspect ratio)
  • Leave everything else as the default and click on Stitch! (bottom right)
  • Check the image
    • If there are significant mismatches then add more control points (either using the generator or manually) and repeat the process
    • If there is a black area in the stitched image, but not in the preview this is a glitch and should go away when we make a small change and re-stitch, or use the inbuilt stitcher
    • processing the resulting stitched image is described in the georeferencing process
  • When satisfactory, record the value of the Lens Parameter b (barrel) for use as an intial value when you next do a stitch with same camera/lens)
Manipulating control points
  • The tools we have are:
    • In the Fast Panorama preview, the Layout tab shows us the strength and distance of the control points between each pair of images (green=good, thick=many, grey=none) - click on any line opens the Control Points tab of the Panorama Editor
    • In the Fast Panorama preview (Preview tab) you can do Control point editing by clicking on the Edit CP icon, then using the mouse to drag out a box to enclose some bad control points, or enclose an area where we want some new control points
    • In the Panorama Editor the Control Points tab shows a pair of images and the control points between them, both graphically and in a list - this is the main tool for deleting, adding and amending control points
    • By selecting the same image in the left and right panes we can add vertical (and horizontal) control points - but make sure the lines are exactly vertical (or horizontal) and we have chosen corresponding parts of the same line
    • We can also add vertical (and horizontal) control points after selecting different images in the left and right panes but we need to specify they are vertical (or horizontal) - this is useful if choose corresponding parts of the same line in two corner images, as it makes sure the whole map is correctly aligned for future stitching edge to edge
    • Under the Photos tab if we select 2 or more images and click on Create control points, they are created just between those images. (Note that if we repeat it duplicates are created, so it is best not to repeat it for any pair unless we have deleted all the existing ones first)
    • (Do not Clean Control points at this point because besides any duplicates any control points we have added manually in the whole project will be deleted)
    • View menu Control Point table - an editable table of control points, click on distance to sort by it (twice for descending)
  • The strategy is:
    • Using the Fast Panorama preview Layout tab to identify pairs of images that should have control points between them but don't (grey lines)
    • For such a pair Create control points just for that pair
    • Do similar for pairs that have only weak links, but delete the existing control points before creating new control points (to avoid creating duplicates)
    • Use the Control Point table to identify large distances
    • Under the Control points tab delete control points that are ill-defined, too near the edge, or spurious, by selecting them and pressing the delete key
    • Under the Control Points tab work through all the pairs of images to check the control points are as expected, adding and/or fine tuning as appropriate
    • When the control points are OK redo the optimisation from the Optimiser tab
Pre-processing DNG files
  • Before being used within Hugin DNG files need some pre-processing
  • To read the DNG (RAW) files and batch convert them into portable 16bit per channel PPM, and then create 16bit per channel TIFFs, use something like:
    • dcraw -v -w -4 dscn*.nef
    • convert -rotate 270 -gamma 2.2 dscn3088.ppm dscn3088.tif  (ImageMagick)
  • Or this can be done individually using urfraw (Manual)
Stitching map sheets edge to edgeOther useful materials can be found at:Terminology and Notes
  • field of view (FOV) ("wrongly used instead of angle of view") is how many degrees of view are included in an image (typically 50°)
  • Hugin uses horizontal field of view (hFoV) and vertical field of view (vFoV)
  • on the Photos tab, the Optimiser tab is only displayed when using the Custom parameters for Geometric Optimisation, similarly the Exposure tab is only displayed when using the Custom parameters for Photometric Optimisation
  • see the manual for more on Optimising
  • the aspect ratio is the ratio (proportion) between width and height of an image, in pixels - cameras are often 4:3
  • normal (rectilinear) - the lens type used by most standard cameras
  • the anchor image is the one that will not be moved around
  • optical axis is the centre longitudinal (in the sense of straight ahead) axis of an optical system
  • yaw, pitch and roll
    • Yaw (y): From side to side (Positive to the right, -180° to +180°)
    • Pitch (p): up and down (Positive is up, -90° to +90°)
    • Roll (r): around the optical axis (like a steering wheel, Positive clockwise, -180° to +180°)
  • Position Image variables
    • X, Y, Z (translation)
    • Tpy Translation plane yaw , Tpp Translation plane pitch
  • Lens Image variables
    • v: Field of View (FoV)
    • a, b and c barrel distortion, pincushion distortion and even wavy distortion.
    • d, e lens shift parameters that correct for the lens optical axis not being in the image center.
    • f, g shear parameters correct for image errors from a scanner
    • ?? horizontal and vertical image shearing (g, t) – eg top to right, bottom to left– no info
  • a stack is images already aligned (eg taken from same point without altering anything) - not relevant to us
  • EV stands for Exposure Value - a standard photographic scale; each increase or decrease by one unit will change the exposure by the equivalent of one f-stop (i.e. halving or doubling the exposure). It is worth adjusting the exposure here in Hugin rather than later in an external image editor, since Hugin uses the camera response curve calculated in the Exposure tab to perform the correction in a linear colour space.
  • Exposure optimisation: can modify
    • the exposure EV
    • the red multiplier Er
    • the blue multiplier Eb.
  • an experimental control points creation algorithm can be invoked by pressing the 'g' key. Corners in the currently selected image are detected, and corresponding control points are set based on the current relative positions of the two images – there are other Hugin Keyboard shortcuts on the same page
  • The warning (of the Dijkstra optimizer) is benign
  • Hugin can produce successful panoramas shot with cameras that always shoot using auto-exposure and auto-whitebalance.
  • Scripting is possible
  • autopano and Pan-o-matic used to be ways to create the project with control points but they are no longer supported - use CPFind (multirow/stacked) instead
  • celeste attempts to identify 'sky' control points and delete them (as clouds move between shots)
  • I had a useful conversation with several hugin users about stitching maps
Odd Notes to be processed
  • Remove borders from images
  • Rename in some logical way (letter better than numbers as Hugin uses numbers!)
  • Print off images
  • Check locations, clarity
  • Work out where they go
  • Start with the largest scale/ clearest
  • Add one at a time
  • Make the lenses all the same
  • add control points pairwise
  • Check each pair for outliers /approximates
  • Optimise just XYZ for one image (maybe Z after XY)
  • Check Preview each time
  • Keeping saving
  • Mask out fuzzy bits (later as control points do not work)
  • Use Ctl Left click to select/unselect individual variables
  • Add images one by one and only control point pairwise
  • do X Y first
  • Include Z where relevant (camera was closer)
  • Order of overlay https://photo.stackexchange.com/questions/27452/hugin-image-layer-order
  • Blender enblen or inbuilt if black bits!

« Last Edit: 20 January 2018, 14:34:04 by PeterC »