线性变换与特征向量

研究一下线性变换和特征向量的关系

from PIL import Image,ImageDraw
import numpy as np
from numpy.linalg import eig


img = Image.open("pic.png")
draw = ImageDraw.Draw(img)#建立绘图对象
width,height = img.size#获取原始图像大小

A=np.array([[5,6],
            [7,2]])


step=34
p_range=500

margin=width/2
for i in range(-p_range,p_range,step):
    for j in range(-p_range,p_range,step):
        vector=np.array([i,j])
        #print(vector)
        #print(vector[0]+margin,vector[1]+margin)
        conversion=np.matmul(A,vector)
        draw.line(((vector[0]+margin,vector[1]+margin),(int(conversion[0])+margin,int(conversion[1])+margin)),fill=(0,0,0))#画一条直线,(0,0)到(width-1,height-1),fill指线的颜色
        draw.line(((vector[0]+margin,vector[1]+margin),(vector[0]+margin,vector[1]+margin)),fill=(255,0,0))#画一条直线,(0,0)到(width-1,height-1),fill指线的颜色

# 画特征向量

vals, vecs=eig(A) # 特征分解,每一列是特征向量

a1=vecs[0,0]
a2=vecs[1,0]

b1=vecs[0,1]
b2=vecs[1,1]

draw.line(((margin,margin),(a1*500+margin,a2*500+margin)),fill=(0,0,255))#画一条直线,(0,0)到(width-1,height-1),fill指线的颜色
draw.line(((margin,margin),(b1*500+margin,b2*500+margin)),fill=(0,0,255))#画一条直线,(0,0)到(width-1,height-1),fill指线的颜色
draw.line(((margin,margin),(margin,margin)),fill=(255,0,255))#画一条直线,(0,0)到(width-1,height-1),fill指线的颜色

img.save("cross_line.png")#保存新图像


print("特征值:",vals)
print("特征向量:",vecs)