【发布时间】:2021-10-18 18:14:01
【问题描述】:
我有两个图像,我需要使用 numpy 和 opencv 使用 numpy 掩码将前景覆盖在背景之上。
import numpy as np
import cv2
import matplotlib.pyplot as plt
background = cv2.imread("background.jpg")
foreground = cv2.imread("foreground.png")
foreground = cv2.cvtColor(foreground ,cv2.COLOR_BGR2GRAY)
background = cv2.cvtColor(background ,cv2.COLOR_BGR2GRAY)
arr = []
for i in range(foreground.shape[1]): #parse forground pixels
temp_row = []
for j in range(foreground.shape[0]):
if((foreground [i][j] == 0)):#if pixel transperant draw background
temp_row.append(background[i][j])
else: # draw forground
temp_row.append(foreground[i][j])
arr.append(temp_row)
res_im = np.array(arr)
plt.figure()
plt.imshow(res_im, cmap='gray', vmin=0, vmax=255)
plt.show()
我使用了这个解决方案,但被告知我需要使用口罩.. 帮助?
【问题讨论】: