How to stitch images together which have overlapping?

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
vittyvirus
Posts: 4
Joined: 2016-03-29T22:24:29-07:00
Authentication code: 1151

How to stitch images together which have overlapping?

Post by vittyvirus »

So I've got 8 images, and I need to stitch them together vertically/horizontally but only in one direction. They've got 10% overlap (i.e. 10% area is common in two adjacent images), so I can't simply use append. I'm a beginner in ImageMagick so I need some help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to stitch images together which have overlapping?

Post by snibgo »

Do the images have transparency?

What do you want to do with the overlaps? A simple alpha-blend might work, so one side of the overlap comes from one image, the other side from the other image, with a blend between them. For some images, a Minimum Error Boundary Cut works well. There are many possibilities. I show some methods on my "Dark paths" and "Blending Pyramids" pages.

If you show some example images, advice might be more specific.
snibgo's IM pages: im.snibgo.com
vittyvirus
Posts: 4
Joined: 2016-03-29T22:24:29-07:00
Authentication code: 1151

Re: How to stitch images together which have overlapping?

Post by vittyvirus »

I just want to create a panorama/mosiac of the images. I could do the job with a photo editor but there's lots of data.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to stitch images together which have overlapping?

Post by fmw42 »

There are lots of ways to merge. Snibgo is just trying to pin down the method you might prefer. A small set of images might help. You can post to some free hosting service and put the URLs here. Also always provide your IM version and platform, since syntax may differ.

If you read his web pages on the relevant topics, you can ask more explicit question.

To start, you either need to match a subsection of one image in the overlay with the full overlap in the other image (see compare) or find small high detail regions in one image overlap and match each to the correspond feature in the other image,

Once you know the offset, then you have to choose between blending and a seam merge or a combination.

All this is quite complex in IM, since there is no one function to do that. So you will have to script it.

You might be better off using some panorama stitching tool. Search Google. There are a number of them.
vittyvirus
Posts: 4
Joined: 2016-03-29T22:24:29-07:00
Authentication code: 1151

Re: How to stitch images together which have overlapping?

Post by vittyvirus »

fmw42 wrote:There are lots of ways to merge. Snibgo is just trying to pin down the method you might prefer. A small set of images might help. You can post to some free hosting service and put the URLs here. Also always provide your IM version and platform, since syntax may differ.

If you read his web pages on the relevant topics, you can ask more explicit question.

To start, you either need to match a subsection of one image in the overlay with the full overlap in the other image (see compare) or find small high detail regions in one image overlap and match each to the correspond feature in the other image,

Once you know the offset, then you have to choose between blending and a seam merge or a combination.

All this is quite complex in IM, since there is no one function to do that. So you will have to script it.

You might be better off using some panorama stitching tool. Search Google. There are a number of them.
Thank you for your response. I should have made it very clear that I just wanted to plain overlap images over other. Turns out this code:

Code: Select all

montage -geometry <offsets> a.png b.png o.png
does the trick for me.

I would like to ask you, is their anyway I could calculate these alignment offsets through imagemagick? Let me make what I want to do more clear. Say I have two different images of stars, but in the two images there is a significant amount of similar stars (overlap). Now I want to make one image out of these two. I'll do that by using montage. But I also need to calculate the horizontal and vertical offset so that the images are correctly aligned. Is there a way to do that automatically?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to stitch images together which have overlapping?

Post by fmw42 »

You need to crop out a subset of the overlap region in one image and compare it to the full overlap from the other image. That will give you the offsets. See compare
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/

If the x or y does not shift much than you can restrict the search area appropriately to cut down time.
vittyvirus
Posts: 4
Joined: 2016-03-29T22:24:29-07:00
Authentication code: 1151

Re: How to stitch images together which have overlapping?

Post by vittyvirus »

fmw42 wrote:You need to crop out a subset of the overlap region in one image and compare it to the full overlap from the other image. That will give you the offsets. See compare
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/

If the x or y does not shift much than you can restrict the search area appropriately to cut down time.
You're a magician!
I think I got the idea. Suppose I need to align images a.png and b.png, and both images are greyscale. I could use brute force by adding black region to the two corners of b.png until the difference gets minimum, but that would be computationally costly (binary search would help, though). How would you do it?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to stitch images together which have overlapping?

Post by fmw42 »

I think I got the idea. Suppose I need to align images a.png and b.png, and both images are greyscale. I could use brute force by adding black region to the two corners of b.png until the difference gets minimum, but that would be computationally costly (binary search would help, though). How would you do it?
I have no idea what you are asking. There is no need to add black to use compare

Code: Select all

compare -metric rmse -subimage-search smallimage largeimage null:
will return the best match location and its rmse difference.

You just need to crop the one image overlap region to some subset around the best viewed star pattern as the smallimage . Then search the overlap subsection in the other image as the largeimage.

If you have something else in mind, please clarify furhter or provide diagrams or actual images for us to see and test.

If you are on Unix (Linux, Mac OSX, or Windows w/Cygwin), then I have some fast correlation scripts on the link below including normalized cross correlation. The run order of magnitude time faster than IM compare.

See also
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076
http://www.fmwconcepts.com/imagemagick/ ... mcrosscorr
http://www.fmwconcepts.com/imagemagick/ ... TCHING.pdf
Post Reply