输入:h ,w 图像块维度,ntr=length(filelist_p) 测试集的照片个数,pathsize 图像块大小边长,step步长,R=searchregion,height,width 照片维度大小,NN,Patches 拼接出来的图像块像素数据,imp,ims 读入的图像double类型一维数据,photo2sketch
过程:
输出:
function [ candidates ] = neighborSelect(h,w,ntr,patchSize,step,R,height,width,NN,Patches,imp,ims,photo2sketch)
if ntr < NN % ntr即最近邻的个数,NN训练图片的个数;判断训练图片个数一定要大于等于近邻个数
error('The number of nearest neighbors must be samller than the number of training images!');
end
% N是最后一块像素块坐标的高,M是最后一块像素块坐标的宽,r是每一列每个的图像块的像素块的坐标,c是每一行每个的图像块的坐标; 话说这样的话为啥不直接用最后的坐标呢?不就多一个图像块罢了
N = height - patchSize + 1;
M = width - patchSize + 1;
r = [1:step:N];
if r(end) == N
;
else
r = [r N];
end
c = [1:step:M];
if c(end) == M
;
else
c = [c M];
end
for i=1:h
fprintf('\tRow %d (out of %d)...',i,h);
% tic;
for j=1:w
indexpatches = zeros(ntr,3); % 3维的意思吗,那就是每一张图片三个维度初始化
neighborpatches = zeros(ntr,patchSize*patchSize);
for kk = 1:ntr
temp = [];
indextemp = [];
startRow = r(i) - R;
startCol = c(j) - R;
endRow = r(i) + patchSize - 1;
endCol = c(j) + patchSize - 1;
Rstart = -R;
Rend = R;
Cstart = -R;
Cend = R;
if startRow < 1
startRow = 1;
Rstart = 1 - startRow;
end
if endRow > height - R
endRow = height;
Rend = height - endRow;
end
if startCol < 1
startCol = 1;
Cstart = 1 - startCol;
end
if endCol > width - R
endCol = width;
Cend = width - endCol;
end
for i0 = Rstart:Rend
for j0 = Cstart:Cend
blk = imp(r(i)+i0:r(i)+i0+patchSize-1,c(j)+j0:c(j)+j0+patchSize-1,kk);
blk = blk(:)';
temp = [temp;blk];
indextemp = [indextemp;i0,j0,kk];
end
end
Dist1 = sum(((repmat(Patches(:,i,j)',[size(temp,1),1])-temp).^2),2);
[foo, idx] = sort(Dist1,'ascend');
indexpatches(kk,:) = indextemp(idx(1),:);
neighborpatches(kk,:) = temp(idx(1),:);
end
Dist = sum(((repmat(Patches(:,i,j)',[ntr,1])-neighborpatches).^2),2);
[foo, idx] = sort(Dist,'ascend');
idx = idx(1:NN);
tol = 10^(-3);
C = zeros(NN,NN);
C = (-repmat(Patches(:,i,j),[1,NN])+neighborpatches(idx,:)')'*(-repmat(Patches(:,i,j),[1,NN])+neighborpatches(idx,:)');
if rank(C) ~= NN
C = C + 10^(-4)*eye(NN);
end
W_Kneighbor = C\ones(NN,1);
W_Kneighbor = W_Kneighbor/sum(W_Kneighbor);
candidates(i,j).W = W_Kneighbor;
candidates(i,j).idx = indexpatches(idx,:);
candidates(i,j).patches = [];
candidates(i,j).ppatches= [];
candidates(i,j).patches3= [];
for nn = 1:NN
i0 = candidates(i,j).idx(nn,1);
j0 = candidates(i,j).idx(nn,2);
qq = candidates(i,j).idx(nn,3);
blk = ims(r(i)+i0:r(i)+i0+patchSize-1,c(j)+j0:c(j)+j0+patchSize-1,qq);
blk = blk(:)';
candidates(i,j).patches = [candidates(i,j).patches;blk];
blk = imp(r(i)+i0:r(i)+i0+patchSize-1,c(j)+j0:c(j)+j0+patchSize-1,qq);
blk = blk(:)';
candidates(i,j).ppatches = [candidates(i,j).ppatches;blk];
end
end
% t = toc;
fprintf('done!\n');
end