Merging scanned images from the command line

Asked by John Allie

I scan a lot of artwork on my flatbed scanner but because the pieces are larger than the scanning glass I need to scan them in pieces and then stitch them back together. I'm looking to automate this process as much as possible, so I was following the tutorial from here: to learn how to control Hugin from the command line. Following the tutorial as written, my output had discarded the top half of the artwork and there was considerable distortion added.

So, in short, what is the correct procedure to follow to simply align and merge two images without applying any distortion correction?

Here is what I'm trying to do:
And here is what I get when I follow the tutorial:

Thanks for any help!

Question information

English Edit question
Hugin Edit question
No assignee Edit question
Solved by:
John Allie
Last query:
Last reply:
Revision history for this message
John Allie (johnallie) said :

I've been doing some more experimenting and I've found a method that _almost_ works. In fact it does work some of the time, but it's unreliable.

This is the procedure I'm using:
pto_gen -o page.pto -p 0 -f 10 *.png
cpfind -o page_cp.pto --multirow page.pto
cpclean -o page_clean.pto page_cp.pto
linefind -o page_lf.pto page_clean.pto
autooptimiser -a -m -l -s -o page_optimized.pto page_lf.pto
pano_modify --canvas=AUTO --crop=AUTO -o page_ready.pto page_optimized.pto
hugin_executor --stitching --prefix=page page_ready.pto

Ironically, in the case of my original test images, it worked absolutely perfectly and I thought I was done:

But then I did some more testing and found that sometimes it still suffers from some minor distortion (note lower-left corner):

And in some cases (especially artwork without black panel borders) it screws up very badly indeed:

Thanks again for any suggestions!

Revision history for this message
tmodes (tmodes) said :

One issue could be the use of linefind. If there no real vertical lines then this confuses Hugin. So maybe try without linefind.

Another issue could be the output projection. Instead of using autooptimiser -s to select a suitable projection I would recommend to use a fixed projection - rectilinear (pano_modify --projection=0).

Revision history for this message
John Allie (johnallie) said :

Thanks for the suggestion! I found this tutorial and was able to adapt their script to suit my purposes. I don't entirely understand how it works, but it does what I want it to, so I can't complain!

Here's what I finally came up with, with a couple imagemagick commands thrown in to rotate the scans and add a small buffer to the bottom (useful because Hugin has a tendency to cut it close on the bottom edge).

#!/usr/bin/env bash


if [ $# -ne 2 ]
    echo "Specify TOP and BOTTOM files"
    exit 1

mogrify -rotate 90 $Top

mogrify -rotate -90 $Bottom
mogrify -gravity south -background white -splice 0x80 $Bottom

pto_gen --projection=0 --fov=10 -o project.pto $Top $Bottom
pto_lensstack -o project1.pto --new-lens i1 project.pto
cpfind -o project1.pto --multirow project1.pto
cpclean -o project2.pto project1.pto
linefind -o project3.pto project2.pto
pto_var -o project4.pto --opt r,d,e,!r0,!d0,!e0 project3.pto
autooptimiser -n -o project5.pto project4.pto
pano_modify --projection=0 --fov=AUTO --center --canvas=AUTO --crop=AUTO --ldr-file=JPG --ldr-compression=100 -o project6.pto project5.pto
hugin_executor --stitching --prefix=page project6.pto

rm *.pto