1、理论
a.优先权的计算
b.最优匹配块的搜索
c.更新置信度
Criminisi图像修复模型最重要的两步就是计算优先权值和搜索最优匹配块,利用最优匹配块进行第一次填充后,图像破损区域的边界随之发生改变,因此图片置信度项也要随着迭代更新,更新的具体方法如下:
2、matlab程序(完整代码,可直接运行)
clc;
clear;
close all;
imagepath =imread( 'hua.bmp'); % 原图像,用于计算PSNR
maskpath =imread('huamask.bmp'); % 破损图像
fillColor=[255,255,0]; % 破损图像中标定的破损区域RGB颜色值
qukuai_size = 5; % 选择填充区块大小3*3,5*5,7*7,9*9,...
disp('开始图像修复')
disp('......')
tic % 计时开始点
[Psnr,inpaintedImg] =RGB_Criminisi(imagepath, maskpath, fillColor, qukuai_size);
disp('图像修复结束')
toc % 计时结束点
Psnr
inpaintedImg=uint8(inpaintedImg);
subplot(1,2,1)
imshow(maskpath);
title('待修复图像');
subplot(1,2,2)
imshow(inpaintedImg);
title('Criminisi算法修复结果');
RGB_Criminisi.m
function [Psnr,inpaintedImg] =RGB_Criminisi(imagepath, maskpath, fillColor, qukuai_size)
img0=imagepath;
fillImg=maskpath;
img = double(fillImg);
fillRegion=img(:,:,1)==fillColor(1)&img(:,:,2)==fillColor(2)&img(:,:,3)==fillColor(3);
origImg = img;
ind = img2ind(img);
%-----------------------------------------------------
sz = [size(img,1) size(img,2)];
z1=size(img,1);
z2=size(img,2);
sourceRegion = ~fillRegion;
% 求等照度线值
[Ix(:,:,3),Iy(:,:,3)] = gradient(img(:,:,3));
[Ix(:,:,2),Iy(:,:,2)] = gradient(img(:,:,2));
[Ix(:,:,1),Iy(:,:,1)] = gradient(img(:,:,1));
Ix = sum(Ix,3)/(3*255); Iy = sum(Iy,3)/(3*255);
temp = Ix; Ix = -Iy; Iy = temp; % 旋转90度
%------------------------------------------------------------------------
%求得梯度值
[ix(:,:,3),iy(:,:,3)] = gradient(img(:,:,3));
[ix(:,:,2),iy(:,:,2)] = gradient(img(:,:,2));
[ix(:,:,1),iy(:,:,1)] = gradient(img(:,:,1));
ix = sum(ix,3)/(3*255); iy = sum(iy,3)/(3*255);
%------------------------------------------------------------------------
% 初始化置信度项C和数据项D值
C = double(sourceRegion);
D = repmat(-.1,sz);
% 修复(直到所有的破损区域都被修复完成)
while any(fillRegion(:))
% 寻找边缘
dR = find(conv2(double(fillRegion),[1,1,1;1,-8,1;1,1,1],'same')>0);
[Nx,Ny] = gradient(double(~fillRegion));
N = [Nx(dR(:)) Ny(dR(:))];
N(~isfinite(N))=0;
% 计算置信度项值
for k=dR'
Hp = qukuai_9(sz, k, qukuai_size);
q = Hp(~(fillRegion(Hp)));
C(k) = sum(C(q))/numel(Hp);
end
% 计算优先权
D(dR) = abs(Ix(dR).*N(:,1)+Iy(dR).*N(:,2)) /255;
priorities =C(dR).*D(dR);
% 找到优先权最大的块 Hp
[~,ndx] = max(priorities(:));
p = dR(ndx(1));
%---------------------
[Hp,rows,cols] = qukuai_9(sz,p,qukuai_size); %9x9块大小
toFill=fillRegion(Hp);
Wpatch=img(rows,cols,:); %得到待修复块
%------------------------------------------------------------------------
%采用全局搜索,寻找最佳匹配块
Hq=whole_match(z1,z2,img,Wpatch,fillColor);
%------------------------------------------------------------------------
% 更新填充区域
fillRegion(Hp(toFill)) = false;
% 更新C(p)值和等照度线值
C(Hp(toFill)) = C(p);
Ix(Hp(toFill)) = Ix(Hq(toFill));
Iy(Hp(toFill)) = Iy(Hq(toFill));
%-----------------------------------------------------------------------
%更新梯度值
ix(Hp(toFill)) = ix(Hq(toFill));
iy(Hp(toFill)) = iy(Hq(toFill));
%----------------------------------------------------------------------
% 从Hq复制图像信息到Hp
ind(Hp(toFill)) = ind(Hq(toFill));
img(rows,cols,:) = ind2img(ind(rows,cols),origImg);
end
inpaintedImg=img;
A=double(img0);
B=double(inpaintedImg);
Psnr=PSNR(A,B);
qukuai_9.m
function [Hp,rows,cols] = qukuai_9(sz, p, qukuai_size)
x = floor(rem(p,sz(1)));
y = floor(p/sz(1))+1;%得到待修复区域中心点所在的位置
w = floor(qukuai_size/2);%块的半径(9*9大小的块,块的半径为4)
rows = max(1,x-w):min(x+w,sz(1));
cols = max(1,y-w):min(y+w,sz(2));
numhang = length(rows);
numlie = length(cols);
HJ = zeros(numhang,numlie);
LJ = zeros(numhang,numlie);
for ii = 1:numlie
HJ(:,ii) = rows;%行铺成的矩阵
end
for jj = 1:numhang
LJ(jj,:) = cols;%列铺成的矩阵
end
Hp = (LJ-1)*sz(1)+HJ;%得到待修复块(在图像中的具体位置)
whole_match.m
%**************************************************************************
%全局搜索,寻找最佳匹配块(彩色图像)
%全局搜索过程中不会出现找不到最佳匹配块的情况
%**************************************************************************
function Block=whole_match(zx,zy,img,Wpatch,fillColor)
%zx,zy为图像的大小
aa=size(Wpatch,1);
bb=size(Wpatch,2);%得到的待修复块的实际大小
sx=zx-aa+1;
sy=zy-bb+1;%确定搜索范围的最大值
all=aa*bb;%一维的大小
min=1.0000e+10;%赋初值
%计算找到的匹配块与待修复块在已知像素点处的距离(只是已知像素点)
for i=1:sx
for j=1:sy
Mpatch=img(i:i+aa-1,j:j+bb-1,:);%得到图像中和待修复块同样大小的块(Match patch)
posunme=Mpatch(:,:,1)==fillColor(1)&Mpatch(:,:,2)==fillColor(2)&Mpatch(:,:,3)==fillColor(3);
if any(posunme(:))%说明有破损点,不作为待修复块的最佳匹配块
continue;
end
Err=0;
for gg=1:aa*bb
%遍历块中的每一个像素点
pf=Wpatch(gg)==fillColor(1)&Wpatch(gg+all)==fillColor(2)&Wpatch(gg+2*all)==fillColor(3);
if pf %说明该位置像素有破损,不参与距离的计算
continue;
end
cha=Wpatch(gg)-Mpatch(gg);Err=Err+cha*cha;
cha=Wpatch(gg+all)-Mpatch(gg+all);Err=Err+cha*cha;
cha=Wpatch(gg+2*all)-Mpatch(gg+2*all);Err=Err+cha*cha;
end
if Err<min
min=Err;%取使得误差最小的块,并记住它们在图像中的起始位置
hk=i;
lk=j;
end
end
end
rows=hk:hk+aa-1;
cols=lk:lk+bb-1;%误差最小的块就是最佳匹配块
Block=form_patch(rows,cols,zx);
form_patch.m
function kuai=form_patch(rows,cols,z)
%知道块在图像中所占的行和列的范围,得到块中每个像素点在图像中的位置(第多少个像素)
%便于引用
numhang=length(rows);
numlie=length(cols);
HJ=zeros(numhang,numlie);
LJ=zeros(numhang,numlie);
for ii=1:numlie
HJ(:,ii)=rows;%行铺成的矩阵
end
for jj=1:numhang
LJ(jj,:)=cols;%列铺成的矩阵
end
kuai=(LJ-1)*z+HJ;%得到待修复块(在图像中的具体位置
img2ind.m
%---------------------------------------------------------------------
% 把RGB图像转换为索引图像
%---------------------------------------------------------------------
function ind = img2ind(img)
s=size(img); ind=reshape(1:s(1)*s(2),s(1),s(2));
ind2img.m
%---------------------------------------------------------------------
% 把索引图像转换为RGB图像
%---------------------------------------------------------------------
function img2 = ind2img(ind,img)
for i=3:-1:1, temp=img(:,:,i); img2(:,:,i)=temp(ind); end;
PSNR.m
%----------------------------------------------------------------------
%PSNR值大小
%-------------------------------------------------------------------------
function PSNR=PSNR(u,v)
[m,n]=size(u(:,:,1));
a=0;
for i=1:m
for j=1:n
t(i,j,1)=u(i,j,1)-v(i,j,1);
a=a+t(i,j,1)^2;
end
end
mse=a/(m*n);
PSNR_R=10*log10(255^2/mse);
b=0;
for i=1:m
for j=1:n
t(i,j,2)=u(i,j,2)-v(i,j,2);
b=b+t(i,j,2)^2;
end
end
mse=b/(m*n);
PSNR_G=10*log10(255^2/mse);
c=0;
for i=1:m
for j=1:n
t(i,j,3)=u(i,j,3)-v(i,j,3);
c=c+t(i,j,3)^2;
end
end
mse=c/(m*n);
PSNR_B=10*log10(255^2/mse);
PSNR=(PSNR_R+PSNR_G+PSNR_B)/3;
3、运行结果
5、文献原文及代码文件点击下方连接,可直接跑通
https://download.csdn.net/download/weixin_41809117/88645671?spm=1001.2014.3001.5503https://download.csdn.net/download/weixin_41809117/88645671?spm=1001.2014.3001.5503
6、参考文献
[1] Criminisi A .Object Removal by Exemplar-based Inpainting (PDF) Jun[J].WI Proc. IEEE Computer Vision and Pattern Recognition, 2003, 2003, 2.文章来源:https://www.toymoban.com/news/detail-785648.html
创作不易,请尊重原创,引用请添加原文链接......文章来源地址https://www.toymoban.com/news/detail-785648.html
到了这里,关于【图像处理-图像修复】Criminisi图像修复算法(附matlab代码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!