1. 程式人生 > >基於SVD的影象壓縮

基於SVD的影象壓縮

基於SVD的影象壓縮

原始影象:手寫數字,32* 32=1024畫素。可以使用SVD來對資料降維

from numpy import linalg as la
from numpy import *

一、分析Sigma的長度個數

通常保留矩陣80%~90%的能量,就可以得到重要的特徵並去除噪聲

def analyse_data(Sigma, loopNum=20):
    """analyse_data(分析Sigma的長度取值)
    
    Args:
        Sigma  Sigma的值
        loopNum 迴圈次數
    """
    #
總方差的集合(總能量值) Sig2 = Sigma ** 2 SigmaSum = sum(Sig2) for i in range(loopNum): SigmaI = sum(Sig2[: i + 1]) print('主成分:%s, 方差佔比:%s%%' % (format(i+1, '2.0f'), format(SigmaI/SigmaSum*100, '4.2f')))

二、將文字資料轉化為矩陣

# 影象壓縮函式
# 載入並轉換資料
def imgLoadData(filename):
    myl = []
    
# 開啟文字檔案,並從檔案以陣列的方式讀入字元 for line in open(filename).readlines(): newRow = [] for i in range(32): newRow.append(int(line[i])) myl.append(newRow) # 矩陣調入後,就可以在螢幕上輸出該矩陣 myMat = mat(myl) return myMat

三、列印矩陣

def printMat(inMat, thresh=0.8):
    # 由於矩陣保護了浮點數,因此定義淺色和深色,遍歷所有矩陣元素,當元素大於閾值列印1,否則列印0
for i in range(32): for k in range(32): if float(inMat[i, k]) > thresh: print(1, end=' ') else: print(0, end=' ') print('')

四、實現影象壓縮,允許基於任意給定的奇異值數目來重構影象

# 實現影象壓縮,允許基於任意給定的奇異值數目來重構影象
def imgCompress(numSV=3, thresh=0.8):
    """imgCompress()
    
    Args:
        numSV    Sigma長度
        thresh   判斷的閾值
    """
    # 構建一個列表
    myMat = imgLoadData('F:/迅雷下載/machinelearninginaction/Ch14/0_5.txt')
    
    print("****original matrix****")
    # 對原始影象進行SVD分解並重構影象e
    printMat(myMat, thresh)
    
    # 通過Sigma重新構成 SigRecom 來實現
    # Sigma是一個對角矩陣,因此需要建立一個全0矩陣,然後將前面的那些奇異值填充到對角線上
    U, Sigma, VT = la.svd(myMat)
    
    # 分析插入的 Sigma 長度
    analyse_data(Sigma, 20)
    
    SigRecon = mat(eye(numSV) * Sigma[: numSV])
    reconMat = U[:, :numSV] * SigRecon * VT[:numSV, :]
    print("****reconstructed matrix using %d singular values *****" % numSV)
    printMat(reconMat, thresh)
imgCompress(2)
****original matrix****
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 
0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 
0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 1 1 1 1 1 1 0 0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 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 
主成分: 1, 方差佔比:62.85%
主成分: 2, 方差佔比:84.81%
主成分: 3, 方差佔比:89.04%
主成分: 4, 方差佔比:92.23%
主成分: 5, 方差佔比:94.35%
主成分: 6, 方差佔比:95.24%
主成分: 7, 方差佔比:96.07%
主成分: 8, 方差佔比:96.85%
主成分: 9, 方差佔比:97.43%
主成分:10, 方差佔比:97.94%
主成分:11, 方差佔比:98.41%
主成分:12, 方差佔比:98.79%
主成分:13, 方差佔比:99.13%
主成分:14, 方差佔比:99.39%
主成分:15, 方差佔比:99.56%
主成分:16, 方差佔比:99.70%
主成分:17, 方差佔比:99.83%
主成分:18, 方差佔比:99.92%
主成分:19, 方差佔比:99.97%
主成分:20, 方差佔比:100.00%
****reconstructed matrix using 2 singular values *****
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 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1 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

五、總結

只需要3個奇異值就能相當精確地對影象實現重構。U和VT都是32*2的矩陣,有三個奇異值,總畫素為64+64+3=131,和原數目1024相比,1024/131=7。7倍的壓縮比