% Compute confidences along the fill front------沿着填补前沿计算自信度
for k=dR'
Hp = getpatch(sz,k);
q = Hp(~(fillRegion(Hp)));
C(k) = sum(C(q))/numel(Hp);
end
% Compute patch priorities = confidence term * data term-----计算修补块的优先权
D(dR) = abs(Ix(dR).*N(:,1)+Iy(dR).*N(:,2)) + 0.001;
priorities = C(dR).* D(dR);
% Find patch with maximum priority, Hp-----寻找最大优先权的修补快
[unused,ndx] = max(priorities(:));
p = dR(ndx(1));
[Hp,rows,cols] = getpatch(sz,p);
toFill = fillRegion(Hp);
% Find exemplar that minimizes error, Hq-------发现错误最小化的样本
Hq = bestexemplar(img,img(rows,cols,:),toFill',sourceRegion);
% Update fill region-------更新填充区域
toFill = logical(toFill); % Marcel 11/30/05
fillRegion(Hp(toFill)) = false;
% Propagate confidence & isophote values---------传递自信度和等照度线的值
C(Hp(toFill)) = C(p);
Ix(Hp(toFill)) = Ix(Hq(toFill));
Iy(Hp(toFill)) = Iy(Hq(toFill));
% Copy image data from Hq to Hp-----从Hq复制图片数据到Hp
ind(Hp(toFill)) = ind(Hq(toFill));
img(rows,cols,:) = ind2img(ind(rows,cols),origImg);
% Visualization stuff ---------可视化的东西
if nargout==6
ind2 = ind;
ind2(logical(fillRegion)) = 1; % Marcel 11/30/05
%ind2(fillRegion) = 1; % Original
fillMovie(iter).cdata=uint8(ind2img(ind2,origImg));
fillMovie(iter).colormap=[];
end
iter = iter+1;
end
inpaintedImg=img;
%---------------------------------------------------------------------
% Scans over the entire image (with a sliding window)整个图像扫描(有一个滑动窗口)
% for the exemplar with the lowest error. Calls a MEX function.为最低的错误的样本。MEX调用一个函数。
%---------------------------------------------------------------------
function Hq = bestexemplar(img,Ip,toFill,sourceRegion)
m=size(Ip,1); mm=size(img,1); n=size(Ip,2); nn=size(img,2);
best = bestexemplarhelper(mm,nn,m,n,img,Ip,toFill,sourceRegion);
Hq = sub2ndx(best(1):best(2),(best(3):best(4))',mm);
%---------------------------------------------------------------------
% Returns the indices for a 9x9 patch centered at pixel p.返回像素中心于第1 9x9修补指数
%---------------------------------------------------------------------
function [Hp,rows,cols] = getpatch(sz,p)
% [x,y] = ind2sub(sz,p); % 2*w+1 == the patch size
w=4; p=p-1; y=floor(p/sz(1))+1; p=rem(p,sz(1)); x=floor(p)+1;
rows = max(x-w,1):min(x+w,sz(1));
cols = (max(y-w,1):min(y+w,sz(2)))';
Hp = sub2ndx(rows,cols,sz(1));
%---------------------------------------------------------------------
% Converts the (rows,cols) subscript-style indices to Matlab index-style
% indices. Unfortunately, 'sub2ind' cannot be used for this.
%---------------------------------------------------------------------
function N = sub2ndx(rows,cols,nTotalRows)
X = rows(ones(length(cols),1),:);
Y = cols(:,ones(1,length(rows)));
N = X+(Y-1)*nTotalRows;
%---------------------------------------------------------------------
% Converts an indexed image into an RGB image, using 'img' as a colormap
%索引图像转换成RGB图像,使用'img'作为一个颜色表
%---------------------------------------------------------------------
function img2 = ind2img(ind,img)
for i=3:-1:1, temp=img(:,:,i); img2(:,:,i)=temp(ind); end;
%---------------------------------------------------------------------
% Converts an RGB image into a indexed image, using the image itself as
% the colormap.利用图像本身的颜色表,将索引图像转换成RGB图像。
%---------------------------------------------------------------------
function ind = img2ind(img)
s=size(img); ind=reshape(1:s(1)*s(2),s(1),s(2));
%---------------------------------------------------------------------
% Loads the image and it's fill region, using 'fillColor' as a marker
% value for knowing which pixels are to be filled.使用fillColor作为标记值了解哪些%像素将被填充来加载图像和他的填充区域
%---------------------------------------------------------------------
function [img,fillImg,fillRegion] = loadimgs(imgFilename,fillFilename,fillColor)
img = imread(imgFilename); fillImg = imread(fillFilename);
fillRegion = fillImg(:,:,1)==fillColor(1) & ...
fillImg(:,:,2)==fillColor(2) & fillImg(:,:,3)==fillColor(3);