9

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.

C.Radford
  • 882
  • 4
  • 13
  • 31

1 Answers1

8

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.

RuDevel
  • 694
  • 3
  • 14
  • Thanks mate that makes a lot of sense. I understand the concept but how does one norm an image area? My issue is the result layered/stitched image is a single image not an image with multiple layers. – C.Radford Feb 22 '17 at 21:29
  • Sorry, norming is the wrong name here- just settin all pixel values to 1 should do the trick as well. I am expanding my answert right now.. – RuDevel Feb 22 '17 at 21:35
  • Thanks mate this works great with the exception of one small thing in your code. dst = np.zeros((100,100),dtype=np.int) should be dst = np.zeros((100,100),np.uint8). This really helped thanks – C.Radford Mar 03 '17 at 14:58
  • So I have been fiddling with this but my issues is I am not working with square boundaries. Do you know how I can do this exact process but setting the inside of a contour to 1 – C.Radford Mar 07 '17 at 19:48
  • @C.Radford did you find a way to calculate the overlap area of the two images. I am also working on a similar problem of image stitching. I need the overlap area for seam finding – Adi shukla Jun 01 '21 at 13:57
  • Sorry @Adishukla I did not. – C.Radford Jun 02 '21 at 11:17
  • @Adishukla: Sorry, I thought it's obvious the rectangles do not need to be completely filled - nor do they need to be squared. You take every shape you have, "paint" it inside of a rectangular and overlap the rectangulars. For the example above let's assume no image pixel is black. So you make two rectangles of the total rectangle size and set all pixels that are non-zero to one for each patch. You overlap the two rectangles and will have the value 2 for every position that is overlapping. – RuDevel Jun 29 '21 at 20:37