顺便说一下,您可以一个一个地映射像素,mapquadtoquad(target rect,orig quad),
但是如果没有像素插值,它看起来会很糟糕;opencv可以做到这一切。
<预先> <代码>!/usr/bin/env蟒蛇
“方形-方形地图
从http://threeblindmiceandamonkey.com/?P=16矩阵H
“”
来自未来进口部
将numpy导入为np
_日期:2010-06-11 Jun Denis
定义DET2(A、B、C、D):
返回A*D-B*C
def mapsquaretoquad(四元):[4][2]
sq=np.零((3,3))
px=四元[0,0]-四元[1,0]+四元[2,0]-四元[3,0]
py=四元[0,1]-四元[1,1]+四元[2,1]-四元[3,1]
如果abs(px)<1e-10和abs(py)<1e-10:
sq[0,0]=四元[1,0]-四元[0,0]
sq[1,0]=四元[2,0]-四元[1,0]
sq[2,0]=四元[0,0]
sq[0,1]=四元[1,1]-四元[0,1]
sq[1,1]=四元[2,1]-四元[1,1]
sq[2,1]=四元[0,1]
Sq[0,2]=0。
Sq[1,2]=0。
Sq[2,2]=1。
返回平方
其他:
dx1=四元[1,0]-四元[2,0]
dx2=四元[3,0]-四元[2,0]
Dy1=四元[1,1]-四元[2,1]
DY2=四元[3,1]-四元[2,1]
Det=Det2(dx1,dx2,dy1,dy2)
如果DET=0:
无返回
sq[0,2]=det2(px,dx2,py,dy2)/det
sq[1,2]=det2(dx1,px,dy1,py)/det
Sq[2,2]=1。
sq[0,0]=四元[1,0]-四元[0,0]+sq[0,2]*四元[1,0]
sq[1,0]=四元[3,0]-四元[0,0]+sq[1,2]*四元[3,0]
sq[2,0]=四元[0,0]
sq[0,1]=四元[1,1]-四元[0,1]+sq[0,2]*四元[1,1]
sq[1,1]=四元[3,1]-四元[0,1]+sq[1,2]*四元[3,1]
sq[2,1]=四元[0,1]
返回平方
#………………………………………………………………………
定义映射四边形(四边形):
返回np.linalg.inv(mapsquaretoquad(quad))。
def mapquadtoquad(A、B):
返回np.dot(mapquartosquare(a),mapsquaretoquad(b))
def perstrans(x,t)表示:
透视变换x nx2,t 3x3:
[X0 Y0 1]t=[A0 B0 W0]->[A0/W0 B0/W0]
[x1 y1]t=[a1 b1 w1]->[a1/w1 b1/w1]
…
“”
x1=np.vstack((x.t,np.ones(len(x)))
Y=np.dot(t.t,x1)
返回(y[:-1]/y[-1]).t
#………………………………………………………………………
如果“名称”,
np.设置“打印选项”(2,阈值=100,抑制=真)。2f
sq=np.数组([[0,0]、[1,0]、[1,1]、[0,1])
Quad=np.数组([[171,72],[331,93],[333,188],[177,210])
打印“四格:”,四格
打印“方形到方形:”,Perstrans(sq,mapsqaretoquad(quad))。
打印“Quad to Square:”,Perstrans(Quad,MapQuadtoSquare(Quad))。
dw,dh=300,250
rect=np.数组([[0,0],[dw,0],[dw,dh],[0,dh]])
QuadQuad=MapQuadToQuad(Quad,Rect)
打印“四边形到四边形转换:”,四边形
打印“Quad to Rect:”,PerTrans(Quad,QuadQuad)
“”
四轮:【171 72】
〔331 93〕
〔333 188〕
〔177 210〕
四方形:【171.72。
〔331〕。93。
〔333〕。188。
〔177〕。210。]
四方形:[-0.0。
〔1〕。0。
〔1〕。1。
〔0〕。1。]
四到四转换:【1.29-0.23-0】。]
[-0.06 1.79-0.]
[-217.24-88.54 1.34]]
四元到矩形:[[0.0。
〔300〕。0。
〔300〕。250。
〔0〕。250。]
“”
< /代码>
g.
threeblindmiceandamonkey
.
二维齐次坐标上的3x3变换可以变换任意4个点(四元)
任何其他四合院;
相反,任何FromQuad和ToQuad,例如卡车的角落和目标矩形,
进行3 x 3变换。
QT有
quadToQuad
可以用它转换像素地图,但我猜你没有qt?
新增10Jun:
从
labs.trolltech.com/page/Graphics/Examples
有一个很好的演示,当你移动角落的时候,哪个四到四个像素地图:
6月11日补充道:@will,这是python中的translate.h(你知道吗?
“”..“”是多行注释。)
perstrans()
是关键;如果不问的话,希望这是有意义的。
顺便说一下,你
能够
逐个映射像素,mapquadtoquad(target rect,orig quad),
但是如果没有像素插值,它看起来会很糟糕;OpenCV可以做到这一切。
#!/usr/bin/env python
""" square <-> quad maps
from http://threeblindmiceandamonkey.com/?p=16 matrix.h
"""
from __future__ import division
import numpy as np
__date__ = "2010-06-11 jun denis"
def det2(a, b, c, d):
return a*d - b*c
def mapSquareToQuad( quad ): # [4][2]
SQ = np.zeros((3,3))
px = quad[0,0] - quad[1,0] + quad[2,0] - quad[3,0]
py = quad[0,1] - quad[1,1] + quad[2,1] - quad[3,1]
if abs(px) < 1e-10 and abs(py) < 1e-10:
SQ[0,0] = quad[1,0] - quad[0,0]
SQ[1,0] = quad[2,0] - quad[1,0]
SQ[2,0] = quad[0,0]
SQ[0,1] = quad[1,1] - quad[0,1]
SQ[1,1] = quad[2,1] - quad[1,1]
SQ[2,1] = quad[0,1]
SQ[0,2] = 0.
SQ[1,2] = 0.
SQ[2,2] = 1.
return SQ
else:
dx1 = quad[1,0] - quad[2,0]
dx2 = quad[3,0] - quad[2,0]
dy1 = quad[1,1] - quad[2,1]
dy2 = quad[3,1] - quad[2,1]
det = det2(dx1,dx2, dy1,dy2)
if det == 0.:
return None
SQ[0,2] = det2(px,dx2, py,dy2) / det
SQ[1,2] = det2(dx1,px, dy1,py) / det
SQ[2,2] = 1.
SQ[0,0] = quad[1,0] - quad[0,0] + SQ[0,2]*quad[1,0]
SQ[1,0] = quad[3,0] - quad[0,0] + SQ[1,2]*quad[3,0]
SQ[2,0] = quad[0,0]
SQ[0,1] = quad[1,1] - quad[0,1] + SQ[0,2]*quad[1,1]
SQ[1,1] = quad[3,1] - quad[0,1] + SQ[1,2]*quad[3,1]
SQ[2,1] = quad[0,1]
return SQ
#...............................................................................
def mapQuadToSquare( quad ):
return np.linalg.inv( mapSquareToQuad( quad ))
def mapQuadToQuad( a, b ):
return np.dot( mapQuadToSquare(a), mapSquareToQuad(b) )
def perstrans( X, t ):
""" perspective transform X Nx2, t 3x3:
[x0 y0 1] t = [a0 b0 w0] -> [a0/w0 b0/w0]
[x1 y1 1] t = [a1 b1 w1] -> [a1/w1 b1/w1]
...
"""
x1 = np.vstack(( X.T, np.ones(len(X)) ))
y = np.dot( t.T, x1 )
return (y[:-1] / y[-1]) .T
#...............................................................................
if __name__ == "__main__":
np.set_printoptions( 2, threshold=100, suppress=True ) # .2f
sq = np.array([[0,0], [1,0], [1,1], [0,1]])
quad = np.array([[171, 72], [331, 93], [333, 188], [177, 210]])
print "quad:", quad
print "square to quad:", perstrans( sq, mapSquareToQuad(quad) )
print "quad to square:", perstrans( quad, mapQuadToSquare(quad) )
dw, dh = 300, 250
rect = np.array([[0, 0], [dw, 0], [dw, dh], [0, dh]])
quadquad = mapQuadToQuad( quad, rect )
print "quad to quad transform:", quadquad
print "quad to rect:", perstrans( quad, quadquad )
"""
quad: [[171 72]
[331 93]
[333 188]
[177 210]]
square to quad: [[ 171. 72.]
[ 331. 93.]
[ 333. 188.]
[ 177. 210.]]
quad to square: [[-0. 0.]
[ 1. 0.]
[ 1. 1.]
[ 0. 1.]]
quad to quad transform: [[ 1.29 -0.23 -0. ]
[ -0.06 1.79 -0. ]
[-217.24 -88.54 1.34]]
quad to rect: [[ 0. 0.]
[ 300. 0.]
[ 300. 250.]
[ 0. 250.]]
"""