Merging scanned images from the command line

Asked by John Allie on 2017-12-11

Hello,
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: https://wiki.panotools.org/Panorama_scripting_in_a_nutshell 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: https://postimg.org/image/67yfx78bp/
And here is what I get when I follow the tutorial: https://postimg.org/image/4g5h1rtyd/

Thanks for any help!

Question information

Language:
English Edit question
Status:
Solved
For:
Hugin Edit question
Assignee:
No assignee Edit question
Solved by:
John Allie
Solved:
2017-12-26
Last query:
2017-12-26
Last reply:
2017-12-25
John Allie (johnallie) said : #1

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: https://s18.postimg.org/vvqv8lpll/perfect_web.jpg

But then I did some more testing and found that sometimes it still suffers from some minor distortion (note lower-left corner): https://s18.postimg.org/j4cp27q55/minor_web.jpg

And in some cases (especially artwork without black panel borders) it screws up very badly indeed: https://s18.postimg.org/rmm56leo9/major_web.jpg

Thanks again for any suggestions!

tmodes (tmodes) said : #2

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).

John Allie (johnallie) said : #3

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

Top=$1
Bottom=$2

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

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