-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParameterCodeImage.m
134 lines (126 loc) · 6.22 KB
/
ParameterCodeImage.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
disp(['===============> Getting parameters, mex-c code, and training images']);
mkdir('working'); % working directory to store .mat files.
mkdir('output'); % directory that contains output from the code.
%% parameters for the outer square bounding box
category = '1';
supressionModeInEStep = 'MatchingPursuit'; % matching pursuit in E step (other options: LocalSurroundSurpression)
templateSize = [60 60];
templateSize = single(templateSize);
partSize = floor(sqrt(templateSize(1)*templateSize(2))); % alias for the template size (radius)
resizeTrainingImages = true;
constantImageArea = 150^2; % for resizing images (keep a constant area)
numRandomStart = 3;
%% parameters for EM clustering
locationPerturbationFraction = .25; % the size of neighborhood for MAX2 pooling, as well as surround supression
locationPerturbationFraction_final = .4; % (used in later EM iterations) the size of neighborhood for MAX2 pooling, as well as surround supression
rotationRange = 0:2:0; % allowed global rotation of the template
subsampleS2 = 1; % subsampling step size for computing SUM2 maps
S2Thres = 5; % cut-off value for detected instances of the template
S1softthres = 0.9; % soft thresholding cutting-off for S1 maps
numIter = 10; % number of EM iterations
%% parameters for active basis
numCluster = 30; % number of data clusters
maxNumClusterMember = 30; % (no need to change) maximum number of training examples in each cluster used in re-learning the template
numElement = 12; % number of Gabors in active basis at the first scale
epsilon = .1; % allowed correlation between selected Gabors
subsample = 1; subsampleM1 = 1; % subsample in computing MAX1 maps
locationShiftLimit = 3; % shift in normal direction = locationShiftLimit*subsample pixels
orientShiftLimit = 1; % shift in orientation
%% parameters for detection (controls scaling of templates)
inhibitFind = -1; % whether to apply inhibition after detection for re-computing MAX2 score
resolutionGap = .1; % gap between consecutive resolutions in detection
numExtend = 1; % number of gaps extended both below and above zero
numResolution = numExtend*2 + 1; % number of resolutions to search for in detection stage
originalResolution = numExtend + 1; % original resolution is the one at which the imresize factor = 1
allResolution = (-numExtend : numExtend)*resolutionGap + 1.;
%% parameters for Gabor filters
numScale = 1; % number of Gabor scales (don't change it)
scales = 0.7; % scales of Gabor wavelets
numOrient = 16; % number of orientations
saturation = 6.; % saturation level for sigmoid transformation
doubleOrNot = -1;
%% parameters for exponential model
binSize = .2; % binsize for computing histogram of q()
numStoredPoint = 50; % number of stored lambda values
spacing = .1; % spacing between two adjacent points
%% parameters for normalization
localOrNot = 1; % if we use local normalization or not. If not, set it to -1
localHalfx = 20; localHalfy = 20; % the half range for local normalization, has to be quite large
windowNormalizeOrNot = -1; % whether normalize within the scanning window in detection
if (localOrNot>0)
windowNormalizeOrNot = -1;
end % if we use local normalization, we should not use window normalization in detection
thresholdFactor = .01; % divide the response by max(average, maxAverage*thresholdFactor)
%% read in positive images
sizeTemplatex = templateSize(1);
sizeTemplatey = templateSize(2);
halfTemplatex = floor(sizeTemplatex/2);
halfTemplatey = floor(sizeTemplatey/2);
imageFolder = 'positiveImage'; % folder of training images
imageName = dir([imageFolder '/*.jpg']);
numImage = size(imageName, 1); % number of training images
Ioriginal = cell(1, numImage);
for img = 1 : numImage
tmpIm = imread([imageFolder '/' imageName(img).name]);
if size(tmpIm,3) == 3
tmpIm = rgb2gray(tmpIm);
end
sx = size(tmpIm,1); sy = size(tmpIm,2);
if resizeTrainingImages
tmpIm = imresize( tmpIm, sqrt(constantImageArea/(sx*sy)), 'bilinear' );
end
Ioriginal{img} = single(tmpIm);
J0 = Ioriginal{img};
J = cell(1, numResolution);
for r=1:numResolution
J{r} = imresize(J0, allResolution(r), 'nearest'); % scaled images
end
multipleResolutionImageName = ['working/multipleResolutionImage' num2str(img)];
save(multipleResolutionImageName, 'J');
end
count = 0;
% generate the set of geometric transformations for each template
for templateScaleInd = 0:0 % large scale change
for rotation = rotationRange
for rowScale = 2.^[0] % small scale change and aspect ratio change
for colScale = 2.^[0]
count = count + 1;
templateTransform{count} = [templateScaleInd rowScale colScale rotation];
end
end
end
end
nTransform = count;
% prepare morph-back mappings for geometric transforms
largestPartSizeX = templateSize(1); largestPartSizeY = templateSize(2);
denseX = -floor(largestPartSizeX/2) + (1:largestPartSizeX);
denseY = -floor(largestPartSizeY/2) + (1:largestPartSizeY);
count = 0;
inRow = zeros(length(denseX)*length(denseY),1,'single');
inCol = zeros(length(denseX)*length(denseY),1,'single');
inO = zeros(numel(inRow),1,'single');
inS = zeros(numel(inRow),1,'single');
for y = denseY
for x = denseX
count = count+1;
inRow(count) = x;
inCol(count) = y;
end
end
outRow = cell(nTransform,1);
outCol = cell(nTransform,1);
for iT = 1:nTransform
tScale = templateTransform{iT}(1);
rScale = templateTransform{iT}(2);
cScale = templateTransform{iT}(3);
[outRow{iT}, outCol{iT}] = ...
mexc_TemplateAffineTransform(tScale,rScale,cScale,...
templateTransform{iT}(4),inRow,inCol,inO,inS,numOrient);
end
save('partLocConfig.mat','templateSize','scales','numRandomStart','maxNumClusterMember',...
'S2Thres','S1softthres','allResolution','resizeTrainingImages','constantImageArea',...
'category','numOrient','localOrNot','subsample','saturation','locationShiftLimit','orientShiftLimit',...
'numElement','thresholdFactor','doubleOrNot', 'numCluster', 'numIter', 'subsampleS2', 'locationPerturbationFraction',...
'partSize','rotationRange','nTransform','templateTransform','S1softthres','subsampleM1','localHalfx','localHalfy',...
'inRow', 'inCol', 'outRow', 'outCol', 'largestPartSizeX', 'largestPartSizeY','locationPerturbationFraction_final');
clear Ioriginal