压缩感知重构算法之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

压缩感知重构算法之IHT算法python实现的更多相关文章

  1. 压缩感知重构算法之IRLS算法python实现

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

  2. 压缩感知重构算法之OLS算法python实现

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

  3. 压缩感知重构算法之CoSaMP算法python实现

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

  4. 压缩感知重构算法之SP算法python实现

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

  5. 压缩感知重构算法之OMP算法python实现

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

  6. 浅谈压缩感知(三十一):压缩感知重构算法之定点连续法FPC

    主要内容: FPC的算法流程 FPC的MATLAB实现 一维信号的实验与结果 基于凸优化的重构算法 基于凸优化的压缩感知重构算法. 约束的凸优化问题: 去约束的凸优化问题: 在压缩感知中,J函数和H函 ...

  7. 浅谈压缩感知(三十):压缩感知重构算法之L1最小二乘

    主要内容: l1_ls的算法流程 l1_ls的MATLAB实现 一维信号的实验与结果 前言 前面所介绍的算法都是在匹配追踪算法MP基础上延伸的贪心算法,从本节开始,介绍基于凸优化的压缩感知重构算法. ...

  8. 浅谈压缩感知(二十八):压缩感知重构算法之广义正交匹配追踪(gOMP)

    主要内容: gOMP的算法流程 gOMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.gOMP的算法流程 广义正交匹配追踪(Generalized OMP, g ...

  9. 浅谈压缩感知(二十六):压缩感知重构算法之分段弱正交匹配追踪(SWOMP)

    主要内容: SWOMP的算法流程 SWOMP的MATLAB实现 一维信号的实验与结果 门限参数a.测量数M与重构成功概率关系的实验与结果 SWOMP与StOMP性能比较 一.SWOMP的算法流程 分段 ...

随机推荐

  1. ValueError: zero-size array to reduction operation maximum which has no identity

    数据打印到第530行之后出现以下异常,求解!

  2. Git如何fork别人的仓库并作为贡献者提交代码

    例如 要fork一份google的MLperf/inference代码,下面介绍具体做法:预备知识git里的参考有几种表示,分别是上游仓库,远程仓库和本地仓库,逻辑关系如下拉取代码的顺序:别的大牛的代 ...

  3. 在VMware CentOS7挂载系统光盘搭建本地仓库

    1.软件准备: 安装VMware环境,在这里我使用的是VMware15 一个虚拟机系统,在这里我使用的是CentOS7(版本不同可能会有一点出入,但是应该相差不大) 在这里还有一个前提是已经建立好了y ...

  4. [UWP]使用Win2D的BorderEffect实现图片的平铺功能

    1. WPF有,而UWP没有的图片平铺功能 在WPF中只要将ImageSource的TileMode属性设置为Tile即可实现图片的平铺,具体可见WPF的这些文档: ImageBrush 类 (Sys ...

  5. nyoj 40-公约数和公倍数(gcd)

    40-公约数和公倍数 内存限制:64MB 时间限制:1000ms Special Judge: No accepted:30 submit:47 题目描述: 小明被一个问题给难住了,现在需要你帮帮忙. ...

  6. set map symbol

    set 声明 let set = new Set();即创建了一个空的set 赋值 let set = new Set(['张三','李四','王五']); 特性 似于数组,但它的一大特性就是所有元素 ...

  7. python:调用bash

    利用os模块 python调用Shell脚本,有三种方法: os.system(cmd)返回值是脚本的退出状态码 os.popen(cmd)返回值是脚本执行过程中的输出内容 commands.gets ...

  8. shell命令管道未读完阻塞了子进程,与等待其结束的父进程死"锁"。

    在exec执行一个子进程,我们希望使用管道取得子进程在重定向后的标准输出上的结果,同时等待子进程的结束.那么是等待子进程结束后才取管道数据,还是边取数据边等待子进程结束呢? 这里有一个调试的例子.u0 ...

  9. 剑指Offer-23.二叉搜索树的后序遍历序列(C++/Java)

    题目: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 分析: 二叉树的后序遍历也就是先访问左子树,再访问右 ...

  10. .NET开发者的机遇与WebAssembly发展史(有彩蛋)

    一.唠唠WebAssembly的发展历程 目前有很多支持WebAssembly的项目,但发展最快的是Blazor,这是一个构建单页面的.NET技术,目前已经从Preview版本升级到了beta版本,微 ...