pyvips
pyvips copied to clipboard
How i can use the below code to find the exact overlap.
For reference i am sending you the 2 images with 20 pixel overlap. So i can find exact displacement for exact overlap.

#!/usr/bin/python
import sys
import pyvips
# you can set the size of the window as well, but 10x10 pixels (the default) is
# probably fine
left = pyvips.Image.new_from_file(sys.argv[1])
right = pyvips.Image.new_from_file(sys.argv[2])
# the expected overlap, or the number of pixels the two images have in common
overlap = int(sys.argv[3])
# harea is the maximum displacement we detect, in pixels ... this is half
# the size of the area searched
# bandno is the band we search in ... green (band 1) is best for JPG images,
# since it has double the resolution
# dx0/dy0 are optional output args ... we set them True and pyvips will return
# their rvalues in result
join, optional = left.mosaic(right, 'horizontal',
left.width - overlap, 0, 0, 0,
harea=15, bandno=0,
dx0=True, dy0=True)
print 'horizontal offset =', optional['dx0']
print 'vertical offset =', optional['dy0']
print 'writing join to x.png ...'
join.write_to_file('x.png')
This is doing a 0th order (ie. just translation) join of two images left-right. It has a relatively robust thing for finding the overlap:
-
Split the overlap area into three sections, top middle and bottom.
-
Search the reference (left) edge for tie-points (high contrast areas) and collect the top 20 in each zone.
-
For each tie-point, search the corresponding area on the right, assuming the start overlap.
-
Put all the best match displacements into a model, find the best translation, and discard the worst pair of tie-points.
-
Repeat 4. until either we have a translation which fits all tie-points well, or we don't have enough tie-points left.
-
Do a feathered join, return the found best displacement.
With this pair of test images:

I see:
john@yingna ~/try $ ./find_overlap.py cd1.1.jpg cd1.2.jpg 100
horizontal offset = -426
vertical offset = 0
writing join to x.png ...

You don't need the joined images, so remove the write_to_file and it won't be generated.
In your LMS optimisation, you'll need to have something to handle missing displacements where the edges just didn't have enough information.
Originally posted by @jcupitt in https://github.com/libvips/pyvips/issues/40#issuecomment-386576906