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

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

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

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

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

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

算法流程

算法分析

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基作为稀疏基,重建算法为CoSaMP算法,图像按列进行处理
# 参考文献: D. Deedell andJ. Tropp, “COSAMP: Iterative Signal Recovery from
#Incomplete and Inaccurate Samples,” 2008.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #导入集成库
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.5 #采样率
Phi=np.random.randn(256*sampleRate,256)
# 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) #CoSaMP算法函数
def cs_CoSaMP(y,D):
S=math.floor(y.shape[0]/4) #稀疏度
residual=y #初始化残差
pos_last=np.array([],dtype=np.int64)
result=np.zeros((256)) for j in range(S): #迭代次数
product=np.fabs(np.dot(D.T,residual))
pos_temp=np.argsort(product)
pos_temp=pos_temp[::-1]#反向,得到前面L个大的位置
pos_temp=pos_temp[0:2*S]#对应步骤3
pos=np.union1d(pos_temp,pos_last) result_temp=np.zeros((256))
result_temp[pos]=np.dot(np.linalg.pinv(D[:,pos]),y) pos_temp=np.argsort(np.fabs(result_temp))
pos_temp=pos_temp[::-1]#反向,得到前面L个大的位置
result[pos_temp[:S]]=result_temp[pos_temp[:S]]
pos_last=pos_temp
residual=y-np.dot(D,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_CoSaMP(img_cs_1d[:,i],Theta_1d) #利用CoSaMP算法计算稀疏系数
sparse_rec_1d[:,i]=column_rec;
img_rec=np.dot(mat_dct_1d,sparse_rec_1d) #稀疏系数乘上基矩阵 #显示重建后的图片
image2=Image.fromarray(img_rec)
image2.show()

matlab代码

function Demo_CS_CoSaMP()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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: D. Deedell andJ. Tropp, “COSAMP: Iterative Signal Recovery from
% Incomplete and Inaccurate Samples,” 2008.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %------------ read in the image --------------
img=imread('lena.bmp'); % testing image
img=double(img);
[height,width]=size(img); %------------ form the measurement matrix and base matrix ---------------
Phi=randn(floor(height/2),width); % only keep one third of the original data
Phi = Phi./repmat(sqrt(sum(Phi.^2,1)),[floor(height/2),1]); % normalize each column 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_cosamp(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_cosamp(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 n=length(y); % length of measurements
s=floor(n/4); % sparsity
r_n=y; % initial residuals sig_pos_lt=[]; % significant pos for last time iteration for times=1:s % number of iterations product=abs(T_Mat'*r_n);
[val,pos]=sort(product,'descend');
sig_pos_cr=pos(1:2*s); % significant pos for curretn iteration sig_pos=union(sig_pos_cr,sig_pos_lt); Aug_t=T_Mat(:,sig_pos); % current selected entries of T_Mat aug_x_cr=zeros(m,1);
aug_x_cr(sig_pos)=(Aug_t'*Aug_t)^(-1)*Aug_t'*y; % temp recovered x (sparse) [val,pos]=sort(abs(aug_x_cr),'descend'); hat_x=zeros(1,m);
hat_x(pos(1:s))=aug_x_cr(pos(1:s));% recovered x with s sparsity sig_pos_lt=pos(1:s); % refresh the significant positions r_n=y-T_Mat*hat_x';
end

参考文献

1、D. Deedell andJ. Tropp, “COSAMP: Iterative Signal Recovery from Incomplete and Inaccurate Samples,” 2008.

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

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

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

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

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

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

  3. 压缩感知重构算法之IHT算法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. 压缩感知重构算法之压缩采样匹配追踪(CoSaMP)

    压缩采样匹配追踪(CompressiveSampling MP)是D. Needell继ROMP之后提出的又一个具有较大影响力的重构算法.CoSaMP也是对OMP的一种改进,每次迭代选择多个原子,除了 ...

  7. 浅谈压缩感知(二十三):压缩感知重构算法之压缩采样匹配追踪(CoSaMP)

    主要内容: CoSaMP的算法流程 CoSaMP的MATLAB实现 一维信号的实验与结果 测量数M与重构成功概率关系的实验与结果 一.CoSaMP的算法流程 压缩采样匹配追踪(CompressiveS ...

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

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

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

    主要内容: StOMP的算法流程 StOMP的MATLAB实现 一维信号的实验与结果 门限参数Ts.测量数M与重构成功概率关系的实验与结果 一.StOMP的算法流程 分段正交匹配追踪(Stagewis ...

随机推荐

  1. python经典面试算法题4.1:如何找出数组中唯一的重复元素

    本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. [百度面试题] 难度系数:⭐⭐⭐ 考察频率:⭐⭐⭐⭐ 题目描述 ...

  2. Flutter 构建的 Mac 桌面应用上无法发出网络?

    在上一篇文章中我们分享了,如何开发桌面应用.在本章文章中,来解决一下为何在 Mac 中无法发出网络情况的原因. 起因 事情​起因是这样的:我总觉得写一个 Demo 不足以体现我们开发同学的能力.直到最 ...

  3. git Lab ssh方式拉取代码失败

    gitLab在linux上已经安装好了, 在配置项目的时候报如下异常 使用http方式没问题, 但是用ssh方式设置repository URL 提示资源库不存在. returned status c ...

  4. nyoj 168-房间安排 (贪心)

    168-房间安排 内存限制:64MB 时间限制:3000ms 特判: No 通过数:33 提交数:71 难度:2 题目描述: 2010年上海世界博览会(Expo2010),是第41届世界博览会.于20 ...

  5. 目录(cd mkdir rmdir rm pwd ls) 文件(ln touch mv rm cat more head rail) 文件权限(chmod chown chgrp) 文件通配符(* ? [])

    记住Linux目录树的结构是一个称职Linux系统管理员的必备素质! 目录漫游cd   cd - 目录显示pwd 目录管理 mkdir -p a/b/c/1 parent创建多层目录 -m 700   ...

  6. 通过django 速成 blog

    1.            创建项目 33进入在python目录下的scripts文件后执行 django-admin.py   startproject  mysite 这样就生成了名为mysite ...

  7. dbstructsync 多套mysql环境表、字段、索引的差异sql产出(原创)

    最近写了一个工具(比较两套测试环境数据库表.表字段.索引的差异) 功能:可以比较两套环境中mysql指定库中表.表字段及索引的差异,返回具体需要同步的执行sql A环境的数据库db 作为sourced ...

  8. windows下大数据开发环境搭建(1)——Hadoop环境搭建

    所需环境 jdk 8 Hadoop下载 http://hadoop.apache.org/releases.html 配置环境变量 HADOOP_HOME: C:\hadoop-2.7.7 Path: ...

  9. python2中的SSL:CERTIFICATE_VERIFY_FAILED错误的解决办法

    在使用urllib2访问一个自签名的https链接时,对于python2.6以下版本,TLS握手期间是不会检查服务器X509的证书签名是否是CA的可信任根证书.不过python2.7以后改变了这种情况 ...

  10. Java基础语法(二)

    目录 一.强类型语言 二.数据类型分类 1.基本数据类型 整数类型 字符类型 浮点类型 布尔类型 2.引用数据类型 三.基本类型转换 自动类型转换 强制类型转换 四.表达式类型的自动提升 承接上篇,谈 ...