当前位置:网站首页>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
边栏推荐
- Analysis skills of time complexity and space complexity
- 多项式插值拟合(一)
- 【单片机仿真】(二十)ORG — 设置起始地址
- 【模板记录】字符串哈希判断回文串
- MySQL数据库中的事务和存储引擎
- [single chip microcomputer simulation] (XI) instruction system logic operation instruction - logic and instruction anl, logic or instruction ORL
- yolov5 ncnn 推理
- Code demonstration of fcos face detection model in openvino
- 你能用到的Mysql常用命令
- 多项式插值拟合(二)
猜你喜欢
JPA初识(ORM思想、JPA的基本操作)
【NoSQL】NoSQL之redis配置与优化(简单操作)
【单片机仿真】(二十)ORG — 设置起始地址
[redis] what is progressive rehash
Graphql first acquaintance
From the perspective of MySQL architecture, how does an SQL statement execute?
Snapshot: data snapshot (data disclosure method)
Tools and methods - Excel plug-in xltools
10. System safety and Application
RTX3090安装pytorch3D
随机推荐
Built in keyboard continuous 444
Snapshot: data snapshot (data disclosure method)
2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )
Analysis skills of time complexity and space complexity
樂視還有400多比特員工?過著沒有老板的神仙日子 官方出來回應了...
工具及方法 - Excel插件XLTools
【单片机仿真】(十八)控制转移类指令 — 空操作指令
【单片机仿真】(十四)指令系统位操作类指令 — 位数据传送指令MOV、位变量修改指令
Code demonstration of fcos face detection model in openvino
深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之ADASYN采样法]
数据源对象管理(第三方对象资源) & 加载properties文件
SysTick定时器的基础学习以及手撕代码
[MCU simulation] (VII) addressing mode - bit addressing
ncnn 线程
MySQL master-slave replication + read write separation
[MCU simulation] (VI) addressing mode - index addressing and relative addressing
GraphQL初识
Fiddler grabbing
First knowledge of JPA (ORM idea, basic operation of JPA)
【单片机仿真】(二)keil 安装教程