压缩感知重构算法之IHT算法python实现

压缩感知重构算法之OMP算法python实现
压缩感知重构算法之CoSaMP算法python实现
压缩感知重构算法之SP算法python实现
压缩感知重构算法之IHT算法python实现
压缩感知重构算法之OLS算法python实现
压缩感知重构算法之IRLS算法python实现

IHT(iterative hard thresholding )算法是压缩感知中一种非常重要的贪婪算法,它具有算法简单的有点,且易于实现,在实际中应用较多。本文给出了IHT算法的python和matlab代码(本文给出的代码未经过优化,所以重建质量不是非常好),以及完整的仿真过程。

算法流程

这里写图片描述

python代码

要利用python实现,电脑必须安装以下程序

  • python (本文用的python版本为3.5.1)
  • numpy python包(本文用的版本为1.10.4)
  • scipy python包(本文用的版本为0.17.0)
  • pillow python包(本文用的版本为3.1.1)
#coding:utf-8
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# DCT基作为稀疏基,重建算法为IHT算法,图像按列进行处理
#  参考文献: Carrillo R E, Polania L F, Barner K E. Iterative hard thresholding for compressed sensing 
#with partially known support[C]
#//Acoustics, Speech and Signal Processing (ICASSP), 
#2011 IEEE International Conference on. IEEE, 2011: 4028-4031.
# 
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#导入集成库
import math

# 导入所需的第三方库文件
import  numpy as np    #对应numpy包
from PIL import Image  #对应pillow包


#读取图像,并变成numpy类型的 array
im = np.array(Image.open('lena.bmp'))#图片大小256*256

#生成高斯随机测量矩阵
sampleRate=0.7  #采样率
Phi=np.random.randn(256,256)
u, s, vh = np.linalg.svd(Phi)
Phi = u[:256*sampleRate,] #将测量矩阵正交化


#生成稀疏基DCT矩阵
mat_dct_1d=np.zeros((256,256))
v=range(256)
for k in range(0,256):  
    dct_1d=np.cos(np.dot(v,k*math.pi/256))
    if k>0:
        dct_1d=dct_1d-np.mean(dct_1d)
    mat_dct_1d[:,k]=dct_1d/np.linalg.norm(dct_1d)

#随机测量
img_cs_1d=np.dot(Phi,im)

#IHT算法函数
def cs_IHT(y,D):    
    K=math.floor(y.shape[0]/3)  #稀疏度    
    result_temp=np.zeros((256))  #初始化重建信号   
    u=0.5  #影响因子
    result=result_temp
    for j in range(K):  #迭代次数
        x_increase=np.dot(D.T,(y-np.dot(D,result_temp)))    #x=D*(y-D*y0)
        result=result_temp+np.dot(x_increase,u) #   x(t+1)=x(t)+D*(y-D*y0)
        temp=np.fabs(result)
        pos=temp.argsort() 
        pos=pos[::-1]#反向,得到前面L个大的位置
        result[pos[K:]]=0
        result_temp=result       
    return  result



#重建
sparse_rec_1d=np.zeros((256,256))   # 初始化稀疏系数矩阵    
Theta_1d=np.dot(Phi,mat_dct_1d)   #测量矩阵乘上基矩阵
for i in range(256):
    print('正在重建第',i,'列。。。')
    column_rec=cs_IHT(img_cs_1d[:,i],Theta_1d)  #利用IHT算法计算稀疏系数
    sparse_rec_1d[:,i]=column_rec;        
img_rec=np.dot(mat_dct_1d,sparse_rec_1d)          #稀疏系数乘上基矩阵

#显示重建后的图片
image2=Image.fromarray(img_rec)
image2.show()

matlab代码

%代码在matlab2010b测试通过
function Demo_CS_IHT()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the DCT basis is selected as the sparse representation dictionary
% instead of seting the whole image as a vector, I process the image in the
% fashion of column-by-column, so as to reduce the complexity.

% Author: Chengfu Huo, roy@mail.ustc.edu.cn, http://home.ustc.edu.cn/~roy
% Reference: T. Blumensath and M. Davies, “Iterative Hard Thresholding for
% Compressed Sensing,” 2008.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%------------ read in the image --------------
img=imread('lena.bmp');     % 256*256大小
img=double(img);
[height,width]=size(img);
sampleRate=0.7; %采样率

%------------ form the measurement matrix and base matrix ---------------
%Phi=randn(floor(height/3),width);  % only keep one third of the original data  
%Phi = Phi./repmat(sqrt(sum(Phi.^2,1)),[floor(height/3),1]); % normalize each column

Phi = orth(rand(256, 256));
Phi=Phi(1:256*sampleRate, :);


mat_dct_1d=zeros(256,256);  % building the DCT basis (corresponding to each column)
for k=0:1:255 
    dct_1d=cos([0:1:255]'*k*pi/256);
    if k>0
        dct_1d=dct_1d-mean(dct_1d); 
    end;
    mat_dct_1d(:,k+1)=dct_1d/norm(dct_1d);
end


%--------- projection ---------
img_cs_1d=Phi*img;          % treat each column as a independent signal


%-------- recover using omp ------------
sparse_rec_1d=zeros(height,width);            
Theta_1d=Phi*mat_dct_1d;
for i=1:width
    column_rec=cs_iht(img_cs_1d(:,i),Theta_1d,height);
    sparse_rec_1d(:,i)=column_rec';           % sparse representation
end
img_rec_1d=mat_dct_1d*sparse_rec_1d;          % inverse transform


%------------ show the results --------------------
figure(1)
subplot(2,2,1),imagesc(img),title('original image')
subplot(2,2,2),imagesc(Phi),title('measurement mat')
subplot(2,2,3),imagesc(mat_dct_1d),title('1d dct mat')
psnr = 20*log10(255/sqrt(mean((img(:)-img_rec_1d(:)).^2)));
subplot(2,2,4),imshow(uint8(img_rec_1d));
title(strcat('PSNR=',num2str(psnr),'dB'));

disp('over')


%************************************************************************%
function hat_x=cs_iht(y,T_Mat,m)
% y=T_Mat*x, T_Mat is n-by-m
% y - measurements
% T_Mat - combination of random matrix and sparse representation basis
% m - size of the original signal
% the sparsity is length(y)/4

hat_x_tp=zeros(m,1);         % initialization with the size of original 
s=floor(length(y)/4);        % sparsity
u=0.5;                       % impact factor

% T_Mat=T_Mat/sqrt(sum(sum(T_Mat.^2))); % normalizae the whole matrix

for times=1:s

    x_increase=T_Mat'*(y-T_Mat*hat_x_tp);

    hat_x=hat_x_tp+u*x_increase;

    [val,pos]=sort((hat_x),'descend');  % why? worse performance with abs()

    hat_x(pos(s+1:end))=0;   % thresholding, keeping the larges s elements

    hat_x_tp=hat_x;          % update

end


参考文章

1、Carrillo R E, Polania L F, Barner K E. Iterative hard thresholding for compressed sensing with partially known support[C]//Acoustics, Speech and Signal Processing (ICASSP), 2011 IEEE International Conference on. IEEE, 2011: 4028-4031.

欢迎python爱好者加入:学习交流群 667279387

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
压缩感知是一种信号处理技术,用于从少量的测量数据中恢复原始信号。在压缩感知中,有多种重构算法可以用于恢复信号。其中一些常见的压缩感知重构算法Python实现包括OMP算法、CoSaMP算法、SP算法IHT算法、OLS算法和IRLS算法。\[1\] 其中,OMP算法是一种基于贪婪策略的压缩感知重构算法,它通过选择最相关的原子来逐步重构信号。CoSaMP算法是一种迭代重构算法,它通过交替进行稀疏表示和信号重构来逐步提高重构精度。SP算法是一种贪婪算法,它通过选择最相关的原子来逐步重构信号。IHT算法是一种迭代硬阈值算法,它通过迭代过程中的硬阈值操作来逐步恢复信号。OLS算法是一种最小二乘算法,它通过最小化重构误差来恢复信号。IRLS算法是一种迭代加权最小二乘算法,它通过迭代过程中的加权最小二乘操作来逐步恢复信号。\[1\] 其中,SP算法是一种非常重要的贪婪算法,它具有较快的计算速度和较好的重构概率,在实际中得到了广泛的应用。\[3\] SP算法的流程包括选择初始稀疏向量、计算残差、选择最相关的原子、更新稀疏向量和重复迭代直到满足停止准则。\[3\] 因此,如果你想在Python实现压缩感知算法,你可以选择其中的任意一种算法进行实现,如OMP算法、CoSaMP算法、SP算法IHT算法、OLS算法或IRLS算法。这些算法的具体实现可以参考相关的文献和代码资源。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [压缩感知重构算法之SP算法python实现](https://blog.csdn.net/hjxzb/article/details/50929590)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [压缩感知重构算法之IRLS算法python实现](https://blog.csdn.net/hjxzb/article/details/51077309)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值