1. 参考文献

2. Hybrid Sparse Representations模型

% demo_Hybrid_Sparse_Representation.m
% Author: HSW
% Date: 2015/3/28
% HARBIN INSTITUTE OF TECHNOLOGY
%
% set matlab
close all;
clear all;
clc;
% ADD PATH
addpath(genpath('inpainting'));
options.null = 0;
% read image
Img = imread('Image\butterfly.bmp');
% Img = imread('Image\peppers.bmp');Img = rgb2gray(Img);
Img = im2double(Img);
if max(Img(:)) < 2Img = Img*255;
endFlagColor = (size(Img,3) == 3);% set mask
SetMask = 2;
if SetMask == 1% read maskMask = imread('Mask\peppers_mask512.bmp');Mask = Mask > 5;MFlagColor = (size(Mask,3) == 3);if MFlagColor ~= FlagColor && FlagColor == 1Mask = repmat(Mask,[1,1,3]);elseif MFlagColor ~= FlagColor && FlagColor == 0Mask = Mask(:,:,1);end
elseif SetMask == 2% Interactively set maskif not(exist('grab_mode'))grab_mode = 'line';endoptions.grab_mode = grab_mode;if not(exist('grab_radius'))grab_radius = 4;endswitch grab_modecase 'points'options.r = grab_radius;U = grab_inpainting_mask(Img,options);case 'line'options.r = grab_radius;[U,options.point_list] = grab_inpainting_mask(Img,options);end %switchIin = find(U(:,:,1) == Inf);Iout = find(U(:,:,1) ~= Inf);m1 = length(Iin); % 缺损点的总数% product the maskMask = zeros(size(Img));if FlagColor == 1tmpMask = zeros([size(Img,1),size(Img,2)]);for channel = 1:3tmpMask(Iin) = 1 ;Mask(:,:,channel) = tmpMask;endelseMask(Iin) = 1; % 缺损区域为1end % if FlagColor
end% if SetMask
nImg = (1-Mask).*Img;
PSNRin = 10*log10(255^2/mean((Img(:)-nImg(:)).^2));
% parameter of Hybrid Sparse Representation Model
Levels = 3;
% Main Hybrid Sparse Representation Model
I = zeros(size(Img));
for channel = 1:size(Img,3)ChannelImg = Img(:,:,channel);ChannelMask = Mask(:,:,channel);TempX = Hybrid_Sparse_Representation(ChannelImg,ChannelMask,Levels);I(:,:,channel) = TempX{Levels};
end %for channel
I = max(0,min(I,255));
PSNRout = 10*log10(255^2/mean((I(:) - Img(:)).^2));
figure;
subplot(1,3,1);
imshow(Img/255,[]);
title('Original Image');
subplot(1,3,2);
imshow(nImg/255,[]);
title(['Masked Image PSNR = ',num2str(PSNRin), ' dB']);
subplot(1,3,3);
imshow(I/255,[]);
title(['Inpainting Image PSNR = ', num2str(PSNRout), ' dB']);

2.2 混合稀疏表示模型

function [xx]=Hybrid_Sparse_Representation(x,mask,levels)N=50;
para=[4 15 10 1;8 21 10 2;16 31 10 4];for level=1:levelsfprintf('level=%d\n',level);step=(2^(levels-level));x_buf=imresize(x,size(x)/step);mask_buf=mask(1:step:end,1:step:end);if level==1;xx_buf=iter_recover(x_buf,x_buf.*~mask_buf,mask_buf,4,para(level,:));xx_init=xx_buf{end};elsefor iter=1:2xx_init=combine_recovery(x_buf,x_init,mask_buf,level,N,para(level,:));x_init=xx_init;endendxx{level}=xx_init;x_init=imresize(xx_init,2);
end
function [xx,snr,dif]=iter_recover(x,x_init,mask,iter,para)N=50;
snr=[];dif=[];[xx{1},snr1,dif1]=combine_recovery(x,x_init,mask,1,N,para);
snr=[snr snr1];dif=[dif dif1];
for i=1:iter[xx{i+1},snr1,dif1]=combine_recovery(x,xx{i},mask,i+1,N,para);  snr=[snr snr1];dif=[dif dif1];
end
% ergodic image inpainting
% 遍历图像修复
% x - M-by-N original image, mask - inpainting mask
function [xx,snr_save,dif,xx_buf]=combine_recovery(x,xx,mask,iter_outer,iter_max,para)% generate damaged image y
% mask=1 - lost block
y=x.*(1-mask);
[M,N]=size(y);
%T=15;W=31;pmax=10;step=4;
T=para(1);W=para(2);pmax=para(3);step=para(4);
if mod(T,2)==0T2=T/2;
else T2=(T-1)/2;end
mask2=zeros(size(mask));
if step>1sx=step/2;sy=step/2;
elsesx=1;sy=1;
end
mask2(sx:step:end,sy:step:end)=mask(sx:step:end,sy:step:end);
[i,j]=find(mask2==1);
nn=bwmorph(mask,'dilate',2)-mask;
J=find(nn);
if iter_outer==1Th=sqrt(var(x(J)))*1.5;xx=y;xx(find(xx==0))=mean(x(J));
elseTh=sqrt(var(x(J)))/(iter_outer-1);
end
Win=kaiser(2*T+1,4)*kaiser(2*T+1,4)';x_save=xx;% main loop
loc=cell(1,length(i));
for k=1:length(i)if mod(k,100)==0fprintf('%d\n',k);endii=i(k);jj=j(k);if ii>T&ii<M-T&jj>T&jj<N-T% build a patch and a dictionaryp1=xx(ii-T:ii+T,jj-T:jj+T);%lm1=1-mask(ii-T:ii+T,jj-T:jj+T);cnt=1;d=[];pos=[];% collect NNs in the resampled spacefor sx=-W:Wfor sy=-W:WSx=ii+sx-T:ii+sx+T;Sy=jj+sy-T:jj+sy+T;if ii+sx-T>0&ii+sx+T<M&jj+sy-T>0&jj+sy+T<Np2=xx(Sx,Sy);%lm2=1-mask(Sx,Sy);e=(p1-p2);%dist=mean2(abs(e));dist=mean2(e.*e);%if dist<th&cnt<pmaxd(cnt)=dist;pos(:,cnt)=[sx sy];cnt=cnt+1;%endendendend% reorder NNs for maximizing sparsification[d1,J]=sort(d);loc{k}=pos(:,J);if iter_outer==1J1=find(d/var2(p1)<1);elseJ1=find(d/var2(p1)<0.5);enddiv(k)=min(pmax,length(J1));end
end
%plot(div);pause;
% transform domain thresholdingTh_step=.05;
iter_total=round(Th/Th_step);
for iter=1:min(iter_max,iter_total)Th=Th-Th_step;%for inner=1:1w=zeros(M,N);xr=w;% nonlocal modelfor k=1:length(i)%fprintf('%d\n',k);pos=loc{k};ii=i(k);jj=j(k);if size(pos,2)>0%pp=div(k);pp=pmax;Y1=zeros(2*T+1,2*T+1,pp);for l=1:ppsx=pos(1,l);sy=pos(2,l);Sx=ii+sx-T:ii+sx+T;Sy=jj+sy-T:jj+sy+T;Y1(:,:,l)=xx(Sx,Sy);end%Z1=dct2(Y1);Z1=fftn(Y1)/sqrt(prod(size(Y1)));%if mean(abs(Z1(:)))<tv_old(k)temp=abs(Z1);TV(iter,k)=mean(temp(:));Z=Z1.*(abs(Z1)>Th);%SIG=1;SIG=sum(abs(Z1(:))>Th)/prod(size(Z));Y2=real(ifftn(Z))*sqrt(prod(size(Y1)));% put all patches back to their original positionsfor l=1:ppsx=pos(1,l);sy=pos(2,l);Sx=ii+sx-T:ii+sx+T;Sy=jj+sy-T:jj+sy+T;xr(Sx,Sy)=xr(Sx,Sy)+Y2(:,:,l).*Win/SIG;w(Sx,Sy)=w(Sx,Sy)+Win/SIG;end%     else%         TV(iter,k)=tv_old(k);%         xr(Sx,Sy)=xx(Sx,Sy);w(Sx,Sy)=1;%     endendend% local model[ii,jj]=find(mask);for k=1:length(ii)Sx=max(1,ii(k)-T2):min(M,ii(k)+T2);Sy=max(1,jj(k)-T2):min(N,jj(k)+T2);Win2=kaiser(length(Sx),4)*kaiser(length(Sy),4)';Z1=dct2(xx(Sx,Sy));Z=Z1.*(abs(Z1)>Th);%SIG2=1;SIG2=sum(abs(Z1(:))>Th)/prod(size(Z));Y2=idct2(Z);xr(Sx,Sy)=xr(Sx,Sy)+Y2.*Win2/SIG2;w(Sx,Sy)=w(Sx,Sy)+Win2/SIG2;end% leave undamaged pixels aloneI=find(w>0);xr(I)=xr(I)./w(I);xx=x.*(1-mask)+xr.*mask;u=xx-x_save;x_save=xx;xx_buf(:,:,iter)=xx;J=find(mask==1);e=x-xx;mse=mean(e(J).^2);snr=10*log10(255*255/mse);dif(iter)=mean(u(J).^2);snr_save(iter)=snr;fprintf('Iteration %d, Th=%f, PSNR=%f, dif=%f\n',iter,Th,snr,dif(iter));imshow(xx,[]);pause(0.1);%end%diff=max(dif(iter-5:iter))-min(dif(iter-5:iter));%     if iter>10&max(dif(iter-5:iter))-min(dif(iter-5:iter))<0.01%         break;%     end
end

2.3 交互式掩模(Mask)相关函数

function v = getoptions(options, name, v, mendatory)% getoptions - retrieve options parameter
%
%   v = getoptions(options, 'entry', v0);
% is equivalent to the code:
%   if isfield(options, 'entry')
%       v = options.entry;
%   else
%       v = v0;
%   end
%
%   Copyright (c) 2007 Gabriel Peyreif nargin<4mendatory = 0;
endif isfield(options, name)v = eval(['options.' name ';']);
elseif mendatoryerror(['You have to provide options.' name '.']);
end 
function [U,point_list] = grab_inpainting_mask(M, options)% grab_inpainting_mask - create a mask from user input
%
%   U = grab_inpainting_mask(M, options);
%
%   Select set of point in an image (useful to select a region for
%   inpainting). The set of point is U==Inf.
%
%   options.r is the radius for selection (default r=5).
%
%   Selection stops with right click.
%
%   Set options.mode='points' to gather disconnected points.
%   Set options.mode='line' to gather connected lines.
%
%   Copyright (c) 2006 Gabriel Peyreif nargin==3 && method==1U = grab_inpainting_mask_old(M, options);return;
endoptions.null = 0;
r = getoptions(options, 'r', 5);
method = getoptions(options, 'mode', 'points');if strcmp(method, 'line')if not(isfield(options, 'point_list'))[V,point_list] = pick_polygons(rescale(sum(M,3)),r);elsepoint_list = options.point_list;V = draw_polygons(rescale(sum(M,3)),r,point_list);end        U = M; U(V==1) = Inf;return;
endm = size(M,1);
n = size(M,2);
s = size(M,3);U = sum(M,3)/3;
b = 1;
[Y,X] = meshgrid(1:n,1:m);
point_list = [];
while b==1clf;hold on;imagesc(rescale(M)); axis image; axis off; colormap gray(256);[y,x,b] = ginput(1);point_list(:,end+1) = [x;y];I = find((X-x).^2 + (Y-y).^2 <= r^2 );U(I) = Inf;for k=1:sMa = M(:,:,k);Ma(I) = 0;M(:,:,k) = Ma;end
end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sk = draw_polygons(mask,r,point_list)sk = mask*0;
for i=1:length(point_list)pl = point_list{i};for k=2:length(pl)sk = draw_line(sk,pl(1,k-1),pl(2,k-1),pl(1,k),pl(2,k),r);end
endfunction [sk,point_list] = pick_polygons(mask,r)% pick_polygons - ask for the user to build a set of curves
%
%   sk = pick_polygons(mask,r);
%
%   mask is a background image (should be in [0,1] approx).
%
%   The user right-click on a set of point which create a curve.
%   Left click stop a curve.
%   Another left click stop the process.
%
%   Copyright (c) 2007 Gabriel Peyren = size(mask,1);sk = zeros(n);
point_list = {};
b = 1;
while b(end)==1% draw a lineclf;imagesc(mask+sk); axis image; axis off;colormap gray(256);[y1,x1,b] = ginput(1);pl = [x1;y1];while b==1clf;imagesc(mask+sk); axis image; axis off;[y2,x2,c] = ginput(1);if c~=1if length(pl)>1point_list{end+1} = pl;endbreak;endpl(:,end+1) = [x2;y2];sk = draw_line(sk,x1,y1,x2,y2,r);x1 = x2; y1 = y2;end
end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sk = draw_line(sk,x1,y1,x2,y2,r)n = size(sk,1);
[Y,X] = meshgrid(1:n,1:n);
q = 80;
t = linspace(0,1,q);
x = x1*t+x2*(1-t); y = y1*t+y2*(1-t);
if r==0x = round( x ); y = round( y );sk( x+(y-1)*n ) = 1;
elsefor k=1:qI = find((X-x(k)).^2 + (Y-y(k)).^2 <= r^2 );sk(I) = 1;end
endfunction U = grab_inpainting_mask_old(M, r)% grab_inpainting_mask - create a mask from user input
%
%   U = grab_inpainting_mask(M, r);
%
%   r is the radius for selection (default r=5).
%
%   Selection stops with right click.
%
%   Copyright (c) 2006 Gabriel Peyr?if nargin<2r = 5;
endm = size(M,1);
n = size(M,2);
s = size(M,3);U = sum(M,3)/3;
b = 1;
[Y,X] = meshgrid(1:n,1:m);
while b==1clf;hold on;imagesc(rescale(M)); axis image; axis off; colormap gray(256);[y,x,b] = ginput(1);I = find((X-x).^2 + (Y-y).^2 <= r^2 );U(I) = Inf;for k=1:sMa = M(:,:,k);Ma(I) = 0;M(:,:,k) = Ma;end
end
function y = rescale(x,a,b)% rescale - rescale data in [a,b]
%
%   y = rescale(x,a,b);
%
%   Copyright (c) 2004 Gabriel Peyr?if nargin<2a = 0;
end
if nargin<3b = 1;
endm = min(x(:));
M = max(x(:));if M-m<epsy = x;
elsey = (b-a) * (x-m)/(M-m) + a;
end

3. 模型效果

图像修复序列——混合稀疏表示(Hybrid Sparse Representations)模型相关推荐

  1. 图像修复序列——BSCB模型

    1. 参考文献 2. BSCB模型代码 2.1 BSCB模型demo % demo_BSCB.m % Author: HSW % Date: 2015/3/25 % HARBIN INSTITUTE ...

  2. 图像修复序列——FFM模型

    1. 参考文献 2. FFM模型实现 2.1 FFM模型代码 %%%%%%%%%%%%%%%%%%%%%%%%%%% Demo of Fast March Method %%%%%%%%%%%%%%% ...

  3. 图像修复序列——FOE模型

    1. 参考文献 2. FOE模型 2.1 FOE 模型实现 %demo_FOE.m % Author: HSW % Date: 2015/3/25 % HARBIN INSTITUTE OF TECH ...

  4. 图像修复 图像补全_图像修复简介

    图像修复 图像补全 In practical applications, images are often corroded by noise. These noises are dust or wa ...

  5. ICCV 2019 | 基于关联语义注意力模型的图像修复

    作者丨薛洁婷 学校丨北京交通大学硕士生 研究方向丨图像翻译 论文引入 图像修复问题的关键是维持修复后图像的全局语义一致性以及破损区域的细节纹理合理性.近期关于图像修复问题的研究主要集中于通过使用空间注 ...

  6. 【计算摄影】浅析图像修复背后的核心技术

    大家好,这是专栏<计算摄影>的第十篇文章,这一个专栏来自于计算机科学与摄影艺术的交叉学科. 作者&编辑 | 言有三 今天要介绍的一个问题是计算机视觉领域中的非常经典的问题,即图像修 ...

  7. 用于自动驾驶的混合稀疏-稠密单目SLAM系统

    点击上方"计算机视觉工坊",选择"星标" 干货第一时间送达 作者丨泡椒味的口香糖 来源丨 GiantPandaCV 0. 引言 随着自动驾驶系统的推广,越来越多 ...

  8. 图像修复系列-图像修复概述

    图像修复的最初是为了去除遮挡或对破损的图像就行修复,但是,修复图像是一个ill-pose(病态)问题,所示,图像修复需要一些前提假设(也就是之前提到过的正则约束),通过在一定约束条件下,建立最优化准则 ...

  9. 运动模糊的图像修复调研

    前言 1.什么是运动模糊 在用摄像机获取景物图像时,如果在相机曝光期间景物和摄像机之间存在相对运动,例如用照相机拍摄快速运动的物体,或者从行驶中的汽车上拍摄外面静止不动的景物时,拍得的照片都可能存在模 ...

最新文章

  1. 知识点2-对二进制的运用
  2. rabbitmq 集群数据存储与单点故障
  3. idea快捷键生成返回类型_十三肝了2晚的《IDEA操作手册-终极秘籍》终于来了......
  4. 动态修改dom node的两种方法性能比较
  5. 滑动翻页效果_Flutter实现3D效果,一个字,炫!
  6. android intent enum,enum类型被intent所携带时需要注意的地方
  7. 微软SQL 报表服务的研究
  8. Vue.js新手入门指南
  9. 用python模拟《流浪地球》的木星引力弹弓效应
  10. java的null类型强转
  11. 【LeetCode】【字符串】题号:*8. 字符串转换整数 (atoi)
  12. AutoML之锦标赛选择
  13. python图像增强算法_python 图像增强算法实现详解
  14. pxe服务器 安装系统,通过PXE网络安装windows系统
  15. java 数字排列组合
  16. 省心!2021精选APP macOS装机必备清单来了
  17. RabbitMQ极速入门
  18. 若依项目中添加导入excel功能
  19. 电脑有线无法连接问题
  20. android action pick,android – 无法使用ACTION_PICK意图选择特定图...

热门文章

  1. Web全栈~25.文件
  2. C++之operator关键字作用(一百一三十)
  3. 同桌的你1080pHD国语中字_马立杰_新浪博客
  4. MySQL基础篇 | 连接查询、子查询(嵌套)
  5. Python基础知识——字典:for循环遍历字典
  6. DL实现semantic segmentation
  7. Cosmos IBC
  8. 【Gradle】Starting a Gradle Daemon, 1 incompatible and 5 stopped Daemons could not be reused
  9. 小白都能看懂 XAMPP的下载安装配置详细教程(含拒绝访问坑)
  10. 用通俗易懂的语言去解释JDK的动态代理