当前位置:网站首页>Affine transformation implementation
Affine transformation implementation
2022-07-19 03:08:00 【HySmiley】
To deal with the affine transformation task, it is necessary to obtain three coordinate points of the target area of two images ((x11,y11),(x21,y21),(x31,y31) And (x12,y12),(x22,y22),(x32,y32)), Three points define a plane , By solving 6 Equations obtained 6 Parameters .
Equation form :
x1=x2*a+y2*b+k1;
y1=x2*c+y2*d+k2;
adopt opencv Self contained cv2.getAffineTransform() Function to obtain the parameter matrix M, And function cv2.warpAffine() Obtain the image after affine transformation .
test

ptsa.jpg

ptsb.jpg

res.jpg
The implementation will ptsa Replace the two triangle colors with ptsb The color of the , keep ptsa It's the same shape . Without getting ptsb In the case of color , Use affine transformation to transform ptsb The shape in changes directly to ptsa, Then change the transformed ptsb The coordinates in and ptsa Correspond and replace .
There are ways to deal with it 2 Kind of :
1、 Reduce the circumscribed rectangle of the triangle area to the same size , After affine transformation, the target area of the image can be directly replaced with the original image .
2、 Do not shrink , The parameter matrix is obtained by direct affine transformation M(a,b,k1,c,d,k2), All coordinates of the target area need to be M Mapping transformation ( The above equation ) To the original .
Method 1 implementation code :
import cv2
import numpy as np
imga=cv2.imread('img/pica.jpg')
imgb=cv2.imread('img/picb.jpg')
ptsa=np.float32([[100,200],[250,100],[300,250]])#ptsa Three point coordinates of the left triangle area of
ptsb=np.float32([[50,120],[150,50],[120,130]])#ptsb Three point coordinates of the left triangle area of
ptsa_=np.float32([[250,100],[300,250],[400,150]])#ptsa Three point coordinates of the right triangle area
ptsb_=np.float32([[150,50],[120,130],[220,80]])#ptsb Three point coordinates of the right triangle area
# np.float32([[0,70*rh],[100*rw,0],[70*rw,80*rh]])
def IsTrangleOrArea(x1, y1, x2, y2, x3, y3):
return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0)
def IsInside(x1, y1, x2, y2, x3, y3, x, y):
# triangle ABC The area of
ABC = IsTrangleOrArea(x1, y1, x2, y2, x3, y3)
# triangle PBC The area of
PBC = IsTrangleOrArea(x, y, x2, y2, x3, y3)
# triangle ABC The area of
PAC = IsTrangleOrArea(x1, y1, x, y, x3, y3)
# triangle ABC The area of
PAB = IsTrangleOrArea(x1, y1, x2, y2, x, y)
return (ABC == PBC + PAC + PAB)
def warpTriangle(ptsa,ptsb,imga,imgb):
r1=cv2.boundingRect(ptsa)
r2=cv2.boundingRect(ptsb)
roia=imga[r1[1]:r1[1]+r1[3],r1[0]:r1[0]+r1[2],:]
roib=imgb[r2[1]:r2[1]+r2[3],r2[0]:r2[0]+r2[2],:]
rate_h=roia.shape[0]/roib.shape[0]
rate_w=roia.shape[1]/roib.shape[1]
roib=cv2.resize(roib,(0,0),fx=rate_w,fy=rate_h)
nptsa=[]
nptsb=[]
for i in range(3):
nptsa.append([ptsa[i,0]-r1[0],ptsa[i,1]-r1[1]])
nptsb.append([(ptsb[i, 0] - r2[0])*rate_w, (ptsb[i, 1] - r2[1])*rate_h])
# print(ptsa)
# print(ptsb)
# print(nptsa)
# print(nptsb)
nptsa=np.array(nptsa,dtype=np.float32)
nptsb=np.array(nptsb,dtype=np.float32)
M=cv2.getAffineTransform(nptsb,nptsa)
dst=cv2.warpAffine(roib,M,(roib.shape[1],roib.shape[0]))
res=imga.copy()
# print(ptsa[0,0], ptsa[0,1], ptsa[1,0], ptsa[1,1],ptsa[2,0], ptsa[2,1])
print(r1[0],r1[0]+r1[2],r1[1], r1[1] + r1[3])
for i in range(r1[1],r1[1]+r1[3]):
for j in range(r1[0], r1[0] + r1[2]):
if IsInside(ptsa[0,0], ptsa[0,1], ptsa[1,0], ptsa[1,1],ptsa[2,0], ptsa[2,1],j,i):
res[i,j,:]=dst[i-r1[1],j-r1[0],:]
return res
# cv2.imshow('roia',roia)
# cv2.imshow('dst',dst)
res=warpTriangle(ptsa,ptsb,imga,imgb)
res=warpTriangle(ptsa_,ptsb_,res,imgb)
cv2.imshow('res',res)
cv2.imshow('imgb',imgb)
cv2.imshow('imga', imga)
cv2.imwrite('res.jpg',res)
cv2.waitKey()
application :
Because the pixels in the rectangular area are traversed in the process of image processing , Affine transformation requires 3 Point coordinates ( That is, a triangle ), The transformed image Some areas It cannot be directly replaced with the original drawing , The effective area is just inside the triangle , So we need to use the area method to replace the effective area . If the situation is complex , You need to split the target area into multiple triangular areas ( Get the coordinates of three points ) Perform affine transformation respectively . At the same time, attention should be paid to boundary treatment .
Affine transformation principle reference :
What is affine transformation (Affine Transformation) - Alexander - Blog Garden
边栏推荐
- 多锻炼身体有好处
- 04_ Service registration Eureka
- [MCU simulation] (IV) addressing mode register addressing and direct addressing
- 【单片机仿真】(八)指令系统 — 数据传送指令
- D. Permutation Restoration(贪心/双指针/set)
- ncnn DataReader&Extractor&blob
- BiSeNetV2-面部分割
- 快照:数据快照(数据兜底方式)
- [MCU simulation] (XVIII) control transfer instructions - empty operation instructions
- About XML file (VI) - the difference between JSON and XML file
猜你喜欢

yolov5 ncnn 推理

zsh: command not found: mysql

SysTick定时器的基础学习以及手撕代码

【剑指Offer】31-35题(判断一个序列是否是栈的出栈序列之一,层序打印二叉树以及分行打印、每行逆着打印),判断序列是否是二叉搜索树的后序遍历路径,二叉树找一条权值为K的路径,复制复杂链表

【NoSQL】NoSQL之redis配置与优化(简单操作)

【MySQL】MHA高可用

【单片机仿真】(二十)ORG — 设置起始地址

RESNET learning notes

Mysql多表查询

2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
随机推荐
[MCU simulation] (IX) instruction system - add, ADDC, sub, Inc, Dec, Da of arithmetic operation instructions
zsh: command not found: mysql
LETV has more than 400 employees? Living a fairy life without a boss, the official responded
BiSeNetV2-面部分割 ncnn推理
多锻炼身体有好处
PyTorch最佳实践和代码模板
Oracle gets the last and first data (gets the first and last data by time)
Letv a plus de 400 employés? Le jour de l'immortel sans patron, les autorités ont répondu...
JDBC connection to MySQL database
Understand JVM garbage collection in one article
[MCU simulation] (II) keil installation tutorial
BiSeNetV2-面部分割
MySQL replication table
4. Some thoughts on asynctool framework
05_服务调用Ribbon
2. Actual use of asynctool framework
MySQL面试题(2022)
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code
内置键盘连续444
All dates in Oracle query time period