-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSearch.m
123 lines (103 loc) · 2.72 KB
/
Search.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
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
function [ p5,p10,p20, recall ] = Search( input_index, displayResult, cate_count)
%SEARCH Summary of this function goes here
% Detailed explanation goes here
p5 = 0;p10=0;p20=0;
directory = 'SR_testData\\MPEGdata';
filelist = dir(fullfile(directory, '*.mat'));
filenames = cell(length(filelist),1);
for i=1:length(filenames)
filenames(i) = {filelist(i).name};
end
[sorted,~] = sort_nat(filenames);
matrixlist = cell(length(sorted),1);
for j = 1:length(sorted)
file = sorted(j);
css_struct = load([directory ,'\\' , file{1}],'saved');
matrixlist{j} = css_struct.saved;
end
%comment this line if input_index is set outside
%input_index = 10;
CSS_I = matrixlist{input_index};
len = length(matrixlist);
costList = zeros(len,1);
for k = 1:len
cost = matchingAlgo(CSS_I, matrixlist{k});
costList(k) = cost;
end
[Y,I] = sort(costList);
%take the top 20 as the query result
count = 20;
resultY = Y(1:count,:);
resultI = I(1:count,:);
for i=1:length(resultY)
value = resultY(i);
index = resultI(i);
file = sorted(index);
disp([file{1},' cost: ',num2str(value)]);
end
inputcssfile = sorted(input_index);
inputcssfilename = inputcssfile{1};
%display first top-20 elements
if(displayResult)
rows = 5;
cols = 5;
img_dir = 'original';
subplot(rows,cols,3);
inputimgname = strrep(inputcssfilename, '.txt.mat','');
imshow(255*imread([img_dir,'\\',inputimgname]));
title('input image');
for i=1:length(resultY)
index = resultI(i);
file = sorted(index);
cssname = file{1};
imgname = strrep(cssname, '.txt.mat','');
subplot(rows,cols,i + 5);
imshow(255*imread([img_dir,'\\',imgname]));
end
end
%calculate count in this class
sum = 0;
count = 0;
for i = 1:length(cate_count)
sum = sum + cate_count(i);
if(sum < input_index)
continue
else
count = cate_count(i);
break;
end
end
%calculate precision
p5 = 0;
p10 = 0;
p20 = 0;
hyphen_idx = find(inputcssfilename == '-');
input_catename = inputcssfilename(1:hyphen_idx-1);
match_count = 0;
for i = 1:20
index = resultI(i);
file = sorted(index);
name = file{1};
hyphen_idx = find(name == '-');
cate_name = name(1:hyphen_idx-1);
if( strcmp(input_catename, cate_name) )
match_count = match_count + 1;
end
if(i == 5)
p5 = 1.0*match_count/min(5, count);
end
if(i == 10)
p10 = 1.0*match_count/min(10, count);
end
if(i == 20)
p20 = 1.0*match_count/min(20, count);
end
end
%calculate recall
same_class_index = input_index:input_index + count - 1;
ranks = zeros(length(same_class_index),1);
for i = 1:length(ranks)
ranks(i) = find(I == same_class_index(i));
end
recall = max(ranks);
end