import cv2
import numpy as np
from PIL import Image
from PIL import ImageDraw
obliq_img = cv2.imread('1.png') #倾斜影像,新的纹理
rows,cols,_ = obliq_img.shape
# img_texture = cv2.imread('2.png') #原始纹理
img_texture = Image.open('3.png')
print('img_texture.size:', img_texture.size)
width, height= img_texture.size
img_texture = np.array(img_texture)
# 平面方式变换: 仿射变换, 对图像进行变换(三点得到一个变换矩阵)
obliq_triangle = [[345,95],[306,257],[390,263]]
obliq_points = np.float32(obliq_triangle)
texture_triangle = [[255,224],[203,423],[310,428]]
texture_points = np.float32(texture_triangle)
matrix = cv2.getAffineTransform(obliq_points,texture_points) #变换矩阵
img_affine = cv2.warpAffine(obliq_img, matrix, (width, height)) #仿射变换后的图像,提供新的纹理
cv2.imwrite( './affine3.png', img_affine)
#需要替换的原始纹理 mask
# fill_mask = np.array([texture_triangle])
# texture_mask = np.zeros([rows, cols], dtype = np.uint8)
# cv2.fillPoly(texture_mask, fill_mask, 255)
# cv2.imwrite( './mask.png', mask)
new_t
抠图-仿射变换-贴图 简易代码段
最新推荐文章于 2022-08-15 19:29:43 发布
本文介绍如何通过OpenCV和Pillow库实现图像仿射变换,将倾斜的纹理应用到原始图片上。作者展示了如何计算变换矩阵并使用它来调整纹理,以匹配图像的特定区域。最后,通过mask操作实现纹理的精确替换。

最低0.47元/天 解锁文章
6131

被折叠的 条评论
为什么被折叠?



