使用opencv将一个图像覆盖在另一个具有透明背景的图像上
Overlaying an image over another image both with transparent background using opencv
我正在尝试使用 opencv.Both 将一张图片放在另一张图片之上。图片具有透明背景。这是我正在尝试的code
s_img = cv2.imread("obama2.png", -1)
l_img = cv2.imread('obama.png',-1)
x_offset = 162
y_offset = 69
y1, y2 = y_offset, y_offset + s_img.shape[0]
x1, x2 = x_offset, x_offset + s_img.shape[1]
alpha_s = s_img[:, :, 3] / 255.0
alpha_l = 1.0 - alpha_s
for c in range(0, 3):
l_img[y1:y2, x1:x2, c] = (alpha_s * s_img[:, :, c] +
alpha_l * l_img[y1:y2, x1:x2, c])
cv2.imwrite('final.png',l_img)
obama.png
obama2.png
final.png
我希望第二个 obama2.png 位于 obama.png 之上(类似于 imagemagick/libvips 中的复合函数)。
我该怎么做才能获得想要的图像
这给了我想要的解决方案,但如果可能的话我更喜欢更好的解决方案
s_img = cv2.imread("obama2.png", -1)
l_img = cv2.imread('obama.png',-1)
for i in range(0,s_img.shape[0]):
for j in range(0,s_img.shape[1]):
if s_img[i][j][3]!=0:
l_img[i+y_offset][j+x_offset][0:3] = s_img[i][j][0:3]
l_img[i+y_offset][j+x_offset][3] = 255
cv2.imwrite('final2.png',l_img)
编辑:看来我错过了一些基本的东西。我必须在循环时考虑 alpha 通道,因为背景图像也具有透明度。
s_img = cv2.imread("obama2.png", -1)
l_img = cv2.imread('obama.png',-1)
x_offset = 162
y_offset = 69
y1, y2 = y_offset, y_offset + s_img.shape[0]
x1, x2 = x_offset, x_offset + s_img.shape[1]
alpha_s = s_img[:, :, 3] / 255.0
alpha_l = 1.0 - alpha_s
for c in range(0, 4):
l_img[y1:y2, x1:x2, c] = (alpha_s * s_img[:, :, c] +
alpha_l * l_img[y1:y2, x1:x2, c])
cv2.imwrite('final.png',l_img)
抱歉,我不太了解OpenCV。但我认为您可能忘记在计算放置位置时减去 obama2 图像宽度的一半。偏移量是相对于 obama2 图像左上角到 obama 图像左上角的偏移量。你可能测量到脸部的中心,在奥巴马图像上大约是 x=162。
但是如果你会用Imagemagick,那过程就很简单了:
convert obama.png obama2.png -geometry +82-6 -compose over -composite result.png
-geometry 参数仅定义 obama2 图像相对于 obama 图像左上角的偏移量。如果我测量奥巴马图像中面部的中心,我得到大约 x=176。所以我要减去obama2宽度的一半,也就是x=192/2=96。所以偏移量将是 x=176-96=80。我将其调整为 82 并将 y=-6 设置为稍微向上移动。
如果您需要 Python 解决方案,那么您可以使用基于 Imagemagick
的 Wand
我正在尝试使用 opencv.Both 将一张图片放在另一张图片之上。图片具有透明背景。这是我正在尝试的code
s_img = cv2.imread("obama2.png", -1)
l_img = cv2.imread('obama.png',-1)
x_offset = 162
y_offset = 69
y1, y2 = y_offset, y_offset + s_img.shape[0]
x1, x2 = x_offset, x_offset + s_img.shape[1]
alpha_s = s_img[:, :, 3] / 255.0
alpha_l = 1.0 - alpha_s
for c in range(0, 3):
l_img[y1:y2, x1:x2, c] = (alpha_s * s_img[:, :, c] +
alpha_l * l_img[y1:y2, x1:x2, c])
cv2.imwrite('final.png',l_img)
obama.png
obama2.png
final.png
我希望第二个 obama2.png 位于 obama.png 之上(类似于 imagemagick/libvips 中的复合函数)。
我该怎么做才能获得想要的图像
这给了我想要的解决方案,但如果可能的话我更喜欢更好的解决方案
s_img = cv2.imread("obama2.png", -1)
l_img = cv2.imread('obama.png',-1)
for i in range(0,s_img.shape[0]):
for j in range(0,s_img.shape[1]):
if s_img[i][j][3]!=0:
l_img[i+y_offset][j+x_offset][0:3] = s_img[i][j][0:3]
l_img[i+y_offset][j+x_offset][3] = 255
cv2.imwrite('final2.png',l_img)
编辑:看来我错过了一些基本的东西。我必须在循环时考虑 alpha 通道,因为背景图像也具有透明度。
s_img = cv2.imread("obama2.png", -1)
l_img = cv2.imread('obama.png',-1)
x_offset = 162
y_offset = 69
y1, y2 = y_offset, y_offset + s_img.shape[0]
x1, x2 = x_offset, x_offset + s_img.shape[1]
alpha_s = s_img[:, :, 3] / 255.0
alpha_l = 1.0 - alpha_s
for c in range(0, 4):
l_img[y1:y2, x1:x2, c] = (alpha_s * s_img[:, :, c] +
alpha_l * l_img[y1:y2, x1:x2, c])
cv2.imwrite('final.png',l_img)
抱歉,我不太了解OpenCV。但我认为您可能忘记在计算放置位置时减去 obama2 图像宽度的一半。偏移量是相对于 obama2 图像左上角到 obama 图像左上角的偏移量。你可能测量到脸部的中心,在奥巴马图像上大约是 x=162。
但是如果你会用Imagemagick,那过程就很简单了:
convert obama.png obama2.png -geometry +82-6 -compose over -composite result.png
-geometry 参数仅定义 obama2 图像相对于 obama 图像左上角的偏移量。如果我测量奥巴马图像中面部的中心,我得到大约 x=176。所以我要减去obama2宽度的一半,也就是x=192/2=96。所以偏移量将是 x=176-96=80。我将其调整为 82 并将 y=-6 设置为稍微向上移动。
如果您需要 Python 解决方案,那么您可以使用基于 Imagemagick
的 Wand