如何水平和垂直连接两个 alpha 图像?

问题描述

我使用 alpha 蒙版创建了直角三角形图像。我想垂直和水平地加入它们。 面具:An alpha mask for right angle triangles

Another alpha mask for right angle triangles

切片后的图像是: Right Angle Triangles using the first alpha mask

Right Angle Triangles using the second alpha mask

我想通过以下方式合并两个直角三角形:

Desired Output

但是,我一直输出不正确: Incorrect output

我想获得一些有关如何解决此问题的指导,以便获得所需的输出

连接三角形的代码: 我使用了以下示例:Masking two images and merge into one image

def mirror_triangles_one():
  x=first_array[0] #The first array contains right angle triangles sliced using alpha mask one
  y=second_array[2] #The second array contains right angle triangles sliced using alpha mask two
  x_im=x
  x_im.paste(y,(-4,0),y)
  x_im.save('mirror_five.png')
  return x_im

原始图片-Original Image

解决方法

这是我在 Python/OpenCV/Numpy 中的做法。但我不得不裁剪你的图像,所以结果不会准确。假设您已将三角形图像填充为带有白色的矩形,则只需 hstack 的两个图像。请注意,它们必须具有相同的高度。

左:

enter image description here

对:

enter image description here

import cv2
import numpy as np

# read right image
right = cv2.imread('right.png')
rhh,rww = right.shape[:2]

# read left image
left = cv2.imread('left.png')
lhh,lww = left.shape[:2]

# compute min height and crop images to same height
minh = min(rhh,lhh)
right = right[0:minh,0:rww]
left = left[0:minh,0:lww]

# stack horizontally
result = np.hstack([left,right])

# save result
cv2.imwrite('left_right_result.jpg',result)

# display result
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

enter image description here