/
extract_features_in_parallel.m
137 lines (117 loc) · 5.14 KB
/
extract_features_in_parallel.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
function extract_features_in_parallel(obj,imgsetname)
%
% This function reads each image in imageset and:
% 1) computes superpixels
% 2) builds edges between superpixels of the same image
% 3) extracts sift features
% 4) store all the extracted info in a file
%
% Input:
% obj: obj of class jcas with all user input
% imgset: collection of image indexes (equal to params.training or
% param.test)
%
% Output:
% There is only one output as such.
% num_features: which is a vector that contains the number of features
% extracted in each image of imageset
%
% The rest of the output is stored in 3 files called 0001-imagedata.mat
% if the input file image is called 0001.jpg.
% The information stored are:
%ImageData :
% img_info.I: RGB image
% img_info.Y: width of image img_feat.I.
% img_info.X: hieght of image img_feat.I.
%
%Superpixels :
% img_sp.Iseg: image iwth superpixels of image img_feat.I
% img_sp.spInd: label given to each pixel (tells you to which
% superpixels that pixel belong to.
% img_sp.nbSp: number of initial label (i.e. number of superpixels).
% img_sp.edges:
% img_sp.length_common_boundary:
%
%Features :
%img_feat : struct containing at least img_feat.locations and
%img_feat.descriptors
if ~obj.destpathmade
error('Before doing anything you need to call obj.makedestpath')
end
display(['Extracting features from ' obj.dbparams.name '. Total number of images in current set ' num2str(length(obj.dbparams.(imgsetname)))]);
% compute number of images in imageset
num_imgs = length(obj.dbparams.(imgsetname));
imgset=obj.dbparams.(imgsetname);
% if it does not exist create destination directory
% if (~exist(obj.dbparams.destmatpath, 'dir'))
% mkdir(obj.dbparams.destmatpath);
% end
% for each image
if ~exist(sprintf(obj.unary.features.destmatpath,'num_features_per_image'),'file')
num_features_per_images=[];
else
load(sprintf(obj.unary.features.destmatpath,'num_features_per_image'),'num_features_per_images');
end
tmp=zeros(1,num_imgs);
parfor i=1:num_imgs
tmp(i)=process_image(obj,imgset(i));
end
num_features_per_images(imgset)=tmp;
save(sprintf(obj.unary.features.destmatpath,'num_features_per_image'),'num_features_per_images');
function num_features=process_image(obj,ind)
fprintf('\t Image: %d \n',ind);
% allocate memory
img_info = struct();
img_sp=struct();
img_feat=struct();
% build filename for storing extracted image information
imagedata_filename = sprintf(obj.dbparams.destmatpath,sprintf('%s-imagedata',obj.dbparams.image_names{ind}));
img_feat_filename=sprintf(obj.unary.features.destmatpath,sprintf('%s-unfeat',obj.dbparams.image_names{ind}));
img_sp_filename=sprintf(obj.superpixels.destmatpath,sprintf('%s-imgsp',obj.dbparams.image_names{ind}));
% check if data have already been computed
if (~exist(imagedata_filename, 'file') || ~exist(img_feat_filename,'file')|| ...
~exist(img_sp_filename,'file') || obj.force_recompute.imagedata || ...
obj.force_recompute.trainingdata_SP || obj.force_recompute.trainingdata_UF)
% load image
img_info.I = imread([obj.dbparams.imgpath,obj.dbparams.image_names{ind},...
obj.dbparams.format]);
% extract image size
[img_info.X,img_info.Y,~] = size(img_info.I);
% H = X; W=Y;
% -----------------------------------------------------------------
% Superpixels
% -----------------------------------------------------------------
if ~exist(img_sp_filename,'file')||obj.force_recompute.trainingdata_SP
[img_sp.spInd,img_sp.nbSp,img_sp.Iseg]=obj.computeSuperpixels(img_info.I);
% -----------------------------------------------------------------
% Build graph
% -----------------------------------------------------------------
% compute initial graph
[~, edges]=lattice(img_info.X,img_info.Y);
% find real edges (i.e. connection between pixels that belong to
% different superpixels)
edges = img_sp.spInd(edges(img_sp.spInd(edges(:,1))-img_sp.spInd(edges(:,2)) ~=0,:));
L = sparse(edges(:,1), edges(:,2), ones(size(edges(:,1),1),1), img_sp.nbSp, img_sp.nbSp);
L=tril(L+L');
img_sp.edges =[];
[img_sp.edges(:,1), img_sp.edges(:,2), img_sp.length_common_boundary] = find(L);
save(img_sp_filename,'img_sp');
end
% -----------------------------------------------------------------
% Features
% -----------------------------------------------------------------
if ~exist(img_feat_filename,'file')||obj.force_recompute.trainingdata_UF
img_feat=obj.computeFeatures_unary(img_info.I);
num_features=img_feat.num_features;
save(img_feat_filename,'img_feat');
end
% -----------------------------------------------------------------
% Save info
% -----------------------------------------------------------------
if ~exist(imagedata_filename, 'file')||obj.force_recompute.imagedata
save(imagedata_filename, 'img_info'); %'features','I', 'H','W','Iseg', 'labels', 'nlabels', 'edges', 'length_common_boundary');
end
else
tmp=load(img_feat_filename);
num_features=tmp.img_feat.num_features;
end