OpenCV 确定相交/重叠区域 [英] OpenCV determine area of intersect/overlap

查看:107
本文介绍了OpenCV 确定相交/重叠区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OpenCV 和 python 创建一个拼接程序,目前正在很好地拼接图像,现在正在尝试将它们混合在一起.最终目标是使用图形切割来更好地缝合它们,但现在我只是根据它们找到的单应性重叠图像.

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.

我的目标是确定重叠区域并将其放入一个蒙版中,我可以将其应用到右上角的图像(即图层的顶部),以便我可以根据距离使用任何那里有 Blender opencv 使用或其他算法.

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.

感谢任何帮助.

推荐答案

如何创建两者的掩码/二进制图像并使用逻辑与?

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

您还可以将每个图像的灰度值副本(图像内容均为 1)转换为每个目标的新副本(用零初始化).

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.

然后将所有这些目标图像添加起来.0 的区域将被覆盖,1 被覆盖,2 到 n 意味着被 2 到 n 图像覆盖.

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.

这在使用 numpy 的广播工具时非常容易和高效.

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)= 重叠 (c)

src2 (b) + src1 (a) = overlap (c)

希望有所帮助.

这篇关于OpenCV 确定相交/重叠区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆