viola-jones: generate_feat_in_struct_for_C.m

File generate_feat_in_struct_for_C.m, 3.6 kB (added by agr, 1 year ago)

Feature Generation Matlab File

Line 
1 % script to generate features in struct form to be used by C code
2 clear all;
3
4 load ('output/features.mat');
5 %load ('output/10f+50f/TrainResults_Stage1_10f.mat');
6 %load ('output/TrainResults_Stage2.mat');
7 %load ('output/TrainResults_Stage3.mat');
8 %load ('output/10f+50f/TrainResults_Stage2_50f_used_as_stage4.mat');
9 load ('output/TrainResults_Stage6_used_as_stage5.mat');
10
11 %-------------------------------------
12 fNbestArray = fNbestArray5;
13 thetaBestArray = thetaBestArray5;
14 pBestArray = pBestArray5;
15 alpha_t_Array = alpha_t_Array5;
16
17 % scaling the feature parameters (so that floating point is not required)
18 % since cmucam3 doesn't have floating points, let's multiply all
19 % "alpha" by a factor of '50', (all that matters is relative
20 % magnitude of alpha, this way we preserve that, w/o losing out
21 % on accuracy due to lack of floating point)
22 scale_factor = 100;
23 thetaBestArray = round(thetaBestArray * scale_factor);
24 alpha_t_Array = round(alpha_t_Array * scale_factor);
25
26 % final threshold (stage dependent)
27 alpha_thresh = round(scale_factor + sum(alpha_t_Array)/2);
28 %-----------------------------------------
29
30 % fNbestArray, thetaBestArray, pBestArray, alpha_t_Array, f
31 num_feat = length(fNbestArray);
32 num_scales = 4;
33 scales = [30, 38, 48 , 60];
34 %scales = scales + 1;   % the scaling function assumes "features" are zero padded
35
36 %cc3_feature
37 %x = zeros(9,1);
38 %y = zeros(9,1);
39 %val_at_corners = zeros(9,1);
40 %parity = 0;
41 %thresh = 0;
42 %alpha = 0;
43
44 fid = fopen('C:\cygwin\home\Dhiraj\cmucam3\cc3\trunk\projects\viola-jones\feat_for_C_stage5.txt','W');
45
46 fprintf(fid, '{ \n');
47 for curr_feat_idx = 1:num_feat
48      fprintf(fid, '{ \n');  % for every feature
49        
50       % parameters that are constant for a particular feat, across all
51       % scales
52       parity = pBestArray(curr_feat_idx);
53       thresh = thetaBestArray(curr_feat_idx);
54       alpha = alpha_t_Array(curr_feat_idx);
55  
56       curr_feat = reshape(f(:,fNbestArray(curr_feat_idx)),25,25);
57     %  curr_feat = curr_feat(1:24,1:24);   % clip the last extra col & row 
58       for curr_scale_idx = 1:num_scales
59           % get all the requires parameters for this feature at this scale
60           feat = get_scaled_feature(curr_feat, scales(curr_scale_idx));
61           [y x] = find(feat ~= 0);
62                    
63           val_at_corners = [];
64           for i = 1:length(y)
65                   val_at_corners = [val_at_corners; feat(y(i),x(i))];
66           end
67          
68           % in C, index starts from zero
69           x = x - 1;
70           y = y - 1;
71          
72           % append zeros to make these arrays generic across all types of
73           % features
74           while ( length(y) ~= 9)
75              y = [y; 0];
76              x = [x; 0];
77              val_at_corners = [val_at_corners; 0];
78           end
79          
80           % print the parameters for this feature
81             fprintf(fid, '{ ');   % beginning
82             fprintf(fid, '{%d, %d, %d, %d, %d, %d, %d, %d, %d}, ', x(1), x(2), x(3), x(4), x(5), x(6), x(7), x(8), x(9));
83             fprintf(fid, '{%d, %d, %d, %d, %d, %d, %d, %d, %d}, ', y(1), y(2), y(3), y(4), y(5), y(6), y(7), y(8), y(9));
84             fprintf(fid, '{%d, %d, %d, %d, %d, %d, %d, %d, %d}, ', val_at_corners(1), val_at_corners(2), val_at_corners(3), val_at_corners(4), val_at_corners(5), val_at_corners(6), val_at_corners(7), val_at_corners(8), val_at_corners(9));
85             fprintf(fid, '%d, ', parity);
86             fprintf(fid, '%4.0d, ', round(thresh));
87             fprintf(fid, '%4.0d', round(alpha));
88             fprintf(fid, ' }, \n'); % end
89       end
90      
91       fprintf(fid, '}, \n');
92 end
93
94 fprintf(fid ,'};');
95 fclose(fid);
96          
97