OpenCV determine area of intersect/overlap

 ̄綄美尐妖づ 提交于 2021-02-06 11:09:35

问题


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.

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.

Any help is appreciated.


回答1:


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) + src1 (a) = overlap (c)

Hope that helps.



来源:https://stackoverflow.com/questions/42402303/opencv-determine-area-of-intersect-overlap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!