viola-jones: get_scaled_feature.m

File get_scaled_feature.m, 3.1 kB (added by agr, 1 year ago)
Line 
1 function [scaled_feature] = get_scaled_feature(orig_f, wsize)
2 % USAGE: function to compute the scaled feature
3 %
4 % OUTPUT:
5 %   scaled_feature = output scaled feature (wsize+1)*(wsize+1)
6 %
7 % INPUT:
8 %   orig_f = original feature (24x24) (actually, 25x25 after zero padding)
9 %   wsize = size of the final scaled feature (square window)
10 %
11 % CREATED BY:
12 %   Dhiraj Goel, Feb 2006
13
14 nonzero_elements_idx = find(orig_f ~= 0);
15 [nRows nCols] = size(orig_f);
16 if (nRows ~= nCols)
17     disp('Error: Base window is not square');
18     return;
19 end
20
21 if (nRows ~= 25)
22     disp('Warning: Size of base window is not 24');     
23     % due to zero padding, the size becomes 25x25
24 end
25
26 % scale the indices of non zero elements to get the new "scaled" matrix
27 wsize = wsize + 1;  % zero padding
28
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 if 0
31 scale_factor = (wsize-1)/(nRows-1);  % actual patch sizes..w/o padding
32
33 % declare the scaled feature
34 scaled_feature = zeros(wsize);
35
36 for i = 1:length(nonzero_elements_idx)
37    curr_col = ceil(nonzero_elements_idx(i)/nRows);
38    curr_row = nonzero_elements_idx(i) - nRows*(curr_col-1);
39    
40    new_row = max(round(curr_row * scale_factor),1);
41    new_col = max(round(curr_col * scale_factor),1);
42    
43    new_row = min(new_row, wsize);
44    new_col = min(new_col, wsize);
45 %   keyboard;
46    scaled_feature(new_row, new_col) = orig_f(curr_row, curr_col);
47    
48 end
49
50 end
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 % new algo
53 % wsize --> padded
54 % nRows --> padded
55
56 scale_factor = (wsize-1)/(nRows-1);  % actual patch sizes..w/o padding
57
58 % declare the scaled feature
59 scaled_feature = zeros(wsize);
60
61 % find the [x y] of nonzero elements in orig_f
62 [y x] = find(orig_f ~= 0);
63
64 % find the scaled feature point for the first non zero element
65 if (y(1) == 1)
66     first_row = 1;
67 else 
68     first_row = max(round(y(1) * scale_factor),1);
69 end
70
71 if (x(1) == 1)
72     first_col = 1;
73 else
74     first_col = max(round(x(1) * scale_factor),1);
75 end
76
77 scaled_feature(first_row, first_col) = orig_f(y(1), x(1));
78
79 last_row = first_row;
80 last_col = first_col;
81
82 % find the scaled points for the remaining elements
83 for j = 2:length(x)
84    
85     if ( x(j) == x(j-1) ) % same col
86         new_row_offset = max(round((y(j)-y(j-1)) * scale_factor),1);
87         new_row = last_row + new_row_offset;
88         new_col = last_col;
89        
90         scaled_feature(new_row, new_col) = orig_f(y(j),x(j));
91        
92         last_col = new_col;
93         last_row = new_row;
94        
95     else   
96         % new col --> same row as first_element, diff col
97         new_row = first_row;
98         new_col_offset = max(round((x(j)-x(j-1))*scale_factor),1);
99         new_col = last_col + new_col_offset;
100        
101         scaled_feature(new_row, new_col) = orig_f(y(j),x(j));
102        
103         last_col = new_col;
104         last_row = new_row;       
105     end
106
107 end
108
109
110 % check if the nonzero elements are in range
111 [y x] = find(scaled_feature ~= 0);
112
113 max_row = max(y);
114 max_col = max(x);
115
116 if (max_row > wsize)
117     offset = max_row - wize;
118    
119     % shift all the rows up by this amount
120     scaled_feature = scaled_feature(offset+1:end,:);
121 end
122
123 if (max_col > wsize)
124     offset = max_col - wsize;
125    
126     scaled_feature = scaled_feature(:,offset+1:end);
127
128 end
129
130
131
132
133
134
135
136
137