-
Notifications
You must be signed in to change notification settings - Fork 0
/
findBestMatchANN.m
45 lines (35 loc) · 1.37 KB
/
findBestMatchANN.m
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
function [ min_difference ] = findBestMatchANN( fVectorB, fVectorBp, A, Ap, neighborhood_size, anno, annPts, k, eps )
%findBestMatchANN Find the pixel in A/Ap that best matches the feature
%vector given using ANN.
% Detailed explanation goes here
%If at the edge of Bp, use brute force match
% if any(fVectorBp == -1)
% min_difference = findBestMatchBrute(fVectorB, fVectorBp, A , Ap, neighborhood_size);
% return;
% end
%ANN match
Afvectors = A.fvectors;
full_neighborhood = neighborhood_size*neighborhood_size;
halfNeighborhoodSize = floor(full_neighborhood/2);
%Gaussian weights
gauss_matrix = fspecial('gaussian', neighborhood_size);
gauss_weights = reshape(gauss_matrix.',1,[]); % single vector form
partial_gauss_weights = gauss_weights(1, 1:halfNeighborhoodSize);
concatGaussWeights = [gauss_weights partial_gauss_weights];
%square root of the gaussian weights
concatGaussWeightsSR = sqrt(concatGaussWeights);
%% Use ANN to find nearest neighbor of this B+Bp fvector
%numRowsA = size(Afvectors,1);
numColsA = size(Afvectors,2);
min_difference.norm = intmax; % start with max integer value
pt = [fVectorB fVectorBp];
pt = pt.*concatGaussWeightsSR;
pt = pt';
[idx dst] = ksearch(anno, pt, k, eps);
% nearest_neighbor = annPts(idx);
rowA = ceil(double(idx)/numColsA);
colA = idx - ((rowA-1)*numColsA);
min_difference.norm = dst;
min_difference.i = rowA;
min_difference.j = colA;
end