OpenCV确定相交/重叠区域

2024-10-08 22:28:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用OpenCV和python创建一个缝合程序,目前正在很好地缝合图像,现在正在尝试将它们混合在一起。最终的目标将是使用一个图形切割,以更好地缝合它们,但现在我只是重叠图像的基础上发现的单应。

这是我目前的结果时,缝合两个图像的照片。 enter image description here

我的目标是确定重叠的区域,并将其放入一个可以应用于右上角图像的遮罩中(即按层放置在顶部的图像),这样我就可以使用opencv使用的任何一个混合器或其他算法根据距离混合它。

这是我要找的东西的图像。 enter image description here

如有任何帮助,我们将不胜感激。


Tags: 图像程序算法图形区域距离目标opencv
1条回答
网友
1楼 · 发布于 2024-10-08 22:28:08

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

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

然后将所有这些目标图像相加。 具有0的区域将被覆盖,1覆盖和2 to n将意味着被2 to n图像覆盖。

当使用numpy的广播工具时,这是非常简单和有效的。

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=重叠(c)enter image description here

希望能有所帮助。

相关问题 更多 >

    热门问题