% 本程序实现图像LENA的压缩传感 % 程序作者:沙威,香港大学电气电子工程学系,wsha@eee.hku.hk % 算法采用正交匹配法,参考文献 Joel A. Tropp and Anna C. Gilbert % Signal Recovery From Random Measurements Via Orthogonal Matching % Pursuit,IEEE TRANSACTIONS ON INFORMATION THEORY, VOL. 53, NO. 12, % DECEMBER 2007. % 该程序没有经过任何优化
import random import numpy as np import time import sys import cv2 import matplotlib.pyplot as plt import scipy.optimize as spopt import scipy.fftpack as spfft import scipy.ndimage as spimg from scipy import linalg from DWT import DWT
# OMP algorithm defOMP(y,A,K): col = A.shape[1] residual = y ind = [] for i inrange(K): prod = np.fabs(np.dot(A.T,residual)) pos = np.argmax(prod) ind.append(pos) a = np.dot(np.linalg.pinv(A[:,ind]),y) residual = y-np.dot(A[:,ind],a)
Res = np.zeros((col,)) Res[ind] = a # print(Res.shape) return Res
functiony = shift(x, step) [R,C,L] = size(x); fori=1:L y(:,i*step:i*step+C-1,i) = x(:,:,i); end end
2. 设定正则化参数和迭代次数
1 2 3 4 5 6
%% %Regularization parameter tau = 0.05; % L1正则化参数,越大解越稀疏
%Number of TwIST main loop iterations maxiterations = 50; % 最大迭代次数
3. 构建CASSI系统的传递模型及其转置
1 2 3 4
%% %Define the functional forms for the Forward and Forward^T models A = @(x) Rfuntwist(x,coded_aperture_shift); AT = @(x) RTfuntwist(x,coded_aperture_shift);
其中两个函数分别为正向传递的模型和它的转置:
1 2 3 4 5 6 7 8 9 10 11 12 13
functiony = Rfuntwist(f,C) [n1,n2,m] = size(C); f = reshape(f,[n1,n2,m]); % reshape成数据立方体格式 gp = f.*C; % 对每个光谱段图像编码 y = sum(gp,3); % 沿光谱维度叠加投影到CCD上 end
functionf = RTfuntwist(y,C) [n1,n2,m] = size(C); y=reshape(y,n1,n2); yp = repmat(y,[1,1,m]); % 沿光谱维度复制m份 f = yp.*C; % 编码 end
4. 定义去噪函数 (放弃软阈值,采用全变分去噪)
1 2 3 4 5 6
%%Preparing to run TwIST %Number of Chambolle iterations to perform for total variation minimization piter = 4; % 全变分去噪迭代次数
%x_twist is the estimate of the data cube. Prepare it to be displayed as a colored cube. x_twist = x_twist_orig; x_twist = x_twist.*(x_twist>=0); % 去掉负值 x_twist = x_twist/max(x_twist(:)); x_twist = shift_back(x_twist,1); save(['rec_result_1.mat'],'x_twist');
其中,shift_back函数如下:
1 2 3 4 5 6
functiony = shift_back(x, step) [R,C,L] = size(x); fori=1:L y(:,:,i) = x(:,i*step:i*step+R-1,i); end end