-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildHistograms.m
105 lines (89 loc) · 4.3 KB
/
BuildHistograms.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
function [ H_all ] = BuildHistograms(imageFileList,dataBaseDir,featureSuffix,dictionarySize,params);
%function [ H_all ] = BuildHistograms( imageFileList, dataBaseDir, featureSuffix, dictionarySize, canSkip )
%
%find texton labels of patches and compute texton histograms of all images
%
% For each image the set of sift descriptors is loaded and then each
% descriptor is labeled with its texton label. Then the global histogram
% is calculated for the image. If you wish to just use the Bag of Features
% image descriptor you can stop at this step, H_all is the histogram or
% Bag of Features descriptor for all input images.
%
% imageFileList: cell of file paths
% imageBaseDir: the base directory for the image files
% dataBaseDir: the base directory for the data files that are generated
% by the algorithm. If this dir is the same as imageBaseDir the files
% will be generated in the same location as the image file
% featureSuffix: this is the suffix appended to the image file name to
% denote the data file that contains the feature textons and coordinates.
% Its default value is '_sift.mat'.
% dictionarySize: size of descriptor dictionary (200 has been found to be
% a good size)
% canSkip: if true the calculation will be skipped if the appropriate data
% file is found in dataBaseDir. This is very useful if you just want to
% update some of the data or if you've added new images.
fprintf('Building Histograms\n\n');
%% load texton dictionary (all texton centers)
inFName = fullfile(dataBaseDir, sprintf('dictionary_%d%s', dictionarySize, featureSuffix));
load(inFName,'dictionary');
fprintf('Loaded texton dictionary: %d textons\n', dictionarySize);
%% compute texton labels of patches and whole-image histograms
H_all = [];
for f = 1:size(imageFileList,1)
imageFName = imageFileList{f};
[dirN base] = fileparts(imageFName);
baseFName = fullfile(dirN, base);
inFName = fullfile(dataBaseDir, sprintf('%s%s', baseFName, featureSuffix));
outFName = fullfile(dataBaseDir, sprintf('%s_texton_ind_%d%s', baseFName,dictionarySize, featureSuffix));
outFName2 = fullfile(dataBaseDir, sprintf('%s_hist_%d%s', baseFName, dictionarySize,featureSuffix));
outFName3 = fullfile(dataBaseDir, sprintf('%s_spquantized_%d%s', baseFName,dictionarySize, featureSuffix));
if(size(dir(outFName),1)~=0 && size(dir(outFName2),1)~=0&& size(dir(outFName3),1)~= 0&¶ms.can_skip && params.can_skip_buildhist)
fprintf('Skipping (build_histogram)%s\n', imageFName);
load(outFName2, 'H');
H_all = [H_all; H];
continue;
end
dictionary = double(dictionary);
%% load sift descriptors
load(inFName, 'features');
features.data = double(features.data);
ndata = size(features.data,1);
fprintf('Loaded (build_histogram) %s, %d descriptors\n', inFName, ndata);
%% find texton indices and compute histogram
texton_ind.data = zeros(ndata,1);
texton_ind.x = features.x;
texton_ind.y = features.y;
texton_ind.hgt = features.hgt;
texton_ind.wid = features.wid;
%run in batches to keep the memory foot print small
batchSize = 10000;
if ndata <= batchSize
dist_mat = sp_dist2(features.data, dictionary);
[min_dist, min_ind] = min(dist_mat, [], 2);
texton_ind.data = min_ind;
else
for j = 1:batchSize:ndata
lo = j;
hi = min(j+batchSize-1,ndata);
dist_mat = sp_dist2(features.data(lo:hi,:), dictionary);
[min_dist, min_ind] = min(dist_mat, [], 2);
texton_ind.data(lo:hi,:) = min_ind;
end
end
f = fopen(outFName3, 'w');
fprintf(f,'%d\n', length(texton_ind.x));
fprintf(f,'(%d,%d)\n', texton_ind.hgt, texton_ind.wid);
for i = 1:length(texton_ind.x),
fprintf(f,'(%d,%d):%d\n', ceil(texton_ind.y(i)), ceil(texton_ind.x(i)), texton_ind.data(i,:));
end
fclose(f);
H = hist(texton_ind.data, 1:dictionarySize);
H_all = [H_all; H];
%% save texton indices and histograms
save(outFName, 'texton_ind');
save(outFName2, 'H');
end
%% save histograms of all images in this directory in a single file
outFName = fullfile(dataBaseDir, sprintf('histograms_%d.mat', dictionarySize));
save(outFName, 'H_all', '-ascii');
end