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(:)) < 2
Img = Img*255;
end
FlagColor = (size(Img,3) == 3);
% set mask
SetMask = 2;
if SetMask == 1
% read mask
Mask = imread('Mask\peppers_mask512.bmp');
Mask = Mask > 5;
MFlagColor = (size(Mask,3) == 3);
if MFlagColor ~= FlagColor && FlagColor == 1
Mask = repmat(Mask,[1,1,3]);
elseif MFlagColor ~= FlagColor && FlagColor == 0
Mask = Mask(:,:,1);
end
elseif SetMask == 2
% Interactively set mask
if not(exist('grab_mode'))
grab_mode = 'line';
end
options.grab_mode = grab_mode;
if not(exist('grab_radius'))
grab_radius = 4;
end
switch grab_mode
case '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 %switch
Iin = find(U(:,:,1) == Inf);
Iout = find(U(:,:,1) ~= Inf);
m1 = length(Iin); % 缺损点的总数
% product the mask
Mask = zeros(size(Img));
if FlagColor == 1
tmpMask = zeros([size(Img,1),size(Img,2)]);
for channel = 1:3
tmpMask(Iin) = 1 ;
Mask(:,:,channel) = tmpMask;
end
else
Mask(Iin) = 1; % 缺损区域为1
end % 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 = ', num2s