【发布时间】:2020-03-31 11:02:54
【问题描述】:
给定一个正方形或矩形的地砖纹理,如何用warpAffine进行任意角度的旋转并保持地砖彼此相邻? (视觉上)
原版地砖
预期结果
4x4 拼接图块 + cv2.BORDER_WRAP,图块接缝彼此相邻。纹理逆时针旋转了 45 度,但当 n 为奇数时,它仅适用于 n*pi/2 角度
代码
texture = cv2.imread(ACTIVE_TEXTURE)
def concat_tile(im_list_2d):
return cv2.vconcat([cv2.hconcat(im_list_h) for im_list_h in im_list_2d])
def rotate_texture(texture, angle=0, scale=1.0):
print(angle)
height, width = texture.shape[:2]
midpoint = (width / 2, height / 2)
rotation_mat = cv2.getRotationMatrix2D(tuple(midpoint), angle, scale)
abs_cos = abs(rotation_mat[0, 0])
abs_sin = abs(rotation_mat[0, 1])
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin)
rotation_mat[0, 2] += bound_w / 2 - midpoint[0]
rotation_mat[1, 2] += bound_h / 2 - midpoint[1]
texture_rotated = cv2.warpAffine(texture, rotation_mat, (bound_w, bound_h), borderMode=cv2.BORDER_WRAP,
flags=cv2.INTER_AREA)
texture_rotated = cv2.resize(texture_rotated, (height, width), interpolation=cv2.INTER_AREA)
return texture_rotated
rot_angle = 45
texture = rotate_texture(texture, angle=rot_angle)
tiles = [[texture for _ in range(4)] for _ in range(4)]
texture = concat_tile(tiles)
cv2.namedWindow('image', cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('image', 600, 600)
cv2.imshow('image', texture)
cv2.waitKey()
cv2.destroyAllWindows()
【问题讨论】:
标签: python algorithm opencv image-processing computational-geometry