OpenCV determine area of intersect/overlap

C.Radford picture C.Radford · Feb 22, 2017 · Viewed 10.8k times · Source

I am creating a stitching program using OpenCV and python and currently am stitching the images well and am now trying to blend them together. The ultimate goal will be to use a graph cut to better stitch them but for now I am just overlapping the images based on their found homography.

Here is a photo of my current result when stitching two images. enter image description here

My goal is to determine the area of overlap and put it into a mask that I can apply to the top right image (that is the one on top in terms of layers) so I can blend it based on the distance using any of there blender opencv uses or another algorithm.

Here is a visual of what I am looking for. enter image description here

Any help is appreciated.

Answer

RuDevel picture RuDevel · Feb 22, 2017

How about creating a mask/binary image of both and use logical AND?

You could also translate a gray valued copy of each of your images (image content all ones) to a fresh copy of the destination (initialized with zeros) for each.

Then you add all of those destination images up. Areas with 0 would then be uncovered, 1 covered and 2 to n would mean covered by 2 to n images.

This is very easy and efficient when using numpy's broadcasting tools.

import cv2
import numpy as np

#our target area (the black background)
dst = np.zeros((100,100),dtype=np.int)
src1 = dst.copy() 
src2 = dst.copy()
src1[50:,50:] = 1 #fake of first translated image (row/col 50-end)
src2[:70,:70] = 1 #fake of second translated image (row/col 0-70)

overlap = src1+src2 #sum of both *element-wise*

cv2.imwrite('a.png', src1*255) #opencv likes it's grey images span from 0-255
cv2.imwrite('b.png', src2*255) #...
cv2.imwrite('c.png', overlap*127) #here vals 0-2, *127 gives (almost) 255 again

np.where(overlap==2) #gives you a mask with all pixels that have value 2

src2 (b) enter image description here + src1 (a) enter image description here = overlap (c) enter image description here

Hope that helps.