程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Linear algebra (based on Python) image change exchange based on matrix transformation

編輯:Python
  1. problem : Chicken and rabbit in the same cage ,10 Head ,28 One foot , Ask how many chickens and rabbits there are
  2. answer : Set up a bivariate linear equation , chicken x only , The rabbit has y only

x+y=10
2x+4y=28

  1. System of linear equations
 Insert a code chip here
import numpy as np
A=np.array([[1,1],[2,4]])# Put all the vectors of the coefficients together 
b=np.array([10,28])# Constant vector 
x=np.linalg.solve(A,b)
print(' The solution of linear equations is :',x)

D:\why\python.exe D:/opencv-python/xianxingdaishu.py
The solution of linear equations is : [6. 4.]

4. problem : Later, roosters and ducks were introduced , Later total 14 head ,40 foot , eyes 28, Ask how many chickens, rabbits and ducks each have
Hypothetical duck z only
Equations :
x+y+z=14
2x+4y+2z=40
2x+2y+2z=28( useless )
5、 Insert : Generating vectors , Vector addition , Number multiplication

import numpy as np
x=np.array([1,2,3])
y=np.array([4,5,6])
print("x={},y={}".format(x,y))
print('x The dimensions are {}'.format(x.shape))#shape Display the dimension of the vector , If it is a vector, there is only one dimension by default , The dimension is displayed as (dim,)
print('x+y={]'.format(x+y))
k=3
print('kx={}'.format(k*x))
print('3x+2y={}'.format(3*x+2*y))

D:\why\python.exe D:/opencv-python/xianxingdaishu.py
x=[1 2 3],y=[4 5 6]
x The dimensions are (3,)
x+y=[5 7 9]
kx=[3 6 9]
3x+2y=[11 16 21]

Judge that an equation has a unique solution :

  • n There are three unknowns n An equation
  • Determinant is not equal to 0

6. Insert : determinant

import numpy as np
A=np.array([[1,1,1],[2,4,2],[2,2,2]])
A_det=np.linalg.det(A)# Calculate determinant 
print('A The determinant value of is ',A_det)
B=np.array([[1,1,1,1],[1,2,0,0],[1,0,3,0],[1,0,0,4]])
B_det=np.linalg.det(B)
print('B The determinant value of is ',B_det)

D:\why\python.exe D:/opencv-python/jj.py
A The determinant value of is 0.0
B The determinant value of is -2.0

7、 Kramer's law
Conclusion :2 The order determinant is made up of 2 It's made up of dimensional vectors , The result is the area of a parallelogram whose two vectors are adjacent sides

import numpu as np
D=np.array([[2.,1,-5,1],[1,-3,0,-6],[0,2,-1,2],[1,4,-7,6]])
D_det=np.linalg.det(D)
D1=np.array([[8.,1,-5,1],[9,-3,0,-6],[-5,2,-1,2],[0,4,-7,6]])
D1_det=np.linalg.det(D1)
D2=np.array([[2.,8,-5,1],[1,9,0,-6],[0,-5,-1,2],[1,0,-7,6]])
D2_det=np.linalg.det(D2)
D3=np.array([[2.,1,8,1],[1,3,9,-6],[0,2,-15,2],[1,4,0,6]])
D1_det=np.linalg.det(D3)
D4=np.array([[2.,1,-5,8],[1,-3,0,9],[0,2,-1,-5],[1,4,-7,0]])
D4_det=np.linalg.det(D4)
x1=D1_det/D_det
x2=D2_det/D_det
x3=D3_det/D_det
x4=D4_det/D_det
print(' The solution of Clem's law to linear differential equations is \n x1={:.2f},\n x2={:.2f},\n x3={:.2f},\n x4={:.2f}'.format(x1,x2,x3,x4))

8、 matrix

import numpy as np
A=np.array([[1,2],[1,-1]])
B=np.array([[1,2,-3],[-1,1,2]])
print('A scale {}'.format(A.shape))
print('B scale {}'.format(B.shape))
print('AB=\n{}'.format(np.matmul(A,B)))

8.1 Unit matrix

import numpy as np
print('B=\n',B,'\n','E=\n',np.eye(3))#3 Order unit matrix 
np.matmul(B,np.eye(3))

8.2 Elementary matrix

import numpy as np
A=np.array([[1,1,1],[2,4,2]])
print('A=\n',A)

8.3 Two lines of the commutative matrix

import numpy as np
A=np.array([[0,1],[1,0]])
np.matmul(P,A)

8.4` Inverse matrix

import numpy as np
A=np.array([[1,0,0],[0,2,0],[0,0,3]])
np.matmul(A,A)
import numpy as np
B=np.array([[0,1],[0,-1]])
print(np.linalg.det(B),' The determinant is 0, Singular matrix ')# Check whether it is strange 
print(np.linalg.pinv(B))
print(np.matul(np,matmul(B,np,linalg.pinv(B)),B))# Verify the definition of generalized inverse 

Be careful : The position of a vector in space is absolute , But its coordinate value is relative , The value of coordinates depends on the selected coordinate vector ( basal )

8.5 Diagonal matrix
Not all matrices can be similar to diagonal matrices

8.6 The eigenvalue , Eigenvector , Diagonalization

import numpy as np
A=np.array([[-2,1,1],[0,2,0],[-4,1,3]])
lamb,p=np.linalg.eig(A)
print(lamb)# The eigenvalue 
print(p)# Eigenvector 
print(np.matmul(np.linalg.inv(p),np.matmul(A,p)))

8.7 Numerical filtering

import numpy as np
res=np.matmul(np.linalg.inv(p),np.matmul(A,p))
res[np.abs(res)<1e-6]=0
print(res)

8.8 Schmidt orthogonalization

import numpy as np
from scipy.linalg import*
A=np.array([[1,2,3],[2,1,3],[3,2,1]])
B=orth(A)# Orthogonalization , SVD is not Schmidt orthogonalization 
print(np.matmul(B,np.transpose(B))# Output identity matrix 
res=np.matmul(B,np.transpose(B)
res[np.abs(res)<1e-6]=0
print(res)

9. Project practice – Image transformation based on matrix transformation

import numpy as np
from math import cos,sin,pi
def vec_2d(x0,y0,alpha):
#alpha The angle of rotation , Radian system 
origin=np.array([[x0,y0,1]])
Transnp.array([[cos(alpha),-sin(alpha),0],[sin(alpha),cos(alpha),0],[0,0,1]])
res=origin.dot(Trans)
x=#___
y=#————
return (x,y)
# Image rotation 
def Trans(x0,y0,W,H,alpha):
origin=np.array([x0,y0,1])
res = origin.dot(np.array[[cos(alpha),0],
[-sin(alpha),cos(alpha),0],
[-0.5*W*cos(alpha)+0.5*H*sin(alpha)+0.5*W,
-0.5*W*sin(alpha)-0.5*H*cos(alpha)+
0.5*H,1]])
return (int(res[0,:2][0]),int(res[0,:2][1]))
from skimage import io,data
imgs=data.horse()
io.imshow(img3)
img3.shape
img4=np.zeros((400,400))
for x in range(img3.shape[0]):
for y in range(img3.shape[1]):
x1,y1=Trans(x,y,328,400,pi/2)
img4[x1-355,y1]=img3[x,y]#355 Just do one step of translation and centering , Ensure the integrity of the picture 
io.imshow(img4)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved