5.第四部分代码函数neighborSelect分析ing..

输入: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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不做笔记的程序员不是好的码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值