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

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

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

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

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

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

Orthogonal Least Squares (OLS)算法流程


实验

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

  • python (本文用的python版本为3.5.1)
  • numpy python包(本文用的版本为1.10.4)
  • scipy python包(本文用的版本为0.17.0)
  • pillow python包(本文用的版本为3.1.1)

python代码

#coding: utf-8
'''
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# DCT基作为稀疏基,重建算法为OMP算法 ,图像按列进行处理
# email:ncuzhangben@qq.com,
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'''
#导入集成库
import math # 导入所需的第三方库文件
import numpy as np #对应numpy包
from PIL import Image #对应pillow包 #读取图像,并变成numpy类型的 array
im = np.array(Image.open('lena.bmp'))
#print (im.shape, im.dtype)uint8 #生成高斯随机测量矩阵
sampleRate=0.7 #采样率
Phi=np.random.randn(256*sampleRate,256) #生成稀疏基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) #OLS算法函数(未完成待修改)
def cs_ols(y,D):
L=math.floor(3*(y.shape[0])/4)
residual=y #初始化残差
index=np.zeros((L),dtype=int)
for i in range(L):
index[i]= -1
result=np.zeros((256))
for j in range(L): #迭代次数
pos_temp=np.argsort(np.fabs(np.dot(np.linalg.pinv(D[:,pos]),y)))#对应步骤2 product=np.fabs(np.dot(D.T,residual))
pos=np.argmax(product) #最大投影系数对应的位置
index[j]=pos
my=np.linalg.pinv(D[:,index>=0])
a=np.dot(my,y)
residual=y-np.dot(D[:,index>=0],a)
result[index>=0]=a
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_ols(img_cs_1d[:,i],Theta_1d) #利用OMP算法计算稀疏系数
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_OLS()
%------------ 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(0.5*height),width); % only keep one third of the original data
Phi = Phi./repmat(sqrt(sum(Phi.^2,1)),[floor(0.5*height),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); % height*width的0矩阵
Theta_1d=Phi*mat_dct_1d;%测量矩阵乘上基矩阵
for i=1:width
column_rec=OLS(img_cs_1d(:,i),Theta_1d);%算法的目的是得到稀疏系数
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),imshow(uint8(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')); %************************************************************************%
function [s, residual] = OLS(y, A, err) % Orthogonal Least Squares [1] for Sparse Signal Reconstruction % Input
% A = N X d dimensional measurment matrix
% y = N dimensional observation vector
% m = sparsity of the underlying signal % Output
% s = estimated sparse signal
% r = residual % [1] T. Blumensath, M. E. Davies; "On the Difference Between Orthogonal
% Matching Pursuit and Orthogonal Least Squares", manuscript 2007 if nargin < 3
err = 1e-5;
end n1=length(y);
m=floor(3*n1/4); s = zeros(size(A,2),1);
r(:,1) = y; L = []; Psi = [];
normA=(sum(A.^2,1)).^0.5;
NI = 1:size(A,2); for i = 2:m+1
DR = A'*r(:,i-1);
[v, I] = max(abs(DR(NI))./normA(NI)');
INI = NI(I);
L = [L' INI']';
NI(I)=[];
Psi = A(:,L);
x = Psi\y;
yApprox = Psi*x;
r(:,i) = y - yApprox;
if norm(r(:,end)) < err
break;
end
end
s(L) = x;
residual = r(:,end);

参考文章

1、Blumensath T, Davies M E. On the difference between orthogonal matching pursuit and orthogonal least squares[J]. 2007.

2、Hashemi A, Vikalo H. Sparse Linear Regression via Generalized Orthogonal Least-Squares[J]. arXiv preprint arXiv:1602.06916, 2016.

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

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

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

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

  2. 压缩感知重构算法之CoSaMP算法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. 浅谈压缩感知(三十一):压缩感知重构算法之定点连续法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. mount 和 /etc/fstab关系。

    mount  -a 自动按照格式执行/etc/fstab里面的文件. /etc/fstab 文件格式: device        mount-point      type      options ...

  2. 项目——基于httpd镜像演示Dockerfile所有的指令

    基于httpd镜像演示Dockerfile所有的指令: 第一步:创建Dockerfile工作目录 [root@localhost harbor]# mkdir /test [root@localhos ...

  3. 深入理解计算机系统 第十章 系统级I/O

    很多高级语言都提供了执行 I/O 的较高级别的函数.为什么我们还要学习 Unix I/O? 原因:1.由于 I/O 和其他系统概念之间有循环依赖关系,故了解 Unix I/O 将帮助我们理解其他的系统 ...

  4. vue-create 报错 command failed: yarn --registry=https://registry.npm.taobao.org --disturl=https://npm.taobao.org/dist 完美解决方案

    @vue/cli 3.x 创建项目失败解决方案 报错信息 command failed: yarn --registry=https://registry.npm.taobao.org --distu ...

  5. pat 1084 Broken Keyboard(20 分)

    1084 Broken Keyboard(20 分) On a broken keyboard, some of the keys are worn out. So when you type som ...

  6. nyoj 366-D的小L (next_permutation())

    366-D的小L 内存限制:64MB 时间限制:4000ms 特判: No 通过数:5 提交数:7 难度:2 题目描述:       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡 ...

  7. Zabbix-(六) JMX监控

    Zabbix-(六) JMX监控 一.前言 Zabbix提供了JMX监控,它通过JMX API获取JVM信息,从而提供监控数据.本文讲述使用JMX监控Tomcat的JVM信息. 准备 Zabbix S ...

  8. Apache服务安装及一些基本操作

    注意:安装apache服务之前记得搭建yum仓库 1.安装apache服务,输入命令“yum install httpd” 安装成功后,会这样显示 2.需要对Apache服务进行启动,输入命令“sys ...

  9. ES6的基础知识(一)

    1.ECMAScript 6.0(以下简称ES6). 2.ECMAScript 和 JavaScript 的关系是,前者是后者的规格,后者是前者的其中一种实现. 3.对ES6支持的浏览器:超过 90% ...

  10. ubuntu 16.04上源码编译dlib教程 | compile dlib on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/c6ead512/,欢迎阅读! compile dlib on ubuntu 16.04 Series Part 1: compil ...