抠图-仿射变换-贴图 简易代码段

本文介绍如何通过OpenCV和Pillow库实现图像仿射变换,将倾斜的纹理应用到原始图片上。作者展示了如何计算变换矩阵并使用它来调整纹理,以匹配图像的特定区域。最后,通过mask操作实现纹理的精确替换。
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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值