使用OpenCV和Python将多边形从一幅图像复制到另一幅图像

6

我有两张图片。我想从一张图片中抓取一个区域(多边形,而非矩形),并将该区域复制到另一张图片上。我该如何实现?以下是我目前的进展。

import cv2
import numpy as np

#load two images
srcfilename = 'foo.jpg'
src1 = cv2.imread(srcfilename)
srcfilename = 'bar.jpg'
src2 = cv2.imread(srcfilename)


src1_mask = np.zeros(src1.shape[:-1])
#create a polygon for region of interest
poly = np.array([ [150,150], [200,100], [350,150], [350,200], [300,220], [200,200], [190,180] ], np.int32)
cv2.fillPoly(src1_mask, [poly], 255)

此时,我已经加载了两个图像,并为该区域创建了多边形和掩码。现在我不知道如何使用这个掩码/多边形将src1的那一部分复制到src2中。

#I can also create a mask that has the same number of channels (3)
src1_mask = np.zeros(src1.shape)
#create a polygon for region of interest
poly = np.array([ [150,150], [200,100], [350,150], [350,200], [300,220], [200,200], [190,180] ], np.int32)
cv2.fillPoly(src1_mask, [poly], (255,255,255))

最终版

我已经成功地使用位运算实现了我的目标,以下是示例代码。

import cv2
import numpy as np
import matplotlib.pyplot as pst

#load two images
srcfilename = 'foo.jpg'
src1 = cv2.imread(srcfilename)
srcfilename = 'bar.jpg'
src2 = cv2.imread(srcfilename)

#create mask template
src1_mask = src1.copy()
src1_mask = cv2.cvtColor(src1_mask,cv2.COLOR_BGR2GRAY)
src1_mask.fill(0)

#define polygon around region
poly = np.array([ [0,0], [20,0], [65,40], [150,40], [225,5], [225,170],[120,200], [10,190] ], np.int32)

#fill polygon in mask
_ = cv2.fillPoly(src1_mask, [poly], 255)

#create region of interest
roi = src2[np.min(poly[:,1]):np.max(poly[:,1]),np.min(poly[:,0]):np.max(poly[:,0])]
mask = src1_mask[np.min(poly[:,1]):np.max(poly[:,1]),np.min(poly[:,0]):np.max(poly[:,0])]

mask_inv = cv2.bitwise_not(mask)
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
src1_cut = src1[np.min(poly[:,1]):np.max(poly[:,1]),np.min(poly[:,0]):np.max(poly[:,0])]

img2_fg = cv2.bitwise_and(src1_cut,src1_cut,mask = mask)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
src2_final = src2.copy()
src2_final[np.min(poly[:,1]):np.max(poly[:,1]),np.min(poly[:,0]):np.max(poly[:,0])] = dst

plt.imshow(cv2.cvtColor(src2_final, cv2.COLOR_BGR2RGB))

1
非常好的答案,我搜索了很久才找到它,甚至尝试了下面的答案,但我可能太蠢了,非常感谢! - NoBlockhit
2个回答

5
假设src1是您的图像,src1_mask是您的二进制掩码:
    src1_mask=cv2.cvtColor(src1_mask,cv2.COLOR_GRAY2BGR)#change mask to a 3 channel image 
    mask_out=cv2.subtract(src1_mask,src1)
    mask_out=cv2.subtract(src1_mask,mask_out)

现在mask_out包含了你定义的多边形内部位于图像src1中的部分。

2
假设 src1 是源图像,poly 是您的多边形,src2 是您想将poly复制到的目标图像。
poly复制到蒙版中:
mask = np.zeros(src1.shape)
cv2.fillPoly(mask,[poly],1)
poly_copied = np.multiply(mask,src1)

在 src2 中遮罩多边形:

mask = np.ones(src2)
#assuming src1 and src2 are of same size
cv2.fillPoly(mask,[poly],0)
src2 = np.multiply(mask,src2)

将poly复制到src2:
src2 = np.add(poly_copied,src2)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接