Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV determine area of intersect/overlap

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.

like image 435
C.Radford Avatar asked Feb 22 '17 21:02

C.Radford


1 Answers

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.

like image 108
RuDevel Avatar answered Oct 25 '22 15:10

RuDevel