当前位置:网站首页>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
边栏推荐
- About XML file (VI) - the difference between JSON and XML file
- 无法访问此网站无法找到DNS地址DNS_PROBE_STARTED怎么办?
- Built in keyboard continuous 444
- [PHP] tp6 multi table connection query
- [MCU simulation] (XVII) control transfer instructions - call and return instructions
- 當你在Linux系統中編譯安裝MySQL數據庫卡住了怎麼辦?
- Detailed explanation of dynamic compression and static compression of gzip
- LETV has more than 400 employees? Living a fairy life without a boss, the official responded
- [redis] what is progressive rehash
- Understand JVM garbage collection in one article
猜你喜欢

All dates in Oracle query time period
4. Some thoughts on asynctool framework

2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)

What happens when you get stuck compiling and installing MySQL database in Linux system?

GraphQL初识

Tools and methods - Excel plug-in xltools

GFS distributed file system

Mysql优化之索引

Has DDD surpassed MVC

【MySQL】数据查询操作(select语句)
随机推荐
[MCU simulation] (XX) org - set start address
要开源节流
[MCU simulation] (IX) instruction system - add, ADDC, sub, Inc, Dec, Da of arithmetic operation instructions
这是数学的问题
【单片机仿真】(二十一)DB(Define Byte)— 定义字节
Mysql优化之索引
ncnn 部分算子不支持的替换操作
C语言基础Day4-数组
[MCU simulation] (XVIII) control transfer instructions - empty operation instructions
多锻炼身体有好处
CorelDRAW 安装不了解决方法
数据源对象管理(第三方对象资源) & 加载properties文件
[single chip microcomputer simulation] (XII) instruction system logic operation instruction - logic XOR instruction XRL, accumulator clear 0 and reverse instruction
Fiddler grabbing
[redis] what is progressive rehash
工具及方法 - Excel插件XLTools
ncnn param文件及bin模型可视化解析
[MCU simulation] (V) addressing mode - immediate addressing and register indirect addressing
yolov6 学习初篇
多项式插值拟合(三)