-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateSiftDescriptors.m
79 lines (64 loc) · 2.99 KB
/
GenerateSiftDescriptors.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
function [] = GenerateSiftDescriptors( imageFileList,params)
imageBaseDir = params.image_dir;
dataBaseDir = params.data_dir;
maxImageSize = params.max_image_size;
numTextonImages=params.num_texton_images;
%function [] = GenerateSiftDescriptors( imageFileList, imageBaseDir, dataBaseDir, maxImageSize, gridSpacing, patchSize, canSkip )
%function [] = GenerateSiftDescriptors( imageFileList, imageBaseDir, dataBaseDir, maxImageSize, gridSpacing, patchSize, canSkip )
%
%Generate the dense grid of sift descriptors for each
% image
%
% 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 files
% maxImageSize: the max image size. If the image is larger it will be
% resampeled.
% gridSpacing: the spacing for the grid to be used when generating the
% sift descriptors
% patchSize: the patch size used for generating the sift descriptor
% 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 Sift Descriptors\n\n');
for f = 1:size(imageFileList,1)
%% load image
imageFName = imageFileList{f};
[dirN base] = fileparts(imageFName);
baseFName = [dirN filesep base];
outFName = fullfile(dataBaseDir, sprintf('%s_sift.mat', baseFName));
imageFName = fullfile(imageBaseDir, imageFName);
if(size(dir(outFName),1)~=0 && params.can_skip && params.can_skip_sift)
fprintf('Skipping %s\n', imageFName);
continue;
end
I = sp_load_image(imageFName);
[hgt wid] = size(I);
if min(hgt,wid) > maxImageSize
I = imresize(I, maxImageSize/min(hgt,wid), 'bicubic');
fprintf('Loaded %s: original size %d x %d, resizing to %d x %d\n', ...
imageFName, wid, hgt, size(I,2), size(I,1));
[hgt wid] = size(I);
end
%% make grid (coordinates of upper left patch corners)
remX = mod(wid-patchSize,gridSpacing);
offsetX = floor(remX/2)+1;
remY = mod(hgt-patchSize,gridSpacing);
offsetY = floor(remY/2)+1;
[gridX,gridY] = meshgrid(offsetX:gridSpacing:wid-patchSize+1, offsetY:gridSpacing:hgt-patchSize+1);
fprintf('Processing %s: wid %d, hgt %d, grid size: %d x %d, %d patches\n', ...
imageFName, wid, hgt, size(gridX,2), size(gridX,1), numel(gridX));
%% find SIFT descriptors
siftArr = sp_find_sift_grid(I, gridX, gridY, patchSize, 0.8);
siftArr = sp_normalize_sift(siftArr);
features.data = siftArr;
features.x = gridX(:) + patchSize/2 - 0.5;
features.y = gridY(:) + patchSize/2 - 0.5;
features.wid = wid;
features.hgt = hgt;
sp_make_dir(outFName);
save(outFName, 'features');
end % for
end % function