Stitch several images that partially overlap into one

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
fttpic
Posts: 8
Joined: 2016-12-20T11:14:36-07:00
Authentication code: 1151

Stitch several images that partially overlap into one

Post by fttpic »

I have several pictures which partially overlap, and I want to get the bigger one, with auto-align and auto-blend, like Adobe Auto-Blend Layers:
https://helpx.adobe.com/photoshop/using ... ayers.html

Example source images:

Image

Image

Expected result:
Image

Of course the alignment would be any x-y direction, and with all the partial images you will get a square or rectangular result with no missing data.

The idea is to join partial screenshots.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Stitch several images that partially overlap into one

Post by snibgo »

IM has no single command that will do this. But scripts can be written, and some of my pages have scripts for aligning by different methods.

Your problem has no unique solution, of course. For example, the images could overlap so the right-side of the first image (which is white) matches any white area on the left of the second image.

The two images can be trimmed to remove the white space, so there is only one solution. This can be found slowly by a brute-force search, or more quickly by making a Gaussian pyramid from each, and doing a small-space brute force search at each level.

Sorry, I don't have an open-source script for the task.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Stitch several images that partially overlap into one

Post by snibgo »

An alternative technique is to trim the white off one image, then crop the four margins from that. Each margin could be, say, 20 pixels high or wide. Search for each margin within the other image. With luck, one of the margins (and only one) is found within the other image. For example:

Code: Select all

convert textL.png ( textR.png -crop 20x+0+0 +repage ) -process srchimg NULL:
0 @ 163,0
So the left margin of the second image is found within the first, at (163,0).
snibgo's IM pages: im.snibgo.com
fttpic
Posts: 8
Joined: 2016-12-20T11:14:36-07:00
Authentication code: 1151

Re: Stitch several images that partially overlap into one

Post by fttpic »

snibgo wrote: 2017-12-22T02:47:58-07:00Your problem has no unique solution, of course. For example, the images could overlap so the right-side of the first image (which is white) matches any white area on the left of the second image.
If we specify that overlaps must happen, and in the content where it is distinguishable, the first image will never be at the right, top or bottom of the second. If would be mandatory that at least two different colors match and try to overlap as much as possible in case letters repeat. But maybe wasn't the best example, if we get images that can only overlap in a single position you can easily tell by the eye.

Another example:

Image
fttpic
Posts: 8
Joined: 2016-12-20T11:14:36-07:00
Authentication code: 1151

Re: Stitch several images that partially overlap into one

Post by fttpic »

Of course the partial images would be in a lossless format like BMP or PNG, otherwise it would not be a 100% match.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Stitch several images that partially overlap into one

Post by Bonzo »

I suggest you try some panorama software like Microsoft ICE or Hugin which may be better as it has more options.
fttpic
Posts: 8
Joined: 2016-12-20T11:14:36-07:00
Authentication code: 1151

Re: Stitch several images that partially overlap into one

Post by fttpic »

Ok thank you all. Finally solved the issue with OpenCV. It has a High level stitching API (Stitcher class) called cv::Stitcher, which is perfect for my needs.

There are several things to keep in mind though about this and other software you recommended:

- FFmpeg is mainly for video manipulation so no use here.

- ImageMagick is oriented for single-picture manipulation only, so not much use here either.

- Microsoft Image Composite Editor (ICE) is a pain in the ass to use and does not auto-detect the position of each picture. It's horrible.

- Hugin is great but requires you a lot of work to reference pictures spatially, so it is very tedious when you have dozens of pictures to manipulate. Furthermore it's not easy to automate because of that.

- cv::Stitcher from OpenCV is the best, however requires a lot of overlap, otherwise the following error will be thrown:

Code: Select all

Can't stitch images, error code = 1
The sample cv::Stitcher has to be compiled from source code because the binaries are not included for any operating system.
fttpic
Posts: 8
Joined: 2016-12-20T11:14:36-07:00
Authentication code: 1151

Re: Stitch several images that partially overlap into one

Post by fttpic »

Knowing how much each image has to be translated, these commands are the deal:
anthony wrote: 2016-10-23T18:41:25-07:00 Update IM Examples Layering, Append with Overlap
http://www.imagemagick.org/Usage/layers/#append_overlap
Give it a couple of hours to update.

However I would point out that a calculated FX solution while 'not as simple' will also let you append with overlap a whole sequence of images. All the offsets are relative to the previous image (using its calculated position and size). This works for any images, or any (and variable) size, and in any direction! This is wny it is a more 'general' solution.

In this case next image is overlaps by 8 pixels then moved down 4 pixels.

Code: Select all

convert font_[0-9].gif \
           -set page '+%[fx:u[t-1]page.x+u[t-1].w-8]+%[fx:u[t-1]page.y+4]' \
           -background none -layers merge +repage show:
Image

See results and more detailed description in (when it updates)...
http://www.imagemagick.org/Usage/layers/#layer_calc_inc
Post Reply