import cv2 as cv
import numpy as np
img=cv.imread(r'T/s.jpg',cv.IMREAD_GRAYSCALE)
img=cv.resize(img,(512,512))
tdata=img.flatten()
tdata=tdata.reshape(512*512,1)
tdata=tdata-tdata.mean()
tdata=tdata/np.sqrt(tdata.var())
mean=np.array([])
mean,eigenvectors=cv.PCACompute(tdata,mean,cv.PCA_DATA_AS_COL,
maxComponents=9)
projectedVectors=cv.PCAProject(tdata,mean,eigenvectors)
result=cv.PCABackProject(tdata,mean,eigenvectors)
result=result.reshape(512,512)
cv.imshow('show',img)
cv.waitKey()
cv.imshow('show',result)
cv.waitKey()
cv.destroyAllWindows()
↧