import cv2
import numpy as np
lena = cv2.imread('lena.jpg')
height, width = lena.shape[:2]
a = 250 # equilateral triangle side
h = a * (3 ** .5) / 2 # equilateral triangle height
# triangle points to be used for masking the image
tri = np.array([[0, h], [a / 2, 0], [a, h]], np.float32)
mask = np.zeros((height, width), dtype=np.uint8)
cv2.fillConvexPoly(mask, tri.astype(np.int32), (255, 255, 255))
tri_crop = cv2.bitwise_and(lena, lena, mask=mask)
# new triangle points
tri_trans = np.array([[3 * a / 2, 0], [a / 2, 0], [a, h]], np.float32)
# transform inital triangle to new triangle points
aff_trans = cv2.getAffineTransform(tri, tri_trans)
tri_warp = cv2.warpAffine(tri_crop, aff_trans, (width, height))
# join triangles together
new_canvas = cv2.bitwise_or(tri_crop, tri_warp)
cv2.imwrite('lena.png', new_canvas)
输出:
所以你可以看到划分两个三角形的线 - 连接不是无缝的 有关如何使连接无缝的任何建议吗?
答案 0 :(得分:0)
现在看看这个魔术:
tri_crop[tri_crop == 0] = tri_warp[tri_crop == 0]