1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| >> run('VLFEATROOT/toolbox/vl_setup');
>> gridSpacing = 6;
>> I = vl_impattern('roofs1') ;
>> I = im2single(rgb2gray(I));
>> [im_h, im_w] = size(I);
>> binSize = 16 ;
magnif = 3 ;
Is = vl_imsmooth(I, sqrt((binSize/magnif)^2 - .25)) ;
[f,d] = vl_dsift(Is, 'size', binSize,'step',8) ;
f(3,:) = binSize/magnif ;
f(4,:) = 0 ;
[f_, d_] = vl_sift(I, 'frames', f) ;
>> d_=double(d_);
for i=1:length(d_(1,:))
x=sum(d_(:,i));
for j=1:length(d_(:,1))
d_(j,i)=d_(j,i)./x;
end
end;
>> patchSize=16;
remX = mod(im_w-patchSize,gridSpacing);
offsetX = floor(remX/2)+1;
remY = mod(im_h-patchSize,gridSpacing);
offsetY = floor(remY/2)+1;
[gridX,gridY] = meshgrid(offsetX:gridSpacing:im_w-patchSize+1, offsetY:gridSpacing:im_h-patchSize+1);
>>
>> x = gridX(:) + binSize/2 - 0.5;
y = gridY(:) + binSize/2 - 0.5;
>> pyramid = [1, 2, 4]; % spatial block structure for the SPM
knn = 5; % number of neighbors for local coding
c = 10;
>> load('C:\Program Files\MATLAB\MATLAB Production Server\R2013a\bin\workspace\New\dictionary\Caltech101_SIFT_Kmeans_1024.mat');
>> B=B';
>> d_=d_';
>> nbase = size(B, 1);
nframe = size(X, 1);
Undefined function or variable 'X'.
Did you mean:
>> nbase = size(B, 1);
nframe = size(d_, 1);
>> idxBin = zeros(nframe, 1);
>> % llc coding
if ~exist('knn', 'var') || isempty(knn),
knn = 5;
end
if ~exist('beta', 'var') || isempty(beta),
beta = 1e-4;
end
% find k nearest neighbors
XX = sum(X.*X, 2);
BB = sum(B.*B, 2);
D = repmat(XX, 1, nbase)-2*X*B'+repmat(BB', nframe, 1);
IDX = zeros(nframe, knn);
for i = 1:nframe,
d = D(i,:);
[dummy, idx] = sort(d, 'ascend');
IDX(i, :) = idx(1:knn);
end
Undefined function or variable 'X'.
Did you mean:
>> % llc coding
if ~exist('knn', 'var') || isempty(knn),
knn = 5;
end
if ~exist('beta', 'var') || isempty(beta),
beta = 1e-4;
end
% find k nearest neighbors
XX = sum(d_.*d_, 2);
BB = sum(B.*B, 2);
D = repmat(XX, 1, nbase)-2*d_*B'+repmat(BB', nframe, 1);
IDX = zeros(nframe, knn);
for i = 1:nframe,
d = D(i,:);
[dummy, idx] = sort(d, 'ascend');
IDX(i, :) = idx(1:knn);
end
>> % llc approximation coding
II = eye(knn, knn);
Coeff = zeros(nframe, nbase);
for i=1:nframe
idx = IDX(i,:);
z = B(idx,:) - repmat(d_(i,:), knn, 1); % shift ith pt to origin
C = z*z'; % local covariance
C = C + II*beta*trace(C); % regularlization (K>D)
w = C\ones(knn,1);
w = w/sum(w); % enforce sum(w)=1
Coeff(i,idx) = w';
end
>> llc_codes=Coeff';
>>
% spatial levels
pLevels = length(pyramid);
% spatial bins on each level
pBins = pyramid.^2;
% total spatial bins
tBins = sum(pBins);
beta = zeros(1024, tBins);
bId = 0;
>> for iter1 = 1:pLevels,
nBins = pBins(iter1);
wUnit = im_w / pyramid(iter1);
hUnit = im_h / pyramid(iter1);
% find to which spatial bin each local descriptor belongs
xBin = ceil(x / wUnit);
yBin = ceil(y / hUnit);
idxBin = (yBin - 1)*pyramid(iter1) + xBin;
for iter2 = 1:nBins,
bId = bId + 1;
sidxBin = find(idxBin == iter2);
if isempty(sidxBin),
continue;
end
beta(:, bId) = max(llc_codes(:, sidxBin), [], 2);
end
end |
Partager