diff options
author | Leo Chan <leochanj@live.unc.edu> | 2020-10-22 01:53:21 -0400 |
---|---|---|
committer | Joshua Bakita <jbakita@cs.unc.edu> | 2020-10-22 01:56:35 -0400 |
commit | d17b33131c14864bd1eae275f49a3f148e21cf29 (patch) | |
tree | 0d8f77922e8d193cb0f6edab83018f057aad64a0 /SD-VBS/common/toolbox/toolbox_basic | |
parent | 601ed25a4c5b66cb75315832c15613a727db2c26 (diff) |
Squashed commit of the sb-vbs branch.
Includes the SD-VBS benchmarks modified to:
- Use libextra to loop as realtime jobs
- Preallocate memory before starting their main computation
- Accept input via stdin instead of via argc
Does not include the SD-VBS matlab code.
Fixes libextra execution in LITMUS^RT.
Diffstat (limited to 'SD-VBS/common/toolbox/toolbox_basic')
525 files changed, 31550 insertions, 0 deletions
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/Distor2Calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/Distor2Calib.m new file mode 100755 index 0000000..a82f583 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/Distor2Calib.m | |||
@@ -0,0 +1,391 @@ | |||
1 | function [fc_2,Rc_2,Tc_2,H_2,distance,V_vert,V_hori,x_all_c,V_hori_pix,V_vert_pix,V_diag1_pix,V_diag2_pix]=Distor2Calib(k_dist,grid_pts_centered,n_sq_x,n_sq_y,Np,W,L,Xgrid_2,f_ini,N_iter,two_focal); | ||
2 | |||
3 | % Computes the calibration parameters knowing the | ||
4 | % distortion factor k_dist | ||
5 | |||
6 | % grid_pts_centered are the grid point coordinates after substraction of | ||
7 | % the optical center. | ||
8 | |||
9 | % can give an optional guess for the focal length f_ini (can set to []) | ||
10 | % can provide the number of iterations for the Iterative Vanishing Point Algorithm | ||
11 | |||
12 | % if the focal length is known perfectly, then, there is no need to iterate, | ||
13 | % and therefore, one can fix: N_iter = 0; | ||
14 | |||
15 | % California Institute of Technology | ||
16 | % (c) Jean-Yves Bouguet - October 7th, 1997 | ||
17 | |||
18 | |||
19 | |||
20 | %keyboard; | ||
21 | |||
22 | if exist('two_focal'), | ||
23 | if isempty(two_focal), | ||
24 | two_focal=0; | ||
25 | end; | ||
26 | else | ||
27 | two_focal = 0; | ||
28 | end; | ||
29 | |||
30 | |||
31 | if exist('N_iter'), | ||
32 | if ~isempty(N_iter), | ||
33 | disp('Use number of iterations provided'); | ||
34 | else | ||
35 | N_iter = 10; | ||
36 | end; | ||
37 | else | ||
38 | N_iter = 10; | ||
39 | end; | ||
40 | |||
41 | if exist('f_ini'), | ||
42 | if ~isempty(f_ini), | ||
43 | disp('Use focal provided'); | ||
44 | if length(f_ini)<2, f_ini=[f_ini;f_ini]; end; | ||
45 | fc_2 = f_ini; | ||
46 | x_all_c = [grid_pts_centered(1,:)/fc_2(1);grid_pts_centered(2,:)/fc_2(2)]; | ||
47 | x_all_c = comp_distortion(x_all_c,k_dist); % we can this time!!! | ||
48 | else | ||
49 | fc_2 = [1;1]; | ||
50 | x_all_c = grid_pts_centered; | ||
51 | end; | ||
52 | else | ||
53 | fc_2 = [1;1]; | ||
54 | x_all_c = grid_pts_centered; | ||
55 | end; | ||
56 | |||
57 | |||
58 | dX = W/n_sq_x; | ||
59 | dY = L/n_sq_y; | ||
60 | |||
61 | |||
62 | N_x = n_sq_x+1; | ||
63 | N_y = n_sq_y+1; | ||
64 | |||
65 | |||
66 | x_grid = zeros(N_x,N_y); | ||
67 | y_grid = zeros(N_x,N_y); | ||
68 | |||
69 | |||
70 | |||
71 | |||
72 | |||
73 | %%% Computation of the four vanishing points in pixels | ||
74 | |||
75 | |||
76 | x_grid(:) = grid_pts_centered(1,:); | ||
77 | y_grid(:) = grid_pts_centered(2,:); | ||
78 | |||
79 | for k=1:n_sq_x+1, | ||
80 | [U,S,V] = svd([x_grid(k,:);y_grid(k,:);ones(1,n_sq_y+1)]); | ||
81 | vert(:,k) = U(:,3); | ||
82 | end; | ||
83 | |||
84 | for k=1:n_sq_y+1, | ||
85 | [U,S,V] = svd([x_grid(:,k)';y_grid(:,k)';ones(1,n_sq_x+1)]); | ||
86 | hori(:,k) = U(:,3); | ||
87 | end; | ||
88 | |||
89 | % 2 principle Vanishing points: | ||
90 | [U,S,V] = svd(vert); | ||
91 | V_vert = U(:,3); | ||
92 | [U,S,V] = svd(hori); | ||
93 | V_hori = U(:,3); | ||
94 | |||
95 | |||
96 | |||
97 | % Square warping: | ||
98 | |||
99 | |||
100 | vert_first = vert(:,1) - dot(V_vert,vert(:,1))/dot(V_vert,V_vert) * V_vert; | ||
101 | vert_last = vert(:,n_sq_x+1) - dot(V_vert,vert(:,n_sq_x+1))/dot(V_vert,V_vert) * V_vert; | ||
102 | |||
103 | hori_first = hori(:,1) - dot(V_hori,hori(:,1))/dot(V_hori,V_hori) * V_hori; | ||
104 | hori_last = hori(:,n_sq_y+1) - dot(V_hori,hori(:,n_sq_y+1))/dot(V_hori,V_hori) * V_hori; | ||
105 | |||
106 | |||
107 | x1 = cross(hori_first,vert_first); | ||
108 | x2 = cross(hori_first,vert_last); | ||
109 | x3 = cross(hori_last,vert_last); | ||
110 | x4 = cross(hori_last,vert_first); | ||
111 | |||
112 | x1 = x1/x1(3); | ||
113 | x2 = x2/x2(3); | ||
114 | x3 = x3/x3(3); | ||
115 | x4 = x4/x4(3); | ||
116 | |||
117 | |||
118 | |||
119 | [square] = Rectangle2Square([x1 x2 x3 x4],W,L); | ||
120 | |||
121 | y1 = square(:,1); | ||
122 | y2 = square(:,2); | ||
123 | y3 = square(:,3); | ||
124 | y4 = square(:,4); | ||
125 | |||
126 | H2 = cross(V_vert,V_hori); | ||
127 | |||
128 | V_diag1 = cross(cross(y1,y3),H2); | ||
129 | V_diag2 = cross(cross(y2,y4),H2); | ||
130 | |||
131 | V_diag1 = V_diag1 / norm(V_diag1); | ||
132 | V_diag2 = V_diag2 / norm(V_diag2); | ||
133 | |||
134 | V_hori_pix = V_hori; | ||
135 | V_vert_pix = V_vert; | ||
136 | V_diag1_pix = V_diag1; | ||
137 | V_diag2_pix = V_diag2; | ||
138 | |||
139 | |||
140 | % end of computation of the vanishing points in pixels. | ||
141 | |||
142 | |||
143 | |||
144 | |||
145 | |||
146 | |||
147 | |||
148 | |||
149 | if two_focal, % only if we attempt to estimate two focals... | ||
150 | % Use diagonal lines also to add two extra vanishing points (?) | ||
151 | N_min = min(N_x,N_y); | ||
152 | |||
153 | if N_min < 2, | ||
154 | use_diag = 0; | ||
155 | two_focal = 0; | ||
156 | disp('Cannot estimate two focals (no existing diagonals)'); | ||
157 | else | ||
158 | use_diag = 1; | ||
159 | Delta_N = abs(N_x-N_y); | ||
160 | N_extra = round((N_min - Delta_N - 1)/2); | ||
161 | diag_list = -N_extra:Delta_N+N_extra; | ||
162 | N_diag = length(diag_list); | ||
163 | diag_1 = zeros(3,N_diag); | ||
164 | diag_2 = zeros(3,N_diag); | ||
165 | end; | ||
166 | else | ||
167 | % Give up the use of the diagonals (so far) | ||
168 | % it seems that the error is increased | ||
169 | use_diag = 0; | ||
170 | end; | ||
171 | |||
172 | |||
173 | |||
174 | % The vertical lines: vert, Horizontal lines: hori | ||
175 | vert = zeros(3,n_sq_x+1); | ||
176 | hori = zeros(3,n_sq_y+1); | ||
177 | |||
178 | for counter_k = 1:N_iter, % the Iterative Vanishing Points Algorithm to | ||
179 | % estimate the focal length accurately | ||
180 | |||
181 | x_grid(:) = x_all_c(1,:); | ||
182 | y_grid(:) = x_all_c(2,:); | ||
183 | |||
184 | for k=1:n_sq_x+1, | ||
185 | [U,S,V] = svd([x_grid(k,:);y_grid(k,:);ones(1,n_sq_y+1)]); | ||
186 | vert(:,k) = U(:,3); | ||
187 | end; | ||
188 | |||
189 | for k=1:n_sq_y+1, | ||
190 | [U,S,V] = svd([x_grid(:,k)';y_grid(:,k)';ones(1,n_sq_x+1)]); | ||
191 | hori(:,k) = U(:,3); | ||
192 | end; | ||
193 | |||
194 | % 2 principle Vanishing points: | ||
195 | [U,S,V] = svd(vert); | ||
196 | V_vert = U(:,3); | ||
197 | [U,S,V] = svd(hori); | ||
198 | V_hori = U(:,3); | ||
199 | |||
200 | |||
201 | |||
202 | % Square warping: | ||
203 | |||
204 | |||
205 | vert_first = vert(:,1) - dot(V_vert,vert(:,1))/dot(V_vert,V_vert) * V_vert; | ||
206 | vert_last = vert(:,n_sq_x+1) - dot(V_vert,vert(:,n_sq_x+1))/dot(V_vert,V_vert) * V_vert; | ||
207 | |||
208 | hori_first = hori(:,1) - dot(V_hori,hori(:,1))/dot(V_hori,V_hori) * V_hori; | ||
209 | hori_last = hori(:,n_sq_y+1) - dot(V_hori,hori(:,n_sq_y+1))/dot(V_hori,V_hori) * V_hori; | ||
210 | |||
211 | |||
212 | x1 = cross(hori_first,vert_first); | ||
213 | x2 = cross(hori_first,vert_last); | ||
214 | x3 = cross(hori_last,vert_last); | ||
215 | x4 = cross(hori_last,vert_first); | ||
216 | |||
217 | x1 = x1/x1(3); | ||
218 | x2 = x2/x2(3); | ||
219 | x3 = x3/x3(3); | ||
220 | x4 = x4/x4(3); | ||
221 | |||
222 | |||
223 | |||
224 | [square] = Rectangle2Square([x1 x2 x3 x4],W,L); | ||
225 | |||
226 | y1 = square(:,1); | ||
227 | y2 = square(:,2); | ||
228 | y3 = square(:,3); | ||
229 | y4 = square(:,4); | ||
230 | |||
231 | H2 = cross(V_vert,V_hori); | ||
232 | |||
233 | V_diag1 = cross(cross(y1,y3),H2); | ||
234 | V_diag2 = cross(cross(y2,y4),H2); | ||
235 | |||
236 | V_diag1 = V_diag1 / norm(V_diag1); | ||
237 | V_diag2 = V_diag2 / norm(V_diag2); | ||
238 | |||
239 | |||
240 | |||
241 | |||
242 | % Estimation of the focal length, and normalization: | ||
243 | |||
244 | % Compute the ellipsis of (1/f^2) positions: | ||
245 | % a * (1/fx)^2 + b * (1/fx)^2 = -c | ||
246 | |||
247 | |||
248 | a1 = V_hori(1); | ||
249 | b1 = V_hori(2); | ||
250 | c1 = V_hori(3); | ||
251 | |||
252 | a2 = V_vert(1); | ||
253 | b2 = V_vert(2); | ||
254 | c2 = V_vert(3); | ||
255 | |||
256 | a3 = V_diag1(1); | ||
257 | b3 = V_diag1(2); | ||
258 | c3 = V_diag1(3); | ||
259 | |||
260 | a4 = V_diag2(1); | ||
261 | b4 = V_diag2(2); | ||
262 | c4 = V_diag2(3); | ||
263 | |||
264 | |||
265 | if two_focal, | ||
266 | |||
267 | |||
268 | A = [a1*a2 b1*b2;a3*a4 b3*b4]; | ||
269 | b = -[c1*c2;c3*c4]; | ||
270 | |||
271 | f = sqrt(abs(1./(inv(A)*b))); | ||
272 | |||
273 | else | ||
274 | |||
275 | f = sqrt(abs(-(c1*c2*(a1*a2 + b1*b2) + c3*c4*(a3*a4 + b3*b4))/(c1^2*c2^2 + c3^2*c4^2))); | ||
276 | |||
277 | f = [f;f]; | ||
278 | |||
279 | end; | ||
280 | |||
281 | |||
282 | |||
283 | % REMARK: | ||
284 | % if both a and b are small, the calibration is impossible. | ||
285 | % if one of them is small, only the other focal length is observable | ||
286 | % if none is small, both focals are observable | ||
287 | |||
288 | |||
289 | fc_2 = fc_2 .* f; | ||
290 | |||
291 | |||
292 | % DEBUG PART: fix focal to 500... | ||
293 | %fc_2= [500;500]; disp('Line 293 to be earased in Distor2Calib.m'); | ||
294 | |||
295 | |||
296 | % end of focal compensation | ||
297 | |||
298 | % normalize by the current focal: | ||
299 | |||
300 | x_all = [grid_pts_centered(1,:)/fc_2(1);grid_pts_centered(2,:)/fc_2(2)]; | ||
301 | |||
302 | % Compensate by the distortion factor: | ||
303 | |||
304 | x_all_c = comp_distortion(x_all,k_dist); | ||
305 | |||
306 | end; | ||
307 | |||
308 | % At that point, we hope that the distortion is gone... | ||
309 | |||
310 | x_grid(:) = x_all_c(1,:); | ||
311 | y_grid(:) = x_all_c(2,:); | ||
312 | |||
313 | for k=1:n_sq_x+1, | ||
314 | [U,S,V] = svd([x_grid(k,:);y_grid(k,:);ones(1,n_sq_y+1)]); | ||
315 | vert(:,k) = U(:,3); | ||
316 | end; | ||
317 | |||
318 | for k=1:n_sq_y+1, | ||
319 | [U,S,V] = svd([x_grid(:,k)';y_grid(:,k)';ones(1,n_sq_x+1)]); | ||
320 | hori(:,k) = U(:,3); | ||
321 | end; | ||
322 | |||
323 | % Vanishing points: | ||
324 | [U,S,V] = svd(vert); | ||
325 | V_vert = U(:,3); | ||
326 | [U,S,V] = svd(hori); | ||
327 | V_hori = U(:,3); | ||
328 | |||
329 | % Horizon: | ||
330 | |||
331 | H_2 = cross(V_vert,V_hori); | ||
332 | |||
333 | % H_2 = cross(V_vert,V_hori); | ||
334 | |||
335 | % pick a plane in front of the camera (positive depth) | ||
336 | if H_2(3) < 0, H_2 = -H_2; end; | ||
337 | |||
338 | |||
339 | % Rotation matrix: | ||
340 | |||
341 | if V_hori(1) < 0, V_hori = -V_hori; end; | ||
342 | |||
343 | V_hori = V_hori/norm(V_hori); | ||
344 | H_2 = H_2/norm(H_2); | ||
345 | |||
346 | V_hori = V_hori - dot(V_hori,H_2)*H_2; | ||
347 | |||
348 | Rc_2 = [V_hori cross(H_2,V_hori) H_2]; | ||
349 | |||
350 | Rc_2 = Rc_2 / det(Rc_2); | ||
351 | |||
352 | %omc_2 = rodrigues(Rc_2); | ||
353 | |||
354 | %Rc_2 = rodrigues(omc_2); | ||
355 | |||
356 | % Find the distance of the plane for translation vector: | ||
357 | |||
358 | xc_2 = [x_all_c;ones(1,Np)]; | ||
359 | |||
360 | Zc_2 = 1./sum(xc_2 .* (Rc_2(:,3)*ones(1,Np))); | ||
361 | |||
362 | Xo_2 = [sum(xc_2 .* (Rc_2(:,1)*ones(1,Np))).*Zc_2 ; sum(xc_2 .* (Rc_2(:,2)*ones(1,Np))).*Zc_2]; | ||
363 | |||
364 | XXo_2 = Xo_2 - mean(Xo_2')'*ones(1,Np); | ||
365 | |||
366 | distance_x = norm(Xgrid_2(1,:))/norm(XXo_2(1,:)); | ||
367 | distance_y = norm(Xgrid_2(2,:))/norm(XXo_2(2,:)); | ||
368 | |||
369 | |||
370 | distance = sum(sum(XXo_2(1:2,:).*Xgrid_2(1:2,:)))/sum(sum(XXo_2(1:2,:).^2)); | ||
371 | |||
372 | alpha = abs(distance_x - distance_y)/distance; | ||
373 | |||
374 | if (alpha>0.1)&~two_focal, | ||
375 | disp('Should use two focals in x and y...'); | ||
376 | end; | ||
377 | |||
378 | % Deduce the translation vector: | ||
379 | |||
380 | Tc_2 = distance * H_2; | ||
381 | |||
382 | |||
383 | |||
384 | |||
385 | |||
386 | return; | ||
387 | |||
388 | V_hori_pix/V_hori_pix(3) | ||
389 | V_vert_pix/V_vert_pix(3) | ||
390 | V_diag1_pix/V_diag1_pix(3) | ||
391 | V_diag2_pix/V_diag2_pix(3) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/Rectangle2Square.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/Rectangle2Square.m new file mode 100755 index 0000000..a6bbbe5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/Rectangle2Square.m | |||
@@ -0,0 +1,19 @@ | |||
1 | function [square] = Rectangle2Square(rectangle,L,W); | ||
2 | |||
3 | % Generate the square from a rectangle of known segment lengths | ||
4 | % from pt1 to pt2 : L | ||
5 | % from pt2 to pt3 : W | ||
6 | |||
7 | [u_hori,u_vert] = UnWarpPlane(rectangle); | ||
8 | |||
9 | coeff_x = sqrt(W/L); | ||
10 | coeff_y = 1/coeff_x; | ||
11 | |||
12 | x_coord = [ 0 coeff_x coeff_x 0]; | ||
13 | y_coord = [ 0 0 coeff_y coeff_y]; | ||
14 | |||
15 | |||
16 | square = rectangle(:,1) * ones(1,4) + u_hori*x_coord + u_vert*y_coord; | ||
17 | square = square ./ (ones(3,1)*square(3,:)); | ||
18 | |||
19 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/UnWarpPlane.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/UnWarpPlane.m new file mode 100755 index 0000000..8addf52 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/UnWarpPlane.m | |||
@@ -0,0 +1,54 @@ | |||
1 | function [u_hori,u_vert] = UnWarpPlane(x1,x2,x3,x4); | ||
2 | |||
3 | % Recovers the two 3D directions of the rectangular patch x1x2x3x4 | ||
4 | % x1 is the origin point, ie any point of planar coordinate (x,y) on the | ||
5 | % rectangular patch will be projected on the image plane at: | ||
6 | % x1 + x * u_hori + y * u_vert | ||
7 | % | ||
8 | % Note: u_hori and u_vert are also the two vanishing points. | ||
9 | |||
10 | |||
11 | if nargin < 4, | ||
12 | |||
13 | x4 = x1(:,4); | ||
14 | x3 = x1(:,3); | ||
15 | x2 = x1(:,2); | ||
16 | x1 = x1(:,1); | ||
17 | |||
18 | end; | ||
19 | |||
20 | |||
21 | % Image Projection: | ||
22 | L1 = cross(x1,x2); | ||
23 | L2 = cross(x4,x3); | ||
24 | L3 = cross(x2,x3); | ||
25 | L4 = cross(x1,x4); | ||
26 | |||
27 | % Vanishing point: | ||
28 | V1 = cross(L1,L2); | ||
29 | V2 = cross(L3,L4); | ||
30 | |||
31 | % Horizon line: | ||
32 | H = cross(V1,V2); | ||
33 | |||
34 | if H(3) < 0, H = -H; end; | ||
35 | |||
36 | |||
37 | H = H / norm(H); | ||
38 | |||
39 | |||
40 | X1 = x1 / dot(H,x1); | ||
41 | X2 = x2 / dot(H,x2); | ||
42 | X3 = x3 / dot(H,x3); | ||
43 | X4 = x4 / dot(H,x4); | ||
44 | |||
45 | scale = X1(3); | ||
46 | |||
47 | X1 = X1/scale; | ||
48 | X2 = X2/scale; | ||
49 | X3 = X3/scale; | ||
50 | X4 = X4/scale; | ||
51 | |||
52 | |||
53 | u_hori = X2 - X1; | ||
54 | u_vert = X4 - X1; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/add_suppress.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/add_suppress.m new file mode 100755 index 0000000..a8a32c0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/add_suppress.m | |||
@@ -0,0 +1,98 @@ | |||
1 | |||
2 | if ~exist('n_ima'), | ||
3 | fprintf(1,'No data to process.\n'); | ||
4 | return; | ||
5 | end; | ||
6 | |||
7 | |||
8 | check_active_images; | ||
9 | |||
10 | |||
11 | fprintf(1,'\nThis function is useful to select a subset of images to calibrate\n'); | ||
12 | |||
13 | fprintf(1,'\nThere are currently %d active images selected for calibration (out of %d):\n',length(ind_active),n_ima); | ||
14 | |||
15 | if ~isempty(ind_active), | ||
16 | |||
17 | for ii = 1:length(ind_active)-2, | ||
18 | |||
19 | fprintf(1,'%d, ',ind_active(ii)); | ||
20 | |||
21 | end; | ||
22 | |||
23 | fprintf(1,'%d and %d.',ind_active(end-1),ind_active(end)); | ||
24 | |||
25 | end; | ||
26 | |||
27 | fprintf(1,'\n'); | ||
28 | |||
29 | |||
30 | fprintf(1,'\nDo you want to suppress or add images from that list?\n'); | ||
31 | |||
32 | choice = 2; | ||
33 | |||
34 | while (choice~=0)&(choice~=1), | ||
35 | choice = input('For suppressing images enter 0, for adding images enter 1 ([]=no change): '); | ||
36 | if isempty(choice), | ||
37 | fprintf(1,'No change applied to the list of active images.\n'); | ||
38 | return; | ||
39 | end; | ||
40 | if (choice~=0)&(choice~=1), | ||
41 | disp('Bad entry. Try again.'); | ||
42 | end; | ||
43 | end; | ||
44 | |||
45 | |||
46 | if choice, | ||
47 | |||
48 | ima_numbers = input('Number(s) of image(s) to add ([] = all images) = '); | ||
49 | |||
50 | if isempty(ima_numbers), | ||
51 | fprintf(1,'All %d images are now active\n',n_ima); | ||
52 | ima_proc = 1:n_ima; | ||
53 | else | ||
54 | ima_proc = ima_numbers; | ||
55 | end; | ||
56 | |||
57 | else | ||
58 | |||
59 | |||
60 | ima_numbers = input('Number(s) of image(s) to suppress ([] = no image) = '); | ||
61 | |||
62 | if isempty(ima_numbers), | ||
63 | fprintf(1,'No image has been suppressed. No modication of the list of active images.\n',n_ima); | ||
64 | ima_proc = []; | ||
65 | else | ||
66 | ima_proc = ima_numbers; | ||
67 | end; | ||
68 | |||
69 | end; | ||
70 | |||
71 | if ~isempty(ima_proc), | ||
72 | |||
73 | active_images(ima_proc) = choice * ones(1,length(ima_proc)); | ||
74 | |||
75 | end; | ||
76 | |||
77 | |||
78 | check_active_images; | ||
79 | |||
80 | |||
81 | fprintf(1,'\nThere is now a total of %d active images for calibration:\n',length(ind_active)); | ||
82 | |||
83 | if ~isempty(ind_active), | ||
84 | |||
85 | for ii = 1:length(ind_active)-2, | ||
86 | |||
87 | fprintf(1,'%d, ',ind_active(ii)); | ||
88 | |||
89 | end; | ||
90 | |||
91 | fprintf(1,'%d and %d.',ind_active(end-1),ind_active(end)); | ||
92 | |||
93 | end; | ||
94 | |||
95 | fprintf(1,'\n\nYou may now run ''Calibration'' to recalibrate based on this new set of images.\n'); | ||
96 | |||
97 | |||
98 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/analyse_error.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/analyse_error.m new file mode 100755 index 0000000..7a9cf0f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/analyse_error.m | |||
@@ -0,0 +1,141 @@ | |||
1 | % Color code for each image: | ||
2 | |||
3 | if ~exist('n_ima')|~exist('fc'), | ||
4 | fprintf(1,'No calibration data available.\n'); | ||
5 | return; | ||
6 | end; | ||
7 | |||
8 | check_active_images; | ||
9 | |||
10 | if ~exist(['ex_' num2str(ind_active(1)) ]), | ||
11 | fprintf(1,'Need to calibrate before analysing reprojection error. Maybe need to load Calib_Results.mat file.\n'); | ||
12 | return; | ||
13 | end; | ||
14 | |||
15 | |||
16 | %if ~exist('no_grid'), | ||
17 | no_grid = 0; | ||
18 | %end; | ||
19 | |||
20 | colors = 'brgkcm'; | ||
21 | |||
22 | |||
23 | figure(5); | ||
24 | |||
25 | for kk = 1:n_ima, | ||
26 | if exist(['y_' num2str(kk)]), | ||
27 | if active_images(kk) & eval(['~isnan(y_' num2str(kk) '(1,1))']), | ||
28 | |||
29 | if ~no_grid, | ||
30 | eval(['XX_kk = X_' num2str(kk) ';']); | ||
31 | N_kk = size(XX_kk,2); | ||
32 | |||
33 | if ~exist(['n_sq_x_' num2str(kk)]), | ||
34 | no_grid = 1; | ||
35 | end; | ||
36 | |||
37 | if ~no_grid, | ||
38 | eval(['n_sq_x = n_sq_x_' num2str(kk) ';']); | ||
39 | eval(['n_sq_y = n_sq_y_' num2str(kk) ';']); | ||
40 | if (N_kk ~= ((n_sq_x+1)*(n_sq_y+1))), | ||
41 | no_grid = 1; | ||
42 | end; | ||
43 | end; | ||
44 | end; | ||
45 | |||
46 | eval(['plot(ex_' num2str(kk) '(1,:),ex_' num2str(kk) '(2,:),''' colors(rem(kk-1,6)+1) '+'');']); | ||
47 | |||
48 | hold on; | ||
49 | end; | ||
50 | end; | ||
51 | end; | ||
52 | |||
53 | hold off; | ||
54 | axis('equal'); | ||
55 | if 1, %~no_grid, | ||
56 | title('Reprojection error (in pixel) - To exit: right button'); | ||
57 | else | ||
58 | title('Reprojection error (in pixel)'); | ||
59 | end; | ||
60 | xlabel('x'); | ||
61 | ylabel('y'); | ||
62 | |||
63 | set(5,'Name','error','NumberTitle','off'); | ||
64 | |||
65 | |||
66 | |||
67 | err_std = std(ex')'; | ||
68 | |||
69 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f] (all active images)\n\n',err_std); | ||
70 | |||
71 | |||
72 | b = 1; | ||
73 | |||
74 | while b==1, | ||
75 | |||
76 | [xp,yp,b] = ginput3(1); | ||
77 | |||
78 | if b==1, | ||
79 | ddd = (ex(1,:)-xp).^2 + (ex(2,:)-yp).^2; | ||
80 | |||
81 | [mind,indmin] = min(ddd); | ||
82 | |||
83 | |||
84 | done = 0; | ||
85 | kk_ima = 1; | ||
86 | while (~done)&(kk_ima<=n_ima), | ||
87 | %fprintf(1,'%d...',kk_ima); | ||
88 | eval(['ex_kk = ex_' num2str(kk_ima) ';']); | ||
89 | sol_kk = find((ex_kk(1,:) == ex(1,indmin))&(ex_kk(2,:) == ex(2,indmin))); | ||
90 | if isempty(sol_kk), | ||
91 | kk_ima = kk_ima + 1; | ||
92 | else | ||
93 | done = 1; | ||
94 | end; | ||
95 | end; | ||
96 | |||
97 | if ~no_grid, | ||
98 | |||
99 | eval(['n_sq_x = n_sq_x_' num2str(kk_ima) ';']); | ||
100 | eval(['n_sq_y = n_sq_y_' num2str(kk_ima) ';']); | ||
101 | |||
102 | Nx = n_sq_x+1; | ||
103 | Ny = n_sq_y+1; | ||
104 | |||
105 | y1 = floor((sol_kk-1)./Nx); | ||
106 | x1 = sol_kk - 1 - Nx*y1; %rem(sol_kk-1,Nx); | ||
107 | |||
108 | y1 = (n_sq_y+1) - y1; | ||
109 | x1 = x1 + 1; | ||
110 | |||
111 | fprintf(1,'\nSelected image: %d\nSelected point: (col,row)=(%d,%d)\nNcol=%d, Nrow=%d\n',[kk_ima x1 y1 Nx Ny]); | ||
112 | fprintf(1,'Pixel error = (%3.5f,%3.5f)\n',[ex(1,indmin) ex(2,indmin)]); | ||
113 | |||
114 | else | ||
115 | |||
116 | eval(['x_kk = x_' num2str(kk_ima) ';']); | ||
117 | |||
118 | xpt = x_kk(:,sol_kk); | ||
119 | |||
120 | fprintf(1,'\nSelected image: %d\nImage coordinates (in pixel): (%3.2f,%3.2f)\n',[kk_ima xpt']); | ||
121 | fprintf(1,'Pixel error = (%3.5f,%3.5f)\n',[ex(1,indmin) ex(2,indmin)]); | ||
122 | |||
123 | |||
124 | end; | ||
125 | |||
126 | |||
127 | if exist(['wintx_' num2str(kk_ima)]), | ||
128 | |||
129 | eval(['wintx = wintx_' num2str(kk_ima) ';']); | ||
130 | eval(['winty = winty_' num2str(kk_ima) ';']); | ||
131 | |||
132 | fprintf(1,'Window size: wintx = %d, winty = %d\n',[wintx winty]); | ||
133 | end; | ||
134 | |||
135 | |||
136 | end; | ||
137 | |||
138 | end; | ||
139 | |||
140 | disp('done'); | ||
141 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/apply_distortion.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/apply_distortion.m new file mode 100755 index 0000000..f5c5b48 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/apply_distortion.m | |||
@@ -0,0 +1,137 @@ | |||
1 | function [xd,dxddk] = apply_distortion(x,k) | ||
2 | |||
3 | |||
4 | [m,n] = size(x); | ||
5 | |||
6 | % Add distortion: | ||
7 | |||
8 | r2 = x(1,:).^2 + x(2,:).^2; | ||
9 | |||
10 | r4 = r2.^2; | ||
11 | |||
12 | % Radial distortion: | ||
13 | |||
14 | cdist = 1 + k(1) * r2 + k(2) * r4; | ||
15 | |||
16 | if nargout > 1, | ||
17 | dcdistdk = [ r2' r4' zeros(n,2)]; | ||
18 | end; | ||
19 | |||
20 | |||
21 | xd1 = x .* (ones(2,1)*cdist); | ||
22 | |||
23 | coeff = (reshape([cdist;cdist],2*n,1)*ones(1,3)); | ||
24 | |||
25 | if nargout > 1, | ||
26 | dxd1dk = zeros(2*n,4); | ||
27 | dxd1dk(1:2:end,:) = (x(1,:)'*ones(1,4)) .* dcdistdk; | ||
28 | dxd1dk(2:2:end,:) = (x(2,:)'*ones(1,4)) .* dcdistdk; | ||
29 | end; | ||
30 | |||
31 | |||
32 | % tangential distortion: | ||
33 | |||
34 | a1 = 2.*x(1,:).*x(2,:); | ||
35 | a2 = r2 + 2*x(1,:).^2; | ||
36 | a3 = r2 + 2*x(2,:).^2; | ||
37 | |||
38 | delta_x = [k(3)*a1 + k(4)*a2 ; | ||
39 | k(3) * a3 + k(4)*a1]; | ||
40 | |||
41 | aa = (2*k(3)*x(2,:)+6*k(4)*x(1,:))'*ones(1,3); | ||
42 | bb = (2*k(3)*x(1,:)+2*k(4)*x(2,:))'*ones(1,3); | ||
43 | cc = (6*k(3)*x(2,:)+2*k(4)*x(1,:))'*ones(1,3); | ||
44 | |||
45 | if nargout > 1, | ||
46 | ddelta_xdk = zeros(2*n,4); | ||
47 | ddelta_xdk(1:2:end,3) = a1'; | ||
48 | ddelta_xdk(1:2:end,4) = a2'; | ||
49 | ddelta_xdk(2:2:end,3) = a3'; | ||
50 | ddelta_xdk(2:2:end,4) = a1'; | ||
51 | end; | ||
52 | |||
53 | xd = xd1 + delta_x; | ||
54 | |||
55 | if nargout > 1, | ||
56 | dxddk = dxd1dk + ddelta_xdk ; | ||
57 | end; | ||
58 | |||
59 | |||
60 | return; | ||
61 | |||
62 | % Test of the Jacobians: | ||
63 | |||
64 | n = 10; | ||
65 | |||
66 | X = 10*randn(3,n); | ||
67 | om = randn(3,1); | ||
68 | T = [10*randn(2,1);40]; | ||
69 | f = 1000*rand(2,1); | ||
70 | c = 1000*randn(2,1); | ||
71 | k = 0.5*randn(4,1); | ||
72 | |||
73 | |||
74 | [x,dxdom,dxdT,dxdf,dxdc,dxdk] = project_points(X,om,T,f,c,k); | ||
75 | |||
76 | |||
77 | % Test on om: NOT OK | ||
78 | |||
79 | dom = 0.000000001 * norm(om)*randn(3,1); | ||
80 | om2 = om + dom; | ||
81 | |||
82 | [x2] = project_points(X,om2,T,f,c,k); | ||
83 | |||
84 | x_pred = x + reshape(dxdom * dom,2,n); | ||
85 | |||
86 | |||
87 | norm(x2-x)/norm(x2 - x_pred) | ||
88 | |||
89 | |||
90 | % Test on T: OK!! | ||
91 | |||
92 | dT = 0.0001 * norm(T)*randn(3,1); | ||
93 | T2 = T + dT; | ||
94 | |||
95 | [x2] = project_points(X,om,T2,f,c,k); | ||
96 | |||
97 | x_pred = x + reshape(dxdT * dT,2,n); | ||
98 | |||
99 | |||
100 | norm(x2-x)/norm(x2 - x_pred) | ||
101 | |||
102 | |||
103 | |||
104 | % Test on f: OK!! | ||
105 | |||
106 | df = 0.001 * norm(f)*randn(2,1); | ||
107 | f2 = f + df; | ||
108 | |||
109 | [x2] = project_points(X,om,T,f2,c,k); | ||
110 | |||
111 | x_pred = x + reshape(dxdf * df,2,n); | ||
112 | |||
113 | |||
114 | norm(x2-x)/norm(x2 - x_pred) | ||
115 | |||
116 | |||
117 | % Test on c: OK!! | ||
118 | |||
119 | dc = 0.01 * norm(c)*randn(2,1); | ||
120 | c2 = c + dc; | ||
121 | |||
122 | [x2] = project_points(X,om,T,f,c2,k); | ||
123 | |||
124 | x_pred = x + reshape(dxdc * dc,2,n); | ||
125 | |||
126 | norm(x2-x)/norm(x2 - x_pred) | ||
127 | |||
128 | % Test on k: OK!! | ||
129 | |||
130 | dk = 0.001 * norm(4)*randn(4,1); | ||
131 | k2 = k + dk; | ||
132 | |||
133 | [x2] = project_points(X,om,T,f,c,k2); | ||
134 | |||
135 | x_pred = x + reshape(dxdk * dk,2,n); | ||
136 | |||
137 | norm(x2-x)/norm(x2 - x_pred) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/calib_gui.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/calib_gui.m new file mode 100755 index 0000000..d591d03 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/calib_gui.m | |||
@@ -0,0 +1,117 @@ | |||
1 | fig_number = 1; | ||
2 | |||
3 | n_row = 4; | ||
4 | n_col = 4; | ||
5 | |||
6 | string_list = cell(n_row,n_col); | ||
7 | callback_list = cell(n_row,n_col); | ||
8 | |||
9 | x_size = 85; | ||
10 | y_size = 14; | ||
11 | gap_x = 0; | ||
12 | font_name = 'clean'; | ||
13 | font_size = 8; | ||
14 | |||
15 | title_figure = 'Camera Calibration Toolbox'; | ||
16 | |||
17 | string_list{1,1} = 'Image names'; | ||
18 | string_list{1,2} = 'Read images'; | ||
19 | string_list{1,3} = 'Extract grid corners'; | ||
20 | string_list{1,4} = 'Calibration'; | ||
21 | string_list{2,1} = 'Show Extrinsic'; | ||
22 | string_list{2,2} = 'Reproject on images'; | ||
23 | string_list{2,3} = 'Analyse error'; | ||
24 | string_list{2,4} = 'Recomp. corners'; | ||
25 | string_list{3,1} = 'Add/Suppress images'; | ||
26 | string_list{3,2} = 'Save'; | ||
27 | string_list{3,3} = 'Load'; | ||
28 | string_list{3,4} = 'Exit'; | ||
29 | |||
30 | string_list{4,1} = 'Comp. Extrinsic'; | ||
31 | string_list{4,2} = 'Undistort image'; | ||
32 | string_list{4,3} = 'Export calib data'; | ||
33 | |||
34 | |||
35 | callback_list{1,1} = 'data_calib;'; | ||
36 | callback_list{1,2} = 'ima_read_calib;'; | ||
37 | callback_list{1,3} = 'click_calib;'; | ||
38 | callback_list{1,4} = 'go_calib_optim;'; | ||
39 | callback_list{2,1} = 'ext_calib;'; | ||
40 | callback_list{2,2} = 'reproject_calib;'; | ||
41 | callback_list{2,3} = 'analyse_error;'; | ||
42 | callback_list{2,4} = 'recomp_corner_calib;'; | ||
43 | callback_list{3,1} = 'add_suppress;'; | ||
44 | callback_list{3,2} = 'saving_calib;'; | ||
45 | callback_list{3,3} = 'loading_calib;'; | ||
46 | callback_list{3,4} = ['disp(''Bye. To run again, type calib_gui.''); close(' num2str(fig_number) ');']; | ||
47 | |||
48 | callback_list{4,1} = 'extrinsic_computation;'; | ||
49 | callback_list{4,2} = 'undistort_image;'; | ||
50 | callback_list{4,3} = 'export_calib_data;'; | ||
51 | |||
52 | |||
53 | %------- BEGIN PROECTED REGION -----------% | ||
54 | %------- DO NOT EDIT IN THIS REGION -----------% | ||
55 | |||
56 | figure(fig_number); clf; | ||
57 | pos = get(fig_number,'Position'); | ||
58 | |||
59 | fig_size_x = x_size*n_col+(n_col+1)*gap_x; | ||
60 | fig_size_y = y_size*n_row+(n_row+1)*gap_x; | ||
61 | |||
62 | set(fig_number,'Units','points', ... | ||
63 | 'BackingStore','off', ... | ||
64 | 'Color',[0.8 0.8 0.8], ... | ||
65 | 'MenuBar','none', ... | ||
66 | 'Resize','off', ... | ||
67 | 'Name',title_figure, ... | ||
68 | 'Position',[pos(1) pos(2) fig_size_x fig_size_y], ... | ||
69 | 'NumberTitle','off'); %,'WindowButtonMotionFcn',['figure(' num2str(fig_number) ');']); | ||
70 | |||
71 | h_mat = zeros(n_row,n_col); | ||
72 | |||
73 | posx = zeros(n_row,n_col); | ||
74 | posy = zeros(n_row,n_col); | ||
75 | |||
76 | for i=n_row:-1:1, | ||
77 | for j = n_col:-1:1, | ||
78 | posx(i,j) = gap_x+(j-1)*(x_size+gap_x); | ||
79 | posy(i,j) = fig_size_y - i*(gap_x+y_size); | ||
80 | end; | ||
81 | end; | ||
82 | |||
83 | for i=n_row:-1:1, | ||
84 | for j = n_col:-1:1, | ||
85 | if ~isempty(string_list{i,j}) & ~isempty(callback_list{i,j}), | ||
86 | h_mat(i,j) = uicontrol('Parent',fig_number, ... | ||
87 | 'Units','points', ... | ||
88 | 'Callback',callback_list{i,j}, ... | ||
89 | 'ListboxTop',0, ... | ||
90 | 'Position',[posx(i,j) posy(i,j) x_size y_size], ... | ||
91 | 'String',string_list{i,j}, ... | ||
92 | 'fontsize',font_size,... | ||
93 | 'fontname',font_name,... | ||
94 | 'Tag','Pushbutton1'); | ||
95 | end; | ||
96 | end; | ||
97 | end; | ||
98 | |||
99 | %------ END PROTECTED REGION ----------------% | ||
100 | |||
101 | if 0, | ||
102 | %-- VERSION: | ||
103 | |||
104 | uicontrol('Parent',fig_number, ... | ||
105 | 'Units','points', ... | ||
106 | 'ListboxTop',0, ... | ||
107 | 'Position',[(fig_size_x-x_size/2)-2 -5 x_size/2 y_size], ... | ||
108 | 'String','ver. 1.0', ... | ||
109 | 'fontsize',8,... | ||
110 | 'BackgroundColor',[0.8 0.8 0.8], ... | ||
111 | 'fontname','clean',... | ||
112 | 'HorizontalAlignment','right', ... | ||
113 | 'Style','text'); | ||
114 | end; | ||
115 | |||
116 | |||
117 | %clear callback_list string_list fig_number fig_size_x fig_size_y i j n_col n_row pos string_list title_figure x_size y_size font_name font_size gap_x h_mat posx posy | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_active_images.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_active_images.m new file mode 100755 index 0000000..fc365a5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_active_images.m | |||
@@ -0,0 +1,19 @@ | |||
1 | |||
2 | if ~exist('active_images'), | ||
3 | active_images = ones(1,n_ima); | ||
4 | end; | ||
5 | n_act = length(active_images); | ||
6 | if n_act < n_ima, | ||
7 | active_images = [active_images ones(1,n_ima-n_act)]; | ||
8 | else | ||
9 | if n_act > n_ima, | ||
10 | active_images = active_images(1:n_ima); | ||
11 | end; | ||
12 | end; | ||
13 | |||
14 | ind_active = find(active_images); | ||
15 | |||
16 | if prod(active_images == 0), | ||
17 | disp('Error: There is no active image'); | ||
18 | break | ||
19 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_convergence.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_convergence.m new file mode 100755 index 0000000..c4b13fd --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_convergence.m | |||
@@ -0,0 +1,48 @@ | |||
1 | %%% Replay the set of solution vectors: | ||
2 | |||
3 | |||
4 | if ~exist('param_list'), | ||
5 | if ~exist('solution'); | ||
6 | fprintf(1,'Error: Need to calibrate first\n'); | ||
7 | return; | ||
8 | else | ||
9 | param_list = solution; | ||
10 | end; | ||
11 | end; | ||
12 | |||
13 | N_iter = size(param_list,2); | ||
14 | |||
15 | if N_iter == 1, | ||
16 | fprintf(1,'Warning: There is a unique state in the list of parameters.\n'); | ||
17 | end; | ||
18 | |||
19 | |||
20 | |||
21 | %M = moviein(N_iter); | ||
22 | |||
23 | for nn = 1:N_iter, | ||
24 | |||
25 | solution = param_list(:,nn); | ||
26 | |||
27 | extract_parameters; | ||
28 | comp_error_calib; | ||
29 | |||
30 | ext_calib; | ||
31 | |||
32 | drawnow; | ||
33 | |||
34 | % Mnn = getframe(gcf); | ||
35 | |||
36 | % M(:,nn) = Mnn; | ||
37 | |||
38 | end; | ||
39 | |||
40 | %fig = gcf; | ||
41 | |||
42 | |||
43 | %figure(fig+1); | ||
44 | %close; | ||
45 | %figure(fig+1); | ||
46 | |||
47 | %clf; | ||
48 | %movie(M,20); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_directory.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_directory.m new file mode 100755 index 0000000..dc23149 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_directory.m | |||
@@ -0,0 +1,97 @@ | |||
1 | % This small script looks in the direcory and checks if the images are there. | ||
2 | % | ||
3 | % This works only on Matlab 5.x (otherwise, the dir commands works differently) | ||
4 | |||
5 | % (c) Jean-Yves Bouguet - Dec. 27th, 1999 | ||
6 | |||
7 | l = dir([calib_name '*']); | ||
8 | |||
9 | Nl = size(l,1); | ||
10 | Nima_valid = 0; | ||
11 | ind_valid = []; | ||
12 | loc_extension = []; | ||
13 | length_name = size(calib_name,2); | ||
14 | |||
15 | if Nl > 0, | ||
16 | |||
17 | for pp = 1:Nl, | ||
18 | filenamepp = l(pp).name; | ||
19 | iii = findstr(filenamepp,calib_name); | ||
20 | |||
21 | loc_ext = findstr(filenamepp,format_image); | ||
22 | string_num = filenamepp(length_name+1:loc_ext - 2); | ||
23 | |||
24 | if isempty(str2num(string_num)), | ||
25 | iii = []; | ||
26 | end; | ||
27 | |||
28 | |||
29 | if ~isempty(iii), | ||
30 | if (iii(1) ~= 1), | ||
31 | iii = []; | ||
32 | end; | ||
33 | end; | ||
34 | |||
35 | |||
36 | |||
37 | if ~isempty(iii) & ~isempty(loc_ext), | ||
38 | |||
39 | Nima_valid = Nima_valid + 1; | ||
40 | ind_valid = [ind_valid pp]; | ||
41 | loc_extension = [loc_extension loc_ext(1)]; | ||
42 | |||
43 | end; | ||
44 | |||
45 | end; | ||
46 | |||
47 | if (Nima_valid==0), | ||
48 | |||
49 | fprintf(1,'No image found. File format may be wrong.\n'); | ||
50 | |||
51 | else | ||
52 | |||
53 | % Get all the string numbers: | ||
54 | |||
55 | string_length = zeros(1,Nima_valid); | ||
56 | indices = zeros(1,Nima_valid); | ||
57 | |||
58 | |||
59 | for ppp = 1:Nima_valid, | ||
60 | |||
61 | name = l(ind_valid(ppp)).name; | ||
62 | string_num = name(length_name+1:loc_extension(ppp) - 2); | ||
63 | string_length(ppp) = size(string_num,2); | ||
64 | indices(ppp) = str2num(string_num); | ||
65 | |||
66 | end; | ||
67 | |||
68 | % Number of images... | ||
69 | first_num = min(indices); | ||
70 | n_ima = max(indices) - first_num + 1; | ||
71 | |||
72 | if min(string_length) == max(string_length), | ||
73 | |||
74 | N_slots = min(string_length); | ||
75 | type_numbering = 1; | ||
76 | |||
77 | else | ||
78 | |||
79 | N_slots = 1; | ||
80 | type_numbering = 0; | ||
81 | |||
82 | end; | ||
83 | |||
84 | image_numbers = first_num:n_ima-1+first_num; | ||
85 | |||
86 | %%% By default, all the images are active for calibration: | ||
87 | |||
88 | active_images = ones(1,n_ima); | ||
89 | |||
90 | end; | ||
91 | |||
92 | else | ||
93 | |||
94 | fprintf(1,'No image found. Basename may be wrong.\n'); | ||
95 | |||
96 | end; | ||
97 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_extracted_images.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_extracted_images.m new file mode 100755 index 0000000..fa7df87 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/check_extracted_images.m | |||
@@ -0,0 +1,37 @@ | |||
1 | check_active_images; | ||
2 | |||
3 | for kk = ind_active, | ||
4 | |||
5 | if ~exist(['x_' num2str(kk)]), | ||
6 | |||
7 | fprintf(1,'WARNING: Need to extract grid corners on image %d\n',kk); | ||
8 | |||
9 | active_images(kk) = 0; | ||
10 | |||
11 | eval(['dX_' num2str(kk) ' = NaN;']); | ||
12 | eval(['dY_' num2str(kk) ' = NaN;']); | ||
13 | |||
14 | eval(['wintx_' num2str(kk) ' = NaN;']); | ||
15 | eval(['winty_' num2str(kk) ' = NaN;']); | ||
16 | |||
17 | eval(['x_' num2str(kk) ' = NaN*ones(2,1);']); | ||
18 | eval(['X_' num2str(kk) ' = NaN*ones(3,1);']); | ||
19 | |||
20 | eval(['n_sq_x_' num2str(kk) ' = NaN;']); | ||
21 | eval(['n_sq_y_' num2str(kk) ' = NaN;']); | ||
22 | |||
23 | else | ||
24 | |||
25 | eval(['xkk = x_' num2str(kk) ';']); | ||
26 | |||
27 | if isnan(xkk(1)), | ||
28 | |||
29 | fprintf(1,'WARNING: Need to extract grid corners on image %d - This image is now set inactive\n',kk); | ||
30 | |||
31 | active_images(kk) = 0; | ||
32 | |||
33 | end; | ||
34 | |||
35 | end; | ||
36 | |||
37 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/clear_windows.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/clear_windows.m new file mode 100755 index 0000000..1eccbd3 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/clear_windows.m | |||
@@ -0,0 +1,4 @@ | |||
1 | for kk = 1:n_ima, | ||
2 | eval(['clear wintx_' num2str(kk)]); | ||
3 | eval(['clear winty_' num2str(kk)]); | ||
4 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/clearwin.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/clearwin.m new file mode 100755 index 0000000..a04be67 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/clearwin.m | |||
@@ -0,0 +1,10 @@ | |||
1 | % Function that clears all the wintx_i and winty_i | ||
2 | % In normal operation of the toolbox, this function should not be | ||
3 | % useful. | ||
4 | % only in cases where you want to re-extract corners using the Extract grid corners another time... not common. You might as well use the Recomp. corners. | ||
5 | |||
6 | for kk = 1:n_ima, | ||
7 | |||
8 | eval(['clear wintx_' num2str(kk) ' winty_' num2str(kk)]); | ||
9 | |||
10 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/click_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/click_calib.m new file mode 100755 index 0000000..1a6d2d7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/click_calib.m | |||
@@ -0,0 +1,193 @@ | |||
1 | %if exist('images_read'); | ||
2 | % active_images = active_images & images_read; | ||
3 | %end; | ||
4 | |||
5 | var2fix = 'dX_default'; | ||
6 | |||
7 | fixvariable; | ||
8 | |||
9 | var2fix = 'dY_default'; | ||
10 | |||
11 | fixvariable; | ||
12 | |||
13 | var2fix = 'map'; | ||
14 | |||
15 | fixvariable; | ||
16 | |||
17 | |||
18 | if ~exist('n_ima'), | ||
19 | data_calib; | ||
20 | end; | ||
21 | |||
22 | check_active_images; | ||
23 | |||
24 | if ~exist(['I_' num2str(ind_active(1))]), | ||
25 | ima_read_calib; | ||
26 | if isempty(ind_read), | ||
27 | disp('Cannot extract corners without images'); | ||
28 | return; | ||
29 | end; | ||
30 | end; | ||
31 | |||
32 | |||
33 | %wintx = 10; % neigborhood of integration for | ||
34 | %winty = 10; % the corner finder | ||
35 | |||
36 | fprintf(1,'\nExtraction of the grid corners on the images\n'); | ||
37 | |||
38 | |||
39 | if ~exist('map'), map = gray(256); end; | ||
40 | |||
41 | |||
42 | disp('WARNING!!! Do not forget to change dX_default and dY_default in click_calib.m!!!') | ||
43 | |||
44 | if ~exist('dX_default'); | ||
45 | |||
46 | % Default size of the pattern squares; | ||
47 | |||
48 | % Setup of JY (old at Caltech) | ||
49 | %dX_default = 21.9250/11; | ||
50 | %dY_default = 18.1250/9; | ||
51 | |||
52 | % Setup of JY (new at Intel) | ||
53 | %dX_default = 1.9750; | ||
54 | %dY_default = 1.9865; | ||
55 | |||
56 | |||
57 | % Setup of Luis and Enrico | ||
58 | %dX_default = 67.7/16; | ||
59 | %dY_default = 50.65/12; | ||
60 | |||
61 | |||
62 | % Setup of German | ||
63 | %dX_default = 10.16; | ||
64 | %dY_default = 10.16; | ||
65 | |||
66 | % Setup of JY (new at Intel) | ||
67 | %dX_default = 1.9750*2.54; | ||
68 | %dY_default = 1.9865*2.54; | ||
69 | |||
70 | % Setup of JY - 3D calibration rig at Intel (new at Intel) | ||
71 | %dX_default = 3; | ||
72 | %dY_default = 3; | ||
73 | |||
74 | % Setup of JY - 3D calibration rig at Intel (new at Intel) - use units in mm to match Zhang | ||
75 | dX_default = 30; | ||
76 | dY_default = 30; | ||
77 | |||
78 | end; | ||
79 | |||
80 | |||
81 | if ~exist('dont_ask'), | ||
82 | dont_ask = 0; | ||
83 | end; | ||
84 | |||
85 | |||
86 | if ~dont_ask, | ||
87 | ima_numbers = input('Number(s) of image(s) to process ([] = all images) = '); | ||
88 | else | ||
89 | ima_numbers = []; | ||
90 | end; | ||
91 | |||
92 | if isempty(ima_numbers), | ||
93 | ima_proc = 1:n_ima; | ||
94 | else | ||
95 | ima_proc = ima_numbers; | ||
96 | end; | ||
97 | |||
98 | |||
99 | % Useful option to add images: | ||
100 | kk_first = ima_proc(1); %input('Start image number ([]=1=first): '); | ||
101 | |||
102 | %if isempty(kk_first), kk_first = 1; end; | ||
103 | |||
104 | |||
105 | if exist(['wintx_' num2str(kk_first)]), | ||
106 | |||
107 | eval(['wintxkk = wintx_' num2str(kk_first) ';']); | ||
108 | |||
109 | if isempty(wintxkk) | isnan(wintxkk), | ||
110 | |||
111 | disp('Window size for corner finder (wintx and winty):'); | ||
112 | wintx = input('wintx ([] = 5) = '); | ||
113 | if isempty(wintx), wintx = 5; end; | ||
114 | wintx = round(wintx); | ||
115 | winty = input('winty ([] = 5) = '); | ||
116 | if isempty(winty), winty = 5; end; | ||
117 | winty = round(winty); | ||
118 | |||
119 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
120 | |||
121 | end; | ||
122 | |||
123 | else | ||
124 | |||
125 | disp('Window size for corner finder (wintx and winty):'); | ||
126 | wintx = input('wintx ([] = 5) = '); | ||
127 | if isempty(wintx), wintx = 5; end; | ||
128 | wintx = round(wintx); | ||
129 | winty = input('winty ([] = 5) = '); | ||
130 | if isempty(winty), winty = 5; end; | ||
131 | winty = round(winty); | ||
132 | |||
133 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
134 | |||
135 | end; | ||
136 | |||
137 | |||
138 | for kk = ima_proc, | ||
139 | if exist(['I_' num2str(kk)]), | ||
140 | click_ima_calib; | ||
141 | active_images(kk) = 1; | ||
142 | else | ||
143 | eval(['dX_' num2str(kk) ' = NaN;']); | ||
144 | eval(['dY_' num2str(kk) ' = NaN;']); | ||
145 | |||
146 | eval(['wintx_' num2str(kk) ' = NaN;']); | ||
147 | eval(['winty_' num2str(kk) ' = NaN;']); | ||
148 | |||
149 | eval(['x_' num2str(kk) ' = NaN*ones(2,1);']); | ||
150 | eval(['X_' num2str(kk) ' = NaN*ones(3,1);']); | ||
151 | |||
152 | eval(['n_sq_x_' num2str(kk) ' = NaN;']); | ||
153 | eval(['n_sq_y_' num2str(kk) ' = NaN;']); | ||
154 | end; | ||
155 | end; | ||
156 | |||
157 | |||
158 | check_active_images; | ||
159 | |||
160 | |||
161 | % Fix potential non-existing variables: | ||
162 | |||
163 | for kk = 1:n_ima, | ||
164 | if ~exist(['x_' num2str(kk)]), | ||
165 | eval(['dX_' num2str(kk) ' = NaN;']); | ||
166 | eval(['dY_' num2str(kk) ' = NaN;']); | ||
167 | |||
168 | eval(['wintx_' num2str(kk) ' = NaN;']); | ||
169 | eval(['winty_' num2str(kk) ' = NaN;']); | ||
170 | |||
171 | eval(['x_' num2str(kk) ' = NaN*ones(2,1);']); | ||
172 | eval(['X_' num2str(kk) ' = NaN*ones(3,1);']); | ||
173 | |||
174 | eval(['n_sq_x_' num2str(kk) ' = NaN;']); | ||
175 | eval(['n_sq_y_' num2str(kk) ' = NaN;']); | ||
176 | end; | ||
177 | end; | ||
178 | |||
179 | |||
180 | string_save = 'save calib_data active_images ind_active wintx winty n_ima type_numbering N_slots first_num image_numbers format_image calib_name Hcal Wcal nx ny map dX_default dY_default dX dY'; | ||
181 | |||
182 | for kk = 1:n_ima, | ||
183 | string_save = [string_save ' X_' num2str(kk) ' x_' num2str(kk) ' n_sq_x_' num2str(kk) ' n_sq_y_' num2str(kk) ' wintx_' num2str(kk) ' winty_' num2str(kk) ' dX_' num2str(kk) ' dY_' num2str(kk)]; | ||
184 | end; | ||
185 | |||
186 | eval(string_save); | ||
187 | |||
188 | disp('done'); | ||
189 | |||
190 | return; | ||
191 | |||
192 | go_calib_optim; | ||
193 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/click_ima_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/click_ima_calib.m new file mode 100755 index 0000000..f0fd4ca --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/click_ima_calib.m | |||
@@ -0,0 +1,230 @@ | |||
1 | % Cleaned-up version of init_calib.m | ||
2 | |||
3 | fprintf(1,'\nProcessing image %d...\n',kk); | ||
4 | |||
5 | eval(['I = I_' num2str(kk) ';']); | ||
6 | |||
7 | if exist(['wintx_' num2str(kk)]), | ||
8 | |||
9 | eval(['wintxkk = wintx_' num2str(kk) ';']); | ||
10 | |||
11 | if ~isempty(wintxkk) & ~isnan(wintxkk), | ||
12 | |||
13 | eval(['wintx = wintx_' num2str(kk) ';']); | ||
14 | eval(['winty = winty_' num2str(kk) ';']); | ||
15 | |||
16 | end; | ||
17 | end; | ||
18 | |||
19 | |||
20 | fprintf(1,'Using (wintx,winty)=(%d,%d) - Window size = %dx%d\n',wintx,winty,2*wintx+1,2*winty+1); | ||
21 | |||
22 | |||
23 | figure(2); | ||
24 | image(I); | ||
25 | colormap(map); | ||
26 | |||
27 | title(['Click on the four extreme corners of the rectangular pattern... Image ' num2str(kk)]); | ||
28 | |||
29 | disp('Click on the four extreme corners of the rectangular complete pattern...'); | ||
30 | |||
31 | [x,y] = ginput3(4); | ||
32 | |||
33 | [Xc,good,bad,type] = cornerfinder([x';y'],I,winty,wintx); % the four corners | ||
34 | |||
35 | x = Xc(1,:)'; | ||
36 | y = Xc(2,:)'; | ||
37 | |||
38 | [y,indy] = sort(y); | ||
39 | x = x(indy); | ||
40 | |||
41 | if (x(2) > x(1)), | ||
42 | x4 = x(1);y4 = y(1); x3 = x(2); y3 = y(2); | ||
43 | else | ||
44 | x4 = x(2);y4 = y(2); x3 = x(1); y3 = y(1); | ||
45 | end; | ||
46 | if (x(3) > x(4)), | ||
47 | x2 = x(3);y2 = y(3); x1 = x(4); y1 = y(4); | ||
48 | else | ||
49 | x2 = x(4);y2 = y(4); x1 = x(3); y1 = y(3); | ||
50 | end; | ||
51 | |||
52 | x = [x1;x2;x3;x4]; | ||
53 | y = [y1;y2;y3;y4]; | ||
54 | |||
55 | |||
56 | figure(2); hold on; | ||
57 | plot([x;x(1)],[y;y(1)],'g-'); | ||
58 | plot(x,y,'og'); | ||
59 | hx=text((x(4)+x(3))/2,(y(4)+y(3))/2 - 20,'X'); | ||
60 | set(hx,'color','g','Fontsize',14); | ||
61 | hy=text((x(4)+x(1))/2-20,(y(4)+y(1))/2,'Y'); | ||
62 | set(hy,'color','g','Fontsize',14); | ||
63 | hold off; | ||
64 | |||
65 | |||
66 | % Try to automatically count the number of squares in the grid | ||
67 | |||
68 | n_sq_x1 = count_squares(I,x1,y1,x2,y2,wintx); | ||
69 | n_sq_x2 = count_squares(I,x3,y3,x4,y4,wintx); | ||
70 | n_sq_y1 = count_squares(I,x2,y2,x3,y3,wintx); | ||
71 | n_sq_y2 = count_squares(I,x4,y4,x1,y1,wintx); | ||
72 | |||
73 | |||
74 | |||
75 | % If could not count the number of squares, enter manually | ||
76 | |||
77 | if (n_sq_x1~=n_sq_x2)|(n_sq_y1~=n_sq_y2), | ||
78 | |||
79 | |||
80 | disp('Could not count the number of squares in the grid. Enter manually.'); | ||
81 | n_sq_x = input('Number of squares along the X direction ([]=10) = '); %6 | ||
82 | if isempty(n_sq_x), n_sq_x = 10; end; | ||
83 | n_sq_y = input('Number of squares along the Y direction ([]=10) = '); %6 | ||
84 | if isempty(n_sq_y), n_sq_y = 10; end; | ||
85 | |||
86 | else | ||
87 | |||
88 | n_sq_x = n_sq_x1; | ||
89 | n_sq_y = n_sq_y1; | ||
90 | |||
91 | end; | ||
92 | |||
93 | |||
94 | % Enter the size of each square | ||
95 | |||
96 | dX = input(['Size dX of each square along the X direction ([]=' num2str(dX_default) 'mm) = ']); | ||
97 | dY = input(['Size dY of each square along the Y direction ([]=' num2str(dY_default) 'mm) = ']); | ||
98 | if isempty(dX), dX = dX_default; else dX_default = dX; end; | ||
99 | if isempty(dY), dY = dY_default; else dY_default = dY; end; | ||
100 | |||
101 | % Compute the inside points through computation of the planar homography (collineation) | ||
102 | |||
103 | a00 = [x(1);y(1);1]; | ||
104 | a10 = [x(2);y(2);1]; | ||
105 | a11 = [x(3);y(3);1]; | ||
106 | a01 = [x(4);y(4);1]; | ||
107 | |||
108 | |||
109 | % Compute the planar collineation: (return the normalization matrix as well) | ||
110 | |||
111 | [Homo,Hnorm,inv_Hnorm] = compute_homography ([a00 a10 a11 a01],[0 1 1 0;0 0 1 1;1 1 1 1]); | ||
112 | |||
113 | |||
114 | % Build the grid using the planar collineation: | ||
115 | |||
116 | x_l = ((0:n_sq_x)'*ones(1,n_sq_y+1))/n_sq_x; | ||
117 | y_l = (ones(n_sq_x+1,1)*(0:n_sq_y))/n_sq_y; | ||
118 | pts = [x_l(:) y_l(:) ones((n_sq_x+1)*(n_sq_y+1),1)]'; | ||
119 | |||
120 | XX = Homo*pts; | ||
121 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
122 | |||
123 | |||
124 | % Complete size of the rectangle | ||
125 | |||
126 | W = n_sq_x*dX; | ||
127 | L = n_sq_y*dY; | ||
128 | |||
129 | |||
130 | |||
131 | |||
132 | %%%%%%%%%%%%%%%%%%%%%%%% ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
133 | figure(2); | ||
134 | hold on; | ||
135 | plot(XX(1,:),XX(2,:),'r+'); | ||
136 | title('The red crosses should be close to the image corners'); | ||
137 | hold off; | ||
138 | |||
139 | disp('If the guessed grid corners (red crosses on the image) are not close to the actual corners,'); | ||
140 | disp('it is necessary to enter an initial guess for the radial distortion factor kc (useful for subpixel detection)'); | ||
141 | quest_distort = input('Need of an initial guess for distortion? ([]=no, other=yes) '); | ||
142 | |||
143 | quest_distort = ~isempty(quest_distort); | ||
144 | |||
145 | if quest_distort, | ||
146 | % Estimation of focal length: | ||
147 | c_g = [size(I,2);size(I,1)]/2 + .5; | ||
148 | f_g = Distor2Calib(0,[[x(1) x(2) x(4) x(3)] - c_g(1);[y(1) y(2) y(4) y(3)] - c_g(2)],1,1,4,W,L,[-W/2 W/2 W/2 -W/2;L/2 L/2 -L/2 -L/2; 0 0 0 0],100,1,1); | ||
149 | f_g = mean(f_g); | ||
150 | script_fit_distortion; | ||
151 | end; | ||
152 | %%%%%%%%%%%%%%%%%%%%% END ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
153 | |||
154 | |||
155 | |||
156 | |||
157 | |||
158 | Np = (n_sq_x+1)*(n_sq_y+1); | ||
159 | |||
160 | disp('Corner extraction...'); | ||
161 | |||
162 | grid_pts = cornerfinder(XX,I,winty,wintx); %%% Finds the exact corners at every points! | ||
163 | |||
164 | |||
165 | |||
166 | %save all_corners x y grid_pts | ||
167 | |||
168 | grid_pts = grid_pts - 1; % subtract 1 to bring the origin to (0,0) instead of (1,1) in matlab (not necessary in C) | ||
169 | |||
170 | |||
171 | |||
172 | ind_corners = [1 n_sq_x+1 (n_sq_x+1)*n_sq_y+1 (n_sq_x+1)*(n_sq_y+1)]; % index of the 4 corners | ||
173 | ind_orig = (n_sq_x+1)*n_sq_y + 1; | ||
174 | xorig = grid_pts(1,ind_orig); | ||
175 | yorig = grid_pts(2,ind_orig); | ||
176 | dxpos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig+1)]'); | ||
177 | dypos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig-n_sq_x-1)]'); | ||
178 | |||
179 | |||
180 | x_box_kk = [grid_pts(1,:)-(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)-(wintx+.5);grid_pts(1,:)-(wintx+.5)]; | ||
181 | y_box_kk = [grid_pts(2,:)-(winty+.5);grid_pts(2,:)-(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)-(winty+.5)]; | ||
182 | |||
183 | |||
184 | figure(3); | ||
185 | image(I); colormap(map); hold on; | ||
186 | plot(grid_pts(1,:)+1,grid_pts(2,:)+1,'r+'); | ||
187 | plot(x_box_kk+1,y_box_kk+1,'-b'); | ||
188 | plot(grid_pts(1,ind_corners)+1,grid_pts(2,ind_corners)+1,'mo'); | ||
189 | plot(xorig+1,yorig+1,'*m'); | ||
190 | h = text(xorig-15,yorig-15,'O'); | ||
191 | set(h,'Color','m','FontSize',14); | ||
192 | h2 = text(dxpos(1)-10,dxpos(2)-10,'dX'); | ||
193 | set(h2,'Color','g','FontSize',14); | ||
194 | h3 = text(dypos(1)-25,dypos(2)-3,'dY'); | ||
195 | set(h3,'Color','g','FontSize',14); | ||
196 | xlabel('Xc (in camera frame)'); | ||
197 | ylabel('Yc (in camera frame)'); | ||
198 | title('Extracted corners'); | ||
199 | zoom on; | ||
200 | drawnow; | ||
201 | hold off; | ||
202 | |||
203 | |||
204 | Xi = reshape(([0:n_sq_x]*dX)'*ones(1,n_sq_y+1),Np,1)'; | ||
205 | Yi = reshape(ones(n_sq_x+1,1)*[n_sq_y:-1:0]*dY,Np,1)'; | ||
206 | Zi = zeros(1,Np); | ||
207 | |||
208 | Xgrid = [Xi;Yi;Zi]; | ||
209 | |||
210 | |||
211 | % All the point coordinates (on the image, and in 3D) - for global optimization: | ||
212 | |||
213 | x = grid_pts; | ||
214 | X = Xgrid; | ||
215 | |||
216 | |||
217 | % Saves all the data into variables: | ||
218 | |||
219 | eval(['dX_' num2str(kk) ' = dX;']); | ||
220 | eval(['dY_' num2str(kk) ' = dY;']); | ||
221 | |||
222 | eval(['wintx_' num2str(kk) ' = wintx;']); | ||
223 | eval(['winty_' num2str(kk) ' = winty;']); | ||
224 | |||
225 | eval(['x_' num2str(kk) ' = x;']); | ||
226 | eval(['X_' num2str(kk) ' = X;']); | ||
227 | |||
228 | eval(['n_sq_x_' num2str(kk) ' = n_sq_x;']); | ||
229 | eval(['n_sq_y_' num2str(kk) ' = n_sq_y;']); | ||
230 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/click_ima_calib3D.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/click_ima_calib3D.m new file mode 100755 index 0000000..7718268 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/click_ima_calib3D.m | |||
@@ -0,0 +1,482 @@ | |||
1 | % Cleaned-up version of init_calib.m | ||
2 | |||
3 | eval(['I = I_' num2str(kk) ';']); | ||
4 | |||
5 | figure(2); | ||
6 | image(I); | ||
7 | colormap(map); | ||
8 | |||
9 | |||
10 | |||
11 | |||
12 | |||
13 | %%%%%%%%%%%%%%%%%%%%%%%%% LEFT PATTERN ACQUISITION %%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
14 | |||
15 | |||
16 | |||
17 | title(['Click on the four extreme corners of the left rectangular pattern... Image ' num2str(kk)]); | ||
18 | |||
19 | disp('Click on the four extreme corners of the left rectangular pattern...'); | ||
20 | |||
21 | [x,y] = ginput3(4); | ||
22 | |||
23 | [Xc,good,bad,type] = cornerfinder([x';y'],I,winty,wintx); % the four corners | ||
24 | |||
25 | x = Xc(1,:)'; | ||
26 | y = Xc(2,:)'; | ||
27 | |||
28 | [y,indy] = sort(y); | ||
29 | x = x(indy); | ||
30 | |||
31 | if (x(2) > x(1)), | ||
32 | x4 = x(1);y4 = y(1); x3 = x(2); y3 = y(2); | ||
33 | else | ||
34 | x4 = x(2);y4 = y(2); x3 = x(1); y3 = y(1); | ||
35 | end; | ||
36 | if (x(3) > x(4)), | ||
37 | x2 = x(3);y2 = y(3); x1 = x(4); y1 = y(4); | ||
38 | else | ||
39 | x2 = x(4);y2 = y(4); x1 = x(3); y1 = y(3); | ||
40 | end; | ||
41 | |||
42 | x = [x1;x2;x3;x4]; | ||
43 | y = [y1;y2;y3;y4]; | ||
44 | |||
45 | |||
46 | figure(2); hold on; | ||
47 | plot([x;x(1)],[y;y(1)],'g-'); | ||
48 | plot(x,y,'og'); | ||
49 | hx=text((x(4)+x(3))/2,(y(4)+y(3))/2 - 20,'X'); | ||
50 | set(hx,'color','g','Fontsize',14); | ||
51 | hy=text((x(4)+x(1))/2-20,(y(4)+y(1))/2,'Y'); | ||
52 | set(hy,'color','g','Fontsize',14); | ||
53 | hold off; | ||
54 | |||
55 | drawnow; | ||
56 | |||
57 | |||
58 | % Try to automatically count the number of squares in the grid | ||
59 | |||
60 | n_sq_x1 = count_squares(I,x1,y1,x2,y2,wintx); | ||
61 | n_sq_x2 = count_squares(I,x3,y3,x4,y4,wintx); | ||
62 | n_sq_y1 = count_squares(I,x2,y2,x3,y3,wintx); | ||
63 | n_sq_y2 = count_squares(I,x4,y4,x1,y1,wintx); | ||
64 | |||
65 | |||
66 | |||
67 | % If could not count the number of squares, enter manually | ||
68 | |||
69 | if (n_sq_x1~=n_sq_x2)|(n_sq_y1~=n_sq_y2), | ||
70 | |||
71 | |||
72 | disp('Could not count the number of squares in the grid. Enter manually.'); | ||
73 | n_sq_x = input('Number of squares along the X direction ([]=10) = '); %6 | ||
74 | if isempty(n_sq_x), n_sq_x = 10; end; | ||
75 | n_sq_y = input('Number of squares along the Y direction ([]=10) = '); %6 | ||
76 | if isempty(n_sq_y), n_sq_y = 10; end; | ||
77 | |||
78 | else | ||
79 | |||
80 | n_sq_x = n_sq_x1; | ||
81 | n_sq_y = n_sq_y1; | ||
82 | |||
83 | end; | ||
84 | |||
85 | |||
86 | if 1, | ||
87 | % Enter the size of each square | ||
88 | |||
89 | dX = input(['Size dX of each square along the X direction ([]=' num2str(dX_default) 'cm) = ']); | ||
90 | dY = input(['Size dY of each square along the Y direction ([]=' num2str(dY_default) 'cm) = ']); | ||
91 | if isempty(dX), dX = dX_default; else dX_default = dX; end; | ||
92 | if isempty(dY), dY = dY_default; else dY_default = dY; end; | ||
93 | |||
94 | else | ||
95 | |||
96 | dX = 3; | ||
97 | dY = 3; | ||
98 | |||
99 | end; | ||
100 | |||
101 | |||
102 | % Compute the inside points through computation of the planar homography (collineation) | ||
103 | |||
104 | a00 = [x(1);y(1);1]; | ||
105 | a10 = [x(2);y(2);1]; | ||
106 | a11 = [x(3);y(3);1]; | ||
107 | a01 = [x(4);y(4);1]; | ||
108 | |||
109 | |||
110 | % Compute the planart collineation: (return the normalization matrice as well) | ||
111 | |||
112 | [Homo,Hnorm,inv_Hnorm] = compute_collineation (a00, a10, a11, a01); | ||
113 | |||
114 | |||
115 | % Build the grid using the planar collineation: | ||
116 | |||
117 | x_l = ((0:n_sq_x)'*ones(1,n_sq_y+1))/n_sq_x; | ||
118 | y_l = (ones(n_sq_x+1,1)*(0:n_sq_y))/n_sq_y; | ||
119 | pts = [x_l(:) y_l(:) ones((n_sq_x+1)*(n_sq_y+1),1)]'; | ||
120 | |||
121 | XX = Homo*pts; | ||
122 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
123 | |||
124 | |||
125 | % Complete size of the rectangle | ||
126 | |||
127 | W = n_sq_x*dX; | ||
128 | L = n_sq_y*dY; | ||
129 | |||
130 | |||
131 | |||
132 | if 1, | ||
133 | %%%%%%%%%%%%%%%%%%%%%%%% ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
134 | figure(2); | ||
135 | hold on; | ||
136 | plot(XX(1,:),XX(2,:),'r+'); | ||
137 | title('The red crosses should be close to the image corners'); | ||
138 | hold off; | ||
139 | |||
140 | disp('If the guessed grid corners (red crosses on the image) are not close to the actual corners,'); | ||
141 | disp('it is necessary to enter an initial guess for the radial distortion factor kc (useful for subpixel detection)'); | ||
142 | quest_distort = input('Need of an initial guess for distortion? ([]=no, other=yes) '); | ||
143 | |||
144 | quest_distort = ~isempty(quest_distort); | ||
145 | |||
146 | if quest_distort, | ||
147 | % Estimation of focal length: | ||
148 | c_g = [size(I,2);size(I,1)]/2 + .5; | ||
149 | f_g = Distor2Calib(0,[[x(1) x(2) x(4) x(3)] - c_g(1);[y(1) y(2) y(4) y(3)] - c_g(2)],1,1,4,W,L,[-W/2 W/2 W/2 -W/2;L/2 L/2 -L/2 -L/2; 0 0 0 0],100,1,1); | ||
150 | f_g = mean(f_g); | ||
151 | script_fit_distortion; | ||
152 | end; | ||
153 | %%%%%%%%%%%%%%%%%%%%% END ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
154 | end; | ||
155 | |||
156 | |||
157 | Np = (n_sq_x+1)*(n_sq_y+1); | ||
158 | |||
159 | disp('Corner extraction...'); | ||
160 | |||
161 | grid_pts = cornerfinder(XX,I,winty,wintx); %%% Finds the exact corners at every points! | ||
162 | |||
163 | %save all_corners x y grid_pts | ||
164 | |||
165 | grid_pts = grid_pts - 1; % subtract 1 to bring the origin to (0,0) instead of (1,1) in matlab (not necessary in C) | ||
166 | |||
167 | |||
168 | % Global Homography from plane to pixel coordinates: | ||
169 | |||
170 | H_total = [1 0 -1 ; 0 1 -1 ; 0 0 1]*Homo*[1 0 0;0 -1 1;0 0 1]*[1/W 0 0 ; 0 1/L 0; 0 0 1]; | ||
171 | % WARNING!!! the first matrix (on the left side) takes care of the transformation of the pixel cooredinates by -1 (previous line) | ||
172 | % If it is not done, then this matrix should not appear (in C) | ||
173 | H_total = H_total / H_total(3,3); | ||
174 | |||
175 | |||
176 | ind_corners = [1 n_sq_x+1 (n_sq_x+1)*n_sq_y+1 (n_sq_x+1)*(n_sq_y+1)]; % index of the 4 corners | ||
177 | ind_orig = (n_sq_x+1)*n_sq_y + 1; | ||
178 | xorig = grid_pts(1,ind_orig); | ||
179 | yorig = grid_pts(2,ind_orig); | ||
180 | dxpos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig+1)]'); | ||
181 | dypos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig-n_sq_x-1)]'); | ||
182 | |||
183 | |||
184 | x_box_kk = [grid_pts(1,:)-(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)-(wintx+.5);grid_pts(1,:)-(wintx+.5)]; | ||
185 | y_box_kk = [grid_pts(2,:)-(winty+.5);grid_pts(2,:)-(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)-(winty+.5)]; | ||
186 | |||
187 | |||
188 | figure(3); | ||
189 | image(I); colormap(map); hold on; | ||
190 | plot(grid_pts(1,:)+1,grid_pts(2,:)+1,'r+'); | ||
191 | plot(x_box_kk+1,y_box_kk+1,'-b'); | ||
192 | plot(grid_pts(1,ind_corners)+1,grid_pts(2,ind_corners)+1,'mo'); | ||
193 | plot(xorig+1,yorig+1,'*m'); | ||
194 | h = text(xorig-15,yorig-15,'O'); | ||
195 | set(h,'Color','m','FontSize',14); | ||
196 | h2 = text(dxpos(1)-10,dxpos(2)-10,'dX'); | ||
197 | set(h2,'Color','g','FontSize',14); | ||
198 | h3 = text(dypos(1)-25,dypos(2)-3,'dY'); | ||
199 | set(h3,'Color','g','FontSize',14); | ||
200 | xlabel('Xc (in camera frame)'); | ||
201 | ylabel('Yc (in camera frame)'); | ||
202 | title('Extracted corners'); | ||
203 | zoom on; | ||
204 | drawnow; | ||
205 | hold off; | ||
206 | |||
207 | |||
208 | Xi = reshape(([0:n_sq_x]*dX)'*ones(1,n_sq_y+1),Np,1)'; | ||
209 | Yi = reshape(ones(n_sq_x+1,1)*[n_sq_y:-1:0]*dY,Np,1)'; | ||
210 | Zi = zeros(1,Np); | ||
211 | |||
212 | Xgrid = [Xi;Yi;Zi]; | ||
213 | |||
214 | |||
215 | % All the point coordinates (on the image, and in 3D) - for global optimization: | ||
216 | |||
217 | x = grid_pts; | ||
218 | X = Xgrid; | ||
219 | |||
220 | |||
221 | % The left pannel info: | ||
222 | |||
223 | xl = x; | ||
224 | Xl = X; | ||
225 | nl_sq_x = n_sq_x; | ||
226 | nl_sq_y = n_sq_y; | ||
227 | Hl = H_total; | ||
228 | |||
229 | |||
230 | |||
231 | |||
232 | |||
233 | |||
234 | %%%%%%%%%%%%%%%%%%%%%%%%% RIGHT PATTERN ACQUISITION %%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
235 | |||
236 | |||
237 | x1 = a10(1)/a10(3); | ||
238 | x4 = a11(1)/a11(3); | ||
239 | |||
240 | y1 = a10(2)/a10(3); | ||
241 | y4 = a11(2)/a11(3); | ||
242 | |||
243 | |||
244 | figure(2); | ||
245 | hold on; | ||
246 | plot([x1 x4],[y1 y4],'c-'); | ||
247 | plot([x1 x4],[y1 y4],'co'); | ||
248 | hold off; | ||
249 | |||
250 | title(['Click on the two remaining extreme corners of the right rectangular pattern... Image ' num2str(kk)]); | ||
251 | |||
252 | disp('Click on the two remaining extreme corners of the right rectangular pattern...'); | ||
253 | |||
254 | [x,y] = ginput3(2); | ||
255 | |||
256 | [Xc,good,bad,type] = cornerfinder([x';y'],I,winty,wintx); % the four corners | ||
257 | |||
258 | x = Xc(1,:)'; | ||
259 | y = Xc(2,:)'; | ||
260 | |||
261 | [y,indy] = sort(y); | ||
262 | x = x(indy); | ||
263 | |||
264 | x2 = x(2); | ||
265 | x3 = x(1); | ||
266 | |||
267 | y2 = y(2); | ||
268 | y3 = y(1); | ||
269 | |||
270 | |||
271 | x = [x1;x2;x3;x4]; | ||
272 | y = [y1;y2;y3;y4]; | ||
273 | |||
274 | figure(2); hold on; | ||
275 | plot([x;x(1)],[y;y(1)],'c-'); | ||
276 | plot(x,y,'oc'); | ||
277 | hx=text((x(4)+x(3))/2,(y(4)+y(3))/2 - 20,'X'); | ||
278 | set(hx,'color','c','Fontsize',14); | ||
279 | hy=text((x(4)+x(1))/2-20,(y(4)+y(1))/2,'Y'); | ||
280 | set(hy,'color','c','Fontsize',14); | ||
281 | hold off; | ||
282 | drawnow; | ||
283 | |||
284 | |||
285 | % Try to automatically count the number of squares in the grid | ||
286 | |||
287 | n_sq_x1 = count_squares(I,x1,y1,x2,y2,wintx); | ||
288 | n_sq_x2 = count_squares(I,x3,y3,x4,y4,wintx); | ||
289 | n_sq_y1 = count_squares(I,x2,y2,x3,y3,wintx); | ||
290 | n_sq_y2 = count_squares(I,x4,y4,x1,y1,wintx); | ||
291 | |||
292 | |||
293 | |||
294 | % If could not count the number of squares, enter manually | ||
295 | |||
296 | if (n_sq_x1~=n_sq_x2)|(n_sq_y1~=n_sq_y2), | ||
297 | |||
298 | |||
299 | disp('Could not count the number of squares in the grid. Enter manually.'); | ||
300 | n_sq_x = input('Number of squares along the X direction ([]=10) = '); %6 | ||
301 | if isempty(n_sq_x), n_sq_x = 10; end; | ||
302 | n_sq_y = input('Number of squares along the Y direction ([]=10) = '); %6 | ||
303 | if isempty(n_sq_y), n_sq_y = 10; end; | ||
304 | |||
305 | else | ||
306 | |||
307 | n_sq_x = n_sq_x1; | ||
308 | n_sq_y = n_sq_y1; | ||
309 | |||
310 | end; | ||
311 | |||
312 | |||
313 | if 1, | ||
314 | % Enter the size of each square | ||
315 | |||
316 | dX = input(['Size dX of each square along the X direction ([]=' num2str(dX_default) 'cm) = ']); | ||
317 | dY = input(['Size dY of each square along the Y direction ([]=' num2str(dY_default) 'cm) = ']); | ||
318 | if isempty(dX), dX = dX_default; else dX_default = dX; end; | ||
319 | if isempty(dY), dY = dY_default; else dY_default = dY; end; | ||
320 | |||
321 | else | ||
322 | |||
323 | dX = 3; | ||
324 | dY = 3; | ||
325 | |||
326 | end; | ||
327 | |||
328 | |||
329 | % Compute the inside points through computation of the planar homography (collineation) | ||
330 | |||
331 | a00 = [x(1);y(1);1]; | ||
332 | a10 = [x(2);y(2);1]; | ||
333 | a11 = [x(3);y(3);1]; | ||
334 | a01 = [x(4);y(4);1]; | ||
335 | |||
336 | |||
337 | % Compute the planart collineation: (return the normalization matrice as well) | ||
338 | |||
339 | [Homo,Hnorm,inv_Hnorm] = compute_collineation (a00, a10, a11, a01); | ||
340 | |||
341 | |||
342 | % Build the grid using the planar collineation: | ||
343 | |||
344 | x_l = ((0:n_sq_x)'*ones(1,n_sq_y+1))/n_sq_x; | ||
345 | y_l = (ones(n_sq_x+1,1)*(0:n_sq_y))/n_sq_y; | ||
346 | pts = [x_l(:) y_l(:) ones((n_sq_x+1)*(n_sq_y+1),1)]'; | ||
347 | |||
348 | XX = Homo*pts; | ||
349 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
350 | |||
351 | |||
352 | % Complete size of the rectangle | ||
353 | |||
354 | W = n_sq_x*dX; | ||
355 | L = n_sq_y*dY; | ||
356 | |||
357 | |||
358 | |||
359 | if 1, | ||
360 | %%%%%%%%%%%%%%%%%%%%%%%% ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
361 | figure(2); | ||
362 | hold on; | ||
363 | plot(XX(1,:),XX(2,:),'r+'); | ||
364 | title('The red crosses should be close to the image corners'); | ||
365 | hold off; | ||
366 | |||
367 | disp('If the guessed grid corners (red crosses on the image) are not close to the actual corners,'); | ||
368 | disp('it is necessary to enter an initial guess for the radial distortion factor kc (useful for subpixel detection)'); | ||
369 | quest_distort = input('Need of an initial guess for distortion? ([]=no, other=yes) '); | ||
370 | |||
371 | quest_distort = ~isempty(quest_distort); | ||
372 | |||
373 | if quest_distort, | ||
374 | % Estimation of focal length: | ||
375 | c_g = [size(I,2);size(I,1)]/2 + .5; | ||
376 | f_g = Distor2Calib(0,[[x(1) x(2) x(4) x(3)] - c_g(1);[y(1) y(2) y(4) y(3)] - c_g(2)],1,1,4,W,L,[-W/2 W/2 W/2 -W/2;L/2 L/2 -L/2 -L/2; 0 0 0 0],100,1,1); | ||
377 | f_g = mean(f_g); | ||
378 | script_fit_distortion; | ||
379 | end; | ||
380 | %%%%%%%%%%%%%%%%%%%%% END ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
381 | end; | ||
382 | |||
383 | |||
384 | Np = (n_sq_x+1)*(n_sq_y+1); | ||
385 | |||
386 | disp('Corner extraction...'); | ||
387 | |||
388 | grid_pts = cornerfinder(XX,I,winty,wintx); %%% Finds the exact corners at every points! | ||
389 | |||
390 | %save all_corners x y grid_pts | ||
391 | |||
392 | grid_pts = grid_pts - 1; % subtract 1 to bring the origin to (0,0) instead of (1,1) in matlab (not necessary in C) | ||
393 | |||
394 | |||
395 | % Global Homography from plane to pixel coordinates: | ||
396 | |||
397 | H_total = [1 0 -1 ; 0 1 -1 ; 0 0 1]*Homo*[1 0 0;0 -1 1;0 0 1]*[1/W 0 0 ; 0 1/L 0; 0 0 1]; | ||
398 | % WARNING!!! the first matrix (on the left side) takes care of the transformation of the pixel cooredinates by -1 (previous line) | ||
399 | % If it is not done, then this matrix should not appear (in C) | ||
400 | H_total = H_total / H_total(3,3); | ||
401 | |||
402 | |||
403 | ind_corners = [1 n_sq_x+1 (n_sq_x+1)*n_sq_y+1 (n_sq_x+1)*(n_sq_y+1)]; % index of the 4 corners | ||
404 | ind_orig = (n_sq_x+1)*n_sq_y + 1; | ||
405 | xorig = grid_pts(1,ind_orig); | ||
406 | yorig = grid_pts(2,ind_orig); | ||
407 | dxpos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig+1)]'); | ||
408 | dypos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig-n_sq_x-1)]'); | ||
409 | |||
410 | |||
411 | x_box_kk = [grid_pts(1,:)-(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)-(wintx+.5);grid_pts(1,:)-(wintx+.5)]; | ||
412 | y_box_kk = [grid_pts(2,:)-(winty+.5);grid_pts(2,:)-(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)-(winty+.5)]; | ||
413 | |||
414 | |||
415 | figure(3); | ||
416 | hold on; | ||
417 | plot(grid_pts(1,:)+1,grid_pts(2,:)+1,'r+'); | ||
418 | plot(x_box_kk+1,y_box_kk+1,'-b'); | ||
419 | plot(grid_pts(1,ind_corners)+1,grid_pts(2,ind_corners)+1,'mo'); | ||
420 | plot(xorig+1,yorig+1,'*m'); | ||
421 | h = text(xorig-15,yorig-15,'O'); | ||
422 | set(h,'Color','m','FontSize',14); | ||
423 | h2 = text(dxpos(1)-10,dxpos(2)-10,'dX'); | ||
424 | set(h2,'Color','g','FontSize',14); | ||
425 | h3 = text(dypos(1)-25,dypos(2)-3,'dY'); | ||
426 | set(h3,'Color','g','FontSize',14); | ||
427 | xlabel('Xc (in camera frame)'); | ||
428 | ylabel('Yc (in camera frame)'); | ||
429 | title('Extracted corners'); | ||
430 | zoom on; | ||
431 | drawnow; | ||
432 | hold off; | ||
433 | |||
434 | |||
435 | Xi = reshape(([0:n_sq_x]*dX)'*ones(1,n_sq_y+1),Np,1)'; | ||
436 | Yi = reshape(ones(n_sq_x+1,1)*[n_sq_y:-1:0]*dY,Np,1)'; | ||
437 | Zi = zeros(1,Np); | ||
438 | |||
439 | Xgrid = [Xi;Yi;Zi]; | ||
440 | |||
441 | |||
442 | % All the point coordinates (on the image, and in 3D) - for global optimization: | ||
443 | |||
444 | x = grid_pts; | ||
445 | X = Xgrid; | ||
446 | |||
447 | |||
448 | % The right pannel info: | ||
449 | |||
450 | xr = x; | ||
451 | Xr = X; | ||
452 | nr_sq_x = n_sq_x; | ||
453 | nr_sq_y = n_sq_y; | ||
454 | Hr = H_total; | ||
455 | |||
456 | |||
457 | |||
458 | %%%%%%%% REGROUP THE LEFT AND RIHT PATTERNS %%%%%%%%%%%%% | ||
459 | |||
460 | |||
461 | Xr2 = [0 0 1;0 1 0;-1 0 0]*Xr + [dX*nl_sq_x;0;0]*ones(1,length(Xr)); | ||
462 | |||
463 | |||
464 | x = [xl xr]; | ||
465 | |||
466 | X = [Xl Xr2]; | ||
467 | |||
468 | |||
469 | |||
470 | eval(['x_' num2str(kk) ' = x;']); | ||
471 | eval(['X_' num2str(kk) ' = X;']); | ||
472 | |||
473 | eval(['nl_sq_x_' num2str(kk) ' = nl_sq_x;']); | ||
474 | eval(['nl_sq_y_' num2str(kk) ' = nl_sq_y;']); | ||
475 | |||
476 | eval(['nr_sq_x_' num2str(kk) ' = nr_sq_x;']); | ||
477 | eval(['nr_sq_y_' num2str(kk) ' = nr_sq_y;']); | ||
478 | |||
479 | % Save the global planar homography: | ||
480 | |||
481 | eval(['Hl_' num2str(kk) ' = Hl;']); | ||
482 | eval(['Hr_' num2str(kk) ' = Hr;']); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_distortion.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_distortion.m new file mode 100755 index 0000000..a0f03de --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_distortion.m | |||
@@ -0,0 +1,38 @@ | |||
1 | function [x_comp] = comp_distortion(x_dist,k2); | ||
2 | |||
3 | % [x_comp] = comp_distortion(x_dist,k2); | ||
4 | % | ||
5 | % compensates the radial distortion of the camera | ||
6 | % on the image plane. | ||
7 | % | ||
8 | % x_dist : the image points got without considering the | ||
9 | % radial distortion. | ||
10 | % x : The image plane points after correction for the distortion | ||
11 | % | ||
12 | % x and x_dist are 2xN arrays | ||
13 | % | ||
14 | % NOTE : This compensation has to be done after the substraction | ||
15 | % of the center of projection, and division by the focal | ||
16 | % length. | ||
17 | % | ||
18 | % (do it up to a second order approximation) | ||
19 | |||
20 | [two,N] = size(x_dist); | ||
21 | |||
22 | if (two ~= 2 ), | ||
23 | error('ERROR : The dimension of the points should be 2xN'); | ||
24 | end; | ||
25 | |||
26 | if length(k2) > 2, | ||
27 | [x_comp] = comp_distortion_oulu(x_dist,k2); | ||
28 | else | ||
29 | |||
30 | radius_2= x_dist(1,:).^2 + x_dist(2,:).^2; | ||
31 | radial_distortion = 1 + ones(2,1)*(k2 * radius_2); | ||
32 | radius_2_comp = (x_dist(1,:).^2 + x_dist(2,:).^2) ./ radial_distortion(1,:); | ||
33 | radial_distortion = 1 + ones(2,1)*(k2 * radius_2_comp); | ||
34 | x_comp = x_dist ./ radial_distortion; | ||
35 | |||
36 | end; | ||
37 | |||
38 | %% Function completely checked : It works fine !!! \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_distortion2.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_distortion2.m new file mode 100755 index 0000000..532ee9a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_distortion2.m | |||
@@ -0,0 +1,71 @@ | |||
1 | function [x_comp] = comp_distortion(x_dist,k2); | ||
2 | |||
3 | % [x_comp] = comp_distortion(x_dist,k2); | ||
4 | % | ||
5 | % compensates the radial distortion of the camera | ||
6 | % on the image plane. | ||
7 | % | ||
8 | % x_dist : the image points got without considering the | ||
9 | % radial distortion. | ||
10 | % k2: Radial distortion factor | ||
11 | % | ||
12 | % x : The image plane points after correction for the distortion | ||
13 | % | ||
14 | % x and x_dist are 2xN arrays | ||
15 | % | ||
16 | % NOTE : This compensation has to be done after the substraction | ||
17 | % of the center of projection, and division by the focal | ||
18 | % length. | ||
19 | % | ||
20 | % Solve for cubic roots using method from Numerical Recipes in C 2nd Ed. | ||
21 | % pages 184-185. | ||
22 | |||
23 | |||
24 | % California Institute of Technology | ||
25 | % (c) Jean-Yves Bouguet - April 27th, 1998 | ||
26 | |||
27 | % fully checked! JYB, april 27th, 1998 - 2am | ||
28 | |||
29 | if k2 ~= 0, | ||
30 | |||
31 | [two,N] = size(x_dist); | ||
32 | |||
33 | if (two ~= 2 ), | ||
34 | error('ERROR : The dimension of the points should be 2xN'); | ||
35 | end; | ||
36 | |||
37 | |||
38 | ph = atan2(x_dist(2,:),x_dist(1,:)); | ||
39 | |||
40 | Q = -1/(3*k2); | ||
41 | R = -x_dist(1,:)./(2*k2*cos(ph)); | ||
42 | |||
43 | R2 = R.^2; | ||
44 | Q3 = Q^3; | ||
45 | |||
46 | |||
47 | if k2 < 0, | ||
48 | |||
49 | % this works in all practical situations (it starts failing for very large | ||
50 | % values of k2) | ||
51 | |||
52 | th = acos(R./sqrt(Q3)); | ||
53 | r = -2*sqrt(Q)*cos((th-2*pi)/3); | ||
54 | |||
55 | else | ||
56 | |||
57 | % note: this always works, even for ridiculous values of k2 | ||
58 | |||
59 | A = (sqrt(R2-Q3)-R).^(1/3); | ||
60 | B = Q*(1./A); | ||
61 | r = (A+B); | ||
62 | |||
63 | end; | ||
64 | |||
65 | x_comp = [r.*cos(ph); r.*sin(ph)]; | ||
66 | |||
67 | else | ||
68 | |||
69 | x_comp = x_dist; | ||
70 | |||
71 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_distortion_oulu.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_distortion_oulu.m new file mode 100755 index 0000000..67d02d5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_distortion_oulu.m | |||
@@ -0,0 +1,47 @@ | |||
1 | function [x] = comp_distortion_oulu(xd,k); | ||
2 | |||
3 | %comp_distortion_oulu.m | ||
4 | % | ||
5 | %[x] = comp_distortion_oulu(xd,k) | ||
6 | % | ||
7 | %Compensates for radial and tangential distortion. Model From Oulu university. | ||
8 | %For more informatino about the distortion model, check the forward projection mapping function: | ||
9 | %project_points.m | ||
10 | % | ||
11 | %INPUT: xd: distorted (normalized) point coordinates in the image plane (2xN matrix) | ||
12 | % k: Distortion coefficients (radial and tangential) (4x1 vector) | ||
13 | % | ||
14 | %OUTPUT: x: undistorted (normalized) point coordinates in the image plane (2xN matrix) | ||
15 | % | ||
16 | %Method: Iterative method for compensation. | ||
17 | % | ||
18 | %NOTE: This compensation has to be done after the subtraction | ||
19 | % of the principal point, and division by the focal length. | ||
20 | |||
21 | |||
22 | if length(k) < 4, | ||
23 | |||
24 | [x] = comp_distortion(xd,k); | ||
25 | |||
26 | else | ||
27 | |||
28 | |||
29 | k1 = k(1); | ||
30 | k2 = k(2); | ||
31 | p1 = k(3); | ||
32 | p2 = k(4); | ||
33 | |||
34 | x = xd; % initial guess | ||
35 | |||
36 | for kk=1:5; | ||
37 | |||
38 | r_2 = sum(x.^2); | ||
39 | k_radial = 1 + k1 * r_2 + k2 * r_2.^2; | ||
40 | delta_x = [2*p1*x(1,:).*x(2,:) + p2*(r_2 + 2*x(1,:).^2) ; | ||
41 | p1 * (r_2 + 2*x(2,:).^2)+2*p2*x(1,:).*x(2,:)]; | ||
42 | x = (xd - delta_x)./(ones(2,1)*k_radial); | ||
43 | |||
44 | end; | ||
45 | |||
46 | end; | ||
47 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_error_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_error_calib.m new file mode 100755 index 0000000..c7bf662 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/comp_error_calib.m | |||
@@ -0,0 +1,46 @@ | |||
1 | %%%%%%%%%%%%%%%%%%%% RECOMPUTES THE REPROJECTION ERROR %%%%%%%%%%%%%%%%%%%%%%%% | ||
2 | |||
3 | check_active_images; | ||
4 | |||
5 | % Reproject the patterns on the images, and compute the pixel errors: | ||
6 | |||
7 | ex = []; % Global error vector | ||
8 | x = []; % Detected corners on the image plane | ||
9 | y = []; % Reprojected points | ||
10 | |||
11 | if ~exist('alpha_c'), | ||
12 | alpha_c = 0; | ||
13 | end; | ||
14 | |||
15 | for kk = 1:n_ima, | ||
16 | |||
17 | eval(['omckk = omc_' num2str(kk) ';']); | ||
18 | eval(['Tckk = Tc_' num2str(kk) ';']); | ||
19 | |||
20 | if active_images(kk) & (~isnan(omckk(1,1))), | ||
21 | |||
22 | %Rkk = rodrigues(omckk); | ||
23 | |||
24 | eval(['y_' num2str(kk) ' = project_points2(X_' num2str(kk) ',omckk,Tckk,fc,cc,kc,alpha_c);']); | ||
25 | |||
26 | eval(['ex_' num2str(kk) ' = x_' num2str(kk) ' -y_' num2str(kk) ';']); | ||
27 | |||
28 | eval(['x_kk = x_' num2str(kk) ';']); | ||
29 | |||
30 | eval(['ex = [ex ex_' num2str(kk) '];']); | ||
31 | eval(['x = [x x_' num2str(kk) '];']); | ||
32 | eval(['y = [y y_' num2str(kk) '];']); | ||
33 | |||
34 | else | ||
35 | |||
36 | % eval(['y_' num2str(kk) ' = NaN*ones(2,1);']); | ||
37 | |||
38 | |||
39 | % If inactivated image, the error does not make sense: | ||
40 | eval(['ex_' num2str(kk) ' = NaN*ones(2,1);']); | ||
41 | |||
42 | end; | ||
43 | |||
44 | end; | ||
45 | |||
46 | err_std = std(ex')'; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_collineation.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_collineation.m new file mode 100755 index 0000000..809c309 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_collineation.m | |||
@@ -0,0 +1,66 @@ | |||
1 | function [H,Hnorm,inv_Hnorm] = compute_collineation (a00, a10, a11, a01); | ||
2 | |||
3 | % new formalism using homographies | ||
4 | |||
5 | a00 = a00 / a00(3); | ||
6 | a10 = a10 / a10(3); | ||
7 | a11 = a11 / a11(3); | ||
8 | a01 = a01 / a01(3); | ||
9 | |||
10 | |||
11 | % Prenormalization of point coordinates (very important): | ||
12 | % (Affine normalization) | ||
13 | |||
14 | ax = [a00(1);a10(1);a11(1);a01(1)]; | ||
15 | ay = [a00(2);a10(2);a11(2);a01(2)]; | ||
16 | |||
17 | mxx = mean(ax); | ||
18 | myy = mean(ay); | ||
19 | ax = ax - mxx; | ||
20 | ay = ay - myy; | ||
21 | |||
22 | scxx = mean(abs(ax)); | ||
23 | scyy = mean(abs(ay)); | ||
24 | |||
25 | |||
26 | Hnorm = [1/scxx 0 -mxx/scxx;0 1/scyy -myy/scyy;0 0 1]; | ||
27 | inv_Hnorm = [scxx 0 mxx ; 0 scyy myy; 0 0 1]; | ||
28 | |||
29 | |||
30 | a00n = Hnorm*a00; | ||
31 | a10n = Hnorm*a10; | ||
32 | a11n = Hnorm*a11; | ||
33 | a01n = Hnorm*a01; | ||
34 | |||
35 | |||
36 | % Computation of the vanishing points: | ||
37 | |||
38 | V1n = cross(cross(a00n,a10n),cross(a01n,a11n)); | ||
39 | V2n = cross(cross(a00n,a01n),cross(a10n,a11n)); | ||
40 | |||
41 | V1 = inv_Hnorm*V1n; | ||
42 | V2 = inv_Hnorm*V2n; | ||
43 | |||
44 | |||
45 | % Normalizaion of the vanishing points: | ||
46 | |||
47 | V1n = V1n/norm(V1n); | ||
48 | V2n = V2n/norm(V2n); | ||
49 | |||
50 | |||
51 | % Closed-form solution of the coefficients: | ||
52 | |||
53 | alpha_x = (a10n(2)*a00n(1) - a10n(1)*a00n(2))/(V1n(2)*a10n(1)-V1n(1)*a10n(2)); | ||
54 | |||
55 | alpha_y = (a01n(2)*a00n(1) - a01n(1)*a00n(2))/(V2n(2)*a01n(1)-V2n(1)*a01n(2)); | ||
56 | |||
57 | |||
58 | % Remaining Homography | ||
59 | |||
60 | Hrem = [alpha_x*V1n alpha_y*V2n a00n]; | ||
61 | |||
62 | |||
63 | % Final homography: | ||
64 | |||
65 | H = inv_Hnorm*Hrem; | ||
66 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_extrinsic.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_extrinsic.m new file mode 100755 index 0000000..5217351 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_extrinsic.m | |||
@@ -0,0 +1,123 @@ | |||
1 | function [omckk,Tckk,Rckk,H,x,ex,JJ] = compute_extrinsic(x_kk,X_kk,fc,cc,kc,alpha_c,MaxIter,thresh_cond), | ||
2 | |||
3 | %compute_extrinsic | ||
4 | % | ||
5 | %[omckk,Tckk,Rckk,H,x,ex] = compute_extrinsic(x_kk,X_kk,fc,cc,kc,alpha_c) | ||
6 | % | ||
7 | %Computes the extrinsic parameters attached to a 3D structure X_kk given its projection | ||
8 | %on the image plane x_kk and the intrinsic camera parameters fc, cc and kc. | ||
9 | %Works with planar and non-planar structures. | ||
10 | % | ||
11 | %INPUT: x_kk: Feature locations on the images | ||
12 | % X_kk: Corresponding grid coordinates | ||
13 | % fc: Camera focal length | ||
14 | % cc: Principal point coordinates | ||
15 | % kc: Distortion coefficients | ||
16 | % alpha_c: Skew coefficient | ||
17 | % | ||
18 | %OUTPUT: omckk: 3D rotation vector attached to the grid positions in space | ||
19 | % Tckk: 3D translation vector attached to the grid positions in space | ||
20 | % Rckk: 3D rotation matrices corresponding to the omc vectors | ||
21 | % H: Homography between points on the grid and points on the image plane (in pixel) | ||
22 | % This makes sense only if the planar that is used in planar. | ||
23 | % x: Reprojections of the points on the image plane | ||
24 | % ex: Reprojection error: ex = x_kk - x; | ||
25 | % | ||
26 | %Method: Computes the normalized point coordinates, then computes the 3D pose | ||
27 | % | ||
28 | %Important functions called within that program: | ||
29 | % | ||
30 | %normalize: Computes the normalize image point coordinates. | ||
31 | % | ||
32 | %pose3D: Computes the 3D pose of the structure given the normalized image projection. | ||
33 | % | ||
34 | %project_points.m: Computes the 2D image projections of a set of 3D points | ||
35 | |||
36 | |||
37 | |||
38 | if nargin < 8, | ||
39 | thresh_cond = inf; | ||
40 | end; | ||
41 | |||
42 | |||
43 | if nargin < 7, | ||
44 | MaxIter = 20; | ||
45 | end; | ||
46 | |||
47 | |||
48 | if nargin < 6, | ||
49 | alpha_c = 0; | ||
50 | if nargin < 5, | ||
51 | kc = zeros(4,1); | ||
52 | if nargin < 4, | ||
53 | cc = zeros(2,1); | ||
54 | if nargin < 3, | ||
55 | fc = ones(2,1); | ||
56 | if nargin < 2, | ||
57 | error('Need 2D projections and 3D points (in compute_extrinsic.m)'); | ||
58 | return; | ||
59 | end; | ||
60 | end; | ||
61 | end; | ||
62 | end; | ||
63 | end; | ||
64 | |||
65 | % Initialization: | ||
66 | |||
67 | [omckk,Tckk,Rckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc,alpha_c); | ||
68 | |||
69 | % Refinement: | ||
70 | |||
71 | [omckk,Tckk,Rckk,JJ] = compute_extrinsic_refine(omckk,Tckk,x_kk,X_kk,fc,cc,kc,alpha_c,MaxIter,thresh_cond); | ||
72 | |||
73 | |||
74 | % computation of the homography (not useful in the end) | ||
75 | |||
76 | H = [Rckk(:,1:2) Tckk]; | ||
77 | |||
78 | % Computes the reprojection error in pixels: | ||
79 | |||
80 | x = project_points2(X_kk,omckk,Tckk,fc,cc,kc,alpha_c); | ||
81 | |||
82 | ex = x_kk - x; | ||
83 | |||
84 | |||
85 | % Converts the homography in pixel units: | ||
86 | |||
87 | KK = [fc(1) alpha_c*fc(1) cc(1);0 fc(2) cc(2); 0 0 1]; | ||
88 | |||
89 | H = KK*H; | ||
90 | |||
91 | |||
92 | |||
93 | |||
94 | return; | ||
95 | |||
96 | |||
97 | % Test of compte extrinsic: | ||
98 | |||
99 | Np = 4; | ||
100 | sx = 10; | ||
101 | sy = 10; | ||
102 | sz = 5; | ||
103 | |||
104 | om = randn(3,1); | ||
105 | T = [0;0;100]; | ||
106 | |||
107 | noise = 2/1000; | ||
108 | |||
109 | XX = [sx*randn(1,Np);sy*randn(1,Np);sz*randn(1,Np)]; | ||
110 | xx = project_points(XX,om,T); | ||
111 | |||
112 | xxn = xx + noise * randn(2,Np); | ||
113 | |||
114 | [omckk,Tckk] = compute_extrinsic(xxn,XX); | ||
115 | |||
116 | [om omckk om-omckk] | ||
117 | [T Tckk T-Tckk] | ||
118 | |||
119 | figure(3); | ||
120 | plot(xx(1,:),xx(2,:),'r+'); | ||
121 | hold on; | ||
122 | plot(xxn(1,:),xxn(2,:),'g+'); | ||
123 | hold off; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_extrinsic_init.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_extrinsic_init.m new file mode 100755 index 0000000..2e6d821 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_extrinsic_init.m | |||
@@ -0,0 +1,151 @@ | |||
1 | function [omckk,Tckk,Rckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc,alpha_c), | ||
2 | |||
3 | %compute_extrinsic | ||
4 | % | ||
5 | %[omckk,Tckk,Rckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc,alpha_c) | ||
6 | % | ||
7 | %Computes the extrinsic parameters attached to a 3D structure X_kk given its projection | ||
8 | %on the image plane x_kk and the intrinsic camera parameters fc, cc and kc. | ||
9 | %Works with planar and non-planar structures. | ||
10 | % | ||
11 | %INPUT: x_kk: Feature locations on the images | ||
12 | % X_kk: Corresponding grid coordinates | ||
13 | % fc: Camera focal length | ||
14 | % cc: Principal point coordinates | ||
15 | % kc: Distortion coefficients | ||
16 | % alpha_c: Skew coefficient | ||
17 | % | ||
18 | %OUTPUT: omckk: 3D rotation vector attached to the grid positions in space | ||
19 | % Tckk: 3D translation vector attached to the grid positions in space | ||
20 | % Rckk: 3D rotation matrices corresponding to the omc vectors | ||
21 | % | ||
22 | %Method: Computes the normalized point coordinates, then computes the 3D pose | ||
23 | % | ||
24 | %Important functions called within that program: | ||
25 | % | ||
26 | %normalize: Computes the normalize image point coordinates. | ||
27 | % | ||
28 | %pose3D: Computes the 3D pose of the structure given the normalized image projection. | ||
29 | % | ||
30 | %project_points.m: Computes the 2D image projections of a set of 3D points | ||
31 | |||
32 | |||
33 | |||
34 | if nargin < 6, | ||
35 | alpha_c = 0; | ||
36 | if nargin < 5, | ||
37 | kc = zeros(4,1); | ||
38 | if nargin < 4, | ||
39 | cc = zeros(2,1); | ||
40 | if nargin < 3, | ||
41 | fc = ones(2,1); | ||
42 | if nargin < 2, | ||
43 | error('Need 2D projections and 3D points (in compute_extrinsic.m)'); | ||
44 | return; | ||
45 | end; | ||
46 | end; | ||
47 | end; | ||
48 | end; | ||
49 | end; | ||
50 | |||
51 | |||
52 | % Compute the normalized coordinates: | ||
53 | |||
54 | xn = normalize(x_kk,fc,cc,kc,alpha_c); | ||
55 | |||
56 | |||
57 | |||
58 | Np = size(xn,2); | ||
59 | |||
60 | %% Check for planarity of the structure: | ||
61 | |||
62 | X_mean = mean(X_kk')'; | ||
63 | |||
64 | Y = X_kk - (X_mean*ones(1,Np)); | ||
65 | |||
66 | YY = Y*Y'; | ||
67 | |||
68 | [U,S,V] = svd(YY); | ||
69 | |||
70 | r = S(3,3)/S(2,2); | ||
71 | |||
72 | if (r < 1e-3)|(Np < 6), %1e-3, %1e-4, %norm(X_kk(3,:)) < eps, % Test of planarity | ||
73 | |||
74 | %fprintf(1,'Planar structure detected: r=%f\n',r); | ||
75 | |||
76 | % Transform the plane to bring it in the Z=0 plane: | ||
77 | |||
78 | R_transform = V'; | ||
79 | |||
80 | if det(R_transform) < 0, R_transform = -R_transform; end; | ||
81 | |||
82 | T_transform = -(R_transform)*X_mean; | ||
83 | |||
84 | X_new = R_transform*X_kk + T_transform*ones(1,Np); | ||
85 | |||
86 | |||
87 | % Compute the planar homography: | ||
88 | |||
89 | H = compute_homography (xn,X_new(1:2,:)); | ||
90 | |||
91 | % De-embed the motion parameters from the homography: | ||
92 | |||
93 | sc = mean([norm(H(:,1));norm(H(:,2))]); | ||
94 | |||
95 | H = H/sc; | ||
96 | |||
97 | omckk = rodrigues([H(:,1:2) cross(H(:,1),H(:,2))]); | ||
98 | Rckk = rodrigues(omckk); | ||
99 | Tckk = H(:,3); | ||
100 | |||
101 | %If Xc = Rckk * X_new + Tckk, then Xc = Rckk * R_transform * X_kk + Tckk + T_transform | ||
102 | |||
103 | Tckk = Tckk + Rckk* T_transform; | ||
104 | Rckk = Rckk * R_transform; | ||
105 | |||
106 | omckk = rodrigues(Rckk); | ||
107 | Rckk = rodrigues(omckk); | ||
108 | |||
109 | |||
110 | else | ||
111 | |||
112 | %fprintf(1,'Non planar structure detected: r=%f\n',r); | ||
113 | |||
114 | % Computes an initial guess for extrinsic parameters (works for general 3d structure, not planar!!!): | ||
115 | % The DLT method is applied here!! | ||
116 | |||
117 | J = zeros(2*Np,12); | ||
118 | |||
119 | xX = (ones(3,1)*xn(1,:)).*X_kk; | ||
120 | yX = (ones(3,1)*xn(2,:)).*X_kk; | ||
121 | |||
122 | J(1:2:end,[1 4 7]) = -X_kk'; | ||
123 | J(2:2:end,[2 5 8]) = X_kk'; | ||
124 | J(1:2:end,[3 6 9]) = xX'; | ||
125 | J(2:2:end,[3 6 9]) = -yX'; | ||
126 | J(1:2:end,12) = xn(1,:)'; | ||
127 | J(2:2:end,12) = -xn(2,:)'; | ||
128 | J(1:2:end,10) = -ones(Np,1); | ||
129 | J(2:2:end,11) = ones(Np,1); | ||
130 | |||
131 | JJ = J'*J; | ||
132 | [U,S,V] = svd(JJ); | ||
133 | |||
134 | RR = reshape(V(1:9,12),3,3); | ||
135 | |||
136 | if det(RR) < 0, | ||
137 | V(:,12) = -V(:,12); | ||
138 | RR = -RR; | ||
139 | end; | ||
140 | |||
141 | [Ur,Sr,Vr] = svd(RR); | ||
142 | |||
143 | Rckk = Ur*Vr'; | ||
144 | |||
145 | sc = norm(V(1:9,12)) / norm(Rckk(:)); | ||
146 | Tckk = V(10:12,12)/sc; | ||
147 | |||
148 | omckk = rodrigues(Rckk); | ||
149 | Rckk = rodrigues(omckk); | ||
150 | |||
151 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_extrinsic_refine.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_extrinsic_refine.m new file mode 100755 index 0000000..a4d066c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_extrinsic_refine.m | |||
@@ -0,0 +1,113 @@ | |||
1 | function [omckk,Tckk,Rckk,JJ] = compute_extrinsic_refine(omc_init,Tc_init,x_kk,X_kk,fc,cc,kc,alpha_c,MaxIter,thresh_cond), | ||
2 | |||
3 | %compute_extrinsic | ||
4 | % | ||
5 | %[omckk,Tckk,Rckk] = compute_extrinsic_refine(x_kk,X_kk,fc,cc,kc,alpha_c,MaxIter) | ||
6 | % | ||
7 | %Computes the extrinsic parameters attached to a 3D structure X_kk given its projection | ||
8 | %on the image plane x_kk and the intrinsic camera parameters fc, cc and kc. | ||
9 | %Works with planar and non-planar structures. | ||
10 | % | ||
11 | %INPUT: x_kk: Feature locations on the images | ||
12 | % X_kk: Corresponding grid coordinates | ||
13 | % fc: Camera focal length | ||
14 | % cc: Principal point coordinates | ||
15 | % kc: Distortion coefficients | ||
16 | % alpha_c: Skew coefficient | ||
17 | % MaxIter: Maximum number of iterations | ||
18 | % | ||
19 | %OUTPUT: omckk: 3D rotation vector attached to the grid positions in space | ||
20 | % Tckk: 3D translation vector attached to the grid positions in space | ||
21 | % Rckk: 3D rotation matrices corresponding to the omc vectors | ||
22 | |||
23 | % | ||
24 | %Method: Computes the normalized point coordinates, then computes the 3D pose | ||
25 | % | ||
26 | %Important functions called within that program: | ||
27 | % | ||
28 | %normalize: Computes the normalize image point coordinates. | ||
29 | % | ||
30 | %pose3D: Computes the 3D pose of the structure given the normalized image projection. | ||
31 | % | ||
32 | %project_points.m: Computes the 2D image projections of a set of 3D points | ||
33 | |||
34 | |||
35 | if nargin < 10, | ||
36 | thresh_cond = inf; | ||
37 | end; | ||
38 | |||
39 | |||
40 | if nargin < 9, | ||
41 | MaxIter = 20; | ||
42 | end; | ||
43 | |||
44 | if nargin < 8, | ||
45 | alpha_c = 0; | ||
46 | if nargin < 7, | ||
47 | kc = zeros(4,1); | ||
48 | if nargin < 6, | ||
49 | cc = zeros(2,1); | ||
50 | if nargin < 5, | ||
51 | fc = ones(2,1); | ||
52 | if nargin < 4, | ||
53 | error('Need 2D projections and 3D points (in compute_extrinsic_refine.m)'); | ||
54 | return; | ||
55 | end; | ||
56 | end; | ||
57 | end; | ||
58 | end; | ||
59 | end; | ||
60 | |||
61 | |||
62 | % Initialization: | ||
63 | |||
64 | omckk = omc_init; | ||
65 | Tckk = Tc_init; | ||
66 | |||
67 | |||
68 | % Final optimization (minimize the reprojection error in pixel): | ||
69 | % through Gradient Descent: | ||
70 | |||
71 | param = [omckk;Tckk]; | ||
72 | |||
73 | change = 1; | ||
74 | |||
75 | iter = 0; | ||
76 | |||
77 | %keyboard; | ||
78 | |||
79 | %fprintf(1,'Gradient descent iterations: '); | ||
80 | |||
81 | while (change > 1e-10)&(iter < MaxIter), | ||
82 | |||
83 | %fprintf(1,'%d...',iter+1); | ||
84 | |||
85 | [x,dxdom,dxdT] = project_points2(X_kk,omckk,Tckk,fc,cc,kc,alpha_c); | ||
86 | |||
87 | ex = x_kk - x; | ||
88 | |||
89 | %keyboard; | ||
90 | |||
91 | JJ = [dxdom dxdT]; | ||
92 | |||
93 | if cond(JJ) > thresh_cond, | ||
94 | change = 0; | ||
95 | else | ||
96 | |||
97 | JJ2 = JJ'*JJ; | ||
98 | |||
99 | param_innov = inv(JJ2)*(JJ')*ex(:); | ||
100 | param_up = param + param_innov; | ||
101 | change = norm(param_innov)/norm(param_up); | ||
102 | param = param_up; | ||
103 | iter = iter + 1; | ||
104 | |||
105 | omckk = param(1:3); | ||
106 | Tckk = param(4:6); | ||
107 | end; | ||
108 | |||
109 | end; | ||
110 | |||
111 | %fprintf(1,'\n'); | ||
112 | |||
113 | Rckk = rodrigues(omckk); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_homography.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_homography.m new file mode 100755 index 0000000..fcc9003 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/compute_homography.m | |||
@@ -0,0 +1,163 @@ | |||
1 | function [H,Hnorm,inv_Hnorm] = compute_homography (m,M); | ||
2 | |||
3 | %compute_homography | ||
4 | % | ||
5 | %[H,Hnorm,inv_Hnorm] = compute_homography (m,M) | ||
6 | % | ||
7 | %Computes the planar homography between the point coordinates on the plane (M) and the image | ||
8 | %point coordinates (m). | ||
9 | % | ||
10 | %INPUT: m: homogeneous coordinates in the image plane (3xN matrix) | ||
11 | % M: homogeneous coordinates in the plane in 3D (3xN matrix) | ||
12 | % | ||
13 | %OUTPUT: H: Homography matrix (3x3 homogeneous matrix) | ||
14 | % Hnorm: Normlization matrix used on the points before homography computation | ||
15 | % (useful for numerical stability is points in pixel coordinates) | ||
16 | % inv_Hnorm: The inverse of Hnorm | ||
17 | % | ||
18 | %Definition: m ~ H*M where "~" means equal up to a non zero scalar factor. | ||
19 | % | ||
20 | %Method: First computes an initial guess for the homography through quasi-linear method. | ||
21 | % Then, if the total number of points is larger than 4, optimize the solution by minimizing | ||
22 | % the reprojection error (in the least squares sense). | ||
23 | % | ||
24 | % | ||
25 | %Important functions called within that program: | ||
26 | % | ||
27 | %comp_distortion_oulu: Undistorts pixel coordinates. | ||
28 | % | ||
29 | %compute_homography.m: Computes the planar homography between points on the grid in 3D, and the image plane. | ||
30 | % | ||
31 | %project_points.m: Computes the 2D image projections of a set of 3D points, and also returns te Jacobian | ||
32 | % matrix (derivative with respect to the intrinsic and extrinsic parameters). | ||
33 | % This function is called within the minimization loop. | ||
34 | |||
35 | |||
36 | |||
37 | |||
38 | Np = size(m,2); | ||
39 | |||
40 | if size(m,1)<3, | ||
41 | m = [m;ones(1,Np)]; | ||
42 | end; | ||
43 | |||
44 | if size(M,1)<3, | ||
45 | M = [M;ones(1,Np)]; | ||
46 | end; | ||
47 | |||
48 | |||
49 | m = m ./ (ones(3,1)*m(3,:)); | ||
50 | M = M ./ (ones(3,1)*M(3,:)); | ||
51 | |||
52 | % Prenormalization of point coordinates (very important): | ||
53 | % (Affine normalization) | ||
54 | |||
55 | ax = m(1,:); | ||
56 | ay = m(2,:); | ||
57 | |||
58 | mxx = mean(ax); | ||
59 | myy = mean(ay); | ||
60 | ax = ax - mxx; | ||
61 | ay = ay - myy; | ||
62 | |||
63 | scxx = mean(abs(ax)); | ||
64 | scyy = mean(abs(ay)); | ||
65 | |||
66 | |||
67 | Hnorm = [1/scxx 0 -mxx/scxx;0 1/scyy -myy/scyy;0 0 1]; | ||
68 | inv_Hnorm = [scxx 0 mxx ; 0 scyy myy; 0 0 1]; | ||
69 | |||
70 | mn = Hnorm*m; | ||
71 | |||
72 | % Compute the homography between m and mn: | ||
73 | |||
74 | % Build the matrix: | ||
75 | |||
76 | L = zeros(2*Np,9); | ||
77 | |||
78 | L(1:2:2*Np,1:3) = M'; | ||
79 | L(2:2:2*Np,4:6) = M'; | ||
80 | L(1:2:2*Np,7:9) = -((ones(3,1)*mn(1,:)).* M)'; | ||
81 | L(2:2:2*Np,7:9) = -((ones(3,1)*mn(2,:)).* M)'; | ||
82 | |||
83 | if Np > 4, | ||
84 | L = L'*L; | ||
85 | end; | ||
86 | |||
87 | [U,S,V] = svd(L); | ||
88 | |||
89 | hh = V(:,9); | ||
90 | hh = hh/hh(9); | ||
91 | |||
92 | Hrem = reshape(hh,3,3)'; | ||
93 | %Hrem = Hrem / Hrem(3,3); | ||
94 | |||
95 | % Final homography: | ||
96 | |||
97 | H = inv_Hnorm*Hrem; | ||
98 | |||
99 | |||
100 | %%% Homography refinement if there are more than 4 points: | ||
101 | |||
102 | if Np > 4, | ||
103 | |||
104 | % Final refinement: | ||
105 | |||
106 | hhv = reshape(H',9,1); | ||
107 | hhv = hhv(1:8); | ||
108 | |||
109 | for iter=1:10, | ||
110 | |||
111 | mrep = H * M; | ||
112 | |||
113 | J = zeros(2*Np,8); | ||
114 | |||
115 | MMM = (M ./ (ones(3,1)*mrep(3,:))); | ||
116 | |||
117 | J(1:2:2*Np,1:3) = -MMM'; | ||
118 | J(2:2:2*Np,4:6) = -MMM'; | ||
119 | |||
120 | mrep = mrep ./ (ones(3,1)*mrep(3,:)); | ||
121 | |||
122 | m_err = m(1:2,:) - mrep(1:2,:); | ||
123 | m_err = m_err(:); | ||
124 | |||
125 | MMM2 = (ones(3,1)*mrep(1,:)) .* MMM; | ||
126 | MMM3 = (ones(3,1)*mrep(2,:)) .* MMM; | ||
127 | |||
128 | J(1:2:2*Np,7:8) = MMM2(1:2,:)'; | ||
129 | J(2:2:2*Np,7:8) = MMM3(1:2,:)'; | ||
130 | |||
131 | MMM = (M ./ (ones(3,1)*mrep(3,:)))'; | ||
132 | |||
133 | hh_innov = inv(J'*J)*J'*m_err; | ||
134 | |||
135 | hhv_up = hhv - hh_innov; | ||
136 | |||
137 | H_up = reshape([hhv_up;1],3,3)'; | ||
138 | |||
139 | %norm(m_err) | ||
140 | %norm(hh_innov) | ||
141 | |||
142 | hhv = hhv_up; | ||
143 | H = H_up; | ||
144 | |||
145 | end; | ||
146 | |||
147 | end; | ||
148 | |||
149 | |||
150 | |||
151 | |||
152 | |||
153 | return; | ||
154 | |||
155 | %test of Jacobian | ||
156 | |||
157 | mrep = H*M; | ||
158 | mrep = mrep ./ (ones(3,1)*mrep(3,:)); | ||
159 | |||
160 | m_err = mrep(1:2,:) - m(1:2,:); | ||
161 | figure(8); | ||
162 | plot(m_err(1,:),m_err(2,:),'r+'); | ||
163 | std(m_err') | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/cornerfinder.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/cornerfinder.m new file mode 100755 index 0000000..9bfa51f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/cornerfinder.m | |||
@@ -0,0 +1,215 @@ | |||
1 | function [xc,good,bad,type] = cornerfinder(xt,I,wintx,winty,wx2,wy2); | ||
2 | |||
3 | %[xc] = cornerfinder(xt,I); | ||
4 | % | ||
5 | %Finds the sub-pixel corners on the image I with initial guess xt | ||
6 | %xt and xc are 2xN matrices. The first component is the x coordinate | ||
7 | %(horizontal) and the second component is the y coordinate (vertical) | ||
8 | % | ||
9 | %Based on Harris corner finder method | ||
10 | % | ||
11 | %Finds corners to a precision below .1 pixel! | ||
12 | %Oct. 14th, 1997 - UPDATED to work with vertical and horizontal edges as well!!! | ||
13 | %Sept 1998 - UPDATED to handle diverged points: we keep the original points | ||
14 | %good is a binary vector indicating wether a feature point has been properly | ||
15 | %found. | ||
16 | % | ||
17 | %Add a zero zone of size wx2,wy2 | ||
18 | %July 15th, 1999 - Bug on the mask building... fixed + change to Gaussian mask with higher | ||
19 | %resolution and larger number of iterations. | ||
20 | |||
21 | |||
22 | % California Institute of Technology | ||
23 | % (c) Jean-Yves Bouguet -- Oct. 14th, 1997 | ||
24 | |||
25 | |||
26 | |||
27 | line_feat = 1; % set to 1 to allow for extraction of line features. | ||
28 | |||
29 | xt = xt'; | ||
30 | xt = fliplr(xt); | ||
31 | |||
32 | |||
33 | if nargin < 4, | ||
34 | winty = 5; | ||
35 | if nargin < 3, | ||
36 | wintx = 5; | ||
37 | end; | ||
38 | end; | ||
39 | |||
40 | |||
41 | if nargin < 6, | ||
42 | wx2 = -1; | ||
43 | wy2 = -1; | ||
44 | end; | ||
45 | |||
46 | |||
47 | %mask = ones(2*wintx+1,2*winty+1); | ||
48 | mask = exp(-((-wintx:wintx)'/(wintx)).^2) * exp(-((-winty:winty)/(winty)).^2); | ||
49 | |||
50 | |||
51 | if (wx2>0) & (wy2>0), | ||
52 | if ((wintx - wx2)>=2)&((winty - wy2)>=2), | ||
53 | mask(wintx+1-wx2:wintx+1+wx2,winty+1-wy2:winty+1+wy2)= zeros(2*wx2+1,2*wy2+1); | ||
54 | end; | ||
55 | end; | ||
56 | |||
57 | offx = [-wintx:wintx]'*ones(1,2*winty+1); | ||
58 | offy = ones(2*wintx+1,1)*[-winty:winty]; | ||
59 | |||
60 | resolution = 0.005; | ||
61 | |||
62 | MaxIter = 10; | ||
63 | |||
64 | [nx,ny] = size(I); | ||
65 | N = size(xt,1); | ||
66 | |||
67 | xc = xt; % first guess... they don't move !!! | ||
68 | |||
69 | type = zeros(1,N); | ||
70 | |||
71 | |||
72 | for i=1:N, | ||
73 | |||
74 | v_extra = resolution + 1; % just larger than resolution | ||
75 | |||
76 | compt = 0; % no iteration yet | ||
77 | |||
78 | while (norm(v_extra) > resolution) & (compt<MaxIter), | ||
79 | |||
80 | cIx = xc(i,1); % | ||
81 | cIy = xc(i,2); % Coords. of the point | ||
82 | crIx = round(cIx); % on the initial image | ||
83 | crIy = round(cIy); % | ||
84 | itIx = cIx - crIx; % Coefficients | ||
85 | itIy = cIy - crIy; % to compute | ||
86 | if itIx > 0, % the sub pixel | ||
87 | vIx = [itIx 1-itIx 0]'; % accuracy. | ||
88 | else | ||
89 | vIx = [0 1+itIx -itIx]'; | ||
90 | end; | ||
91 | if itIy > 0, | ||
92 | vIy = [itIy 1-itIy 0]; | ||
93 | else | ||
94 | vIy = [0 1+itIy -itIy]; | ||
95 | end; | ||
96 | |||
97 | |||
98 | % What if the sub image is not in? | ||
99 | |||
100 | if (crIx-wintx-2 < 1), xmin=1; xmax = 2*wintx+5; | ||
101 | elseif (crIx+wintx+2 > nx), xmax = nx; xmin = nx-2*wintx-4; | ||
102 | else | ||
103 | xmin = crIx-wintx-2; xmax = crIx+wintx+2; | ||
104 | end; | ||
105 | |||
106 | if (crIy-winty-2 < 1), ymin=1; ymax = 2*winty+5; | ||
107 | elseif (crIy+winty+2 > ny), ymax = ny; ymin = ny-2*winty-4; | ||
108 | else | ||
109 | ymin = crIy-winty-2; ymax = crIy+winty+2; | ||
110 | end; | ||
111 | |||
112 | |||
113 | SI = I(xmin:xmax,ymin:ymax); % The necessary neighborhood | ||
114 | SI = conv2(conv2(SI,vIx,'same'),vIy,'same'); | ||
115 | SI = SI(2:2*wintx+4,2:2*winty+4); % The subpixel interpolated neighborhood | ||
116 | [gy,gx] = gradient(SI); % The gradient image | ||
117 | gx = gx(2:2*wintx+2,2:2*winty+2); % extraction of the useful parts only | ||
118 | gy = gy(2:2*wintx+2,2:2*winty+2); % of the gradients | ||
119 | |||
120 | px = cIx + offx; | ||
121 | py = cIy + offy; | ||
122 | |||
123 | gxx = gx .* gx .* mask; | ||
124 | gyy = gy .* gy .* mask; | ||
125 | gxy = gx .* gy .* mask; | ||
126 | |||
127 | |||
128 | bb = [sum(sum(gxx .* px + gxy .* py)); sum(sum(gxy .* px + gyy .* py))]; | ||
129 | |||
130 | a = sum(sum(gxx)); | ||
131 | b = sum(sum(gxy)); | ||
132 | c = sum(sum(gyy)); | ||
133 | |||
134 | dt = a*c - b^2; | ||
135 | |||
136 | xc2 = [c*bb(1)-b*bb(2) a*bb(2)-b*bb(1)]/dt; | ||
137 | |||
138 | |||
139 | %keyboard; | ||
140 | |||
141 | if line_feat, | ||
142 | |||
143 | G = [a b;b c]; | ||
144 | [U,S,V] = svd(G); | ||
145 | |||
146 | %keyboard; | ||
147 | |||
148 | % If non-invertible, then project the point onto the edge orthogonal: | ||
149 | |||
150 | if (S(1,1)/S(2,2) > 50), | ||
151 | % projection operation: | ||
152 | xc2 = xc2 + sum((xc(i,:)-xc2).*(V(:,2)'))*V(:,2)'; | ||
153 | type(i) = 1; | ||
154 | end; | ||
155 | |||
156 | end; | ||
157 | |||
158 | |||
159 | %keyboard; | ||
160 | |||
161 | % G = [a b;b c]; | ||
162 | % [U,S,V] = svd(G); | ||
163 | |||
164 | |||
165 | % if S(1,1)/S(2,2) > 150, | ||
166 | % bb2 = U'*bb; | ||
167 | % xc2 = (V*[bb2(1)/S(1,1) ;0])'; | ||
168 | % else | ||
169 | % xc2 = [c*bb(1)-b*bb(2) a*bb(2)-b*bb(1)]/dt; | ||
170 | % end; | ||
171 | |||
172 | |||
173 | %if (abs(a)> 50*abs(c)), | ||
174 | % xc2 = [(c*bb(1)-b*bb(2))/dt xc(i,2)]; | ||
175 | % elseif (abs(c)> 50*abs(a)) | ||
176 | % xc2 = [xc(i,1) (a*bb(2)-b*bb(1))/dt]; | ||
177 | % else | ||
178 | % xc2 = [c*bb(1)-b*bb(2) a*bb(2)-b*bb(1)]/dt; | ||
179 | % end; | ||
180 | |||
181 | %keyboard; | ||
182 | |||
183 | v_extra = xc(i,:) - xc2; | ||
184 | |||
185 | xc(i,:) = xc2; | ||
186 | |||
187 | % keyboard; | ||
188 | |||
189 | compt = compt + 1; | ||
190 | |||
191 | end | ||
192 | end; | ||
193 | |||
194 | |||
195 | % check for points that diverge: | ||
196 | |||
197 | delta_x = xc(:,1) - xt(:,1); | ||
198 | delta_y = xc(:,2) - xt(:,2); | ||
199 | |||
200 | %keyboard; | ||
201 | |||
202 | |||
203 | bad = (abs(delta_x) > wintx) | (abs(delta_y) > winty); | ||
204 | good = ~bad; | ||
205 | in_bad = find(bad); | ||
206 | |||
207 | % For the diverged points, keep the original guesses: | ||
208 | |||
209 | xc(in_bad,:) = xt(in_bad,:); | ||
210 | |||
211 | xc = fliplr(xc); | ||
212 | xc = xc'; | ||
213 | |||
214 | bad = bad'; | ||
215 | good = good'; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/count_squares.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/count_squares.m new file mode 100755 index 0000000..0e226c0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/count_squares.m | |||
@@ -0,0 +1,74 @@ | |||
1 | function ns = count_squares(I,x1,y1,x2,y2,win); | ||
2 | |||
3 | %keyboard; | ||
4 | |||
5 | [ny,nx] = size(I); | ||
6 | |||
7 | lambda = [y1 - y2;x2 - x1;x1*y2 - x2*y1]; | ||
8 | |||
9 | lambda = 1/sqrt(lambda(1)^2 + lambda(2)^2) * lambda; | ||
10 | |||
11 | l1 = lambda + [0;0;win]; | ||
12 | l2 = lambda - [0;0;win]; | ||
13 | |||
14 | |||
15 | dx = x2-x1; | ||
16 | dy = y2 - y1; | ||
17 | |||
18 | |||
19 | if abs(dx) > abs(dy), | ||
20 | |||
21 | if x2 > x1, | ||
22 | xs = x1:x2; | ||
23 | else | ||
24 | xs = x1:-1:x2; | ||
25 | end; | ||
26 | |||
27 | ys = -(lambda(3) + lambda(1)*xs)/lambda(2); | ||
28 | |||
29 | else | ||
30 | |||
31 | if y2 > y1, | ||
32 | ys = y1:y2; | ||
33 | else | ||
34 | ys = y1:-1:y2; | ||
35 | end; | ||
36 | xs = -(lambda(3) + lambda(2)*ys)/lambda(1); | ||
37 | |||
38 | end; | ||
39 | |||
40 | |||
41 | |||
42 | Np = length(xs); | ||
43 | |||
44 | xs_mat = ones(2*win + 1,1)*xs; | ||
45 | ys_mat = ones(2*win + 1,1)*ys; | ||
46 | |||
47 | win_mat = (-win:win)'*ones(1,Np); | ||
48 | |||
49 | |||
50 | xs_mat2 = round(xs_mat - win_mat * lambda(1)); | ||
51 | ys_mat2 = round(ys_mat - win_mat * lambda(2)); | ||
52 | |||
53 | ind_mat = (xs_mat2 - 1) * ny + ys_mat2; | ||
54 | |||
55 | ima_patch = zeros(2*win + 1,Np); | ||
56 | |||
57 | ima_patch(:) = I(ind_mat(:)); | ||
58 | |||
59 | %ima2 = ima_patch(:,win+1:end-win); | ||
60 | |||
61 | filtk = [ones(win,Np);zeros(1,Np);-ones(win,Np)]; | ||
62 | |||
63 | out_f = sum(filtk.*ima_patch); | ||
64 | |||
65 | out_f_f = conv2(out_f,[1/4 1/2 1/4],'same'); | ||
66 | |||
67 | out_f_f = out_f_f(win+1:end-win); | ||
68 | |||
69 | ns = length(find(((out_f_f(2:end)>=0)&(out_f_f(1:end-1)<0)) | ((out_f_f(2:end)<=0)&(out_f_f(1:end-1)>0))))+1; | ||
70 | |||
71 | |||
72 | |||
73 | |||
74 | return; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/data_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/data_calib.m new file mode 100755 index 0000000..422769b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/data_calib.m | |||
@@ -0,0 +1,92 @@ | |||
1 | %%% This script alets the user enter the name of the images (base name, numbering scheme,... | ||
2 | |||
3 | |||
4 | % Checks that there are some images in the directory: | ||
5 | |||
6 | l_ras = dir('*ras'); | ||
7 | s_ras = size(l_ras,1); | ||
8 | l_bmp = dir('*bmp'); | ||
9 | s_bmp = size(l_bmp,1); | ||
10 | l_tif = dir('*tif'); | ||
11 | s_tif = size(l_tif,1); | ||
12 | l_pgm = dir('*pgm'); | ||
13 | s_pgm = size(l_pgm,1); | ||
14 | l_jpg = dir('*jpg'); | ||
15 | s_jpg = size(l_jpg,1); | ||
16 | |||
17 | s_tot = s_ras + s_bmp + s_tif + s_pgm + s_jpg; | ||
18 | |||
19 | if s_tot < 1, | ||
20 | fprintf(1,'No image in this directory in either ras, bmp, tif, pgm or jpg format. Change directory and try again.\n'); | ||
21 | break; | ||
22 | end; | ||
23 | |||
24 | |||
25 | % IF yes, display the directory content: | ||
26 | |||
27 | dir; | ||
28 | |||
29 | Nima_valid = 0; | ||
30 | |||
31 | while (Nima_valid==0), | ||
32 | |||
33 | fprintf(1,'\n'); | ||
34 | calib_name = input('Basename camera calibration images (without number nor suffix): ','s'); | ||
35 | |||
36 | format_image = '0'; | ||
37 | |||
38 | while format_image == '0', | ||
39 | |||
40 | format_image = input('Image format: ([]=''r''=''ras'', ''b''=''bmp'', ''t''=''tif'', ''p''=''pgm'', ''j''=''jpg'', ''m''=''ppm'') ','s'); | ||
41 | |||
42 | if isempty(format_image), | ||
43 | format_image = 'ras'; | ||
44 | end; | ||
45 | |||
46 | if lower(format_image(1)) == 'm', | ||
47 | format_image = 'ppm'; | ||
48 | else | ||
49 | if lower(format_image(1)) == 'b', | ||
50 | format_image = 'bmp'; | ||
51 | else | ||
52 | if lower(format_image(1)) == 't', | ||
53 | format_image = 'tif'; | ||
54 | else | ||
55 | if lower(format_image(1)) == 'p', | ||
56 | format_image = 'pgm'; | ||
57 | else | ||
58 | if lower(format_image(1)) == 'j', | ||
59 | format_image = 'jpg'; | ||
60 | else | ||
61 | if lower(format_image(1)) == 'r', | ||
62 | format_image = 'ras'; | ||
63 | else | ||
64 | disp('Invalid image format'); | ||
65 | format_image = '0'; % Ask for format once again | ||
66 | end; | ||
67 | end; | ||
68 | end; | ||
69 | end; | ||
70 | end; | ||
71 | end; | ||
72 | end; | ||
73 | |||
74 | |||
75 | check_directory; | ||
76 | |||
77 | end; | ||
78 | |||
79 | |||
80 | |||
81 | %string_save = 'save calib_data n_ima type_numbering N_slots image_numbers format_image calib_name first_num'; | ||
82 | |||
83 | %eval(string_save); | ||
84 | |||
85 | |||
86 | |||
87 | if (Nima_valid~=0), | ||
88 | % Reading images: | ||
89 | |||
90 | ima_read_calib; % may be launched from the toolbox itself | ||
91 | end; | ||
92 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/error_analysis.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/error_analysis.m new file mode 100755 index 0000000..85feac5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/error_analysis.m | |||
@@ -0,0 +1,182 @@ | |||
1 | %%% ERROR_ANALYSIS | ||
2 | %%% This simulation helps coputing the acturacies of calibration | ||
3 | %%% Run it after the main calibration | ||
4 | |||
5 | |||
6 | |||
7 | N_runs = 200; | ||
8 | |||
9 | %N_ima_active = 4; | ||
10 | |||
11 | saving = 1; | ||
12 | |||
13 | if 1, %~exist('fc_list'), % initialization | ||
14 | |||
15 | % Initialization: | ||
16 | |||
17 | load Calib_Results; | ||
18 | check_active_images; | ||
19 | |||
20 | fc_list = []; | ||
21 | cc_list = []; | ||
22 | kc_list = []; | ||
23 | active_images_list = []; | ||
24 | |||
25 | |||
26 | for kk=1:n_ima, | ||
27 | |||
28 | eval(['omc_list_' num2str(kk) ' = [];']); | ||
29 | eval(['Tc_list_' num2str(kk) ' = [];']); | ||
30 | |||
31 | end; | ||
32 | |||
33 | %sx = median(abs(ex(1,:)))*1.4836; | ||
34 | %sy = median(abs(ex(2,:)))*1.4836; | ||
35 | |||
36 | sx = std(ex(1,:)); | ||
37 | sy = std(ex(2,:)); | ||
38 | |||
39 | % Saving the feature locations: | ||
40 | |||
41 | for kk = 1:n_ima, | ||
42 | |||
43 | eval(['x_save_' num2str(kk) ' = x_' num2str(kk) ';']); | ||
44 | eval(['y_save_' num2str(kk) ' = y_' num2str(kk) ';']); | ||
45 | |||
46 | end; | ||
47 | |||
48 | active_images_save = active_images; | ||
49 | ind_active_save = ind_active; | ||
50 | |||
51 | fc_save = fc; | ||
52 | cc_save = cc; | ||
53 | kc_save = kc; | ||
54 | KK_save = KK; | ||
55 | |||
56 | |||
57 | end; | ||
58 | |||
59 | |||
60 | |||
61 | |||
62 | %%% The main loop: | ||
63 | |||
64 | |||
65 | for ntrial = 1:N_runs, | ||
66 | |||
67 | fprintf(1,'\nRun number: %d\n',ntrial); | ||
68 | fprintf(1, '----------\n'); | ||
69 | |||
70 | for kk = 1:n_ima, | ||
71 | |||
72 | eval(['y_kk = y_save_' num2str(kk) ';']) | ||
73 | |||
74 | if active_images(kk) & ~isnan(y_kk(1,1)), | ||
75 | |||
76 | Nkk = size(y_kk,2); | ||
77 | |||
78 | x_kk_new = y_kk + [sx * randn(1,Nkk);sy*randn(1,Nkk)]; | ||
79 | |||
80 | eval(['x_' num2str(kk) ' = x_kk_new;']); | ||
81 | |||
82 | end; | ||
83 | |||
84 | end; | ||
85 | |||
86 | N_active = length(ind_active_save); | ||
87 | junk = randn(1,N_active); | ||
88 | [junk,junk2] = sort(junk); | ||
89 | |||
90 | active_images = zeros(1,n_ima); | ||
91 | active_images(ind_active_save(junk2(1:N_ima_active))) = ones(1,N_ima_active); | ||
92 | |||
93 | fc = fc_save; | ||
94 | cc = cc_save; | ||
95 | kc = kc_save; | ||
96 | KK = KK_save; | ||
97 | |||
98 | go_calib_optim; | ||
99 | |||
100 | fc_list = [fc_list fc]; | ||
101 | cc_list = [cc_list cc]; | ||
102 | kc_list = [kc_list kc]; | ||
103 | active_images_list = [active_images_list active_images']; | ||
104 | |||
105 | for kk=1:n_ima, | ||
106 | |||
107 | eval(['omc_list_' num2str(kk) ' = [ omc_list_' num2str(kk) ' omc_' num2str(kk) ' ];']); | ||
108 | eval(['Tc_list_' num2str(kk) ' = [ Tc_list_' num2str(kk) ' Tc_' num2str(kk) ' ];']); | ||
109 | |||
110 | end; | ||
111 | |||
112 | end; | ||
113 | |||
114 | |||
115 | |||
116 | |||
117 | if 0, | ||
118 | |||
119 | % Restoring the feature locations: | ||
120 | |||
121 | for kk = 1:n_ima, | ||
122 | |||
123 | eval(['x_' num2str(kk) ' = x_save_' num2str(kk) ';']); | ||
124 | |||
125 | end; | ||
126 | |||
127 | fprintf(1,'\nFinal run (with the real data)\n'); | ||
128 | fprintf(1, '------------------------------\n'); | ||
129 | |||
130 | active_images = active_images_save; | ||
131 | ind_active = ind_active_save; | ||
132 | |||
133 | go_calib_optim; | ||
134 | |||
135 | fc_list = [fc_list fc]; | ||
136 | cc_list = [cc_list cc]; | ||
137 | kc_list = [kc_list kc]; | ||
138 | active_images_list = [active_images_list active_images']; | ||
139 | |||
140 | for kk=1:n_ima, | ||
141 | |||
142 | eval(['omc_list_' num2str(kk) ' = [ omc_list_' num2str(kk) ' omc_' num2str(kk) ' ];']); | ||
143 | eval(['Tc_list_' num2str(kk) ' = [ Tc_list_' num2str(kk) ' Tc_' num2str(kk) ' ];']); | ||
144 | |||
145 | end; | ||
146 | |||
147 | end; | ||
148 | |||
149 | |||
150 | |||
151 | |||
152 | |||
153 | if saving, | ||
154 | |||
155 | disp(['Save Calibration accuracy results under Calib_Accuracies_' num2str(N_ima_active) '.mat']); | ||
156 | |||
157 | string_save = ['save Calib_Accuracies_' num2str(N_ima_active) ' active_images n_ima N_ima_active N_runs active_images_list fc cc kc fc_list cc_list kc_list']; | ||
158 | |||
159 | for kk = 1:n_ima, | ||
160 | string_save = [string_save ' Tc_list_' num2str(kk) ' omc_list_' num2str(kk) ' Tc_' num2str(kk) ' omc_' num2str(kk) ]; | ||
161 | end; | ||
162 | |||
163 | eval(string_save); | ||
164 | |||
165 | end; | ||
166 | |||
167 | |||
168 | return; | ||
169 | |||
170 | std(fc_list') | ||
171 | |||
172 | std(cc_list') | ||
173 | |||
174 | std(kc_list') | ||
175 | |||
176 | for kk = 1:n_ima, | ||
177 | |||
178 | eval(['std(Tc_list_' num2str(kk) ''')']) | ||
179 | |||
180 | end; | ||
181 | |||
182 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/export_calib_data.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/export_calib_data.m new file mode 100755 index 0000000..39506a8 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/export_calib_data.m | |||
@@ -0,0 +1,99 @@ | |||
1 | %% Export calibration data (corners + 3D coordinates) to | ||
2 | %% text files (in Willson-Heikkila's format or Zhang's format) | ||
3 | |||
4 | %% Thanks to Michael Goesele (from the Max-Planck-Institut) for the original suggestion | ||
5 | %% of adding thsi export function to the toolbox. | ||
6 | |||
7 | |||
8 | if ~exist('n_ima'), | ||
9 | fprintf(1,'ERROR: No calibration data to export\n'); | ||
10 | |||
11 | else | ||
12 | |||
13 | check_active_images; | ||
14 | |||
15 | check_extracted_images; | ||
16 | |||
17 | check_active_images; | ||
18 | |||
19 | fprintf(1,'Tool that exports calibration data to Willson-Heikkila or Zhang formats\n'); | ||
20 | |||
21 | qformat = -1; | ||
22 | |||
23 | while (qformat ~=0)&(qformat ~=1), | ||
24 | |||
25 | fprintf(1,'Two possible formats of export: 0=Willson and Heikkila, 1=Zhang\n') | ||
26 | qformat = input('Format of export (enter 0 or 1): '); | ||
27 | |||
28 | if isempty(qformat) | ||
29 | qformat = -1; | ||
30 | end; | ||
31 | |||
32 | if (qformat ~=0)&(qformat ~=1), | ||
33 | |||
34 | fprintf(1,'Invalid entry. Try again.\n') | ||
35 | |||
36 | end; | ||
37 | |||
38 | end; | ||
39 | |||
40 | if qformat == 0, | ||
41 | |||
42 | fprintf(1,'\nExport of calibration data to text files (Willson and Heikkila''s format)\n'); | ||
43 | outputfile = input('File basename: ','s'); | ||
44 | |||
45 | for kk = ind_active, | ||
46 | |||
47 | eval(['X_kk = X_' num2str(kk) ';']); | ||
48 | eval(['x_kk = x_' num2str(kk) ';']); | ||
49 | |||
50 | Xx = [X_kk ; x_kk]'; | ||
51 | |||
52 | file_name = [outputfile num2str(kk)]; | ||
53 | |||
54 | disp(['Exporting calibration data (3D world + 2D image coordinates) of image ' num2str(kk) ' to file ' file_name '...']); | ||
55 | |||
56 | eval(['save ' file_name ' Xx -ASCII']); | ||
57 | |||
58 | end; | ||
59 | |||
60 | else | ||
61 | |||
62 | fprintf(1,'\nExport of calibration data to text files (Zhang''s format)\n'); | ||
63 | modelfile = input('File basename for the 3D world coordinates: ','s'); | ||
64 | datafile = input('File basename for the 2D image coordinates: ','s'); | ||
65 | |||
66 | for kk = ind_active, | ||
67 | |||
68 | eval(['X_kk = X_' num2str(kk) ';']); | ||
69 | eval(['x_kk = x_' num2str(kk) ';']); | ||
70 | |||
71 | if ~exist(['n_sq_x_' num2str(kk)]), | ||
72 | n_sq_x = 1; | ||
73 | n_sq_y = size(X_kk,2); | ||
74 | else | ||
75 | eval(['n_sq_x = n_sq_x_' num2str(kk) ';']); | ||
76 | eval(['n_sq_y = n_sq_y_' num2str(kk) ';']); | ||
77 | end; | ||
78 | |||
79 | X = reshape(X_kk(1,:)',n_sq_x+1,n_sq_y+1)'; | ||
80 | Y = reshape(X_kk(2,:)',n_sq_x+1,n_sq_y+1)'; | ||
81 | XY = reshape([X;Y],n_sq_y+1,2*(n_sq_x+1)); | ||
82 | |||
83 | x = reshape(x_kk(1,:)',n_sq_x+1,n_sq_y+1)'; | ||
84 | y = reshape(x_kk(2,:)',n_sq_x+1,n_sq_y+1)'; | ||
85 | xy = reshape([x;y],n_sq_y+1,2*(n_sq_x+1)); | ||
86 | |||
87 | disp(['Exporting calibration data of image ' num2str(kk) ' to files ' modelfile num2str(kk) '.txt and ' datafile num2str(kk) '.txt...']); | ||
88 | |||
89 | eval(['save ' modelfile num2str(kk) '.txt XY -ASCII']); | ||
90 | eval(['save ' datafile num2str(kk) '.txt xy -ASCII']); | ||
91 | |||
92 | end; | ||
93 | |||
94 | |||
95 | end; | ||
96 | |||
97 | fprintf(1,'done\n'); | ||
98 | |||
99 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/ext_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/ext_calib.m new file mode 100755 index 0000000..04d6319 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/ext_calib.m | |||
@@ -0,0 +1,152 @@ | |||
1 | |||
2 | %%%%%%%%%%%%%%%%%%%% SHOW EXTRINSIC RESULTS %%%%%%%%%%%%%%%%%%%%%%%% | ||
3 | |||
4 | if ~exist('n_ima')|~exist('fc'), | ||
5 | fprintf(1,'No calibration data available.\n'); | ||
6 | return; | ||
7 | end; | ||
8 | |||
9 | check_active_images; | ||
10 | |||
11 | if ~exist(['omc_' num2str(ind_active(1))]), | ||
12 | fprintf(1,'No calibration data available.\n'); | ||
13 | return; | ||
14 | end; | ||
15 | |||
16 | %if ~exist('no_grid'), | ||
17 | no_grid = 0; | ||
18 | %end; | ||
19 | |||
20 | if ~exist(['n_sq_x_' num2str(ind_active(1))]), | ||
21 | no_grid = 1; | ||
22 | end; | ||
23 | |||
24 | |||
25 | if 0, | ||
26 | |||
27 | err_std = std(ex'); | ||
28 | |||
29 | fprintf(1,'\n\nCalibration results without principal point estimation:\n\n'); | ||
30 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
31 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
32 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
33 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
34 | |||
35 | end; | ||
36 | |||
37 | |||
38 | % Color code for each image: | ||
39 | |||
40 | colors = 'brgkcm'; | ||
41 | |||
42 | |||
43 | %%% Show the extrinsic parameters | ||
44 | |||
45 | if ~exist('dX'), | ||
46 | eval(['dX = norm(Tc_' num2str(ind_active(1)) ')/10;']); | ||
47 | dY = dX; | ||
48 | end; | ||
49 | |||
50 | IP = 5*dX*([0 nx-1 nx-1 0 0 ; 0 0 ny-1 ny-1 0;1 1 1 1 1] - [cc;0]*ones(1,5)) ./ ([fc;1]*ones(1,5)); | ||
51 | BASE = 5*dX*([0 1 0 0 0 0;0 0 0 1 0 0;0 0 0 0 0 1]); | ||
52 | IP = reshape([IP;BASE(:,1)*ones(1,5);IP],3,15); | ||
53 | |||
54 | figure(4); | ||
55 | [a,b] = view; | ||
56 | |||
57 | figure(4); | ||
58 | plot3(BASE(1,:),BASE(3,:),-BASE(2,:),'b-','linewidth',2'); | ||
59 | hold on; | ||
60 | plot3(IP(1,:),IP(3,:),-IP(2,:),'r-','linewidth',2); | ||
61 | text(6*dX,0,0,'X_c'); | ||
62 | text(-dX,5*dX,0,'Z_c'); | ||
63 | text(0,0,-6*dX,'Y_c'); | ||
64 | text(-dX,-dX,dX,'O_c'); | ||
65 | |||
66 | |||
67 | for kk = 1:n_ima, | ||
68 | |||
69 | if active_images(kk); | ||
70 | |||
71 | if exist(['X_' num2str(kk)]) & exist(['omc_' num2str(kk)]), | ||
72 | |||
73 | eval(['XX_kk = X_' num2str(kk) ';']); | ||
74 | |||
75 | if ~isnan(XX_kk(1,1)) | ||
76 | |||
77 | eval(['omc_kk = omc_' num2str(kk) ';']); | ||
78 | eval(['Tc_kk = Tc_' num2str(kk) ';']); | ||
79 | N_kk = size(XX_kk,2); | ||
80 | |||
81 | if ~exist(['n_sq_x_' num2str(kk)]), | ||
82 | no_grid = 1; | ||
83 | else | ||
84 | eval(['n_sq_x = n_sq_x_' num2str(kk) ';']); | ||
85 | if isnan(n_sq_x(1)), | ||
86 | no_grid = 1; | ||
87 | end; | ||
88 | end; | ||
89 | |||
90 | |||
91 | if ~no_grid, | ||
92 | eval(['n_sq_x = n_sq_x_' num2str(kk) ';']); | ||
93 | eval(['n_sq_y = n_sq_y_' num2str(kk) ';']); | ||
94 | if (N_kk ~= ((n_sq_x+1)*(n_sq_y+1))), | ||
95 | no_grid = 1; | ||
96 | end; | ||
97 | end; | ||
98 | |||
99 | if ~isnan(omc_kk(1,1)), | ||
100 | |||
101 | R_kk = rodrigues(omc_kk); | ||
102 | |||
103 | YY_kk = R_kk * XX_kk + Tc_kk * ones(1,length(XX_kk)); | ||
104 | |||
105 | uu = [-dX;-dY;0]/2; | ||
106 | uu = R_kk * uu + Tc_kk; | ||
107 | |||
108 | if ~no_grid, | ||
109 | YYx = zeros(n_sq_x+1,n_sq_y+1); | ||
110 | YYy = zeros(n_sq_x+1,n_sq_y+1); | ||
111 | YYz = zeros(n_sq_x+1,n_sq_y+1); | ||
112 | |||
113 | YYx(:) = YY_kk(1,:); | ||
114 | YYy(:) = YY_kk(2,:); | ||
115 | YYz(:) = YY_kk(3,:); | ||
116 | |||
117 | %keyboard; | ||
118 | |||
119 | figure(4); | ||
120 | hhh= mesh(YYx,YYz,-YYy); | ||
121 | set(hhh,'edgecolor',colors(rem(kk-1,6)+1),'linewidth',1); %,'facecolor','none'); | ||
122 | %plot3(YY_kk(1,:),YY_kk(3,:),-YY_kk(2,:),['o' colors(rem(kk-1,6)+1)]); | ||
123 | text(uu(1),uu(3),-uu(2),num2str(kk),'fontsize',14,'color',colors(rem(kk-1,6)+1)); | ||
124 | else | ||
125 | |||
126 | figure(4); | ||
127 | plot3(YY_kk(1,:),YY_kk(3,:),-YY_kk(2,:),['.' colors(rem(kk-1,6)+1)]); | ||
128 | text(uu(1),uu(3),-uu(2),num2str(kk),'fontsize',14,'color',colors(rem(kk-1,6)+1)); | ||
129 | |||
130 | end; | ||
131 | |||
132 | end; | ||
133 | |||
134 | end; | ||
135 | |||
136 | end; | ||
137 | |||
138 | end; | ||
139 | |||
140 | end; | ||
141 | |||
142 | figure(4);rotate3d on; | ||
143 | axis('equal'); | ||
144 | title('Extrinsic parameters'); | ||
145 | %view(60,30); | ||
146 | view(a,b); | ||
147 | hold off; | ||
148 | |||
149 | set(4,'Name','3D','NumberTitle','off'); | ||
150 | |||
151 | %fprintf(1,'To generate the complete movie associated to the optimization loop, try: check_convergence;\n'); | ||
152 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extract_grid.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extract_grid.m new file mode 100755 index 0000000..0cf9abe --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extract_grid.m | |||
@@ -0,0 +1,234 @@ | |||
1 | function [x,X,n_sq_x,n_sq_y,ind_orig,ind_x,ind_y] = extract_grid(I,wintx,winty,fc,cc,kc,dX,dY); | ||
2 | |||
3 | map = gray(256); | ||
4 | |||
5 | minI = min(I(:)); | ||
6 | maxI = max(I(:)); | ||
7 | |||
8 | Id = 255*(I - minI)/(maxI - minI); | ||
9 | |||
10 | figure(2); | ||
11 | image(Id); | ||
12 | colormap(map); | ||
13 | |||
14 | |||
15 | if nargin < 2, | ||
16 | |||
17 | disp('Window size for corner finder (wintx and winty):'); | ||
18 | wintx = input('wintx ([] = 5) = '); | ||
19 | if isempty(wintx), wintx = 5; end; | ||
20 | wintx = round(wintx); | ||
21 | winty = input('winty ([] = 5) = '); | ||
22 | if isempty(winty), winty = 5; end; | ||
23 | winty = round(winty); | ||
24 | |||
25 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
26 | |||
27 | end; | ||
28 | |||
29 | |||
30 | |||
31 | title('Click on the four extreme corners of the rectangular pattern...'); | ||
32 | |||
33 | disp('Click on the four extreme corners of the rectangular complete pattern...'); | ||
34 | |||
35 | [x,y] = ginput3(4); | ||
36 | |||
37 | [Xc,good,bad,type] = cornerfinder([x';y'],I,winty,wintx); % the four corners | ||
38 | |||
39 | x = Xc(1,:)'; | ||
40 | y = Xc(2,:)'; | ||
41 | |||
42 | [y,indy] = sort(y); | ||
43 | x = x(indy); | ||
44 | |||
45 | if (x(2) > x(1)), | ||
46 | x4 = x(1);y4 = y(1); x3 = x(2); y3 = y(2); | ||
47 | else | ||
48 | x4 = x(2);y4 = y(2); x3 = x(1); y3 = y(1); | ||
49 | end; | ||
50 | if (x(3) > x(4)), | ||
51 | x2 = x(3);y2 = y(3); x1 = x(4); y1 = y(4); | ||
52 | else | ||
53 | x2 = x(4);y2 = y(4); x1 = x(3); y1 = y(3); | ||
54 | end; | ||
55 | |||
56 | x = [x1;x2;x3;x4]; | ||
57 | y = [y1;y2;y3;y4]; | ||
58 | |||
59 | |||
60 | figure(2); hold on; | ||
61 | plot([x;x(1)],[y;y(1)],'g-'); | ||
62 | plot(x,y,'og'); | ||
63 | hx=text((x(4)+x(3))/2,(y(4)+y(3))/2 - 20,'X'); | ||
64 | set(hx,'color','g','Fontsize',14); | ||
65 | hy=text((x(4)+x(1))/2-20,(y(4)+y(1))/2,'Y'); | ||
66 | set(hy,'color','g','Fontsize',14); | ||
67 | hold off; | ||
68 | |||
69 | |||
70 | % Try to automatically count the number of squares in the grid | ||
71 | |||
72 | n_sq_x1 = count_squares(I,x1,y1,x2,y2,wintx); | ||
73 | n_sq_x2 = count_squares(I,x3,y3,x4,y4,wintx); | ||
74 | n_sq_y1 = count_squares(I,x2,y2,x3,y3,wintx); | ||
75 | n_sq_y2 = count_squares(I,x4,y4,x1,y1,wintx); | ||
76 | |||
77 | |||
78 | |||
79 | % If could not count the number of squares, enter manually | ||
80 | |||
81 | if (n_sq_x1~=n_sq_x2)|(n_sq_y1~=n_sq_y2), | ||
82 | |||
83 | |||
84 | disp('Could not count the number of squares in the grid. Enter manually.'); | ||
85 | n_sq_x = input('Number of squares along the X direction ([]=10) = '); %6 | ||
86 | if isempty(n_sq_x), n_sq_x = 10; end; | ||
87 | n_sq_y = input('Number of squares along the Y direction ([]=10) = '); %6 | ||
88 | if isempty(n_sq_y), n_sq_y = 10; end; | ||
89 | |||
90 | else | ||
91 | |||
92 | n_sq_x = n_sq_x1; | ||
93 | n_sq_y = n_sq_y1; | ||
94 | |||
95 | end; | ||
96 | |||
97 | if ~exist('dX')|~exist('dY'), | ||
98 | |||
99 | % Enter the size of each square | ||
100 | |||
101 | dX = input(['Size dX of each square along the X direction ([]=30mm) = ']); | ||
102 | dY = input(['Size dY of each square along the Y direction ([]=30mm) = ']); | ||
103 | if isempty(dX), dX = 30; end; | ||
104 | if isempty(dY), dY = 30; end; | ||
105 | |||
106 | end; | ||
107 | |||
108 | |||
109 | % Compute the inside points through computation of the planar homography (collineation) | ||
110 | |||
111 | a00 = [x(1);y(1);1]; | ||
112 | a10 = [x(2);y(2);1]; | ||
113 | a11 = [x(3);y(3);1]; | ||
114 | a01 = [x(4);y(4);1]; | ||
115 | |||
116 | |||
117 | % Compute the planart collineation: (return the normalization matrice as well) | ||
118 | |||
119 | [Homo,Hnorm,inv_Hnorm] = compute_homography ([a00 a10 a11 a01],[0 1 1 0;0 0 1 1;1 1 1 1]); | ||
120 | |||
121 | |||
122 | % Build the grid using the planar collineation: | ||
123 | |||
124 | x_l = ((0:n_sq_x)'*ones(1,n_sq_y+1))/n_sq_x; | ||
125 | y_l = (ones(n_sq_x+1,1)*(0:n_sq_y))/n_sq_y; | ||
126 | pts = [x_l(:) y_l(:) ones((n_sq_x+1)*(n_sq_y+1),1)]'; | ||
127 | |||
128 | XX = Homo*pts; | ||
129 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
130 | |||
131 | |||
132 | % Complete size of the rectangle | ||
133 | |||
134 | W = n_sq_x*dX; | ||
135 | L = n_sq_y*dY; | ||
136 | |||
137 | |||
138 | |||
139 | if nargin < 6, | ||
140 | |||
141 | %%%%%%%%%%%%%%%%%%%%%%%% ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
142 | figure(2); | ||
143 | hold on; | ||
144 | plot(XX(1,:),XX(2,:),'r+'); | ||
145 | title('The red crosses should be close to the image corners'); | ||
146 | hold off; | ||
147 | |||
148 | disp('If the guessed grid corners (red crosses on the image) are not close to the actual corners,'); | ||
149 | disp('it is necessary to enter an initial guess for the radial distortion factor kc (useful for subpixel detection)'); | ||
150 | quest_distort = input('Need of an initial guess for distortion? ([]=no, other=yes) '); | ||
151 | |||
152 | quest_distort = ~isempty(quest_distort); | ||
153 | |||
154 | if quest_distort, | ||
155 | % Estimation of focal length: | ||
156 | c_g = [size(I,2);size(I,1)]/2 + .5; | ||
157 | f_g = Distor2Calib(0,[[x(1) x(2) x(4) x(3)] - c_g(1);[y(1) y(2) y(4) y(3)] - c_g(2)],1,1,4,W,L,[-W/2 W/2 W/2 -W/2;L/2 L/2 -L/2 -L/2; 0 0 0 0],100,1,1); | ||
158 | f_g = mean(f_g); | ||
159 | script_fit_distortion; | ||
160 | end; | ||
161 | %%%%%%%%%%%%%%%%%%%%% END ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
162 | |||
163 | else | ||
164 | |||
165 | xy_corners_undist = comp_distortion_oulu([(x' - cc(1))/fc(1);(y'-cc(2))/fc(1)],kc); | ||
166 | |||
167 | xu = xy_corners_undist(1,:)'; | ||
168 | yu = xy_corners_undist(2,:)'; | ||
169 | |||
170 | [XXu] = projectedGrid ( [xu(1);yu(1)], [xu(2);yu(2)],[xu(3);yu(3)], [xu(4);yu(4)],n_sq_x+1,n_sq_y+1); % The full grid | ||
171 | |||
172 | r2 = sum(XXu.^2); | ||
173 | XX = (ones(2,1)*(1 + kc(1) * r2 + kc(2) * (r2.^2))) .* XXu; | ||
174 | XX(1,:) = fc(1)*XX(1,:)+cc(1); | ||
175 | XX(2,:) = fc(2)*XX(2,:)+cc(2); | ||
176 | |||
177 | end; | ||
178 | |||
179 | |||
180 | Np = (n_sq_x+1)*(n_sq_y+1); | ||
181 | |||
182 | disp('Corner extraction...'); | ||
183 | |||
184 | grid_pts = cornerfinder(XX,I,winty,wintx); %%% Finds the exact corners at every points! | ||
185 | |||
186 | grid_pts = grid_pts - 1; % subtract 1 to bring the origin to (0,0) instead of (1,1) in matlab (not necessary in C) | ||
187 | |||
188 | ind_corners = [1 n_sq_x+1 (n_sq_x+1)*n_sq_y+1 (n_sq_x+1)*(n_sq_y+1)]; % index of the 4 corners | ||
189 | ind_orig = (n_sq_x+1)*n_sq_y + 1; | ||
190 | xorig = grid_pts(1,ind_orig); | ||
191 | yorig = grid_pts(2,ind_orig); | ||
192 | dxpos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig+1)]'); | ||
193 | dypos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig-n_sq_x-1)]'); | ||
194 | |||
195 | |||
196 | ind_x = (n_sq_x+1)*(n_sq_y + 1); | ||
197 | ind_y = 1; | ||
198 | |||
199 | x_box_kk = [grid_pts(1,:)-(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)-(wintx+.5);grid_pts(1,:)-(wintx+.5)]; | ||
200 | y_box_kk = [grid_pts(2,:)-(winty+.5);grid_pts(2,:)-(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)-(winty+.5)]; | ||
201 | |||
202 | |||
203 | figure(3); | ||
204 | image(Id); colormap(map); hold on; | ||
205 | plot(grid_pts(1,:)+1,grid_pts(2,:)+1,'r+'); | ||
206 | plot(x_box_kk+1,y_box_kk+1,'-b'); | ||
207 | plot(grid_pts(1,ind_corners)+1,grid_pts(2,ind_corners)+1,'mo'); | ||
208 | plot(xorig+1,yorig+1,'*m'); | ||
209 | h = text(xorig-15,yorig-15,'O'); | ||
210 | set(h,'Color','m','FontSize',14); | ||
211 | h2 = text(dxpos(1)-10,dxpos(2)-10,'dX'); | ||
212 | set(h2,'Color','g','FontSize',14); | ||
213 | h3 = text(dypos(1)-25,dypos(2)-3,'dY'); | ||
214 | set(h3,'Color','g','FontSize',14); | ||
215 | xlabel('Xc (in camera frame)'); | ||
216 | ylabel('Yc (in camera frame)'); | ||
217 | title('Extracted corners'); | ||
218 | zoom on; | ||
219 | drawnow; | ||
220 | hold off; | ||
221 | |||
222 | |||
223 | Xi = reshape(([0:n_sq_x]*dX)'*ones(1,n_sq_y+1),Np,1)'; | ||
224 | Yi = reshape(ones(n_sq_x+1,1)*[n_sq_y:-1:0]*dY,Np,1)'; | ||
225 | Zi = zeros(1,Np); | ||
226 | |||
227 | Xgrid = [Xi;Yi;Zi]; | ||
228 | |||
229 | |||
230 | % All the point coordinates (on the image, and in 3D) - for global optimization: | ||
231 | |||
232 | x = grid_pts; | ||
233 | X = Xgrid; | ||
234 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extract_parameters.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extract_parameters.m new file mode 100755 index 0000000..035b97d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extract_parameters.m | |||
@@ -0,0 +1,46 @@ | |||
1 | |||
2 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
3 | |||
4 | check_active_images; | ||
5 | |||
6 | fc = solution(1:2);%*** | ||
7 | cc = solution(3:4);%*** | ||
8 | alpha_c = solution(5);%*** | ||
9 | kc = solution(6:9);%*** | ||
10 | |||
11 | |||
12 | % Calibration matrix: | ||
13 | |||
14 | KK = [fc(1) fc(1)*alpha_c cc(1);0 fc(2) cc(2); 0 0 1]; | ||
15 | inv_KK = inv(KK); | ||
16 | |||
17 | % Extract the extrinsic paramters, and recomputer the collineations | ||
18 | |||
19 | for kk = 1:n_ima, | ||
20 | |||
21 | if active_images(kk), | ||
22 | |||
23 | omckk = solution(15+6*(kk-1) + 1:15+6*(kk-1) + 3);%*** | ||
24 | Tckk = solution(15+6*(kk-1) + 4:15+6*(kk-1) + 6);%*** | ||
25 | |||
26 | Rckk = rodrigues(omckk); | ||
27 | |||
28 | Hkk = KK * [Rckk(:,1) Rckk(:,2) Tckk]; | ||
29 | |||
30 | Hkk = Hkk / Hkk(3,3); | ||
31 | |||
32 | else | ||
33 | |||
34 | omckk = NaN*ones(3,1); | ||
35 | Tckk = NaN*ones(3,1); | ||
36 | Rckk = NaN*ones(3,3); | ||
37 | Hkk = NaN*ones(3,3); | ||
38 | |||
39 | end; | ||
40 | |||
41 | eval(['omc_' num2str(kk) ' = omckk;']); | ||
42 | eval(['Rc_' num2str(kk) ' = Rckk;']); | ||
43 | eval(['Tc_' num2str(kk) ' = Tckk;']); | ||
44 | eval(['H_' num2str(kk) '= Hkk;']); | ||
45 | |||
46 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extract_parameters3D.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extract_parameters3D.m new file mode 100755 index 0000000..841c6ab --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extract_parameters3D.m | |||
@@ -0,0 +1,36 @@ | |||
1 | |||
2 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
3 | |||
4 | |||
5 | fc = solution(1:2); | ||
6 | kc = solution(3:6); | ||
7 | cc = solution(6*n_ima + 4 +3:6*n_ima + 5 +3); | ||
8 | |||
9 | % Calibration matrix: | ||
10 | |||
11 | KK = [fc(1) 0 cc(1);0 fc(2) cc(2); 0 0 1]; | ||
12 | inv_KK = inv(KK); | ||
13 | |||
14 | % Extract the extrinsic paramters, and recomputer the collineations | ||
15 | |||
16 | for kk = 1:n_ima, | ||
17 | |||
18 | omckk = solution(4+6*(kk-1) + 3:6*kk + 3); | ||
19 | |||
20 | Tckk = solution(6*kk+1 + 3:6*kk+3 + 3); | ||
21 | |||
22 | Rckk = rodrigues(omckk); | ||
23 | |||
24 | Hlkk = KK * [Rckk(:,1) Rckk(:,2) Tckk]; | ||
25 | |||
26 | Hlkk = Hlkk / Hlkk(3,3); | ||
27 | |||
28 | eval(['omc_' num2str(kk) ' = omckk;']); | ||
29 | eval(['Rc_' num2str(kk) ' = Rckk;']); | ||
30 | eval(['Tc_' num2str(kk) ' = Tckk;']); | ||
31 | |||
32 | eval(['Hl_' num2str(kk) '=Hlkk;']); | ||
33 | |||
34 | end; | ||
35 | |||
36 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extrinsic_computation.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extrinsic_computation.m new file mode 100755 index 0000000..fbba78e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/extrinsic_computation.m | |||
@@ -0,0 +1,185 @@ | |||
1 | %%% INPUT THE IMAGE FILE NAME: | ||
2 | |||
3 | if ~exist('fc')|~exist('cc')|~exist('kc')|~exist('alpha_c'), | ||
4 | fprintf(1,'No intrinsic camera parameters available.\n'); | ||
5 | return; | ||
6 | end; | ||
7 | |||
8 | dir; | ||
9 | |||
10 | fprintf(1,'\n'); | ||
11 | disp('Computation of the extrinsic parameters from an image of a pattern'); | ||
12 | disp('The intrinsic camera parameters are assumed to be known (previously computed)'); | ||
13 | |||
14 | fprintf(1,'\n'); | ||
15 | image_name = input('Image name (full name without extension): ','s'); | ||
16 | |||
17 | format_image2 = '0'; | ||
18 | |||
19 | while format_image2 == '0', | ||
20 | |||
21 | format_image2 = input('Image format: ([]=''r''=''ras'', ''b''=''bmp'', ''t''=''tif'', ''p''=''pgm'', ''j''=''jpg'', ''m''=''ppm'') ','s'); | ||
22 | |||
23 | if isempty(format_image2), | ||
24 | format_image2 = 'ras'; | ||
25 | end; | ||
26 | |||
27 | if lower(format_image2(1)) == 'm', | ||
28 | format_image2 = 'ppm'; | ||
29 | else | ||
30 | if lower(format_image2(1)) == 'b', | ||
31 | format_image2 = 'bmp'; | ||
32 | else | ||
33 | if lower(format_image2(1)) == 't', | ||
34 | format_image2 = 'tif'; | ||
35 | else | ||
36 | if lower(format_image2(1)) == 'p', | ||
37 | format_image2 = 'pgm'; | ||
38 | else | ||
39 | if lower(format_image2(1)) == 'j', | ||
40 | format_image2 = 'jpg'; | ||
41 | else | ||
42 | if lower(format_image2(1)) == 'r', | ||
43 | format_image2 = 'ras'; | ||
44 | else | ||
45 | disp('Invalid image format'); | ||
46 | format_image2 = '0'; % Ask for format once again | ||
47 | end; | ||
48 | end; | ||
49 | end; | ||
50 | end; | ||
51 | end; | ||
52 | end; | ||
53 | end; | ||
54 | |||
55 | ima_name = [image_name '.' format_image2]; | ||
56 | |||
57 | |||
58 | %%% READ IN IMAGE: | ||
59 | |||
60 | if format_image2(1) == 'p', | ||
61 | if format_image2(2) == 'p', | ||
62 | I = double(loadppm(ima_name)); | ||
63 | else | ||
64 | I = double(loadpgm(ima_name)); | ||
65 | end; | ||
66 | else | ||
67 | if format_image2(1) == 'r', | ||
68 | I = readras(ima_name); | ||
69 | else | ||
70 | I = double(imread(ima_name)); | ||
71 | end; | ||
72 | end; | ||
73 | |||
74 | if size(I,3)>1, | ||
75 | I = I(:,:,2); | ||
76 | end; | ||
77 | |||
78 | |||
79 | %%% EXTRACT GRID CORNERS: | ||
80 | |||
81 | fprintf(1,'\nExtraction of the grid corners on the image\n'); | ||
82 | |||
83 | disp('Window size for corner finder (wintx and winty):'); | ||
84 | wintx = input('wintx ([] = 5) = '); | ||
85 | if isempty(wintx), wintx = 5; end; | ||
86 | wintx = round(wintx); | ||
87 | winty = input('winty ([] = 5) = '); | ||
88 | if isempty(winty), winty = 5; end; | ||
89 | winty = round(winty); | ||
90 | |||
91 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
92 | |||
93 | [x_ext,X_ext,n_sq_x,n_sq_y,ind_orig,ind_x,ind_y] = extract_grid(I,wintx,winty,fc,cc,kc); | ||
94 | |||
95 | |||
96 | |||
97 | %%% Computation of the Extrinsic Parameters attached to the grid: | ||
98 | |||
99 | [omc_ext,Tc_ext,Rc_ext,H_ext] = compute_extrinsic(x_ext,X_ext,fc,cc,kc,alpha_c); | ||
100 | |||
101 | |||
102 | %%% Reproject the points on the image: | ||
103 | |||
104 | [x_reproj] = project_points2(X_ext,omc_ext,Tc_ext,fc,cc,kc,alpha_c); | ||
105 | |||
106 | err_reproj = x_ext - x_reproj; | ||
107 | |||
108 | err_std2 = std(err_reproj')'; | ||
109 | |||
110 | |||
111 | Basis = [X_ext(:,[ind_orig ind_x ind_orig ind_y ind_orig ])]; | ||
112 | |||
113 | VX = Basis(:,2) - Basis(:,1); | ||
114 | VY = Basis(:,4) - Basis(:,1); | ||
115 | |||
116 | nX = norm(VX); | ||
117 | nY = norm(VY); | ||
118 | |||
119 | VZ = min(nX,nY) * cross(VX/nX,VY/nY); | ||
120 | |||
121 | Basis = [Basis VZ]; | ||
122 | |||
123 | [x_basis] = project_points2(Basis,omc_ext,Tc_ext,fc,cc,kc,alpha_c); | ||
124 | |||
125 | dxpos = (x_basis(:,2) + x_basis(:,1))/2; | ||
126 | dypos = (x_basis(:,4) + x_basis(:,3))/2; | ||
127 | dzpos = (x_basis(:,6) + x_basis(:,5))/2; | ||
128 | |||
129 | |||
130 | |||
131 | figure(2); | ||
132 | image(I); | ||
133 | colormap(gray(256)); | ||
134 | hold on; | ||
135 | plot(x_ext(1,:)+1,x_ext(2,:)+1,'r+'); | ||
136 | plot(x_reproj(1,:)+1,x_reproj(2,:)+1,'yo'); | ||
137 | h = text(x_ext(1,ind_orig)-25,x_ext(2,ind_orig)-25,'O'); | ||
138 | set(h,'Color','g','FontSize',14); | ||
139 | h2 = text(dxpos(1)+1,dxpos(2)-30,'X'); | ||
140 | set(h2,'Color','g','FontSize',14); | ||
141 | h3 = text(dypos(1)-30,dypos(2)+1,'Y'); | ||
142 | set(h3,'Color','g','FontSize',14); | ||
143 | h4 = text(dzpos(1)-10,dzpos(2)-20,'Z'); | ||
144 | set(h4,'Color','g','FontSize',14); | ||
145 | plot(x_basis(1,:)+1,x_basis(2,:)+1,'g-','linewidth',2); | ||
146 | title('Image points (+) and reprojected grid points (o)'); | ||
147 | hold off; | ||
148 | |||
149 | |||
150 | fprintf(1,'\n\nExtrinsic parameters:\n\n'); | ||
151 | fprintf(1,'Translation vector: Tc_ext = [ %3.6f \t %3.6f \t %3.6f ]\n',Tc_ext); | ||
152 | fprintf(1,'Rotation vector: omc_ext = [ %3.6f \t %3.6f \t %3.6f ]\n',omc_ext); | ||
153 | fprintf(1,'Rotation matrix: Rc_ext = [ %3.6f \t %3.6f \t %3.6f\n',Rc_ext(1,:)'); | ||
154 | fprintf(1,' %3.6f \t %3.6f \t %3.6f\n',Rc_ext(2,:)'); | ||
155 | fprintf(1,' %3.6f \t %3.6f \t %3.6f ]\n',Rc_ext(3,:)'); | ||
156 | fprintf(1,'Pixel error: err = [ %3.5f \t %3.5f ]\n\n',err_std2); | ||
157 | |||
158 | |||
159 | |||
160 | |||
161 | |||
162 | return; | ||
163 | |||
164 | |||
165 | % Stores the results: | ||
166 | |||
167 | kk = 1; | ||
168 | |||
169 | % Stores location of grid wrt camera: | ||
170 | |||
171 | eval(['omc_' num2str(kk) ' = omc_ext;']); | ||
172 | eval(['Tc_' num2str(kk) ' = Tc_ext;']); | ||
173 | |||
174 | % Stores the projected points: | ||
175 | |||
176 | eval(['y_' num2str(kk) ' = x_reproj;']); | ||
177 | eval(['X_' num2str(kk) ' = X_ext;']); | ||
178 | eval(['x_' num2str(kk) ' = x_ext;']); | ||
179 | |||
180 | |||
181 | % Organize the points in a grid: | ||
182 | |||
183 | eval(['n_sq_x_' num2str(kk) ' = n_sq_x;']); | ||
184 | eval(['n_sq_y_' num2str(kk) ' = n_sq_y;']); | ||
185 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/fixallvariables.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/fixallvariables.m new file mode 100755 index 0000000..b5808f3 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/fixallvariables.m | |||
@@ -0,0 +1,19 @@ | |||
1 | % Code that clears all empty or NaN variables | ||
2 | |||
3 | varlist = whos; | ||
4 | |||
5 | if ~isempty(varlist), | ||
6 | |||
7 | Nvar = size(varlist,1); | ||
8 | |||
9 | for c_var = 1:Nvar, | ||
10 | |||
11 | var2fix = varlist(c_var).name; | ||
12 | |||
13 | fixvariable; | ||
14 | |||
15 | end; | ||
16 | |||
17 | end; | ||
18 | |||
19 | clear varlist var2fix Nvar c_var \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/fixvariable.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/fixvariable.m new file mode 100755 index 0000000..2213431 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/fixvariable.m | |||
@@ -0,0 +1,18 @@ | |||
1 | % Code that clears an empty variable, or a NaN vsriable. | ||
2 | % Does not clear structures, or cells. | ||
3 | |||
4 | if exist('var2fix'), | ||
5 | if eval(['exist(''' var2fix ''') == 1']), | ||
6 | if eval(['isempty(' var2fix ')']), | ||
7 | eval(['clear ' var2fix ]); | ||
8 | else | ||
9 | if eval(['~isstruct(' var2fix ')']), | ||
10 | if eval(['~iscell(' var2fix ')']), | ||
11 | if eval(['isnan(' var2fix '(1))']), | ||
12 | eval(['clear ' var2fix ]); | ||
13 | end; | ||
14 | end; | ||
15 | end; | ||
16 | end; | ||
17 | end; | ||
18 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/ginput3.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/ginput3.m new file mode 100755 index 0000000..56fe496 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/ginput3.m | |||
@@ -0,0 +1,216 @@ | |||
1 | function [out1,out2,out3] = ginput2(arg1) | ||
2 | %GINPUT Graphical input from mouse. | ||
3 | % [X,Y] = GINPUT(N) gets N points from the current axes and returns | ||
4 | % the X- and Y-coordinates in length N vectors X and Y. The cursor | ||
5 | % can be positioned using a mouse (or by using the Arrow Keys on some | ||
6 | % systems). Data points are entered by pressing a mouse button | ||
7 | % or any key on the keyboard except carriage return, which terminates | ||
8 | % the input before N points are entered. | ||
9 | % | ||
10 | % [X,Y] = GINPUT gathers an unlimited number of points until the | ||
11 | % return key is pressed. | ||
12 | % | ||
13 | % [X,Y,BUTTON] = GINPUT(N) returns a third result, BUTTON, that | ||
14 | % contains a vector of integers specifying which mouse button was | ||
15 | % used (1,2,3 from left) or ASCII numbers if a key on the keyboard | ||
16 | % was used. | ||
17 | |||
18 | % Copyright (c) 1984-96 by The MathWorks, Inc. | ||
19 | % $Revision: 5.18 $ $Date: 1996/11/10 17:48:08 $ | ||
20 | |||
21 | % Fixed version by Jean-Yves Bouguet to have a cross instead of 2 lines | ||
22 | % More visible for images | ||
23 | |||
24 | P = NaN*ones(16,16); | ||
25 | P(1:15,1:15) = 2*ones(15,15); | ||
26 | P(2:14,2:14) = ones(13,13); | ||
27 | P(3:13,3:13) = NaN*ones(11,11); | ||
28 | P(6:10,6:10) = 2*ones(5,5); | ||
29 | P(7:9,7:9) = 1*ones(3,3); | ||
30 | |||
31 | out1 = []; out2 = []; out3 = []; y = []; | ||
32 | c = computer; | ||
33 | if ~strcmp(c(1:2),'PC') & ~strcmp(c(1:2),'MA') | ||
34 | tp = get(0,'TerminalProtocol'); | ||
35 | else | ||
36 | tp = 'micro'; | ||
37 | end | ||
38 | |||
39 | if ~strcmp(tp,'none') & ~strcmp(tp,'x') & ~strcmp(tp,'micro'), | ||
40 | if nargout == 1, | ||
41 | if nargin == 1, | ||
42 | eval('out1 = trmginput(arg1);'); | ||
43 | else | ||
44 | eval('out1 = trmginput;'); | ||
45 | end | ||
46 | elseif nargout == 2 | nargout == 0, | ||
47 | if nargin == 1, | ||
48 | eval('[out1,out2] = trmginput(arg1);'); | ||
49 | else | ||
50 | eval('[out1,out2] = trmginput;'); | ||
51 | end | ||
52 | if nargout == 0 | ||
53 | out1 = [ out1 out2 ]; | ||
54 | end | ||
55 | elseif nargout == 3, | ||
56 | if nargin == 1, | ||
57 | eval('[out1,out2,out3] = trmginput(arg1);'); | ||
58 | else | ||
59 | eval('[out1,out2,out3] = trmginput;'); | ||
60 | end | ||
61 | end | ||
62 | else | ||
63 | |||
64 | fig = gcf; | ||
65 | figure(gcf); | ||
66 | |||
67 | if nargin == 0 | ||
68 | how_many = -1; | ||
69 | b = []; | ||
70 | else | ||
71 | how_many = arg1; | ||
72 | b = []; | ||
73 | if isstr(how_many) ... | ||
74 | | size(how_many,1) ~= 1 | size(how_many,2) ~= 1 ... | ||
75 | | ~(fix(how_many) == how_many) ... | ||
76 | | how_many < 0 | ||
77 | error('Requires a positive integer.') | ||
78 | end | ||
79 | if how_many == 0 | ||
80 | ptr_fig = 0; | ||
81 | while(ptr_fig ~= fig) | ||
82 | ptr_fig = get(0,'PointerWindow'); | ||
83 | end | ||
84 | scrn_pt = get(0,'PointerLocation'); | ||
85 | loc = get(fig,'Position'); | ||
86 | pt = [scrn_pt(1) - loc(1), scrn_pt(2) - loc(2)]; | ||
87 | out1 = pt(1); y = pt(2); | ||
88 | elseif how_many < 0 | ||
89 | error('Argument must be a positive integer.') | ||
90 | end | ||
91 | end | ||
92 | |||
93 | pointer = get(gcf,'pointer'); | ||
94 | |||
95 | set(gcf,'Pointer','custom','PointerShapeCData',P,'PointerShapeHotSpot',[8,8]); | ||
96 | %set(gcf,'pointer','crosshair'); | ||
97 | fig_units = get(fig,'units'); | ||
98 | char = 0; | ||
99 | |||
100 | while how_many ~= 0 | ||
101 | % Use no-side effect WAITFORBUTTONPRESS | ||
102 | waserr = 0; | ||
103 | eval('keydown = wfbp;', 'waserr = 1;'); | ||
104 | if(waserr == 1) | ||
105 | if(ishandle(fig)) | ||
106 | set(fig,'pointer',pointer,'units',fig_units); | ||
107 | error('Interrupted'); | ||
108 | else | ||
109 | error('Interrupted by figure deletion'); | ||
110 | end | ||
111 | end | ||
112 | |||
113 | ptr_fig = get(0,'CurrentFigure'); | ||
114 | if(ptr_fig == fig) | ||
115 | if keydown | ||
116 | char = get(fig, 'CurrentCharacter'); | ||
117 | button = abs(get(fig, 'CurrentCharacter')); | ||
118 | scrn_pt = get(0, 'PointerLocation'); | ||
119 | set(fig,'units','pixels') | ||
120 | loc = get(fig, 'Position'); | ||
121 | pt = [scrn_pt(1) - loc(1), scrn_pt(2) - loc(2)]; | ||
122 | set(fig,'CurrentPoint',pt); | ||
123 | else | ||
124 | button = get(fig, 'SelectionType'); | ||
125 | if strcmp(button,'open') | ||
126 | button = b(length(b)); | ||
127 | elseif strcmp(button,'normal') | ||
128 | button = 1; | ||
129 | elseif strcmp(button,'extend') | ||
130 | button = 2; | ||
131 | elseif strcmp(button,'alt') | ||
132 | button = 3; | ||
133 | else | ||
134 | error('Invalid mouse selection.') | ||
135 | end | ||
136 | end | ||
137 | pt = get(gca, 'CurrentPoint'); | ||
138 | |||
139 | how_many = how_many - 1; | ||
140 | |||
141 | if(char == 13) % & how_many ~= 0) | ||
142 | % if the return key was pressed, char will == 13, | ||
143 | % and that's our signal to break out of here whether | ||
144 | % or not we have collected all the requested data | ||
145 | % points. | ||
146 | % If this was an early breakout, don't include | ||
147 | % the <Return> key info in the return arrays. | ||
148 | % We will no longer count it if it's the last input. | ||
149 | break; | ||
150 | end | ||
151 | |||
152 | out1 = [out1;pt(1,1)]; | ||
153 | y = [y;pt(1,2)]; | ||
154 | b = [b;button]; | ||
155 | end | ||
156 | end | ||
157 | |||
158 | set(fig,'pointer',pointer,'units',fig_units); | ||
159 | |||
160 | if nargout > 1 | ||
161 | out2 = y; | ||
162 | if nargout > 2 | ||
163 | out3 = b; | ||
164 | end | ||
165 | else | ||
166 | out1 = [out1 y]; | ||
167 | end | ||
168 | |||
169 | end | ||
170 | |||
171 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
172 | function key = wfbp | ||
173 | %WFBP Replacement for WAITFORBUTTONPRESS that has no side effects. | ||
174 | |||
175 | % Remove figure button functions | ||
176 | fprops = {'windowbuttonupfcn','buttondownfcn', ... | ||
177 | 'windowbuttondownfcn','windowbuttonmotionfcn'}; | ||
178 | fig = gcf; | ||
179 | fvals = get(fig,fprops); | ||
180 | set(fig,fprops,{'','','',''}) | ||
181 | |||
182 | % Remove all other buttondown functions | ||
183 | ax = findobj(fig,'type','axes'); | ||
184 | if isempty(ax) | ||
185 | ch = {}; | ||
186 | else | ||
187 | ch = get(ax,{'Children'}); | ||
188 | end | ||
189 | for i=1:length(ch), | ||
190 | ch{i} = ch{i}(:)'; | ||
191 | end | ||
192 | h = [ax(:)',ch{:}]; | ||
193 | vals = get(h,{'buttondownfcn'}); | ||
194 | mt = repmat({''},size(vals)); | ||
195 | set(h,{'buttondownfcn'},mt); | ||
196 | |||
197 | % Now wait for that buttonpress, and check for error conditions | ||
198 | waserr = 0; | ||
199 | eval(['if nargout==0,', ... | ||
200 | ' waitforbuttonpress,', ... | ||
201 | 'else,', ... | ||
202 | ' keydown = waitforbuttonpress;',... | ||
203 | 'end' ], 'waserr = 1;'); | ||
204 | |||
205 | % Put everything back | ||
206 | if(ishandle(fig)) | ||
207 | set(fig,fprops,fvals) | ||
208 | set(h,{'buttondownfcn'},vals) | ||
209 | end | ||
210 | |||
211 | if(waserr == 1) | ||
212 | error('Interrupted'); | ||
213 | end | ||
214 | |||
215 | if nargout>0, key = keydown; end | ||
216 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/go_calib_optim.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/go_calib_optim.m new file mode 100755 index 0000000..ad19f64 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/go_calib_optim.m | |||
@@ -0,0 +1,139 @@ | |||
1 | %go_calib_optim | ||
2 | % | ||
3 | %Main calibration function. Computes the intrinsic andextrinsic parameters. | ||
4 | %Runs as a script. | ||
5 | % | ||
6 | %INPUT: x_1,x_2,x_3,...: Feature locations on the images | ||
7 | % X_1,X_2,X_3,...: Corresponding grid coordinates | ||
8 | % | ||
9 | %OUTPUT: fc: Camera focal length | ||
10 | % cc: Principal point coordinates | ||
11 | % kc: Distortion coefficients | ||
12 | % KK: The camera matrix (containing fc and cc) | ||
13 | % omc_1,omc_2,omc_3,...: 3D rotation vectors attached to the grid positions in space | ||
14 | % Tc_1,Tc_2,Tc_3,...: 3D translation vectors attached to the grid positions in space | ||
15 | % Rc_1,Rc_2,Rc_3,...: 3D rotation matrices corresponding to the omc vectors | ||
16 | % | ||
17 | %Method: Minimizes the pixel reprojection error in the least squares sense over the intrinsic | ||
18 | % camera parameters, and the extrinsic parameters (3D locations of the grids in space) | ||
19 | % | ||
20 | %Note: If the intrinsic camera parameters (fc, cc, kc) do not exist before, they are initialized through | ||
21 | % the function init_intrinsic_param.m. Otherwise, the variables in memory are used as initial guesses. | ||
22 | % | ||
23 | %Note: The row vector active_images consists of zeros and ones. To deactivate an image, set the | ||
24 | % corresponding entry in the active_images vector to zero. | ||
25 | % | ||
26 | %VERY IMPORTANT: This function works for 2D and 3D calibration rigs, except for init_intrinsic_param.m | ||
27 | %that is so far implemented to work only with 2D rigs. | ||
28 | %In the future, a more general function will be there. | ||
29 | %For now, if using a 3D calibration rig, set quick_init to 1 for an easy initialization of the focal length | ||
30 | |||
31 | |||
32 | if ~exist('n_ima'), | ||
33 | data_calib; % Load the images | ||
34 | click_calib; % Extract the corners | ||
35 | end; | ||
36 | |||
37 | |||
38 | check_active_images; | ||
39 | |||
40 | check_extracted_images; | ||
41 | |||
42 | check_active_images; | ||
43 | |||
44 | |||
45 | desactivated_images = []; | ||
46 | |||
47 | |||
48 | if ~exist('center_optim'), | ||
49 | center_optim = 1; %%% Set this variable to 0 if your do not want to estimate the principal point | ||
50 | %%% Required when using one image, and VERY RECOMMENDED WHEN USING LESS THAN 4 images | ||
51 | end; | ||
52 | |||
53 | % Check 3D-ness of the calibration rig: | ||
54 | rig3D = 0; | ||
55 | for kk = ind_active, | ||
56 | eval(['X_kk = X_' num2str(kk) ';']); | ||
57 | if is3D(X_kk), | ||
58 | rig3D = 1; | ||
59 | end; | ||
60 | end; | ||
61 | |||
62 | |||
63 | if center_optim & (length(ind_active) < 2) & ~rig3D, | ||
64 | fprintf(1,'\nPrincipal point rejected from the optimization when using one image and planar rig (center_optim = 1).\n'); | ||
65 | center_optim = 0; %%% when using a single image, please, no principal point estimation!!! | ||
66 | est_alpha = 0; | ||
67 | end; | ||
68 | |||
69 | if ~exist('dont_ask'), | ||
70 | dont_ask = 0; | ||
71 | end; | ||
72 | |||
73 | if center_optim & (length(ind_active) < 5), | ||
74 | fprintf(1,'\nThe principal point estimation may be unreliable (using less than 5 images for calibration).\n'); | ||
75 | if ~dont_ask, | ||
76 | quest = input('Are you sure you want to keep the principal point in the optimization process? ([]=yes, other=no) '); | ||
77 | center_optim = isempty(quest); | ||
78 | end; | ||
79 | end; | ||
80 | |||
81 | if center_optim, | ||
82 | fprintf(1,'\nINFO: To reject the principal point from the optimization, set center_optim = 0 in go_calib_optim.m\n'); | ||
83 | end; | ||
84 | |||
85 | if ~exist('est_alpha'), | ||
86 | est_alpha = 0; % by default, do not estimate skew | ||
87 | end; | ||
88 | |||
89 | if ~center_optim & (est_alpha), | ||
90 | fprintf(1,'WARNING: Since there is no principal point estimation, no skew estimation (est_alpha = 0)\n'); | ||
91 | est_alpha = 0; | ||
92 | else | ||
93 | if ~est_alpha, | ||
94 | fprintf(1,'WARNING: Skew not optimized. Check variable est_alpha.\n'); | ||
95 | alpha_c = 0; | ||
96 | else | ||
97 | fprintf(1,'WARNING: Skew is optimized. To disable skew estimation, set est_alpha=0.\n'); | ||
98 | end; | ||
99 | end; | ||
100 | |||
101 | |||
102 | if ~exist('est_dist'); | ||
103 | est_dist = [1;1;1;1]; | ||
104 | end; | ||
105 | if ~prod(est_dist), | ||
106 | fprintf(1,'\nWARNING: Distortion not fully estimated. Check variable est_dist.\n'); | ||
107 | end; | ||
108 | |||
109 | |||
110 | |||
111 | |||
112 | %%% MAIN OPTIMIZATION CALL!!!!! (look into this function for the details of implementation) | ||
113 | go_calib_optim_iter; | ||
114 | |||
115 | |||
116 | |||
117 | if ~isempty(desactivated_images), | ||
118 | |||
119 | param_list_save = param_list; | ||
120 | |||
121 | fprintf(1,'\nNew optimization including the images that have been deactivated during the previous optimization.\n'); | ||
122 | active_images(desactivated_images) = ones(1,length(desactivated_images)); | ||
123 | desactivated_images = []; | ||
124 | |||
125 | go_calib_optim_iter; | ||
126 | |||
127 | if ~isempty(desactivated_images), | ||
128 | fprintf(1,['List of images left desactivated: ' num2str(desactivated_images) '\n' ] ); | ||
129 | end; | ||
130 | |||
131 | param_list = [param_list_save(:,1:end-1) param_list]; | ||
132 | |||
133 | end; | ||
134 | |||
135 | |||
136 | %%%%%%%%%%%%%%%%%%%% GRAPHICAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%% | ||
137 | |||
138 | %graphout_calib; | ||
139 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/go_calib_optim_iter.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/go_calib_optim_iter.m new file mode 100755 index 0000000..e3d22f6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/go_calib_optim_iter.m | |||
@@ -0,0 +1,394 @@ | |||
1 | %go_calib_optim_iter | ||
2 | % | ||
3 | %Main calibration function. Computes the intrinsic andextrinsic parameters. | ||
4 | %Runs as a script. | ||
5 | % | ||
6 | %INPUT: x_1,x_2,x_3,...: Feature locations on the images | ||
7 | % X_1,X_2,X_3,...: Corresponding grid coordinates | ||
8 | % | ||
9 | %OUTPUT: fc: Camera focal length | ||
10 | % cc: Principal point coordinates | ||
11 | % kc: Distortion coefficients | ||
12 | % KK: The camera matrix (containing fc and cc) | ||
13 | % omc_1,omc_2,omc_3,...: 3D rotation vectors attached to the grid positions in space | ||
14 | % Tc_1,Tc_2,Tc_3,...: 3D translation vectors attached to the grid positions in space | ||
15 | % Rc_1,Rc_2,Rc_3,...: 3D rotation matrices corresponding to the omc vectors | ||
16 | % | ||
17 | %Method: Minimizes the pixel reprojection error in the least squares sense over the intrinsic | ||
18 | % camera parameters, and the extrinsic parameters (3D locations of the grids in space) | ||
19 | % | ||
20 | %Note: If the intrinsic camera parameters (fc, cc, kc) do not exist before, they are initialized through | ||
21 | % the function init_intrinsic_param.m. Otherwise, the variables in memory are used as initial guesses. | ||
22 | % | ||
23 | %Note: The row vector active_images consists of zeros and ones. To deactivate an image, set the | ||
24 | % corresponding entry in the active_images vector to zero. | ||
25 | % | ||
26 | %VERY IMPORTANT: This function works for 2D and 3D calibration rigs, except for init_intrinsic_param.m | ||
27 | %that is so far implemented to work only with 2D rigs. | ||
28 | %In the future, a more general function will be there. | ||
29 | %For now, if using a 3D calibration rig, quick_init is set to 1 for an easy initialization of the focal length | ||
30 | |||
31 | |||
32 | if ~exist('center_optim'), | ||
33 | center_optim = 1; %%% Set this variable to 0 if your do not want to estimate the principal point | ||
34 | end; | ||
35 | |||
36 | if ~exist('est_dist'), | ||
37 | est_dist = [1;1;1;1]; | ||
38 | end; | ||
39 | |||
40 | if ~exist('est_alpha'), | ||
41 | est_alpha = 0; % by default, do not estimate skew | ||
42 | end; | ||
43 | |||
44 | |||
45 | % Little fix in case of stupid values in the binary variables: | ||
46 | center_optim = ~~center_optim; | ||
47 | est_alpha = ~~est_alpha; | ||
48 | est_dist = ~~est_dist; | ||
49 | |||
50 | |||
51 | if ~exist('nx')&~exist('ny'), | ||
52 | fprintf(1,'WARNING: No image size (nx,ny) available. Setting nx=640 and ny=480\n'); | ||
53 | nx = 640; | ||
54 | ny = 480; | ||
55 | end; | ||
56 | |||
57 | |||
58 | check_active_images; | ||
59 | |||
60 | |||
61 | quick_init = 0; % Set to 1 for using a quick init (necessary when using 3D rigs) | ||
62 | |||
63 | |||
64 | if ~center_optim, % In the case where the principal point is not estimated, keep it at the center of the image | ||
65 | fprintf(1,'Principal point not optimized (center_optim=0). It is kept at the center of the image.\n'); | ||
66 | cc = [(nx-1)/2;(ny-1)/2]; | ||
67 | end; | ||
68 | |||
69 | |||
70 | if ~prod(est_dist), | ||
71 | fprintf(1,'\nDistortion not fully estimated. Check variable est_dist.\n'); | ||
72 | end; | ||
73 | |||
74 | if ~est_alpha, | ||
75 | fprintf(1,'Skew not optimized (est_alpha=0).\n'); | ||
76 | alpha_c = 0; | ||
77 | end; | ||
78 | |||
79 | |||
80 | % Check 3D-ness of the calibration rig: | ||
81 | rig3D = 0; | ||
82 | for kk = ind_active, | ||
83 | eval(['X_kk = X_' num2str(kk) ';']); | ||
84 | if is3D(X_kk), | ||
85 | rig3D = 1; | ||
86 | end; | ||
87 | end; | ||
88 | |||
89 | % If the rig is 3D, then no choice: the only valid initialization is manual! | ||
90 | if rig3D, | ||
91 | quick_init = 1; | ||
92 | end; | ||
93 | |||
94 | |||
95 | |||
96 | alpha_smooth = 1; % set alpha_smooth = 1; for steepest gradient descent | ||
97 | |||
98 | |||
99 | % Conditioning threshold for view rejection | ||
100 | thresh_cond = 1e6; | ||
101 | |||
102 | |||
103 | |||
104 | %% Initialization of the intrinsic parameters (if necessary) | ||
105 | |||
106 | if ~exist('cc'), | ||
107 | fprintf(1,'Initialization of the principal point at the center of the image.\n'); | ||
108 | cc = [(nx-1)/2;(ny-1)/2]; | ||
109 | alpha_smooth = 0.4; % slow convergence | ||
110 | end; | ||
111 | |||
112 | |||
113 | if ~exist('kc'), | ||
114 | fprintf(1,'Initialization of the image distortion to zero.\n'); | ||
115 | kc = zeros(4,1); | ||
116 | alpha_smooth = 0.4; % slow convergence | ||
117 | end; | ||
118 | |||
119 | if ~exist('alpha_c'), | ||
120 | fprintf(1,'Initialization of the image skew to zero.\n'); | ||
121 | alpha_c = 0; | ||
122 | alpha_smooth = 0.4; % slow convergence | ||
123 | end; | ||
124 | |||
125 | if ~exist('fc')& quick_init, | ||
126 | FOV_angle = 35; % Initial camera field of view in degrees | ||
127 | fprintf(1,['Initialization of the focal length to a FOV of ' num2str(FOV_angle) ' degrees.\n']); | ||
128 | fc = (nx/2)/tan(pi*FOV_angle/360) * ones(2,1); | ||
129 | alpha_smooth = 0.4; % slow | ||
130 | end; | ||
131 | |||
132 | |||
133 | if ~exist('fc'), | ||
134 | % Initialization of the intrinsic parameters: | ||
135 | fprintf(1,'Initialization of the intrinsic parameters using the vanishing points of planar patterns.\n') | ||
136 | init_intrinsic_param; % The right way to go (if quick_init is not active)! | ||
137 | alpha_smooth = 0.4; % slow convergence | ||
138 | end; | ||
139 | |||
140 | |||
141 | if ~prod(est_dist), | ||
142 | % If no distortion estimated, set to zero the variables that are not estimated | ||
143 | kc = kc .* est_dist; | ||
144 | end; | ||
145 | |||
146 | |||
147 | |||
148 | |||
149 | |||
150 | %% Initialization of the extrinsic parameters for global minimization: | ||
151 | |||
152 | |||
153 | init_param = [fc;cc;alpha_c;kc;zeros(6,1)]; | ||
154 | |||
155 | |||
156 | |||
157 | for kk = 1:n_ima, | ||
158 | |||
159 | if exist(['x_' num2str(kk)]), | ||
160 | |||
161 | eval(['x_kk = x_' num2str(kk) ';']); | ||
162 | eval(['X_kk = X_' num2str(kk) ';']); | ||
163 | |||
164 | if (isnan(x_kk(1,1))), | ||
165 | if active_images(kk), | ||
166 | fprintf(1,'Warning: Cannot calibrate with image %d. Need to extract grid corners first.\n',kk) | ||
167 | fprintf(1,' Set active_images(%d)=1; and run Extract grid corners.\n',kk) | ||
168 | end; | ||
169 | active_images(kk) = 0; | ||
170 | end; | ||
171 | if active_images(kk), | ||
172 | [omckk,Tckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc,alpha_c); | ||
173 | [omckk,Tckk,Rckk,JJ_kk] = compute_extrinsic_refine(omckk,Tckk,x_kk,X_kk,fc,cc,kc,alpha_c,20,thresh_cond); | ||
174 | if cond(JJ_kk)> thresh_cond, | ||
175 | active_images(kk) = 0; | ||
176 | omckk = NaN*ones(3,1); | ||
177 | Tckk = NaN*ones(3,1); | ||
178 | fprintf(1,'\nWarning: View #%d ill-conditioned. This image is now set inactive.\n',kk) | ||
179 | desactivated_images = [desactivated_images kk]; | ||
180 | end; | ||
181 | if isnan(omckk(1,1)), | ||
182 | %fprintf(1,'\nWarning: Desactivating image %d. Re-activate it later by typing:\nactive_images(%d)=1;\nand re-run optimization\n',[kk kk]) | ||
183 | active_images(kk) = 0; | ||
184 | end; | ||
185 | else | ||
186 | omckk = NaN*ones(3,1); | ||
187 | Tckk = NaN*ones(3,1); | ||
188 | end; | ||
189 | |||
190 | else | ||
191 | |||
192 | omckk = NaN*ones(3,1); | ||
193 | Tckk = NaN*ones(3,1); | ||
194 | |||
195 | if active_images(kk), | ||
196 | fprintf(1,'Warning: Cannot calibrate with image %d. Need to extract grid corners first.\n',kk) | ||
197 | fprintf(1,' Set active_images(%d)=1; and run Extract grid corners.\n',kk) | ||
198 | end; | ||
199 | |||
200 | active_images(kk) = 0; | ||
201 | |||
202 | end; | ||
203 | |||
204 | eval(['omc_' num2str(kk) ' = omckk;']); | ||
205 | eval(['Tc_' num2str(kk) ' = Tckk;']); | ||
206 | |||
207 | init_param = [init_param; omckk ; Tckk]; | ||
208 | |||
209 | end; | ||
210 | |||
211 | |||
212 | check_active_images; | ||
213 | |||
214 | |||
215 | |||
216 | %-------------------- Main Optimization: | ||
217 | |||
218 | fprintf(1,'\nMain calibration optimization procedure - Number of images: %d\n',length(ind_active)); | ||
219 | |||
220 | |||
221 | param = init_param; | ||
222 | change = 1; | ||
223 | |||
224 | iter = 0; | ||
225 | |||
226 | fprintf(1,'Gradient descent iterations: '); | ||
227 | |||
228 | param_list = param; | ||
229 | |||
230 | MaxIter = 30; | ||
231 | |||
232 | |||
233 | while (change > 1e-6)&(iter < MaxIter), | ||
234 | |||
235 | fprintf(1,'%d...',iter+1); | ||
236 | |||
237 | |||
238 | %% The first step consists of updating the whole vector of knowns (intrinsic + extrinsic of active | ||
239 | %% images) through a one step steepest gradient descent. | ||
240 | |||
241 | JJ = []; | ||
242 | ex = []; | ||
243 | |||
244 | f = param(1:2); | ||
245 | c = param(3:4); | ||
246 | alpha = param(5); | ||
247 | k = param(6:9); | ||
248 | |||
249 | |||
250 | for kk = 1:n_ima, | ||
251 | |||
252 | if active_images(kk), | ||
253 | |||
254 | %omckk = param(4+6*(kk-1) + 3:6*kk + 3); | ||
255 | |||
256 | %Tckk = param(6*kk+1 + 3:6*kk+3 + 3); | ||
257 | |||
258 | omckk = param(15+6*(kk-1) + 1:15+6*(kk-1) + 3); | ||
259 | |||
260 | Tckk = param(15+6*(kk-1) + 4:15+6*(kk-1) + 6); | ||
261 | |||
262 | if isnan(omckk(1)), | ||
263 | fprintf(1,'Intrinsic parameters at frame %d do not exist\n',kk); | ||
264 | return; | ||
265 | end; | ||
266 | |||
267 | eval(['X_kk = X_' num2str(kk) ';']); | ||
268 | eval(['x_kk = x_' num2str(kk) ';']); | ||
269 | |||
270 | Np = size(X_kk,2); | ||
271 | |||
272 | JJkk = zeros(2*Np,n_ima * 6 + 15); | ||
273 | |||
274 | [x,dxdom,dxdT,dxdf,dxdc,dxdk,dxdalpha] = project_points2(X_kk,omckk,Tckk,f,c,k,alpha); | ||
275 | |||
276 | exkk = x_kk - x; | ||
277 | |||
278 | ex = [ex;exkk(:)]; | ||
279 | |||
280 | JJkk(:,1:2) = dxdf; | ||
281 | JJkk(:,3:4) = dxdc; | ||
282 | JJkk(:,5) = dxdalpha; | ||
283 | JJkk(:,6:9) = dxdk; | ||
284 | JJkk(:,15+6*(kk-1) + 1:15+6*(kk-1) + 3) = dxdom; | ||
285 | JJkk(:,15+6*(kk-1) + 4:15+6*(kk-1) + 6) = dxdT; | ||
286 | |||
287 | |||
288 | |||
289 | JJ = [JJ;JJkk]; | ||
290 | |||
291 | |||
292 | % Check if this view is ill-conditioned: | ||
293 | JJ_kk = [dxdom dxdT]; | ||
294 | if cond(JJ_kk)> thresh_cond, | ||
295 | active_images(kk) = 0; | ||
296 | fprintf(1,'\nWarning: View #%d ill-conditioned. This image is now set inactive.\n',kk) | ||
297 | desactivated_images = [desactivated_images kk]; | ||
298 | param(15+6*(kk-1) + 1:15+6*(kk-1) + 6) = NaN*ones(6,1); | ||
299 | end; | ||
300 | |||
301 | |||
302 | end; | ||
303 | |||
304 | end; | ||
305 | |||
306 | |||
307 | % List of active images (necessary if changed): | ||
308 | check_active_images; | ||
309 | |||
310 | |||
311 | % The following vector helps to select the variables to update (for only active images): | ||
312 | |||
313 | ind_Jac = find([ones(2,1);center_optim*ones(2,1);est_alpha;est_dist;zeros(6,1);reshape(ones(6,1)*active_images,6*n_ima,1)])'; | ||
314 | |||
315 | |||
316 | JJ = JJ(:,ind_Jac); | ||
317 | |||
318 | JJ2 = JJ'*JJ; | ||
319 | |||
320 | |||
321 | % Smoothing coefficient: | ||
322 | |||
323 | alpha_smooth2 = 1-(1-alpha_smooth)^(iter+1); %set to 1 to undo any smoothing! | ||
324 | |||
325 | |||
326 | param_innov = alpha_smooth2*inv(JJ2)*(JJ')*ex; | ||
327 | param_up = param(ind_Jac) + param_innov; | ||
328 | param(ind_Jac) = param_up; | ||
329 | |||
330 | |||
331 | % New intrinsic parameters: | ||
332 | |||
333 | fc_current = param(1:2); | ||
334 | cc_current = param(3:4); | ||
335 | alpha_current = param(5); | ||
336 | kc_current = param(6:9); | ||
337 | |||
338 | |||
339 | % Change on the intrinsic parameters: | ||
340 | change = norm([fc_current;cc_current] - [f;c])/norm([fc_current;cc_current]); | ||
341 | |||
342 | |||
343 | %% Second step: (optional) - It makes convergence faster, and the region of convergence LARGER!!! | ||
344 | %% Recompute the extrinsic parameters only using compute_extrinsic.m (this may be useful sometimes) | ||
345 | %% The complete gradient descent method is useful to precisely update the intrinsic parameters. | ||
346 | |||
347 | MaxIter2 = 20; | ||
348 | |||
349 | |||
350 | for kk = 1:n_ima, | ||
351 | if active_images(kk), | ||
352 | omc_current = param(15+6*(kk-1) + 1:15+6*(kk-1) + 3); | ||
353 | Tc_current = param(15+6*(kk-1) + 4:15+6*(kk-1) + 6); | ||
354 | eval(['X_kk = X_' num2str(kk) ';']); | ||
355 | eval(['x_kk = x_' num2str(kk) ';']); | ||
356 | [omc_current,Tc_current] = compute_extrinsic_init(x_kk,X_kk,fc_current,cc_current,kc_current,alpha_current); | ||
357 | [omckk,Tckk,Rckk,JJ_kk] = compute_extrinsic_refine(omc_current,Tc_current,x_kk,X_kk,fc_current,cc_current,kc_current,alpha_current,MaxIter2,thresh_cond); | ||
358 | if cond(JJ_kk)> thresh_cond, | ||
359 | active_images(kk) = 0; | ||
360 | fprintf(1,'\nWarning: View #%d ill-conditioned. This image is now set inactive.\n',kk) | ||
361 | desactivated_images = [desactivated_images kk]; | ||
362 | omckk = NaN*ones(3,1); | ||
363 | Tckk = NaN*ones(3,1); | ||
364 | end; | ||
365 | param(15+6*(kk-1) + 1:15+6*(kk-1) + 3) = omckk; | ||
366 | param(15+6*(kk-1) + 4:15+6*(kk-1) + 6) = Tckk; | ||
367 | end; | ||
368 | end; | ||
369 | |||
370 | param_list = [param_list param]; | ||
371 | |||
372 | iter = iter + 1; | ||
373 | |||
374 | end; | ||
375 | |||
376 | fprintf(1,'\n'); | ||
377 | |||
378 | |||
379 | solution = param; | ||
380 | |||
381 | |||
382 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
383 | |||
384 | extract_parameters; | ||
385 | |||
386 | comp_error_calib; | ||
387 | |||
388 | fprintf(1,'\n\nCalibration results after optimization:\n\n'); | ||
389 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f ]\n',fc); | ||
390 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f ]\n',cc); | ||
391 | fprintf(1,'Skew: alpha_c = [ %3.5f ] => angle of pixel = %3.5f degrees\n',alpha_c,90 - atan(alpha_c)*180/pi); | ||
392 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f ]\n',kc); | ||
393 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f ]\n\n',err_std); | ||
394 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/ima_read_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/ima_read_calib.m new file mode 100755 index 0000000..dbbc4e0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/ima_read_calib.m | |||
@@ -0,0 +1,158 @@ | |||
1 | |||
2 | if ~exist('calib_name')|~exist('format_image'), | ||
3 | data_calib; | ||
4 | return; | ||
5 | end; | ||
6 | |||
7 | check_directory; | ||
8 | |||
9 | if ~exist('n_ima'), | ||
10 | data_calib; | ||
11 | return; | ||
12 | end; | ||
13 | |||
14 | check_active_images; | ||
15 | |||
16 | |||
17 | images_read = active_images; | ||
18 | |||
19 | |||
20 | if exist('image_numbers'), | ||
21 | first_num = image_numbers(1); | ||
22 | end; | ||
23 | |||
24 | |||
25 | % Just to fix a minor bug: | ||
26 | if ~exist('first_num'), | ||
27 | first_num = image_numbers(1); | ||
28 | end; | ||
29 | |||
30 | |||
31 | image_numbers = first_num:n_ima-1+first_num; | ||
32 | |||
33 | no_image_file = 0; | ||
34 | |||
35 | i = 1; | ||
36 | |||
37 | while (i <= n_ima), % & (~no_image_file), | ||
38 | |||
39 | if active_images(i), | ||
40 | |||
41 | %fprintf(1,'Loading image %d...\n',i); | ||
42 | |||
43 | if ~type_numbering, | ||
44 | number_ext = num2str(image_numbers(i)); | ||
45 | else | ||
46 | number_ext = sprintf(['%.' num2str(N_slots) 'd'],image_numbers(i)); | ||
47 | end; | ||
48 | |||
49 | ima_name = [calib_name number_ext '.' format_image]; | ||
50 | |||
51 | if i == ind_active(1), | ||
52 | fprintf(1,'Loading image '); | ||
53 | end; | ||
54 | |||
55 | if exist(ima_name), | ||
56 | |||
57 | fprintf(1,'%d...',i); | ||
58 | |||
59 | if format_image(1) == 'p', | ||
60 | if format_image(2) == 'p', | ||
61 | Ii = double(loadppm(ima_name)); | ||
62 | else | ||
63 | Ii = double(loadpgm(ima_name)); | ||
64 | end; | ||
65 | else | ||
66 | if format_image(1) == 'r', | ||
67 | Ii = readras(ima_name); | ||
68 | else | ||
69 | Ii = double(imread(ima_name)); | ||
70 | end; | ||
71 | end; | ||
72 | |||
73 | |||
74 | if size(Ii,3)>1, | ||
75 | Ii = Ii(:,:,2); | ||
76 | end; | ||
77 | |||
78 | eval(['I_' num2str(i) ' = Ii;']); | ||
79 | |||
80 | else | ||
81 | |||
82 | fprintf(1,'%d...no image...',i); | ||
83 | |||
84 | images_read(i) = 0; | ||
85 | |||
86 | %no_image_file = 1; | ||
87 | |||
88 | end; | ||
89 | |||
90 | end; | ||
91 | |||
92 | i = i+1; | ||
93 | |||
94 | end; | ||
95 | |||
96 | |||
97 | ind_read = find(images_read); | ||
98 | |||
99 | |||
100 | |||
101 | |||
102 | if isempty(ind_read), | ||
103 | |||
104 | fprintf(1,'\nWARNING! No image were read\n'); | ||
105 | |||
106 | no_image_file = 1; | ||
107 | |||
108 | |||
109 | else | ||
110 | |||
111 | |||
112 | %fprintf(1,'\nWARNING! Every exsisting image in the directory is set active.\n'); | ||
113 | |||
114 | |||
115 | if no_image_file, | ||
116 | |||
117 | %fprintf(1,'WARNING! Some images were not read properly\n'); | ||
118 | |||
119 | end; | ||
120 | |||
121 | |||
122 | fprintf(1,'\n'); | ||
123 | |||
124 | if size(I_1,1)~=480, | ||
125 | small_calib_image = 1; | ||
126 | else | ||
127 | small_calib_image = 0; | ||
128 | end; | ||
129 | |||
130 | [Hcal,Wcal] = size(I_1); % size of the calibration image | ||
131 | |||
132 | [ny,nx] = size(I_1); | ||
133 | |||
134 | clickname = []; | ||
135 | |||
136 | map = gray(256); | ||
137 | |||
138 | %string_save = 'save calib_data n_ima type_numbering N_slots image_numbers format_image calib_name Hcal Wcal nx ny map small_calib_image'; | ||
139 | |||
140 | %eval(string_save); | ||
141 | |||
142 | disp('done'); | ||
143 | %click_calib; | ||
144 | |||
145 | end; | ||
146 | |||
147 | if ~exist('map'), map = gray(256); end; | ||
148 | |||
149 | active_images = images_read; | ||
150 | |||
151 | % Show all the calibration images: | ||
152 | |||
153 | |||
154 | if ~isempty(ind_read), | ||
155 | |||
156 | mosaic; | ||
157 | |||
158 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/init_intrinsic_param.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/init_intrinsic_param.m new file mode 100755 index 0000000..94a5240 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/init_intrinsic_param.m | |||
@@ -0,0 +1,158 @@ | |||
1 | %init_intrinsic_param | ||
2 | % | ||
3 | %Initialization of the intrinsic parameters. | ||
4 | %Runs as a script. | ||
5 | % | ||
6 | %INPUT: x_1,x_2,x_3,...: Feature locations on the images | ||
7 | % X_1,X_2,X_3,...: Corresponding grid coordinates | ||
8 | % | ||
9 | %OUTPUT: fc: Camera focal length | ||
10 | % cc: Principal point coordinates | ||
11 | % kc: Distortion coefficients | ||
12 | % alpha_c: skew coefficient | ||
13 | % KK: The camera matrix (containing fc, cc and alpha_c) | ||
14 | % | ||
15 | %Method: Computes the planar homographies H_1, H_2, H_3, ... and computes | ||
16 | % the focal length fc from orthogonal vanishing points constraint. | ||
17 | % The principal point cc is assumed at the center of the image. | ||
18 | % Assumes no image distortion (kc = [0;0;0;0]) | ||
19 | % | ||
20 | %Note: The row vector active_images consists of zeros and ones. To deactivate an image, set the | ||
21 | % corresponding entry in the active_images vector to zero. | ||
22 | % | ||
23 | % | ||
24 | %Important function called within that program: | ||
25 | % | ||
26 | %compute_homography.m: Computes the planar homography between points on the grid in 3D, and the image plane. | ||
27 | % | ||
28 | % | ||
29 | %VERY IMPORTANT: This function works only with 2D rigs. | ||
30 | %In the future, a more general function will be there (working with 3D rigs as well). | ||
31 | |||
32 | |||
33 | |||
34 | check_active_images; | ||
35 | |||
36 | if ~exist(['x_' num2str(ind_active(1)) ]), | ||
37 | click_calib; | ||
38 | end; | ||
39 | |||
40 | |||
41 | fprintf(1,'\nInitialization of the intrinsic parameters - Number of images: %d\n',length(ind_active)); | ||
42 | |||
43 | |||
44 | % Initialize the homographies: | ||
45 | |||
46 | for kk = 1:n_ima, | ||
47 | eval(['x_kk = x_' num2str(kk) ';']); | ||
48 | eval(['X_kk = X_' num2str(kk) ';']); | ||
49 | if (isnan(x_kk(1,1))), | ||
50 | if active_images(kk), | ||
51 | fprintf(1,'WARNING: Cannot calibrate with image %d. Need to extract grid corners first.\n',kk) | ||
52 | fprintf(1,' Set active_images(%d)=1; and run Extract grid corners.\n',kk) | ||
53 | end; | ||
54 | active_images(kk) = 0; | ||
55 | end; | ||
56 | if active_images(kk), | ||
57 | eval(['H_' num2str(kk) ' = compute_homography(x_kk,X_kk(1:2,:));']); | ||
58 | else | ||
59 | eval(['H_' num2str(kk) ' = NaN*ones(3,3);']); | ||
60 | end; | ||
61 | end; | ||
62 | |||
63 | check_active_images; | ||
64 | |||
65 | % initial guess for principal point and distortion: | ||
66 | |||
67 | if ~exist('nx'), [ny,nx] = size(I); end; | ||
68 | |||
69 | c_init = [nx;ny]/2 - 0.5; % initialize at the center of the image | ||
70 | k_init = [0;0;0;0]; % initialize to zero (no distortion) | ||
71 | |||
72 | |||
73 | |||
74 | % Compute explicitely the focal length using all the (mutually orthogonal) vanishing points | ||
75 | % note: The vanihing points are hidden in the planar collineations H_kk | ||
76 | |||
77 | A = []; | ||
78 | b = []; | ||
79 | |||
80 | % matrix that subtract the principal point: | ||
81 | Sub_cc = [1 0 -c_init(1);0 1 -c_init(2);0 0 1]; | ||
82 | |||
83 | for kk=1:n_ima, | ||
84 | |||
85 | if active_images(kk), | ||
86 | |||
87 | eval(['Hkk = H_' num2str(kk) ';']); | ||
88 | |||
89 | Hkk = Sub_cc * Hkk; | ||
90 | |||
91 | % Extract vanishing points (direct and diagonals): | ||
92 | |||
93 | V_hori_pix = Hkk(:,1); | ||
94 | V_vert_pix = Hkk(:,2); | ||
95 | V_diag1_pix = (Hkk(:,1)+Hkk(:,2))/2; | ||
96 | V_diag2_pix = (Hkk(:,1)-Hkk(:,2))/2; | ||
97 | |||
98 | V_hori_pix = V_hori_pix/norm(V_hori_pix); | ||
99 | V_vert_pix = V_vert_pix/norm(V_vert_pix); | ||
100 | V_diag1_pix = V_diag1_pix/norm(V_diag1_pix); | ||
101 | V_diag2_pix = V_diag2_pix/norm(V_diag2_pix); | ||
102 | |||
103 | a1 = V_hori_pix(1); | ||
104 | b1 = V_hori_pix(2); | ||
105 | c1 = V_hori_pix(3); | ||
106 | |||
107 | a2 = V_vert_pix(1); | ||
108 | b2 = V_vert_pix(2); | ||
109 | c2 = V_vert_pix(3); | ||
110 | |||
111 | a3 = V_diag1_pix(1); | ||
112 | b3 = V_diag1_pix(2); | ||
113 | c3 = V_diag1_pix(3); | ||
114 | |||
115 | a4 = V_diag2_pix(1); | ||
116 | b4 = V_diag2_pix(2); | ||
117 | c4 = V_diag2_pix(3); | ||
118 | |||
119 | A_kk = [a1*a2 b1*b2; | ||
120 | a3*a4 b3*b4]; | ||
121 | |||
122 | b_kk = -[c1*c2;c3*c4]; | ||
123 | |||
124 | |||
125 | A = [A;A_kk]; | ||
126 | b = [b;b_kk]; | ||
127 | |||
128 | end; | ||
129 | |||
130 | end; | ||
131 | |||
132 | |||
133 | % use all the vanishing points to estimate focal length: | ||
134 | |||
135 | f_init = sqrt(abs(1./(inv(A'*A)*A'*b))); % if using a two-focal model for initial guess | ||
136 | |||
137 | alpha_init = 0; | ||
138 | |||
139 | %f_init = sqrt(b'*(sum(A')') / (b'*b)) * ones(2,1); % if single focal length model is used | ||
140 | |||
141 | |||
142 | % Global calibration matrix (initial guess): | ||
143 | |||
144 | KK = [f_init(1) alpha_init*f_init(1) c_init(1);0 f_init(2) c_init(2); 0 0 1]; | ||
145 | inv_KK = inv(KK); | ||
146 | |||
147 | |||
148 | cc = c_init; | ||
149 | fc = f_init; | ||
150 | kc = k_init; | ||
151 | alpha_c = alpha_init; | ||
152 | |||
153 | |||
154 | fprintf(1,'\n\nCalibration parameters after initialization:\n\n'); | ||
155 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f ]\n',fc); | ||
156 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f ]\n',cc); | ||
157 | fprintf(1,'Skew: alpha_c = [ %3.5f ] => angle of pixel = %3.5f degrees\n',alpha_c,90 - atan(alpha_c)*180/pi); | ||
158 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f ]\n',kc); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/is3D.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/is3D.m new file mode 100755 index 0000000..ab00b3d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/is3D.m | |||
@@ -0,0 +1,19 @@ | |||
1 | function test = is3D(X), | ||
2 | |||
3 | |||
4 | Np = size(X,2); | ||
5 | |||
6 | %% Check for planarity of the structure: | ||
7 | |||
8 | X_mean = mean(X')'; | ||
9 | |||
10 | Y = X - (X_mean*ones(1,Np)); | ||
11 | |||
12 | YY = Y*Y'; | ||
13 | |||
14 | [U,S,V] = svd(YY); | ||
15 | |||
16 | r = S(3,3)/S(2,2); | ||
17 | |||
18 | test = (r > 1e-3); | ||
19 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loading_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loading_calib.m new file mode 100755 index 0000000..a0f50d2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loading_calib.m | |||
@@ -0,0 +1,10 @@ | |||
1 | if ~exist('Calib_Results.mat'), | ||
2 | fprintf(1,'\nCalibration file Calib_Results.mat not found!\n'); | ||
3 | return; | ||
4 | end; | ||
5 | |||
6 | fprintf(1,'\nLoading calibration results from Calib_Results.mat\n'); | ||
7 | |||
8 | load Calib_Results | ||
9 | |||
10 | fprintf(1,'done\n'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadinr.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadinr.m new file mode 100755 index 0000000..91b6f89 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadinr.m | |||
@@ -0,0 +1,52 @@ | |||
1 | %LOADINR Load an INRIMAGE format file | ||
2 | % | ||
3 | % LOADINR(filename, im) | ||
4 | % | ||
5 | % Load an INRIA image format file and return it as a matrix | ||
6 | % | ||
7 | % SEE ALSO: saveinr | ||
8 | % | ||
9 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
10 | |||
11 | |||
12 | % Peter Corke 1996 | ||
13 | |||
14 | function im = loadinr(fname, im) | ||
15 | |||
16 | fid = fopen(fname, 'r'); | ||
17 | |||
18 | s = fgets(fid); | ||
19 | if strcmp(s(1:12), '#INRIMAGE-4#') == 0, | ||
20 | error('not INRIMAGE format'); | ||
21 | end | ||
22 | |||
23 | % not very complete, only looks for the X/YDIM keys | ||
24 | while 1, | ||
25 | s = fgets(fid); | ||
26 | n = length(s) - 1; | ||
27 | if s(1) == '#', | ||
28 | break | ||
29 | end | ||
30 | if strcmp(s(1:5), 'XDIM='), | ||
31 | cols = str2num(s(6:n)); | ||
32 | end | ||
33 | if strcmp(s(1:5), 'YDIM='), | ||
34 | rows = str2num(s(6:n)); | ||
35 | end | ||
36 | if strcmp(s(1:4), 'CPU='), | ||
37 | if strcmp(s(5:n), 'sun') == 0, | ||
38 | error('not sun data ordering'); | ||
39 | end | ||
40 | end | ||
41 | |||
42 | end | ||
43 | disp(['INRIMAGE format file ' num2str(rows) ' x ' num2str(cols)]) | ||
44 | |||
45 | % now the binary data | ||
46 | fseek(fid, 256, 'bof'); | ||
47 | [im count] = fread(fid, [cols rows], 'float32'); | ||
48 | im = im'; | ||
49 | if count ~= (rows*cols), | ||
50 | error('file too short'); | ||
51 | end | ||
52 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadpgm.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadpgm.m new file mode 100755 index 0000000..dfa8b61 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadpgm.m | |||
@@ -0,0 +1,89 @@ | |||
1 | %LOADPGM Load a PGM image | ||
2 | % | ||
3 | % I = loadpgm(filename) | ||
4 | % | ||
5 | % Returns a matrix containing the image loaded from the PGM format | ||
6 | % file filename. Handles ASCII (P2) and binary (P5) PGM file formats. | ||
7 | % | ||
8 | % If the filename has no extension, and open fails, a '.pgm' will | ||
9 | % be appended. | ||
10 | % | ||
11 | % | ||
12 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
13 | |||
14 | |||
15 | % Peter Corke 1994 | ||
16 | |||
17 | function I = loadpgm(file) | ||
18 | white = [' ' 9 10 13]; % space, tab, lf, cr | ||
19 | white = setstr(white); | ||
20 | |||
21 | fid = fopen(file, 'r'); | ||
22 | if fid < 0, | ||
23 | fid = fopen([file '.pgm'], 'r'); | ||
24 | end | ||
25 | if fid < 0, | ||
26 | error('Couldn''t open file'); | ||
27 | end | ||
28 | |||
29 | magic = fread(fid, 2, 'char'); | ||
30 | while 1 | ||
31 | c = fread(fid,1,'char'); | ||
32 | if c == '#', | ||
33 | fgetl(fid); | ||
34 | elseif ~any(c == white) | ||
35 | fseek(fid, -1, 'cof'); % unputc() | ||
36 | break; | ||
37 | end | ||
38 | end | ||
39 | cols = fscanf(fid, '%d', 1); | ||
40 | while 1 | ||
41 | c = fread(fid,1,'char'); | ||
42 | if c == '#', | ||
43 | fgetl(fid); | ||
44 | elseif ~any(c == white) | ||
45 | fseek(fid, -1, 'cof'); % unputc() | ||
46 | break; | ||
47 | end | ||
48 | end | ||
49 | rows = fscanf(fid, '%d', 1); | ||
50 | while 1 | ||
51 | c = fread(fid,1,'char'); | ||
52 | if c == '#', | ||
53 | fgetl(fid); | ||
54 | elseif ~any(c == white) | ||
55 | fseek(fid, -1, 'cof'); % unputc() | ||
56 | break; | ||
57 | end | ||
58 | end | ||
59 | maxval = fscanf(fid, '%d', 1); | ||
60 | while 1 | ||
61 | c = fread(fid,1,'char'); | ||
62 | if c == '#', | ||
63 | fgetl(fid); | ||
64 | elseif ~any(c == white) | ||
65 | fseek(fid, -1, 'cof'); % unputc() | ||
66 | break; | ||
67 | end | ||
68 | end | ||
69 | if magic(1) == 'P', | ||
70 | if magic(2) == '2', | ||
71 | %disp(['ASCII PGM file ' num2str(rows) ' x ' num2str(cols)]) | ||
72 | I = fscanf(fid, '%d', [cols rows])'; | ||
73 | elseif magic(2) == '5', | ||
74 | %disp(['Binary PGM file ' num2str(rows) ' x ' num2str(cols)]) | ||
75 | if maxval == 1, | ||
76 | fmt = 'unint1'; | ||
77 | elseif maxval == 15, | ||
78 | fmt = 'uint4'; | ||
79 | elseif maxval == 255, | ||
80 | fmt = 'uint8'; | ||
81 | elseif maxval == 2^32-1, | ||
82 | fmt = 'uint32'; | ||
83 | end | ||
84 | I = fread(fid, [cols rows], fmt)'; | ||
85 | else | ||
86 | disp('Not a PGM file'); | ||
87 | end | ||
88 | end | ||
89 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadppm.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadppm.m new file mode 100755 index 0000000..0c004fc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadppm.m | |||
@@ -0,0 +1,109 @@ | |||
1 | %LOADPPM Load a PPM image | ||
2 | % | ||
3 | % I = loadppm(filename) | ||
4 | % | ||
5 | % Returns a matrix containing the image loaded from the PPM format | ||
6 | % file filename. Handles ASCII (P3) and binary (P6) PPM file formats. | ||
7 | % | ||
8 | % If the filename has no extension, and open fails, a '.ppm' and | ||
9 | % '.pnm' extension will be tried. | ||
10 | % | ||
11 | % SEE ALSO: saveppm loadpgm | ||
12 | % | ||
13 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
14 | |||
15 | |||
16 | % Peter Corke 1994 | ||
17 | |||
18 | function I = loadppm(file) | ||
19 | white = [' ' 9 10 13]; % space, tab, lf, cr | ||
20 | white = setstr(white); | ||
21 | |||
22 | fid = fopen(file, 'r'); | ||
23 | if fid < 0, | ||
24 | fid = fopen([file '.ppm'], 'r'); | ||
25 | end | ||
26 | if fid < 0, | ||
27 | fid = fopen([file '.pnm'], 'r'); | ||
28 | end | ||
29 | if fid < 0, | ||
30 | error('Couldn''t open file'); | ||
31 | end | ||
32 | |||
33 | magic = fread(fid, 2, 'char'); | ||
34 | while 1 | ||
35 | c = fread(fid,1,'char'); | ||
36 | if c == '#', | ||
37 | fgetl(fid); | ||
38 | elseif ~any(c == white) | ||
39 | fseek(fid, -1, 'cof'); % unputc() | ||
40 | break; | ||
41 | end | ||
42 | end | ||
43 | cols = fscanf(fid, '%d', 1); | ||
44 | while 1 | ||
45 | c = fread(fid,1,'char'); | ||
46 | if c == '#', | ||
47 | fgetl(fid); | ||
48 | elseif ~any(c == white) | ||
49 | fseek(fid, -1, 'cof'); % unputc() | ||
50 | break; | ||
51 | end | ||
52 | end | ||
53 | rows = fscanf(fid, '%d', 1); | ||
54 | while 1 | ||
55 | c = fread(fid,1,'char'); | ||
56 | if c == '#', | ||
57 | fgetl(fid); | ||
58 | elseif ~any(c == white) | ||
59 | fseek(fid, -1, 'cof'); % unputc() | ||
60 | break; | ||
61 | end | ||
62 | end | ||
63 | maxval = fscanf(fid, '%d', 1); | ||
64 | while 1 | ||
65 | c = fread(fid,1,'char'); | ||
66 | if c == '#', | ||
67 | fgetl(fid); | ||
68 | elseif ~any(c == white) | ||
69 | fseek(fid, -1, 'cof'); % unputc() | ||
70 | break; | ||
71 | end | ||
72 | end | ||
73 | if magic(1) == 'P', | ||
74 | if magic(2) == '3', | ||
75 | %disp(['ASCII PPM file ' num2str(rows) ' x ' num2str(cols)]) | ||
76 | I = fscanf(fid, '%d', [cols*3 rows]); | ||
77 | elseif magic(2) == '6', | ||
78 | %disp(['Binary PPM file ' num2str(rows) ' x ' num2str(cols)]) | ||
79 | if maxval == 1, | ||
80 | fmt = 'unint1'; | ||
81 | elseif maxval == 15, | ||
82 | fmt = 'uint4'; | ||
83 | elseif maxval == 255, | ||
84 | fmt = 'uint8'; | ||
85 | elseif maxval == 2^32-1, | ||
86 | fmt = 'uint32'; | ||
87 | end | ||
88 | I = fread(fid, [cols*3 rows], fmt); | ||
89 | else | ||
90 | disp('Not a PPM file'); | ||
91 | end | ||
92 | end | ||
93 | % | ||
94 | % now the matrix has interleaved columns of R, G, B | ||
95 | % | ||
96 | I = I'; | ||
97 | size(I); | ||
98 | R = I(:,1:3:(cols*3)); | ||
99 | G = I(:,2:3:(cols*3)); | ||
100 | B = I(:,3:3:(cols*3)); | ||
101 | fclose(fid); | ||
102 | |||
103 | |||
104 | I = zeros(rows,cols,3); | ||
105 | I(:,:,1) = R; | ||
106 | I(:,:,2) = G; | ||
107 | I(:,:,3) = B; | ||
108 | I = uint8(I); | ||
109 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/mean_std_robust.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/mean_std_robust.m new file mode 100755 index 0000000..0d18a62 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/mean_std_robust.m | |||
@@ -0,0 +1,7 @@ | |||
1 | function [m,s] = mean_std_robust(x); | ||
2 | |||
3 | x = x(:); | ||
4 | |||
5 | m = median(x); | ||
6 | |||
7 | s = median(abs(x - m))*1.4836; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/mosaic.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/mosaic.m new file mode 100755 index 0000000..b056661 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/mosaic.m | |||
@@ -0,0 +1,92 @@ | |||
1 | |||
2 | if ~exist('I_1'), | ||
3 | active_images_save = active_images; | ||
4 | ima_read_calib; | ||
5 | active_images = active_images_save; | ||
6 | check_active_images; | ||
7 | end; | ||
8 | |||
9 | check_active_images; | ||
10 | |||
11 | if isempty(ind_read), | ||
12 | return; | ||
13 | end; | ||
14 | |||
15 | |||
16 | n_col = floor(sqrt(n_ima*nx/ny)); | ||
17 | |||
18 | n_row = ceil(n_ima / n_col); | ||
19 | |||
20 | |||
21 | ker2 = 1; | ||
22 | for ii = 1:n_col, | ||
23 | ker2 = conv(ker2,[1/4 1/2 1/4]); | ||
24 | end; | ||
25 | |||
26 | |||
27 | II = I_1(1:n_col:end,1:n_col:end); | ||
28 | |||
29 | [ny2,nx2] = size(II); | ||
30 | |||
31 | |||
32 | |||
33 | kk_c = 1; | ||
34 | |||
35 | II_mosaic = []; | ||
36 | |||
37 | for jj = 1:n_row, | ||
38 | |||
39 | |||
40 | II_row = []; | ||
41 | |||
42 | for ii = 1:n_col, | ||
43 | |||
44 | if (exist(['I_' num2str(kk_c)])) & (kk_c <= n_ima), | ||
45 | |||
46 | if active_images(kk_c), | ||
47 | eval(['I = I_' num2str(kk_c) ';']); | ||
48 | %I = conv2(conv2(I,ker2,'same'),ker2','same'); % anti-aliasing | ||
49 | I = I(1:n_col:end,1:n_col:end); | ||
50 | else | ||
51 | I = zeros(ny2,nx2); | ||
52 | end; | ||
53 | |||
54 | else | ||
55 | |||
56 | I = zeros(ny2,nx2); | ||
57 | |||
58 | end; | ||
59 | |||
60 | |||
61 | |||
62 | II_row = [II_row I]; | ||
63 | |||
64 | if ii ~= n_col, | ||
65 | |||
66 | II_row = [II_row zeros(ny2,3)]; | ||
67 | |||
68 | end; | ||
69 | |||
70 | |||
71 | kk_c = kk_c + 1; | ||
72 | |||
73 | end; | ||
74 | |||
75 | nn2 = size(II_row,2); | ||
76 | |||
77 | if jj ~= n_row, | ||
78 | II_row = [II_row; zeros(3,nn2)]; | ||
79 | end; | ||
80 | |||
81 | II_mosaic = [II_mosaic ; II_row]; | ||
82 | |||
83 | end; | ||
84 | |||
85 | figure(2); | ||
86 | image(II_mosaic); | ||
87 | colormap(gray(256)); | ||
88 | title('Calibration images'); | ||
89 | set(gca,'Xtick',[]) | ||
90 | set(gca,'Ytick',[]) | ||
91 | axis('image'); | ||
92 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/normalize.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/normalize.m new file mode 100755 index 0000000..6dc7149 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/normalize.m | |||
@@ -0,0 +1,43 @@ | |||
1 | function [xn] = normalize(x_kk,fc,cc,kc,alpha_c), | ||
2 | |||
3 | %normalize | ||
4 | % | ||
5 | %[xn] = normalize(x_kk,fc,cc,kc,alpha_c) | ||
6 | % | ||
7 | %Computes the normalized coordinates xn given the pixel coordinates x_kk | ||
8 | %and the intrinsic camera parameters fc, cc and kc. | ||
9 | % | ||
10 | %INPUT: x_kk: Feature locations on the images | ||
11 | % fc: Camera focal length | ||
12 | % cc: Principal point coordinates | ||
13 | % kc: Distortion coefficients | ||
14 | % alpha_c: Skew coefficient | ||
15 | % | ||
16 | %OUTPUT: xn: Normalized feature locations on the image plane (a 2XN matrix) | ||
17 | % | ||
18 | %Important functions called within that program: | ||
19 | % | ||
20 | %comp_distortion_oulu: undistort pixel coordinates. | ||
21 | |||
22 | if nargin < 5, | ||
23 | alpha_c = 0; | ||
24 | if nargin < 4; | ||
25 | kc = [0;0;0;0]; | ||
26 | if nargin < 3; | ||
27 | cc = [0;0]; | ||
28 | if nargin < 2, | ||
29 | fc = [1;1]; | ||
30 | end; | ||
31 | end; | ||
32 | end; | ||
33 | end; | ||
34 | |||
35 | |||
36 | % First subtract principal point, and divide by the focal length: | ||
37 | temp = (x_kk(2,:) - cc(2))/fc(2); | ||
38 | x_distort = [(x_kk(1,:) - cc(1))/fc(1) - alpha_c*temp;temp]; | ||
39 | |||
40 | |||
41 | %Compensate for lens distortion: | ||
42 | |||
43 | xn = comp_distortion_oulu(x_distort,kc); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/pgmread.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/pgmread.m new file mode 100755 index 0000000..c96ccb7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/pgmread.m | |||
@@ -0,0 +1,26 @@ | |||
1 | function img = pgmread(filename) | ||
2 | % function img = pgmread(filename) | ||
3 | % this is my version of pgmread for the pgm file created by XV. | ||
4 | % | ||
5 | % this program also corrects for the shifts in the image from pm file. | ||
6 | |||
7 | fid = fopen(filename,'r'); | ||
8 | fscanf(fid, 'P5\n'); | ||
9 | cmt = '#'; | ||
10 | while findstr(cmt, '#'), | ||
11 | cmt = fgets(fid); | ||
12 | if length(findstr(cmt, '#')) ~= 1, | ||
13 | YX = sscanf(cmt, '%d %d'); | ||
14 | y = YX(1); x = YX(2); | ||
15 | end | ||
16 | end | ||
17 | |||
18 | %fgets(fid); | ||
19 | |||
20 | %img = fscanf(fid,'%d',size); | ||
21 | %img = img'; | ||
22 | |||
23 | img = fread(fid,[y,x],'uint8'); | ||
24 | img = img'; | ||
25 | fclose(fid); | ||
26 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/project2_oulu.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/project2_oulu.m new file mode 100755 index 0000000..c5c4a34 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/project2_oulu.m | |||
@@ -0,0 +1,53 @@ | |||
1 | function [x] = project2_oulu(X,R,T,f,t,k) | ||
2 | %PROJECT Subsidiary to calib | ||
3 | |||
4 | % (c) Pietro Perona -- March 24, 1994 | ||
5 | % California Institute of Technology | ||
6 | % Pasadena, CA | ||
7 | % | ||
8 | % Renamed because project exists in matlab 5.2!!! | ||
9 | % Now uses the more elaborate intrinsic model from Oulu | ||
10 | |||
11 | |||
12 | |||
13 | [m,n] = size(X); | ||
14 | |||
15 | Y = R*X + T*ones(1,n); | ||
16 | Z = Y(3,:); | ||
17 | |||
18 | f = f(:); %% make a column vector | ||
19 | if length(f)==1, | ||
20 | f = [f f]'; | ||
21 | end; | ||
22 | |||
23 | x = (Y(1:2,:) ./ (ones(2,1) * Z)) ; | ||
24 | |||
25 | |||
26 | radius_2 = x(1,:).^2 + x(2,:).^2; | ||
27 | |||
28 | if length(k) > 1, | ||
29 | |||
30 | radial_distortion = 1 + ones(2,1) * ((k(1) * radius_2) + (k(2) * radius_2.^2)); | ||
31 | |||
32 | if length(k) < 4, | ||
33 | |||
34 | delta_x = zeros(2,n); | ||
35 | |||
36 | else | ||
37 | |||
38 | delta_x = [2*k(3)*x(1,:).*x(2,:) + k(4)*(radius_2 + 2*x(1,:).^2) ; | ||
39 | k(3) * (radius_2 + 2*x(2,:).^2)+2*k(4)*x(1,:).*x(2,:)]; | ||
40 | |||
41 | end; | ||
42 | |||
43 | |||
44 | else | ||
45 | |||
46 | radial_distortion = 1 + ones(2,1) * ((k(1) * radius_2)); | ||
47 | |||
48 | delta_x = zeros(2,n); | ||
49 | |||
50 | end; | ||
51 | |||
52 | |||
53 | x = (x .* radial_distortion + delta_x).* (f * ones(1,n)) + t*ones(1,n); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/project_points.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/project_points.m new file mode 100755 index 0000000..1823490 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/project_points.m | |||
@@ -0,0 +1,276 @@ | |||
1 | function [xp,dxpdom,dxpdT,dxpdf,dxpdc,dxpdk] = project_points(X,om,T,f,c,k) | ||
2 | |||
3 | %project_points.m | ||
4 | % | ||
5 | %[xp,dxpdom,dxpdT,dxpdf,dxpdc,dxpdk] = project_points(X,om,T,f,c,k) | ||
6 | % | ||
7 | %Projects a 3D structure onto the image plane. | ||
8 | % | ||
9 | %INPUT: X: 3D structure in the world coordinate frame (3xN matrix for N points) | ||
10 | % (om,T): Rigid motion parameters between world coordinate frame and camera reference frame | ||
11 | % om: rotation vector (3x1 vector); T: translation vector (3x1 vector) | ||
12 | % f: camera focal length in units of horizontal and vertical pixel units (2x1 vector) | ||
13 | % c: principal point location in pixel units (2x1 vector) | ||
14 | % k: Distortion coefficients (radial and tangential) (4x1 vector) | ||
15 | % | ||
16 | %OUTPUT: xp: Projected pixel coordinates (2xN matrix for N points) | ||
17 | % dxpdom: Derivative of xp with respect to om ((2N)x3 matrix) | ||
18 | % dxpdT: Derivative of xp with respect to T ((2N)x3 matrix) | ||
19 | % dxpdf: Derivative of xp with respect to f ((2N)x2 matrix) | ||
20 | % dxpdc: Derivative of xp with respect to c ((2N)x2 matrix) | ||
21 | % dxpdk: Derivative of xp with respect to k ((2N)x4 matrix) | ||
22 | % | ||
23 | %Definitions: | ||
24 | %Let P be a point in 3D of coordinates X in the world reference frame (stored in the matrix X) | ||
25 | %The coordinate vector of P in the camera reference frame is: Xc = R*X + T | ||
26 | %where R is the rotation matrix corresponding to the rotation vector om: R = rodrigues(om); | ||
27 | %call x, y and z the 3 coordinates of Xc: x = Xc(1); y = Xc(2); z = Xc(3); | ||
28 | %The pinehole projection coordinates of P is [a;b] where a=x/z and b=y/z. | ||
29 | %call r^2 = a^2 + b^2. | ||
30 | %The distorted point coordinates are: xd = [xx;yy] where: | ||
31 | % | ||
32 | %xx = a * (1 + kc(1)*r^2 + kc(2)*r^4) + 2*kc(3)*a*b + kc(4)*(r^2 + 2*a^2); | ||
33 | %yy = b * (1 + kc(1)*r^2 + kc(2)*r^4) + kc(3)*(r^2 + 2*b^2) + 2*kc(4)*a*b; | ||
34 | % | ||
35 | %The left terms correspond to radial distortion, the right terms correspond to tangential distortion | ||
36 | % | ||
37 | %Fianlly, convertion into pixel coordinates: The final pixel coordinates vector xp=[xxp;yyp] where: | ||
38 | % | ||
39 | %xxp = f(1)*xx + c(1) | ||
40 | %yyp = f(2)*yy + c(2) | ||
41 | % | ||
42 | % | ||
43 | %NOTE: About 90 percent of the code takes care fo computing the Jacobian matrices | ||
44 | % | ||
45 | % | ||
46 | %Important function called within that program: | ||
47 | % | ||
48 | %rodrigues.m: Computes the rotation matrix corresponding to a rotation vector | ||
49 | % | ||
50 | %rigid_motion.m: Computes the rigid motion transformation of a given structure | ||
51 | |||
52 | |||
53 | |||
54 | if nargin < 6, | ||
55 | k = zeros(4,1); | ||
56 | if nargin < 5, | ||
57 | c = zeros(2,1); | ||
58 | if nargin < 4, | ||
59 | f = ones(2,1); | ||
60 | if nargin < 3, | ||
61 | T = zeros(3,1); | ||
62 | if nargin < 2, | ||
63 | om = zeros(3,1); | ||
64 | if nargin < 1, | ||
65 | error('Need at least a 3D structure to project (in project_points.m)'); | ||
66 | return; | ||
67 | end; | ||
68 | end; | ||
69 | end; | ||
70 | end; | ||
71 | end; | ||
72 | end; | ||
73 | |||
74 | |||
75 | [m,n] = size(X); | ||
76 | |||
77 | [Y,dYdom,dYdT] = rigid_motion(X,om,T); | ||
78 | |||
79 | |||
80 | inv_Z = 1./Y(3,:); | ||
81 | |||
82 | x = (Y(1:2,:) .* (ones(2,1) * inv_Z)) ; | ||
83 | |||
84 | |||
85 | bb = (-x(1,:) .* inv_Z)'*ones(1,3); | ||
86 | cc = (-x(2,:) .* inv_Z)'*ones(1,3); | ||
87 | |||
88 | |||
89 | dxdom = zeros(2*n,3); | ||
90 | dxdom(1:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdom(1:3:end,:) + bb .* dYdom(3:3:end,:); | ||
91 | dxdom(2:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdom(2:3:end,:) + cc .* dYdom(3:3:end,:); | ||
92 | |||
93 | dxdT = zeros(2*n,3); | ||
94 | dxdT(1:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdT(1:3:end,:) + bb .* dYdT(3:3:end,:); | ||
95 | dxdT(2:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdT(2:3:end,:) + cc .* dYdT(3:3:end,:); | ||
96 | |||
97 | |||
98 | % Add distortion: | ||
99 | |||
100 | r2 = x(1,:).^2 + x(2,:).^2; | ||
101 | |||
102 | |||
103 | |||
104 | dr2dom = 2*((x(1,:)')*ones(1,3)) .* dxdom(1:2:end,:) + 2*((x(2,:)')*ones(1,3)) .* dxdom(2:2:end,:); | ||
105 | dr2dT = 2*((x(1,:)')*ones(1,3)) .* dxdT(1:2:end,:) + 2*((x(2,:)')*ones(1,3)) .* dxdT(2:2:end,:); | ||
106 | |||
107 | |||
108 | r4 = r2.^2; | ||
109 | |||
110 | dr4dom = 2*((r2')*ones(1,3)) .* dr2dom; | ||
111 | dr4dT = 2*((r2')*ones(1,3)) .* dr2dT; | ||
112 | |||
113 | |||
114 | % Radial distortion: | ||
115 | |||
116 | cdist = 1 + k(1) * r2 + k(2) * r4; | ||
117 | |||
118 | dcdistdom = k(1) * dr2dom + k(2) * dr4dom; | ||
119 | dcdistdT = k(1) * dr2dT+ k(2) * dr4dT; | ||
120 | dcdistdk = [ r2' r4' zeros(n,2)]; | ||
121 | |||
122 | |||
123 | xd1 = x .* (ones(2,1)*cdist); | ||
124 | |||
125 | dxd1dom = zeros(2*n,3); | ||
126 | dxd1dom(1:2:end,:) = (x(1,:)'*ones(1,3)) .* dcdistdom; | ||
127 | dxd1dom(2:2:end,:) = (x(2,:)'*ones(1,3)) .* dcdistdom; | ||
128 | coeff = (reshape([cdist;cdist],2*n,1)*ones(1,3)); | ||
129 | dxd1dom = dxd1dom + coeff.* dxdom; | ||
130 | |||
131 | dxd1dT = zeros(2*n,3); | ||
132 | dxd1dT(1:2:end,:) = (x(1,:)'*ones(1,3)) .* dcdistdT; | ||
133 | dxd1dT(2:2:end,:) = (x(2,:)'*ones(1,3)) .* dcdistdT; | ||
134 | dxd1dT = dxd1dT + coeff.* dxdT; | ||
135 | |||
136 | dxd1dk = zeros(2*n,4); | ||
137 | dxd1dk(1:2:end,:) = (x(1,:)'*ones(1,4)) .* dcdistdk; | ||
138 | dxd1dk(2:2:end,:) = (x(2,:)'*ones(1,4)) .* dcdistdk; | ||
139 | |||
140 | |||
141 | |||
142 | % tangential distortion: | ||
143 | |||
144 | a1 = 2.*x(1,:).*x(2,:); | ||
145 | a2 = r2 + 2*x(1,:).^2; | ||
146 | a3 = r2 + 2*x(2,:).^2; | ||
147 | |||
148 | delta_x = [k(3)*a1 + k(4)*a2 ; | ||
149 | k(3) * a3 + k(4)*a1]; | ||
150 | |||
151 | |||
152 | ddelta_xdx = zeros(2*n,2*n); | ||
153 | aa = (2*k(3)*x(2,:)+6*k(4)*x(1,:))'*ones(1,3); | ||
154 | bb = (2*k(3)*x(1,:)+2*k(4)*x(2,:))'*ones(1,3); | ||
155 | cc = (6*k(3)*x(2,:)+2*k(4)*x(1,:))'*ones(1,3); | ||
156 | |||
157 | ddelta_xdom = zeros(2*n,3); | ||
158 | ddelta_xdom(1:2:end,:) = aa .* dxdom(1:2:end,:) + bb .* dxdom(2:2:end,:); | ||
159 | ddelta_xdom(2:2:end,:) = bb .* dxdom(1:2:end,:) + cc .* dxdom(2:2:end,:); | ||
160 | |||
161 | ddelta_xdT = zeros(2*n,3); | ||
162 | ddelta_xdT(1:2:end,:) = aa .* dxdT(1:2:end,:) + bb .* dxdT(2:2:end,:); | ||
163 | ddelta_xdT(2:2:end,:) = bb .* dxdT(1:2:end,:) + cc .* dxdT(2:2:end,:); | ||
164 | |||
165 | ddelta_xdk = zeros(2*n,4); | ||
166 | ddelta_xdk(1:2:end,3) = a1'; | ||
167 | ddelta_xdk(1:2:end,4) = a2'; | ||
168 | ddelta_xdk(2:2:end,3) = a3'; | ||
169 | ddelta_xdk(2:2:end,4) = a1'; | ||
170 | |||
171 | |||
172 | |||
173 | xd2 = xd1 + delta_x; | ||
174 | |||
175 | dxd2dom = dxd1dom + ddelta_xdom ; | ||
176 | dxd2dT = dxd1dT + ddelta_xdT; | ||
177 | dxd2dk = dxd1dk + ddelta_xdk ; | ||
178 | |||
179 | |||
180 | % Pixel coordinates: | ||
181 | |||
182 | xp = xd2 .* (f * ones(1,n)) + c*ones(1,n); | ||
183 | |||
184 | coeff = reshape(f*ones(1,n),2*n,1); | ||
185 | |||
186 | dxpdom = (coeff*ones(1,3)) .* dxd2dom; | ||
187 | dxpdT = (coeff*ones(1,3)) .* dxd2dT; | ||
188 | dxpdk = (coeff*ones(1,4)) .* dxd2dk; | ||
189 | |||
190 | dxpdf = zeros(2*n,2); | ||
191 | dxpdf(1:2:end,1) = xd2(1,:)'; | ||
192 | dxpdf(2:2:end,2) = xd2(2,:)'; | ||
193 | |||
194 | dxpdc = zeros(2*n,2); | ||
195 | dxpdc(1:2:end,1) = ones(n,1); | ||
196 | dxpdc(2:2:end,2) = ones(n,1); | ||
197 | |||
198 | |||
199 | return; | ||
200 | |||
201 | % Test of the Jacobians: | ||
202 | |||
203 | n = 10; | ||
204 | |||
205 | X = 10*randn(3,n); | ||
206 | om = randn(3,1); | ||
207 | T = [10*randn(2,1);40]; | ||
208 | f = 1000*rand(2,1); | ||
209 | c = 1000*randn(2,1); | ||
210 | k = 0.5*randn(4,1); | ||
211 | |||
212 | |||
213 | [x,dxdom,dxdT,dxdf,dxdc,dxdk] = project_points(X,om,T,f,c,k); | ||
214 | |||
215 | |||
216 | % Test on om: NOT OK | ||
217 | |||
218 | dom = 0.000000001 * norm(om)*randn(3,1); | ||
219 | om2 = om + dom; | ||
220 | |||
221 | [x2] = project_points(X,om2,T,f,c,k); | ||
222 | |||
223 | x_pred = x + reshape(dxdom * dom,2,n); | ||
224 | |||
225 | |||
226 | norm(x2-x)/norm(x2 - x_pred) | ||
227 | |||
228 | |||
229 | % Test on T: OK!! | ||
230 | |||
231 | dT = 0.0001 * norm(T)*randn(3,1); | ||
232 | T2 = T + dT; | ||
233 | |||
234 | [x2] = project_points(X,om,T2,f,c,k); | ||
235 | |||
236 | x_pred = x + reshape(dxdT * dT,2,n); | ||
237 | |||
238 | |||
239 | norm(x2-x)/norm(x2 - x_pred) | ||
240 | |||
241 | |||
242 | |||
243 | % Test on f: OK!! | ||
244 | |||
245 | df = 0.001 * norm(f)*randn(2,1); | ||
246 | f2 = f + df; | ||
247 | |||
248 | [x2] = project_points(X,om,T,f2,c,k); | ||
249 | |||
250 | x_pred = x + reshape(dxdf * df,2,n); | ||
251 | |||
252 | |||
253 | norm(x2-x)/norm(x2 - x_pred) | ||
254 | |||
255 | |||
256 | % Test on c: OK!! | ||
257 | |||
258 | dc = 0.01 * norm(c)*randn(2,1); | ||
259 | c2 = c + dc; | ||
260 | |||
261 | [x2] = project_points(X,om,T,f,c2,k); | ||
262 | |||
263 | x_pred = x + reshape(dxdc * dc,2,n); | ||
264 | |||
265 | norm(x2-x)/norm(x2 - x_pred) | ||
266 | |||
267 | % Test on k: OK!! | ||
268 | |||
269 | dk = 0.001 * norm(4)*randn(4,1); | ||
270 | k2 = k + dk; | ||
271 | |||
272 | [x2] = project_points(X,om,T,f,c,k2); | ||
273 | |||
274 | x_pred = x + reshape(dxdk * dk,2,n); | ||
275 | |||
276 | norm(x2-x)/norm(x2 - x_pred) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/project_points2.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/project_points2.m new file mode 100755 index 0000000..5bb1b91 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/project_points2.m | |||
@@ -0,0 +1,312 @@ | |||
1 | function [xp,dxpdom,dxpdT,dxpdf,dxpdc,dxpdk,dxpdalpha] = project_points2(X,om,T,f,c,k,alpha) | ||
2 | |||
3 | %project_points.m | ||
4 | % | ||
5 | %[xp,dxpdom,dxpdT,dxpdf,dxpdc,dxpdk] = project_points2(X,om,T,f,c,k,alpha) | ||
6 | % | ||
7 | %Projects a 3D structure onto the image plane. | ||
8 | % | ||
9 | %INPUT: X: 3D structure in the world coordinate frame (3xN matrix for N points) | ||
10 | % (om,T): Rigid motion parameters between world coordinate frame and camera reference frame | ||
11 | % om: rotation vector (3x1 vector); T: translation vector (3x1 vector) | ||
12 | % f: camera focal length in units of horizontal and vertical pixel units (2x1 vector) | ||
13 | % c: principal point location in pixel units (2x1 vector) | ||
14 | % k: Distortion coefficients (radial and tangential) (4x1 vector) | ||
15 | % alpha: Skew coefficient between x and y pixel (alpha = 0 <=> square pixels) | ||
16 | % | ||
17 | %OUTPUT: xp: Projected pixel coordinates (2xN matrix for N points) | ||
18 | % dxpdom: Derivative of xp with respect to om ((2N)x3 matrix) | ||
19 | % dxpdT: Derivative of xp with respect to T ((2N)x3 matrix) | ||
20 | % dxpdf: Derivative of xp with respect to f ((2N)x2 matrix) | ||
21 | % dxpdc: Derivative of xp with respect to c ((2N)x2 matrix) | ||
22 | % dxpdk: Derivative of xp with respect to k ((2N)x4 matrix) | ||
23 | % | ||
24 | %Definitions: | ||
25 | %Let P be a point in 3D of coordinates X in the world reference frame (stored in the matrix X) | ||
26 | %The coordinate vector of P in the camera reference frame is: Xc = R*X + T | ||
27 | %where R is the rotation matrix corresponding to the rotation vector om: R = rodrigues(om); | ||
28 | %call x, y and z the 3 coordinates of Xc: x = Xc(1); y = Xc(2); z = Xc(3); | ||
29 | %The pinehole projection coordinates of P is [a;b] where a=x/z and b=y/z. | ||
30 | %call r^2 = a^2 + b^2. | ||
31 | %The distorted point coordinates are: xd = [xx;yy] where: | ||
32 | % | ||
33 | %xx = a * (1 + kc(1)*r^2 + kc(2)*r^4) + 2*kc(3)*a*b + kc(4)*(r^2 + 2*a^2); | ||
34 | %yy = b * (1 + kc(1)*r^2 + kc(2)*r^4) + kc(3)*(r^2 + 2*b^2) + 2*kc(4)*a*b; | ||
35 | % | ||
36 | %The left terms correspond to radial distortion, the right terms correspond to tangential distortion | ||
37 | % | ||
38 | %Finally, convertion into pixel coordinates: The final pixel coordinates vector xp=[xxp;yyp] where: | ||
39 | % | ||
40 | %xxp = f(1)*(xx + alpha*yy) + c(1) | ||
41 | %yyp = f(2)*yy + c(2) | ||
42 | % | ||
43 | % | ||
44 | %NOTE: About 90 percent of the code takes care fo computing the Jacobian matrices | ||
45 | % | ||
46 | % | ||
47 | %Important function called within that program: | ||
48 | % | ||
49 | %rodrigues.m: Computes the rotation matrix corresponding to a rotation vector | ||
50 | % | ||
51 | %rigid_motion.m: Computes the rigid motion transformation of a given structure | ||
52 | |||
53 | |||
54 | if nargin < 7, | ||
55 | alpha = 0; | ||
56 | if nargin < 6, | ||
57 | k = zeros(4,1); | ||
58 | if nargin < 5, | ||
59 | c = zeros(2,1); | ||
60 | if nargin < 4, | ||
61 | f = ones(2,1); | ||
62 | if nargin < 3, | ||
63 | T = zeros(3,1); | ||
64 | if nargin < 2, | ||
65 | om = zeros(3,1); | ||
66 | if nargin < 1, | ||
67 | error('Need at least a 3D structure to project (in project_points.m)'); | ||
68 | return; | ||
69 | end; | ||
70 | end; | ||
71 | end; | ||
72 | end; | ||
73 | end; | ||
74 | end; | ||
75 | end; | ||
76 | |||
77 | |||
78 | [m,n] = size(X); | ||
79 | |||
80 | [Y,dYdom,dYdT] = rigid_motion(X,om,T); | ||
81 | |||
82 | |||
83 | inv_Z = 1./Y(3,:); | ||
84 | |||
85 | x = (Y(1:2,:) .* (ones(2,1) * inv_Z)) ; | ||
86 | |||
87 | |||
88 | bb = (-x(1,:) .* inv_Z)'*ones(1,3); | ||
89 | cc = (-x(2,:) .* inv_Z)'*ones(1,3); | ||
90 | |||
91 | |||
92 | dxdom = zeros(2*n,3); | ||
93 | dxdom(1:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdom(1:3:end,:) + bb .* dYdom(3:3:end,:); | ||
94 | dxdom(2:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdom(2:3:end,:) + cc .* dYdom(3:3:end,:); | ||
95 | |||
96 | dxdT = zeros(2*n,3); | ||
97 | dxdT(1:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdT(1:3:end,:) + bb .* dYdT(3:3:end,:); | ||
98 | dxdT(2:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdT(2:3:end,:) + cc .* dYdT(3:3:end,:); | ||
99 | |||
100 | |||
101 | % Add distortion: | ||
102 | |||
103 | r2 = x(1,:).^2 + x(2,:).^2; | ||
104 | |||
105 | |||
106 | |||
107 | dr2dom = 2*((x(1,:)')*ones(1,3)) .* dxdom(1:2:end,:) + 2*((x(2,:)')*ones(1,3)) .* dxdom(2:2:end,:); | ||
108 | dr2dT = 2*((x(1,:)')*ones(1,3)) .* dxdT(1:2:end,:) + 2*((x(2,:)')*ones(1,3)) .* dxdT(2:2:end,:); | ||
109 | |||
110 | |||
111 | r4 = r2.^2; | ||
112 | |||
113 | dr4dom = 2*((r2')*ones(1,3)) .* dr2dom; | ||
114 | dr4dT = 2*((r2')*ones(1,3)) .* dr2dT; | ||
115 | |||
116 | |||
117 | % Radial distortion: | ||
118 | |||
119 | cdist = 1 + k(1) * r2 + k(2) * r4; | ||
120 | |||
121 | dcdistdom = k(1) * dr2dom + k(2) * dr4dom; | ||
122 | dcdistdT = k(1) * dr2dT+ k(2) * dr4dT; | ||
123 | dcdistdk = [ r2' r4' zeros(n,2)]; | ||
124 | |||
125 | |||
126 | xd1 = x .* (ones(2,1)*cdist); | ||
127 | |||
128 | dxd1dom = zeros(2*n,3); | ||
129 | dxd1dom(1:2:end,:) = (x(1,:)'*ones(1,3)) .* dcdistdom; | ||
130 | dxd1dom(2:2:end,:) = (x(2,:)'*ones(1,3)) .* dcdistdom; | ||
131 | coeff = (reshape([cdist;cdist],2*n,1)*ones(1,3)); | ||
132 | dxd1dom = dxd1dom + coeff.* dxdom; | ||
133 | |||
134 | dxd1dT = zeros(2*n,3); | ||
135 | dxd1dT(1:2:end,:) = (x(1,:)'*ones(1,3)) .* dcdistdT; | ||
136 | dxd1dT(2:2:end,:) = (x(2,:)'*ones(1,3)) .* dcdistdT; | ||
137 | dxd1dT = dxd1dT + coeff.* dxdT; | ||
138 | |||
139 | dxd1dk = zeros(2*n,4); | ||
140 | dxd1dk(1:2:end,:) = (x(1,:)'*ones(1,4)) .* dcdistdk; | ||
141 | dxd1dk(2:2:end,:) = (x(2,:)'*ones(1,4)) .* dcdistdk; | ||
142 | |||
143 | |||
144 | |||
145 | % tangential distortion: | ||
146 | |||
147 | a1 = 2.*x(1,:).*x(2,:); | ||
148 | a2 = r2 + 2*x(1,:).^2; | ||
149 | a3 = r2 + 2*x(2,:).^2; | ||
150 | |||
151 | delta_x = [k(3)*a1 + k(4)*a2 ; | ||
152 | k(3) * a3 + k(4)*a1]; | ||
153 | |||
154 | |||
155 | ddelta_xdx = zeros(2*n,2*n); | ||
156 | aa = (2*k(3)*x(2,:)+6*k(4)*x(1,:))'*ones(1,3); | ||
157 | bb = (2*k(3)*x(1,:)+2*k(4)*x(2,:))'*ones(1,3); | ||
158 | cc = (6*k(3)*x(2,:)+2*k(4)*x(1,:))'*ones(1,3); | ||
159 | |||
160 | ddelta_xdom = zeros(2*n,3); | ||
161 | ddelta_xdom(1:2:end,:) = aa .* dxdom(1:2:end,:) + bb .* dxdom(2:2:end,:); | ||
162 | ddelta_xdom(2:2:end,:) = bb .* dxdom(1:2:end,:) + cc .* dxdom(2:2:end,:); | ||
163 | |||
164 | ddelta_xdT = zeros(2*n,3); | ||
165 | ddelta_xdT(1:2:end,:) = aa .* dxdT(1:2:end,:) + bb .* dxdT(2:2:end,:); | ||
166 | ddelta_xdT(2:2:end,:) = bb .* dxdT(1:2:end,:) + cc .* dxdT(2:2:end,:); | ||
167 | |||
168 | ddelta_xdk = zeros(2*n,4); | ||
169 | ddelta_xdk(1:2:end,3) = a1'; | ||
170 | ddelta_xdk(1:2:end,4) = a2'; | ||
171 | ddelta_xdk(2:2:end,3) = a3'; | ||
172 | ddelta_xdk(2:2:end,4) = a1'; | ||
173 | |||
174 | |||
175 | |||
176 | xd2 = xd1 + delta_x; | ||
177 | |||
178 | dxd2dom = dxd1dom + ddelta_xdom ; | ||
179 | dxd2dT = dxd1dT + ddelta_xdT; | ||
180 | dxd2dk = dxd1dk + ddelta_xdk ; | ||
181 | |||
182 | |||
183 | % Add Skew: | ||
184 | |||
185 | xd3 = [xd2(1,:) + alpha*xd2(2,:);xd2(2,:)]; | ||
186 | |||
187 | % Compute: dxd3dom, dxd3dT, dxd3dk, dxd3dalpha | ||
188 | |||
189 | dxd3dom = zeros(2*n,3); | ||
190 | dxd3dom(1:2:2*n,:) = dxd2dom(1:2:2*n,:) + alpha*dxd2dom(2:2:2*n,:); | ||
191 | dxd3dom(2:2:2*n,:) = dxd2dom(2:2:2*n,:); | ||
192 | dxd3dT = zeros(2*n,3); | ||
193 | dxd3dT(1:2:2*n,:) = dxd2dT(1:2:2*n,:) + alpha*dxd2dT(2:2:2*n,:); | ||
194 | dxd3dT(2:2:2*n,:) = dxd2dT(2:2:2*n,:); | ||
195 | dxd3dk = zeros(2*n,4); | ||
196 | dxd3dk(1:2:2*n,:) = dxd2dk(1:2:2*n,:) + alpha*dxd2dk(2:2:2*n,:); | ||
197 | dxd3dk(2:2:2*n,:) = dxd2dk(2:2:2*n,:); | ||
198 | dxd3dalpha = zeros(2*n,1); | ||
199 | dxd3dalpha(1:2:2*n,:) = xd2(2,:)'; | ||
200 | |||
201 | |||
202 | |||
203 | % Pixel coordinates: | ||
204 | |||
205 | xp = xd3 .* (f * ones(1,n)) + c*ones(1,n); | ||
206 | |||
207 | coeff = reshape(f*ones(1,n),2*n,1); | ||
208 | |||
209 | dxpdom = (coeff*ones(1,3)) .* dxd3dom; | ||
210 | dxpdT = (coeff*ones(1,3)) .* dxd3dT; | ||
211 | dxpdk = (coeff*ones(1,4)) .* dxd3dk; | ||
212 | dxpdalpha = (coeff) .* dxd3dalpha; | ||
213 | |||
214 | dxpdf = zeros(2*n,2); | ||
215 | dxpdf(1:2:end,1) = xd2(1,:)'; | ||
216 | dxpdf(2:2:end,2) = xd2(2,:)'; | ||
217 | |||
218 | dxpdc = zeros(2*n,2); | ||
219 | dxpdc(1:2:end,1) = ones(n,1); | ||
220 | dxpdc(2:2:end,2) = ones(n,1); | ||
221 | |||
222 | |||
223 | return; | ||
224 | |||
225 | % Test of the Jacobians: | ||
226 | |||
227 | n = 10; | ||
228 | |||
229 | X = 10*randn(3,n); | ||
230 | om = randn(3,1); | ||
231 | T = [10*randn(2,1);40]; | ||
232 | f = 1000*rand(2,1); | ||
233 | c = 1000*randn(2,1); | ||
234 | k = 0.5*randn(4,1); | ||
235 | alpha = 0.01*randn(1,1); | ||
236 | |||
237 | [x,dxdom,dxdT,dxdf,dxdc,dxdk,dxdalpha] = project_points2(X,om,T,f,c,k,alpha); | ||
238 | |||
239 | |||
240 | % Test on om: NOT OK | ||
241 | |||
242 | dom = 0.000000001 * norm(om)*randn(3,1); | ||
243 | om2 = om + dom; | ||
244 | |||
245 | [x2] = project_points2(X,om2,T,f,c,k,alpha); | ||
246 | |||
247 | x_pred = x + reshape(dxdom * dom,2,n); | ||
248 | |||
249 | |||
250 | norm(x2-x)/norm(x2 - x_pred) | ||
251 | |||
252 | |||
253 | % Test on T: OK!! | ||
254 | |||
255 | dT = 0.0001 * norm(T)*randn(3,1); | ||
256 | T2 = T + dT; | ||
257 | |||
258 | [x2] = project_points2(X,om,T2,f,c,k,alpha); | ||
259 | |||
260 | x_pred = x + reshape(dxdT * dT,2,n); | ||
261 | |||
262 | |||
263 | norm(x2-x)/norm(x2 - x_pred) | ||
264 | |||
265 | |||
266 | |||
267 | % Test on f: OK!! | ||
268 | |||
269 | df = 0.001 * norm(f)*randn(2,1); | ||
270 | f2 = f + df; | ||
271 | |||
272 | [x2] = project_points2(X,om,T,f2,c,k,alpha); | ||
273 | |||
274 | x_pred = x + reshape(dxdf * df,2,n); | ||
275 | |||
276 | |||
277 | norm(x2-x)/norm(x2 - x_pred) | ||
278 | |||
279 | |||
280 | % Test on c: OK!! | ||
281 | |||
282 | dc = 0.01 * norm(c)*randn(2,1); | ||
283 | c2 = c + dc; | ||
284 | |||
285 | [x2] = project_points2(X,om,T,f,c2,k,alpha); | ||
286 | |||
287 | x_pred = x + reshape(dxdc * dc,2,n); | ||
288 | |||
289 | norm(x2-x)/norm(x2 - x_pred) | ||
290 | |||
291 | % Test on k: OK!! | ||
292 | |||
293 | dk = 0.001 * norm(k)*randn(4,1); | ||
294 | k2 = k + dk; | ||
295 | |||
296 | [x2] = project_points2(X,om,T,f,c,k2,alpha); | ||
297 | |||
298 | x_pred = x + reshape(dxdk * dk,2,n); | ||
299 | |||
300 | norm(x2-x)/norm(x2 - x_pred) | ||
301 | |||
302 | |||
303 | % Test on alpha: OK!! | ||
304 | |||
305 | dalpha = 0.001 * norm(k)*randn(1,1); | ||
306 | alpha2 = alpha + dalpha; | ||
307 | |||
308 | [x2] = project_points2(X,om,T,f,c,k,alpha2); | ||
309 | |||
310 | x_pred = x + reshape(dxdalpha * dalpha,2,n); | ||
311 | |||
312 | norm(x2-x)/norm(x2 - x_pred) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/projectedGrid.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/projectedGrid.m new file mode 100755 index 0000000..561a7d0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/projectedGrid.m | |||
@@ -0,0 +1,24 @@ | |||
1 | function [XX,H] = projectedGrid ( P1, P2, P3, P4 , nx, ny); | ||
2 | |||
3 | % new formalism using homographies | ||
4 | |||
5 | a00 = [P1;1]; | ||
6 | a10 = [P2;1]; | ||
7 | a11 = [P3;1]; | ||
8 | a01 = [P4;1]; | ||
9 | |||
10 | % Compute the planart collineation: | ||
11 | |||
12 | [H] = compute_collineation (a00, a10, a11, a01); | ||
13 | |||
14 | |||
15 | % Build the grid using the planar collineation: | ||
16 | |||
17 | x_l = ((0:(nx-1))'*ones(1,ny))/(nx-1); | ||
18 | y_l = (ones(nx,1)*(0:(ny-1)))/(ny-1); | ||
19 | |||
20 | pts = [x_l(:) y_l(:) ones(nx*ny,1)]'; | ||
21 | |||
22 | XX = H*pts; | ||
23 | |||
24 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/projector_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/projector_calib.m new file mode 100755 index 0000000..bb4ef86 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/projector_calib.m | |||
@@ -0,0 +1,258 @@ | |||
1 | %%% This code is an additional code that helps doing projector calibration in 3D scanning setup. | ||
2 | %%% This is not a useful code for anyone else but me. | ||
3 | %%% I included it in the toolbox for illustration only. | ||
4 | |||
5 | |||
6 | load camera_results; | ||
7 | |||
8 | |||
9 | proj_name = input('Basename projector calibration images (without number nor suffix): ','s'); | ||
10 | |||
11 | |||
12 | i = 1; | ||
13 | |||
14 | while (i <= n_ima), % & (~no_image_file), | ||
15 | |||
16 | if active_images(i), | ||
17 | |||
18 | %fprintf(1,'Loading image %d...\n',i); | ||
19 | |||
20 | if ~type_numbering, | ||
21 | number_ext = num2str(image_numbers(i)); | ||
22 | else | ||
23 | number_ext = sprintf(['%.' num2str(N_slots) 'd'],image_numbers(i)); | ||
24 | end; | ||
25 | |||
26 | ima_namep = [proj_name number_ext 'p.' format_image]; | ||
27 | ima_namen = [proj_name number_ext 'n.' format_image]; | ||
28 | |||
29 | if i == ind_active(1), | ||
30 | fprintf(1,'Loading image '); | ||
31 | end; | ||
32 | |||
33 | fprintf(1,'%d...',i); | ||
34 | |||
35 | if format_image(1) == 'p', | ||
36 | if format_image(2) == 'p', | ||
37 | Ip = double(loadppm(ima_namep)); | ||
38 | In = double(loadppm(ima_namen)); | ||
39 | else | ||
40 | Ip = double(loadpgm(ima_namep)); | ||
41 | In = double(loadpgm(ima_namen)); | ||
42 | end; | ||
43 | else | ||
44 | if format_image(1) == 'r', | ||
45 | Ip = readras(ima_namep); | ||
46 | In = readras(ima_namen); | ||
47 | else | ||
48 | Ip = double(imread(ima_namep)); | ||
49 | In = double(imread(ima_namen)); | ||
50 | end; | ||
51 | end; | ||
52 | |||
53 | |||
54 | if size(Ip,3)>1, | ||
55 | Ip = Ip(:,:,2); | ||
56 | In = In(:,:,2); | ||
57 | end; | ||
58 | |||
59 | eval(['Ip_' num2str(i) ' = Ip;']); | ||
60 | eval(['In_' num2str(i) ' = In;']); | ||
61 | |||
62 | end; | ||
63 | |||
64 | i = i+1; | ||
65 | |||
66 | end; | ||
67 | |||
68 | |||
69 | fprintf(1,'\nExtraction of the grid corners on the image\n'); | ||
70 | |||
71 | disp('Window size for corner finder (wintx and winty):'); | ||
72 | wintx = input('wintx ([] = 5) = '); | ||
73 | if isempty(wintx), wintx = 5; end; | ||
74 | wintx = round(wintx); | ||
75 | winty = input('winty ([] = 5) = '); | ||
76 | if isempty(winty), winty = 5; end; | ||
77 | winty = round(winty); | ||
78 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
79 | |||
80 | |||
81 | disp('The projector you are using is the DLP or Intel'); | ||
82 | nx = 800; | ||
83 | ny = 600; | ||
84 | |||
85 | dX = input('Size dX in x of the squares (in pixels) [50] = '); | ||
86 | dY = input('Size dY in y of the squares (in pixels) [50] = '); | ||
87 | |||
88 | if isempty(dX), dX=50; end; | ||
89 | if isempty(dY), dY=50; end; | ||
90 | |||
91 | dXoff = input('Position in x of your reference (in pixels) [399.5] = '); | ||
92 | dYoff = input('Position in y of your reference (in pixels) [299.5] = '); | ||
93 | |||
94 | if isempty(dXoff), dXoff=399.5; end; | ||
95 | if isempty(dYoff), dYoff=299.5; end; | ||
96 | |||
97 | end; | ||
98 | |||
99 | |||
100 | |||
101 | for kk = ind_active, | ||
102 | |||
103 | eval(['Ip = Ip_' num2str(kk) ';']); | ||
104 | eval(['In = In_' num2str(kk) ';']); | ||
105 | |||
106 | [x,X,n_sq_x,n_sq_y,ind_orig,ind_x,ind_y] = extract_grid(In,wintx,winty,fc,cc,kc,dX,dY); | ||
107 | xproj = x; | ||
108 | |||
109 | Np_proj = size(x,2); | ||
110 | |||
111 | figure(2); | ||
112 | image(Ip); | ||
113 | hold on; | ||
114 | plot(xproj(1,:)+1,xproj(2,:)+1,'r+'); | ||
115 | title('Click on your reference point'); | ||
116 | xlabel('Xc (in camera frame)'); | ||
117 | ylabel('Yc (in camera frame)'); | ||
118 | hold off; | ||
119 | |||
120 | disp('Click on your reference point...'); | ||
121 | |||
122 | [xr,yr] = ginput2(1); | ||
123 | |||
124 | err = sqrt(sum((xproj - [xr;yr]*ones(1,Np_proj)).^2)); | ||
125 | ind_ref = find(err == min(err)); | ||
126 | |||
127 | ref_pt = xproj(:,ind_ref); | ||
128 | |||
129 | |||
130 | figure(2); | ||
131 | hold on; | ||
132 | plot(ref_pt(1)+1,ref_pt(2)+1,'go'); hold off; | ||
133 | |||
134 | |||
135 | nn = floor(ind_ref/(n_sq_x+1)); | ||
136 | off_x = ind_ref - nn*(n_sq_x+1) - 1; | ||
137 | off_y = n_sq_y - nn; | ||
138 | |||
139 | |||
140 | xprojn = xproj - cc * ones(1,Np_proj); | ||
141 | xprojn = xprojn ./ (fc * ones(1,Np_proj)); | ||
142 | xprojn = comp_distortion(xprojn,kc); | ||
143 | |||
144 | eval(['Rc = Rc_' num2str(kk) ';']); | ||
145 | eval(['Tc = Tc_' num2str(kk) ';']); | ||
146 | |||
147 | Zc = ((Rc(:,3)'*Tc) * (1./(Rc(:,3)' * [xprojn; ones(1,Np_proj)]))); | ||
148 | Xcp = (ones(3,1)*Zc) .* [xprojn; ones(1,Np_proj)]; % % in the camera frame | ||
149 | %Xproj = Rc'* Xcp - (Rc'*Tc)*ones(1,Np); % in the object frame !!! it works! | ||
150 | %Xproj(3,:) = zeros(1,Np); | ||
151 | |||
152 | eval(['X_proj_' num2str(kk) ' = Xcp;']); % coordinates of the points in the | ||
153 | |||
154 | x_proj = X(1:2,:) + ([dXoff - dX * off_x ; dYoff - dY * off_y]*ones(1,Np_proj)); | ||
155 | |||
156 | eval(['x_proj_' num2str(kk) ' = x_proj;']); % coordinates of the points in the | ||
157 | |||
158 | end; | ||
159 | |||
160 | |||
161 | |||
162 | X_proj = []; | ||
163 | x_proj = []; | ||
164 | |||
165 | for kk = ind_active, | ||
166 | eval(['X_proj = [X_proj X_proj_' num2str(kk) '];']); | ||
167 | eval(['x_proj = [x_proj x_proj_' num2str(kk) '];']); | ||
168 | end; | ||
169 | |||
170 | |||
171 | %Save camera parameters: | ||
172 | fc_save = fc; | ||
173 | cc_save = cc; | ||
174 | kc_save = kc; | ||
175 | |||
176 | omc_1_save = omc_1; | ||
177 | Rc_1_save = Rc_1; | ||
178 | Tc_1_save = Tc_1; | ||
179 | |||
180 | |||
181 | % Get started to calibrate projector: | ||
182 | clear fc cc kc | ||
183 | |||
184 | n_ima = 1; | ||
185 | X_1 = X_proj; | ||
186 | x_1 = x_proj; | ||
187 | |||
188 | |||
189 | % Image size: (may or may not be available) | ||
190 | |||
191 | nx = 800; | ||
192 | ny = 600; | ||
193 | |||
194 | % No calibration image is available (only the corner coordinates) | ||
195 | |||
196 | no_image = 1; | ||
197 | |||
198 | % Set the toolbox not to prompt the user (choose default values) | ||
199 | |||
200 | dont_ask = 1; | ||
201 | |||
202 | % Do not estimate distortion: | ||
203 | |||
204 | est_dist = [0;0;0;0]; | ||
205 | est_dist = ones(4,1); | ||
206 | |||
207 | center_optim = 1; | ||
208 | |||
209 | % Run the main calibration routine: | ||
210 | |||
211 | go_calib_optim_iter; | ||
212 | |||
213 | % Shows the extrinsic parameters: | ||
214 | |||
215 | dX = 3; | ||
216 | dY = 3; | ||
217 | |||
218 | ext_calib; | ||
219 | |||
220 | % Reprojection on the original images: | ||
221 | |||
222 | reproject_calib; | ||
223 | |||
224 | |||
225 | |||
226 | |||
227 | %----------------------- Retrieve results: | ||
228 | |||
229 | % Intrinsic: | ||
230 | |||
231 | % Projector: | ||
232 | fp = fc; | ||
233 | cp = cc; | ||
234 | kp = kc; | ||
235 | |||
236 | % Camera: | ||
237 | fc = fc_save; | ||
238 | cc = cc_save; | ||
239 | kc = kc_save; | ||
240 | |||
241 | % Extrinsic: | ||
242 | |||
243 | % Relative position of projector and camera: | ||
244 | T = Tc_1; | ||
245 | om = omc_1; | ||
246 | R = rodrigues(om); | ||
247 | |||
248 | % Relative prosition of camera wrt world: | ||
249 | omc = omc_1_save; | ||
250 | Rc = Rc_1_save; | ||
251 | Tc = Tc_1_save; | ||
252 | |||
253 | % relative position of projector wrt world: | ||
254 | Rp = R*Rc; | ||
255 | omp = rodrigues(Rp); | ||
256 | Tp = T + R*Tc; | ||
257 | |||
258 | eval(['save calib_cam_proj R om T fc fp cc cp kc kp Rc Rp Tc Tp']); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/readras.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/readras.m new file mode 100755 index 0000000..fc1820b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/readras.m | |||
@@ -0,0 +1,87 @@ | |||
1 | function [X, map] = readras(filename, ys, ye, xs, xe); | ||
2 | %READRAS Read an image file in sun raster format. | ||
3 | % READRAS('imagefile.ras') reads a "sun.raster" image file. | ||
4 | % [X, map] = READRAS('imagefile.ras') returns both the image and a | ||
5 | % color map, so that | ||
6 | % [X, map] = readras('imagefile.ras'); | ||
7 | % image(X) | ||
8 | % colormap(map) | ||
9 | % axis('equal') | ||
10 | % will display the result with the proper colors. | ||
11 | % NOTE: readras cannot deal with complicated color maps. | ||
12 | % In fact, Matlab doesn't quite allow to work with colormaps | ||
13 | % with more than 64 entries. | ||
14 | % | ||
15 | |||
16 | %% | ||
17 | %% (C) Thomas K. Leung 3/30/93. | ||
18 | %% California Institute of Technology. | ||
19 | %% Modified by Andrea Mennucci to deal with color images | ||
20 | %% | ||
21 | |||
22 | % PC and UNIX version of readras - Jean-Yves Bouguet - Dec. 1998 | ||
23 | |||
24 | dot = max(find(filename == '.')); | ||
25 | suffix = filename(dot+1:dot+3); | ||
26 | |||
27 | if(strcmp(suffix, 'ras')) % raster file format % | ||
28 | fp = fopen(filename, 'rb'); | ||
29 | if(fp<0) error(['Cannot open ' filename '.']), end | ||
30 | |||
31 | %Read and crack the 32-byte header | ||
32 | fseek(fp, 4, -1); | ||
33 | |||
34 | width = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
35 | |||
36 | height = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
37 | |||
38 | depth = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
39 | |||
40 | length = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
41 | |||
42 | type = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
43 | |||
44 | maptype = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
45 | |||
46 | maplen = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
47 | |||
48 | maplen = maplen / 3; | ||
49 | |||
50 | if maptype == 2 % RMT_RAW | ||
51 | map = fread(fp, [maplen, 3], 'uchar')/255; | ||
52 | % if maplen<64, map=[map',zeros(3,64-maplen)]';maplen=64; end; | ||
53 | elseif maptype == 1 % RMT_EQUAL_RGB | ||
54 | map(:,1) = fread(fp, [maplen], 'uchar'); | ||
55 | map(:,2) = fread(fp, [maplen], 'uchar'); | ||
56 | map(:,3) = fread(fp, [maplen], 'uchar'); | ||
57 | %maxmap = max(max(map)); | ||
58 | map = map/255; | ||
59 | if maplen<64, map=[map',zeros(3,64-maplen)]'; maplen=64; end; | ||
60 | else % RMT_NONE | ||
61 | map = []; | ||
62 | end | ||
63 | % if maplen>64, | ||
64 | % map=[map',zeros(3,256-maplen)]'; | ||
65 | % end; | ||
66 | |||
67 | % Read the image | ||
68 | |||
69 | if rem(width,2) == 1 | ||
70 | Xt = fread(fp, [width+1, height], 'uchar'); | ||
71 | X = Xt(1:width, :)'; | ||
72 | else | ||
73 | Xt = fread(fp, [width, height], 'uchar'); | ||
74 | X = Xt'; | ||
75 | end | ||
76 | X = X + 1; | ||
77 | fclose(fp); | ||
78 | else | ||
79 | error('Image file name must end in either ''ras'' or ''rast''.'); | ||
80 | end | ||
81 | |||
82 | |||
83 | if nargin == 5 | ||
84 | |||
85 | X = X(ys:ye, xs:xe); | ||
86 | |||
87 | end \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/recomp_corner_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/recomp_corner_calib.m new file mode 100755 index 0000000..0909c69 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/recomp_corner_calib.m | |||
@@ -0,0 +1,119 @@ | |||
1 | % Re-select te corners after calibration | ||
2 | |||
3 | if ~exist('n_ima')|~exist('fc'), | ||
4 | fprintf(1,'No calibration data available.\n'); | ||
5 | return; | ||
6 | end; | ||
7 | |||
8 | check_active_images; | ||
9 | |||
10 | flag = 0; | ||
11 | for kk = ind_active, | ||
12 | if ~exist(['y_' num2str(kk)]), | ||
13 | flag = 1; | ||
14 | else | ||
15 | eval(['ykk = y_' num2str(kk) ';']); | ||
16 | if isnan(ykk(1,1)), | ||
17 | flag = 1; | ||
18 | end; | ||
19 | end; | ||
20 | end; | ||
21 | |||
22 | if flag, | ||
23 | fprintf(1,'Need to calibrate once before before recomputing image corners. Maybe need to load Calib_Results.mat file.\n'); | ||
24 | return; | ||
25 | end; | ||
26 | |||
27 | |||
28 | if ~exist(['I_' num2str(ind_active(1))]), | ||
29 | n_ima_save = n_ima; | ||
30 | active_images_save = active_images; | ||
31 | ima_read_calib; | ||
32 | n_ima = n_ima_save; | ||
33 | active_images = active_images_save; | ||
34 | check_active_images; | ||
35 | if no_image_file, | ||
36 | disp('Cannot extract corners without images'); | ||
37 | return; | ||
38 | end; | ||
39 | end; | ||
40 | |||
41 | fprintf(1,'\nRe-extraction of the grid corners on the images (after first calibration)\n'); | ||
42 | |||
43 | disp('Window size for corner finder (wintx and winty):'); | ||
44 | wintx = input('wintx ([] = 5) = '); | ||
45 | if isempty(wintx), wintx = 5; end; | ||
46 | wintx = round(wintx); | ||
47 | winty = input('winty ([] = 5) = '); | ||
48 | if isempty(winty), winty = 5; end; | ||
49 | winty = round(winty); | ||
50 | |||
51 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
52 | |||
53 | ima_numbers = input('Number(s) of image(s) to process ([] = all images) = '); | ||
54 | |||
55 | if isempty(ima_numbers), | ||
56 | ima_proc = 1:n_ima; | ||
57 | else | ||
58 | ima_proc = ima_numbers; | ||
59 | end; | ||
60 | |||
61 | q_auto = input('Use the projection of 3D grid or manual click ([]=auto, other=manual): '); | ||
62 | |||
63 | fprintf(1,'Processing image '); | ||
64 | |||
65 | for kk = ima_proc; | ||
66 | |||
67 | if active_images(kk), | ||
68 | |||
69 | fprintf(1,'%d...',kk); | ||
70 | |||
71 | if isempty(q_auto), | ||
72 | |||
73 | eval(['I = I_' num2str(kk) ';']); | ||
74 | |||
75 | eval(['y = y_' num2str(kk) ';']); | ||
76 | |||
77 | xc = cornerfinder(y+1,I,winty,wintx); % the four corners | ||
78 | |||
79 | eval(['wintx_' num2str(kk) ' = wintx;']); | ||
80 | eval(['winty_' num2str(kk) ' = winty;']); | ||
81 | |||
82 | eval(['x_' num2str(kk) '= xc - 1;']); | ||
83 | |||
84 | else | ||
85 | |||
86 | fprintf(1,'\n'); | ||
87 | |||
88 | click_ima_calib; | ||
89 | |||
90 | end; | ||
91 | |||
92 | else | ||
93 | |||
94 | if ~exist(['omc_' num2str(kk)]), | ||
95 | |||
96 | eval(['dX_' num2str(kk) ' = NaN;']); | ||
97 | eval(['dY_' num2str(kk) ' = NaN;']); | ||
98 | |||
99 | eval(['wintx_' num2str(kk) ' = NaN;']); | ||
100 | eval(['winty_' num2str(kk) ' = NaN;']); | ||
101 | |||
102 | eval(['x_' num2str(kk) ' = NaN*ones(2,1);']); | ||
103 | eval(['X_' num2str(kk) ' = NaN*ones(3,1);']); | ||
104 | |||
105 | eval(['n_sq_x_' num2str(kk) ' = NaN;']); | ||
106 | eval(['n_sq_y_' num2str(kk) ' = NaN;']); | ||
107 | |||
108 | end; | ||
109 | |||
110 | end; | ||
111 | |||
112 | |||
113 | end; | ||
114 | |||
115 | % Recompute the error: | ||
116 | |||
117 | comp_error_calib; | ||
118 | |||
119 | fprintf(1,'\ndone\n'); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rect.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rect.m new file mode 100755 index 0000000..ccac7a5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rect.m | |||
@@ -0,0 +1,127 @@ | |||
1 | function [Irec] = rect(I,R,f,c,k,alpha,KK_new); | ||
2 | |||
3 | |||
4 | if nargin < 5, | ||
5 | k = 0; | ||
6 | if nargin < 4, | ||
7 | c = [0;0]; | ||
8 | if nargin < 3, | ||
9 | f = [1;1]; | ||
10 | if nargin < 2, | ||
11 | R = eye(3); | ||
12 | if nargin < 1, | ||
13 | error('ERROR: Need an image to rectify'); | ||
14 | break; | ||
15 | end; | ||
16 | end; | ||
17 | end; | ||
18 | end; | ||
19 | end; | ||
20 | |||
21 | |||
22 | if nargin < 7, | ||
23 | if nargin < 6, | ||
24 | KK_new = [f(1) 0 c(1);0 f(2) c(2);0 0 1]; | ||
25 | else | ||
26 | KK_new = alpha; % the 6th argument is actually KK_new | ||
27 | end; | ||
28 | alpha = 0; | ||
29 | end; | ||
30 | |||
31 | |||
32 | |||
33 | % Note: R is the motion of the points in space | ||
34 | % So: X2 = R*X where X: coord in the old reference frame, X2: coord in the new ref frame. | ||
35 | |||
36 | |||
37 | if ~exist('KK_new'), | ||
38 | KK_new = [f(1) alpha_c*fc(1) c(1);0 f(2) c(2);0 0 1]; | ||
39 | end; | ||
40 | |||
41 | |||
42 | [nr,nc] = size(I); | ||
43 | |||
44 | Irec = 255*ones(nr,nc); | ||
45 | |||
46 | [mx,my] = meshgrid(1:nc, 1:nr); | ||
47 | px = reshape(mx',nc*nr,1); | ||
48 | py = reshape(my',nc*nr,1); | ||
49 | |||
50 | rays = inv(KK_new)*[(px - 1)';(py - 1)';ones(1,length(px))]; | ||
51 | |||
52 | |||
53 | % Rotation: (or affine transformation): | ||
54 | |||
55 | rays2 = R'*rays; | ||
56 | |||
57 | x = [rays2(1,:)./rays2(3,:);rays2(2,:)./rays2(3,:)]; | ||
58 | |||
59 | |||
60 | % Add distortion: | ||
61 | |||
62 | k1 = k(1); | ||
63 | k2 = k(2); | ||
64 | |||
65 | p1 = k(3); | ||
66 | p2 = k(4); | ||
67 | |||
68 | r_2 = sum(x.^2); | ||
69 | |||
70 | delta_x = [2*p1*x(1,:).*x(2,:) + p2*(r_2 + 2*x(1,:).^2) ; | ||
71 | p1 * (r_2 + 2*x(2,:).^2)+2*p2*x(1,:).*x(2,:)]; | ||
72 | |||
73 | xd = (ones(2,1)*( 1 + k1 * r_2 + k2 * r_2.^2)) .* x + delta_x; | ||
74 | |||
75 | |||
76 | % Reconvert in pixels: | ||
77 | |||
78 | px2 = f(1)*(xd(1,:)+alpha_c*xd(2,:))+c(1); | ||
79 | py2 = f(2)*xd(2,:)+c(2); | ||
80 | |||
81 | |||
82 | % Interpolate between the closest pixels: | ||
83 | |||
84 | px_0 = floor(px2); | ||
85 | px_1 = px_0 + 1; | ||
86 | alpha_x = px2 - px_0; | ||
87 | |||
88 | py_0 = floor(py2); | ||
89 | py_1 = py_0 + 1; | ||
90 | alpha_y = py2 - py_0; | ||
91 | |||
92 | good_points = find((px_0 >= 0) & (px_1 <= (nc-1)) & (py_0 >= 0) & (py_1 <= (nr-1))); | ||
93 | |||
94 | I_lu = I(px_0(good_points) * nr + py_0(good_points) + 1); | ||
95 | I_ru = I(px_1(good_points) * nr + py_0(good_points) + 1); | ||
96 | I_ld = I(px_0(good_points) * nr + py_1(good_points) + 1); | ||
97 | I_rd = I(px_1(good_points) * nr + py_1(good_points) + 1); | ||
98 | |||
99 | |||
100 | I_interp = (1 - alpha_y(good_points)).*((1 - alpha_x(good_points)).* I_lu + alpha_x(good_points) .* I_ru) + alpha_y(good_points) .* ((1 - alpha_x(good_points)).* I_ld + alpha_x(good_points) .* I_rd); | ||
101 | |||
102 | |||
103 | Irec((px(good_points)-1)*nr + py(good_points)) = I_interp; | ||
104 | |||
105 | |||
106 | |||
107 | return; | ||
108 | |||
109 | |||
110 | % Convert in indices: | ||
111 | |||
112 | fact = 3; | ||
113 | |||
114 | [XX,YY]= meshgrid(1:nc,1:nr); | ||
115 | [XXi,YYi]= meshgrid(1:1/fact:nc,1:1/fact:nr); | ||
116 | |||
117 | %tic; | ||
118 | Iinterp = interp2(XX,YY,I,XXi,YYi); | ||
119 | %toc | ||
120 | |||
121 | [nri,nci] = size(Iinterp); | ||
122 | |||
123 | |||
124 | ind_col = round(fact*(f(1)*xd(1,:)+c(1)))+1; | ||
125 | ind_row = round(fact*(f(2)*xd(2,:)+c(2)))+1; | ||
126 | |||
127 | good_points = find((ind_col >=1)&(ind_col<=nci)&(ind_row >=1)& (ind_row <=nri)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/reproject_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/reproject_calib.m new file mode 100755 index 0000000..d3ad3d2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/reproject_calib.m | |||
@@ -0,0 +1,121 @@ | |||
1 | %%%%%%%%%%%%%%%%%%%% REPROJECT ON THE IMAGES %%%%%%%%%%%%%%%%%%%%%%%% | ||
2 | |||
3 | if ~exist('n_ima')|~exist('fc'), | ||
4 | fprintf(1,'No calibration data available.\n'); | ||
5 | return; | ||
6 | end; | ||
7 | |||
8 | if ~exist('no_image'), | ||
9 | no_image = 0; | ||
10 | end; | ||
11 | |||
12 | if ~exist('nx')&~exist('ny'), | ||
13 | fprintf(1,'WARNING: No image size (nx,ny) available. Setting nx=640 and ny=480\n'); | ||
14 | nx = 640; | ||
15 | ny = 480; | ||
16 | end; | ||
17 | |||
18 | |||
19 | check_active_images; | ||
20 | |||
21 | |||
22 | % Color code for each image: | ||
23 | |||
24 | colors = 'brgkcm'; | ||
25 | |||
26 | % Reproject the patterns on the images, and compute the pixel errors: | ||
27 | |||
28 | % Reload the images if necessary | ||
29 | |||
30 | if ~exist(['omc_' num2str(ind_active(1)) ]), | ||
31 | fprintf(1,'Need to calibrate before showing image reprojection. Maybe need to load Calib_Results.mat file.\n'); | ||
32 | return; | ||
33 | end; | ||
34 | |||
35 | if ~no_image, | ||
36 | if ~exist(['I_' num2str(ind_active(1)) ]'), | ||
37 | n_ima_save = n_ima; | ||
38 | active_images_save = active_images; | ||
39 | ima_read_calib; | ||
40 | n_ima = n_ima_save; | ||
41 | active_images = active_images_save; | ||
42 | check_active_images; | ||
43 | if no_image_file, | ||
44 | fprintf(1,'WARNING: Do not show the original images\n'); %return; | ||
45 | end; | ||
46 | end; | ||
47 | else | ||
48 | no_image_file = 1; | ||
49 | end; | ||
50 | |||
51 | |||
52 | if ~exist('dont_ask'), | ||
53 | dont_ask = 0; | ||
54 | end; | ||
55 | |||
56 | |||
57 | if ~dont_ask, | ||
58 | ima_numbers = input('Number(s) of image(s) to show ([] = all images) = '); | ||
59 | else | ||
60 | ima_numbers = []; | ||
61 | end; | ||
62 | |||
63 | |||
64 | if isempty(ima_numbers), | ||
65 | ima_proc = 1:n_ima; | ||
66 | else | ||
67 | ima_proc = ima_numbers; | ||
68 | end; | ||
69 | |||
70 | |||
71 | figure(5); | ||
72 | for kk = ima_proc, %1:n_ima, | ||
73 | if exist(['y_' num2str(kk)]), | ||
74 | if active_images(kk) & eval(['~isnan(y_' num2str(kk) '(1,1))']), | ||
75 | eval(['plot(ex_' num2str(kk) '(1,:),ex_' num2str(kk) '(2,:),''' colors(rem(kk-1,6)+1) '+'');']); | ||
76 | hold on; | ||
77 | end; | ||
78 | end; | ||
79 | end; | ||
80 | hold off; | ||
81 | axis('equal'); | ||
82 | title('Reprojection error (in pixel)'); | ||
83 | xlabel('x'); | ||
84 | ylabel('y'); | ||
85 | drawnow; | ||
86 | |||
87 | set(5,'Name','error','NumberTitle','off'); | ||
88 | |||
89 | |||
90 | |||
91 | for kk = ima_proc, | ||
92 | if exist(['y_' num2str(kk)]), | ||
93 | if active_images(kk) & eval(['~isnan(y_' num2str(kk) '(1,1))']), | ||
94 | |||
95 | if exist(['I_' num2str(kk)]), | ||
96 | eval(['I = I_' num2str(kk) ';']); | ||
97 | else | ||
98 | I = 255*ones(ny,nx); | ||
99 | end; | ||
100 | |||
101 | figure(5+kk); | ||
102 | image(I); hold on; | ||
103 | colormap(gray(256)); | ||
104 | title(['Image ' num2str(kk) ' - Image points (+) and reprojected grid points (o)']); | ||
105 | eval(['plot(x_' num2str(kk) '(1,:)+1,x_' num2str(kk) '(2,:)+1,''r+'');']); | ||
106 | eval(['plot(y_' num2str(kk) '(1,:)+1,y_' num2str(kk) '(2,:)+1,''' colors(rem(kk-1,6)+1) 'o'');']); | ||
107 | zoom on; | ||
108 | axis([1 nx 1 ny]); | ||
109 | hold off; | ||
110 | drawnow; | ||
111 | |||
112 | set(5+kk,'Name',num2str(kk),'NumberTitle','off'); | ||
113 | |||
114 | end; | ||
115 | end; | ||
116 | end; | ||
117 | |||
118 | |||
119 | err_std = std(ex')'; | ||
120 | |||
121 | fprintf(1,'Pixel error: err = [%3.5f %3.5f] (all active images)\n\n',err_std); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rigid_motion.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rigid_motion.m new file mode 100755 index 0000000..473405c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rigid_motion.m | |||
@@ -0,0 +1,66 @@ | |||
1 | function [Y,dYdom,dYdT] = rigid_motion(X,om,T); | ||
2 | |||
3 | %rigid_motion.m | ||
4 | % | ||
5 | %[Y,dYdom,dYdT] = rigid_motion(X,om,T) | ||
6 | % | ||
7 | %Computes the rigid motion transformation Y = R*X+T, where R = rodrigues(om). | ||
8 | % | ||
9 | %INPUT: X: 3D structure in the world coordinate frame (3xN matrix for N points) | ||
10 | % (om,T): Rigid motion parameters between world coordinate frame and camera reference frame | ||
11 | % om: rotation vector (3x1 vector); T: translation vector (3x1 vector) | ||
12 | % | ||
13 | %OUTPUT: Y: 3D coordinates of the structure points in the camera reference frame (3xN matrix for N points) | ||
14 | % dYdom: Derivative of Y with respect to om ((3N)x3 matrix) | ||
15 | % dYdT: Derivative of Y with respect to T ((3N)x3 matrix) | ||
16 | % | ||
17 | %Definitions: | ||
18 | %Let P be a point in 3D of coordinates X in the world reference frame (stored in the matrix X) | ||
19 | %The coordinate vector of P in the camera reference frame is: Y = R*X + T | ||
20 | %where R is the rotation matrix corresponding to the rotation vector om: R = rodrigues(om); | ||
21 | % | ||
22 | %Important function called within that program: | ||
23 | % | ||
24 | %rodrigues.m: Computes the rotation matrix corresponding to a rotation vector | ||
25 | |||
26 | |||
27 | |||
28 | if nargin < 3, | ||
29 | T = zeros(3,1); | ||
30 | if nargin < 2, | ||
31 | om = zeros(3,1); | ||
32 | if nargin < 1, | ||
33 | error('Need at least a 3D structure as input (in rigid_motion.m)'); | ||
34 | return; | ||
35 | end; | ||
36 | end; | ||
37 | end; | ||
38 | |||
39 | |||
40 | [R,dRdom] = rodrigues(om); | ||
41 | |||
42 | [m,n] = size(X); | ||
43 | |||
44 | Y = R*X + T*ones(1,n); | ||
45 | |||
46 | if nargout > 1, | ||
47 | |||
48 | |||
49 | dYdR = zeros(3*n,9); | ||
50 | dYdT = zeros(3*n,3); | ||
51 | |||
52 | dYdR(1:3:end,1:3:end) = X'; | ||
53 | dYdR(2:3:end,2:3:end) = X'; | ||
54 | dYdR(3:3:end,3:3:end) = X'; | ||
55 | |||
56 | dYdT(1:3:end,1) = ones(n,1); | ||
57 | dYdT(2:3:end,2) = ones(n,1); | ||
58 | dYdT(3:3:end,3) = ones(n,1); | ||
59 | |||
60 | dYdom = dYdR * dRdom; | ||
61 | |||
62 | end; | ||
63 | |||
64 | |||
65 | |||
66 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rodrigues.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rodrigues.m new file mode 100755 index 0000000..9d55337 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rodrigues.m | |||
@@ -0,0 +1,217 @@ | |||
1 | function [out,dout]=rodrigues(in) | ||
2 | |||
3 | % RODRIGUES Transform rotation matrix into rotation vector and viceversa. | ||
4 | % | ||
5 | % Sintax: [OUT]=RODRIGUES(IN) | ||
6 | % If IN is a 3x3 rotation matrix then OUT is the | ||
7 | % corresponding 3x1 rotation vector | ||
8 | % if IN is a rotation 3-vector then OUT is the | ||
9 | % corresponding 3x3 rotation matrix | ||
10 | % | ||
11 | |||
12 | %% | ||
13 | %% Copyright (c) March 1993 -- Pietro Perona | ||
14 | %% California Institute of Technology | ||
15 | %% | ||
16 | |||
17 | %% ALL CHECKED BY JEAN-YVES BOUGUET, October 1995. | ||
18 | %% FOR ALL JACOBIAN MATRICES !!! LOOK AT THE TEST AT THE END !! | ||
19 | |||
20 | %% BUG when norm(om)=pi fixed -- April 6th, 1997; | ||
21 | %% Jean-Yves Bouguet | ||
22 | |||
23 | |||
24 | [m,n] = size(in); | ||
25 | %bigeps = 10e+4*eps; | ||
26 | bigeps = 10e+20*eps; | ||
27 | |||
28 | if ((m==1) & (n==3)) | ((m==3) & (n==1)) %% it is a rotation vector | ||
29 | theta = norm(in); | ||
30 | if theta < eps | ||
31 | R = eye(3); | ||
32 | |||
33 | %if nargout > 1, | ||
34 | |||
35 | dRdin = [0 0 0; | ||
36 | 0 0 1; | ||
37 | 0 -1 0; | ||
38 | 0 0 -1; | ||
39 | 0 0 0; | ||
40 | 1 0 0; | ||
41 | 0 1 0; | ||
42 | -1 0 0; | ||
43 | 0 0 0]; | ||
44 | |||
45 | %end; | ||
46 | |||
47 | else | ||
48 | if n==length(in) in=in'; end; %% make it a column vec. if necess. | ||
49 | |||
50 | %m3 = [in,theta] | ||
51 | |||
52 | dm3din = [eye(3);in'/theta]; | ||
53 | |||
54 | omega = in/theta; | ||
55 | |||
56 | %m2 = [omega;theta] | ||
57 | |||
58 | dm2dm3 = [eye(3)/theta -in/theta^2; zeros(1,3) 1]; | ||
59 | |||
60 | alpha = cos(theta); | ||
61 | beta = sin(theta); | ||
62 | gamma = 1-cos(theta); | ||
63 | omegav=[[0 -omega(3) omega(2)];[omega(3) 0 -omega(1)];[-omega(2) omega(1) 0 ]]; | ||
64 | A = omega*omega'; | ||
65 | |||
66 | %m1 = [alpha;beta;gamma;omegav;A]; | ||
67 | |||
68 | dm1dm2 = zeros(21,4); | ||
69 | dm1dm2(1,4) = -sin(theta); | ||
70 | dm1dm2(2,4) = cos(theta); | ||
71 | dm1dm2(3,4) = sin(theta); | ||
72 | dm1dm2(4:12,1:3) = [0 0 0 0 0 1 0 -1 0; | ||
73 | 0 0 -1 0 0 0 1 0 0; | ||
74 | 0 1 0 -1 0 0 0 0 0]'; | ||
75 | |||
76 | w1 = omega(1); | ||
77 | w2 = omega(2); | ||
78 | w3 = omega(3); | ||
79 | |||
80 | dm1dm2(13:21,1) = [2*w1;w2;w3;w2;0;0;w3;0;0]; | ||
81 | dm1dm2(13: 21,2) = [0;w1;0;w1;2*w2;w3;0;w3;0]; | ||
82 | dm1dm2(13:21,3) = [0;0;w1;0;0;w2;w1;w2;2*w3]; | ||
83 | |||
84 | R = eye(3)*alpha + omegav*beta + A*gamma; | ||
85 | |||
86 | dRdm1 = zeros(9,21); | ||
87 | |||
88 | dRdm1([1 5 9],1) = ones(3,1); | ||
89 | dRdm1(:,2) = omegav(:); | ||
90 | dRdm1(:,4:12) = beta*eye(9); | ||
91 | dRdm1(:,3) = A(:); | ||
92 | dRdm1(:,13:21) = gamma*eye(9); | ||
93 | |||
94 | dRdin = dRdm1 * dm1dm2 * dm2dm3 * dm3din; | ||
95 | |||
96 | |||
97 | end; | ||
98 | out = R; | ||
99 | dout = dRdin; | ||
100 | |||
101 | %% it is prob. a rot matr. | ||
102 | elseif ((m==n) & (m==3) & (norm(in' * in - eye(3)) < bigeps)... | ||
103 | & (abs(det(in)-1) < bigeps)) | ||
104 | R = in; | ||
105 | |||
106 | |||
107 | |||
108 | tr = (trace(R)-1)/2; | ||
109 | dtrdR = [1 0 0 0 1 0 0 0 1]/2; | ||
110 | theta = real(acos(tr)); | ||
111 | |||
112 | |||
113 | if sin(theta) >= 1e-5, | ||
114 | |||
115 | dthetadtr = -1/sqrt(1-tr^2); | ||
116 | |||
117 | dthetadR = dthetadtr * dtrdR; | ||
118 | % var1 = [vth;theta]; | ||
119 | vth = 1/(2*sin(theta)); | ||
120 | dvthdtheta = -vth*cos(theta)/sin(theta); | ||
121 | dvar1dtheta = [dvthdtheta;1]; | ||
122 | |||
123 | dvar1dR = dvar1dtheta * dthetadR; | ||
124 | |||
125 | |||
126 | om1 = [R(3,2)-R(2,3), R(1,3)-R(3,1), R(2,1)-R(1,2)]'; | ||
127 | |||
128 | dom1dR = [0 0 0 0 0 1 0 -1 0; | ||
129 | 0 0 -1 0 0 0 1 0 0; | ||
130 | 0 1 0 -1 0 0 0 0 0]; | ||
131 | |||
132 | % var = [om1;vth;theta]; | ||
133 | dvardR = [dom1dR;dvar1dR]; | ||
134 | |||
135 | % var2 = [om;theta]; | ||
136 | om = vth*om1; | ||
137 | domdvar = [vth*eye(3) om1 zeros(3,1)]; | ||
138 | dthetadvar = [0 0 0 0 1]; | ||
139 | dvar2dvar = [domdvar;dthetadvar]; | ||
140 | |||
141 | |||
142 | out = om*theta; | ||
143 | domegadvar2 = [theta*eye(3) om]; | ||
144 | |||
145 | dout = domegadvar2 * dvar2dvar * dvardR; | ||
146 | |||
147 | |||
148 | else | ||
149 | if tr > 0; % case norm(om)=0; | ||
150 | |||
151 | out = [0 0 0]'; | ||
152 | |||
153 | dout = [0 0 0 0 0 1/2 0 -1/2 0; | ||
154 | 0 0 -1/2 0 0 0 1/2 0 0; | ||
155 | 0 1/2 0 -1/2 0 0 0 0 0]; | ||
156 | else % case norm(om)=pi; %% fixed April 6th | ||
157 | |||
158 | |||
159 | out = theta * (sqrt((diag(R)+1)/2).*[1;2*(R(1,2:3)>=0)'-1]); | ||
160 | %keyboard; | ||
161 | |||
162 | if nargout > 1, | ||
163 | fprintf(1,'WARNING!!!! Jacobian domdR undefined!!!\n'); | ||
164 | dout = NaN*ones(3,9); | ||
165 | end; | ||
166 | end; | ||
167 | end; | ||
168 | |||
169 | else | ||
170 | error('Neither a rotation matrix nor a rotation vector were provided'); | ||
171 | end; | ||
172 | |||
173 | return; | ||
174 | |||
175 | %% test of the Jacobians: | ||
176 | |||
177 | %%%% TEST OF dRdom: | ||
178 | om = randn(3,1); | ||
179 | dom = randn(3,1)/1000000; | ||
180 | |||
181 | [R1,dR1] = rodrigues(om); | ||
182 | R2 = rodrigues(om+dom); | ||
183 | |||
184 | R2a = R1 + reshape(dR1 * dom,3,3); | ||
185 | |||
186 | gain = norm(R2 - R1)/norm(R2 - R2a) | ||
187 | |||
188 | %%% TEST OF dOmdR: | ||
189 | om = randn(3,1); | ||
190 | R = rodrigues(om); | ||
191 | dom = randn(3,1)/10000; | ||
192 | dR = rodrigues(om+dom) - R; | ||
193 | |||
194 | [omc,domdR] = rodrigues(R); | ||
195 | [om2] = rodrigues(R+dR); | ||
196 | |||
197 | om_app = omc + domdR*dR(:); | ||
198 | |||
199 | gain = norm(om2 - omc)/norm(om2 - om_app) | ||
200 | |||
201 | |||
202 | %%% OTHER BUG: (FIXED NOW!!!) | ||
203 | |||
204 | omu = randn(3,1); | ||
205 | omu = omu/norm(omu) | ||
206 | om = pi*omu; | ||
207 | [R,dR]= rodrigues(om); | ||
208 | [om2] = rodrigues(R); | ||
209 | [om om2] | ||
210 | |||
211 | %%% NORMAL OPERATION | ||
212 | |||
213 | om = randn(3,1); | ||
214 | [R,dR]= rodrigues(om); | ||
215 | [om2] = rodrigues(R); | ||
216 | [om om2] | ||
217 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rotation.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rotation.m new file mode 100755 index 0000000..87ee2fe --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rotation.m | |||
@@ -0,0 +1,23 @@ | |||
1 | function [] = rotation(st); | ||
2 | |||
3 | if nargin < 1, | ||
4 | st= 1; | ||
5 | end; | ||
6 | |||
7 | |||
8 | fig = gcf; | ||
9 | |||
10 | ax = gca; | ||
11 | |||
12 | vv = get(ax,'view'); | ||
13 | |||
14 | az = vv(1); | ||
15 | el = vv(2); | ||
16 | |||
17 | for azi = az:-abs(st):az-360, | ||
18 | |||
19 | set(ax,'view',[azi el]); | ||
20 | figure(fig); | ||
21 | drawnow; | ||
22 | |||
23 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/run_error_analysis.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/run_error_analysis.m new file mode 100755 index 0000000..095e17e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/run_error_analysis.m | |||
@@ -0,0 +1,65 @@ | |||
1 | %%% Program that launchs the complete | ||
2 | |||
3 | for N_ima_active = 1:30, | ||
4 | |||
5 | error_analysis; | ||
6 | |||
7 | end; | ||
8 | |||
9 | |||
10 | |||
11 | return; | ||
12 | |||
13 | |||
14 | f = []; | ||
15 | f_std = []; | ||
16 | |||
17 | c = []; | ||
18 | c_std = []; | ||
19 | |||
20 | k = []; | ||
21 | k_std = []; | ||
22 | |||
23 | NN = 30; | ||
24 | |||
25 | for rr = 1:NN, | ||
26 | |||
27 | load(['Calib_Accuracies_' num2str(rr)]); | ||
28 | |||
29 | [m1,s1] = mean_std_robust(fc_list(1,:)); | ||
30 | [m2,s2] = mean_std_robust(fc_list(2,:)); | ||
31 | |||
32 | f = [f [m1;m2]]; | ||
33 | f_std = [f_std [s1;s2]]; | ||
34 | |||
35 | [m1,s1] = mean_std_robust(cc_list(1,:)); | ||
36 | [m2,s2] = mean_std_robust(cc_list(2,:)); | ||
37 | |||
38 | c = [c [m1;m2]]; | ||
39 | c_std = [c_std [s1;s2]]; | ||
40 | |||
41 | [m1,s1] = mean_std_robust(kc_list(1,:)); | ||
42 | [m2,s2] = mean_std_robust(kc_list(2,:)); | ||
43 | [m3,s3] = mean_std_robust(kc_list(3,:)); | ||
44 | [m4,s4] = mean_std_robust(kc_list(4,:)); | ||
45 | |||
46 | k = [k [m1;m2;m3;m4]]; | ||
47 | k_std = [k_std [s1;s2;s3;s4]]; | ||
48 | |||
49 | end; | ||
50 | |||
51 | figure(1); | ||
52 | errorbar([1:NN;1:NN]',f',f_std'); | ||
53 | figure(2); | ||
54 | errorbar([1:NN;1:NN]',c',c_std'); | ||
55 | figure(3); | ||
56 | errorbar([1:NN;1:NN;1:NN;1:NN]',k',k_std'); | ||
57 | |||
58 | figure(4); | ||
59 | semilogy(f_std'); | ||
60 | |||
61 | figure(5); | ||
62 | semilogy(c_std'); | ||
63 | |||
64 | figure(6); | ||
65 | semilogy(k_std'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/saveinr.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/saveinr.m new file mode 100755 index 0000000..a176e39 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/saveinr.m | |||
@@ -0,0 +1,46 @@ | |||
1 | %SAVEINR Write an INRIMAGE format file | ||
2 | % | ||
3 | % SAVEINR(filename, im) | ||
4 | % | ||
5 | % Saves the specified image array in a INRIA image format file. | ||
6 | % | ||
7 | % SEE ALSO: loadinr | ||
8 | % | ||
9 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
10 | |||
11 | % Peter Corke 1996 | ||
12 | |||
13 | function saveinr(fname, im) | ||
14 | |||
15 | fid = fopen(fname, 'w'); | ||
16 | [r,c] = size(im'); | ||
17 | |||
18 | % build the header | ||
19 | hdr = []; | ||
20 | s = sprintf('#INRIMAGE-4#{\n'); | ||
21 | hdr = [hdr s]; | ||
22 | s = sprintf('XDIM=%d\n',c); | ||
23 | hdr = [hdr s]; | ||
24 | s = sprintf('YDIM=%d\n',r); | ||
25 | hdr = [hdr s]; | ||
26 | s = sprintf('ZDIM=1\n'); | ||
27 | hdr = [hdr s]; | ||
28 | s = sprintf('VDIM=1\n'); | ||
29 | hdr = [hdr s]; | ||
30 | s = sprintf('TYPE=float\n'); | ||
31 | hdr = [hdr s]; | ||
32 | s = sprintf('PIXSIZE=32\n'); | ||
33 | hdr = [hdr s]; | ||
34 | s = sprintf('SCALE=2**0\n'); | ||
35 | hdr = [hdr s]; | ||
36 | s = sprintf('CPU=sun\n#'); | ||
37 | hdr = [hdr s]; | ||
38 | |||
39 | % make it 256 bytes long and write it | ||
40 | hdr256 = zeros(1,256); | ||
41 | hdr256(1:length(hdr)) = hdr; | ||
42 | fwrite(fid, hdr256, 'uchar'); | ||
43 | |||
44 | % now the binary data | ||
45 | fwrite(fid, im', 'float32'); | ||
46 | fclose(fid) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/savepgm.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/savepgm.m new file mode 100755 index 0000000..0cee75d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/savepgm.m | |||
@@ -0,0 +1,22 @@ | |||
1 | %SAVEPGM Write a PGM format file | ||
2 | % | ||
3 | % SAVEPGM(filename, im) | ||
4 | % | ||
5 | % Saves the specified image array in a binary (P5) format PGM image file. | ||
6 | % | ||
7 | % SEE ALSO: loadpgm | ||
8 | % | ||
9 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
10 | |||
11 | |||
12 | % Peter Corke 1994 | ||
13 | |||
14 | function savepgm(fname, im) | ||
15 | |||
16 | fid = fopen(fname, 'w'); | ||
17 | [r,c] = size(im'); | ||
18 | fprintf(fid, 'P5\n'); | ||
19 | fprintf(fid, '%d %d\n', r, c); | ||
20 | fprintf(fid, '255\n'); | ||
21 | fwrite(fid, im', 'uchar'); | ||
22 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/saveppm.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/saveppm.m new file mode 100755 index 0000000..ece092b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/saveppm.m | |||
@@ -0,0 +1,45 @@ | |||
1 | %SAVEPPM Write a PPM format file | ||
2 | % | ||
3 | % SAVEPPM(filename, I) | ||
4 | % | ||
5 | % Saves the specified red, green and blue planes in a binary (P6) | ||
6 | % format PPM image file. | ||
7 | % | ||
8 | % SEE ALSO: loadppm | ||
9 | % | ||
10 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
11 | |||
12 | |||
13 | % Peter Corke 1994 | ||
14 | |||
15 | function saveppm(fname, I) | ||
16 | |||
17 | I = double(I); | ||
18 | |||
19 | if size(I,3) == 1, | ||
20 | R = I; | ||
21 | G = I; | ||
22 | B = I; | ||
23 | else | ||
24 | R = I(:,:,1); | ||
25 | G = I(:,:,2); | ||
26 | B = I(:,:,3); | ||
27 | end; | ||
28 | |||
29 | %keyboard; | ||
30 | |||
31 | fid = fopen(fname, 'w'); | ||
32 | [r,c] = size(R'); | ||
33 | fprintf(fid, 'P6\n'); | ||
34 | fprintf(fid, '%d %d\n', r, c); | ||
35 | fprintf(fid, '255\n'); | ||
36 | R = R'; | ||
37 | G = G'; | ||
38 | B = B'; | ||
39 | im = [R(:) G(:) B(:)]; | ||
40 | %im = reshape(im,r,c*3); | ||
41 | im = im'; | ||
42 | %im = im(:); | ||
43 | fwrite(fid, im, 'uchar'); | ||
44 | fclose(fid); | ||
45 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/saving_calib.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/saving_calib.m new file mode 100755 index 0000000..3f98a8b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/saving_calib.m | |||
@@ -0,0 +1,95 @@ | |||
1 | |||
2 | if ~exist('n_ima')|~exist('fc'), | ||
3 | fprintf(1,'No calibration data available.\n'); | ||
4 | return; | ||
5 | end; | ||
6 | |||
7 | check_active_images; | ||
8 | |||
9 | if ~exist('solution_init'), solution_init = []; end; | ||
10 | |||
11 | for kk = 1:n_ima, | ||
12 | if ~exist(['dX_' num2str(kk)]), eval(['dX_' num2str(kk) '= dX;']); end; | ||
13 | if ~exist(['dY_' num2str(kk)]), eval(['dY_' num2str(kk) '= dY;']); end; | ||
14 | end; | ||
15 | |||
16 | if ~exist('param_list'), | ||
17 | param_list = solution; | ||
18 | end; | ||
19 | |||
20 | if ~exist('wintx'), | ||
21 | wintx = []; | ||
22 | winty = []; | ||
23 | end; | ||
24 | |||
25 | if ~exist('dX_default'), | ||
26 | dX_default = []; | ||
27 | dY_default = []; | ||
28 | end; | ||
29 | |||
30 | if ~exist('alpha_c'), | ||
31 | alpha_c = 0; | ||
32 | end; | ||
33 | |||
34 | for kk = 1:n_ima, | ||
35 | if ~exist(['y_' num2str(kk)]), | ||
36 | eval(['y_' num2str(kk) ' = NaN*ones(2,1);']); | ||
37 | end; | ||
38 | if ~exist(['n_sq_x_' num2str(kk)]), | ||
39 | eval(['n_sq_x_' num2str(kk) ' = NaN;']); | ||
40 | eval(['n_sq_y_' num2str(kk) ' = NaN;']); | ||
41 | end; | ||
42 | if ~exist(['wintx_' num2str(kk)]), | ||
43 | eval(['wintx_' num2str(kk) ' = NaN;']); | ||
44 | eval(['winty_' num2str(kk) ' = NaN;']); | ||
45 | end; | ||
46 | end; | ||
47 | |||
48 | save_name = 'Calib_Results'; | ||
49 | |||
50 | if exist([ save_name '.mat'])==2, | ||
51 | disp('WARNING: File Calib_Results.mat already exists'); | ||
52 | pfn = -1; | ||
53 | cont = 1; | ||
54 | while cont, | ||
55 | pfn = pfn + 1; | ||
56 | postfix = ['_old'num2str(pfn)]; | ||
57 | save_name = [ 'Calib_Results' postfix]; | ||
58 | cont = (exist([ save_name '.mat'])==2); | ||
59 | end; | ||
60 | copyfile('Calib_Results.mat',[save_name '.mat']); | ||
61 | disp(['Copying the current Calib_Results.mat file to ' save_name '.mat']); | ||
62 | end; | ||
63 | |||
64 | |||
65 | save_name = 'Calib_Results'; | ||
66 | |||
67 | if exist('calib_name'), | ||
68 | |||
69 | fprintf(1,['\nSaving calibration results under ' save_name '.mat\n']); | ||
70 | |||
71 | string_save = ['save ' save_name ' center_optim param_list active_images ind_active center_optim est_alpha est_dist fc kc cc alpha_c ex x y solution solution_init wintx winty n_ima type_numbering N_slots small_calib_image first_num image_numbers format_image calib_name Hcal Wcal nx ny map dX_default dY_default KK inv_KK dX dY']; | ||
72 | |||
73 | for kk = 1:n_ima, | ||
74 | string_save = [string_save ' X_' num2str(kk) ' x_' num2str(kk) ' y_' num2str(kk) ' ex_' num2str(kk) ' omc_' num2str(kk) ' Rc_' num2str(kk) ' Tc_' num2str(kk) ' H_' num2str(kk) ' n_sq_x_' num2str(kk) ' n_sq_y_' num2str(kk) ' wintx_' num2str(kk) ' winty_' num2str(kk) ' dX_' num2str(kk) ' dY_' num2str(kk)]; | ||
75 | end; | ||
76 | |||
77 | else | ||
78 | |||
79 | fprintf(1,['\nSaving calibration results under ' save_name '.mat (no image version)\n']); | ||
80 | |||
81 | string_save = ['save ' save_name ' center_optim param_list active_images ind_active center_optim est_alpha est_dist fc kc cc alpha_c ex x y solution solution_init wintx winty n_ima nx ny dX_default dY_default KK inv_KK dX dY']; | ||
82 | |||
83 | for kk = 1:n_ima, | ||
84 | string_save = [string_save ' X_' num2str(kk) ' x_' num2str(kk) ' y_' num2str(kk) ' ex_' num2str(kk) ' omc_' num2str(kk) ' Rc_' num2str(kk) ' Tc_' num2str(kk) ' H_' num2str(kk) ' n_sq_x_' num2str(kk) ' n_sq_y_' num2str(kk) ' wintx_' num2str(kk) ' winty_' num2str(kk) ' dX_' num2str(kk) ' dY_' num2str(kk)]; | ||
85 | end; | ||
86 | |||
87 | end; | ||
88 | |||
89 | |||
90 | |||
91 | %fprintf(1,'To load later click on Load\n'); | ||
92 | |||
93 | eval(string_save); | ||
94 | |||
95 | fprintf(1,'done\n'); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/script_fit_distortion.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/script_fit_distortion.m new file mode 100755 index 0000000..c5e5430 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/script_fit_distortion.m | |||
@@ -0,0 +1,39 @@ | |||
1 | |||
2 | satis_distort = 0; | ||
3 | |||
4 | disp(['Estimated focal: ' num2str(f_g) ' pixels']); | ||
5 | |||
6 | while ~satis_distort, | ||
7 | |||
8 | k_g = input('Guess for distortion factor kc ([]=0): '); | ||
9 | |||
10 | if isempty(k_g), k_g = 0; end; | ||
11 | |||
12 | xy_corners_undist = comp_distortion2([x' - c_g(1);y'-c_g(2)]/f_g,k_g); | ||
13 | |||
14 | xu = xy_corners_undist(1,:)'; | ||
15 | yu = xy_corners_undist(2,:)'; | ||
16 | |||
17 | [XXu] = projectedGrid ( [xu(1);yu(1)], [xu(2);yu(2)],[xu(3);yu(3)], [xu(4);yu(4)],n_sq_x+1,n_sq_y+1); % The full grid | ||
18 | |||
19 | XX = (ones(2,1)*(1 + k_g * sum(XXu.^2))) .* XXu; | ||
20 | XX(1,:) = f_g*XX(1,:)+c_g(1); | ||
21 | XX(2,:) = f_g*XX(2,:)+c_g(2); | ||
22 | |||
23 | figure(2); | ||
24 | image(I); | ||
25 | colormap(map); | ||
26 | zoom on; | ||
27 | hold on; | ||
28 | %plot(f_g*XXu(1,:)+c_g(1),f_g*XXu(2,:)+c_g(2),'ro'); | ||
29 | plot(XX(1,:),XX(2,:),'r+'); | ||
30 | title('The red crosses should be on the grid corners...'); | ||
31 | hold off; | ||
32 | |||
33 | satis_distort = input('Satisfied with distortion? ([]=no, other=yes) '); | ||
34 | |||
35 | satis_distort = ~isempty(satis_distort); | ||
36 | |||
37 | |||
38 | end; | ||
39 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/startup.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/startup.m new file mode 100755 index 0000000..aad0fa4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/startup.m | |||
@@ -0,0 +1,9 @@ | |||
1 | % Main camera calibration toolbox: | ||
2 | |||
3 | calib_gui; | ||
4 | |||
5 | %calib_gui; | ||
6 | |||
7 | path(pwd,path); | ||
8 | |||
9 | format compact | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/undistort_image.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/undistort_image.m new file mode 100755 index 0000000..d9a7574 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/undistort_image.m | |||
@@ -0,0 +1,193 @@ | |||
1 | %%% INPUT THE IMAGE FILE NAME: | ||
2 | |||
3 | if ~exist('fc')|~exist('cc')|~exist('kc')|~exist('alpha_c'), | ||
4 | fprintf(1,'No intrinsic camera parameters available.\n'); | ||
5 | return; | ||
6 | end; | ||
7 | |||
8 | KK = [fc(1) alpha_c*fc(1) cc(1);0 fc(2) cc(2) ; 0 0 1]; | ||
9 | |||
10 | disp('Program that undistorts images'); | ||
11 | disp('The intrinsic camera parameters are assumed to be known (previously computed)'); | ||
12 | |||
13 | fprintf(1,'\n'); | ||
14 | |||
15 | quest = input('Do you want to undistort all the calibration images ([],0) or a new image (1)? '); | ||
16 | |||
17 | if isempty(quest), | ||
18 | quest = 0; | ||
19 | end; | ||
20 | |||
21 | if ~quest, | ||
22 | |||
23 | if ~exist(['I_' num2str(ind_active(1))]), | ||
24 | ima_read_calib; | ||
25 | end; | ||
26 | |||
27 | check_active_images; | ||
28 | |||
29 | format_image2 = format_image; | ||
30 | if format_image2(1) == 'j', | ||
31 | format_image2 = 'bmp'; | ||
32 | end; | ||
33 | |||
34 | for kk = 1:n_ima, | ||
35 | |||
36 | if exist(['I_' num2str(kk)]), | ||
37 | |||
38 | eval(['I = I_' num2str(kk) ';']); | ||
39 | [I2] = rect(I,eye(3),fc,cc,kc,KK); | ||
40 | |||
41 | if ~type_numbering, | ||
42 | number_ext = num2str(image_numbers(kk)); | ||
43 | else | ||
44 | number_ext = sprintf(['%.' num2str(N_slots) 'd'],image_numbers(kk)); | ||
45 | end; | ||
46 | |||
47 | ima_name2 = [calib_name '_rect' number_ext '.' format_image2]; | ||
48 | |||
49 | fprintf(1,['Saving undistorted image under ' ima_name2 '...\n']); | ||
50 | |||
51 | |||
52 | if format_image2(1) == 'p', | ||
53 | if format_images2(2) == 'p', | ||
54 | saveppm(ima_name2,uint8(round(I2))); | ||
55 | else | ||
56 | savepgm(ima_name2,uint8(round(I2))); | ||
57 | end; | ||
58 | else | ||
59 | if format_image2(1) == 'r', | ||
60 | writeras(ima_name2,round(I2),gray(256)); | ||
61 | else | ||
62 | imwrite(uint8(round(I2)),gray(256),ima_name2,format_image2); | ||
63 | end; | ||
64 | end; | ||
65 | |||
66 | |||
67 | end; | ||
68 | |||
69 | end; | ||
70 | |||
71 | fprintf(1,'done\n'); | ||
72 | |||
73 | else | ||
74 | |||
75 | dir; | ||
76 | fprintf(1,'\n'); | ||
77 | |||
78 | image_name = input('Image name (full name without extension): ','s'); | ||
79 | |||
80 | format_image2 = '0'; | ||
81 | |||
82 | while format_image2 == '0', | ||
83 | |||
84 | format_image2 = input('Image format: ([]=''r''=''ras'', ''b''=''bmp'', ''t''=''tif'', ''p''=''pgm'', ''j''=''jpg'', ''m''=''ppm'') ','s'); | ||
85 | |||
86 | if isempty(format_image2), | ||
87 | format_image2 = 'ras'; | ||
88 | end; | ||
89 | |||
90 | if lower(format_image2(1)) == 'm', | ||
91 | format_image2 = 'ppm'; | ||
92 | else | ||
93 | if lower(format_image2(1)) == 'b', | ||
94 | format_image2 = 'bmp'; | ||
95 | else | ||
96 | if lower(format_image2(1)) == 't', | ||
97 | format_image2 = 'tif'; | ||
98 | else | ||
99 | if lower(format_image2(1)) == 'p', | ||
100 | format_image2 = 'pgm'; | ||
101 | else | ||
102 | if lower(format_image2(1)) == 'j', | ||
103 | format_image2 = 'jpg'; | ||
104 | else | ||
105 | if lower(format_image2(1)) == 'r', | ||
106 | format_image2 = 'ras'; | ||
107 | else | ||
108 | disp('Invalid image format'); | ||
109 | format_image2 = '0'; % Ask for format once again | ||
110 | end; | ||
111 | end; | ||
112 | end; | ||
113 | end; | ||
114 | end; | ||
115 | end; | ||
116 | end; | ||
117 | |||
118 | ima_name = [image_name '.' format_image2]; | ||
119 | |||
120 | |||
121 | %%% READ IN IMAGE: | ||
122 | |||
123 | if format_image2(1) == 'p', | ||
124 | if format_image2(2) == 'p', | ||
125 | I = double(loadppm(ima_name)); | ||
126 | else | ||
127 | I = double(loadpgm(ima_name)); | ||
128 | end; | ||
129 | else | ||
130 | if format_image2(1) == 'r', | ||
131 | I = readras(ima_name); | ||
132 | else | ||
133 | I = double(imread(ima_name)); | ||
134 | end; | ||
135 | end; | ||
136 | |||
137 | if size(I,3)>1, | ||
138 | I = I(:,:,2); | ||
139 | end; | ||
140 | |||
141 | |||
142 | if (size(I,1)>ny)|(size(I,2)>nx), | ||
143 | I = I(1:ny,1:nx); | ||
144 | end; | ||
145 | |||
146 | |||
147 | %% SHOW THE ORIGINAL IMAGE: | ||
148 | |||
149 | figure(2); | ||
150 | image(I); | ||
151 | colormap(gray(256)); | ||
152 | title('Original image (with distortion) - Stored in array I'); | ||
153 | drawnow; | ||
154 | |||
155 | |||
156 | %% UNDISTORT THE IMAGE: | ||
157 | |||
158 | fprintf(1,'Computing the undistorted image...') | ||
159 | |||
160 | [I2] = rect(I,eye(3),fc,cc,kc,alpha_c,KK); | ||
161 | |||
162 | fprintf(1,'done\n'); | ||
163 | |||
164 | figure(3); | ||
165 | image(I2); | ||
166 | colormap(gray(256)); | ||
167 | title('Undistorted image - Stored in array I2'); | ||
168 | drawnow; | ||
169 | |||
170 | |||
171 | %% SAVE THE IMAGE IN FILE: | ||
172 | |||
173 | ima_name2 = [image_name '_rect.' format_image2]; | ||
174 | |||
175 | fprintf(1,['Saving undistorted image under ' ima_name2 '...']); | ||
176 | |||
177 | if format_image2(1) == 'p', | ||
178 | if format_images2(2) == 'p', | ||
179 | saveppm(ima_name2,uint8(round(I2))); | ||
180 | else | ||
181 | savepgm(ima_name2,uint8(round(I2))); | ||
182 | end; | ||
183 | else | ||
184 | if format_image2(1) == 'r', | ||
185 | writeras(ima_name2,round(I2),gray(256)); | ||
186 | else | ||
187 | imwrite(uint8(round(I2)),gray(256),ima_name2,format_image2); | ||
188 | end; | ||
189 | end; | ||
190 | |||
191 | fprintf(1,'done\n'); | ||
192 | |||
193 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/willson_convert.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/willson_convert.m new file mode 100755 index 0000000..8946349 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/willson_convert.m | |||
@@ -0,0 +1,89 @@ | |||
1 | function [fc,cc,kc,Rc,Tc,omc,nx,ny] = willson_convert(Ncx,Nfx,dx,dy,dpx,dpy,Cx,Cy,sx,f,kappa1,Tx,Ty,Tz,Rx,Ry,Rz,p1,p2); | ||
2 | |||
3 | %Conversion from Reg Willson's calibration format to my format | ||
4 | |||
5 | % Conversion: | ||
6 | |||
7 | % Focal length: | ||
8 | fc = [sx/dpx ; 1/dpy]*f; | ||
9 | |||
10 | % Principal point; | ||
11 | cc = [Cx;Cy]; | ||
12 | |||
13 | % Extrinsic parameters: | ||
14 | Rx = rodrigues([Rx;0;0]); | ||
15 | Ry = rodrigues([0;Ry;0]); | ||
16 | Rz = rodrigues([0;0;Rz]); | ||
17 | |||
18 | Rc = Rz * Ry * Rx; | ||
19 | |||
20 | omc = rodrigues(Rc); | ||
21 | |||
22 | Tc = [Tx;Ty;Tz]; | ||
23 | |||
24 | |||
25 | % More tricky: Take care of the distorsion: | ||
26 | |||
27 | Nfy = round(Nfx * 3/4); | ||
28 | |||
29 | nx = Nfx; | ||
30 | ny = Nfy; | ||
31 | |||
32 | % Select a set of DISTORTED coordinates uniformely distributed across the image: | ||
33 | |||
34 | [xp_dist,yp_dist] = meshgrid(0:Nfx-1,0:Nfy); | ||
35 | |||
36 | xp_dist = xp_dist(:)'; | ||
37 | yp_dist = yp_dist(:)'; | ||
38 | |||
39 | |||
40 | % Apply UNDISTORTION according to Willson: | ||
41 | |||
42 | xp_sensor_dist = dpx*(xp_dist - Cx)/sx; | ||
43 | yp_sensor_dist = dpy*(yp_dist - Cy); | ||
44 | |||
45 | dist_fact = 1 + kappa1*(xp_sensor_dist.^2 + yp_sensor_dist.^2); | ||
46 | |||
47 | xp_sensor = xp_sensor_dist .* dist_fact; | ||
48 | yp_sensor = yp_sensor_dist .* dist_fact; | ||
49 | |||
50 | xp = xp_sensor * sx / dpx + Cx; | ||
51 | yp = yp_sensor / dpy + Cy; | ||
52 | |||
53 | ind= find((xp > 0) & (xp < Nfx-1) & (yp > 0) & (yp < Nfy-1)); | ||
54 | |||
55 | xp = xp(ind); | ||
56 | yp = yp(ind); | ||
57 | xp_dist = xp_dist(ind); | ||
58 | yp_dist = yp_dist(ind); | ||
59 | |||
60 | |||
61 | % Now, find my own set of parameters: | ||
62 | |||
63 | x_dist = [(xp_dist - cc(1))/fc(1);(yp_dist - cc(2))/fc(2)]; | ||
64 | x = [(xp - cc(1))/fc(1);(yp - cc(2))/fc(2)]; | ||
65 | |||
66 | k = [0;0;0;0]; | ||
67 | |||
68 | for kk = 1:5, | ||
69 | |||
70 | [xd,dxddk] = apply_distortion(x,k); | ||
71 | |||
72 | err = x_dist - xd; | ||
73 | |||
74 | %norm(err) | ||
75 | |||
76 | k_step = inv(dxddk'*dxddk)*(dxddk')*err(:); | ||
77 | |||
78 | k = k + k_step; %inv(dxddk'*dxddk)*(dxddk')*err(:); | ||
79 | |||
80 | %norm(k_step)/norm(k) | ||
81 | |||
82 | if norm(k_step)/norm(k) < 10e-10, | ||
83 | break; | ||
84 | end; | ||
85 | |||
86 | end; | ||
87 | |||
88 | |||
89 | kc = k; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/willson_read.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/willson_read.m new file mode 100755 index 0000000..bbde63c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/willson_read.m | |||
@@ -0,0 +1,59 @@ | |||
1 | % Read in Reg Willson's data file, and convert it into my data format: | ||
2 | |||
3 | %dir; | ||
4 | |||
5 | %calib_file = input('Reg Willson calibration file name: ','s'); | ||
6 | |||
7 | if exist(calib_file), | ||
8 | |||
9 | |||
10 | load(calib_file); | ||
11 | |||
12 | inddot = find(calib_file == '.'); | ||
13 | |||
14 | if isempty(inddot), | ||
15 | varname = calib_file; | ||
16 | else | ||
17 | varname = calib_file(1:inddot(1)-1); | ||
18 | end; | ||
19 | |||
20 | eval(['calib_params = ' varname ';']) | ||
21 | |||
22 | Ncx = calib_params(1); | ||
23 | Nfx = calib_params(2); | ||
24 | dx = calib_params(3); | ||
25 | dy = calib_params(4); | ||
26 | dpx = calib_params(5); | ||
27 | dpy = calib_params(6); | ||
28 | Cx = calib_params(7); | ||
29 | Cy = calib_params(8); | ||
30 | sx = calib_params(9); | ||
31 | f = calib_params(10); | ||
32 | kappa1 = calib_params(11); | ||
33 | Tx = calib_params(12); | ||
34 | Ty = calib_params(13); | ||
35 | Tz = calib_params(14); | ||
36 | Rx = calib_params(15); | ||
37 | Ry = calib_params(16); | ||
38 | Rz = calib_params(17); | ||
39 | p1 = calib_params(18); | ||
40 | p2 = calib_params(19); | ||
41 | |||
42 | % Conversion: | ||
43 | [fc,cc,kc,Rc_1,Tc_1,omc_1,nx,ny] = willson_convert(Ncx,Nfx,dx,dy,dpx,dpy,Cx,Cy,sx,f,kappa1,Tx,Ty,Tz,Rx,Ry,Rz,p1,p2); | ||
44 | |||
45 | KK = [fc(1) 0 cc(1);0 fc(2) cc(2) ; 0 0 1]; | ||
46 | |||
47 | n_ima = 1; | ||
48 | |||
49 | X_1 = [NaN;NaN;NaN]; | ||
50 | x_1 = [NaN;NaN]; | ||
51 | |||
52 | map = gray(256); | ||
53 | |||
54 | else | ||
55 | |||
56 | disp(['WARNING: Calibration file ' calib_file ' not found']); | ||
57 | |||
58 | end; | ||
59 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/writeras.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/writeras.m new file mode 100755 index 0000000..c7eb7bc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/writeras.m | |||
@@ -0,0 +1,105 @@ | |||
1 | function writeras(filename, image, map); | ||
2 | %WRITERAS Write an image file in sun raster format. | ||
3 | % WRITERAS('imagefile.ras', image_matrix, map) writes a | ||
4 | % "sun.raster" image file. | ||
5 | |||
6 | % Written by Thomas K. Leung 3/30/93. | ||
7 | % @ California Institute of Technology. | ||
8 | |||
9 | |||
10 | % PC and UNIX version of writeras - Jean-Yves Bouguet - Dec. 1998 | ||
11 | |||
12 | dot = max(find(filename == '.')); | ||
13 | suffix = filename(dot+1:dot+3); | ||
14 | |||
15 | if nargin < 3, | ||
16 | map = []; | ||
17 | end; | ||
18 | |||
19 | if(strcmp(suffix, 'ras')) | ||
20 | %Write header | ||
21 | |||
22 | fp = fopen(filename, 'wb'); | ||
23 | if(fp < 0) error(['Cannot open ' filename '.']), end | ||
24 | |||
25 | [height, width] = size(image); | ||
26 | image = image - 1; | ||
27 | mapsize = size(map, 1)*size(map,2); | ||
28 | %fwrite(fp, ... | ||
29 | % [1504078485, width, height, 8, height*width, 1, 1, mapsize], ... | ||
30 | % 'long'); | ||
31 | |||
32 | |||
33 | zero_str = '00000000'; | ||
34 | |||
35 | % MAGIC NUMBER: | ||
36 | |||
37 | |||
38 | fwrite(fp,89,'uchar'); | ||
39 | fwrite(fp,166,'uchar'); | ||
40 | fwrite(fp,106,'uchar'); | ||
41 | fwrite(fp,149,'uchar'); | ||
42 | |||
43 | width_str = dec2hex(width); | ||
44 | width_str = [zero_str(1:8-length(width_str)) width_str]; | ||
45 | |||
46 | for ii = 1:2:7, | ||
47 | fwrite(fp,hex2dec(width_str(ii:ii+1)),'uchar'); | ||
48 | end; | ||
49 | |||
50 | |||
51 | height_str = dec2hex(height); | ||
52 | height_str = [zero_str(1:8-length(height_str)) height_str]; | ||
53 | |||
54 | for ii = 1:2:7, | ||
55 | fwrite(fp,hex2dec(height_str(ii:ii+1)),'uchar'); | ||
56 | end; | ||
57 | |||
58 | fwrite(fp,0,'uchar'); | ||
59 | fwrite(fp,0,'uchar'); | ||
60 | fwrite(fp,0,'uchar'); | ||
61 | fwrite(fp,8,'uchar'); | ||
62 | |||
63 | ll = height*width; | ||
64 | ll_str = dec2hex(ll); | ||
65 | ll_str = [zero_str(1:8-length(ll_str)) ll_str]; | ||
66 | |||
67 | for ii = 1:2:7, | ||
68 | fwrite(fp,hex2dec(ll_str(ii:ii+1)),'uchar'); | ||
69 | end; | ||
70 | |||
71 | fwrite(fp,0,'uchar'); | ||
72 | fwrite(fp,0,'uchar'); | ||
73 | fwrite(fp,0,'uchar'); | ||
74 | fwrite(fp,1,'uchar'); | ||
75 | fwrite(fp,0,'uchar'); | ||
76 | fwrite(fp,0,'uchar'); | ||
77 | fwrite(fp,0,'uchar'); | ||
78 | fwrite(fp,~~mapsize,'uchar'); | ||
79 | |||
80 | mapsize_str = dec2hex(mapsize); | ||
81 | mapsize_str = [zero_str(1:8-length(mapsize_str)) mapsize_str]; | ||
82 | |||
83 | %keyboard; | ||
84 | |||
85 | for ii = 1:2:7, | ||
86 | fwrite(fp,hex2dec(mapsize_str(ii:ii+1)),'uchar'); | ||
87 | end; | ||
88 | |||
89 | |||
90 | if mapsize ~= 0 | ||
91 | map = min(255, fix(255*map)); | ||
92 | fwrite(fp, map, 'uchar'); | ||
93 | end | ||
94 | if rem(width,2) == 1 | ||
95 | image = [image'; zeros(1, height)]'; | ||
96 | top = 255 * ones(size(image)); | ||
97 | fwrite(fp, min(top,image)', 'uchar'); | ||
98 | else | ||
99 | top = 255 * ones(size(image)); | ||
100 | fwrite(fp, min(top,image)', 'uchar'); | ||
101 | end | ||
102 | fclose(fp); | ||
103 | else | ||
104 | error('Image file name must end in either ''ras'' or ''rast''.'); | ||
105 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/README b/SD-VBS/common/toolbox/toolbox_basic/affine/README new file mode 100755 index 0000000..e578a74 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/README | |||
@@ -0,0 +1,5 @@ | |||
1 | Top level program is "compute_AD.m". Use "compute_AD_disp.m" if one | ||
2 | wants to display results as program runs. | ||
3 | |||
4 | The testing programs are called "simulation.m" for synthetic images, | ||
5 | and "test_affine.m" for real images. | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/carve_it.m b/SD-VBS/common/toolbox/toolbox_basic/affine/carve_it.m new file mode 100755 index 0000000..1a44f89 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/carve_it.m | |||
@@ -0,0 +1,25 @@ | |||
1 | function img = carve_it(I,center,window_size_h) | ||
2 | |||
3 | [size_y,size_x]= size(I); | ||
4 | min_x = round(center(1)-window_size_h(1)); | ||
5 | max_x = round(center(1)+window_size_h(1)); | ||
6 | min_y = round(center(2)-window_size_h(2)); | ||
7 | max_y = round(center(2)+window_size_h(2)); | ||
8 | window_size = window_size_h*2 +1; | ||
9 | |||
10 | if (min_x <1)|(max_x > size_x)|(min_y<1)|(max_y>size_y), | ||
11 | disp('window too big'); | ||
12 | center | ||
13 | window_size_h | ||
14 | img = zeros(window_size(2),window_size(1)); | ||
15 | n_min_x = max(1,round(min_x)); | ||
16 | n_min_y = max(1,round(min_y)); | ||
17 | n_max_x = min(size_x,round(max_x)); | ||
18 | n_max_y = min(size_y,round(max_y)); | ||
19 | img(1+(n_min_y-min_y):window_size(2)-(max_y-n_max_y),1+(n_min_x-min_x):window_size(1)-(max_x-n_max_x))=I(n_min_y:n_max_y,n_min_x:n_max_x); | ||
20 | else | ||
21 | img = I(center(2)-window_size_h(2):center(2)+window_size_h(2),... | ||
22 | center(1)-window_size_h(1):center(1)+window_size_h(1)); | ||
23 | end | ||
24 | |||
25 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/compute_AD.m b/SD-VBS/common/toolbox/toolbox_basic/affine/compute_AD.m new file mode 100755 index 0000000..a39acd6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/compute_AD.m | |||
@@ -0,0 +1,90 @@ | |||
1 | function [A,D,mask] =... | ||
2 | compute_AD(img_i,img_j,center_i,center_j,window_size_h,num_iter,w,num_trans,Dest,mask) | ||
3 | % | ||
4 | % function [A,D,mask] = ... | ||
5 | % compute_AD(img_i,img_j,center_i,center_j,window_size_h,num_iter,w, | ||
6 | % mask,num_trans) | ||
7 | % | ||
8 | % A: Affine motion; | ||
9 | % D: Displacement; | ||
10 | % | ||
11 | % img_i, img_j: the two image(in full size); | ||
12 | % center_i, center_j: the centers of the feature in two images; | ||
13 | % window_size_h: half size of the feature window; | ||
14 | % num_iter: number of iterations; | ||
15 | % w: parameter used in "grad.m" for computing gaussians used for | ||
16 | % gradient estimation; | ||
17 | % | ||
18 | % num_trans: OPTIONAL, number of translation iteration; default = 3; | ||
19 | % mask: OPTIONAL, if some area of the square shaped feature window should | ||
20 | % be weighted less; | ||
21 | % | ||
22 | |||
23 | % | ||
24 | % Jianbo Shi | ||
25 | % | ||
26 | |||
27 | if ~exist('Dest'), | ||
28 | Dest = [0,0]; | ||
29 | end | ||
30 | |||
31 | if ~exist('mask'), | ||
32 | mask = ones(2*window_size_h+1)'; | ||
33 | end | ||
34 | |||
35 | % set the default num_trans | ||
36 | if ~exist('num_trans'), | ||
37 | num_trans= 3; | ||
38 | end | ||
39 | |||
40 | % normalize image intensity to the range of 0.0-1.0 | ||
41 | img_i = norm_inten(img_i); | ||
42 | img_j = norm_inten(img_j); | ||
43 | |||
44 | window_size = 2*window_size_h + 1; | ||
45 | I = carve_it(img_i,center_i,window_size_h); | ||
46 | J = carve_it(img_j,center_j,window_size_h); | ||
47 | |||
48 | % init. step | ||
49 | J_computed = I; | ||
50 | D_computed = Dest; | ||
51 | A_computed = eye(2); | ||
52 | J_computed = compute_J(A_computed,D_computed,img_i,center_i,window_size_h); | ||
53 | |||
54 | %% level of noise | ||
55 | sig = 0.1; | ||
56 | |||
57 | records = zeros(num_iter,6); | ||
58 | errs = zeros(1,num_iter); | ||
59 | |||
60 | k = 1; | ||
61 | % iteration | ||
62 | while k <= num_iter, | ||
63 | [A,D] = iter_AD(J_computed,J,mask,w,k,num_trans); | ||
64 | |||
65 | A_computed = A*A_computed; | ||
66 | D_computed = (A*D_computed')' + D; | ||
67 | |||
68 | % compute the warped image | ||
69 | J_computed = compute_J(A_computed,D_computed,img_i,center_i,window_size_h); | ||
70 | |||
71 | % compute the SSD error | ||
72 | errs(k) = sqrt(sum(sum((mask.*(J_computed-J)).^2)))/prod(size(J)); | ||
73 | |||
74 | % update the mask, discounting possible occlusion region | ||
75 | if (k>num_trans), | ||
76 | mask = exp(-abs(J_computed-J)/sig); | ||
77 | end | ||
78 | |||
79 | % record the A and D | ||
80 | records(k,:) = [reshape(A_computed,1,4),reshape(D_computed,1,2)]; | ||
81 | |||
82 | k = k+1; | ||
83 | end | ||
84 | |||
85 | [tmp,id] = min(errs); | ||
86 | A = reshape(records(id,1:4),2,2); | ||
87 | D = reshape(records(id,5:6),1,2); | ||
88 | |||
89 | J_computed = compute_J(A,D,img_i,center_i,window_size_h); | ||
90 | mask = exp(-abs(J_computed-J)/sig); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/compute_AD_disp.m b/SD-VBS/common/toolbox/toolbox_basic/affine/compute_AD_disp.m new file mode 100755 index 0000000..f2e6c62 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/compute_AD_disp.m | |||
@@ -0,0 +1,103 @@ | |||
1 | function [A,D,mask] =... | ||
2 | compute_AD_disp(img_i,img_j,center_i,center_j,window_size_h,num_iter,w,fig_disp,num_trans,Dest,mask) | ||
3 | % | ||
4 | % function [A,D,mask] = ... | ||
5 | % compute_AD_disp(img_i,img_j,center_i,center_j,window_size_h,num_iter,w, | ||
6 | % fig_disp,mask,num_trans) | ||
7 | % | ||
8 | % Computing affine transform for matching to image patches. Display results | ||
9 | % as program runs. | ||
10 | % | ||
11 | % A: Affine motion; | ||
12 | % D: Displacement; | ||
13 | % | ||
14 | % | ||
15 | % img_i, img_j: the two image(in full size); | ||
16 | % center_i, center_j: the centers of the feature in two images; | ||
17 | % window_size_h: half size of the feature window; | ||
18 | % num_iter: number of iterations; | ||
19 | % w: parameter used in "grad.m" for computing gaussians used for | ||
20 | % gradient estimation; | ||
21 | % fig_disp: figure for display; | ||
22 | % | ||
23 | % num_trans: OPTIONAL, number of translation iteration; | ||
24 | % mask: OPTIONAL, if some area of the square shaped feature window should | ||
25 | % be weighted less; | ||
26 | % | ||
27 | |||
28 | |||
29 | % | ||
30 | % Jianbo Shi | ||
31 | % | ||
32 | |||
33 | if ~exist('mask'), | ||
34 | mask = ones(2*window_size_h+1)'; | ||
35 | end | ||
36 | |||
37 | if ~exist('Dest'), | ||
38 | Dest = [0,0]; | ||
39 | end | ||
40 | |||
41 | % set the default num_trans | ||
42 | if ~exist('num_trans'), | ||
43 | num_trans= 3; | ||
44 | end | ||
45 | |||
46 | % normalize image intensity to the range of 0.0-1.0 | ||
47 | img_i = norm_inten(img_i); | ||
48 | img_j = norm_inten(img_j); | ||
49 | |||
50 | window_size = 2*window_size_h + 1; | ||
51 | I = carve_it(img_i,center_i,window_size_h); | ||
52 | J = carve_it(img_j,center_j,window_size_h); | ||
53 | |||
54 | % init. step | ||
55 | D_computed = Dest; | ||
56 | A_computed = eye(2); | ||
57 | J_computed = compute_J(A_computed,D_computed,img_i,center_i,window_size_h); | ||
58 | |||
59 | |||
60 | |||
61 | figure(fig_disp);subplot(1,3,1);imagesc(I);colormap(gray);axis('image'); | ||
62 | subplot(1,3,3);imagesc(J);axis('image'); | ||
63 | drawnow; | ||
64 | |||
65 | sig = 0.1; | ||
66 | |||
67 | records = zeros(num_iter,6); | ||
68 | errs = zeros(1,num_iter); | ||
69 | |||
70 | k = 1; | ||
71 | % iteration | ||
72 | while k <= num_iter, | ||
73 | [A,D] = iter_AD(J_computed,J,mask,w,k,num_trans); | ||
74 | |||
75 | A_computed = A*A_computed; | ||
76 | D_computed = (A*D_computed')' + D; | ||
77 | |||
78 | % compute the warped image | ||
79 | J_computed = compute_J(A_computed,D_computed,img_i,center_i,window_size_h); | ||
80 | |||
81 | % compute the SSD error | ||
82 | errs(k) = sqrt(sum(sum((mask.*(J_computed-J)).^2)))/prod(size(J)) | ||
83 | |||
84 | % update the mask, discounting possible occlusion region | ||
85 | if (k>num_trans+1), | ||
86 | mask = exp(-abs(J_computed-J)/sig); | ||
87 | end | ||
88 | |||
89 | % record the A and D | ||
90 | records(k,:) = [reshape(A_computed,1,4),reshape(D_computed,1,2)]; | ||
91 | |||
92 | figure(fig_disp);subplot(1,3,2);imagesc(J_computed);axis('image'); | ||
93 | title(sprintf('iter:%d: dx=%3.3f, dy = %3.3f',k,D_computed(1),D_computed(2)));drawnow; | ||
94 | |||
95 | k = k+1; | ||
96 | end | ||
97 | |||
98 | [tmp,id] = min(errs); | ||
99 | A = reshape(records(id,1:4),2,2); | ||
100 | D = reshape(records(id,5:6),1,2); | ||
101 | |||
102 | J_computed = compute_J(A,D,img_i,center_i,window_size_h); | ||
103 | mask = exp(-abs(J_computed-J)/sig); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/compute_J.m b/SD-VBS/common/toolbox/toolbox_basic/affine/compute_J.m new file mode 100755 index 0000000..80db273 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/compute_J.m | |||
@@ -0,0 +1,31 @@ | |||
1 | function [JJ,mask] = compute_J(A,D,I,center,window_size_h) | ||
2 | %% function J = compute_J(A,D,I,center,window_size_h) | ||
3 | % | ||
4 | |||
5 | [size_y,size_x] = size(I); | ||
6 | |||
7 | center_x = center(1); | ||
8 | center_y = center(2); | ||
9 | |||
10 | [XX,YY] = meshgrid(1:size_x,1:size_y); | ||
11 | x = reshape(XX,size_x*size_y,1); | ||
12 | y = reshape(YY,size_x*size_y,1); | ||
13 | index(:,1) = x-center_x; | ||
14 | index(:,2) = y-center_y; | ||
15 | |||
16 | position_new = A*index'+ [D(1),0;0,D(2)]*ones(2,size_x*size_y); | ||
17 | position_new(1,:) = position_new(1,:)+center_x; | ||
18 | position_new(2,:) = position_new(2,:)+center_y; | ||
19 | |||
20 | position_new_x = reshape(position_new(1,:),size_y,size_x); | ||
21 | position_new_y = reshape(position_new(2,:),size_y,size_x); | ||
22 | |||
23 | [J,mask]= m_interp4(I,position_new_x,position_new_y); | ||
24 | |||
25 | JJ = J(center(2)-window_size_h(2):center(2)+window_size_h(2),... | ||
26 | center(1)-window_size_h(1):center(1)+window_size_h(1)); | ||
27 | mask = mask(center(2)-window_size_h(2):center(2)+window_size_h(2),... | ||
28 | center(1)-window_size_h(1):center(1)+window_size_h(1)); | ||
29 | |||
30 | |||
31 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/find_AD.m b/SD-VBS/common/toolbox/toolbox_basic/affine/find_AD.m new file mode 100755 index 0000000..3cccefb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/find_AD.m | |||
@@ -0,0 +1,82 @@ | |||
1 | function [A,D] = find_AD(I,J,mask,w) | ||
2 | % | ||
3 | % [A,D] = find_AD(I,J,mask,w) | ||
4 | % | ||
5 | % find the matrix affine transform A and displacement D, | ||
6 | % such that SSD difference of I(Ax-d)-J(x) is minimized, | ||
7 | % | ||
8 | |||
9 | % | ||
10 | % Jianbo Shi | ||
11 | % | ||
12 | |||
13 | |||
14 | [gy1,gx1] = grad(I,w); | ||
15 | [gy2,gx2] = grad(J,w); | ||
16 | |||
17 | gx = 0.5*(gx1+gx2); | ||
18 | gy = 0.5*(gy1+gy2); | ||
19 | |||
20 | [size_y,size_x] = size(I); | ||
21 | [center_y,center_x] = find_center(size_y,size_x); | ||
22 | mask = mask(w+1:size_y-w,w+1:size_x-w); | ||
23 | |||
24 | [x,y] = meshgrid(1:size_x,1:size_y); | ||
25 | x = x- center_x; | ||
26 | y = y-center_y; | ||
27 | |||
28 | x = x(w+1:size_y-w,w+1:size_x-w); | ||
29 | y = y(w+1:size_y-w,w+1:size_x-w); | ||
30 | |||
31 | gx_sqr = gx.*mask.*gx; | ||
32 | gx_gy = gx.*mask.*gy; | ||
33 | gy_sqr = gy.*mask.*gy; | ||
34 | |||
35 | x_sqr = x.*x; | ||
36 | x_y = x.*y; | ||
37 | y_sqr = y.*y; | ||
38 | |||
39 | T= zeros(6,6); | ||
40 | T(1,1) = 0.5*trapz(trapz(gx_sqr.*x_sqr)); | ||
41 | T(2,1) = trapz(trapz(gx_gy.*x_y)); | ||
42 | T(3,1) = trapz(trapz(gx_sqr.*x_y)); | ||
43 | T(4,1) = trapz(trapz(gx_gy.*x_sqr)); | ||
44 | T(5,1) = trapz(trapz(gx_sqr.*x)); | ||
45 | T(6,1) = trapz(trapz(gx_gy.*x)); | ||
46 | T(2,2) = 0.5*trapz(trapz(gy_sqr.*y_sqr)); | ||
47 | T(3,2) = trapz(trapz(gx_gy.*y_sqr)); | ||
48 | T(4,2) = trapz(trapz(gy_sqr.*x_y)); | ||
49 | T(5,2) = trapz(trapz(gx_gy.*y)); | ||
50 | T(6,2) = trapz(trapz(gy_sqr.*y)); | ||
51 | T(3,3) = 0.5*trapz(trapz(gx_sqr.*y_sqr)); | ||
52 | T(4,3) = trapz(trapz(gx_gy.*x_y)); | ||
53 | T(5,3) = trapz(trapz(gx_sqr.*y)); | ||
54 | T(6,3) = trapz(trapz(gx_gy.*y)); | ||
55 | T(4,4) = 0.5*trapz(trapz(gy_sqr.*x_sqr)); | ||
56 | T(5,4) = trapz(trapz(gx_gy.*x)); | ||
57 | T(6,4) = trapz(trapz(gy_sqr.*x)); | ||
58 | T(5,5) = 0.5*trapz(trapz(gx_sqr)); | ||
59 | T(6,5) = trapz(trapz(gx_gy)); | ||
60 | T(6,6) = 0.5*trapz(trapz(gy_sqr)); | ||
61 | |||
62 | T = T+T'; | ||
63 | |||
64 | J = J(w+1:size_y-w,w+1:size_x-w); | ||
65 | I = I(w+1:size_y-w,w+1:size_x-w); | ||
66 | |||
67 | |||
68 | diff = (J-I).*mask; | ||
69 | b(1) = trapz(trapz(diff.*gx.*x)); | ||
70 | b(2) = trapz(trapz(diff.*gy.*y)); | ||
71 | b(3) = trapz(trapz(diff.*gx.*y)); | ||
72 | b(4) = trapz(trapz(diff.*gy.*x)); | ||
73 | b(5) = trapz(trapz(diff.*gx)); | ||
74 | b(6) = trapz(trapz(diff.*gy)); | ||
75 | |||
76 | a = inv(T)*b'; | ||
77 | |||
78 | A = [1+a(1), a(3); | ||
79 | a(4),1+a(2)]; | ||
80 | |||
81 | D= [a(5),a(6)]; | ||
82 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/find_D.m b/SD-VBS/common/toolbox/toolbox_basic/affine/find_D.m new file mode 100755 index 0000000..1e42cb2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/find_D.m | |||
@@ -0,0 +1,65 @@ | |||
1 | function D = find_D(I,J,mask,w) | ||
2 | % | ||
3 | % function D = find_D(I,J,mask,w) | ||
4 | % | ||
5 | % find the vector D such that it minimizes then | ||
6 | % difference between I(Ax-d)-J(x). | ||
7 | % | ||
8 | % mask: the weight matrix, | ||
9 | % w: window size for estimating gradiant, use a large value | ||
10 | % when A,D are large. | ||
11 | % | ||
12 | |||
13 | % | ||
14 | % NOTE: Because gradient values on the boarder regions of | ||
15 | % I and J can not be computed accuately when using | ||
16 | % a gaussian of large support, those boarder regions | ||
17 | % of width w are not used in computing D. | ||
18 | % | ||
19 | |||
20 | % | ||
21 | % Jianbo Shi | ||
22 | % | ||
23 | |||
24 | [gy1,gx1] = grad(I,w); | ||
25 | [gy2,gx2] = grad(J,w); | ||
26 | |||
27 | gx = 0.5*(gx1+gx2); | ||
28 | gy = 0.5*(gy1+gy2); | ||
29 | |||
30 | [size_y,size_x] = size(I); | ||
31 | [center_y,center_x] = find_center(size_y,size_x); | ||
32 | mask = mask(w+1:size_y-w,w+1:size_x-w); | ||
33 | |||
34 | [x,y] = meshgrid(1:size_x,1:size_y); | ||
35 | x = x- center_x; | ||
36 | y = y-center_y; | ||
37 | |||
38 | x = x(w+1:size_y-w,w+1:size_x-w); | ||
39 | y = y(w+1:size_y-w,w+1:size_x-w); | ||
40 | |||
41 | gx_sqr = gx.*mask.*gx; | ||
42 | gx_gy = gx.*mask.*gy; | ||
43 | gy_sqr = gy.*mask.*gy; | ||
44 | |||
45 | |||
46 | T= zeros(2,2); | ||
47 | |||
48 | T(1,1) = 0.5*trapz(trapz(gx_sqr)); | ||
49 | T(2,1) = trapz(trapz(gx_gy)); | ||
50 | T(2,2) = 0.5*trapz(trapz(gy_sqr)); | ||
51 | |||
52 | T = T+T'; | ||
53 | |||
54 | J = J(w+1:size_y-w,w+1:size_x-w); | ||
55 | I = I(w+1:size_y-w,w+1:size_x-w); | ||
56 | |||
57 | |||
58 | diff = (J-I).*mask; | ||
59 | b(1) = trapz(trapz(diff.*gx)); | ||
60 | b(2) = trapz(trapz(diff.*gy)); | ||
61 | |||
62 | a = inv(T)*b'; | ||
63 | |||
64 | D= [a(1),a(2)]; | ||
65 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/find_center.m b/SD-VBS/common/toolbox/toolbox_basic/affine/find_center.m new file mode 100755 index 0000000..b12ac7b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/find_center.m | |||
@@ -0,0 +1,4 @@ | |||
1 | function [center_x,center_y] = find_center(size_x,size_y) | ||
2 | |||
3 | center_x = 0.5*(size_x +1); | ||
4 | center_y = 0.5*(size_y +1); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/gen_feature_s.m b/SD-VBS/common/toolbox/toolbox_basic/affine/gen_feature_s.m new file mode 100755 index 0000000..3c113e9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/gen_feature_s.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function I = gen_feature_s(size_of_feature) | ||
2 | % function I = gen_feature(size_of_feature) | ||
3 | % generates a spherical features with size | ||
4 | % of "size_of_feature" | ||
5 | % | ||
6 | |||
7 | ss = round(0.4*size_of_feature); | ||
8 | [X,Y,II] = hemisphere_s(ss); | ||
9 | |||
10 | II = abs(II); | ||
11 | II = 1/max(max(II))*II; | ||
12 | |||
13 | I = zeros(size_of_feature,size_of_feature); | ||
14 | |||
15 | t = round((size_of_feature-ss)/2); | ||
16 | |||
17 | I(1+t:1+t+ss,1+t:1+t+ss) = II; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/grad.m b/SD-VBS/common/toolbox/toolbox_basic/affine/grad.m new file mode 100755 index 0000000..53bab55 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/grad.m | |||
@@ -0,0 +1,24 @@ | |||
1 | % gradient of an image | ||
2 | % coordinates (r, c) follow matrix convention; | ||
3 | % the gaussian is truncated at x = +- tail, and there are samples samples | ||
4 | % inbetween, where samples = hsamples * 2 + 1 | ||
5 | |||
6 | function[gr,gc] = gradient(image, hsamples) | ||
7 | |||
8 | tail=4; | ||
9 | samples = hsamples * 2 + 1; | ||
10 | |||
11 | x = linspace(-tail, tail, samples); | ||
12 | gauss = exp(-x.^2); | ||
13 | n = gauss * ones(samples,1); | ||
14 | gauss = gauss/n; | ||
15 | |||
16 | gaussderiv = -x.*gauss; | ||
17 | n = -gaussderiv*linspace(1,samples,samples)'; | ||
18 | gaussderiv = gaussderiv/n; | ||
19 | |||
20 | gr = conv2(conv2(image, gaussderiv','valid'), gauss,'valid'); | ||
21 | gc = conv2(conv2(image, gaussderiv,'valid'), gauss','valid'); | ||
22 | |||
23 | %gr = conv2(conv2(image, gaussderiv','same'), gauss,'same'); | ||
24 | %gc = conv2(conv2(image, gaussderiv,'same'), gauss','same'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/hemisphere_s.m b/SD-VBS/common/toolbox/toolbox_basic/affine/hemisphere_s.m new file mode 100755 index 0000000..5300183 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/hemisphere_s.m | |||
@@ -0,0 +1,27 @@ | |||
1 | function [x,y,z] = hemisphere(r) | ||
2 | %HEMISPHERE Generate sphere and transform from spherical coordinates. | ||
3 | % | ||
4 | % [X,Y,Z] = HEMISPHERE(N) generates three (n+1)-by-(n+1) | ||
5 | % matrices so that SURF(X,Y,Z) produces a sphere. | ||
6 | % | ||
7 | % [X,Y,Z] = HEMISPHERE(R,N) generates three (n+1)-by-(n+1) | ||
8 | % matrices so that SURF(X,Y,Z,R) produces a sphere colored by R | ||
9 | % | ||
10 | % [X,Y,Z] = HEMISPHERE(R,THETA,PHI) converts from spherical coordinates | ||
11 | % to cartesian coordinates. | ||
12 | |||
13 | % Modified from | ||
14 | % Clay M. Thompson 4-24-91 | ||
15 | % Copyright (c) 1991-92 by the MathWorks, Inc. | ||
16 | % by Carlo Tomasi | ||
17 | |||
18 | error(nargchk(1,3,nargin)); | ||
19 | |||
20 | n = r; | ||
21 | % 0 <= theta <= 2*pi and 0 <= phi <= pi/2 | ||
22 | [theta,phi] = meshgrid((pi/n/2)*[-n:2:n],(pi/2/n)*[-n:2:n]); | ||
23 | r = ones(n+1,n+1); | ||
24 | |||
25 | x = r .* cos(phi) .* sin(theta); | ||
26 | y = r .* sin(phi); | ||
27 | z = r .* cos(phi) .* cos(theta).*phi.*theta; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/im.m b/SD-VBS/common/toolbox/toolbox_basic/affine/im.m new file mode 100755 index 0000000..6450120 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/im.m | |||
@@ -0,0 +1,3 @@ | |||
1 | function im(I) | ||
2 | |||
3 | imagesc(I);axis('image');drawnow; \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/iter_AD.m b/SD-VBS/common/toolbox/toolbox_basic/affine/iter_AD.m new file mode 100755 index 0000000..50bdae1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/iter_AD.m | |||
@@ -0,0 +1,26 @@ | |||
1 | function [A,D] = iter_AD(I,J,mask,w,k,num_trans) | ||
2 | % | ||
3 | % function [A,D] = iter_AD(I,J,mask,w,k,num_trans) | ||
4 | % | ||
5 | % find the affine motion A, and displacement D, | ||
6 | % such that difference between I(Ax-D) and J(x) is minimized. | ||
7 | % If k <= num_trans, only translation is computed. This is useful | ||
8 | % in practice, when translation is relative large. | ||
9 | % | ||
10 | % mask: the weight matrix, | ||
11 | % w: window size for estimating gradiant, use a large value | ||
12 | % when A,D are large. | ||
13 | % | ||
14 | |||
15 | % | ||
16 | % Jianbo Shi | ||
17 | % | ||
18 | |||
19 | |||
20 | if k <= num_trans, | ||
21 | D = find_D(I,J,mask,w); | ||
22 | A = eye(2); | ||
23 | else | ||
24 | [A,D] = find_AD(I,J,mask,w); | ||
25 | end | ||
26 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/m_interp4.m b/SD-VBS/common/toolbox/toolbox_basic/affine/m_interp4.m new file mode 100755 index 0000000..314f140 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/m_interp4.m | |||
@@ -0,0 +1,49 @@ | |||
1 | function [F,mask] = m_interp4(z,s,t) | ||
2 | %INTERP4 2-D bilinear data interpolation. | ||
3 | % ZI = INTERP4(Z,XI,YI) assumes X = 1:N and Y = 1:M, where | ||
4 | % [M,N] = SIZE(Z). | ||
5 | % | ||
6 | % Copyright (c) 1984-93 by The MathWorks, Inc. | ||
7 | % Clay M. Thompson 4-26-91, revised 7-3-91, 3-22-93 by CMT. | ||
8 | % | ||
9 | % modified to | ||
10 | |||
11 | |||
12 | [nrows,ncols] = size(z); | ||
13 | |||
14 | |||
15 | if any(size(z)<[3 3]), error('Z must be at least 3-by-3.'); end | ||
16 | if size(s)~=size(t), error('XI and YI must be the same size.'); end | ||
17 | |||
18 | % Check for out of range values of s and set to 1 | ||
19 | sout = find((s<1)|(s>ncols)); | ||
20 | if length(sout)>0, s(sout) = ones(size(sout)); end | ||
21 | |||
22 | % Check for out of range values of t and set to 1 | ||
23 | tout = find((t<1)|(t>nrows)); | ||
24 | if length(tout)>0, t(tout) = ones(size(tout)); end | ||
25 | |||
26 | % Matrix element indexing | ||
27 | ndx = floor(t)+floor(s-1)*nrows; | ||
28 | |||
29 | % Compute intepolation parameters, check for boundary value. | ||
30 | d = find(s==ncols); | ||
31 | s(:) = (s - floor(s)); | ||
32 | if length(d)>0, s(d) = s(d)+1; ndx(d) = ndx(d)-nrows; end | ||
33 | |||
34 | % Compute intepolation parameters, check for boundary value. | ||
35 | d = find(t==nrows); | ||
36 | t(:) = (t - floor(t)); | ||
37 | if length(d)>0, t(d) = t(d)+1; ndx(d) = ndx(d)-1; end | ||
38 | d = []; | ||
39 | |||
40 | % Now interpolate, reuse u and v to save memory. | ||
41 | F = ( z(ndx).*(1-t) + z(ndx+1).*t ).*(1-s) + ... | ||
42 | ( z(ndx+nrows).*(1-t) + z(ndx+(nrows+1)).*t ).*s; | ||
43 | |||
44 | mask = ones(size(z)); | ||
45 | |||
46 | % Now set out of range values to zeros. | ||
47 | if length(sout)>0, F(sout) = zeros(size(sout));mask(sout)=zeros(size(sout));end | ||
48 | if length(tout)>0, F(tout) = zeros(size(tout));mask(tout)=zeros(size(tout));end | ||
49 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/norm_inten.m b/SD-VBS/common/toolbox/toolbox_basic/affine/norm_inten.m new file mode 100755 index 0000000..8e8865b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/norm_inten.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function I = norm_inten(J) | ||
2 | % | ||
3 | % I = norm_inten(J) | ||
4 | % | ||
5 | % normalize image intensity to the range of 0.0-1.0 | ||
6 | % | ||
7 | |||
8 | max_J = max(max(J)); | ||
9 | min_J = min(min(J)); | ||
10 | |||
11 | I = (J-min_J)/(max_J-min_J); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/pan.0.pgm b/SD-VBS/common/toolbox/toolbox_basic/affine/pan.0.pgm new file mode 100755 index 0000000..2e7b5f6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/pan.0.pgm | |||
@@ -0,0 +1,53 @@ | |||
1 | P5 | ||
2 | # CREATOR: XV Version 3.10a Rev: 12/29/94 | ||
3 | 128 96 | ||
4 | 255 | ||
5 | qquvvvvwzyz{}~€‚‚‚ƒ„††…†‡ˆˆŠŠŽŒŽ‘’“•–”–——›œž ¡ ©¤¢£¥¥¥¥§¥§©««¬°±²²¶¶µ´³²´³³¶¹»ÀÀ¿ÁÃÄÂÄÄÃÄÃÅÆÊÇÆÆÅÊÉÈÊÏÞËÄÃÄÂÂÃÃÀÁ»qrrsvxwxyzzz{{|~~€‚ƒƒ…………†‡†‡ˆ‰ŠŒŽŽŽ‘’’“”•–•“•˜™šœœŸŸŸ¡¡Ÿª°¥¤¦§¥©«ªªª«¬®°³´¶¶¹·¶··¸¸¹¹º½½¾¿»¼¿ÀÀÂÂÃÈÇÈÊËÉÈÊÊËÎÍÉØ×ÈÆÄÄÃÅÅÃÂÃÀtuwuruzzyzz{}}}‚„„„†‡‡‰‰‰‰‹‹‘”’’““•—–˜——™š›žžŸŸ¡¤¥¦¤¦·¬§¨©ª¬ª¬¯¯²³³¶·¹¹¹½¼½½¼¼¼¾¿ÀÁÃÃÁÄÄÃÆÇÇÈÉÊÉÉÊÊÎÍËËÌËÍÞÌÂÄÂÂÃÅÅÃÁÃÂtvwxyxwwz{{z|{|}‚„„…‡ˆˆ‰‰Š‹‹Œ‘‘’“•—˜˜™™š›œžžž ¢¢¡¢£¢±º¬©ªª«¬®®®°³´µ¸¹¹¹»¿¾¾½¾¾¾½¿¿ÂÂÁÀÀ¿¿¿ÂÆÈËÎÒÔÖ×ÖÔÓÐÎÎÎßâÒÕØÜááááàÜÙÕtvwwyz{ywz}}‚‚ƒ†‰‰ˆˆ‡‡ˆŒ‘“”’“–—˜™š™šš›œœ›œžœœž Ÿ š ´œ•™š¡¥¨¬®®®ª©ªª¬ª¨¦¥¢ÇÕÃÄÅÅÅÇÅÅÅÂÅÆÇÇÊËËÍÊÍÐÐÑÐÑâÖÓÓÔÖ×Ô×ÖÖ××Ðm„zuuvvvurvuwxz{{{}€ƒ„ƒ„††‡‰Šˆ‰ŠŒŽ“•™šœœ›œžž¡¢££¤¤¦¦©¨§¨§µ³¢¦ª®±³®¢žžžŸŸŸŸ ¤§©©ªª§ËÚÁÁÀÂÅÄÅÇÆÇËÍÎÏÐÏÍÏÑÒÔ×ÙÙãèÝÛÜÝÜÛÛÝÚØÙØÓBUw…xz}~}~{{€€‚„„ƒ…‡†ˆ‡Š‹‹Ž‘“”““•—˜˜š›Ÿ¡ Ÿ¢¤¦¦§¦ªª«®®±Á®±³µ°¡šš™š™™›¡¥§¨©«¥ÃàÈÇÊÎÍËÎÐÑÒÔÖÖÕØÖÖÚÞàáãâáîéâæåçéåâÞàâààÝGcZW„}}~}~ƒƒ„…„†‰‰‰‹‹‹‘‘’’’“••–—˜œŸŸžŸ ¡££¥¨§§ªª«®°°°¯ÅÀ¨³·µ 𡢡 ŸŸ ¤ª«¯¨ÄèÑÏÑÓÔרØ×ÛßÞßââãæéêêêêéíòéëììîíëíîïïííêGeoxae“~€‚‚ƒ~„ˆ‡‰‰ˆ‰‰‹Š’“”—šššœ ¡¢¡¡£¥¦¦¦¨¬®¯®¯°²²²²³´»Î·°¸¹µ¥ œš›œœž¢¦ª¬®®²¸²ÇëØ×××ÝàáåæéêìëïîìïîðððóóöôðñóôóôôööõõóòîFakswvey‚ƒ„……„€ƒˆ‰Š‹‹ŒŒ‘‘’”•–˜˜™š›Ÿ ¢£¥¨©ª¬¬¬®°°³´µ¹ºº½¾ÁÁ¿ÊÓº¿Ã¹±«¥§±²±®©©±´²³´±ËñáßáãæèìðòõõõöøùøõîæßØÏÓõðïñóõ÷ùõôöîëççïIbjnpu|x_‹Ž„††‡‰ˆ„‰Ž‘‘’”–———›œž ¢¤¥§ª©ª¬®¯°²¯®³´µ³²´¸º¼¿ÅÎâ¡nbb_eq†¦Â¿Ìå÷ýýýýýüüüüýýýýþþþþþþþþýýýýüüüüçæ¼®ã–½øúú§>¸áq±}̶Gbjkloqw|pn‹rcbbehq…‡ˆ‰‹Ž‘’”•–ššœžŸ¢¤¦©ª«®°¥Œ› “ »ÂÄÅÅÆÊÍÏÓ×ÙÜç«Õûúúü÷‹.ÃýýýýýýýüüøíüýÚÞîØüÝÈþþ«¨î–Ù»“ô臞‰fšb€’Žf0^xF_VYqH^ehkoopqy€f†ðþþþûä‹|”–˜–‹“¢£ ŸŸŸ¢¤§¨ª¬²±°°x;33>AV¨ËÐÒ×ÞáÌÆçñúã•ÄyÂýýýÉi5!q¸¬ŽcMi<2<3WWB=XFoZMqeHNtMdfV„wNi…v\uat}ˆŽc*dHY]ZtG]aced`C233++%)(*59:@7B>F>FEA=<\g^gb\^Ya][\PD83.2<Fgua?>X_=Hj?42)Wh3%GkgpD8€…€xb`vJQjFktVR|LvtTytLU{OdoU…}N]‡sSwZl‚Š‘[+e€IVYRqI[^___W?519+&'112>;9G<@;E8BG<:5V[FYYLPIHHMA4=A83:PMd{m@BdmUOi?$ByC!'Q_pS$l„‚|hSrJMqG^ugp‡Os~Rq|QRN\wM}€SYŒyQuPe‡’X#_€NQ_RpFX\]^YK512:)&(032>?9B=E:G7AJ@B;W\GRWHOHEJIB5>C@:4NQ^xp<5[njF€lJ, %pIQk]L*&X€w{qReNBnIGe’Ô¨Yj‡KayPR€XQ€Qu‚XQOtUb‰Ž”^%[QPcRoGUY]YNC445=)%+.01?98EAG=E6>H@B<S\KPY?LFKOKG9GA8>7BFRwvN\r>itK2WRJmRZ9(AepvPZ[BpVCf‡Î¨U]ŠN[zSKgU‡Pj‰bJ‘QpX\‹Ž”d%U…VPhNmFUYYTG>26;?*'+247><;HCM@I7<LCE:T[JKWAJJMVJMAGB394<GLny]#HpGY‡Y:!Eh-(ekhG'2zli{_UkBgfAXl³§YLˆWP~cO€qQ‚T_rM…†TlaZŒŽ”m'LƒYMiMrGVYVNC?368@.)+123?<9JEL@K;;K?D;QXNL\FOTQYOVKB@3:26FHg}o/ | ||
6 | 3oXKŒrJ*4r=UzhI'-%lzp|mWtF[n<GZŸžhIbO„nVSx]X~MŽUgkZŽŽ“u,H†^MoPqHTTPH?</4<?*),.+2><<I>J@H:;JAF>O[SM_PR]XVLSJ8>8565BD_v>)hiEwU-"lLGpcaHA$U~o~wRjLJrCCZ†ŒuDtsF}yPq„SmlT…MsY`tZŒŽ“~2CƒeMrRqIRSMGMXPF>A.**0.<C>APIR>L?9K;DEQ]XP`VS]\[OML5?C8?7BDX|~R[sBm‚_7[_"=cqfj_W;Is|~T\[HvRCa^DxGj…HqPg…W\yX‰ŠSe”e^ZŠ•„9@„kLrPmGOOIM[\MC>@+(*-,7>;<MDM=G<7G>GAJYUU_ZU^ZOKGP?IH2?5>GRu€e&KuK]”gH#DoNXhj]FE]jb|rvZRmIp_?fbCxV[‘NdƒQ\…[U…Qy‹W`”o[Š\‚‘”ˆA<qNvPmHOKFM[\NB>?)(,,+>BBDSHP@FA8ICG@F\WV]bS_\KLISOED2:4:FNpˆt85teTœhQ+*ENWOG6*+;PVm|x…kPpK`pVp–zuoZ–YW…_Z‹hP…PmŽ`[•{[“\}‘—ŽL;~wSvQiFIHDLXVJ@<>(''+)7AEESGO<GE;HBG@B_a[^^RTURVNWUGC3789KNh„}F*lxM‚zV3)7/5FHD:*"7JKTy~†yOiKQn69.bƒR„hR‡oW‡uT†Ve“m[–ˆ^‹]s‘–‘Q3t|PuRdDFECJTM?3 | ||
7 | |||
8 | ")+8ABBSFN=FD8HFHFB`a]^UONRRYRWVH=:787AA_‚ƒ`!aMd‘gN,!*5?>4*))(ATq~†„Q[VIt<6:RTq}U‚VV|a^˜x]^af‘–“V+nOoS]EFEELOB/&5?ABREK=CI;KLIH=b_\YVPMT[cZVVGDA4<6>BT}‡m+N|\[šf/"$');GGC7,%2W]l€ƒ^UhHk:+DE‡Z`ŽWw†Tr‡Vmn]˜ƒXŠ”byeb”•–\%h†QlV]GGHDJL;+.%%1'1-!<?BRIH>?L@GMIN@\aYTVVQWgaWPMKIC1:6<ES}|><€pY‚;%%"&/BPF<>9* ,NUVzƒmV{GW5 #FBuhZUj‹\c‹XiX“‹W}•epmc–•—c(^†Pf[[EEFDJH4$&(COEZsW.<CCNEFEAN?FQHM?Z_UTSZYc_`^QVTNI3726BKo‹ƒO+t}O<*-,2<@=71-*($%9GJn„}XlKD2";Dc}X‹^]‹b\ˆ^]ŒZˆUv—ijwd••˜k'XˆTbaXDDECJD+3N<.Av‚h:8B?NIDE>Q=GQDR:V]U^]`_`\^[M]VOI8868CIfˆ‰_!a‚_.')/3:1-35.+)#,DPFcƒƒbdY:1,;M[ŠXv\m\‹k[”\~•Wo˜pg‚a“–s(TŠUafUBCGBF:&&""$.ReV4 8A?NHDJ>O>DPISAP][dcb[ZSZfX`QBH?37/AD\„u0F~T.31./:?:'9DBGJW}…dVj@5"3@XXŒWo„^Ž}_…xY[n™bjžyjŒdŽ”—|+RŠ[ZlWDFGDA3+,!12'6FDIKJO>RBGUMUCQdcjfaWWT^gZXYFHK291<FW}’‚D6sC%8LPFFpmM,39j†t]qH'1LfT_d–\„_ŒY~egœlh¤gf‘”„4Q‡a[s[CDECB.'+-3'7EFLNKR?QDGRDMDJadh[UVY_ecUMXPTM18*2FQwˆU+^..?Xga\o„d4+8i„~aoN$RvUpsaœasŠ\qŽ^wmcŸzj¥Ši’g‡”–Œ>K‰hZuWHGDDE.# | ||
9 | !"9HGJOLUCPEHRKMFJaf`XW^_e`^aUa^KI5:.,FLkd$I#:WRQXXiy|wz‹s;1/]ƒaf]%! DuZg…\˜g`ŒWhŒ_i}e™‚c¡Žje|—˜’EDiXzVEGFDC1-)%0ZdH')3;=HGEMKVBQJJTJNHHfc`WOjg_X]dXabOK674+EId‹p6+!Njv„†„}}‚›v>-"S~ƒvŠ‚zˆ{šc…sbŽb`ˆfd„g˜a“o‹hx–••XA€s\[GFFFC/44,-ZkL+*0=AGFCILUHQLJPAMHA^ZX[OkcVXdfYX]LLA3:1>K^‡“„C $Tgmt{vuz{}‡”–|>%R|ž¦¦©¨«¯²©pn‚hŠs^ˆqb‰m‹’Zz”kfr–“”]<vw[}_EGIJSK>AOUaga^bcehnppprvvvtw{xz{x~€ƒƒ„‚„‡‡‡‰‰‡ŠŠŠ‹Œ‘”šœ˜’v)+UL>516HZU?8?QhsE!cª¯°®¯®¯°²±²²²´³´´³³³´µ³²µ³´µ±°±¯±°¯¯®®««¬«ªFFIL[bcfjlnoqruwyyz|~~}}„€€€€€€„……ƒƒƒƒ„………„‚ƒƒ‚ƒ„ƒ‚‚‚€‚zE:\?+)69:“|=.0BTx^28^nmkijiiijfffdbcb`aa`_]^^``_\\\\]^`__a`b`bdb_GHJM[XPNOQTTVVWYZZ[[\\^]]^_`aabbbbbbcdfgfggghikllmoppqqrqqrrsrB>eSLIE:[²˜C<LahjDZ~‡Š‹‹ŒŽŽ‘‘‘’’“”“”••——––———˜˜™˜˜˜˜˜˜—–•IJKK_kmmoqrsuwyz}~‚ƒ‚„†ˆˆ‰ˆ‰Œ‹‹ŒŒ’’‘’““”••—™›š››œ›ššžW.Ex†wft‹ £„aq‰œœg8Xž©¨«ª¬¬®®¯¬«««¬®®®¬«««ªª¬ª««ª©ªª©©ª¨¨¦¥¢HHJKZddcfhkmopttvxyz}~}}~‚‚ƒ‚„†……†‡‡ˆˆ‡ŠŠŠŠŠŠŠŒŒŒŒŒŠŠŠ‹Œs@;kž˜ŸŸ‰¥±®¬¢´·bNp‰‰‰‰ˆ‡††††…†„„ƒ€~}{zxwwwttrrppmmihhfdb`_][VIKMLI5 | ||
10 | |||
11 | |||
12 | |||
13 | |||
14 | |||
15 | |||
16 | |||
17 | |||
18 | 5H]vˆŸ§˜‚‹Ž´¸®¢‰bW* #$$&&%'&%$&'%$##&$$$#$$$##$$#!##$"IIIHG=* | ||
19 | |||
20 | (KUf~•žv1GCV¨´§—ƒvN$!-01* ! ! ! ILLJLE2 | ||
21 | |||
22 | |||
23 | |||
24 | |||
25 | |||
26 | |||
27 | ;R]qŠ–vHE`…¨¨™ŠsF'&0/!=?B' JJLJGB8(3>2,94>BB.7AB11<'' | ||
28 | "5$EXg€‘Œ~p€š©¤•ˆ|W5((..!!%GHN) <YT:EBMXS\jcIM\kfKJJJIE?)5B42@;FKH3@IG9;D | ||
29 | |||
30 | #nf$!" | ||
31 | ;Xfx€’µš˜š‘†|yr:T¤‰47A3,MSQ* $2 TigRMHYc]]ieMLbonIKLLIGB4DR;;HAVXY9M]^CN^' | ||
32 | "^Z%!"<iTIªœ<h–G:’œ™W7YcmZ<).+1LtŠ„{€‚=F†€Cy—o6…šs' Bk*B]RSKSr|qWSRhxqnvq\et€ƒLKJJKGE:VeCK\TkokBcruL[q/ | ||
33 | |||
34 | *m`%06$?hRJ§žBn—EQÃÈÈ΂0W`adw•–¥¡‡„otp5RŠt9^X7£Ê‡jßïÙæ1@1;QOQP`Š“|`d`„™‹ƒ“‰xŠž‘JLMLIHG;YoJUg]t}pEn}~L^o(*n[,PdC M©™?q—DT½ÄÄÌ!EY\bYQZ^hy}|wcql3T“u.5ªÏ_¾Õ¨º¿×€/C5LPTXNi‘—‰qpoš£ŒŽ•Šˆš¨ŽIKMKKIG<\rGZg^zmDpz~Mcn% | ||
35 | /td0g|K O¬—<xœBYÅÉÈÊ{?UXWL<4C_v{yyixo1\—w.;¯ÏzX º®È½áz>5*U{`<S“”€qs{¦ ‹’ІŒš¬ˆJLMNNKG>_uH\h_xfDt|~Ngk! /t_$$&T¬’={ž@^ÇÈÊËz @U\iux†’’‚xzx—s3Z”s-AµËyj«´ÄÁÛvO@2RRIQMx™“xst¢ž–Œ‚¦¬~KNNNOO_WesG_jbx‚fHw}Nnj | ||
36 | 5|]$##Z²’?—?bËÊÎÍpQdPY`inr}‡„|u}e¨š7V”u%OÀÏvl§¶¿»Ëz8HwgMeZƒ˜•xvr„£›•£¥•Œ¤¢rLNNNVgjYgvKegcy„gJx}~Qsi !#3_O#>EL™¥§\cOONWWRT]kqkozdÀ¯:1><"2LTSq«·¹ÐÙÀgM=FNHFIUŽ£‘qrq‰¥œ‘ œ–Ž–£œpOPRRdmkXfuQjhg~ˆjLzPxf | ||
37 | |||
38 | |||
39 | #&!]½mOKIOY_badin\‚ùø¨G:6$<WZ_y• ¿ԵH^\EKEAA[“¦‘rrsš±˜“£™•”›¦›oMMPTellUewWqfa€‰lLz€~S|b | ||
40 | "$5—ÆÁŸOMMJFMXafdVlãùûûöÃs1#">WZb§ÇÇܬNRJDKB>Bg›¨qvv•–˜« •—œ¥oMNQVgolUiv[sdc€‰qN}ƒ€S|b | ||
41 | |||
42 | %%Ÿôêۼ׎GOPMKQ\\SSÖøùûûûûûû¸=&FX[f€¦ÍÉá¶\<R^MVL?s¤¬rwwŸ®”žªœ–©§‡pMOPWhnmNez]we_€ŠtO|‚|Zƒ^ | ||
43 | |||
44 | $%J×ÚÛôè»ÄàwHMMLNOHZ«óøúûûùöûûûý¾l\]o•¡×çíˆ>KWZOUOF|ª‰uzz£”¡¨›™œ¥¨~tMPRXjqlJb|e{eby†zV~„|[†\ | ||
45 | |||
46 | !$' ÷ôãäóòص»¢nILJG;q¦Vªöüüüû÷úûûúúúÉfqœ²kCY‚aDGZbOVWQ“²©†zƒ¬°˜©¦ž›¡ª§wzKMPXinkC[}awhbˆ|Y‡|\‡Z | ||
47 | $%>¬ôüüôèæôõë·—\{€S@F‡•b„å÷üüüûôõûûúúúúûç¾¥lY\Z<=5czK^nb³¥{}†©–®§Ÿ¢§ t{KMQXjojFW€fyqf„Œ…`‡|_ŽY | ||
48 | '&KåôøüüüôêçôöôØžh]j<w¤‰v¾ïýüüüüùùûûúúúúûûûû°SPO5>7PŠwO|w©¸¤~€‚´ª˜¬ª¢¡¨›pzLNOZloh>N‚csyh††[|‰}aY %&NÐýûùüüüüôîìôööï·œˆexh´òú¿Îüýüüüûøùûûúúù÷úûûûôôôºG?5Lp^;^zµ·˜s€~•À« µ³©««‘qwLMOZloh>F…cpƒm†’c~‹€b”_ '%8Ìûøøöõ÷÷÷÷÷ïï÷õõõ鼸ª—åúìãÊ÷úüüüüùùúúýýýóóûûûýýý§9><Sa`FZž¾¿˜sƒ¤Ë¨¨¶´±¬¬¶ŽvnLNPYmpi@AƒjhŒl‘’]{‡^j | ||
49 | $%6Ðûûøø÷óö÷÷÷÷ðï÷õõõõéÒßàãííúöìóúüüüûõöúúýýüòõûûûýúø±RJTXOFKEˆÁÁ‘w…‚«Ñ±«º³±®°´„}qIMNYknhG>~p^”k}—žbz‘’\ˆw! | ||
50 | )' Ÿûûûøøóð÷÷÷÷÷óîõõõõõåÖñòýýýýõéôúüüüûöøúúýýüòôûûúûúúÈxŒf\WVIH”ÅÇŒz…|²Ï´´¿º²«²³{…kMOOZoqhE9~`pwšžbv’•a€|* | ||
51 | ()aÚäêûøøóðö÷÷÷÷óïõõõõõïÙîøýýýýøõ÷úüüüû÷÷úúýýõçóûúøýøú÷Ã`‰µ“›´£ÂÊ|„³É°¸Ã½µ°´¬q‚bNNP^ppiC0{a„yu¡jq“™l…4 % | ||
52 | "*(ÙÚØñùùöõöööö÷ôòöööööêÜîõúúúúùö÷ùûûûûøøûûüüùëñøóñýêòûÕgaG]uVÁ¸‚‚‡‹ÀźÇù¶º¦q‚SMNQ^qohI&jYfˆu•œqd“—szˆ6 | ||
53 | ! !)+ >ÌïáÒßùù÷òôööö÷ôóöööööïçïôúúúúøõ÷ùûûûû÷÷ûûüüûêñøîïýòîöÑS‡Å_˜¨s¾Â²{†ŠÁ¿£¼ÈÁ¸µ´–p~JMMO_wwpcVapXWkWr{OKƒ‘fpk' $!#..!^ÄóáÈÙùùùóóööö÷õñõööööòëðõúúúúøö÷ùûûûûøøûûüüûðôøíñýðèñáef}rWh„ÅÂ¥x†„’¸¤ÅÅ¿¸·²‘x{DKOQbrrtwy|€„ˆ‹’“–——™œ¡¤¥¨ª«ª«ª©¨§¨¨¥£¡£¡žš”¥»ìéÂÑøù÷ìîööö÷ôðôööööôïïî÷úúúôñ÷ùûûûû÷÷ûûüü÷ëóøööýûáßû„GNBF7s»±‰gkwšÅ¨Â¼·´¥†s?MPRYF997:<><>>><<;;:<<?@BCGKQTV[_bhmuz…‰¥ÊÅáÚ¸Ëøøõëñ÷÷÷÷òíöööööõëè×ðùùùôð÷øúúúùóöûûûûùîóøðòýýÖÎýîÔÖÖÕ×ÕÕÕÙÛÛÙÙÙÙÖÕÖÖÕØÚÛÚÜÚLNR_jkhilmmoqtttuuvussrpoommlkjigedbba^\ZXXUCҿȸÏÂóøöïñ÷÷÷÷óåðööööôéåÕóùùùôðöøúúúúøøûûûûúëò÷îóýüÚØýë—€€ƒƒ„††‡ˆ‰‹Œ‘’‘’““”•——LLQbrsuvy{}‚†‰ŠŽ‘’“”••–˜˜™šž žžž ¡¤¤¢¥§¥§§˜Ì×ÁÇ´íÃîøòçóöö÷÷öäîööööõéêâùùùùòòøøúúúúùùûûûûûîòøéðýüßåýüµz{ywvvtstuqpqponmlkkjljigHMMH2 "%*,17;>AEINS[aflqtx}v¹Í½ÉÄÖÅìøõéï÷÷÷÷ôäïööööõéãìøøøùíî÷øúúúúùúûûûûûù÷ôÁãú÷ÙåýýëÌÈÈÈÇÇÆÇÇÆÆÅÄÃÁÁÀ¾½¾»º¸¸¶ILLMB$ LÒâÎØÝÔ²æøøíî÷÷÷õòæî÷÷÷÷öïàòøøøøëéøøúúú÷÷ûûû÷÷÷÷ûôºåðåØñýý÷žƒƒˆ‹”˜žŸ£¦ª®°³´·¹»½¿Á¿IKKJD- TÐæÕÐä×¶Ù÷÷êé÷÷÷õóæë÷÷÷÷öðäîøøøøéå÷øúúúôóùûû÷÷÷÷úùÜçßÖÑ÷ýýý“#&$# ! ! !!JKLIF7 PÐóèÎßÓ»Ööøîî÷÷÷õõêç÷÷÷÷öïàîøøøøòîøøúúúòíùûû÷÷÷òùòÎÕÕÔàýýýýÎC&$#" !""!"""!"#"%$EJLHC;' ]ÔöñÔÚñÖõøñð÷÷÷õõéç÷÷÷÷öíÝðøøø÷îìøøúúúðç÷ûû÷÷õéïðÙÈÍÖáüýýýõw%&%##"!#"!"#"#""""#$$&'IJIHF?.uÚùôÜØÂ›Çôøòíöøøõõëæ÷÷÷÷øîÌéøøø÷íð÷÷úúúïÝöýýúú÷ìòàľÒÜàöüüüü¨2)&$"#$#!"""#"#&%%&)(()HJKGDA7$ &‹ØùöâÖÆ¯ÅñøóæôøøõõçÞõ÷÷÷÷êÞöøøøöëñ÷÷úúúïÞóøýúúôÞïß²«Óß×èüüüüâW,(%%%$"#$####$&&()(+.0GHJJHB=,@¯áùøé×À¥¸êøóéôøøõóåÅë÷÷ôðæÜóøøøöéï÷÷úúúèÙ÷ýýúúõàò衤Üèßäüüüüú‡$('''%%&''&%&&'(),*,/2FGFGEB?2 [ÌëùùïÔ¯œ¤Üòñçôøøòïã¿ßóòìßÝòøøøøòåñ÷÷øöö×âýýýúúñßôâÖðêàúüüüúÀ9-))('%'''&&((+---.022GHHGFB?7& oÜóøùíϦœ—ÍëïêöøöóïÛ»ÛïðæÛäöøùùùõåóûûüüûæíùùùûûèÑéÊ™©ÔöóÖêûûûüîn+++(()'&%&'*+--.04333FFFGFB?9*~Ýó÷ùó×§¤œÂÙáØòøõðëÖ·ÑëçÞßðö÷ùùùôäöûûüûéÈçùùùûùÔ×õ§°ËùñØêûûûüüŸ.-+*((')()+,-./013444FGJIGDA;0 "—âùùùõݵ±£ÁÖÑÎóøöïêâÃËæÞÕäñóôùùùïÞ÷ûûüûêÐëùùùûùäîð³·½ÚùùÜãûûûüüÛS5/++,+++-/./10362251DGGGEB@<4& B»ñ÷óôõÒ°³¢¹ËÊ´àøôâÔÖÁÆáââðêðòøùùïÛ÷ûûüüïÜóùùùùóØîâ¡¶ÛõùùíÛóûûüüü…)-.,,+*,.00122244444FFEEDA@=8- _Öôôíëôླྀ£´À±ÇÞÖÍÏÒźÔÜêóáìòùùùïÖòøøüüìëûûûúøèØõÑ›´à÷ùúõÞêúúûûû¹60,,-.../0334555553/BBDDCB?;6." yÐéèæÞÝìܵ›°À̹ÀÚÞÔÊÒǹÑàïðâîñòñ÷íÙõøøüüíéúûúúúçÛøÄ‘µæöùùöàáøúûûûîn-.-/../112333335421EDDEB@?<80%6–á÷÷÷öèÃÓÑ•¨¸ÆÂÀÚÏÈÈÑIJÌäòíÛëóùùùíÚôøøüøàãûûûú÷ÔÙú¿Œ¶èøúúúèÌéúûûûû¥6.,/121134464543244ADECAA?<93'N—Ñö÷÷÷øëÖⲫ½ÈĹÒÓÍÐØÌ¬ÉëôçØîò÷õöÜÐôøøüôÓâúúúùïÓãô¸‰©ÑôùúúïÙìúûùùûßZ20012/233121132122@?B@A?A=:4+!d‡†ÏÍ·¼¼œ¥½®¬ÂÌȵÈÒО¿¨ÁæòãÕïñö÷÷ãÃñøøüöÑåùûöôïÜìö°˜¹ñøðíñæàýýýýû™/44314442034532441BABBCB@><82& ‚ÆÞô÷õÑ™”˜†˜³ÂĬ´³›‘Ÿ««¾ëòáÏßðòò÷îËîøøüøÔàø÷ðøîÝíö¤}Ÿ°äêæçîõòûýýýýúÐQ84443444222333121>@CDDCB?<93(DÀúÝ÷÷÷ùúÝŠb‚¯¬™‹“§n‚ž®ÃìòáÓêð÷÷öíËáøøüüÝäüüûûöàìç€t¡¸¼áúúýúìöýýýýû÷…88432421255422122=>@AABA@<94+"nݱ³Ùô÷캙”™vkwwy„œ¯¢yWp’¥ÐòÜÍéò÷÷õçÈØôøüøæîñìêéæÍæævp›±òúúúýý÷öüýð½‹U973222122233220252@AABCB@><97.#(®rH 9ltvpmb\ZZr™³¢T !&¡ËÊñòìô÷ïÎÓïöýîÞíåàëìçÊëÞnkœ¥ÖúúúûÝs2.BS@78423322312311221/11>@ABABA=<;72'(0&;_ggea\SQU]i™¯žSˆÆÌÝæôõõíÎÏëöýùè÷üûúûöâñÖfl–¡ÆÅV7@CDK<3443223322334443233110=>CCBBAB@<94)! ""!!"""##%"#&'!(AW^_``\XY^XWb‚Ÿ“P%¤µÈñøöøøðÐÍáôýýýýüüûùôæñÒxmŒ—€Z8:>FNI:899:7878557677766777554>?@BABB@?<95. !!"""##$%%##$%%&((''(-5@LSWYXVTRKDI^`hz`5*, /“©´øüøøøóÑÑßðüýüüüüüøíÛîÎuaƒ‹X=;=DOYI889:9::999879::999:98886 \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/pan.1.pgm b/SD-VBS/common/toolbox/toolbox_basic/affine/pan.1.pgm new file mode 100755 index 0000000..9d57f70 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/pan.1.pgm | |||
@@ -0,0 +1,59 @@ | |||
1 | P5 | ||
2 | # CREATOR: XV Version 3.10a Rev: 12/29/94 | ||
3 | 128 96 | ||
4 | 255 | ||
5 | prsrxxwxz{{||||}~~‚‚‚„ƒ„…ˆ‡‰‰‰ŠŠŠ‹‘’’’””–™ššœžœœžž¡ «§££¤¦¦§¨¨§¨««¬¬«¯°±³µ·µ³´´µ´·¸¹¼¿¾¿ÂÅÄÄÄÄÂÂÃÃÄÉÈÇÆÆÈÊÈÈÌØÅ¿Ã¿À¿¾¸prsttuyxyyz{|{{|~ƒƒƒ„†…††‡‡‰‰Š‰ŒŽŽ‘‘’“’“””“—ššš›žŸŸ¡¢¢Ÿ«®£¤¤§§¨©««¬®¯°°°µ·¹¸·¸¸¹¸¶º»½¾¾¿»¼¾¿ÀÀÂÄÇÇÇÊËËÉÊÊÊÌÌÉרÇÅÂÁÀÂÁÀ¼tuuuwwvwyxzz{|}€€ƒ‚„†‡ˆˆŠ‹‹ŒŽŒ’’’”•–•˜”•š›™šŸž¢££¤£¦·ª¨©©ªªª¬®°²²±³¶¹¹¹¾½½¿¼¼¼ÀÃÂÂÂÁÃÃÆÄÄÅÆÇÊÉÆÇÊÈËÍÌËÊÊÆÚÊÀÀ¿¿¾ÁÀ¿¼tuwwxxzyy{{{{||}~ƒ†…„†‡ˆ‰ŠŠŒŽŽ‘““••”•–—˜™šœœžŸŸŸ ¢£££¥¡°¹¬ª«ª¬®®¯±³µµ···º¼¾¾¿½¼½¾¿¿¿ÃÁÁÀ¿À¿ÁÄÇËÍÓÖÖ×ÖÕÔÒÑÑÑÏÞäÕÖÖÙÛØÕÓÒËuvuwyyz{~{z~~~€€‚ƒ……†ˆ‰‰ˆ‡ŠŠ‹Œ“••—˜˜š›œ›œœœŸŸŸŸ ¡¡¡Ÿ››¡³œ—˜œ¢£¦ª®¯¬¬¬ª¨©«ª©§¦¥¥ÌÕÄÃÁÂÅÉÅÄÆÃÅÇÊÊÍÍÌÎÊÍÎÏÑÑÓâ×ÕÔÓÕ×ÓÔÖÕÏ_`k€{x{{zzywzyz{{{}~~€‚„……†††‡‡‰‰‰ŠŠ‹“•™šœžŸ¡¢££¤¥¦§©¨©ª¦¶µ£§©®³µ®£œœžžŸŸŸ¡£¦©¨«¬ªÎÚÁÂÁÂÅÅÇÈÈÇËÍÏÏÐÍÍÑÒÓÕרÙâèÞÜÛÜÜÛÚÚרÕ8:E[‡{|€‚‚}…„ƒ„…‡…‡‰‰‹ŒŒŽŽ’’’””˜˜˜˜š›Ÿ £Ÿ¢¤¨§¨ªªª«¬¾¬®±µµ¯¢™™šš›››œ›Ÿ¤¨¨©¬§ÆâÊÇÌÎÌÊÍÐÒÒÒÕÕÕÕÔÕÛÝáááááïæâäãäåãàßááÛ57@_WY…„€€‚‚€ƒƒ……‡ˆˆˆŠŒŒŒ‘‘’“”•––™šžž ¡¡¢£¥§¨¨ª««¯¯²±Ä¿¨´·´¢š¢¡ ¡ŸžŸ £ª¬°©ÅéÑÏÑÓÓ×ØØØÛÞÞàäããåèèèëéèìóéèééëìêìííîê:9D`ny_i”ƒ€‚ƒ†‚‡‰‡‡‡Š‹Š‰‹ŒŽŽŽ‘’“•–™™™›œž ¡¤¤£¥§¨©©®¯°¯®±³´³³´µ¾Î´¯·¹³®¨ ›žŸžŸ¡¦¬¯°¯´¸°ÈëÖØÙÙßßàèèëìëëïííññðððòîïôòôóôóõöö÷öóí8:B^kryxc}„……††ˆ„†ŠŠ‹ŒŒŽ‘‘’••˜˜˜™š›Ÿ¡£¦§©«¬¬°±³´¶»½½¿¿¿Á¼ÈÑ·¿Â¸°¬§¨¯³³²¯¨ª®±´³´´¯ÇïÜÜßâæéìòôôöøøøøøóìæâÜÒÉ÷õóô÷ø÷÷îëôÝÛÚ8;C[gorv}wb‘…ˆŠŠ‹ˆ†‹Ž‘’“““’”——˜š›žŸ¡£¤¤¦ª«°±²²²´²³·¸·µ¶¹»¹½ÄÊà ned_gxÇÊÙñüýýýüüüüüüüüþþþþýýýýýýýýüüüüûûÕÑÀ®¥ß·øúø¥0çmšt9;D^iloquz~nq’xifdefq€„…ˆŠ‹’“•—˜™›Ÿ¡¢¦§§ª°ª’Ÿ¥—¤½ÂÄÅÆÉËÍÐÓØÙÜç¨Õûûûü÷†1ÅýýýýýüüüüõèûüÚÞíÕûÛÀüýª¦ï‘ͺŒäØx—„^Ž[r‚‡ˆk4\|JSZ87A[gjknqtw{€f”öýýýû䀔–˜”•œŸžž ¢¡£¨¨«°¯°°y962??XªÉÏÑÔÛßÍÇîñúà•ÁvÃøøüÃb0 n® ’WGa7-84Z`HA`Hu_NulKQwIaiTƒxNc‡w]uZm|‡i&[~KQ_8:BZdeffcK5850)%)09::B6BAIAGIEB>]linhbc[e_^aRC71/4=Eera?=Y_=He<30(Wd0$FkenD9„y_]wKQlImuVS|LvvSvrKT{MbqSƒ|R[ŠvSvUb€‰]$\KM`;;CX`cba[A2/41)&2-/=74C<?9G9AE=;5U`BVTDJBDEJB6<A7,4RNc{l?BenWOi?"Bu@#$M[lP'mƒyfWtLNsK`vkv‡St}NmSQ€MZ~Nz}UT‹{OrQcƒ‰b$XRIb:;CU^__\Q:226-%'/03=75C<C:D7@J??8V]DPVBIE@DI@2=A@;5NS^xn;9`lgH‚kH+$lBSi]I((Z~tzpNaQErKGe”Ú¨Wj‡L`wPQ€ZPOpZNŽOqV\ˆŒ’e$QVKl;=EVZ]\RF7559.+)32/:;9C>C8H8?H<?<TYFMY=MFHPLF9FB9?6DHRsuK ]p?lqJ5\UIiNX9'C‚fnuPYZBpWCg‡ÐªUZˆNYyVNjV†Qf‹kK‹ƒNi]YŠŽ”n(I…^Go<=DQYZVLA579<*'+1-4=66E@H=D7;LCE<TZGGU=GJLULL@DA5:4=FLpyZ HqH\‡X:!Fe)$agiG&0yjgz]TlCgfDZk³¥ZK†WM}fQ€tQ€W^tK‚ˆSgeW‹Œ“w.D€aJp>?EUYVQF>7:>A,%(2-6A:;N@KBL:7L?G@UYJ@XEMQP\GTF@@4:58EHh}m/6nXOmG*3q<S{jJ$)&lwqnUvG[n>FU™hJƒcLƒqV|ƒStbW…MzTcoY‹Œ’~3C‚iLq??ESURJA=46<?,)+2,6A<=J?H=H;:J?G=OVMK`NQ]YWIRI8?8875CF`u=)kiGƒqO,"nL*Vn]T)*T~n~xNiLKsDC\ˆŠuErtG|yOm„ShpUއLm’_\{V‡”†::}oLtA@FSSNFFTSFA@+*,.-;=?AOBO:F=6L?IDQ^WN]RP_YTKIJ2@D8<6FFY|zN]vFpƒ^6$^cDSYjlt;0@‚s|T]^GuRBb`FyGh‡Ko}NgƒWV|T„‹Wd—jY‹[€”ŠJ6xsMq@BFQQKL[[QD>A+)*+*7>>>LEL>I?7G<GBL\ZQ_YS\XKHBL;GG3?8@HSw€c$ItP_•hG4RgqgRJ]q{G&%5|rv€[RlHoaAfcBxWZ’OaR[†`V†Pu‹[`—u[ŽZyŽ”J2vzPu=@GNLJN]]QD<>+,,0+<>>ES@KDGC;GBHBF^^X]aP[WEKBRNGC0969GKo„q45teWjJERTOC3,7O_K*#h}{‡oRqJanUi†rwqW—]U„`WkR†Qne[–…Z]sŽ”T1t~PrACEJIFKYWLB>>'#(-*8>>@REM<EF:HBEEC`a\\[MOPMSITPED6847KMhƒ{E+mzO}_53CMKB.#6FKH3+Vƒ€‡{PiJOm69.aƒOjP†oT†xVƒXb•s[”\…]j‘••T)l‚Nl@?DFFDISQG>) | ||
6 | &':=@CSDJ>GH<HFHI@ab\VPJFNMVSVXG<<696AA_…^a~S6(#'3BJ</(*'>UC/ Gˆ~„ƒPZWIr:37Q“SnV}V~Tyf_˜{\”b|ca•”•\&a‚MeACEHGELUF6&6<@BREH;?K<JLII>caYUSNLRX`YWUEDB4;7?EVˆm*O{P'$&)8DIG;.'.S[E'2}„^RiGh:*ED„[^Vt†To‡Tjr\–…V‚•cqj`’“•b"[‡Oa?CHIGELQ?/1) -#+'&7=DTHI==L>GOFO=^\WRPUSYd^PKIHHC062;EP{‹}7 | ||
7 | =S*# (>RG9?9/ @VJ3'j~~ƒmUyGX1!FCujYŽUf†ZaXgƒYŽVz“ffub”•—k'UˆR]ACDEFFKL8'&#@UERnX-9BCNEDC=L=FPFL:X\RRSZYc]]YKPPOI3616CLn‰ƒL".),*-8A@;53/*%$8HH;R„}„~YmLC/#;Ec~YŒa[ˆ`[‰b\ŽY‚Vp˜mgc““—r%RˆWXCCEFHFKG-+OA3;t„n98@@QFEJAS?GRGR:S`W]\_b`W[[P]VNH8845BIf…ˆ`" #&+/40/78/,+%!8NE>#B„„‚^aW;2.9NY‹W~v]q^ˆq^•Ys•^lui‡dŽ–˜{+O†_UACEEFCE>''')',JeZ=!7A<JHFL>Q@EPEP?P^Ycdc]WOXd\cSDG>46.CF[„s4!!,42-.7;8)-<CJKJ26z|~„dXmB5$6CYXŒXn†]‹`…}Z‹]h™igŸ}hg‹”–0K‡eWEFFHHGE7..!%&34$2C>EKEP@THIULTAMbajebUUV`h[XUFIN2;-;FW}‘F%(4HPH=blQ49:1$f}w‡uZrF%.MjX€ee˜^‡^{]hc›ul¥…hf†“•‰?G„lVBDDEHFC1(- | ||
8 | )2%0@DEKHS>SFIUFLCHachZYXW`fbUOYPUO19*2GPu„B'8Od`Ue€l<)51U†zƒ}`qN&QuTpwaœaoŠYm]tr`f¡i’e~”—’EAƒqWEEGIHEG0! | ||
9 | #"6FEHQKTEQDERKMDEae^UR^^d\V[P`^KI3</.GNmŒŠB/LMEMTcu{zv…‘ŽzJ53/Bˆy€ƒ\aY Dw[eŒ]–faVd‹`c‚h˜Š_š‘jŒcy——–J<|tXEFGIIIG3+(%+O`H'(2:>IIFOLU@NIHTKKDCe_ZTMkg\UXcW^^JH6:6+DGbЉ<@es€…††€~‚žŸ“|I0*3.2y}ƒs‡Œ…„|ˆ‚z›a„ue‘eaŠe_ˆh•’`Œ—k„hr–”—]:z}YFEFIIJI323()UkO*(.9AIDBKGOCNJKRCNHA^UT[Jg`WVbdZV[JNA5<0>I\†.Ehpx}yxz{‹™£ž„I'!,$#g~‚z£¦«¬®¬©®¥nl„gŠt[†qcŠh€Yu™fwii””i;oYEGIKLIPI;:GM[c^X[]acgklmmqpoqswuvwt{zy}z€‚†………‡†ˆ‡‰‹Œ’›“BKSG<69G[ZI@EUlwT(I¦±¯¯±²°®®®°±±±¯¯°²´µ¶µµ´³²±±³³±²°°¯¯°¯¯®¬¬ªª§FEFILMYgiikoprvwy}€}‚……†…†„…††…††‡†‡‡ˆ††‡‡‡‰‰‰ˆŠˆ‡‡‡ˆ‰ˆ†…„ƒY-UM2)-88{}B/'8Xsi4(Imnnmnmlhhigeccecb__`a^^\^]\\\]^]]][\^^``bbceebFGGJLLZYOMNOOQRTTSUVYYYZ[[\\^^___^^^`abbcegffffgijiknnnonooqY(2`TGCA=K¦©K9>V_xwHPo…†ˆ‰‰‰‹‹ŽŽŽ“’’““•••———™˜—˜™™˜™š›šš™š›š˜HIJMMN^lqopqrsuwzz}ƒ‚‚„†‰ŒŒŒŽŽ‘’“”””““”–••–˜››œ›œœšn4Ajymeiƒœ¨†`_v“žM@‚¦©©ªªª¬«¬¬®®«¬¯®®®®°°¯¬®®®®ªª©¨©¨©©ªªª©¨§FGHJKKZghgilnoruwwz|}~‚ƒ‚ƒ„………………†‡ˆ‰ˆˆ‰Š‹ŒŽŽŽŽŽŽ~M;aŠ—˜¡¬«šº¨wWj‚ŠŠ‰ˆ‰ˆˆ‡……‚ƒ„ƒ‚€€~}|{yxwvtrpomlkhffebaaa_\[YWTKMMLNMK8" | ||
10 | |||
11 | |||
12 | |||
13 | |||
14 | |||
15 | |||
16 | |||
17 | |||
18 | |||
19 | |||
20 | ,?Qt‰œ§š†˜ž·»´«”taA!!"$&%$&'''&(&'&'(&%%#%$"$$%#!"##"HKKJJKI?+ | ||
21 | |||
22 | |||
23 | |||
24 | ASd|“ ƒ>AGR¡¶®ž‰ve5"++!$" .12,!! "! ! JJLMLLKD5 | ||
25 | |||
26 | |||
27 | |||
28 | 3P\m†•|E?Ww¤®Ÿz¦ƒ*11''1/ =AB) KKNMKKID;%->5-:3:@C.5AC52= (+3 !?Wf{Ž~o{˜©¨™‹²~(00&%-. #$EDN+ 8WX=GCMZTZfdIL]IJLJKKKJA+1D3,@8FKL.9GG64F kj(#$4S`u‚“¿Â£šž”ˆ|œÄx3qq;S¤Š69G6*MQQ( + | ||
29 | NeeRMDRd^]feGG[JJKLONMKE2>S@9LAVXY9H[]CG_) | ||
30 | |||
31 | |||
32 | \[%"$<hSF¨š=j—F:|\bmdJ1005QuŒ…|ŸÀt:ƒ?FˆDz™r6…œu' | ||
33 | <l,=^SUMOq{rXUNdvpmwu^_qKKKKONMJE6JgLEbSmopC]rtORo, | ||
34 | &kb'-4%@gTI¦›:k˜CNÄ©caa[exp„„„´Ås:vq6RŒt8[|Z0ŸË‰gÝò›ªÜé”3A2:QPQU_ˆ•€hd]™‡‘Žy{ƒIKLLNMLJH:QoOOm`s}xFg|NWt- | ||
35 | (l`*QfDK§˜;o˜ES¼¹|[_d_Xccfx€x‡ÁËt;vl2T–x-4ªÑ‚_¾Ö¦·¿×„,D5JOSYQf–ˆrqj˜¤‹‹”‰ˆ”LLJKOLLJH=TsKToc{vFm{€N[r( | ||
36 | +rc.g€NP—<vŸG[ÆÉ™YWUPB9C]pwxv’ÅÆp<yq3Y—w-:ÕY¡¸©É¿å~;6(Nt_<Rz’–€nurŸŠ‹“ŠƒŠ—LLLMNOOKI=XuITk_x€pEo{€Ods% | ||
37 | |||
38 | -tb%&)!T“;{ŸB^ÆÈŸZ[eqr~‰‘…xxo¥Åp=u3X˜u,AµÎye«³ªÄ¾ÞuK=.QQDOKv™“xrv{ŸžŽ•‰†žLLNPRQPO[V\zJWi`{‚qGu|„Rlq" | ||
39 | 3{`$"%Y±“>˜>cÊÃUYbksw‚ŒŠwx_’Þp:{s3O–v-I¿Óvj¥°¾¼Î€z7GuhLc[–˜{ts£Ÿ”¡¤ŽŠ’žJLNNOOUfkW`zL\ia|ƒoHv~Soo #& 5bR$AHM˜¡pMNUXSSZhqlkx^›ër0QK-.<:#2NUQmª´µÏÖÀeO:FMGCHR¥”ssq†¥ŽŸž’Œ–¡PORRRSdnmYb|Naj_}†rJx„Tsj | ||
40 | $% ˆyFLIQY]dhbgnvlÚú±=77*,43$8UZ\lŽ”½¼Ï¸IY\DNHBF\”¥oop’³š‘ š–“— MOOOORdnmV`|Sgk^~‡uKw‚Tuh #%+€¸¦UKKHEJS^dda[Ëûûýñ°^/1:8':VX`|˜¤ÆÃÖªKPICMD?Abš¨Žnrq«—•ª¡“–˜¢NNMQSUgpoU^SgiZ‰{Mx‚„Sye | ||
41 | |||
42 | |||
43 | |||
44 | %%!¤çٺɒBJNMKQ[\WL·ûûûýýýýû«G9$ ! >W[bx˜¡ËÅÛ³Z5G_LZN@q¢«Šnvs˜°”™¬ž˜œ¥¤MNOQQTgpnP^Zni\~†}QuU|c | ||
45 | |||
46 | |||
47 | $& _×ÒáäÆÂÛyFKLKLNKOùúûûýýýýûûù¹K'%%EX[kœª×èîˆ<HR`RTODx¨®‰pwt™¬“œ¨™•ž¤©OONOQWiqoJRbul[}‡„Vzƒ„Z€a | ||
48 | &'‡øõäîîÞ¹ÙÎpHLKFB[`©õúúüüüüýýýýü½<)HY[o™±g?W_BCUgGMZJŒ±¬‡wvz¥®•¡¦œ™¡¨¥NOOPQViqmEQ‚_nk\‰„Xz†„[†a | ||
49 | &' -”ðúúöêóóê™]svM>Azqv×ôúúüüùùýýýýüüüÊ…^^vœ¢jT[[;;*UzKYn^›´ª}x{©§’¥©£ž£¦ŸLMNOQViqkDN„bqt_…‹^z‰ƒ_‹_ %&AÒïúúúúõèóõóà¢f]kAjŸ†n¯êúúúüüûûýýýýüüüüùø§‚Ç¡nOOP7</H‰yKyt¢¹¦z}ƒ‰¬”¨¯¨¡ ¤œLNNPQYkplFHƒbk~e‚Ž‘\v‹„`Œ` | ||
50 | %'SÔý÷ùýúúú÷ìóõõó½—ˆhzg«òúÓÖúúúúüüüüýýýýüüüüùùùõÖÀìë»D>.Do[6^t³ºr½«š¯°ª¦§£“PMMPRVjokIBilŽn~Ž•]xŠad | ||
51 | %'!?Ñøø÷ô÷÷öööôñôôôõ渲Ÿ‹‘ÝüííÛîûûûýýýýüüüüúùùúýýýýüÄÇØá¨7<3La^CV”·¼—l€šÇ¨¥´´±««µ”MNOQRYlpmK>€rc‘lx–^vŽ[ˆo | ||
52 | ')`êøøø÷õö÷öööóïóôôõõëÔÖÝäðë÷üúúûûûýýýýüüüüúøøúýýýýüßÉÒì¨QEMTQFHB¿Á“s‚~¢Ñ³¨¹´²®µ‰NMPOOYmpkN8|~`‘kv–at•^„z' | ||
53 | '&:Éøøøø÷òó÷öööõñóôôõõîåõõøúüüüüûûûûýýýýüüüüúúúúýýýýüùñùûYƒ[[UTG@‡ÅÊŽw…zªÏ´®¼¶³ª¯µƒLMNPPYnrlO6zŠ]‡tp›Ÿcn’—`z€- | ||
54 | ))–ìïõøø÷ôõ÷öööõñóôôõõñæóùùúüüüüûûûûýýýýüüüüù÷÷úýýýüûü˵٠a€²˜²ˆ—ÀÇzƒ|DZ´Â¼µ±³²uNPQPQ]qtlN+o‘`{}t›žmg•šmw‹7& | ||
55 | ! !/MÈàáè÷÷ø÷÷øùùùøòôööõõóìðöööûûûûûûûûûûûûùùùùù÷÷ùúùúøùû¸OHnµlZFZ}Q¤Â¾„€†·Æª³Ã¿·´¸ªmNNMNQ]qtlP$a^]Žrvb•™xv9 !!xúæÙâ÷÷øõöøùùùøõõööõõôóôöööûûûûûûûûûûûûùùùùùøøùõóúø÷úàkD\‹P|Ã]’p¸À¸~„‰‰»Ã¡µÇÁµ±²›lLNPRR_wytgYbs]XpYk~UF{ees,$!!6æèÕØ÷÷ø÷÷øùùùùóôööõõõóõöööûûûûûûûûûûûûùùùùùøùùôõúúùøùû¶™–RazrVi‡ÇƬxˆ‡’ÅÀ¸³‘nKLMPS`rssvy|ƒ†‹‘“•™šœŸ£¥§©©«¬««ª¨§¦¥¤£ ›°¼ÖðÛÏó÷øõõøùùùùôôööõõõôôôõöûûúúûûûûûûûûùùùùùøøùúúúúõëøð†LZ?HKDG:r¾±kny•Ƴ§Â½¶´¥‹wKLNPRZH9:99<=<>=@=::<;;==?@BGLORUY_ciosx~ƒ„€±ÄÏãоï÷øôóøöööõóöööõõõñóéó÷øøø÷ûûûûûûûûøøøøûûûû÷÷ùùôÓùùÜÑÑÒÔÔÕÖÖÕÔÔ×ÙÞÜÛÙ×ÖÖÙØ×ÙÚÜÙLKMOP^lnlllmnopsuuuwwvutsqpopnmmjhgeeb`__]\_»ÀÐÇÛÂç÷øõõøöööõðòööõõôïëÞô÷øøø÷ûûûûûûûûøøøøûùúûîñøù÷Ùúü¶x{}}~€ƒ…‡ŠŠ‹Œ“”••–—˜–JLMKN\nrsru{}€…‡‹ŒŽ’“•”•—˜—˜™›Ÿž Ÿ¡££££¢£¯ßÆÚÂÚÐß÷øôôøõõöôîóööõõõðìä÷÷øøøøûûûûûûûûøøøøûûûûðòùùøâüüÕ…{zvvtrrqpprqpoommmmkkihgheLKJNOK: | ||
56 | |||
57 | $',048:>CHNTX^chd—æÉÍÇÂÇáöøôôøöööôíñööõõõîëñ÷÷ø÷õöûûûûûûûûøøøøûûûûÑßøõêàüüùØÉÇÅÅÄÄÄÄÅÃÄÄÄÄÂÁÁ¿¿¿½»º¸·´LMLLMNH* }èÖËÚՏɸùõôùõõõóíñ÷÷öööðêöùùööòôûûûûúúúúùùùùûûûûÅÖöñÝäùùý¶o{…ˆ•™ ¢¥©®´¸·¸¹¼ÀÃÆÂLJJLJJG4 | ||
58 | †ëæÇâá½É÷ùõñøõõõôïð÷÷öööïé÷ùùööíðûûûûúúúúùùùùûûûûáêëáÍäùùýÐE(%!KKLLIIH>%ˆíïÓÚÙ¼Îõù÷ñ÷õõõõðñ÷÷öööïç÷ùùööóôûûûûúùùúùùùùûûûúãâåâÞôùùýõ{&#!! !!!!" !#LLKLKIE?- êóàÒÏÉóùøõøõõõôðð÷÷öööðçõùùööòóûûûûúøøúùùùùûö÷÷æÙÍÚÜôùùýý¯2&$#""!!! !!""!!"!#""$MKLMJFGB5 4¬ðöèÓÖ¥»ðøöôøõõõõñï÷÷÷÷÷ðâòøøööòôûûûûûùôúûûûûüüüæÏÊËÞàôüüûûì`+$#$$!$$""#! ######%'&KIJJIGEB:( N¾ñöìØÎ¶¿ëø÷òõõõõõïêö÷÷÷õëæ÷ø÷öôñôûûûûûöðùúûûûùíöâÉ»Îåéïûüûûû#)'%$"###""#####!#%%&%&)KHHJIIFC?/ lÒò÷ñßɪáööô÷õõõòãÈì÷÷õðéê÷øøöôòöûûûûûìåúûûûûùêùá°¡ÚíöðûüûûûÃ<+($$$'&&$#$$%$#$%&&&),+KIHJJHGD?4# …Þô÷ôäÁ¦ ÒñõòöõóïëÛÃâóòíááóøøøöòîöûú÷øõäòûûûûûîáùâ«—Îìøìöüûûûós(*(&'&&&&&&&&&'('&)*,..IHHIJFECA9(-¢æö÷õฤžÅéòðõôïëèÚÄÙñòçßç÷øøøõðíóýýýýùôúûùùù÷ßÓëÚ¬§Éïûåèüúúúú¨7-,)(''&'''')(&'*-./123IIIHEFECA<0 =¹ìõ÷÷뾬¢¸×èæñóïëçÖ½Óëèßãòøøøøõíêôýýýüïãöûùùù÷ØØóЫºÈóüïðûúúúúã\0-*)(''''('')*+,--0123HHGHHIGEC>4%UÙùöö÷îó¥ºÖÜàôõòìçÜÉÏåÞ×çòøøøøõìèõýýýýéÜöûùùùõâèôÁ¦ÀÓúüòçùúúúúû(,*+)))*+***+,-/223365GEHIFFDDB?7)kàúóñõí¶¡®ËÓÆæôòåÒÑÇÌáâäñòö÷øøôçäõýýýýöñúûùùøðÚïì°¯ÏëüüúïõúúúúûÈD0,**((*+++++./0344223GGHGGFIFCA</"ìøðëïïÑ»¤®ÂɾÍßÍ¿ÅËÈÈÙßêïîùøúúöæâöüüüü÷÷ûûúúøèßöÛ¡®Õõýûûòðûûûûüòz-,-.-)+,,-./131343343EDDFEEEDC@<2&3¬åìêæÜèëÆŸ©ÀÎÃÃÕ×ÍÅÐÊÃÕãðñïùõòóõåâöüüüüõöûûúúøãàøÐ™¬×öýûûòíúûûûüü6.,---,,..0/023332222CCDFGHGEA><6-!FÁúúööóÚËØ©£»ÌÐÉÐÇÃÇÔÌÀÓçòñí÷ùúúöãßöüüüûïõûûúúöÜçú‘¬Ùöýûû÷éóûûûüüîk0/-/.,-./01212342243DDDEGGECB@<7."`¯÷úöööóáÞĪ»ÆÉ¼ÊÎÈÍÚλÓëõïí÷÷ø÷óÕßöüüüùèóûúùøîÛêò¹‹¦Ìðüûûûìîûûúûüü 1.-.--.-0/0223310110CBDDECDB??<80$,{w³Ù¼µ¿® ¯°§ºÅʶ¶¿Ç¼²º±ÆçóëèñóùùôÕÍôüüü÷âòûûòóòèõ÷¶‚”àûúúúïÙòûûüüüÜZ111/./0110111310110BABDEEFC@?=;4) 7§Ïðúûí²‘–ެºÆº°¥”Š™§³ÉìôæÚêöôõõâÑòüüüøãíõôðõõñ÷÷ªy˜¤ÐííçÜÛìûûûüüüú’0200010011331110121D?ACEDEECA>;7,#lͺÎ÷ûûûûô®qy¢ ‘‰“§›qw’¢¼ìóßÜíôùùóâÌíüüüüòöûöóúúãîð–n›´È½ÝøúúûûûûüüüüÌM5331121/1220100220?=@BDDDDB@=97.%!¢Ì¯ÐñûŒ™ˆjrut˜¯¥~UhŠ¢ÆìÕÕëõùøðÝÇæüüüùëîìêíìäÚðìˆj’¤Ôýúúúúûûûûü÷ÍŸ`9431121212111100/00@=AAEHGDC@>:62&R¯…A*\qsrmaYWUk•±¤T#’ÊÚòóñøõçÉß÷úúñéñíï÷öíÚòæ€j Àþøøøøû¿j;=UG7322243122112100110-.?=>@CDEDCA><83* ,0$,Rfgeb_URU]h¨™S | ||
59 | ŽÈ×ÜïõöóåËÚóùúöôûüøøüöîøæ|b‘«›Çð¹X5?BIL;2222201232332111221011/?<=>BDDA@BA>;4+! !" !""!$$%$'&(6Q\\_b]ZXZXR^yœ Y(œ¬ÁíõùùõçÍÓîúúúüüüûúú÷ñøÜŠo‡ Œ|Z:;AJOE7355356866544565565553311>=??BBCDCA@>950! "!!#"!""$$%#$$%%&*29GQUWYUTTLDEVWZeX9180>Ž£°òøùùöìÒÕæöúúüüüüùöñìõךry•M==AJTS<6799788768987677886676765 \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/readpgm.m b/SD-VBS/common/toolbox/toolbox_basic/affine/readpgm.m new file mode 100755 index 0000000..a5fd7f2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/readpgm.m | |||
@@ -0,0 +1,26 @@ | |||
1 | function img = pgmread(filename) | ||
2 | % function img = pgmread(filename) | ||
3 | % this is my version of pgmread for the pgm file created by XV. | ||
4 | % | ||
5 | % this program also corrects for the shifts in the image from pm file. | ||
6 | |||
7 | |||
8 | fid = fopen(filename,'r'); | ||
9 | fscanf(fid, 'P5\n'); | ||
10 | cmt = '#'; | ||
11 | while findstr(cmt, '#'), | ||
12 | cmt = fgets(fid); | ||
13 | if length(findstr(cmt, '#')) ~= 1, | ||
14 | YX = sscanf(cmt, '%d %d'); | ||
15 | y = YX(1); x = YX(2); | ||
16 | end | ||
17 | end | ||
18 | |||
19 | fgets(fid); | ||
20 | |||
21 | %img = fscanf(fid,'%d',size); | ||
22 | %img = img'; | ||
23 | |||
24 | img = fread(fid,[y,x],'uint8'); | ||
25 | img = img'; | ||
26 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/simulation.m b/SD-VBS/common/toolbox/toolbox_basic/affine/simulation.m new file mode 100755 index 0000000..2186a6d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/simulation.m | |||
@@ -0,0 +1,42 @@ | |||
1 | clear | ||
2 | |||
3 | figure(1);colormap(gray); | ||
4 | |||
5 | %------------ Parameters -------------------------- | ||
6 | window_size_h = 40; | ||
7 | window_size = 2*window_size_h+1; | ||
8 | noise_level = 40/256; | ||
9 | |||
10 | % define A and D | ||
11 | x_ext = -0.423; | ||
12 | ext = 1.232; | ||
13 | A = [ext+x_ext, 0.2534; 0.3423,ext]; | ||
14 | |||
15 | D = [3,1]; | ||
16 | |||
17 | %------------- compute image I and J --------------- | ||
18 | disp('generating I') | ||
19 | I_init = gen_feature_s(window_size); | ||
20 | [size_y,size_x] = size(I_init); | ||
21 | |||
22 | %define image center | ||
23 | [center_x,center_y] = find_center(size_x,size_y); | ||
24 | |||
25 | % adding noise to image I | ||
26 | I = I_init+noise_level*rand(size_y,size_x); | ||
27 | % make sure all intensities are positive | ||
28 | I = I.*(I>0); | ||
29 | |||
30 | disp('computing J') | ||
31 | J_init = compute_J(A,D,I_init,[center_x,center_y],[window_size_h,window_size_h]); | ||
32 | J = J_init+noise_level*rand(size_y,size_x); | ||
33 | J = J.*(J>0); | ||
34 | |||
35 | |||
36 | %------------- compute A and residue ---------------- | ||
37 | c = [center_x,center_y]; | ||
38 | num_iter = 8; w = 9;win_h = [window_size_h,window_size_h]; | ||
39 | |||
40 | fig_disp = 1; | ||
41 | [Ac,Dc,mask] = compute_AD_disp(I,J,c,c,win_h,num_iter,w,fig_disp); | ||
42 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/sports1_11_28.jpeg b/SD-VBS/common/toolbox/toolbox_basic/affine/sports1_11_28.jpeg new file mode 100755 index 0000000..39ebed5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/sports1_11_28.jpeg | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affine/test_affine.m b/SD-VBS/common/toolbox/toolbox_basic/affine/test_affine.m new file mode 100755 index 0000000..41b48b9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affine/test_affine.m | |||
@@ -0,0 +1,33 @@ | |||
1 | %%% This is a test program for Affine tracker %%%% | ||
2 | |||
3 | disp(sprintf('This is a test program of Affine tracker')); | ||
4 | |||
5 | %% read in images | ||
6 | |||
7 | disp(sprintf('read in images')); | ||
8 | I = readpgm('pan.0.pgm'); | ||
9 | J = readpgm('pan.1.pgm'); | ||
10 | |||
11 | figure(1); im(I); colormap(gray); | ||
12 | figure(2); im(J); colormap(gray); | ||
13 | |||
14 | |||
15 | figure(1);disp(sprintf('click on the center of a image window')); | ||
16 | c = round(ginput(1)); | ||
17 | |||
18 | %% compute the displacement of that image window | ||
19 | disp(sprintf('computing...')); | ||
20 | |||
21 | win_hsize_temp = [8,8]; | ||
22 | w = 3; | ||
23 | num_iter = 6; | ||
24 | |||
25 | disp_flag = 1; | ||
26 | |||
27 | win_h = win_hsize_temp + [w,w]; | ||
28 | if disp_flag == 1, | ||
29 | figure_id = 3; | ||
30 | [A,D,mask] = compute_AD_disp(I,J,c,c,win_h,num_iter,w,figure_id); | ||
31 | else | ||
32 | [A,D,mask] = compute_AD(I,J,c,c,win_h,num_iter,w); | ||
33 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affinityic.c b/SD-VBS/common/toolbox/toolbox_basic/affinityic.c new file mode 100755 index 0000000..e48762a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affinityic.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /*================================================================ | ||
2 | * function w = affinityic(emag,ephase,pi,pj,sigma) | ||
3 | * Input: | ||
4 | * emag = edge strength at each pixel | ||
5 | * ephase = edge phase at each pixel | ||
6 | * [pi,pj] = index pair representation for MALTAB sparse matrices | ||
7 | * sigma = sigma for IC energy | ||
8 | * Output: | ||
9 | * w = affinity with IC at [pi,pj] | ||
10 | * | ||
11 | |||
12 | % test sequence | ||
13 | f = synimg(10); | ||
14 | [i,j] = cimgnbmap(size(f),2); | ||
15 | [ex,ey,egx,egy] = quadedgep(f); | ||
16 | a = affinityic(ex,ey,egx,egy,i,j) | ||
17 | show_dist_w(f,a); | ||
18 | |||
19 | * Jianbo Shi, Stella X. Yu, Nov 19, 2001. | ||
20 | *=================================================================*/ | ||
21 | |||
22 | # include "mex.h" | ||
23 | # include "math.h" | ||
24 | |||
25 | void mexFunction( | ||
26 | int nargout, | ||
27 | mxArray *out[], | ||
28 | int nargin, | ||
29 | const mxArray *in[] | ||
30 | ) | ||
31 | { | ||
32 | /* declare variables */ | ||
33 | int nr, nc, np, total; | ||
34 | int i, j, k, ix, iy, jx, jy, ii, jj, iip1, jjp1, iip2, jjp2, step; | ||
35 | double sigma, di, dj, a, z, maxori, phase1, phase2, slope; | ||
36 | int *ir, *jc; | ||
37 | unsigned long *pi, *pj; | ||
38 | double *emag, *ephase, *w; | ||
39 | |||
40 | /* check argument */ | ||
41 | if (nargin<4) { | ||
42 | mexErrMsgTxt("Four input arguments required"); | ||
43 | } | ||
44 | if (nargout>1) { | ||
45 | mexErrMsgTxt("Too many output arguments"); | ||
46 | } | ||
47 | |||
48 | /* get edgel information */ | ||
49 | nr = mxGetM(in[0]); | ||
50 | nc = mxGetN(in[0]); | ||
51 | if ( nr*nc ==0 || nr != mxGetM(in[1]) || nc != mxGetN(in[1]) ) { | ||
52 | mexErrMsgTxt("Edge magnitude and phase shall be of the same image size"); | ||
53 | } | ||
54 | emag = mxGetPr(in[0]); | ||
55 | ephase = mxGetPr(in[1]); | ||
56 | np = nr * nc; | ||
57 | |||
58 | /* get new index pair */ | ||
59 | if (!mxIsUint32(in[2]) | !mxIsUint32(in[3])) { | ||
60 | mexErrMsgTxt("Index pair shall be of type UINT32"); | ||
61 | } | ||
62 | if (mxGetM(in[3]) * mxGetN(in[3]) != np + 1) { | ||
63 | mexErrMsgTxt("Wrong index representation"); | ||
64 | } | ||
65 | pi = mxGetData(in[2]); | ||
66 | pj = mxGetData(in[3]); | ||
67 | |||
68 | /* create output */ | ||
69 | out[0] = mxCreateSparse(np,np,pj[np],mxREAL); | ||
70 | if (out[0]==NULL) { | ||
71 | mexErrMsgTxt("Not enough memory for the output matrix"); | ||
72 | } | ||
73 | w = mxGetPr(out[0]); | ||
74 | ir = mxGetIr(out[0]); | ||
75 | jc = mxGetJc(out[0]); | ||
76 | |||
77 | /* find my sigma */ | ||
78 | if (nargin<5) { | ||
79 | sigma = 0; | ||
80 | for (k=0; k<np; k++) { | ||
81 | if (emag[k]>sigma) { sigma = emag[k]; } | ||
82 | } | ||
83 | sigma = sigma / 10; | ||
84 | printf("sigma = %6.5f",sigma); | ||
85 | } else { | ||
86 | sigma = mxGetScalar(in[4]); | ||
87 | } | ||
88 | a = 1.0/ (sigma); | ||
89 | |||
90 | /* computation */ | ||
91 | total = 0; | ||
92 | for (j=0; j<np; j++) { | ||
93 | |||
94 | jc[j] = total; | ||
95 | jx = j / nr; /* col */ | ||
96 | jy = j % nr; /* row */ | ||
97 | |||
98 | for (k=pj[j]; k<pj[j+1]; k++) { | ||
99 | |||
100 | i = pi[k]; | ||
101 | |||
102 | if (i==j) { | ||
103 | maxori = 1; | ||
104 | |||
105 | } else { | ||
106 | |||
107 | ix = i / nr; | ||
108 | iy = i % nr; | ||
109 | |||
110 | /* scan */ | ||
111 | di = (double) (iy - jy); | ||
112 | dj = (double) (ix - jx); | ||
113 | |||
114 | maxori = 0.; | ||
115 | phase1 = ephase[j]; | ||
116 | |||
117 | |||
118 | /* sample in i direction */ | ||
119 | if (abs(di) >= abs(dj)) { | ||
120 | slope = dj / di; | ||
121 | step = (iy>=jy) ? 1 : -1; | ||
122 | |||
123 | iip1 = jy; | ||
124 | jjp1 = jx; | ||
125 | |||
126 | |||
127 | for (ii=0;ii<abs(di);ii++){ | ||
128 | iip2 = iip1 + step; | ||
129 | jjp2 = (int)(0.5 + slope*(iip2-jy) + jx); | ||
130 | |||
131 | phase2 = ephase[iip2+jjp2*nr]; | ||
132 | |||
133 | if (phase1 != phase2) { | ||
134 | z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]); | ||
135 | if (z > maxori){ | ||
136 | maxori = z; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | iip1 = iip2; | ||
141 | jjp1 = jjp2; | ||
142 | phase1 = phase2; | ||
143 | } | ||
144 | |||
145 | /* sample in j direction */ | ||
146 | } else { | ||
147 | slope = di / dj; | ||
148 | step = (ix>=jx) ? 1: -1; | ||
149 | |||
150 | jjp1 = jx; | ||
151 | iip1 = jy; | ||
152 | |||
153 | |||
154 | for (jj=0;jj<abs(dj);jj++){ | ||
155 | jjp2 = jjp1 + step; | ||
156 | iip2 = (int)(0.5+ slope*(jjp2-jx) + jy); | ||
157 | |||
158 | phase2 = ephase[iip2+jjp2*nr]; | ||
159 | |||
160 | if (phase1 != phase2){ | ||
161 | z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]); | ||
162 | if (z > maxori){ | ||
163 | maxori = z; | ||
164 | } | ||
165 | |||
166 | } | ||
167 | |||
168 | iip1 = iip2; | ||
169 | jjp1 = jjp2; | ||
170 | phase1 = phase2; | ||
171 | } | ||
172 | } | ||
173 | |||
174 | maxori = 0.5 * maxori*a; | ||
175 | maxori = exp(-maxori*maxori); | ||
176 | } | ||
177 | ir[total] = i; | ||
178 | |||
179 | w[total] = maxori + 0.005; | ||
180 | total = total + 1; | ||
181 | |||
182 | } /* i */ | ||
183 | } /* j */ | ||
184 | |||
185 | jc[np] = total; | ||
186 | } | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib/TOOLBOX_calib.tar b/SD-VBS/common/toolbox/toolbox_basic/calib/TOOLBOX_calib.tar new file mode 100755 index 0000000..92ab3a0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib/TOOLBOX_calib.tar | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/Distor2Calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/Distor2Calib.m new file mode 100755 index 0000000..a82f583 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/Distor2Calib.m | |||
@@ -0,0 +1,391 @@ | |||
1 | function [fc_2,Rc_2,Tc_2,H_2,distance,V_vert,V_hori,x_all_c,V_hori_pix,V_vert_pix,V_diag1_pix,V_diag2_pix]=Distor2Calib(k_dist,grid_pts_centered,n_sq_x,n_sq_y,Np,W,L,Xgrid_2,f_ini,N_iter,two_focal); | ||
2 | |||
3 | % Computes the calibration parameters knowing the | ||
4 | % distortion factor k_dist | ||
5 | |||
6 | % grid_pts_centered are the grid point coordinates after substraction of | ||
7 | % the optical center. | ||
8 | |||
9 | % can give an optional guess for the focal length f_ini (can set to []) | ||
10 | % can provide the number of iterations for the Iterative Vanishing Point Algorithm | ||
11 | |||
12 | % if the focal length is known perfectly, then, there is no need to iterate, | ||
13 | % and therefore, one can fix: N_iter = 0; | ||
14 | |||
15 | % California Institute of Technology | ||
16 | % (c) Jean-Yves Bouguet - October 7th, 1997 | ||
17 | |||
18 | |||
19 | |||
20 | %keyboard; | ||
21 | |||
22 | if exist('two_focal'), | ||
23 | if isempty(two_focal), | ||
24 | two_focal=0; | ||
25 | end; | ||
26 | else | ||
27 | two_focal = 0; | ||
28 | end; | ||
29 | |||
30 | |||
31 | if exist('N_iter'), | ||
32 | if ~isempty(N_iter), | ||
33 | disp('Use number of iterations provided'); | ||
34 | else | ||
35 | N_iter = 10; | ||
36 | end; | ||
37 | else | ||
38 | N_iter = 10; | ||
39 | end; | ||
40 | |||
41 | if exist('f_ini'), | ||
42 | if ~isempty(f_ini), | ||
43 | disp('Use focal provided'); | ||
44 | if length(f_ini)<2, f_ini=[f_ini;f_ini]; end; | ||
45 | fc_2 = f_ini; | ||
46 | x_all_c = [grid_pts_centered(1,:)/fc_2(1);grid_pts_centered(2,:)/fc_2(2)]; | ||
47 | x_all_c = comp_distortion(x_all_c,k_dist); % we can this time!!! | ||
48 | else | ||
49 | fc_2 = [1;1]; | ||
50 | x_all_c = grid_pts_centered; | ||
51 | end; | ||
52 | else | ||
53 | fc_2 = [1;1]; | ||
54 | x_all_c = grid_pts_centered; | ||
55 | end; | ||
56 | |||
57 | |||
58 | dX = W/n_sq_x; | ||
59 | dY = L/n_sq_y; | ||
60 | |||
61 | |||
62 | N_x = n_sq_x+1; | ||
63 | N_y = n_sq_y+1; | ||
64 | |||
65 | |||
66 | x_grid = zeros(N_x,N_y); | ||
67 | y_grid = zeros(N_x,N_y); | ||
68 | |||
69 | |||
70 | |||
71 | |||
72 | |||
73 | %%% Computation of the four vanishing points in pixels | ||
74 | |||
75 | |||
76 | x_grid(:) = grid_pts_centered(1,:); | ||
77 | y_grid(:) = grid_pts_centered(2,:); | ||
78 | |||
79 | for k=1:n_sq_x+1, | ||
80 | [U,S,V] = svd([x_grid(k,:);y_grid(k,:);ones(1,n_sq_y+1)]); | ||
81 | vert(:,k) = U(:,3); | ||
82 | end; | ||
83 | |||
84 | for k=1:n_sq_y+1, | ||
85 | [U,S,V] = svd([x_grid(:,k)';y_grid(:,k)';ones(1,n_sq_x+1)]); | ||
86 | hori(:,k) = U(:,3); | ||
87 | end; | ||
88 | |||
89 | % 2 principle Vanishing points: | ||
90 | [U,S,V] = svd(vert); | ||
91 | V_vert = U(:,3); | ||
92 | [U,S,V] = svd(hori); | ||
93 | V_hori = U(:,3); | ||
94 | |||
95 | |||
96 | |||
97 | % Square warping: | ||
98 | |||
99 | |||
100 | vert_first = vert(:,1) - dot(V_vert,vert(:,1))/dot(V_vert,V_vert) * V_vert; | ||
101 | vert_last = vert(:,n_sq_x+1) - dot(V_vert,vert(:,n_sq_x+1))/dot(V_vert,V_vert) * V_vert; | ||
102 | |||
103 | hori_first = hori(:,1) - dot(V_hori,hori(:,1))/dot(V_hori,V_hori) * V_hori; | ||
104 | hori_last = hori(:,n_sq_y+1) - dot(V_hori,hori(:,n_sq_y+1))/dot(V_hori,V_hori) * V_hori; | ||
105 | |||
106 | |||
107 | x1 = cross(hori_first,vert_first); | ||
108 | x2 = cross(hori_first,vert_last); | ||
109 | x3 = cross(hori_last,vert_last); | ||
110 | x4 = cross(hori_last,vert_first); | ||
111 | |||
112 | x1 = x1/x1(3); | ||
113 | x2 = x2/x2(3); | ||
114 | x3 = x3/x3(3); | ||
115 | x4 = x4/x4(3); | ||
116 | |||
117 | |||
118 | |||
119 | [square] = Rectangle2Square([x1 x2 x3 x4],W,L); | ||
120 | |||
121 | y1 = square(:,1); | ||
122 | y2 = square(:,2); | ||
123 | y3 = square(:,3); | ||
124 | y4 = square(:,4); | ||
125 | |||
126 | H2 = cross(V_vert,V_hori); | ||
127 | |||
128 | V_diag1 = cross(cross(y1,y3),H2); | ||
129 | V_diag2 = cross(cross(y2,y4),H2); | ||
130 | |||
131 | V_diag1 = V_diag1 / norm(V_diag1); | ||
132 | V_diag2 = V_diag2 / norm(V_diag2); | ||
133 | |||
134 | V_hori_pix = V_hori; | ||
135 | V_vert_pix = V_vert; | ||
136 | V_diag1_pix = V_diag1; | ||
137 | V_diag2_pix = V_diag2; | ||
138 | |||
139 | |||
140 | % end of computation of the vanishing points in pixels. | ||
141 | |||
142 | |||
143 | |||
144 | |||
145 | |||
146 | |||
147 | |||
148 | |||
149 | if two_focal, % only if we attempt to estimate two focals... | ||
150 | % Use diagonal lines also to add two extra vanishing points (?) | ||
151 | N_min = min(N_x,N_y); | ||
152 | |||
153 | if N_min < 2, | ||
154 | use_diag = 0; | ||
155 | two_focal = 0; | ||
156 | disp('Cannot estimate two focals (no existing diagonals)'); | ||
157 | else | ||
158 | use_diag = 1; | ||
159 | Delta_N = abs(N_x-N_y); | ||
160 | N_extra = round((N_min - Delta_N - 1)/2); | ||
161 | diag_list = -N_extra:Delta_N+N_extra; | ||
162 | N_diag = length(diag_list); | ||
163 | diag_1 = zeros(3,N_diag); | ||
164 | diag_2 = zeros(3,N_diag); | ||
165 | end; | ||
166 | else | ||
167 | % Give up the use of the diagonals (so far) | ||
168 | % it seems that the error is increased | ||
169 | use_diag = 0; | ||
170 | end; | ||
171 | |||
172 | |||
173 | |||
174 | % The vertical lines: vert, Horizontal lines: hori | ||
175 | vert = zeros(3,n_sq_x+1); | ||
176 | hori = zeros(3,n_sq_y+1); | ||
177 | |||
178 | for counter_k = 1:N_iter, % the Iterative Vanishing Points Algorithm to | ||
179 | % estimate the focal length accurately | ||
180 | |||
181 | x_grid(:) = x_all_c(1,:); | ||
182 | y_grid(:) = x_all_c(2,:); | ||
183 | |||
184 | for k=1:n_sq_x+1, | ||
185 | [U,S,V] = svd([x_grid(k,:);y_grid(k,:);ones(1,n_sq_y+1)]); | ||
186 | vert(:,k) = U(:,3); | ||
187 | end; | ||
188 | |||
189 | for k=1:n_sq_y+1, | ||
190 | [U,S,V] = svd([x_grid(:,k)';y_grid(:,k)';ones(1,n_sq_x+1)]); | ||
191 | hori(:,k) = U(:,3); | ||
192 | end; | ||
193 | |||
194 | % 2 principle Vanishing points: | ||
195 | [U,S,V] = svd(vert); | ||
196 | V_vert = U(:,3); | ||
197 | [U,S,V] = svd(hori); | ||
198 | V_hori = U(:,3); | ||
199 | |||
200 | |||
201 | |||
202 | % Square warping: | ||
203 | |||
204 | |||
205 | vert_first = vert(:,1) - dot(V_vert,vert(:,1))/dot(V_vert,V_vert) * V_vert; | ||
206 | vert_last = vert(:,n_sq_x+1) - dot(V_vert,vert(:,n_sq_x+1))/dot(V_vert,V_vert) * V_vert; | ||
207 | |||
208 | hori_first = hori(:,1) - dot(V_hori,hori(:,1))/dot(V_hori,V_hori) * V_hori; | ||
209 | hori_last = hori(:,n_sq_y+1) - dot(V_hori,hori(:,n_sq_y+1))/dot(V_hori,V_hori) * V_hori; | ||
210 | |||
211 | |||
212 | x1 = cross(hori_first,vert_first); | ||
213 | x2 = cross(hori_first,vert_last); | ||
214 | x3 = cross(hori_last,vert_last); | ||
215 | x4 = cross(hori_last,vert_first); | ||
216 | |||
217 | x1 = x1/x1(3); | ||
218 | x2 = x2/x2(3); | ||
219 | x3 = x3/x3(3); | ||
220 | x4 = x4/x4(3); | ||
221 | |||
222 | |||
223 | |||
224 | [square] = Rectangle2Square([x1 x2 x3 x4],W,L); | ||
225 | |||
226 | y1 = square(:,1); | ||
227 | y2 = square(:,2); | ||
228 | y3 = square(:,3); | ||
229 | y4 = square(:,4); | ||
230 | |||
231 | H2 = cross(V_vert,V_hori); | ||
232 | |||
233 | V_diag1 = cross(cross(y1,y3),H2); | ||
234 | V_diag2 = cross(cross(y2,y4),H2); | ||
235 | |||
236 | V_diag1 = V_diag1 / norm(V_diag1); | ||
237 | V_diag2 = V_diag2 / norm(V_diag2); | ||
238 | |||
239 | |||
240 | |||
241 | |||
242 | % Estimation of the focal length, and normalization: | ||
243 | |||
244 | % Compute the ellipsis of (1/f^2) positions: | ||
245 | % a * (1/fx)^2 + b * (1/fx)^2 = -c | ||
246 | |||
247 | |||
248 | a1 = V_hori(1); | ||
249 | b1 = V_hori(2); | ||
250 | c1 = V_hori(3); | ||
251 | |||
252 | a2 = V_vert(1); | ||
253 | b2 = V_vert(2); | ||
254 | c2 = V_vert(3); | ||
255 | |||
256 | a3 = V_diag1(1); | ||
257 | b3 = V_diag1(2); | ||
258 | c3 = V_diag1(3); | ||
259 | |||
260 | a4 = V_diag2(1); | ||
261 | b4 = V_diag2(2); | ||
262 | c4 = V_diag2(3); | ||
263 | |||
264 | |||
265 | if two_focal, | ||
266 | |||
267 | |||
268 | A = [a1*a2 b1*b2;a3*a4 b3*b4]; | ||
269 | b = -[c1*c2;c3*c4]; | ||
270 | |||
271 | f = sqrt(abs(1./(inv(A)*b))); | ||
272 | |||
273 | else | ||
274 | |||
275 | f = sqrt(abs(-(c1*c2*(a1*a2 + b1*b2) + c3*c4*(a3*a4 + b3*b4))/(c1^2*c2^2 + c3^2*c4^2))); | ||
276 | |||
277 | f = [f;f]; | ||
278 | |||
279 | end; | ||
280 | |||
281 | |||
282 | |||
283 | % REMARK: | ||
284 | % if both a and b are small, the calibration is impossible. | ||
285 | % if one of them is small, only the other focal length is observable | ||
286 | % if none is small, both focals are observable | ||
287 | |||
288 | |||
289 | fc_2 = fc_2 .* f; | ||
290 | |||
291 | |||
292 | % DEBUG PART: fix focal to 500... | ||
293 | %fc_2= [500;500]; disp('Line 293 to be earased in Distor2Calib.m'); | ||
294 | |||
295 | |||
296 | % end of focal compensation | ||
297 | |||
298 | % normalize by the current focal: | ||
299 | |||
300 | x_all = [grid_pts_centered(1,:)/fc_2(1);grid_pts_centered(2,:)/fc_2(2)]; | ||
301 | |||
302 | % Compensate by the distortion factor: | ||
303 | |||
304 | x_all_c = comp_distortion(x_all,k_dist); | ||
305 | |||
306 | end; | ||
307 | |||
308 | % At that point, we hope that the distortion is gone... | ||
309 | |||
310 | x_grid(:) = x_all_c(1,:); | ||
311 | y_grid(:) = x_all_c(2,:); | ||
312 | |||
313 | for k=1:n_sq_x+1, | ||
314 | [U,S,V] = svd([x_grid(k,:);y_grid(k,:);ones(1,n_sq_y+1)]); | ||
315 | vert(:,k) = U(:,3); | ||
316 | end; | ||
317 | |||
318 | for k=1:n_sq_y+1, | ||
319 | [U,S,V] = svd([x_grid(:,k)';y_grid(:,k)';ones(1,n_sq_x+1)]); | ||
320 | hori(:,k) = U(:,3); | ||
321 | end; | ||
322 | |||
323 | % Vanishing points: | ||
324 | [U,S,V] = svd(vert); | ||
325 | V_vert = U(:,3); | ||
326 | [U,S,V] = svd(hori); | ||
327 | V_hori = U(:,3); | ||
328 | |||
329 | % Horizon: | ||
330 | |||
331 | H_2 = cross(V_vert,V_hori); | ||
332 | |||
333 | % H_2 = cross(V_vert,V_hori); | ||
334 | |||
335 | % pick a plane in front of the camera (positive depth) | ||
336 | if H_2(3) < 0, H_2 = -H_2; end; | ||
337 | |||
338 | |||
339 | % Rotation matrix: | ||
340 | |||
341 | if V_hori(1) < 0, V_hori = -V_hori; end; | ||
342 | |||
343 | V_hori = V_hori/norm(V_hori); | ||
344 | H_2 = H_2/norm(H_2); | ||
345 | |||
346 | V_hori = V_hori - dot(V_hori,H_2)*H_2; | ||
347 | |||
348 | Rc_2 = [V_hori cross(H_2,V_hori) H_2]; | ||
349 | |||
350 | Rc_2 = Rc_2 / det(Rc_2); | ||
351 | |||
352 | %omc_2 = rodrigues(Rc_2); | ||
353 | |||
354 | %Rc_2 = rodrigues(omc_2); | ||
355 | |||
356 | % Find the distance of the plane for translation vector: | ||
357 | |||
358 | xc_2 = [x_all_c;ones(1,Np)]; | ||
359 | |||
360 | Zc_2 = 1./sum(xc_2 .* (Rc_2(:,3)*ones(1,Np))); | ||
361 | |||
362 | Xo_2 = [sum(xc_2 .* (Rc_2(:,1)*ones(1,Np))).*Zc_2 ; sum(xc_2 .* (Rc_2(:,2)*ones(1,Np))).*Zc_2]; | ||
363 | |||
364 | XXo_2 = Xo_2 - mean(Xo_2')'*ones(1,Np); | ||
365 | |||
366 | distance_x = norm(Xgrid_2(1,:))/norm(XXo_2(1,:)); | ||
367 | distance_y = norm(Xgrid_2(2,:))/norm(XXo_2(2,:)); | ||
368 | |||
369 | |||
370 | distance = sum(sum(XXo_2(1:2,:).*Xgrid_2(1:2,:)))/sum(sum(XXo_2(1:2,:).^2)); | ||
371 | |||
372 | alpha = abs(distance_x - distance_y)/distance; | ||
373 | |||
374 | if (alpha>0.1)&~two_focal, | ||
375 | disp('Should use two focals in x and y...'); | ||
376 | end; | ||
377 | |||
378 | % Deduce the translation vector: | ||
379 | |||
380 | Tc_2 = distance * H_2; | ||
381 | |||
382 | |||
383 | |||
384 | |||
385 | |||
386 | return; | ||
387 | |||
388 | V_hori_pix/V_hori_pix(3) | ||
389 | V_vert_pix/V_vert_pix(3) | ||
390 | V_diag1_pix/V_diag1_pix(3) | ||
391 | V_diag2_pix/V_diag2_pix(3) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/Multi_Calib_oulu.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/Multi_Calib_oulu.m new file mode 100755 index 0000000..62ca9ae --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/Multi_Calib_oulu.m | |||
@@ -0,0 +1,12 @@ | |||
1 | |||
2 | % enter image names, numbers, ... | ||
3 | data_calib; | ||
4 | |||
5 | %read images from files | ||
6 | ima_read_calib; | ||
7 | |||
8 | click_calib; | ||
9 | |||
10 | %go_calib; % the original version | ||
11 | |||
12 | go_calib_optim; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/Rectangle2Square.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/Rectangle2Square.m new file mode 100755 index 0000000..a6bbbe5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/Rectangle2Square.m | |||
@@ -0,0 +1,19 @@ | |||
1 | function [square] = Rectangle2Square(rectangle,L,W); | ||
2 | |||
3 | % Generate the square from a rectangle of known segment lengths | ||
4 | % from pt1 to pt2 : L | ||
5 | % from pt2 to pt3 : W | ||
6 | |||
7 | [u_hori,u_vert] = UnWarpPlane(rectangle); | ||
8 | |||
9 | coeff_x = sqrt(W/L); | ||
10 | coeff_y = 1/coeff_x; | ||
11 | |||
12 | x_coord = [ 0 coeff_x coeff_x 0]; | ||
13 | y_coord = [ 0 0 coeff_y coeff_y]; | ||
14 | |||
15 | |||
16 | square = rectangle(:,1) * ones(1,4) + u_hori*x_coord + u_vert*y_coord; | ||
17 | square = square ./ (ones(3,1)*square(3,:)); | ||
18 | |||
19 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/UnWarpPlane.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/UnWarpPlane.m new file mode 100755 index 0000000..8addf52 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/UnWarpPlane.m | |||
@@ -0,0 +1,54 @@ | |||
1 | function [u_hori,u_vert] = UnWarpPlane(x1,x2,x3,x4); | ||
2 | |||
3 | % Recovers the two 3D directions of the rectangular patch x1x2x3x4 | ||
4 | % x1 is the origin point, ie any point of planar coordinate (x,y) on the | ||
5 | % rectangular patch will be projected on the image plane at: | ||
6 | % x1 + x * u_hori + y * u_vert | ||
7 | % | ||
8 | % Note: u_hori and u_vert are also the two vanishing points. | ||
9 | |||
10 | |||
11 | if nargin < 4, | ||
12 | |||
13 | x4 = x1(:,4); | ||
14 | x3 = x1(:,3); | ||
15 | x2 = x1(:,2); | ||
16 | x1 = x1(:,1); | ||
17 | |||
18 | end; | ||
19 | |||
20 | |||
21 | % Image Projection: | ||
22 | L1 = cross(x1,x2); | ||
23 | L2 = cross(x4,x3); | ||
24 | L3 = cross(x2,x3); | ||
25 | L4 = cross(x1,x4); | ||
26 | |||
27 | % Vanishing point: | ||
28 | V1 = cross(L1,L2); | ||
29 | V2 = cross(L3,L4); | ||
30 | |||
31 | % Horizon line: | ||
32 | H = cross(V1,V2); | ||
33 | |||
34 | if H(3) < 0, H = -H; end; | ||
35 | |||
36 | |||
37 | H = H / norm(H); | ||
38 | |||
39 | |||
40 | X1 = x1 / dot(H,x1); | ||
41 | X2 = x2 / dot(H,x2); | ||
42 | X3 = x3 / dot(H,x3); | ||
43 | X4 = x4 / dot(H,x4); | ||
44 | |||
45 | scale = X1(3); | ||
46 | |||
47 | X1 = X1/scale; | ||
48 | X2 = X2/scale; | ||
49 | X3 = X3/scale; | ||
50 | X4 = X4/scale; | ||
51 | |||
52 | |||
53 | u_hori = X2 - X1; | ||
54 | u_vert = X4 - X1; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/add_suppress.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/add_suppress.m new file mode 100755 index 0000000..b9bcc57 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/add_suppress.m | |||
@@ -0,0 +1,91 @@ | |||
1 | check_active_images; | ||
2 | |||
3 | |||
4 | fprintf(1,'\nThis function is useful to select a subset of images to calibrate\n'); | ||
5 | |||
6 | fprintf(1,'\nThere are currently %d active images selected for calibration (out of %d):\n',length(ind_active),n_ima); | ||
7 | |||
8 | if ~isempty(ind_active), | ||
9 | |||
10 | for ii = 1:length(ind_active)-2, | ||
11 | |||
12 | fprintf(1,'%d, ',ind_active(ii)); | ||
13 | |||
14 | end; | ||
15 | |||
16 | fprintf(1,'%d and %d.',ind_active(end-1),ind_active(end)); | ||
17 | |||
18 | end; | ||
19 | |||
20 | fprintf(1,'\n'); | ||
21 | |||
22 | |||
23 | fprintf(1,'\nDo you want to suppress or add images from that list?\n'); | ||
24 | |||
25 | choice = 2; | ||
26 | |||
27 | while (choice~=0)&(choice~=1), | ||
28 | choice = input('For suppressing images enter 0, for adding images enter 1 ([]=no change): '); | ||
29 | if isempty(choice), | ||
30 | fprintf(1,'No change applied to the list of active images.\n'); | ||
31 | return; | ||
32 | end; | ||
33 | if (choice~=0)&(choice~=1), | ||
34 | disp('Bad entry. Try again.'); | ||
35 | end; | ||
36 | end; | ||
37 | |||
38 | |||
39 | if choice, | ||
40 | |||
41 | ima_numbers = input('Number(s) of image(s) to add ([] = all images) = '); | ||
42 | |||
43 | if isempty(ima_numbers), | ||
44 | fprintf(1,'All %d images are now active\n',n_ima); | ||
45 | ima_proc = 1:n_ima; | ||
46 | else | ||
47 | ima_proc = ima_numbers; | ||
48 | end; | ||
49 | |||
50 | else | ||
51 | |||
52 | |||
53 | ima_numbers = input('Number(s) of image(s) to suppress ([] = no image) = '); | ||
54 | |||
55 | if isempty(ima_numbers), | ||
56 | fprintf(1,'No image has been suppressed. No modication of the list of active images.\n',n_ima); | ||
57 | ima_proc = []; | ||
58 | else | ||
59 | ima_proc = ima_numbers; | ||
60 | end; | ||
61 | |||
62 | end; | ||
63 | |||
64 | if ~isempty(ima_proc), | ||
65 | |||
66 | active_images(ima_proc) = choice * ones(1,length(ima_proc)); | ||
67 | |||
68 | end; | ||
69 | |||
70 | |||
71 | check_active_images; | ||
72 | |||
73 | |||
74 | fprintf(1,'\nThere is now a total of %d active images for calibration:\n',length(ind_active)); | ||
75 | |||
76 | if ~isempty(ind_active), | ||
77 | |||
78 | for ii = 1:length(ind_active)-2, | ||
79 | |||
80 | fprintf(1,'%d, ',ind_active(ii)); | ||
81 | |||
82 | end; | ||
83 | |||
84 | fprintf(1,'%d and %d.',ind_active(end-1),ind_active(end)); | ||
85 | |||
86 | end; | ||
87 | |||
88 | fprintf(1,'\n\nYou may now run ''Calibration'' to recalibrate based on this new set of images.\n'); | ||
89 | |||
90 | |||
91 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/analyse_error.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/analyse_error.m new file mode 100755 index 0000000..5bfa3b5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/analyse_error.m | |||
@@ -0,0 +1,104 @@ | |||
1 | % Color code for each image: | ||
2 | |||
3 | check_active_images; | ||
4 | |||
5 | if ~exist(['ex_' num2str(ind_active(1)) ]), | ||
6 | fprintf(1,'Need to calibrate before analysing reprojection error. Maybe need to load Calib_Results.mat file.\n'); | ||
7 | return; | ||
8 | end; | ||
9 | |||
10 | |||
11 | if ~exist('no_grid'), | ||
12 | no_grid = 0; | ||
13 | end; | ||
14 | |||
15 | colors = 'brgkcm'; | ||
16 | |||
17 | |||
18 | figure(5); | ||
19 | |||
20 | for kk = 1:n_ima, | ||
21 | if active_images(kk) & eval(['~isnan(y_' num2str(kk) '(1,1))']), | ||
22 | eval(['plot(ex_' num2str(kk) '(1,:),ex_' num2str(kk) '(2,:),''' colors(rem(kk-1,6)+1) '+'');']); | ||
23 | hold on; | ||
24 | end; | ||
25 | end; | ||
26 | hold off; | ||
27 | axis('equal'); | ||
28 | if 1, %~no_grid, | ||
29 | title('Reprojection error (in pixel) - To exit: right button'); | ||
30 | else | ||
31 | title('Reprojection error (in pixel)'); | ||
32 | end; | ||
33 | xlabel('x'); | ||
34 | ylabel('y'); | ||
35 | |||
36 | set(5,'Name','error','NumberTitle','off'); | ||
37 | |||
38 | |||
39 | |||
40 | %err_std = std(ex')'; | ||
41 | |||
42 | %fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
43 | |||
44 | b = 1; | ||
45 | |||
46 | while b==1, | ||
47 | |||
48 | [xp,yp,b] = ginput3(1); | ||
49 | |||
50 | if b==1, | ||
51 | ddd = (ex(1,:)-xp).^2 + (ex(2,:)-yp).^2; | ||
52 | |||
53 | [mind,indmin] = min(ddd); | ||
54 | |||
55 | |||
56 | done = 0; | ||
57 | kk_ima = 1; | ||
58 | while (~done)&(kk_ima<=n_ima), | ||
59 | %fprintf(1,'%d...',kk_ima); | ||
60 | eval(['ex_kk = ex_' num2str(kk_ima) ';']); | ||
61 | sol_kk = find((ex_kk(1,:) == ex(1,indmin))&(ex_kk(2,:) == ex(2,indmin))); | ||
62 | if isempty(sol_kk), | ||
63 | kk_ima = kk_ima + 1; | ||
64 | else | ||
65 | done = 1; | ||
66 | end; | ||
67 | end; | ||
68 | |||
69 | if ~no_grid, | ||
70 | |||
71 | eval(['n_sq_x = n_sq_x_' num2str(kk_ima) ';']); | ||
72 | eval(['n_sq_y = n_sq_y_' num2str(kk_ima) ';']); | ||
73 | |||
74 | Nx = n_sq_x+1; | ||
75 | Ny = n_sq_y+1; | ||
76 | |||
77 | y1 = floor((sol_kk-1)./Nx); | ||
78 | x1 = sol_kk - 1 - Nx*y1; %rem(sol_kk-1,Nx); | ||
79 | |||
80 | y1 = (n_sq_y+1) - y1; | ||
81 | x1 = x1 + 1; | ||
82 | |||
83 | fprintf(1,'\nSelected image: %d\nSelected point: (col,row)=(%d,%d)\nNcol=%d, Nrow=%d\n',[kk_ima x1 y1 Nx Ny]); | ||
84 | fprintf(1,'Pixel error = (%3.5f,%3.5f)\n',[ex(1,indmin) ex(2,indmin)]); | ||
85 | |||
86 | else | ||
87 | |||
88 | eval(['x_kk = x_' num2str(kk_ima) ';']); | ||
89 | |||
90 | xpt = x_kk(:,sol_kk); | ||
91 | |||
92 | fprintf(1,'\nSelected image: %d\nImage coordinates (in pixel): (%3.2f,%3.2f)\n',[kk_ima xpt']); | ||
93 | fprintf(1,'Pixel error = (%3.5f,%3.5f)\n',[ex(1,indmin) ex(2,indmin)]); | ||
94 | |||
95 | |||
96 | end; | ||
97 | |||
98 | |||
99 | end; | ||
100 | |||
101 | end; | ||
102 | |||
103 | disp('done'); | ||
104 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/calib.m new file mode 100755 index 0000000..5b0fdac --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/calib.m | |||
@@ -0,0 +1,74 @@ | |||
1 | if ~exist('instructions'), instructions = 1; end; | ||
2 | |||
3 | if instructions, | ||
4 | |||
5 | fprintf(1,'\n'); | ||
6 | fprintf(1,'*----------------------------------------------------------------------------------------------------*\n'); | ||
7 | fprintf(1,'| Main Calibration toolbox (2D and 3D rigs) |\n'); | ||
8 | fprintf(1,'| (c) Jean-Yves Bouguet - September 9th, 1999 |\n'); | ||
9 | fprintf(1,'*----------------------------------------------------------------------------------------------------*\n\n\n'); | ||
10 | |||
11 | fprintf(1,'CLICK ON:\n\n'); | ||
12 | |||
13 | fprintf(1,'2D: To perform camera calibration from multiple views of a 2D planar grid. \n'); | ||
14 | fprintf(1,' Set default size of grid (in dX_default and dY_default) in click_calib.m.\n'); | ||
15 | fprintf(1,'3D: To perform camera calibration from multiple views of a 3D grid corner. \n'); | ||
16 | fprintf(1,' Set default size of grids (in dX_default and dY_default) in click_calib3D.m.\n'); | ||
17 | fprintf(1,'Exit: To close the calibration tool. \n'); | ||
18 | |||
19 | end; | ||
20 | |||
21 | instructions = 0; | ||
22 | |||
23 | fig_number = 1; | ||
24 | |||
25 | n_row = 1; | ||
26 | n_col = 3; | ||
27 | |||
28 | string_list = cell(n_row,n_col); | ||
29 | callback_list = cell(n_row,n_col); | ||
30 | |||
31 | x_size = 40; | ||
32 | y_size = 20; | ||
33 | |||
34 | title_figure = 'Calibration tool'; | ||
35 | |||
36 | string_list{1,1} = '2D rig'; | ||
37 | string_list{1,2} = '3D rig'; | ||
38 | string_list{1,3} = 'Exit'; | ||
39 | |||
40 | callback_list{1,1} = 'calib_gui;'; | ||
41 | callback_list{1,2} = 'calib3D_gui;'; | ||
42 | callback_list{1,3} = ['disp(''Bye. To run again, type calib.''); close(' num2str(fig_number) ');']; | ||
43 | |||
44 | |||
45 | figure(fig_number); clf; | ||
46 | pos = get(fig_number,'Position'); | ||
47 | |||
48 | fig_size_x = x_size*n_col+(n_col+1)*2; | ||
49 | fig_size_y = y_size*n_row+(n_row+1)*2; | ||
50 | |||
51 | set(fig_number,'Units','points', ... | ||
52 | 'BackingStore','off', ... | ||
53 | 'Color',[0.8 0.8 0.8], ... | ||
54 | 'MenuBar','none', ... | ||
55 | 'Resize','off', ... | ||
56 | 'Name',title_figure, ... | ||
57 | 'Position',[pos(1) pos(2) fig_size_x fig_size_y], ... | ||
58 | 'NumberTitle','off'); | ||
59 | |||
60 | |||
61 | for i=n_row:-1:1, | ||
62 | for j = n_col:-1:1, | ||
63 | if (~isempty(callback_list{i,j}))&(~isempty(string_list{i,j})), | ||
64 | uicontrol('Parent',fig_number, ... | ||
65 | 'Units','points', ... | ||
66 | 'Callback',callback_list{i,j}, ... | ||
67 | 'ListboxTop',0, ... | ||
68 | 'Position',[(2+(j-1)*(x_size+2)) (fig_size_y - i*(2+y_size)) x_size y_size], ... | ||
69 | 'String',string_list{i,j}, ... | ||
70 | 'Tag','Pushbutton1'); | ||
71 | end; | ||
72 | end; | ||
73 | end; | ||
74 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/calib3D_gui.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/calib3D_gui.m new file mode 100755 index 0000000..ff24f6b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/calib3D_gui.m | |||
@@ -0,0 +1,115 @@ | |||
1 | if ~exist('instructions3D'), instructions3D = 1; end; | ||
2 | |||
3 | if instructions3D, | ||
4 | |||
5 | fprintf(1,'\n'); | ||
6 | fprintf(1,'*----------------------------------------------------------------------------------------------------*\n'); | ||
7 | fprintf(1,'| Canera calibration from multiple images of the Intel 3D calibration rig |\n'); | ||
8 | fprintf(1,'| (c) Jean-Yves Bouguet - September 2nd, 1999 |\n'); | ||
9 | fprintf(1,'*----------------------------------------------------------------------------------------------------*\n\n\n'); | ||
10 | |||
11 | fprintf(1,'LIST OF CALIBRATION COMMANDS (to be executed from 1 to 5):\n\n'); | ||
12 | |||
13 | fprintf(1,'1- Image names: Lets the user enter the file names of the calibration images (max = 30 images).\n'); | ||
14 | fprintf(1,' It includes basename, image type (''tif'', ''bmp'' or ''ras''), numbering scheme.\n'); | ||
15 | fprintf(1,' Automatically launchs the next step (Read images).\n'); | ||
16 | fprintf(1,'2- Read images: Reads in the calibration images from files.\n'); | ||
17 | fprintf(1,' Does not automatically launch the next step (Extract grid corners).\n'); | ||
18 | fprintf(1,'3- Extract grid corners: Extracts the grid corners from the image.\n'); | ||
19 | fprintf(1,' Based six maual clicks per image.\n'); | ||
20 | fprintf(1,' The calibration data is saved under ''calib_data.mat''.\n'); | ||
21 | fprintf(1,' Automatically launchs the next step (Run calibration).\n'); | ||
22 | fprintf(1,'4- Run calibration: Main calibration procedure.\n'); | ||
23 | fprintf(1,' Optimization of intrinsic and extrinsic parameters to minimize\n'); | ||
24 | fprintf(1,' the reprojection error (in the least squares sense.\n'); | ||
25 | fprintf(1,' Estimated parameters: 2 focal lengths, principal point,\n'); | ||
26 | fprintf(1,' radial (2 coeff. -> 4 degree model) and tangential (2 coeff.) distortion,\n'); | ||
27 | fprintf(1,' and extrinsic parameters (6 parameters per image).\n'); | ||
28 | fprintf(1,' The final solution is saved under ''Calib_Results.mat''.\n'); | ||
29 | fprintf(1,' For a description of the intrinsic camera model, refer to the reference:\n'); | ||
30 | fprintf(1,' "A Four-step Camera Calibration Procedure with implicit Image Correction"\n'); | ||
31 | fprintf(1,' Janne Heikkila and Olli Silven, Infotech Oulu and Department of EE\n'); | ||
32 | fprintf(1,' University of Oulu, Appeared in CVPR''97, Puerto Rico.\n'); | ||
33 | fprintf(1,' Visit http://www.ee.oulu.fi/~jth/calibr/Calibration.html\n'); | ||
34 | fprintf(1,' Automatically launchs the next step (Graphic out).\n'); | ||
35 | fprintf(1,'5- Graphic out: Generates the graphical output associated to the current calibration solution.\n'); | ||
36 | fprintf(1,' It shows the 3D locations of the grids, and reprojects the 3D patterns on the\n'); | ||
37 | fprintf(1,' original calibration images.\n'); | ||
38 | fprintf(1,'6- sol. with center: Lets the user select the calibration solution with computed principal point.\n'); | ||
39 | fprintf(1,' This is the default case (solution retained after Run calibration).\n'); | ||
40 | fprintf(1,' Automatically (re)generates the graphical output associated to that solution.\n'); | ||
41 | fprintf(1,'7- sol. without center: Lets the users select the calibration solution without computed principal point.\n'); | ||
42 | fprintf(1,' In that case, the principal point is assumed at the center of the image.\n'); | ||
43 | fprintf(1,' Automatically generates the graphical output associated to that solution.\n'); | ||
44 | fprintf(1,' This option is sometimes useful when the principal point is difficult to\n'); | ||
45 | fprintf(1,' estimate (in particular when the camera field of view is small).\n'); | ||
46 | fprintf(1,'8- Back to main: Goes back to the main calbration toolbox window.\n\n\n'); | ||
47 | |||
48 | end; | ||
49 | |||
50 | instructions3D = 0; | ||
51 | |||
52 | global X_1 x_1 X_2 x_2 X_3 x_3 X_4 x_4 X_5 x_5 X_6 x_6 X_7 x_7 X_8 x_8 X_9 x_9 X_10 x_10 X_11 x_11 X_12 x_12 X_13 x_13 X_14 x_14 X_15 x_15 X_16 x_16 X_17 x_17 X_18 x_18 X_19 x_19 X_20 x_20 X_21 x_21 X_22 x_22 X_23 x_23 X_24 x_24 X_25 x_25 X_26 x_26 X_27 x_27 X_28 x_28 X_29 x_29 X_30 x_30 | ||
53 | |||
54 | |||
55 | fig_number = 1; | ||
56 | |||
57 | n_row = 2; | ||
58 | n_col = 4; | ||
59 | |||
60 | string_list = cell(n_row,n_col); | ||
61 | callback_list = cell(n_row,n_col); | ||
62 | |||
63 | x_size = 85; | ||
64 | y_size = 20; | ||
65 | |||
66 | title_figure = 'Camera calibration tool (3D rig)'; | ||
67 | |||
68 | string_list{1,1} = 'Image names'; | ||
69 | string_list{1,2} = 'Read images'; | ||
70 | string_list{1,3} = 'Extract grid corners'; | ||
71 | string_list{1,4} = 'Run calibration'; | ||
72 | string_list{2,1} = 'Graphic out'; | ||
73 | string_list{2,2} = 'sol. with center'; | ||
74 | string_list{2,3} = 'sol. without center'; | ||
75 | string_list{2,4} = 'Back to main'; | ||
76 | |||
77 | callback_list{1,1} = 'data_calib;'; | ||
78 | callback_list{1,2} = 'ima_read_calib;'; | ||
79 | callback_list{1,3} = 'click_calib3D;'; | ||
80 | callback_list{1,4} = 'go_calib_optim3D;'; | ||
81 | callback_list{2,1} = 'graphout_calib3D;'; | ||
82 | callback_list{2,2} = 'select_sol_with_center3D;'; | ||
83 | callback_list{2,3} = 'select_sol_no_center3D;'; | ||
84 | callback_list{2,4} = 'calib;'; | ||
85 | |||
86 | |||
87 | figure(fig_number); clf; | ||
88 | pos = get(fig_number,'Position'); | ||
89 | |||
90 | fig_size_x = x_size*n_col+(n_col+1)*2; | ||
91 | fig_size_y = y_size*n_row+(n_row+1)*2; | ||
92 | |||
93 | set(fig_number,'Units','points', ... | ||
94 | 'BackingStore','off', ... | ||
95 | 'Color',[0.8 0.8 0.8], ... | ||
96 | 'MenuBar','none', ... | ||
97 | 'Resize','off', ... | ||
98 | 'Name',title_figure, ... | ||
99 | 'Position',[pos(1) pos(2) fig_size_x fig_size_y], ... | ||
100 | 'NumberTitle','off'); | ||
101 | |||
102 | |||
103 | for i=n_row:-1:1, | ||
104 | for j = n_col:-1:1, | ||
105 | if (~isempty(callback_list{i,j}))&(~isempty(string_list{i,j})), | ||
106 | uicontrol('Parent',fig_number, ... | ||
107 | 'Units','points', ... | ||
108 | 'Callback',callback_list{i,j}, ... | ||
109 | 'ListboxTop',0, ... | ||
110 | 'Position',[(2+(j-1)*(x_size+2)) (fig_size_y - i*(2+y_size)) x_size y_size], ... | ||
111 | 'String',string_list{i,j}, ... | ||
112 | 'Tag','Pushbutton1'); | ||
113 | end; | ||
114 | end; | ||
115 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/calib_gui.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/calib_gui.m new file mode 100755 index 0000000..62a45dd --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/calib_gui.m | |||
@@ -0,0 +1,81 @@ | |||
1 | fig_number = 1; | ||
2 | |||
3 | n_row = 5; | ||
4 | n_col = 4; | ||
5 | |||
6 | string_list = cell(n_row,n_col); | ||
7 | callback_list = cell(n_row,n_col); | ||
8 | |||
9 | x_size = 85; | ||
10 | y_size = 20; | ||
11 | |||
12 | title_figure = 'Camera calibration tool (2D rig)'; | ||
13 | |||
14 | string_list{1,1} = 'Image names'; | ||
15 | string_list{1,2} = 'Read images'; | ||
16 | string_list{1,3} = 'Extract grid corners'; | ||
17 | %string_list{1,4} = 'Initialization'; | ||
18 | string_list{1,4} = 'Calibration'; | ||
19 | string_list{2,1} = 'Show Extrinsic'; | ||
20 | string_list{2,2} = 'Reproject on images'; | ||
21 | string_list{2,3} = 'Analyse error'; | ||
22 | string_list{2,4} = 'Recomp. corners'; | ||
23 | string_list{3,1} = 'Add/Suppress images'; | ||
24 | string_list{3,2} = 'Save'; | ||
25 | string_list{3,3} = 'Load'; | ||
26 | string_list{3,4} = 'Exit'; | ||
27 | |||
28 | string_list{5,1} = 'Comp. Extrinsic'; | ||
29 | string_list{5,2} = 'Undistort image'; | ||
30 | |||
31 | |||
32 | callback_list{1,1} = 'data_calib;'; | ||
33 | callback_list{1,2} = 'ima_read_calib;'; | ||
34 | callback_list{1,3} = 'click_calib;'; | ||
35 | %callback_list{1,4} = 'init_calib_param;'; | ||
36 | callback_list{1,4} = 'go_calib_optim;'; | ||
37 | callback_list{2,1} = 'ext_calib;'; | ||
38 | callback_list{2,2} = 'reproject_calib;'; | ||
39 | callback_list{2,3} = 'analyse_error;'; | ||
40 | callback_list{2,4} = 'recomp_corner_calib;'; | ||
41 | callback_list{3,1} = 'add_suppress;'; | ||
42 | callback_list{3,2} = 'saving_calib;'; | ||
43 | callback_list{3,3} = 'loading_calib;'; | ||
44 | callback_list{3,4} = ['disp(''Bye. To run again, type calib_gui.''); close(' num2str(fig_number) ');']; | ||
45 | |||
46 | callback_list{5,1} = 'extrinsic_computation;'; | ||
47 | callback_list{5,2} = 'undistort_image;'; | ||
48 | |||
49 | |||
50 | figure(fig_number); clf; | ||
51 | pos = get(fig_number,'Position'); | ||
52 | |||
53 | fig_size_x = x_size*n_col+(n_col+1)*2; | ||
54 | fig_size_y = y_size*n_row+(n_row+1)*2; | ||
55 | |||
56 | set(fig_number,'Units','points', ... | ||
57 | 'BackingStore','off', ... | ||
58 | 'Color',[0.8 0.8 0.8], ... | ||
59 | 'MenuBar','none', ... | ||
60 | 'Resize','off', ... | ||
61 | 'Name',title_figure, ... | ||
62 | 'Position',[pos(1) pos(2) fig_size_x fig_size_y], ... | ||
63 | 'NumberTitle','off'); %,'WindowButtonMotionFcn',['figure(' num2str(fig_number) ');']); | ||
64 | |||
65 | |||
66 | for i=n_row:-1:1, | ||
67 | for j = n_col:-1:1, | ||
68 | if (~isempty(callback_list{i,j}))&(~isempty(string_list{i,j})), | ||
69 | uicontrol('Parent',fig_number, ... | ||
70 | 'Units','points', ... | ||
71 | 'Callback',callback_list{i,j}, ... | ||
72 | 'ListboxTop',0, ... | ||
73 | 'Position',[(2+(j-1)*(x_size+2)) (fig_size_y - i*(2+y_size)) x_size y_size], ... | ||
74 | 'String',string_list{i,j}, ... | ||
75 | 'Tag','Pushbutton1'); | ||
76 | end; | ||
77 | end; | ||
78 | end; | ||
79 | |||
80 | |||
81 | clear callback_list string_list fig_number fig_size_x fig_size_y i j n_col n_row pos string_list title_figure x_size y_size | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/check_active_images.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/check_active_images.m new file mode 100755 index 0000000..4f09b62 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/check_active_images.m | |||
@@ -0,0 +1,14 @@ | |||
1 | |||
2 | if ~exist('active_images'), | ||
3 | active_images = ones(1,n_ima); | ||
4 | end; | ||
5 | n_act = length(active_images); | ||
6 | if n_act < n_ima, | ||
7 | active_images = [active_images ones(1,n_ima-n_act)]; | ||
8 | else | ||
9 | if n_act > n_ima, | ||
10 | active_images = active_images(1:n_ima); | ||
11 | end; | ||
12 | end; | ||
13 | |||
14 | ind_active = find(active_images); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/check_convergence.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/check_convergence.m new file mode 100755 index 0000000..8602c39 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/check_convergence.m | |||
@@ -0,0 +1,17 @@ | |||
1 | %%% Replay the set of solution vectors: | ||
2 | |||
3 | N_iter = size(param_list,2); | ||
4 | |||
5 | for nn = 1:N_iter, | ||
6 | |||
7 | solution = param_list(:,nn); | ||
8 | |||
9 | extract_parameters; | ||
10 | comp_error_calib; | ||
11 | |||
12 | ext_calib; | ||
13 | |||
14 | drawnow; | ||
15 | |||
16 | |||
17 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/check_planarity.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/check_planarity.m new file mode 100755 index 0000000..be0410b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/check_planarity.m | |||
@@ -0,0 +1,41 @@ | |||
1 | % Check the planarity of a structure: | ||
2 | |||
3 | X = X_1; | ||
4 | N = size(X,2); | ||
5 | |||
6 | %X(3,:) = 0.1*randn(1,N); | ||
7 | |||
8 | om = rand(3,1); | ||
9 | T = 10*rand(3,1); | ||
10 | R = rodrigues(om); | ||
11 | |||
12 | X = R * X + T*ones(1,N); | ||
13 | |||
14 | |||
15 | |||
16 | |||
17 | |||
18 | |||
19 | N = size(X,2); | ||
20 | X_mean = mean(X')'; | ||
21 | |||
22 | Y = X - (X_mean*ones(1,N)); | ||
23 | |||
24 | YY = Y*Y'; | ||
25 | |||
26 | [U,S,V] = svd(YY); | ||
27 | |||
28 | r = S(3,3)/S(2,2); | ||
29 | |||
30 | % if r is less than 1e-4: | ||
31 | |||
32 | R_transform = V'; | ||
33 | T_transform = -(V')*X_mean; | ||
34 | |||
35 | |||
36 | % Thresh for r: 1e-4 | ||
37 | |||
38 | X_new = R_transform*X + T_transform*ones(1,N); | ||
39 | |||
40 | |||
41 | % If Xc = Rc * X_new + Tc, then Xc = Rc * R_transform * X + Tc + T_transform | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_calib.m new file mode 100755 index 0000000..047cc7b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_calib.m | |||
@@ -0,0 +1,99 @@ | |||
1 | |||
2 | if ~exist('I_1'), | ||
3 | ima_read_calib; | ||
4 | if no_image_file, | ||
5 | disp('Cannot extract corners without images'); | ||
6 | return; | ||
7 | end; | ||
8 | end; | ||
9 | |||
10 | check_active_images; | ||
11 | |||
12 | %wintx = 10; % neigborhood of integration for | ||
13 | %winty = 10; % the corner finder | ||
14 | |||
15 | fprintf(1,'\nExtraction of the grid corners on the images\n'); | ||
16 | |||
17 | disp('Window size for corner finder (wintx and winty):'); | ||
18 | wintx = input('wintx ([] = 5) = '); | ||
19 | if isempty(wintx), wintx = 5; end; | ||
20 | wintx = round(wintx); | ||
21 | winty = input('winty ([] = 5) = '); | ||
22 | if isempty(winty), winty = 5; end; | ||
23 | winty = round(winty); | ||
24 | |||
25 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
26 | |||
27 | if ~exist('map'), map = gray(256); end; | ||
28 | |||
29 | |||
30 | disp('WARNING!!! Do not forget to change dX_default and dY_default in click_calib.m!!!') | ||
31 | |||
32 | |||
33 | % Default size of the pattern squares; | ||
34 | |||
35 | % Setup of JY (old at Caltech) | ||
36 | dX_default = 21.9250/11; | ||
37 | dY_default = 18.1250/9; | ||
38 | |||
39 | % Setup of JY (new at Intel) | ||
40 | dX_default = 1.9750; | ||
41 | dY_default = 1.9865; | ||
42 | |||
43 | |||
44 | % Setup of Luis and Enrico | ||
45 | dX_default = 67.7/16; | ||
46 | dY_default = 50.65/12; | ||
47 | |||
48 | |||
49 | % Setup of German | ||
50 | dX_default = 10.16; | ||
51 | dY_default = 10.16; | ||
52 | |||
53 | % Setup of JY (new at Intel) | ||
54 | dX_default = 1.9750*2.54; | ||
55 | dY_default = 1.9865*2.54; | ||
56 | |||
57 | % Setup of JY - 3D calibration rig at Intel (new at Intel) | ||
58 | dX_default = 3; | ||
59 | dY_default = 3; | ||
60 | |||
61 | % Useful option to add images: | ||
62 | kk_first = input('Start image number ([]=1=first): '); | ||
63 | |||
64 | if isempty(kk_first), kk_first = 1; end; | ||
65 | |||
66 | for kk = kk_first:n_ima, | ||
67 | if active_images(kk), | ||
68 | click_ima_calib; | ||
69 | else | ||
70 | eval(['dX_' num2str(kk) ' = NaN;']); | ||
71 | eval(['dY_' num2str(kk) ' = NaN;']); | ||
72 | |||
73 | eval(['wintx_' num2str(kk) ' = NaN;']); | ||
74 | eval(['winty_' num2str(kk) ' = NaN;']); | ||
75 | |||
76 | eval(['x_' num2str(kk) ' = NaN*ones(2,1);']); | ||
77 | eval(['X_' num2str(kk) ' = NaN*ones(3,1);']); | ||
78 | |||
79 | eval(['n_sq_x_' num2str(kk) ' = NaN;']); | ||
80 | eval(['n_sq_y_' num2str(kk) ' = NaN;']); | ||
81 | end; | ||
82 | end; | ||
83 | |||
84 | |||
85 | |||
86 | string_save = 'save calib_data active_images ind_active wintx winty n_ima type_numbering N_slots first_num image_numbers format_image calib_name Hcal Wcal nx ny map dX_default dY_default dX dY'; | ||
87 | |||
88 | for kk = 1:n_ima, | ||
89 | string_save = [string_save ' X_' num2str(kk) ' x_' num2str(kk) ' n_sq_x_' num2str(kk) ' n_sq_y_' num2str(kk) ' wintx_' num2str(kk) ' winty_' num2str(kk) ' dX_' num2str(kk) ' dY_' num2str(kk)]; | ||
90 | end; | ||
91 | |||
92 | eval(string_save); | ||
93 | |||
94 | disp('done'); | ||
95 | |||
96 | return; | ||
97 | |||
98 | go_calib_optim; | ||
99 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_calib3D.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_calib3D.m new file mode 100755 index 0000000..e761cd1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_calib3D.m | |||
@@ -0,0 +1,79 @@ | |||
1 | |||
2 | if ~exist('I_1'), | ||
3 | ima_read_calib; | ||
4 | if no_image_file, | ||
5 | disp('Cannot extract corners without images'); | ||
6 | return; | ||
7 | end; | ||
8 | end; | ||
9 | |||
10 | %wintx = 10; % neigborhood of integration for | ||
11 | %winty = 10; % the corner finder | ||
12 | |||
13 | fprintf(1,'\nExtraction of the grid corners on the images\n'); | ||
14 | |||
15 | disp('Window size for corner finder (wintx and winty):'); | ||
16 | wintx = input('wintx ([] = 5) = '); | ||
17 | if isempty(wintx), wintx = 5; end; | ||
18 | wintx = round(wintx); | ||
19 | winty = input('winty ([] = 5) = '); | ||
20 | if isempty(winty), winty = 5; end; | ||
21 | winty = round(winty); | ||
22 | |||
23 | |||
24 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
25 | |||
26 | |||
27 | disp('WARNNG!!! Do not forget to change dX_default and dY_default in click_calib.m!!!') | ||
28 | |||
29 | |||
30 | % Default size of the pattern squares; | ||
31 | |||
32 | % Setup of JY (old at Caltech) | ||
33 | dX_default = 21.9250/11; | ||
34 | dY_default = 18.1250/9; | ||
35 | |||
36 | % Setup of JY (new at Intel) | ||
37 | dX_default = 1.9750; | ||
38 | dY_default = 1.9865; | ||
39 | |||
40 | |||
41 | % Setup of Luis and Enrico | ||
42 | dX_default = 67.7/16; | ||
43 | dY_default = 50.65/12; | ||
44 | |||
45 | |||
46 | % Setup of German | ||
47 | dX_default = 10.16; | ||
48 | dY_default = 10.16; | ||
49 | |||
50 | % Setup of JY (new at Intel) | ||
51 | dX_default = 1.9750*2.54; | ||
52 | dY_default = 1.9865*2.54; | ||
53 | |||
54 | |||
55 | % Setup of JY - 3D calibration rig at Intel (new at Intel) | ||
56 | dX_default = 3; | ||
57 | dY_default = 3; | ||
58 | |||
59 | % Useful option to add images: | ||
60 | kk_first = input('Start image number ([]=1=first): '); | ||
61 | |||
62 | if isempty(kk_first), kk_first = 1; end; | ||
63 | |||
64 | for kk = kk_first:n_ima, | ||
65 | click_ima_calib3D; %Simple version | ||
66 | %init_calib; %advanced vesion (more messy) | ||
67 | end; | ||
68 | |||
69 | |||
70 | |||
71 | string_save = 'save calib_data wintx winty n_ima type_numbering N_slots first_num image_numbers format_image calib_name Hcal Wcal nx ny map dX_default dY_default dX dY'; | ||
72 | |||
73 | for kk = 1:n_ima, | ||
74 | string_save = [string_save ' X_' num2str(kk) ' x_' num2str(kk) ' Hl_' num2str(kk) ' nl_sq_x_' num2str(kk) ' nl_sq_y_' num2str(kk) ' Hr_' num2str(kk) ' nr_sq_x_' num2str(kk) ' nr_sq_y_' num2str(kk)]; | ||
75 | end; | ||
76 | |||
77 | eval(string_save); | ||
78 | |||
79 | go_calib_optim3D; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_ima_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_ima_calib.m new file mode 100755 index 0000000..5197870 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_ima_calib.m | |||
@@ -0,0 +1,218 @@ | |||
1 | % Cleaned-up version of init_calib.m | ||
2 | |||
3 | fprintf(1,'\nProcessing image %d...\n',kk); | ||
4 | |||
5 | eval(['I = I_' num2str(kk) ';']); | ||
6 | |||
7 | figure(2); | ||
8 | image(I); | ||
9 | colormap(map); | ||
10 | |||
11 | title(['Click on the four extreme corners of the rectangular pattern... Image ' num2str(kk)]); | ||
12 | |||
13 | disp('Click on the four extreme corners of the rectangular complete pattern...'); | ||
14 | |||
15 | [x,y] = ginput3(4); | ||
16 | |||
17 | [Xc,good,bad,type] = cornerfinder([x';y'],I,winty,wintx); % the four corners | ||
18 | |||
19 | x = Xc(1,:)'; | ||
20 | y = Xc(2,:)'; | ||
21 | |||
22 | [y,indy] = sort(y); | ||
23 | x = x(indy); | ||
24 | |||
25 | if (x(2) > x(1)), | ||
26 | x4 = x(1);y4 = y(1); x3 = x(2); y3 = y(2); | ||
27 | else | ||
28 | x4 = x(2);y4 = y(2); x3 = x(1); y3 = y(1); | ||
29 | end; | ||
30 | if (x(3) > x(4)), | ||
31 | x2 = x(3);y2 = y(3); x1 = x(4); y1 = y(4); | ||
32 | else | ||
33 | x2 = x(4);y2 = y(4); x1 = x(3); y1 = y(3); | ||
34 | end; | ||
35 | |||
36 | x = [x1;x2;x3;x4]; | ||
37 | y = [y1;y2;y3;y4]; | ||
38 | |||
39 | |||
40 | figure(2); hold on; | ||
41 | plot([x;x(1)],[y;y(1)],'g-'); | ||
42 | plot(x,y,'og'); | ||
43 | hx=text((x(4)+x(3))/2,(y(4)+y(3))/2 - 20,'X'); | ||
44 | set(hx,'color','g','Fontsize',14); | ||
45 | hy=text((x(4)+x(1))/2-20,(y(4)+y(1))/2,'Y'); | ||
46 | set(hy,'color','g','Fontsize',14); | ||
47 | hold off; | ||
48 | |||
49 | |||
50 | % Try to automatically count the number of squares in the grid | ||
51 | |||
52 | n_sq_x1 = count_squares(I,x1,y1,x2,y2,wintx); | ||
53 | n_sq_x2 = count_squares(I,x3,y3,x4,y4,wintx); | ||
54 | n_sq_y1 = count_squares(I,x2,y2,x3,y3,wintx); | ||
55 | n_sq_y2 = count_squares(I,x4,y4,x1,y1,wintx); | ||
56 | |||
57 | |||
58 | |||
59 | % If could not count the number of squares, enter manually | ||
60 | |||
61 | if (n_sq_x1~=n_sq_x2)|(n_sq_y1~=n_sq_y2), | ||
62 | |||
63 | |||
64 | disp('Could not count the number of squares in the grid. Enter manually.'); | ||
65 | n_sq_x = input('Number of squares along the X direction ([]=10) = '); %6 | ||
66 | if isempty(n_sq_x), n_sq_x = 10; end; | ||
67 | n_sq_y = input('Number of squares along the Y direction ([]=10) = '); %6 | ||
68 | if isempty(n_sq_y), n_sq_y = 10; end; | ||
69 | |||
70 | else | ||
71 | |||
72 | n_sq_x = n_sq_x1; | ||
73 | n_sq_y = n_sq_y1; | ||
74 | |||
75 | end; | ||
76 | |||
77 | |||
78 | % Enter the size of each square | ||
79 | |||
80 | dX = input(['Size dX of each square along the X direction ([]=' num2str(dX_default) 'cm) = ']); | ||
81 | dY = input(['Size dY of each square along the Y direction ([]=' num2str(dY_default) 'cm) = ']); | ||
82 | if isempty(dX), dX = dX_default; else dX_default = dX; end; | ||
83 | if isempty(dY), dY = dY_default; else dY_default = dY; end; | ||
84 | |||
85 | % Compute the inside points through computation of the planar homography (collineation) | ||
86 | |||
87 | a00 = [x(1);y(1);1]; | ||
88 | a10 = [x(2);y(2);1]; | ||
89 | a11 = [x(3);y(3);1]; | ||
90 | a01 = [x(4);y(4);1]; | ||
91 | |||
92 | |||
93 | % Compute the planar collineation: (return the normalization matrix as well) | ||
94 | |||
95 | [Homo,Hnorm,inv_Hnorm] = compute_homography ([a00 a10 a11 a01],[0 1 1 0;0 0 1 1;1 1 1 1]); | ||
96 | |||
97 | |||
98 | % Build the grid using the planar collineation: | ||
99 | |||
100 | x_l = ((0:n_sq_x)'*ones(1,n_sq_y+1))/n_sq_x; | ||
101 | y_l = (ones(n_sq_x+1,1)*(0:n_sq_y))/n_sq_y; | ||
102 | pts = [x_l(:) y_l(:) ones((n_sq_x+1)*(n_sq_y+1),1)]'; | ||
103 | |||
104 | XX = Homo*pts; | ||
105 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
106 | |||
107 | |||
108 | % Complete size of the rectangle | ||
109 | |||
110 | W = n_sq_x*dX; | ||
111 | L = n_sq_y*dY; | ||
112 | |||
113 | |||
114 | |||
115 | |||
116 | %%%%%%%%%%%%%%%%%%%%%%%% ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
117 | figure(2); | ||
118 | hold on; | ||
119 | plot(XX(1,:),XX(2,:),'r+'); | ||
120 | title('The red crosses should be close to the image corners'); | ||
121 | hold off; | ||
122 | |||
123 | disp('If the guessed grid corners (red crosses on the image) are not close to the actual corners,'); | ||
124 | disp('it is necessary to enter an initial guess for the radial distortion factor kc (useful for subpixel detection)'); | ||
125 | quest_distort = input('Need of an initial guess for distortion? ([]=no, other=yes) '); | ||
126 | |||
127 | quest_distort = ~isempty(quest_distort); | ||
128 | |||
129 | if quest_distort, | ||
130 | % Estimation of focal length: | ||
131 | c_g = [size(I,2);size(I,1)]/2 + .5; | ||
132 | f_g = Distor2Calib(0,[[x(1) x(2) x(4) x(3)] - c_g(1);[y(1) y(2) y(4) y(3)] - c_g(2)],1,1,4,W,L,[-W/2 W/2 W/2 -W/2;L/2 L/2 -L/2 -L/2; 0 0 0 0],100,1,1); | ||
133 | f_g = mean(f_g); | ||
134 | script_fit_distortion; | ||
135 | end; | ||
136 | %%%%%%%%%%%%%%%%%%%%% END ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
137 | |||
138 | |||
139 | |||
140 | |||
141 | |||
142 | Np = (n_sq_x+1)*(n_sq_y+1); | ||
143 | |||
144 | disp('Corner extraction...'); | ||
145 | |||
146 | grid_pts = cornerfinder(XX,I,winty,wintx); %%% Finds the exact corners at every points! | ||
147 | |||
148 | |||
149 | |||
150 | %save all_corners x y grid_pts | ||
151 | |||
152 | grid_pts = grid_pts - 1; % subtract 1 to bring the origin to (0,0) instead of (1,1) in matlab (not necessary in C) | ||
153 | |||
154 | |||
155 | % Global Homography from plane to pixel coordinates: | ||
156 | |||
157 | |||
158 | |||
159 | |||
160 | ind_corners = [1 n_sq_x+1 (n_sq_x+1)*n_sq_y+1 (n_sq_x+1)*(n_sq_y+1)]; % index of the 4 corners | ||
161 | ind_orig = (n_sq_x+1)*n_sq_y + 1; | ||
162 | xorig = grid_pts(1,ind_orig); | ||
163 | yorig = grid_pts(2,ind_orig); | ||
164 | dxpos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig+1)]'); | ||
165 | dypos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig-n_sq_x-1)]'); | ||
166 | |||
167 | |||
168 | x_box_kk = [grid_pts(1,:)-(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)-(wintx+.5);grid_pts(1,:)-(wintx+.5)]; | ||
169 | y_box_kk = [grid_pts(2,:)-(winty+.5);grid_pts(2,:)-(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)-(winty+.5)]; | ||
170 | |||
171 | |||
172 | figure(3); | ||
173 | image(I); colormap(map); hold on; | ||
174 | plot(grid_pts(1,:)+1,grid_pts(2,:)+1,'r+'); | ||
175 | plot(x_box_kk+1,y_box_kk+1,'-b'); | ||
176 | plot(grid_pts(1,ind_corners)+1,grid_pts(2,ind_corners)+1,'mo'); | ||
177 | plot(xorig+1,yorig+1,'*m'); | ||
178 | h = text(xorig-15,yorig-15,'O'); | ||
179 | set(h,'Color','m','FontSize',14); | ||
180 | h2 = text(dxpos(1)-10,dxpos(2)-10,'dX'); | ||
181 | set(h2,'Color','g','FontSize',14); | ||
182 | h3 = text(dypos(1)-25,dypos(2)-3,'dY'); | ||
183 | set(h3,'Color','g','FontSize',14); | ||
184 | xlabel('Xc (in camera frame)'); | ||
185 | ylabel('Yc (in camera frame)'); | ||
186 | title('Extracted corners'); | ||
187 | zoom on; | ||
188 | drawnow; | ||
189 | hold off; | ||
190 | |||
191 | |||
192 | Xi = reshape(([0:n_sq_x]*dX)'*ones(1,n_sq_y+1),Np,1)'; | ||
193 | Yi = reshape(ones(n_sq_x+1,1)*[n_sq_y:-1:0]*dY,Np,1)'; | ||
194 | Zi = zeros(1,Np); | ||
195 | |||
196 | Xgrid = [Xi;Yi;Zi]; | ||
197 | |||
198 | |||
199 | % All the point coordinates (on the image, and in 3D) - for global optimization: | ||
200 | |||
201 | x = grid_pts; | ||
202 | X = Xgrid; | ||
203 | |||
204 | |||
205 | % Saves all the data into variables: | ||
206 | |||
207 | eval(['dX_' num2str(kk) ' = dX;']); | ||
208 | eval(['dY_' num2str(kk) ' = dY;']); | ||
209 | |||
210 | eval(['wintx_' num2str(kk) ' = wintx;']); | ||
211 | eval(['winty_' num2str(kk) ' = winty;']); | ||
212 | |||
213 | eval(['x_' num2str(kk) ' = x;']); | ||
214 | eval(['X_' num2str(kk) ' = X;']); | ||
215 | |||
216 | eval(['n_sq_x_' num2str(kk) ' = n_sq_x;']); | ||
217 | eval(['n_sq_y_' num2str(kk) ' = n_sq_y;']); | ||
218 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_ima_calib3D.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_ima_calib3D.m new file mode 100755 index 0000000..7718268 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/click_ima_calib3D.m | |||
@@ -0,0 +1,482 @@ | |||
1 | % Cleaned-up version of init_calib.m | ||
2 | |||
3 | eval(['I = I_' num2str(kk) ';']); | ||
4 | |||
5 | figure(2); | ||
6 | image(I); | ||
7 | colormap(map); | ||
8 | |||
9 | |||
10 | |||
11 | |||
12 | |||
13 | %%%%%%%%%%%%%%%%%%%%%%%%% LEFT PATTERN ACQUISITION %%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
14 | |||
15 | |||
16 | |||
17 | title(['Click on the four extreme corners of the left rectangular pattern... Image ' num2str(kk)]); | ||
18 | |||
19 | disp('Click on the four extreme corners of the left rectangular pattern...'); | ||
20 | |||
21 | [x,y] = ginput3(4); | ||
22 | |||
23 | [Xc,good,bad,type] = cornerfinder([x';y'],I,winty,wintx); % the four corners | ||
24 | |||
25 | x = Xc(1,:)'; | ||
26 | y = Xc(2,:)'; | ||
27 | |||
28 | [y,indy] = sort(y); | ||
29 | x = x(indy); | ||
30 | |||
31 | if (x(2) > x(1)), | ||
32 | x4 = x(1);y4 = y(1); x3 = x(2); y3 = y(2); | ||
33 | else | ||
34 | x4 = x(2);y4 = y(2); x3 = x(1); y3 = y(1); | ||
35 | end; | ||
36 | if (x(3) > x(4)), | ||
37 | x2 = x(3);y2 = y(3); x1 = x(4); y1 = y(4); | ||
38 | else | ||
39 | x2 = x(4);y2 = y(4); x1 = x(3); y1 = y(3); | ||
40 | end; | ||
41 | |||
42 | x = [x1;x2;x3;x4]; | ||
43 | y = [y1;y2;y3;y4]; | ||
44 | |||
45 | |||
46 | figure(2); hold on; | ||
47 | plot([x;x(1)],[y;y(1)],'g-'); | ||
48 | plot(x,y,'og'); | ||
49 | hx=text((x(4)+x(3))/2,(y(4)+y(3))/2 - 20,'X'); | ||
50 | set(hx,'color','g','Fontsize',14); | ||
51 | hy=text((x(4)+x(1))/2-20,(y(4)+y(1))/2,'Y'); | ||
52 | set(hy,'color','g','Fontsize',14); | ||
53 | hold off; | ||
54 | |||
55 | drawnow; | ||
56 | |||
57 | |||
58 | % Try to automatically count the number of squares in the grid | ||
59 | |||
60 | n_sq_x1 = count_squares(I,x1,y1,x2,y2,wintx); | ||
61 | n_sq_x2 = count_squares(I,x3,y3,x4,y4,wintx); | ||
62 | n_sq_y1 = count_squares(I,x2,y2,x3,y3,wintx); | ||
63 | n_sq_y2 = count_squares(I,x4,y4,x1,y1,wintx); | ||
64 | |||
65 | |||
66 | |||
67 | % If could not count the number of squares, enter manually | ||
68 | |||
69 | if (n_sq_x1~=n_sq_x2)|(n_sq_y1~=n_sq_y2), | ||
70 | |||
71 | |||
72 | disp('Could not count the number of squares in the grid. Enter manually.'); | ||
73 | n_sq_x = input('Number of squares along the X direction ([]=10) = '); %6 | ||
74 | if isempty(n_sq_x), n_sq_x = 10; end; | ||
75 | n_sq_y = input('Number of squares along the Y direction ([]=10) = '); %6 | ||
76 | if isempty(n_sq_y), n_sq_y = 10; end; | ||
77 | |||
78 | else | ||
79 | |||
80 | n_sq_x = n_sq_x1; | ||
81 | n_sq_y = n_sq_y1; | ||
82 | |||
83 | end; | ||
84 | |||
85 | |||
86 | if 1, | ||
87 | % Enter the size of each square | ||
88 | |||
89 | dX = input(['Size dX of each square along the X direction ([]=' num2str(dX_default) 'cm) = ']); | ||
90 | dY = input(['Size dY of each square along the Y direction ([]=' num2str(dY_default) 'cm) = ']); | ||
91 | if isempty(dX), dX = dX_default; else dX_default = dX; end; | ||
92 | if isempty(dY), dY = dY_default; else dY_default = dY; end; | ||
93 | |||
94 | else | ||
95 | |||
96 | dX = 3; | ||
97 | dY = 3; | ||
98 | |||
99 | end; | ||
100 | |||
101 | |||
102 | % Compute the inside points through computation of the planar homography (collineation) | ||
103 | |||
104 | a00 = [x(1);y(1);1]; | ||
105 | a10 = [x(2);y(2);1]; | ||
106 | a11 = [x(3);y(3);1]; | ||
107 | a01 = [x(4);y(4);1]; | ||
108 | |||
109 | |||
110 | % Compute the planart collineation: (return the normalization matrice as well) | ||
111 | |||
112 | [Homo,Hnorm,inv_Hnorm] = compute_collineation (a00, a10, a11, a01); | ||
113 | |||
114 | |||
115 | % Build the grid using the planar collineation: | ||
116 | |||
117 | x_l = ((0:n_sq_x)'*ones(1,n_sq_y+1))/n_sq_x; | ||
118 | y_l = (ones(n_sq_x+1,1)*(0:n_sq_y))/n_sq_y; | ||
119 | pts = [x_l(:) y_l(:) ones((n_sq_x+1)*(n_sq_y+1),1)]'; | ||
120 | |||
121 | XX = Homo*pts; | ||
122 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
123 | |||
124 | |||
125 | % Complete size of the rectangle | ||
126 | |||
127 | W = n_sq_x*dX; | ||
128 | L = n_sq_y*dY; | ||
129 | |||
130 | |||
131 | |||
132 | if 1, | ||
133 | %%%%%%%%%%%%%%%%%%%%%%%% ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
134 | figure(2); | ||
135 | hold on; | ||
136 | plot(XX(1,:),XX(2,:),'r+'); | ||
137 | title('The red crosses should be close to the image corners'); | ||
138 | hold off; | ||
139 | |||
140 | disp('If the guessed grid corners (red crosses on the image) are not close to the actual corners,'); | ||
141 | disp('it is necessary to enter an initial guess for the radial distortion factor kc (useful for subpixel detection)'); | ||
142 | quest_distort = input('Need of an initial guess for distortion? ([]=no, other=yes) '); | ||
143 | |||
144 | quest_distort = ~isempty(quest_distort); | ||
145 | |||
146 | if quest_distort, | ||
147 | % Estimation of focal length: | ||
148 | c_g = [size(I,2);size(I,1)]/2 + .5; | ||
149 | f_g = Distor2Calib(0,[[x(1) x(2) x(4) x(3)] - c_g(1);[y(1) y(2) y(4) y(3)] - c_g(2)],1,1,4,W,L,[-W/2 W/2 W/2 -W/2;L/2 L/2 -L/2 -L/2; 0 0 0 0],100,1,1); | ||
150 | f_g = mean(f_g); | ||
151 | script_fit_distortion; | ||
152 | end; | ||
153 | %%%%%%%%%%%%%%%%%%%%% END ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
154 | end; | ||
155 | |||
156 | |||
157 | Np = (n_sq_x+1)*(n_sq_y+1); | ||
158 | |||
159 | disp('Corner extraction...'); | ||
160 | |||
161 | grid_pts = cornerfinder(XX,I,winty,wintx); %%% Finds the exact corners at every points! | ||
162 | |||
163 | %save all_corners x y grid_pts | ||
164 | |||
165 | grid_pts = grid_pts - 1; % subtract 1 to bring the origin to (0,0) instead of (1,1) in matlab (not necessary in C) | ||
166 | |||
167 | |||
168 | % Global Homography from plane to pixel coordinates: | ||
169 | |||
170 | H_total = [1 0 -1 ; 0 1 -1 ; 0 0 1]*Homo*[1 0 0;0 -1 1;0 0 1]*[1/W 0 0 ; 0 1/L 0; 0 0 1]; | ||
171 | % WARNING!!! the first matrix (on the left side) takes care of the transformation of the pixel cooredinates by -1 (previous line) | ||
172 | % If it is not done, then this matrix should not appear (in C) | ||
173 | H_total = H_total / H_total(3,3); | ||
174 | |||
175 | |||
176 | ind_corners = [1 n_sq_x+1 (n_sq_x+1)*n_sq_y+1 (n_sq_x+1)*(n_sq_y+1)]; % index of the 4 corners | ||
177 | ind_orig = (n_sq_x+1)*n_sq_y + 1; | ||
178 | xorig = grid_pts(1,ind_orig); | ||
179 | yorig = grid_pts(2,ind_orig); | ||
180 | dxpos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig+1)]'); | ||
181 | dypos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig-n_sq_x-1)]'); | ||
182 | |||
183 | |||
184 | x_box_kk = [grid_pts(1,:)-(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)-(wintx+.5);grid_pts(1,:)-(wintx+.5)]; | ||
185 | y_box_kk = [grid_pts(2,:)-(winty+.5);grid_pts(2,:)-(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)-(winty+.5)]; | ||
186 | |||
187 | |||
188 | figure(3); | ||
189 | image(I); colormap(map); hold on; | ||
190 | plot(grid_pts(1,:)+1,grid_pts(2,:)+1,'r+'); | ||
191 | plot(x_box_kk+1,y_box_kk+1,'-b'); | ||
192 | plot(grid_pts(1,ind_corners)+1,grid_pts(2,ind_corners)+1,'mo'); | ||
193 | plot(xorig+1,yorig+1,'*m'); | ||
194 | h = text(xorig-15,yorig-15,'O'); | ||
195 | set(h,'Color','m','FontSize',14); | ||
196 | h2 = text(dxpos(1)-10,dxpos(2)-10,'dX'); | ||
197 | set(h2,'Color','g','FontSize',14); | ||
198 | h3 = text(dypos(1)-25,dypos(2)-3,'dY'); | ||
199 | set(h3,'Color','g','FontSize',14); | ||
200 | xlabel('Xc (in camera frame)'); | ||
201 | ylabel('Yc (in camera frame)'); | ||
202 | title('Extracted corners'); | ||
203 | zoom on; | ||
204 | drawnow; | ||
205 | hold off; | ||
206 | |||
207 | |||
208 | Xi = reshape(([0:n_sq_x]*dX)'*ones(1,n_sq_y+1),Np,1)'; | ||
209 | Yi = reshape(ones(n_sq_x+1,1)*[n_sq_y:-1:0]*dY,Np,1)'; | ||
210 | Zi = zeros(1,Np); | ||
211 | |||
212 | Xgrid = [Xi;Yi;Zi]; | ||
213 | |||
214 | |||
215 | % All the point coordinates (on the image, and in 3D) - for global optimization: | ||
216 | |||
217 | x = grid_pts; | ||
218 | X = Xgrid; | ||
219 | |||
220 | |||
221 | % The left pannel info: | ||
222 | |||
223 | xl = x; | ||
224 | Xl = X; | ||
225 | nl_sq_x = n_sq_x; | ||
226 | nl_sq_y = n_sq_y; | ||
227 | Hl = H_total; | ||
228 | |||
229 | |||
230 | |||
231 | |||
232 | |||
233 | |||
234 | %%%%%%%%%%%%%%%%%%%%%%%%% RIGHT PATTERN ACQUISITION %%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
235 | |||
236 | |||
237 | x1 = a10(1)/a10(3); | ||
238 | x4 = a11(1)/a11(3); | ||
239 | |||
240 | y1 = a10(2)/a10(3); | ||
241 | y4 = a11(2)/a11(3); | ||
242 | |||
243 | |||
244 | figure(2); | ||
245 | hold on; | ||
246 | plot([x1 x4],[y1 y4],'c-'); | ||
247 | plot([x1 x4],[y1 y4],'co'); | ||
248 | hold off; | ||
249 | |||
250 | title(['Click on the two remaining extreme corners of the right rectangular pattern... Image ' num2str(kk)]); | ||
251 | |||
252 | disp('Click on the two remaining extreme corners of the right rectangular pattern...'); | ||
253 | |||
254 | [x,y] = ginput3(2); | ||
255 | |||
256 | [Xc,good,bad,type] = cornerfinder([x';y'],I,winty,wintx); % the four corners | ||
257 | |||
258 | x = Xc(1,:)'; | ||
259 | y = Xc(2,:)'; | ||
260 | |||
261 | [y,indy] = sort(y); | ||
262 | x = x(indy); | ||
263 | |||
264 | x2 = x(2); | ||
265 | x3 = x(1); | ||
266 | |||
267 | y2 = y(2); | ||
268 | y3 = y(1); | ||
269 | |||
270 | |||
271 | x = [x1;x2;x3;x4]; | ||
272 | y = [y1;y2;y3;y4]; | ||
273 | |||
274 | figure(2); hold on; | ||
275 | plot([x;x(1)],[y;y(1)],'c-'); | ||
276 | plot(x,y,'oc'); | ||
277 | hx=text((x(4)+x(3))/2,(y(4)+y(3))/2 - 20,'X'); | ||
278 | set(hx,'color','c','Fontsize',14); | ||
279 | hy=text((x(4)+x(1))/2-20,(y(4)+y(1))/2,'Y'); | ||
280 | set(hy,'color','c','Fontsize',14); | ||
281 | hold off; | ||
282 | drawnow; | ||
283 | |||
284 | |||
285 | % Try to automatically count the number of squares in the grid | ||
286 | |||
287 | n_sq_x1 = count_squares(I,x1,y1,x2,y2,wintx); | ||
288 | n_sq_x2 = count_squares(I,x3,y3,x4,y4,wintx); | ||
289 | n_sq_y1 = count_squares(I,x2,y2,x3,y3,wintx); | ||
290 | n_sq_y2 = count_squares(I,x4,y4,x1,y1,wintx); | ||
291 | |||
292 | |||
293 | |||
294 | % If could not count the number of squares, enter manually | ||
295 | |||
296 | if (n_sq_x1~=n_sq_x2)|(n_sq_y1~=n_sq_y2), | ||
297 | |||
298 | |||
299 | disp('Could not count the number of squares in the grid. Enter manually.'); | ||
300 | n_sq_x = input('Number of squares along the X direction ([]=10) = '); %6 | ||
301 | if isempty(n_sq_x), n_sq_x = 10; end; | ||
302 | n_sq_y = input('Number of squares along the Y direction ([]=10) = '); %6 | ||
303 | if isempty(n_sq_y), n_sq_y = 10; end; | ||
304 | |||
305 | else | ||
306 | |||
307 | n_sq_x = n_sq_x1; | ||
308 | n_sq_y = n_sq_y1; | ||
309 | |||
310 | end; | ||
311 | |||
312 | |||
313 | if 1, | ||
314 | % Enter the size of each square | ||
315 | |||
316 | dX = input(['Size dX of each square along the X direction ([]=' num2str(dX_default) 'cm) = ']); | ||
317 | dY = input(['Size dY of each square along the Y direction ([]=' num2str(dY_default) 'cm) = ']); | ||
318 | if isempty(dX), dX = dX_default; else dX_default = dX; end; | ||
319 | if isempty(dY), dY = dY_default; else dY_default = dY; end; | ||
320 | |||
321 | else | ||
322 | |||
323 | dX = 3; | ||
324 | dY = 3; | ||
325 | |||
326 | end; | ||
327 | |||
328 | |||
329 | % Compute the inside points through computation of the planar homography (collineation) | ||
330 | |||
331 | a00 = [x(1);y(1);1]; | ||
332 | a10 = [x(2);y(2);1]; | ||
333 | a11 = [x(3);y(3);1]; | ||
334 | a01 = [x(4);y(4);1]; | ||
335 | |||
336 | |||
337 | % Compute the planart collineation: (return the normalization matrice as well) | ||
338 | |||
339 | [Homo,Hnorm,inv_Hnorm] = compute_collineation (a00, a10, a11, a01); | ||
340 | |||
341 | |||
342 | % Build the grid using the planar collineation: | ||
343 | |||
344 | x_l = ((0:n_sq_x)'*ones(1,n_sq_y+1))/n_sq_x; | ||
345 | y_l = (ones(n_sq_x+1,1)*(0:n_sq_y))/n_sq_y; | ||
346 | pts = [x_l(:) y_l(:) ones((n_sq_x+1)*(n_sq_y+1),1)]'; | ||
347 | |||
348 | XX = Homo*pts; | ||
349 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
350 | |||
351 | |||
352 | % Complete size of the rectangle | ||
353 | |||
354 | W = n_sq_x*dX; | ||
355 | L = n_sq_y*dY; | ||
356 | |||
357 | |||
358 | |||
359 | if 1, | ||
360 | %%%%%%%%%%%%%%%%%%%%%%%% ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
361 | figure(2); | ||
362 | hold on; | ||
363 | plot(XX(1,:),XX(2,:),'r+'); | ||
364 | title('The red crosses should be close to the image corners'); | ||
365 | hold off; | ||
366 | |||
367 | disp('If the guessed grid corners (red crosses on the image) are not close to the actual corners,'); | ||
368 | disp('it is necessary to enter an initial guess for the radial distortion factor kc (useful for subpixel detection)'); | ||
369 | quest_distort = input('Need of an initial guess for distortion? ([]=no, other=yes) '); | ||
370 | |||
371 | quest_distort = ~isempty(quest_distort); | ||
372 | |||
373 | if quest_distort, | ||
374 | % Estimation of focal length: | ||
375 | c_g = [size(I,2);size(I,1)]/2 + .5; | ||
376 | f_g = Distor2Calib(0,[[x(1) x(2) x(4) x(3)] - c_g(1);[y(1) y(2) y(4) y(3)] - c_g(2)],1,1,4,W,L,[-W/2 W/2 W/2 -W/2;L/2 L/2 -L/2 -L/2; 0 0 0 0],100,1,1); | ||
377 | f_g = mean(f_g); | ||
378 | script_fit_distortion; | ||
379 | end; | ||
380 | %%%%%%%%%%%%%%%%%%%%% END ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
381 | end; | ||
382 | |||
383 | |||
384 | Np = (n_sq_x+1)*(n_sq_y+1); | ||
385 | |||
386 | disp('Corner extraction...'); | ||
387 | |||
388 | grid_pts = cornerfinder(XX,I,winty,wintx); %%% Finds the exact corners at every points! | ||
389 | |||
390 | %save all_corners x y grid_pts | ||
391 | |||
392 | grid_pts = grid_pts - 1; % subtract 1 to bring the origin to (0,0) instead of (1,1) in matlab (not necessary in C) | ||
393 | |||
394 | |||
395 | % Global Homography from plane to pixel coordinates: | ||
396 | |||
397 | H_total = [1 0 -1 ; 0 1 -1 ; 0 0 1]*Homo*[1 0 0;0 -1 1;0 0 1]*[1/W 0 0 ; 0 1/L 0; 0 0 1]; | ||
398 | % WARNING!!! the first matrix (on the left side) takes care of the transformation of the pixel cooredinates by -1 (previous line) | ||
399 | % If it is not done, then this matrix should not appear (in C) | ||
400 | H_total = H_total / H_total(3,3); | ||
401 | |||
402 | |||
403 | ind_corners = [1 n_sq_x+1 (n_sq_x+1)*n_sq_y+1 (n_sq_x+1)*(n_sq_y+1)]; % index of the 4 corners | ||
404 | ind_orig = (n_sq_x+1)*n_sq_y + 1; | ||
405 | xorig = grid_pts(1,ind_orig); | ||
406 | yorig = grid_pts(2,ind_orig); | ||
407 | dxpos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig+1)]'); | ||
408 | dypos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig-n_sq_x-1)]'); | ||
409 | |||
410 | |||
411 | x_box_kk = [grid_pts(1,:)-(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)-(wintx+.5);grid_pts(1,:)-(wintx+.5)]; | ||
412 | y_box_kk = [grid_pts(2,:)-(winty+.5);grid_pts(2,:)-(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)-(winty+.5)]; | ||
413 | |||
414 | |||
415 | figure(3); | ||
416 | hold on; | ||
417 | plot(grid_pts(1,:)+1,grid_pts(2,:)+1,'r+'); | ||
418 | plot(x_box_kk+1,y_box_kk+1,'-b'); | ||
419 | plot(grid_pts(1,ind_corners)+1,grid_pts(2,ind_corners)+1,'mo'); | ||
420 | plot(xorig+1,yorig+1,'*m'); | ||
421 | h = text(xorig-15,yorig-15,'O'); | ||
422 | set(h,'Color','m','FontSize',14); | ||
423 | h2 = text(dxpos(1)-10,dxpos(2)-10,'dX'); | ||
424 | set(h2,'Color','g','FontSize',14); | ||
425 | h3 = text(dypos(1)-25,dypos(2)-3,'dY'); | ||
426 | set(h3,'Color','g','FontSize',14); | ||
427 | xlabel('Xc (in camera frame)'); | ||
428 | ylabel('Yc (in camera frame)'); | ||
429 | title('Extracted corners'); | ||
430 | zoom on; | ||
431 | drawnow; | ||
432 | hold off; | ||
433 | |||
434 | |||
435 | Xi = reshape(([0:n_sq_x]*dX)'*ones(1,n_sq_y+1),Np,1)'; | ||
436 | Yi = reshape(ones(n_sq_x+1,1)*[n_sq_y:-1:0]*dY,Np,1)'; | ||
437 | Zi = zeros(1,Np); | ||
438 | |||
439 | Xgrid = [Xi;Yi;Zi]; | ||
440 | |||
441 | |||
442 | % All the point coordinates (on the image, and in 3D) - for global optimization: | ||
443 | |||
444 | x = grid_pts; | ||
445 | X = Xgrid; | ||
446 | |||
447 | |||
448 | % The right pannel info: | ||
449 | |||
450 | xr = x; | ||
451 | Xr = X; | ||
452 | nr_sq_x = n_sq_x; | ||
453 | nr_sq_y = n_sq_y; | ||
454 | Hr = H_total; | ||
455 | |||
456 | |||
457 | |||
458 | %%%%%%%% REGROUP THE LEFT AND RIHT PATTERNS %%%%%%%%%%%%% | ||
459 | |||
460 | |||
461 | Xr2 = [0 0 1;0 1 0;-1 0 0]*Xr + [dX*nl_sq_x;0;0]*ones(1,length(Xr)); | ||
462 | |||
463 | |||
464 | x = [xl xr]; | ||
465 | |||
466 | X = [Xl Xr2]; | ||
467 | |||
468 | |||
469 | |||
470 | eval(['x_' num2str(kk) ' = x;']); | ||
471 | eval(['X_' num2str(kk) ' = X;']); | ||
472 | |||
473 | eval(['nl_sq_x_' num2str(kk) ' = nl_sq_x;']); | ||
474 | eval(['nl_sq_y_' num2str(kk) ' = nl_sq_y;']); | ||
475 | |||
476 | eval(['nr_sq_x_' num2str(kk) ' = nr_sq_x;']); | ||
477 | eval(['nr_sq_y_' num2str(kk) ' = nr_sq_y;']); | ||
478 | |||
479 | % Save the global planar homography: | ||
480 | |||
481 | eval(['Hl_' num2str(kk) ' = Hl;']); | ||
482 | eval(['Hr_' num2str(kk) ' = Hr;']); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_distortion.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_distortion.m new file mode 100755 index 0000000..a0f03de --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_distortion.m | |||
@@ -0,0 +1,38 @@ | |||
1 | function [x_comp] = comp_distortion(x_dist,k2); | ||
2 | |||
3 | % [x_comp] = comp_distortion(x_dist,k2); | ||
4 | % | ||
5 | % compensates the radial distortion of the camera | ||
6 | % on the image plane. | ||
7 | % | ||
8 | % x_dist : the image points got without considering the | ||
9 | % radial distortion. | ||
10 | % x : The image plane points after correction for the distortion | ||
11 | % | ||
12 | % x and x_dist are 2xN arrays | ||
13 | % | ||
14 | % NOTE : This compensation has to be done after the substraction | ||
15 | % of the center of projection, and division by the focal | ||
16 | % length. | ||
17 | % | ||
18 | % (do it up to a second order approximation) | ||
19 | |||
20 | [two,N] = size(x_dist); | ||
21 | |||
22 | if (two ~= 2 ), | ||
23 | error('ERROR : The dimension of the points should be 2xN'); | ||
24 | end; | ||
25 | |||
26 | if length(k2) > 2, | ||
27 | [x_comp] = comp_distortion_oulu(x_dist,k2); | ||
28 | else | ||
29 | |||
30 | radius_2= x_dist(1,:).^2 + x_dist(2,:).^2; | ||
31 | radial_distortion = 1 + ones(2,1)*(k2 * radius_2); | ||
32 | radius_2_comp = (x_dist(1,:).^2 + x_dist(2,:).^2) ./ radial_distortion(1,:); | ||
33 | radial_distortion = 1 + ones(2,1)*(k2 * radius_2_comp); | ||
34 | x_comp = x_dist ./ radial_distortion; | ||
35 | |||
36 | end; | ||
37 | |||
38 | %% Function completely checked : It works fine !!! \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_distortion2.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_distortion2.m new file mode 100755 index 0000000..532ee9a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_distortion2.m | |||
@@ -0,0 +1,71 @@ | |||
1 | function [x_comp] = comp_distortion(x_dist,k2); | ||
2 | |||
3 | % [x_comp] = comp_distortion(x_dist,k2); | ||
4 | % | ||
5 | % compensates the radial distortion of the camera | ||
6 | % on the image plane. | ||
7 | % | ||
8 | % x_dist : the image points got without considering the | ||
9 | % radial distortion. | ||
10 | % k2: Radial distortion factor | ||
11 | % | ||
12 | % x : The image plane points after correction for the distortion | ||
13 | % | ||
14 | % x and x_dist are 2xN arrays | ||
15 | % | ||
16 | % NOTE : This compensation has to be done after the substraction | ||
17 | % of the center of projection, and division by the focal | ||
18 | % length. | ||
19 | % | ||
20 | % Solve for cubic roots using method from Numerical Recipes in C 2nd Ed. | ||
21 | % pages 184-185. | ||
22 | |||
23 | |||
24 | % California Institute of Technology | ||
25 | % (c) Jean-Yves Bouguet - April 27th, 1998 | ||
26 | |||
27 | % fully checked! JYB, april 27th, 1998 - 2am | ||
28 | |||
29 | if k2 ~= 0, | ||
30 | |||
31 | [two,N] = size(x_dist); | ||
32 | |||
33 | if (two ~= 2 ), | ||
34 | error('ERROR : The dimension of the points should be 2xN'); | ||
35 | end; | ||
36 | |||
37 | |||
38 | ph = atan2(x_dist(2,:),x_dist(1,:)); | ||
39 | |||
40 | Q = -1/(3*k2); | ||
41 | R = -x_dist(1,:)./(2*k2*cos(ph)); | ||
42 | |||
43 | R2 = R.^2; | ||
44 | Q3 = Q^3; | ||
45 | |||
46 | |||
47 | if k2 < 0, | ||
48 | |||
49 | % this works in all practical situations (it starts failing for very large | ||
50 | % values of k2) | ||
51 | |||
52 | th = acos(R./sqrt(Q3)); | ||
53 | r = -2*sqrt(Q)*cos((th-2*pi)/3); | ||
54 | |||
55 | else | ||
56 | |||
57 | % note: this always works, even for ridiculous values of k2 | ||
58 | |||
59 | A = (sqrt(R2-Q3)-R).^(1/3); | ||
60 | B = Q*(1./A); | ||
61 | r = (A+B); | ||
62 | |||
63 | end; | ||
64 | |||
65 | x_comp = [r.*cos(ph); r.*sin(ph)]; | ||
66 | |||
67 | else | ||
68 | |||
69 | x_comp = x_dist; | ||
70 | |||
71 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_distortion_oulu.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_distortion_oulu.m new file mode 100755 index 0000000..67d02d5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_distortion_oulu.m | |||
@@ -0,0 +1,47 @@ | |||
1 | function [x] = comp_distortion_oulu(xd,k); | ||
2 | |||
3 | %comp_distortion_oulu.m | ||
4 | % | ||
5 | %[x] = comp_distortion_oulu(xd,k) | ||
6 | % | ||
7 | %Compensates for radial and tangential distortion. Model From Oulu university. | ||
8 | %For more informatino about the distortion model, check the forward projection mapping function: | ||
9 | %project_points.m | ||
10 | % | ||
11 | %INPUT: xd: distorted (normalized) point coordinates in the image plane (2xN matrix) | ||
12 | % k: Distortion coefficients (radial and tangential) (4x1 vector) | ||
13 | % | ||
14 | %OUTPUT: x: undistorted (normalized) point coordinates in the image plane (2xN matrix) | ||
15 | % | ||
16 | %Method: Iterative method for compensation. | ||
17 | % | ||
18 | %NOTE: This compensation has to be done after the subtraction | ||
19 | % of the principal point, and division by the focal length. | ||
20 | |||
21 | |||
22 | if length(k) < 4, | ||
23 | |||
24 | [x] = comp_distortion(xd,k); | ||
25 | |||
26 | else | ||
27 | |||
28 | |||
29 | k1 = k(1); | ||
30 | k2 = k(2); | ||
31 | p1 = k(3); | ||
32 | p2 = k(4); | ||
33 | |||
34 | x = xd; % initial guess | ||
35 | |||
36 | for kk=1:5; | ||
37 | |||
38 | r_2 = sum(x.^2); | ||
39 | k_radial = 1 + k1 * r_2 + k2 * r_2.^2; | ||
40 | delta_x = [2*p1*x(1,:).*x(2,:) + p2*(r_2 + 2*x(1,:).^2) ; | ||
41 | p1 * (r_2 + 2*x(2,:).^2)+2*p2*x(1,:).*x(2,:)]; | ||
42 | x = (xd - delta_x)./(ones(2,1)*k_radial); | ||
43 | |||
44 | end; | ||
45 | |||
46 | end; | ||
47 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_error_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_error_calib.m new file mode 100755 index 0000000..f8d6fde --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/comp_error_calib.m | |||
@@ -0,0 +1,40 @@ | |||
1 | %%%%%%%%%%%%%%%%%%%% RECOMPUTES THE REPROJECTION ERROR %%%%%%%%%%%%%%%%%%%%%%%% | ||
2 | |||
3 | check_active_images; | ||
4 | |||
5 | % Reproject the patterns on the images, and compute the pixel errors: | ||
6 | |||
7 | ex = []; % Global error vector | ||
8 | x = []; % Detected corners on the image plane | ||
9 | y = []; % Reprojected points | ||
10 | |||
11 | for kk = 1:n_ima, | ||
12 | |||
13 | eval(['omckk = omc_' num2str(kk) ';']); | ||
14 | eval(['Tckk = Tc_' num2str(kk) ';']); | ||
15 | |||
16 | if active_images(kk) & (~isnan(omckk(1,1))), | ||
17 | |||
18 | Rkk = rodrigues(omckk); | ||
19 | |||
20 | eval(['y_' num2str(kk) ' = project2_oulu(X_' num2str(kk) ',Rkk,Tckk,fc,cc,kc);']); | ||
21 | |||
22 | eval(['ex_' num2str(kk) ' = x_' num2str(kk) ' -y_' num2str(kk) ';']); | ||
23 | |||
24 | eval(['x_kk = x_' num2str(kk) ';']); | ||
25 | |||
26 | eval(['ex = [ex ex_' num2str(kk) '];']); | ||
27 | eval(['x = [x x_' num2str(kk) '];']); | ||
28 | eval(['y = [y y_' num2str(kk) '];']); | ||
29 | |||
30 | else | ||
31 | |||
32 | eval(['y_' num2str(kk) ' = NaN*ones(2,1);']); | ||
33 | |||
34 | eval(['ex_' num2str(kk) ' = NaN*ones(2,1);']); | ||
35 | |||
36 | end; | ||
37 | |||
38 | end; | ||
39 | |||
40 | err_std = std(ex')'; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_collineation.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_collineation.m new file mode 100755 index 0000000..809c309 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_collineation.m | |||
@@ -0,0 +1,66 @@ | |||
1 | function [H,Hnorm,inv_Hnorm] = compute_collineation (a00, a10, a11, a01); | ||
2 | |||
3 | % new formalism using homographies | ||
4 | |||
5 | a00 = a00 / a00(3); | ||
6 | a10 = a10 / a10(3); | ||
7 | a11 = a11 / a11(3); | ||
8 | a01 = a01 / a01(3); | ||
9 | |||
10 | |||
11 | % Prenormalization of point coordinates (very important): | ||
12 | % (Affine normalization) | ||
13 | |||
14 | ax = [a00(1);a10(1);a11(1);a01(1)]; | ||
15 | ay = [a00(2);a10(2);a11(2);a01(2)]; | ||
16 | |||
17 | mxx = mean(ax); | ||
18 | myy = mean(ay); | ||
19 | ax = ax - mxx; | ||
20 | ay = ay - myy; | ||
21 | |||
22 | scxx = mean(abs(ax)); | ||
23 | scyy = mean(abs(ay)); | ||
24 | |||
25 | |||
26 | Hnorm = [1/scxx 0 -mxx/scxx;0 1/scyy -myy/scyy;0 0 1]; | ||
27 | inv_Hnorm = [scxx 0 mxx ; 0 scyy myy; 0 0 1]; | ||
28 | |||
29 | |||
30 | a00n = Hnorm*a00; | ||
31 | a10n = Hnorm*a10; | ||
32 | a11n = Hnorm*a11; | ||
33 | a01n = Hnorm*a01; | ||
34 | |||
35 | |||
36 | % Computation of the vanishing points: | ||
37 | |||
38 | V1n = cross(cross(a00n,a10n),cross(a01n,a11n)); | ||
39 | V2n = cross(cross(a00n,a01n),cross(a10n,a11n)); | ||
40 | |||
41 | V1 = inv_Hnorm*V1n; | ||
42 | V2 = inv_Hnorm*V2n; | ||
43 | |||
44 | |||
45 | % Normalizaion of the vanishing points: | ||
46 | |||
47 | V1n = V1n/norm(V1n); | ||
48 | V2n = V2n/norm(V2n); | ||
49 | |||
50 | |||
51 | % Closed-form solution of the coefficients: | ||
52 | |||
53 | alpha_x = (a10n(2)*a00n(1) - a10n(1)*a00n(2))/(V1n(2)*a10n(1)-V1n(1)*a10n(2)); | ||
54 | |||
55 | alpha_y = (a01n(2)*a00n(1) - a01n(1)*a00n(2))/(V2n(2)*a01n(1)-V2n(1)*a01n(2)); | ||
56 | |||
57 | |||
58 | % Remaining Homography | ||
59 | |||
60 | Hrem = [alpha_x*V1n alpha_y*V2n a00n]; | ||
61 | |||
62 | |||
63 | % Final homography: | ||
64 | |||
65 | H = inv_Hnorm*Hrem; | ||
66 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_extrinsic.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_extrinsic.m new file mode 100755 index 0000000..4b4d7dd --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_extrinsic.m | |||
@@ -0,0 +1,123 @@ | |||
1 | function [omckk,Tckk,Rckk,H,x,ex,JJ] = compute_extrinsic(x_kk,X_kk,fc,cc,kc,MaxIter,thresh_cond), | ||
2 | |||
3 | %compute_extrinsic | ||
4 | % | ||
5 | %[omckk,Tckk,Rckk,H,x,ex] = compute_extrinsic(x_kk,X_kk,fc,cc,kc,refine) | ||
6 | % | ||
7 | %Computes the extrinsic parameters attached to a 3D structure X_kk given its projection | ||
8 | %on the image plane x_kk and the intrinsic camera parameters fc, cc and kc. | ||
9 | %Works with planar and non-planar structures. | ||
10 | % | ||
11 | %INPUT: x_kk: Feature locations on the images | ||
12 | % X_kk: Corresponding grid coordinates | ||
13 | % fc: Camera focal length | ||
14 | % cc: Principal point coordinates | ||
15 | % kc: Distortion coefficients | ||
16 | % refine: set to 1 for refining the extrinsic parameters iteratively | ||
17 | % [OPTIONAL: Default value: 1] | ||
18 | % | ||
19 | %OUTPUT: omckk: 3D rotation vector attached to the grid positions in space | ||
20 | % Tckk: 3D translation vector attached to the grid positions in space | ||
21 | % Rckk: 3D rotation matrices corresponding to the omc vectors | ||
22 | % H: Homography between points on the grid and points on the image plane (in pixel) | ||
23 | % This makes sense only if the planar that is used in planar. | ||
24 | % x: Reprojections of the points on the image plane | ||
25 | % ex: Reprojection error: ex = x_kk - x; | ||
26 | % | ||
27 | %Method: Computes the normalized point coordinates, then computes the 3D pose | ||
28 | % | ||
29 | %Important functions called within that program: | ||
30 | % | ||
31 | %normalize: Computes the normalize image point coordinates. | ||
32 | % | ||
33 | %pose3D: Computes the 3D pose of the structure given the normalized image projection. | ||
34 | % | ||
35 | %project_points.m: Computes the 2D image projections of a set of 3D points | ||
36 | |||
37 | |||
38 | |||
39 | if nargin < 7, | ||
40 | thresh_cond = inf; | ||
41 | end; | ||
42 | |||
43 | |||
44 | if nargin < 6, | ||
45 | MaxIter = 20; | ||
46 | end; | ||
47 | |||
48 | |||
49 | |||
50 | if nargin < 5, | ||
51 | kc = zeros(4,1); | ||
52 | if nargin < 4, | ||
53 | cc = zeros(2,1); | ||
54 | if nargin < 3, | ||
55 | fc = ones(2,1); | ||
56 | if nargin < 2, | ||
57 | error('Need 2D projections and 3D points (in compute_extrinsic.m)'); | ||
58 | return; | ||
59 | end; | ||
60 | end; | ||
61 | end; | ||
62 | end; | ||
63 | |||
64 | |||
65 | % Initialization: | ||
66 | |||
67 | [omckk,Tckk,Rckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc); | ||
68 | |||
69 | % Refinement: | ||
70 | |||
71 | [omckk,Tckk,Rckk,JJ] = compute_extrinsic_refine(omckk,Tckk,x_kk,X_kk,fc,cc,kc,MaxIter,thresh_cond); | ||
72 | |||
73 | |||
74 | % computation of the homography (not useful in the end) | ||
75 | |||
76 | H = [Rckk(:,1:2) Tckk]; | ||
77 | |||
78 | % Computes the reprojection error in pixels: | ||
79 | |||
80 | x = project_points(X_kk,omckk,Tckk,fc,cc,kc); | ||
81 | |||
82 | ex = x_kk - x; | ||
83 | |||
84 | |||
85 | % Converts the homography in pixel units: | ||
86 | |||
87 | KK = [fc(1) 0 cc(1);0 fc(2) cc(2); 0 0 1]; | ||
88 | |||
89 | H = KK*H; | ||
90 | |||
91 | |||
92 | |||
93 | |||
94 | return; | ||
95 | |||
96 | |||
97 | % Test of compte extrinsic: | ||
98 | |||
99 | Np = 4; | ||
100 | sx = 10; | ||
101 | sy = 10; | ||
102 | sz = 5; | ||
103 | |||
104 | om = randn(3,1); | ||
105 | T = [0;0;100]; | ||
106 | |||
107 | noise = 2/1000; | ||
108 | |||
109 | XX = [sx*randn(1,Np);sy*randn(1,Np);sz*randn(1,Np)]; | ||
110 | xx = project_points(XX,om,T); | ||
111 | |||
112 | xxn = xx + noise * randn(2,Np); | ||
113 | |||
114 | [omckk,Tckk] = compute_extrinsic(xxn,XX); | ||
115 | |||
116 | [om omckk om-omckk] | ||
117 | [T Tckk T-Tckk] | ||
118 | |||
119 | figure(3); | ||
120 | plot(xx(1,:),xx(2,:),'r+'); | ||
121 | hold on; | ||
122 | plot(xxn(1,:),xxn(2,:),'g+'); | ||
123 | hold off; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_extrinsic_init.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_extrinsic_init.m new file mode 100755 index 0000000..207ea30 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_extrinsic_init.m | |||
@@ -0,0 +1,149 @@ | |||
1 | function [omckk,Tckk,Rckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc), | ||
2 | |||
3 | %compute_extrinsic | ||
4 | % | ||
5 | %[omckk,Tckk,Rckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc) | ||
6 | % | ||
7 | %Computes the extrinsic parameters attached to a 3D structure X_kk given its projection | ||
8 | %on the image plane x_kk and the intrinsic camera parameters fc, cc and kc. | ||
9 | %Works with planar and non-planar structures. | ||
10 | % | ||
11 | %INPUT: x_kk: Feature locations on the images | ||
12 | % X_kk: Corresponding grid coordinates | ||
13 | % fc: Camera focal length | ||
14 | % cc: Principal point coordinates | ||
15 | % kc: Distortion coefficients | ||
16 | % | ||
17 | %OUTPUT: omckk: 3D rotation vector attached to the grid positions in space | ||
18 | % Tckk: 3D translation vector attached to the grid positions in space | ||
19 | % Rckk: 3D rotation matrices corresponding to the omc vectors | ||
20 | % | ||
21 | %Method: Computes the normalized point coordinates, then computes the 3D pose | ||
22 | % | ||
23 | %Important functions called within that program: | ||
24 | % | ||
25 | %normalize: Computes the normalize image point coordinates. | ||
26 | % | ||
27 | %pose3D: Computes the 3D pose of the structure given the normalized image projection. | ||
28 | % | ||
29 | %project_points.m: Computes the 2D image projections of a set of 3D points | ||
30 | |||
31 | |||
32 | |||
33 | |||
34 | if nargin < 5, | ||
35 | kc = zeros(4,1); | ||
36 | if nargin < 4, | ||
37 | cc = zeros(2,1); | ||
38 | if nargin < 3, | ||
39 | fc = ones(2,1); | ||
40 | if nargin < 2, | ||
41 | error('Need 2D projections and 3D points (in compute_extrinsic.m)'); | ||
42 | return; | ||
43 | end; | ||
44 | end; | ||
45 | end; | ||
46 | end; | ||
47 | |||
48 | |||
49 | |||
50 | % Compute the normalized coordinates: | ||
51 | |||
52 | xn = normalize(x_kk,fc,cc,kc); | ||
53 | |||
54 | |||
55 | |||
56 | Np = size(xn,2); | ||
57 | |||
58 | %% Check for planarity of the structure: | ||
59 | |||
60 | X_mean = mean(X_kk')'; | ||
61 | |||
62 | Y = X_kk - (X_mean*ones(1,Np)); | ||
63 | |||
64 | YY = Y*Y'; | ||
65 | |||
66 | [U,S,V] = svd(YY); | ||
67 | |||
68 | r = S(3,3)/S(2,2); | ||
69 | |||
70 | if (r < 1e-3)|(Np < 6), %1e-3, %1e-4, %norm(X_kk(3,:)) < eps, % Test of planarity | ||
71 | |||
72 | %fprintf(1,'Planar structure detected: r=%f\n',r); | ||
73 | |||
74 | % Transform the plane to bring it in the Z=0 plane: | ||
75 | |||
76 | R_transform = V'; | ||
77 | |||
78 | if det(R_transform) < 0, R_transform = -R_transform; end; | ||
79 | |||
80 | T_transform = -(R_transform)*X_mean; | ||
81 | |||
82 | X_new = R_transform*X_kk + T_transform*ones(1,Np); | ||
83 | |||
84 | |||
85 | % Compute the planar homography: | ||
86 | |||
87 | H = compute_homography (xn,X_new(1:2,:)); | ||
88 | |||
89 | % De-embed the motion parameters from the homography: | ||
90 | |||
91 | sc = mean([norm(H(:,1));norm(H(:,2))]); | ||
92 | |||
93 | H = H/sc; | ||
94 | |||
95 | omckk = rodrigues([H(:,1:2) cross(H(:,1),H(:,2))]); | ||
96 | Rckk = rodrigues(omckk); | ||
97 | Tckk = H(:,3); | ||
98 | |||
99 | %If Xc = Rckk * X_new + Tckk, then Xc = Rckk * R_transform * X_kk + Tckk + T_transform | ||
100 | |||
101 | Tckk = Tckk + Rckk* T_transform; | ||
102 | Rckk = Rckk * R_transform; | ||
103 | |||
104 | omckk = rodrigues(Rckk); | ||
105 | Rckk = rodrigues(omckk); | ||
106 | |||
107 | |||
108 | else | ||
109 | |||
110 | %fprintf(1,'Non planar structure detected: r=%f\n',r); | ||
111 | |||
112 | % Computes an initial guess for extrinsic parameters (works for general 3d structure, not planar!!!): | ||
113 | % The DLT method is applied here!! | ||
114 | |||
115 | J = zeros(2*Np,12); | ||
116 | |||
117 | xX = (ones(3,1)*xn(1,:)).*X_kk; | ||
118 | yX = (ones(3,1)*xn(2,:)).*X_kk; | ||
119 | |||
120 | J(1:2:end,[1 4 7]) = -X_kk'; | ||
121 | J(2:2:end,[2 5 8]) = X_kk'; | ||
122 | J(1:2:end,[3 6 9]) = xX'; | ||
123 | J(2:2:end,[3 6 9]) = -yX'; | ||
124 | J(1:2:end,12) = xn(1,:)'; | ||
125 | J(2:2:end,12) = -xn(2,:)'; | ||
126 | J(1:2:end,10) = -ones(Np,1); | ||
127 | J(2:2:end,11) = ones(Np,1); | ||
128 | |||
129 | JJ = J'*J; | ||
130 | [U,S,V] = svd(JJ); | ||
131 | |||
132 | RR = reshape(V(1:9,12),3,3); | ||
133 | |||
134 | if det(RR) < 0, | ||
135 | V(:,12) = -V(:,12); | ||
136 | RR = -RR; | ||
137 | end; | ||
138 | |||
139 | [Ur,Sr,Vr] = svd(RR); | ||
140 | |||
141 | Rckk = Ur*Vr'; | ||
142 | |||
143 | sc = norm(V(1:9,12)) / norm(Rckk(:)); | ||
144 | Tckk = V(10:12,12)/sc; | ||
145 | |||
146 | omckk = rodrigues(Rckk); | ||
147 | Rckk = rodrigues(omckk); | ||
148 | |||
149 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_extrinsic_refine.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_extrinsic_refine.m new file mode 100755 index 0000000..69474c4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_extrinsic_refine.m | |||
@@ -0,0 +1,110 @@ | |||
1 | function [omckk,Tckk,Rckk,JJ] = compute_extrinsic_refine(omc_init,Tc_init,x_kk,X_kk,fc,cc,kc,MaxIter,thresh_cond), | ||
2 | |||
3 | %compute_extrinsic | ||
4 | % | ||
5 | %[omckk,Tckk,Rckk] = compute_extrinsic_refine(x_kk,X_kk,fc,cc,kc,MaxIter) | ||
6 | % | ||
7 | %Computes the extrinsic parameters attached to a 3D structure X_kk given its projection | ||
8 | %on the image plane x_kk and the intrinsic camera parameters fc, cc and kc. | ||
9 | %Works with planar and non-planar structures. | ||
10 | % | ||
11 | %INPUT: x_kk: Feature locations on the images | ||
12 | % X_kk: Corresponding grid coordinates | ||
13 | % fc: Camera focal length | ||
14 | % cc: Principal point coordinates | ||
15 | % kc: Distortion coefficients | ||
16 | % MaxIter: Maximum number of iterations | ||
17 | % | ||
18 | %OUTPUT: omckk: 3D rotation vector attached to the grid positions in space | ||
19 | % Tckk: 3D translation vector attached to the grid positions in space | ||
20 | % Rckk: 3D rotation matrices corresponding to the omc vectors | ||
21 | |||
22 | % | ||
23 | %Method: Computes the normalized point coordinates, then computes the 3D pose | ||
24 | % | ||
25 | %Important functions called within that program: | ||
26 | % | ||
27 | %normalize: Computes the normalize image point coordinates. | ||
28 | % | ||
29 | %pose3D: Computes the 3D pose of the structure given the normalized image projection. | ||
30 | % | ||
31 | %project_points.m: Computes the 2D image projections of a set of 3D points | ||
32 | |||
33 | |||
34 | if nargin < 9, | ||
35 | thresh_cond = inf; | ||
36 | end; | ||
37 | |||
38 | |||
39 | if nargin < 8, | ||
40 | MaxIter = 20; | ||
41 | end; | ||
42 | |||
43 | |||
44 | if nargin < 7, | ||
45 | kc = zeros(4,1); | ||
46 | if nargin < 6, | ||
47 | cc = zeros(2,1); | ||
48 | if nargin < 5, | ||
49 | fc = ones(2,1); | ||
50 | if nargin < 4, | ||
51 | error('Need 2D projections and 3D points (in compute_extrinsic_refine.m)'); | ||
52 | return; | ||
53 | end; | ||
54 | end; | ||
55 | end; | ||
56 | end; | ||
57 | |||
58 | |||
59 | % Initialization: | ||
60 | |||
61 | omckk = omc_init; | ||
62 | Tckk = Tc_init; | ||
63 | |||
64 | |||
65 | % Final optimization (minimize the reprojection error in pixel): | ||
66 | % through Gradient Descent: | ||
67 | |||
68 | param = [omckk;Tckk]; | ||
69 | |||
70 | change = 1; | ||
71 | |||
72 | iter = 0; | ||
73 | |||
74 | %keyboard; | ||
75 | |||
76 | %fprintf(1,'Gradient descent iterations: '); | ||
77 | |||
78 | while (change > 1e-10)&(iter < MaxIter), | ||
79 | |||
80 | %fprintf(1,'%d...',iter+1); | ||
81 | |||
82 | [x,dxdom,dxdT] = project_points(X_kk,omckk,Tckk,fc,cc,kc); | ||
83 | |||
84 | ex = x_kk - x; | ||
85 | |||
86 | %keyboard; | ||
87 | |||
88 | JJ = [dxdom dxdT]; | ||
89 | |||
90 | if cond(JJ) > thresh_cond, | ||
91 | change = 0; | ||
92 | else | ||
93 | |||
94 | JJ2 = JJ'*JJ; | ||
95 | |||
96 | param_innov = inv(JJ2)*(JJ')*ex(:); | ||
97 | param_up = param + param_innov; | ||
98 | change = norm(param_innov)/norm(param_up); | ||
99 | param = param_up; | ||
100 | iter = iter + 1; | ||
101 | |||
102 | omckk = param(1:3); | ||
103 | Tckk = param(4:6); | ||
104 | end; | ||
105 | |||
106 | end; | ||
107 | |||
108 | %fprintf(1,'\n'); | ||
109 | |||
110 | Rckk = rodrigues(omckk); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_homography.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_homography.m new file mode 100755 index 0000000..fcc9003 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/compute_homography.m | |||
@@ -0,0 +1,163 @@ | |||
1 | function [H,Hnorm,inv_Hnorm] = compute_homography (m,M); | ||
2 | |||
3 | %compute_homography | ||
4 | % | ||
5 | %[H,Hnorm,inv_Hnorm] = compute_homography (m,M) | ||
6 | % | ||
7 | %Computes the planar homography between the point coordinates on the plane (M) and the image | ||
8 | %point coordinates (m). | ||
9 | % | ||
10 | %INPUT: m: homogeneous coordinates in the image plane (3xN matrix) | ||
11 | % M: homogeneous coordinates in the plane in 3D (3xN matrix) | ||
12 | % | ||
13 | %OUTPUT: H: Homography matrix (3x3 homogeneous matrix) | ||
14 | % Hnorm: Normlization matrix used on the points before homography computation | ||
15 | % (useful for numerical stability is points in pixel coordinates) | ||
16 | % inv_Hnorm: The inverse of Hnorm | ||
17 | % | ||
18 | %Definition: m ~ H*M where "~" means equal up to a non zero scalar factor. | ||
19 | % | ||
20 | %Method: First computes an initial guess for the homography through quasi-linear method. | ||
21 | % Then, if the total number of points is larger than 4, optimize the solution by minimizing | ||
22 | % the reprojection error (in the least squares sense). | ||
23 | % | ||
24 | % | ||
25 | %Important functions called within that program: | ||
26 | % | ||
27 | %comp_distortion_oulu: Undistorts pixel coordinates. | ||
28 | % | ||
29 | %compute_homography.m: Computes the planar homography between points on the grid in 3D, and the image plane. | ||
30 | % | ||
31 | %project_points.m: Computes the 2D image projections of a set of 3D points, and also returns te Jacobian | ||
32 | % matrix (derivative with respect to the intrinsic and extrinsic parameters). | ||
33 | % This function is called within the minimization loop. | ||
34 | |||
35 | |||
36 | |||
37 | |||
38 | Np = size(m,2); | ||
39 | |||
40 | if size(m,1)<3, | ||
41 | m = [m;ones(1,Np)]; | ||
42 | end; | ||
43 | |||
44 | if size(M,1)<3, | ||
45 | M = [M;ones(1,Np)]; | ||
46 | end; | ||
47 | |||
48 | |||
49 | m = m ./ (ones(3,1)*m(3,:)); | ||
50 | M = M ./ (ones(3,1)*M(3,:)); | ||
51 | |||
52 | % Prenormalization of point coordinates (very important): | ||
53 | % (Affine normalization) | ||
54 | |||
55 | ax = m(1,:); | ||
56 | ay = m(2,:); | ||
57 | |||
58 | mxx = mean(ax); | ||
59 | myy = mean(ay); | ||
60 | ax = ax - mxx; | ||
61 | ay = ay - myy; | ||
62 | |||
63 | scxx = mean(abs(ax)); | ||
64 | scyy = mean(abs(ay)); | ||
65 | |||
66 | |||
67 | Hnorm = [1/scxx 0 -mxx/scxx;0 1/scyy -myy/scyy;0 0 1]; | ||
68 | inv_Hnorm = [scxx 0 mxx ; 0 scyy myy; 0 0 1]; | ||
69 | |||
70 | mn = Hnorm*m; | ||
71 | |||
72 | % Compute the homography between m and mn: | ||
73 | |||
74 | % Build the matrix: | ||
75 | |||
76 | L = zeros(2*Np,9); | ||
77 | |||
78 | L(1:2:2*Np,1:3) = M'; | ||
79 | L(2:2:2*Np,4:6) = M'; | ||
80 | L(1:2:2*Np,7:9) = -((ones(3,1)*mn(1,:)).* M)'; | ||
81 | L(2:2:2*Np,7:9) = -((ones(3,1)*mn(2,:)).* M)'; | ||
82 | |||
83 | if Np > 4, | ||
84 | L = L'*L; | ||
85 | end; | ||
86 | |||
87 | [U,S,V] = svd(L); | ||
88 | |||
89 | hh = V(:,9); | ||
90 | hh = hh/hh(9); | ||
91 | |||
92 | Hrem = reshape(hh,3,3)'; | ||
93 | %Hrem = Hrem / Hrem(3,3); | ||
94 | |||
95 | % Final homography: | ||
96 | |||
97 | H = inv_Hnorm*Hrem; | ||
98 | |||
99 | |||
100 | %%% Homography refinement if there are more than 4 points: | ||
101 | |||
102 | if Np > 4, | ||
103 | |||
104 | % Final refinement: | ||
105 | |||
106 | hhv = reshape(H',9,1); | ||
107 | hhv = hhv(1:8); | ||
108 | |||
109 | for iter=1:10, | ||
110 | |||
111 | mrep = H * M; | ||
112 | |||
113 | J = zeros(2*Np,8); | ||
114 | |||
115 | MMM = (M ./ (ones(3,1)*mrep(3,:))); | ||
116 | |||
117 | J(1:2:2*Np,1:3) = -MMM'; | ||
118 | J(2:2:2*Np,4:6) = -MMM'; | ||
119 | |||
120 | mrep = mrep ./ (ones(3,1)*mrep(3,:)); | ||
121 | |||
122 | m_err = m(1:2,:) - mrep(1:2,:); | ||
123 | m_err = m_err(:); | ||
124 | |||
125 | MMM2 = (ones(3,1)*mrep(1,:)) .* MMM; | ||
126 | MMM3 = (ones(3,1)*mrep(2,:)) .* MMM; | ||
127 | |||
128 | J(1:2:2*Np,7:8) = MMM2(1:2,:)'; | ||
129 | J(2:2:2*Np,7:8) = MMM3(1:2,:)'; | ||
130 | |||
131 | MMM = (M ./ (ones(3,1)*mrep(3,:)))'; | ||
132 | |||
133 | hh_innov = inv(J'*J)*J'*m_err; | ||
134 | |||
135 | hhv_up = hhv - hh_innov; | ||
136 | |||
137 | H_up = reshape([hhv_up;1],3,3)'; | ||
138 | |||
139 | %norm(m_err) | ||
140 | %norm(hh_innov) | ||
141 | |||
142 | hhv = hhv_up; | ||
143 | H = H_up; | ||
144 | |||
145 | end; | ||
146 | |||
147 | end; | ||
148 | |||
149 | |||
150 | |||
151 | |||
152 | |||
153 | return; | ||
154 | |||
155 | %test of Jacobian | ||
156 | |||
157 | mrep = H*M; | ||
158 | mrep = mrep ./ (ones(3,1)*mrep(3,:)); | ||
159 | |||
160 | m_err = mrep(1:2,:) - m(1:2,:); | ||
161 | figure(8); | ||
162 | plot(m_err(1,:),m_err(2,:),'r+'); | ||
163 | std(m_err') | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/convert_oulu.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/convert_oulu.m new file mode 100755 index 0000000..726806e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/convert_oulu.m | |||
@@ -0,0 +1,35 @@ | |||
1 | %% Converts data file from oulu to mine: | ||
2 | |||
3 | load cademo, | ||
4 | n_ima = 0; | ||
5 | |||
6 | no_error = 1; | ||
7 | |||
8 | ii = 1; | ||
9 | |||
10 | while no_error, | ||
11 | |||
12 | dataname = ['data' num2str(ii)]; | ||
13 | |||
14 | if exist(dataname), | ||
15 | |||
16 | n_ima = n_ima +1; | ||
17 | |||
18 | eval(['x_' num2str(ii) '= ' dataname '(:,4:5)'';']) | ||
19 | eval(['X_' num2str(ii) '= ' dataname '(:,1:3)'';']) | ||
20 | |||
21 | else | ||
22 | no_error = 0; | ||
23 | end; | ||
24 | |||
25 | ii = ii + 1; | ||
26 | |||
27 | end; | ||
28 | |||
29 | nx = 500; | ||
30 | ny = 500; | ||
31 | |||
32 | no_image = 1; | ||
33 | no_grid = 1; | ||
34 | |||
35 | save data n_ima x_1 X_1 x_2 X_2 x_3 X_3 nx ny no_image no_grid | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/cornerfinder.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/cornerfinder.m new file mode 100755 index 0000000..9bfa51f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/cornerfinder.m | |||
@@ -0,0 +1,215 @@ | |||
1 | function [xc,good,bad,type] = cornerfinder(xt,I,wintx,winty,wx2,wy2); | ||
2 | |||
3 | %[xc] = cornerfinder(xt,I); | ||
4 | % | ||
5 | %Finds the sub-pixel corners on the image I with initial guess xt | ||
6 | %xt and xc are 2xN matrices. The first component is the x coordinate | ||
7 | %(horizontal) and the second component is the y coordinate (vertical) | ||
8 | % | ||
9 | %Based on Harris corner finder method | ||
10 | % | ||
11 | %Finds corners to a precision below .1 pixel! | ||
12 | %Oct. 14th, 1997 - UPDATED to work with vertical and horizontal edges as well!!! | ||
13 | %Sept 1998 - UPDATED to handle diverged points: we keep the original points | ||
14 | %good is a binary vector indicating wether a feature point has been properly | ||
15 | %found. | ||
16 | % | ||
17 | %Add a zero zone of size wx2,wy2 | ||
18 | %July 15th, 1999 - Bug on the mask building... fixed + change to Gaussian mask with higher | ||
19 | %resolution and larger number of iterations. | ||
20 | |||
21 | |||
22 | % California Institute of Technology | ||
23 | % (c) Jean-Yves Bouguet -- Oct. 14th, 1997 | ||
24 | |||
25 | |||
26 | |||
27 | line_feat = 1; % set to 1 to allow for extraction of line features. | ||
28 | |||
29 | xt = xt'; | ||
30 | xt = fliplr(xt); | ||
31 | |||
32 | |||
33 | if nargin < 4, | ||
34 | winty = 5; | ||
35 | if nargin < 3, | ||
36 | wintx = 5; | ||
37 | end; | ||
38 | end; | ||
39 | |||
40 | |||
41 | if nargin < 6, | ||
42 | wx2 = -1; | ||
43 | wy2 = -1; | ||
44 | end; | ||
45 | |||
46 | |||
47 | %mask = ones(2*wintx+1,2*winty+1); | ||
48 | mask = exp(-((-wintx:wintx)'/(wintx)).^2) * exp(-((-winty:winty)/(winty)).^2); | ||
49 | |||
50 | |||
51 | if (wx2>0) & (wy2>0), | ||
52 | if ((wintx - wx2)>=2)&((winty - wy2)>=2), | ||
53 | mask(wintx+1-wx2:wintx+1+wx2,winty+1-wy2:winty+1+wy2)= zeros(2*wx2+1,2*wy2+1); | ||
54 | end; | ||
55 | end; | ||
56 | |||
57 | offx = [-wintx:wintx]'*ones(1,2*winty+1); | ||
58 | offy = ones(2*wintx+1,1)*[-winty:winty]; | ||
59 | |||
60 | resolution = 0.005; | ||
61 | |||
62 | MaxIter = 10; | ||
63 | |||
64 | [nx,ny] = size(I); | ||
65 | N = size(xt,1); | ||
66 | |||
67 | xc = xt; % first guess... they don't move !!! | ||
68 | |||
69 | type = zeros(1,N); | ||
70 | |||
71 | |||
72 | for i=1:N, | ||
73 | |||
74 | v_extra = resolution + 1; % just larger than resolution | ||
75 | |||
76 | compt = 0; % no iteration yet | ||
77 | |||
78 | while (norm(v_extra) > resolution) & (compt<MaxIter), | ||
79 | |||
80 | cIx = xc(i,1); % | ||
81 | cIy = xc(i,2); % Coords. of the point | ||
82 | crIx = round(cIx); % on the initial image | ||
83 | crIy = round(cIy); % | ||
84 | itIx = cIx - crIx; % Coefficients | ||
85 | itIy = cIy - crIy; % to compute | ||
86 | if itIx > 0, % the sub pixel | ||
87 | vIx = [itIx 1-itIx 0]'; % accuracy. | ||
88 | else | ||
89 | vIx = [0 1+itIx -itIx]'; | ||
90 | end; | ||
91 | if itIy > 0, | ||
92 | vIy = [itIy 1-itIy 0]; | ||
93 | else | ||
94 | vIy = [0 1+itIy -itIy]; | ||
95 | end; | ||
96 | |||
97 | |||
98 | % What if the sub image is not in? | ||
99 | |||
100 | if (crIx-wintx-2 < 1), xmin=1; xmax = 2*wintx+5; | ||
101 | elseif (crIx+wintx+2 > nx), xmax = nx; xmin = nx-2*wintx-4; | ||
102 | else | ||
103 | xmin = crIx-wintx-2; xmax = crIx+wintx+2; | ||
104 | end; | ||
105 | |||
106 | if (crIy-winty-2 < 1), ymin=1; ymax = 2*winty+5; | ||
107 | elseif (crIy+winty+2 > ny), ymax = ny; ymin = ny-2*winty-4; | ||
108 | else | ||
109 | ymin = crIy-winty-2; ymax = crIy+winty+2; | ||
110 | end; | ||
111 | |||
112 | |||
113 | SI = I(xmin:xmax,ymin:ymax); % The necessary neighborhood | ||
114 | SI = conv2(conv2(SI,vIx,'same'),vIy,'same'); | ||
115 | SI = SI(2:2*wintx+4,2:2*winty+4); % The subpixel interpolated neighborhood | ||
116 | [gy,gx] = gradient(SI); % The gradient image | ||
117 | gx = gx(2:2*wintx+2,2:2*winty+2); % extraction of the useful parts only | ||
118 | gy = gy(2:2*wintx+2,2:2*winty+2); % of the gradients | ||
119 | |||
120 | px = cIx + offx; | ||
121 | py = cIy + offy; | ||
122 | |||
123 | gxx = gx .* gx .* mask; | ||
124 | gyy = gy .* gy .* mask; | ||
125 | gxy = gx .* gy .* mask; | ||
126 | |||
127 | |||
128 | bb = [sum(sum(gxx .* px + gxy .* py)); sum(sum(gxy .* px + gyy .* py))]; | ||
129 | |||
130 | a = sum(sum(gxx)); | ||
131 | b = sum(sum(gxy)); | ||
132 | c = sum(sum(gyy)); | ||
133 | |||
134 | dt = a*c - b^2; | ||
135 | |||
136 | xc2 = [c*bb(1)-b*bb(2) a*bb(2)-b*bb(1)]/dt; | ||
137 | |||
138 | |||
139 | %keyboard; | ||
140 | |||
141 | if line_feat, | ||
142 | |||
143 | G = [a b;b c]; | ||
144 | [U,S,V] = svd(G); | ||
145 | |||
146 | %keyboard; | ||
147 | |||
148 | % If non-invertible, then project the point onto the edge orthogonal: | ||
149 | |||
150 | if (S(1,1)/S(2,2) > 50), | ||
151 | % projection operation: | ||
152 | xc2 = xc2 + sum((xc(i,:)-xc2).*(V(:,2)'))*V(:,2)'; | ||
153 | type(i) = 1; | ||
154 | end; | ||
155 | |||
156 | end; | ||
157 | |||
158 | |||
159 | %keyboard; | ||
160 | |||
161 | % G = [a b;b c]; | ||
162 | % [U,S,V] = svd(G); | ||
163 | |||
164 | |||
165 | % if S(1,1)/S(2,2) > 150, | ||
166 | % bb2 = U'*bb; | ||
167 | % xc2 = (V*[bb2(1)/S(1,1) ;0])'; | ||
168 | % else | ||
169 | % xc2 = [c*bb(1)-b*bb(2) a*bb(2)-b*bb(1)]/dt; | ||
170 | % end; | ||
171 | |||
172 | |||
173 | %if (abs(a)> 50*abs(c)), | ||
174 | % xc2 = [(c*bb(1)-b*bb(2))/dt xc(i,2)]; | ||
175 | % elseif (abs(c)> 50*abs(a)) | ||
176 | % xc2 = [xc(i,1) (a*bb(2)-b*bb(1))/dt]; | ||
177 | % else | ||
178 | % xc2 = [c*bb(1)-b*bb(2) a*bb(2)-b*bb(1)]/dt; | ||
179 | % end; | ||
180 | |||
181 | %keyboard; | ||
182 | |||
183 | v_extra = xc(i,:) - xc2; | ||
184 | |||
185 | xc(i,:) = xc2; | ||
186 | |||
187 | % keyboard; | ||
188 | |||
189 | compt = compt + 1; | ||
190 | |||
191 | end | ||
192 | end; | ||
193 | |||
194 | |||
195 | % check for points that diverge: | ||
196 | |||
197 | delta_x = xc(:,1) - xt(:,1); | ||
198 | delta_y = xc(:,2) - xt(:,2); | ||
199 | |||
200 | %keyboard; | ||
201 | |||
202 | |||
203 | bad = (abs(delta_x) > wintx) | (abs(delta_y) > winty); | ||
204 | good = ~bad; | ||
205 | in_bad = find(bad); | ||
206 | |||
207 | % For the diverged points, keep the original guesses: | ||
208 | |||
209 | xc(in_bad,:) = xt(in_bad,:); | ||
210 | |||
211 | xc = fliplr(xc); | ||
212 | xc = xc'; | ||
213 | |||
214 | bad = bad'; | ||
215 | good = good'; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/count_squares.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/count_squares.m new file mode 100755 index 0000000..0e226c0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/count_squares.m | |||
@@ -0,0 +1,74 @@ | |||
1 | function ns = count_squares(I,x1,y1,x2,y2,win); | ||
2 | |||
3 | %keyboard; | ||
4 | |||
5 | [ny,nx] = size(I); | ||
6 | |||
7 | lambda = [y1 - y2;x2 - x1;x1*y2 - x2*y1]; | ||
8 | |||
9 | lambda = 1/sqrt(lambda(1)^2 + lambda(2)^2) * lambda; | ||
10 | |||
11 | l1 = lambda + [0;0;win]; | ||
12 | l2 = lambda - [0;0;win]; | ||
13 | |||
14 | |||
15 | dx = x2-x1; | ||
16 | dy = y2 - y1; | ||
17 | |||
18 | |||
19 | if abs(dx) > abs(dy), | ||
20 | |||
21 | if x2 > x1, | ||
22 | xs = x1:x2; | ||
23 | else | ||
24 | xs = x1:-1:x2; | ||
25 | end; | ||
26 | |||
27 | ys = -(lambda(3) + lambda(1)*xs)/lambda(2); | ||
28 | |||
29 | else | ||
30 | |||
31 | if y2 > y1, | ||
32 | ys = y1:y2; | ||
33 | else | ||
34 | ys = y1:-1:y2; | ||
35 | end; | ||
36 | xs = -(lambda(3) + lambda(2)*ys)/lambda(1); | ||
37 | |||
38 | end; | ||
39 | |||
40 | |||
41 | |||
42 | Np = length(xs); | ||
43 | |||
44 | xs_mat = ones(2*win + 1,1)*xs; | ||
45 | ys_mat = ones(2*win + 1,1)*ys; | ||
46 | |||
47 | win_mat = (-win:win)'*ones(1,Np); | ||
48 | |||
49 | |||
50 | xs_mat2 = round(xs_mat - win_mat * lambda(1)); | ||
51 | ys_mat2 = round(ys_mat - win_mat * lambda(2)); | ||
52 | |||
53 | ind_mat = (xs_mat2 - 1) * ny + ys_mat2; | ||
54 | |||
55 | ima_patch = zeros(2*win + 1,Np); | ||
56 | |||
57 | ima_patch(:) = I(ind_mat(:)); | ||
58 | |||
59 | %ima2 = ima_patch(:,win+1:end-win); | ||
60 | |||
61 | filtk = [ones(win,Np);zeros(1,Np);-ones(win,Np)]; | ||
62 | |||
63 | out_f = sum(filtk.*ima_patch); | ||
64 | |||
65 | out_f_f = conv2(out_f,[1/4 1/2 1/4],'same'); | ||
66 | |||
67 | out_f_f = out_f_f(win+1:end-win); | ||
68 | |||
69 | ns = length(find(((out_f_f(2:end)>=0)&(out_f_f(1:end-1)<0)) | ((out_f_f(2:end)<=0)&(out_f_f(1:end-1)>0))))+1; | ||
70 | |||
71 | |||
72 | |||
73 | |||
74 | return; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/data_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/data_calib.m new file mode 100755 index 0000000..318ec15 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/data_calib.m | |||
@@ -0,0 +1,89 @@ | |||
1 | %%% This script alets the user enter the name of the images (base name, numbering scheme,... | ||
2 | |||
3 | dir; | ||
4 | |||
5 | %disp('Camera Calibration using multiple images of a planar checkerboard pattern'); | ||
6 | %disp('Model: 2 focals, 2 radial dist. coeff., 2 tangential dist. coeff. and principle point'); | ||
7 | %disp(' => 8DOF intrinsic model ([Heikkila and Silven, University of Oulu])'); | ||
8 | |||
9 | fprintf(1,'\n'); | ||
10 | calib_name = input('Basename camera calibration images (without number nor suffix): ','s'); | ||
11 | |||
12 | format_image = '0'; | ||
13 | |||
14 | while format_image == '0', | ||
15 | |||
16 | format_image = input('Image format: ([]=''r''=''ras'', ''b''=''bmp'', ''t''=''tif'', ''p''=''pgm'', ''j''=''jpg'') ','s'); | ||
17 | |||
18 | if isempty(format_image), | ||
19 | format_image = 'ras'; | ||
20 | end; | ||
21 | |||
22 | if lower(format_image(1)) == 'b', | ||
23 | format_image = 'bmp'; | ||
24 | else | ||
25 | if lower(format_image(1)) == 't', | ||
26 | format_image = 'tif'; | ||
27 | else | ||
28 | if lower(format_image(1)) == 'p', | ||
29 | format_image = 'pgm'; | ||
30 | else | ||
31 | if lower(format_image(1)) == 'j', | ||
32 | format_image = 'jpg'; | ||
33 | else | ||
34 | if lower(format_image(1)) == 'r', | ||
35 | format_image = 'ras'; | ||
36 | else | ||
37 | disp('Invalid image format'); | ||
38 | format_image = '0'; % Ask for format once again | ||
39 | end; | ||
40 | end; | ||
41 | end; | ||
42 | end; | ||
43 | end; | ||
44 | |||
45 | end; | ||
46 | |||
47 | |||
48 | n_ima = 1000; | ||
49 | while n_ima > 30, | ||
50 | n_ima = input('Number of calibration images: '); | ||
51 | n_ima = round(n_ima); | ||
52 | end; | ||
53 | |||
54 | type_numbering = input('Type of numbering (ex: []=4,other=04): '); | ||
55 | |||
56 | type_numbering = ~isempty(type_numbering); | ||
57 | |||
58 | if type_numbering, | ||
59 | |||
60 | N_slots = input('Number of spaces for numbers? (ex: 2 -> 04, 3 -> 004), ([]=3) '); | ||
61 | |||
62 | if isempty(N_slots), N_slots = 3; end; | ||
63 | |||
64 | else | ||
65 | |||
66 | N_slots = 1; % not used anyway, but useful for saving | ||
67 | |||
68 | end; | ||
69 | |||
70 | |||
71 | first_num = input('First image number? (0,1,2...) ([]=0) '); | ||
72 | |||
73 | if isempty(first_num), first_num = 0; end; | ||
74 | |||
75 | image_numbers = first_num:n_ima-1+first_num; | ||
76 | |||
77 | |||
78 | %%% By default, all the images are active for calibration: | ||
79 | |||
80 | active_images = ones(1,n_ima); | ||
81 | |||
82 | %string_save = 'save calib_data n_ima type_numbering N_slots image_numbers format_image calib_name first_num'; | ||
83 | |||
84 | %eval(string_save); | ||
85 | |||
86 | % Reading images: | ||
87 | |||
88 | ima_read_calib; | ||
89 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/error_analysis.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/error_analysis.m new file mode 100755 index 0000000..85feac5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/error_analysis.m | |||
@@ -0,0 +1,182 @@ | |||
1 | %%% ERROR_ANALYSIS | ||
2 | %%% This simulation helps coputing the acturacies of calibration | ||
3 | %%% Run it after the main calibration | ||
4 | |||
5 | |||
6 | |||
7 | N_runs = 200; | ||
8 | |||
9 | %N_ima_active = 4; | ||
10 | |||
11 | saving = 1; | ||
12 | |||
13 | if 1, %~exist('fc_list'), % initialization | ||
14 | |||
15 | % Initialization: | ||
16 | |||
17 | load Calib_Results; | ||
18 | check_active_images; | ||
19 | |||
20 | fc_list = []; | ||
21 | cc_list = []; | ||
22 | kc_list = []; | ||
23 | active_images_list = []; | ||
24 | |||
25 | |||
26 | for kk=1:n_ima, | ||
27 | |||
28 | eval(['omc_list_' num2str(kk) ' = [];']); | ||
29 | eval(['Tc_list_' num2str(kk) ' = [];']); | ||
30 | |||
31 | end; | ||
32 | |||
33 | %sx = median(abs(ex(1,:)))*1.4836; | ||
34 | %sy = median(abs(ex(2,:)))*1.4836; | ||
35 | |||
36 | sx = std(ex(1,:)); | ||
37 | sy = std(ex(2,:)); | ||
38 | |||
39 | % Saving the feature locations: | ||
40 | |||
41 | for kk = 1:n_ima, | ||
42 | |||
43 | eval(['x_save_' num2str(kk) ' = x_' num2str(kk) ';']); | ||
44 | eval(['y_save_' num2str(kk) ' = y_' num2str(kk) ';']); | ||
45 | |||
46 | end; | ||
47 | |||
48 | active_images_save = active_images; | ||
49 | ind_active_save = ind_active; | ||
50 | |||
51 | fc_save = fc; | ||
52 | cc_save = cc; | ||
53 | kc_save = kc; | ||
54 | KK_save = KK; | ||
55 | |||
56 | |||
57 | end; | ||
58 | |||
59 | |||
60 | |||
61 | |||
62 | %%% The main loop: | ||
63 | |||
64 | |||
65 | for ntrial = 1:N_runs, | ||
66 | |||
67 | fprintf(1,'\nRun number: %d\n',ntrial); | ||
68 | fprintf(1, '----------\n'); | ||
69 | |||
70 | for kk = 1:n_ima, | ||
71 | |||
72 | eval(['y_kk = y_save_' num2str(kk) ';']) | ||
73 | |||
74 | if active_images(kk) & ~isnan(y_kk(1,1)), | ||
75 | |||
76 | Nkk = size(y_kk,2); | ||
77 | |||
78 | x_kk_new = y_kk + [sx * randn(1,Nkk);sy*randn(1,Nkk)]; | ||
79 | |||
80 | eval(['x_' num2str(kk) ' = x_kk_new;']); | ||
81 | |||
82 | end; | ||
83 | |||
84 | end; | ||
85 | |||
86 | N_active = length(ind_active_save); | ||
87 | junk = randn(1,N_active); | ||
88 | [junk,junk2] = sort(junk); | ||
89 | |||
90 | active_images = zeros(1,n_ima); | ||
91 | active_images(ind_active_save(junk2(1:N_ima_active))) = ones(1,N_ima_active); | ||
92 | |||
93 | fc = fc_save; | ||
94 | cc = cc_save; | ||
95 | kc = kc_save; | ||
96 | KK = KK_save; | ||
97 | |||
98 | go_calib_optim; | ||
99 | |||
100 | fc_list = [fc_list fc]; | ||
101 | cc_list = [cc_list cc]; | ||
102 | kc_list = [kc_list kc]; | ||
103 | active_images_list = [active_images_list active_images']; | ||
104 | |||
105 | for kk=1:n_ima, | ||
106 | |||
107 | eval(['omc_list_' num2str(kk) ' = [ omc_list_' num2str(kk) ' omc_' num2str(kk) ' ];']); | ||
108 | eval(['Tc_list_' num2str(kk) ' = [ Tc_list_' num2str(kk) ' Tc_' num2str(kk) ' ];']); | ||
109 | |||
110 | end; | ||
111 | |||
112 | end; | ||
113 | |||
114 | |||
115 | |||
116 | |||
117 | if 0, | ||
118 | |||
119 | % Restoring the feature locations: | ||
120 | |||
121 | for kk = 1:n_ima, | ||
122 | |||
123 | eval(['x_' num2str(kk) ' = x_save_' num2str(kk) ';']); | ||
124 | |||
125 | end; | ||
126 | |||
127 | fprintf(1,'\nFinal run (with the real data)\n'); | ||
128 | fprintf(1, '------------------------------\n'); | ||
129 | |||
130 | active_images = active_images_save; | ||
131 | ind_active = ind_active_save; | ||
132 | |||
133 | go_calib_optim; | ||
134 | |||
135 | fc_list = [fc_list fc]; | ||
136 | cc_list = [cc_list cc]; | ||
137 | kc_list = [kc_list kc]; | ||
138 | active_images_list = [active_images_list active_images']; | ||
139 | |||
140 | for kk=1:n_ima, | ||
141 | |||
142 | eval(['omc_list_' num2str(kk) ' = [ omc_list_' num2str(kk) ' omc_' num2str(kk) ' ];']); | ||
143 | eval(['Tc_list_' num2str(kk) ' = [ Tc_list_' num2str(kk) ' Tc_' num2str(kk) ' ];']); | ||
144 | |||
145 | end; | ||
146 | |||
147 | end; | ||
148 | |||
149 | |||
150 | |||
151 | |||
152 | |||
153 | if saving, | ||
154 | |||
155 | disp(['Save Calibration accuracy results under Calib_Accuracies_' num2str(N_ima_active) '.mat']); | ||
156 | |||
157 | string_save = ['save Calib_Accuracies_' num2str(N_ima_active) ' active_images n_ima N_ima_active N_runs active_images_list fc cc kc fc_list cc_list kc_list']; | ||
158 | |||
159 | for kk = 1:n_ima, | ||
160 | string_save = [string_save ' Tc_list_' num2str(kk) ' omc_list_' num2str(kk) ' Tc_' num2str(kk) ' omc_' num2str(kk) ]; | ||
161 | end; | ||
162 | |||
163 | eval(string_save); | ||
164 | |||
165 | end; | ||
166 | |||
167 | |||
168 | return; | ||
169 | |||
170 | std(fc_list') | ||
171 | |||
172 | std(cc_list') | ||
173 | |||
174 | std(kc_list') | ||
175 | |||
176 | for kk = 1:n_ima, | ||
177 | |||
178 | eval(['std(Tc_list_' num2str(kk) ''')']) | ||
179 | |||
180 | end; | ||
181 | |||
182 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/ext_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/ext_calib.m new file mode 100755 index 0000000..d41d068 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/ext_calib.m | |||
@@ -0,0 +1,130 @@ | |||
1 | |||
2 | %%%%%%%%%%%%%%%%%%%% SHOW EXTRINSIC RESULTS %%%%%%%%%%%%%%%%%%%%%%%% | ||
3 | |||
4 | check_active_images; | ||
5 | |||
6 | if ~exist(['omc_' num2str(ind_active(1))]), | ||
7 | fprintf(1,'Need to calibrate before showing extrinsic results. Maybe need to load Calib_Results.mat file.\n'); | ||
8 | return; | ||
9 | end; | ||
10 | |||
11 | %if ~exist('no_grid'), | ||
12 | no_grid = 0; | ||
13 | %end; | ||
14 | |||
15 | if ~exist(['n_sq_x_' num2str(ind_active(1))]), | ||
16 | no_grid = 1; | ||
17 | end; | ||
18 | |||
19 | |||
20 | if 0, | ||
21 | |||
22 | err_std = std(ex'); | ||
23 | |||
24 | fprintf(1,'\n\nCalibration results without principal point estimation:\n\n'); | ||
25 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
26 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
27 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
28 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
29 | |||
30 | end; | ||
31 | |||
32 | |||
33 | % Color code for each image: | ||
34 | |||
35 | colors = 'brgkcm'; | ||
36 | |||
37 | |||
38 | %%% Show the extrinsic parameters | ||
39 | |||
40 | if ~exist('dX'), | ||
41 | eval(['dX = norm(Tc_' num2str(ind_active(1)) ')/10;']); | ||
42 | dY = dX; | ||
43 | end; | ||
44 | |||
45 | IP = 5*dX*([0 nx-1 nx-1 0 0 ; 0 0 ny-1 ny-1 0;1 1 1 1 1] - [cc;0]*ones(1,5)) ./ ([fc;1]*ones(1,5)); | ||
46 | BASE = 5*dX*([0 1 0 0 0 0;0 0 0 1 0 0;0 0 0 0 0 1]); | ||
47 | IP = reshape([IP;BASE(:,1)*ones(1,5);IP],3,15); | ||
48 | |||
49 | figure(4); | ||
50 | [a,b] = view; | ||
51 | |||
52 | figure(4); | ||
53 | plot3(BASE(1,:),BASE(3,:),-BASE(2,:),'b-','linewidth',2'); | ||
54 | hold on; | ||
55 | plot3(IP(1,:),IP(3,:),-IP(2,:),'r-','linewidth',2); | ||
56 | text(6*dX,0,0,'X_c'); | ||
57 | text(-dX,5*dX,0,'Z_c'); | ||
58 | text(0,0,-6*dX,'Y_c'); | ||
59 | text(-dX,-dX,dX,'O_c'); | ||
60 | |||
61 | |||
62 | for kk = 1:n_ima, | ||
63 | |||
64 | if active_images(kk); | ||
65 | |||
66 | eval(['XX_kk = X_' num2str(kk) ';']); | ||
67 | eval(['omc_kk = omc_' num2str(kk) ';']); | ||
68 | eval(['Tc_kk = Tc_' num2str(kk) ';']); | ||
69 | N_kk = size(XX_kk,2); | ||
70 | |||
71 | if ~exist(['n_sq_x_' num2str(kk)]), | ||
72 | no_grid = 1; | ||
73 | end; | ||
74 | |||
75 | if ~no_grid, | ||
76 | eval(['n_sq_x = n_sq_x_' num2str(kk) ';']); | ||
77 | eval(['n_sq_y = n_sq_y_' num2str(kk) ';']); | ||
78 | if (N_kk ~= ((n_sq_x+1)*(n_sq_y+1))), | ||
79 | no_grid = 1; | ||
80 | end; | ||
81 | end; | ||
82 | |||
83 | if ~isnan(omc_kk(1,1)), | ||
84 | |||
85 | R_kk = rodrigues(omc_kk); | ||
86 | |||
87 | YY_kk = R_kk * XX_kk + Tc_kk * ones(1,length(XX_kk)); | ||
88 | |||
89 | uu = [-dX;-dY;0]/2; | ||
90 | uu = R_kk * uu + Tc_kk; | ||
91 | |||
92 | if ~no_grid, | ||
93 | YYx = zeros(n_sq_x+1,n_sq_y+1); | ||
94 | YYy = zeros(n_sq_x+1,n_sq_y+1); | ||
95 | YYz = zeros(n_sq_x+1,n_sq_y+1); | ||
96 | |||
97 | YYx(:) = YY_kk(1,:); | ||
98 | YYy(:) = YY_kk(2,:); | ||
99 | YYz(:) = YY_kk(3,:); | ||
100 | |||
101 | %keyboard; | ||
102 | |||
103 | figure(4); | ||
104 | hhh= mesh(YYx,YYz,-YYy); | ||
105 | set(hhh,'edgecolor',colors(rem(kk-1,6)+1),'linewidth',1); %,'facecolor','none'); | ||
106 | %plot3(YY_kk(1,:),YY_kk(3,:),-YY_kk(2,:),['o' colors(rem(kk-1,6)+1)]); | ||
107 | text(uu(1),uu(3),-uu(2),num2str(kk),'fontsize',14,'color',colors(rem(kk-1,6)+1)); | ||
108 | else | ||
109 | |||
110 | figure(4); | ||
111 | plot3(YY_kk(1,:),YY_kk(3,:),-YY_kk(2,:),['.' colors(rem(kk-1,6)+1)]); | ||
112 | text(uu(1),uu(3),-uu(2),num2str(kk),'fontsize',14,'color',colors(rem(kk-1,6)+1)); | ||
113 | |||
114 | end; | ||
115 | |||
116 | end; | ||
117 | |||
118 | end; | ||
119 | |||
120 | end; | ||
121 | |||
122 | figure(4);rotate3d on; | ||
123 | axis('equal'); | ||
124 | title('Extrinsic parameters'); | ||
125 | %view(60,30); | ||
126 | view(a,b); | ||
127 | hold off; | ||
128 | |||
129 | set(4,'Name','3D','NumberTitle','off'); | ||
130 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extract_grid.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extract_grid.m new file mode 100755 index 0000000..1e3cbdb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extract_grid.m | |||
@@ -0,0 +1,227 @@ | |||
1 | function [x,X,n_sq_x,n_sq_y,ind_orig,ind_x,ind_y] = extract_grid(I,wintx,winty,fc,cc,kc); | ||
2 | |||
3 | map = gray(256); | ||
4 | |||
5 | figure(2); | ||
6 | image(I); | ||
7 | colormap(map); | ||
8 | |||
9 | |||
10 | if nargin < 2, | ||
11 | |||
12 | disp('Window size for corner finder (wintx and winty):'); | ||
13 | wintx = input('wintx ([] = 5) = '); | ||
14 | if isempty(wintx), wintx = 5; end; | ||
15 | wintx = round(wintx); | ||
16 | winty = input('winty ([] = 5) = '); | ||
17 | if isempty(winty), winty = 5; end; | ||
18 | winty = round(winty); | ||
19 | |||
20 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
21 | |||
22 | end; | ||
23 | |||
24 | |||
25 | |||
26 | title('Click on the four extreme corners of the rectangular pattern...'); | ||
27 | |||
28 | disp('Click on the four extreme corners of the rectangular complete pattern...'); | ||
29 | |||
30 | [x,y] = ginput3(4); | ||
31 | |||
32 | [Xc,good,bad,type] = cornerfinder([x';y'],I,winty,wintx); % the four corners | ||
33 | |||
34 | x = Xc(1,:)'; | ||
35 | y = Xc(2,:)'; | ||
36 | |||
37 | [y,indy] = sort(y); | ||
38 | x = x(indy); | ||
39 | |||
40 | if (x(2) > x(1)), | ||
41 | x4 = x(1);y4 = y(1); x3 = x(2); y3 = y(2); | ||
42 | else | ||
43 | x4 = x(2);y4 = y(2); x3 = x(1); y3 = y(1); | ||
44 | end; | ||
45 | if (x(3) > x(4)), | ||
46 | x2 = x(3);y2 = y(3); x1 = x(4); y1 = y(4); | ||
47 | else | ||
48 | x2 = x(4);y2 = y(4); x1 = x(3); y1 = y(3); | ||
49 | end; | ||
50 | |||
51 | x = [x1;x2;x3;x4]; | ||
52 | y = [y1;y2;y3;y4]; | ||
53 | |||
54 | |||
55 | figure(2); hold on; | ||
56 | plot([x;x(1)],[y;y(1)],'g-'); | ||
57 | plot(x,y,'og'); | ||
58 | hx=text((x(4)+x(3))/2,(y(4)+y(3))/2 - 20,'X'); | ||
59 | set(hx,'color','g','Fontsize',14); | ||
60 | hy=text((x(4)+x(1))/2-20,(y(4)+y(1))/2,'Y'); | ||
61 | set(hy,'color','g','Fontsize',14); | ||
62 | hold off; | ||
63 | |||
64 | |||
65 | % Try to automatically count the number of squares in the grid | ||
66 | |||
67 | n_sq_x1 = count_squares(I,x1,y1,x2,y2,wintx); | ||
68 | n_sq_x2 = count_squares(I,x3,y3,x4,y4,wintx); | ||
69 | n_sq_y1 = count_squares(I,x2,y2,x3,y3,wintx); | ||
70 | n_sq_y2 = count_squares(I,x4,y4,x1,y1,wintx); | ||
71 | |||
72 | |||
73 | |||
74 | % If could not count the number of squares, enter manually | ||
75 | |||
76 | if (n_sq_x1~=n_sq_x2)|(n_sq_y1~=n_sq_y2), | ||
77 | |||
78 | |||
79 | disp('Could not count the number of squares in the grid. Enter manually.'); | ||
80 | n_sq_x = input('Number of squares along the X direction ([]=10) = '); %6 | ||
81 | if isempty(n_sq_x), n_sq_x = 10; end; | ||
82 | n_sq_y = input('Number of squares along the Y direction ([]=10) = '); %6 | ||
83 | if isempty(n_sq_y), n_sq_y = 10; end; | ||
84 | |||
85 | else | ||
86 | |||
87 | n_sq_x = n_sq_x1; | ||
88 | n_sq_y = n_sq_y1; | ||
89 | |||
90 | end; | ||
91 | |||
92 | |||
93 | % Enter the size of each square | ||
94 | |||
95 | dX = input(['Size dX of each square along the X direction ([]=3cm) = ']); | ||
96 | dY = input(['Size dY of each square along the Y direction ([]=3cm) = ']); | ||
97 | if isempty(dX), dX = 3; end; | ||
98 | if isempty(dY), dY = 3; end; | ||
99 | |||
100 | |||
101 | |||
102 | % Compute the inside points through computation of the planar homography (collineation) | ||
103 | |||
104 | a00 = [x(1);y(1);1]; | ||
105 | a10 = [x(2);y(2);1]; | ||
106 | a11 = [x(3);y(3);1]; | ||
107 | a01 = [x(4);y(4);1]; | ||
108 | |||
109 | |||
110 | % Compute the planart collineation: (return the normalization matrice as well) | ||
111 | |||
112 | [Homo,Hnorm,inv_Hnorm] = compute_homography ([a00 a10 a11 a01],[0 1 1 0;0 0 1 1;1 1 1 1]); | ||
113 | |||
114 | |||
115 | % Build the grid using the planar collineation: | ||
116 | |||
117 | x_l = ((0:n_sq_x)'*ones(1,n_sq_y+1))/n_sq_x; | ||
118 | y_l = (ones(n_sq_x+1,1)*(0:n_sq_y))/n_sq_y; | ||
119 | pts = [x_l(:) y_l(:) ones((n_sq_x+1)*(n_sq_y+1),1)]'; | ||
120 | |||
121 | XX = Homo*pts; | ||
122 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
123 | |||
124 | |||
125 | % Complete size of the rectangle | ||
126 | |||
127 | W = n_sq_x*dX; | ||
128 | L = n_sq_y*dY; | ||
129 | |||
130 | |||
131 | |||
132 | if nargin < 6, | ||
133 | |||
134 | %%%%%%%%%%%%%%%%%%%%%%%% ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
135 | figure(2); | ||
136 | hold on; | ||
137 | plot(XX(1,:),XX(2,:),'r+'); | ||
138 | title('The red crosses should be close to the image corners'); | ||
139 | hold off; | ||
140 | |||
141 | disp('If the guessed grid corners (red crosses on the image) are not close to the actual corners,'); | ||
142 | disp('it is necessary to enter an initial guess for the radial distortion factor kc (useful for subpixel detection)'); | ||
143 | quest_distort = input('Need of an initial guess for distortion? ([]=no, other=yes) '); | ||
144 | |||
145 | quest_distort = ~isempty(quest_distort); | ||
146 | |||
147 | if quest_distort, | ||
148 | % Estimation of focal length: | ||
149 | c_g = [size(I,2);size(I,1)]/2 + .5; | ||
150 | f_g = Distor2Calib(0,[[x(1) x(2) x(4) x(3)] - c_g(1);[y(1) y(2) y(4) y(3)] - c_g(2)],1,1,4,W,L,[-W/2 W/2 W/2 -W/2;L/2 L/2 -L/2 -L/2; 0 0 0 0],100,1,1); | ||
151 | f_g = mean(f_g); | ||
152 | script_fit_distortion; | ||
153 | end; | ||
154 | %%%%%%%%%%%%%%%%%%%%% END ADDITIONAL STUFF IN THE CASE OF HIGHLY DISTORTED IMAGES %%%%%%%%%%%%% | ||
155 | |||
156 | else | ||
157 | |||
158 | xy_corners_undist = comp_distortion_oulu([(x' - cc(1))/fc(1);(y'-cc(2))/fc(1)],kc); | ||
159 | |||
160 | xu = xy_corners_undist(1,:)'; | ||
161 | yu = xy_corners_undist(2,:)'; | ||
162 | |||
163 | [XXu] = projectedGrid ( [xu(1);yu(1)], [xu(2);yu(2)],[xu(3);yu(3)], [xu(4);yu(4)],n_sq_x+1,n_sq_y+1); % The full grid | ||
164 | |||
165 | r2 = sum(XXu.^2); | ||
166 | XX = (ones(2,1)*(1 + kc(1) * r2 + kc(2) * (r2.^2))) .* XXu; | ||
167 | XX(1,:) = fc(1)*XX(1,:)+cc(1); | ||
168 | XX(2,:) = fc(2)*XX(2,:)+cc(2); | ||
169 | |||
170 | end; | ||
171 | |||
172 | |||
173 | Np = (n_sq_x+1)*(n_sq_y+1); | ||
174 | |||
175 | disp('Corner extraction...'); | ||
176 | |||
177 | grid_pts = cornerfinder(XX,I,winty,wintx); %%% Finds the exact corners at every points! | ||
178 | |||
179 | grid_pts = grid_pts - 1; % subtract 1 to bring the origin to (0,0) instead of (1,1) in matlab (not necessary in C) | ||
180 | |||
181 | ind_corners = [1 n_sq_x+1 (n_sq_x+1)*n_sq_y+1 (n_sq_x+1)*(n_sq_y+1)]; % index of the 4 corners | ||
182 | ind_orig = (n_sq_x+1)*n_sq_y + 1; | ||
183 | xorig = grid_pts(1,ind_orig); | ||
184 | yorig = grid_pts(2,ind_orig); | ||
185 | dxpos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig+1)]'); | ||
186 | dypos = mean([grid_pts(:,ind_orig) grid_pts(:,ind_orig-n_sq_x-1)]'); | ||
187 | |||
188 | |||
189 | ind_x = (n_sq_x+1)*(n_sq_y + 1); | ||
190 | ind_y = 1; | ||
191 | |||
192 | x_box_kk = [grid_pts(1,:)-(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)+(wintx+.5);grid_pts(1,:)-(wintx+.5);grid_pts(1,:)-(wintx+.5)]; | ||
193 | y_box_kk = [grid_pts(2,:)-(winty+.5);grid_pts(2,:)-(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)+(winty+.5);grid_pts(2,:)-(winty+.5)]; | ||
194 | |||
195 | |||
196 | figure(3); | ||
197 | image(I); colormap(map); hold on; | ||
198 | plot(grid_pts(1,:)+1,grid_pts(2,:)+1,'r+'); | ||
199 | plot(x_box_kk+1,y_box_kk+1,'-b'); | ||
200 | plot(grid_pts(1,ind_corners)+1,grid_pts(2,ind_corners)+1,'mo'); | ||
201 | plot(xorig+1,yorig+1,'*m'); | ||
202 | h = text(xorig-15,yorig-15,'O'); | ||
203 | set(h,'Color','m','FontSize',14); | ||
204 | h2 = text(dxpos(1)-10,dxpos(2)-10,'dX'); | ||
205 | set(h2,'Color','g','FontSize',14); | ||
206 | h3 = text(dypos(1)-25,dypos(2)-3,'dY'); | ||
207 | set(h3,'Color','g','FontSize',14); | ||
208 | xlabel('Xc (in camera frame)'); | ||
209 | ylabel('Yc (in camera frame)'); | ||
210 | title('Extracted corners'); | ||
211 | zoom on; | ||
212 | drawnow; | ||
213 | hold off; | ||
214 | |||
215 | |||
216 | Xi = reshape(([0:n_sq_x]*dX)'*ones(1,n_sq_y+1),Np,1)'; | ||
217 | Yi = reshape(ones(n_sq_x+1,1)*[n_sq_y:-1:0]*dY,Np,1)'; | ||
218 | Zi = zeros(1,Np); | ||
219 | |||
220 | Xgrid = [Xi;Yi;Zi]; | ||
221 | |||
222 | |||
223 | % All the point coordinates (on the image, and in 3D) - for global optimization: | ||
224 | |||
225 | x = grid_pts; | ||
226 | X = Xgrid; | ||
227 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extract_parameters.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extract_parameters.m new file mode 100755 index 0000000..8e0e1f1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extract_parameters.m | |||
@@ -0,0 +1,46 @@ | |||
1 | |||
2 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
3 | |||
4 | check_active_images; | ||
5 | |||
6 | fc = solution(1:2); | ||
7 | kc = solution(3:6); | ||
8 | cc = solution(6*n_ima + 4 +3:6*n_ima + 5 +3); | ||
9 | |||
10 | % Calibration matrix: | ||
11 | |||
12 | KK = [fc(1) 0 cc(1);0 fc(2) cc(2); 0 0 1]; | ||
13 | inv_KK = inv(KK); | ||
14 | |||
15 | % Extract the extrinsic paramters, and recomputer the collineations | ||
16 | |||
17 | for kk = 1:n_ima, | ||
18 | |||
19 | if active_images(kk), | ||
20 | |||
21 | omckk = solution(4+6*(kk-1) + 3:6*kk + 3); | ||
22 | Tckk = solution(6*kk+1 + 3:6*kk+3 + 3); | ||
23 | |||
24 | Rckk = rodrigues(omckk); | ||
25 | |||
26 | Hkk = KK * [Rckk(:,1) Rckk(:,2) Tckk]; | ||
27 | |||
28 | Hkk = Hkk / Hkk(3,3); | ||
29 | |||
30 | else | ||
31 | |||
32 | omckk = NaN*ones(3,1); | ||
33 | Tckk = NaN*ones(3,1); | ||
34 | Rckk = NaN*ones(3,3); | ||
35 | Hkk = NaN*ones(3,3); | ||
36 | |||
37 | end; | ||
38 | |||
39 | eval(['omc_' num2str(kk) ' = omckk;']); | ||
40 | eval(['Rc_' num2str(kk) ' = Rckk;']); | ||
41 | eval(['Tc_' num2str(kk) ' = Tckk;']); | ||
42 | eval(['H_' num2str(kk) '= Hkk;']); | ||
43 | |||
44 | end; | ||
45 | |||
46 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extract_parameters3D.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extract_parameters3D.m new file mode 100755 index 0000000..841c6ab --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extract_parameters3D.m | |||
@@ -0,0 +1,36 @@ | |||
1 | |||
2 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
3 | |||
4 | |||
5 | fc = solution(1:2); | ||
6 | kc = solution(3:6); | ||
7 | cc = solution(6*n_ima + 4 +3:6*n_ima + 5 +3); | ||
8 | |||
9 | % Calibration matrix: | ||
10 | |||
11 | KK = [fc(1) 0 cc(1);0 fc(2) cc(2); 0 0 1]; | ||
12 | inv_KK = inv(KK); | ||
13 | |||
14 | % Extract the extrinsic paramters, and recomputer the collineations | ||
15 | |||
16 | for kk = 1:n_ima, | ||
17 | |||
18 | omckk = solution(4+6*(kk-1) + 3:6*kk + 3); | ||
19 | |||
20 | Tckk = solution(6*kk+1 + 3:6*kk+3 + 3); | ||
21 | |||
22 | Rckk = rodrigues(omckk); | ||
23 | |||
24 | Hlkk = KK * [Rckk(:,1) Rckk(:,2) Tckk]; | ||
25 | |||
26 | Hlkk = Hlkk / Hlkk(3,3); | ||
27 | |||
28 | eval(['omc_' num2str(kk) ' = omckk;']); | ||
29 | eval(['Rc_' num2str(kk) ' = Rckk;']); | ||
30 | eval(['Tc_' num2str(kk) ' = Tckk;']); | ||
31 | |||
32 | eval(['Hl_' num2str(kk) '=Hlkk;']); | ||
33 | |||
34 | end; | ||
35 | |||
36 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extrinsic_computation.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extrinsic_computation.m new file mode 100755 index 0000000..8cf10db --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/extrinsic_computation.m | |||
@@ -0,0 +1,173 @@ | |||
1 | %%% INPUT THE IMAGE FILE NAME: | ||
2 | |||
3 | dir; | ||
4 | |||
5 | fprintf(1,'\n'); | ||
6 | disp('Computation of the extrinsic parameters from an image of a pattern'); | ||
7 | disp('The intrinsic camera parameters are assumed to be known (previously computed)'); | ||
8 | |||
9 | fprintf(1,'\n'); | ||
10 | image_name = input('Image name (full name without extension): ','s'); | ||
11 | |||
12 | format_image2 = '0'; | ||
13 | |||
14 | while format_image2 == '0', | ||
15 | |||
16 | format_image2 = input('Image format: ([]=''r''=''ras'', ''b''=''bmp'', ''t''=''tif'', ''p''=''pgm'', ''j''=''jpg'') ','s'); | ||
17 | |||
18 | if isempty(format_image2), | ||
19 | format_image2 = 'ras'; | ||
20 | end; | ||
21 | |||
22 | if lower(format_image2(1)) == 'b', | ||
23 | format_image2 = 'bmp'; | ||
24 | else | ||
25 | if lower(format_image2(1)) == 't', | ||
26 | format_image2 = 'tif'; | ||
27 | else | ||
28 | if lower(format_image2(1)) == 'p', | ||
29 | format_image2 = 'pgm'; | ||
30 | else | ||
31 | if lower(format_image2(1)) == 'j', | ||
32 | format_image2 = 'jpg'; | ||
33 | else | ||
34 | if lower(format_image2(1)) == 'r', | ||
35 | format_image2 = 'ras'; | ||
36 | else | ||
37 | disp('Invalid image format'); | ||
38 | format_image2 = '0'; % Ask for format once again | ||
39 | end; | ||
40 | end; | ||
41 | end; | ||
42 | end; | ||
43 | end; | ||
44 | end; | ||
45 | |||
46 | ima_name = [image_name '.' format_image]; | ||
47 | |||
48 | |||
49 | |||
50 | %%% READ IN IMAGE: | ||
51 | |||
52 | if format_image(1) == 'p', | ||
53 | I = double(pgmread(ima_name)); | ||
54 | else | ||
55 | if format_image(1) == 'r', | ||
56 | I = readras(ima_name); | ||
57 | else | ||
58 | I = double(imread(ima_name)); | ||
59 | end; | ||
60 | end; | ||
61 | |||
62 | if size(I,3)>1, | ||
63 | I = I(:,:,2); | ||
64 | end; | ||
65 | |||
66 | |||
67 | %%% EXTRACT GRID CORNERS: | ||
68 | |||
69 | fprintf(1,'\nExtraction of the grid corners on the image\n'); | ||
70 | |||
71 | disp('Window size for corner finder (wintx and winty):'); | ||
72 | wintx = input('wintx ([] = 5) = '); | ||
73 | if isempty(wintx), wintx = 5; end; | ||
74 | wintx = round(wintx); | ||
75 | winty = input('winty ([] = 5) = '); | ||
76 | if isempty(winty), winty = 5; end; | ||
77 | winty = round(winty); | ||
78 | |||
79 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
80 | |||
81 | [x_ext,X_ext,n_sq_x,n_sq_y,ind_orig,ind_x,ind_y] = extract_grid(I,wintx,winty,fc,cc,kc); | ||
82 | |||
83 | |||
84 | |||
85 | %%% Computation of the Extrinsic Parameters attached to the grid: | ||
86 | |||
87 | [omc_ext,Tc_ext,Rc_ext,H_ext] = compute_extrinsic(x_ext,X_ext,fc,cc,kc); | ||
88 | |||
89 | |||
90 | %%% Reproject the points on the image: | ||
91 | |||
92 | [x_reproj] = project_points(X_ext,omc_ext,Tc_ext,fc,cc,kc); | ||
93 | |||
94 | err_reproj = x_ext - x_reproj; | ||
95 | |||
96 | err_std2 = std(err_reproj')'; | ||
97 | |||
98 | |||
99 | Basis = [X_ext(:,[ind_orig ind_x ind_orig ind_y ind_orig ])]; | ||
100 | |||
101 | VX = Basis(:,2) - Basis(:,1); | ||
102 | VY = Basis(:,4) - Basis(:,1); | ||
103 | |||
104 | nX = norm(VX); | ||
105 | nY = norm(VY); | ||
106 | |||
107 | VZ = min(nX,nY) * cross(VX/nX,VY/nY); | ||
108 | |||
109 | Basis = [Basis VZ]; | ||
110 | |||
111 | [x_basis] = project_points(Basis,omc_ext,Tc_ext,fc,cc,kc); | ||
112 | |||
113 | dxpos = (x_basis(:,2) + x_basis(:,1))/2; | ||
114 | dypos = (x_basis(:,4) + x_basis(:,3))/2; | ||
115 | dzpos = (x_basis(:,6) + x_basis(:,5))/2; | ||
116 | |||
117 | |||
118 | |||
119 | figure(2); | ||
120 | image(I); | ||
121 | colormap(gray(256)); | ||
122 | hold on; | ||
123 | plot(x_ext(1,:)+1,x_ext(2,:)+1,'r+'); | ||
124 | plot(x_reproj(1,:)+1,x_reproj(2,:)+1,'yo'); | ||
125 | h = text(x_ext(1,ind_orig)-25,x_ext(2,ind_orig)-25,'O'); | ||
126 | set(h,'Color','g','FontSize',14); | ||
127 | h2 = text(dxpos(1)+1,dxpos(2)-30,'X'); | ||
128 | set(h2,'Color','g','FontSize',14); | ||
129 | h3 = text(dypos(1)-30,dypos(2)+1,'Y'); | ||
130 | set(h3,'Color','g','FontSize',14); | ||
131 | h4 = text(dzpos(1)-10,dzpos(2)-20,'Z'); | ||
132 | set(h4,'Color','g','FontSize',14); | ||
133 | plot(x_basis(1,:)+1,x_basis(2,:)+1,'g-','linewidth',2); | ||
134 | title('Image points (+) and reprojected grid points (o)'); | ||
135 | hold off; | ||
136 | |||
137 | |||
138 | fprintf(1,'\n\nExtrinsic parameters:\n\n'); | ||
139 | fprintf(1,'Translation vector: Tc_ext = [ %3.6f \t %3.6f \t %3.6f ]\n',Tc_ext); | ||
140 | fprintf(1,'Rotation vector: omc_ext = [ %3.6f \t %3.6f \t %3.6f ]\n',omc_ext); | ||
141 | fprintf(1,'Rotation matrix: Rc_ext = [ %3.6f \t %3.6f \t %3.6f\n',Rc_ext(1,:)'); | ||
142 | fprintf(1,' %3.6f \t %3.6f \t %3.6f\n',Rc_ext(2,:)'); | ||
143 | fprintf(1,' %3.6f \t %3.6f \t %3.6f ]\n',Rc_ext(3,:)'); | ||
144 | fprintf(1,'Pixel error: err = [ %3.5f \t %3.5f ]\n\n',err_std2); | ||
145 | |||
146 | |||
147 | |||
148 | |||
149 | |||
150 | return; | ||
151 | |||
152 | |||
153 | % Stores the results: | ||
154 | |||
155 | kk = 1; | ||
156 | |||
157 | % Stores location of grid wrt camera: | ||
158 | |||
159 | eval(['omc_' num2str(kk) ' = omc_ext;']); | ||
160 | eval(['Tc_' num2str(kk) ' = Tc_ext;']); | ||
161 | |||
162 | % Stores the projected points: | ||
163 | |||
164 | eval(['y_' num2str(kk) ' = x_reproj;']); | ||
165 | eval(['X_' num2str(kk) ' = X_ext;']); | ||
166 | eval(['x_' num2str(kk) ' = x_ext;']); | ||
167 | |||
168 | |||
169 | % Organize the points in a grid: | ||
170 | |||
171 | eval(['n_sq_x_' num2str(kk) ' = n_sq_x;']); | ||
172 | eval(['n_sq_y_' num2str(kk) ' = n_sq_y;']); | ||
173 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/ginput3.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/ginput3.m new file mode 100755 index 0000000..56fe496 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/ginput3.m | |||
@@ -0,0 +1,216 @@ | |||
1 | function [out1,out2,out3] = ginput2(arg1) | ||
2 | %GINPUT Graphical input from mouse. | ||
3 | % [X,Y] = GINPUT(N) gets N points from the current axes and returns | ||
4 | % the X- and Y-coordinates in length N vectors X and Y. The cursor | ||
5 | % can be positioned using a mouse (or by using the Arrow Keys on some | ||
6 | % systems). Data points are entered by pressing a mouse button | ||
7 | % or any key on the keyboard except carriage return, which terminates | ||
8 | % the input before N points are entered. | ||
9 | % | ||
10 | % [X,Y] = GINPUT gathers an unlimited number of points until the | ||
11 | % return key is pressed. | ||
12 | % | ||
13 | % [X,Y,BUTTON] = GINPUT(N) returns a third result, BUTTON, that | ||
14 | % contains a vector of integers specifying which mouse button was | ||
15 | % used (1,2,3 from left) or ASCII numbers if a key on the keyboard | ||
16 | % was used. | ||
17 | |||
18 | % Copyright (c) 1984-96 by The MathWorks, Inc. | ||
19 | % $Revision: 5.18 $ $Date: 1996/11/10 17:48:08 $ | ||
20 | |||
21 | % Fixed version by Jean-Yves Bouguet to have a cross instead of 2 lines | ||
22 | % More visible for images | ||
23 | |||
24 | P = NaN*ones(16,16); | ||
25 | P(1:15,1:15) = 2*ones(15,15); | ||
26 | P(2:14,2:14) = ones(13,13); | ||
27 | P(3:13,3:13) = NaN*ones(11,11); | ||
28 | P(6:10,6:10) = 2*ones(5,5); | ||
29 | P(7:9,7:9) = 1*ones(3,3); | ||
30 | |||
31 | out1 = []; out2 = []; out3 = []; y = []; | ||
32 | c = computer; | ||
33 | if ~strcmp(c(1:2),'PC') & ~strcmp(c(1:2),'MA') | ||
34 | tp = get(0,'TerminalProtocol'); | ||
35 | else | ||
36 | tp = 'micro'; | ||
37 | end | ||
38 | |||
39 | if ~strcmp(tp,'none') & ~strcmp(tp,'x') & ~strcmp(tp,'micro'), | ||
40 | if nargout == 1, | ||
41 | if nargin == 1, | ||
42 | eval('out1 = trmginput(arg1);'); | ||
43 | else | ||
44 | eval('out1 = trmginput;'); | ||
45 | end | ||
46 | elseif nargout == 2 | nargout == 0, | ||
47 | if nargin == 1, | ||
48 | eval('[out1,out2] = trmginput(arg1);'); | ||
49 | else | ||
50 | eval('[out1,out2] = trmginput;'); | ||
51 | end | ||
52 | if nargout == 0 | ||
53 | out1 = [ out1 out2 ]; | ||
54 | end | ||
55 | elseif nargout == 3, | ||
56 | if nargin == 1, | ||
57 | eval('[out1,out2,out3] = trmginput(arg1);'); | ||
58 | else | ||
59 | eval('[out1,out2,out3] = trmginput;'); | ||
60 | end | ||
61 | end | ||
62 | else | ||
63 | |||
64 | fig = gcf; | ||
65 | figure(gcf); | ||
66 | |||
67 | if nargin == 0 | ||
68 | how_many = -1; | ||
69 | b = []; | ||
70 | else | ||
71 | how_many = arg1; | ||
72 | b = []; | ||
73 | if isstr(how_many) ... | ||
74 | | size(how_many,1) ~= 1 | size(how_many,2) ~= 1 ... | ||
75 | | ~(fix(how_many) == how_many) ... | ||
76 | | how_many < 0 | ||
77 | error('Requires a positive integer.') | ||
78 | end | ||
79 | if how_many == 0 | ||
80 | ptr_fig = 0; | ||
81 | while(ptr_fig ~= fig) | ||
82 | ptr_fig = get(0,'PointerWindow'); | ||
83 | end | ||
84 | scrn_pt = get(0,'PointerLocation'); | ||
85 | loc = get(fig,'Position'); | ||
86 | pt = [scrn_pt(1) - loc(1), scrn_pt(2) - loc(2)]; | ||
87 | out1 = pt(1); y = pt(2); | ||
88 | elseif how_many < 0 | ||
89 | error('Argument must be a positive integer.') | ||
90 | end | ||
91 | end | ||
92 | |||
93 | pointer = get(gcf,'pointer'); | ||
94 | |||
95 | set(gcf,'Pointer','custom','PointerShapeCData',P,'PointerShapeHotSpot',[8,8]); | ||
96 | %set(gcf,'pointer','crosshair'); | ||
97 | fig_units = get(fig,'units'); | ||
98 | char = 0; | ||
99 | |||
100 | while how_many ~= 0 | ||
101 | % Use no-side effect WAITFORBUTTONPRESS | ||
102 | waserr = 0; | ||
103 | eval('keydown = wfbp;', 'waserr = 1;'); | ||
104 | if(waserr == 1) | ||
105 | if(ishandle(fig)) | ||
106 | set(fig,'pointer',pointer,'units',fig_units); | ||
107 | error('Interrupted'); | ||
108 | else | ||
109 | error('Interrupted by figure deletion'); | ||
110 | end | ||
111 | end | ||
112 | |||
113 | ptr_fig = get(0,'CurrentFigure'); | ||
114 | if(ptr_fig == fig) | ||
115 | if keydown | ||
116 | char = get(fig, 'CurrentCharacter'); | ||
117 | button = abs(get(fig, 'CurrentCharacter')); | ||
118 | scrn_pt = get(0, 'PointerLocation'); | ||
119 | set(fig,'units','pixels') | ||
120 | loc = get(fig, 'Position'); | ||
121 | pt = [scrn_pt(1) - loc(1), scrn_pt(2) - loc(2)]; | ||
122 | set(fig,'CurrentPoint',pt); | ||
123 | else | ||
124 | button = get(fig, 'SelectionType'); | ||
125 | if strcmp(button,'open') | ||
126 | button = b(length(b)); | ||
127 | elseif strcmp(button,'normal') | ||
128 | button = 1; | ||
129 | elseif strcmp(button,'extend') | ||
130 | button = 2; | ||
131 | elseif strcmp(button,'alt') | ||
132 | button = 3; | ||
133 | else | ||
134 | error('Invalid mouse selection.') | ||
135 | end | ||
136 | end | ||
137 | pt = get(gca, 'CurrentPoint'); | ||
138 | |||
139 | how_many = how_many - 1; | ||
140 | |||
141 | if(char == 13) % & how_many ~= 0) | ||
142 | % if the return key was pressed, char will == 13, | ||
143 | % and that's our signal to break out of here whether | ||
144 | % or not we have collected all the requested data | ||
145 | % points. | ||
146 | % If this was an early breakout, don't include | ||
147 | % the <Return> key info in the return arrays. | ||
148 | % We will no longer count it if it's the last input. | ||
149 | break; | ||
150 | end | ||
151 | |||
152 | out1 = [out1;pt(1,1)]; | ||
153 | y = [y;pt(1,2)]; | ||
154 | b = [b;button]; | ||
155 | end | ||
156 | end | ||
157 | |||
158 | set(fig,'pointer',pointer,'units',fig_units); | ||
159 | |||
160 | if nargout > 1 | ||
161 | out2 = y; | ||
162 | if nargout > 2 | ||
163 | out3 = b; | ||
164 | end | ||
165 | else | ||
166 | out1 = [out1 y]; | ||
167 | end | ||
168 | |||
169 | end | ||
170 | |||
171 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
172 | function key = wfbp | ||
173 | %WFBP Replacement for WAITFORBUTTONPRESS that has no side effects. | ||
174 | |||
175 | % Remove figure button functions | ||
176 | fprops = {'windowbuttonupfcn','buttondownfcn', ... | ||
177 | 'windowbuttondownfcn','windowbuttonmotionfcn'}; | ||
178 | fig = gcf; | ||
179 | fvals = get(fig,fprops); | ||
180 | set(fig,fprops,{'','','',''}) | ||
181 | |||
182 | % Remove all other buttondown functions | ||
183 | ax = findobj(fig,'type','axes'); | ||
184 | if isempty(ax) | ||
185 | ch = {}; | ||
186 | else | ||
187 | ch = get(ax,{'Children'}); | ||
188 | end | ||
189 | for i=1:length(ch), | ||
190 | ch{i} = ch{i}(:)'; | ||
191 | end | ||
192 | h = [ax(:)',ch{:}]; | ||
193 | vals = get(h,{'buttondownfcn'}); | ||
194 | mt = repmat({''},size(vals)); | ||
195 | set(h,{'buttondownfcn'},mt); | ||
196 | |||
197 | % Now wait for that buttonpress, and check for error conditions | ||
198 | waserr = 0; | ||
199 | eval(['if nargout==0,', ... | ||
200 | ' waitforbuttonpress,', ... | ||
201 | 'else,', ... | ||
202 | ' keydown = waitforbuttonpress;',... | ||
203 | 'end' ], 'waserr = 1;'); | ||
204 | |||
205 | % Put everything back | ||
206 | if(ishandle(fig)) | ||
207 | set(fig,fprops,fvals) | ||
208 | set(h,{'buttondownfcn'},vals) | ||
209 | end | ||
210 | |||
211 | if(waserr == 1) | ||
212 | error('Interrupted'); | ||
213 | end | ||
214 | |||
215 | if nargout>0, key = keydown; end | ||
216 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim.m new file mode 100755 index 0000000..6eb1c82 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim.m | |||
@@ -0,0 +1,60 @@ | |||
1 | %go_calib_optim | ||
2 | % | ||
3 | %Main calibration function. Computes the intrinsic andextrinsic parameters. | ||
4 | %Runs as a script. | ||
5 | % | ||
6 | %INPUT: x_1,x_2,x_3,...: Feature locations on the images | ||
7 | % X_1,X_2,X_3,...: Corresponding grid coordinates | ||
8 | % | ||
9 | %OUTPUT: fc: Camera focal length | ||
10 | % cc: Principal point coordinates | ||
11 | % kc: Distortion coefficients | ||
12 | % KK: The camera matrix (containing fc and cc) | ||
13 | % omc_1,omc_2,omc_3,...: 3D rotation vectors attached to the grid positions in space | ||
14 | % Tc_1,Tc_2,Tc_3,...: 3D translation vectors attached to the grid positions in space | ||
15 | % Rc_1,Rc_2,Rc_3,...: 3D rotation matrices corresponding to the omc vectors | ||
16 | % | ||
17 | %Method: Minimizes the pixel reprojection error in the least squares sense over the intrinsic | ||
18 | % camera parameters, and the extrinsic parameters (3D locations of the grids in space) | ||
19 | % | ||
20 | %Note: If the intrinsic camera parameters (fc, cc, kc) do not exist before, they are initialized through | ||
21 | % the function init_intrinsic_param.m. Otherwise, the variables in memory are used as initial guesses. | ||
22 | % | ||
23 | %Note: The row vector active_images consists of zeros and ones. To deactivate an image, set the | ||
24 | % corresponding entry in the active_images vector to zero. | ||
25 | % | ||
26 | %VERY IMPORTANT: This function works for 2D and 3D calibration rigs, except for init_intrinsic_param.m | ||
27 | %that is so far implemented to work only with 2D rigs. | ||
28 | %In the future, a more general function will be there. | ||
29 | %For now, if using a 3D calibration rig, set quick_init to 1 for an easy initialization of the focal length | ||
30 | |||
31 | |||
32 | desactivated_images = []; | ||
33 | |||
34 | |||
35 | go_calib_optim_iter; | ||
36 | |||
37 | |||
38 | if ~isempty(desactivated_images), | ||
39 | |||
40 | param_list_save = param_list; | ||
41 | |||
42 | fprintf(1,'\nNew optimization including the images that have been deactivated during the previous optimization.\n'); | ||
43 | active_images(desactivated_images) = ones(1,length(desactivated_images)); | ||
44 | desactivated_images = []; | ||
45 | |||
46 | go_calib_optim_iter; | ||
47 | |||
48 | if ~isempty(desactivated_images), | ||
49 | fprintf(1,['List of images left desactivated: ' num2str(desactivated_images) '\n' ] ); | ||
50 | end; | ||
51 | |||
52 | param_list = [param_list_save(:,1:end-1) param_list]; | ||
53 | |||
54 | end; | ||
55 | |||
56 | |||
57 | %%%%%%%%%%%%%%%%%%%% GRAPHICAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%% | ||
58 | |||
59 | %graphout_calib; | ||
60 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim3D.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim3D.m new file mode 100755 index 0000000..8cc5e30 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim3D.m | |||
@@ -0,0 +1,264 @@ | |||
1 | % Simplified version of go_calib.m | ||
2 | |||
3 | |||
4 | if ~exist('x_1'), | ||
5 | click_calib; | ||
6 | end; | ||
7 | |||
8 | |||
9 | fprintf(1,'\nMain calibration procedure\n'); | ||
10 | |||
11 | % initial guess for principal point and distortion: | ||
12 | c_init = [nx;ny]/2 - 0.5; % initialize at the center of the image | ||
13 | k_init = [0;0;0;0]; % initialize to zero (no distortion) | ||
14 | |||
15 | |||
16 | % Compute explicitely the focal lentgh using all the (mutually orthogonal) vanishing points | ||
17 | % note: The vanihing points are hidden in the planar collineations H_kk | ||
18 | |||
19 | A = []; | ||
20 | b = []; | ||
21 | |||
22 | % matrix that subtract the principal point: | ||
23 | Sub_cc = [1 0 -c_init(1);0 1 -c_init(2);0 0 1]; | ||
24 | |||
25 | |||
26 | for kk=1:n_ima, | ||
27 | |||
28 | % left Pattern: | ||
29 | |||
30 | eval(['Hlkk = Hl_' num2str(kk) ';']); | ||
31 | |||
32 | Hlkk = Sub_cc * Hlkk; | ||
33 | |||
34 | % Extract vanishing points (direct and diagonals): | ||
35 | |||
36 | Vl_hori_pix = Hlkk(:,1); | ||
37 | Vl_vert_pix = Hlkk(:,2); | ||
38 | Vl_diag1_pix = (Hlkk(:,1)+Hlkk(:,2))/2; | ||
39 | Vl_diag2_pix = (Hlkk(:,1)-Hlkk(:,2))/2; | ||
40 | |||
41 | Vl_hori_pix = Vl_hori_pix/norm(Vl_hori_pix); | ||
42 | Vl_vert_pix = Vl_vert_pix/norm(Vl_vert_pix); | ||
43 | Vl_diag1_pix = Vl_diag1_pix/norm(Vl_diag1_pix); | ||
44 | Vl_diag2_pix = Vl_diag2_pix/norm(Vl_diag2_pix); | ||
45 | |||
46 | al1 = Vl_hori_pix(1); | ||
47 | bl1 = Vl_hori_pix(2); | ||
48 | cl1 = Vl_hori_pix(3); | ||
49 | |||
50 | al2 = Vl_vert_pix(1); | ||
51 | bl2 = Vl_vert_pix(2); | ||
52 | cl2 = Vl_vert_pix(3); | ||
53 | |||
54 | al3 = Vl_diag1_pix(1); | ||
55 | bl3 = Vl_diag1_pix(2); | ||
56 | cl3 = Vl_diag1_pix(3); | ||
57 | |||
58 | al4 = Vl_diag2_pix(1); | ||
59 | bl4 = Vl_diag2_pix(2); | ||
60 | cl4 = Vl_diag2_pix(3); | ||
61 | |||
62 | % right Pattern: | ||
63 | |||
64 | eval(['Hrkk = Hr_' num2str(kk) ';']); | ||
65 | |||
66 | Hrkk = Sub_cc * Hrkk; | ||
67 | |||
68 | % Extract vanishing points (direct and diagonals): | ||
69 | |||
70 | Vr_hori_pix = Hrkk(:,1); | ||
71 | Vr_vert_pix = Hrkk(:,2); | ||
72 | Vr_diag1_pix = (Hrkk(:,1)+Hrkk(:,2))/2; | ||
73 | Vr_diag2_pix = (Hrkk(:,1)-Hrkk(:,2))/2; | ||
74 | |||
75 | Vr_hori_pix = Vr_hori_pix/norm(Vl_hori_pix); | ||
76 | Vr_vert_pix = Vr_vert_pix/norm(Vl_vert_pix); | ||
77 | Vr_diag1_pix = Vr_diag1_pix/norm(Vr_diag1_pix); | ||
78 | Vr_diag2_pix = Vr_diag2_pix/norm(Vr_diag2_pix); | ||
79 | |||
80 | ar1 = Vr_hori_pix(1); | ||
81 | br1 = Vr_hori_pix(2); | ||
82 | cr1 = Vr_hori_pix(3); | ||
83 | |||
84 | ar2 = Vr_vert_pix(1); | ||
85 | br2 = Vr_vert_pix(2); | ||
86 | cr2 = Vr_vert_pix(3); | ||
87 | |||
88 | ar3 = Vr_diag1_pix(1); | ||
89 | br3 = Vr_diag1_pix(2); | ||
90 | cr3 = Vr_diag1_pix(3); | ||
91 | |||
92 | ar4 = Vr_diag2_pix(1); | ||
93 | br4 = Vr_diag2_pix(2); | ||
94 | cr4 = Vr_diag2_pix(3); | ||
95 | |||
96 | |||
97 | % Collect all the constraints: | ||
98 | |||
99 | A_kk = [al1*al2 bl1*bl2; | ||
100 | al3*al4 bl3*bl4; | ||
101 | ar1*ar2 br1*br2; | ||
102 | ar3*ar4 br3*br4; | ||
103 | al1*ar1 bl1*br1]; | ||
104 | |||
105 | b_kk = -[cl1*cl2;cl3*cl4;cr1*cr2;cr3*cr4;cl1*cr1]; | ||
106 | |||
107 | |||
108 | A = [A;A_kk]; | ||
109 | b = [b;b_kk]; | ||
110 | |||
111 | end; | ||
112 | |||
113 | % use all the vanishing points to estimate focal length: | ||
114 | |||
115 | f_init = sqrt(abs(1./(inv(A'*A)*A'*b))); % if using a two-focal model for initial guess | ||
116 | |||
117 | %f_init = sqrt(b'*(sum(A')') / (b'*b)) * ones(2,1); % if single focal length model is used | ||
118 | |||
119 | |||
120 | % Global calibration matrix (initial guess): | ||
121 | |||
122 | KK = [f_init(1) 0 c_init(1);0 f_init(2) c_init(2); 0 0 1]; | ||
123 | inv_KK = inv(KK); | ||
124 | |||
125 | |||
126 | % Computing of the extrinsic parameters (from the collineations) | ||
127 | |||
128 | for kk = 1:n_ima, | ||
129 | |||
130 | eval(['Hlkk = Hl_' num2str(kk) ';']); | ||
131 | |||
132 | Hl2 = inv_KK*Hlkk; | ||
133 | |||
134 | sc = mean([norm(Hl2(:,1));norm(Hl2(:,2))]); | ||
135 | |||
136 | Hl2 = Hl2/sc; | ||
137 | |||
138 | eval(['Hrkk = Hr_' num2str(kk) ';']); | ||
139 | |||
140 | Hr2 = inv_KK*Hrkk; | ||
141 | |||
142 | sc = mean([norm(Hr2(:,1));norm(Hr2(:,2))]); | ||
143 | |||
144 | Hr2 = Hr2/sc; | ||
145 | |||
146 | omcl = rodrigues([Hl2(:,1:2) cross(Hl2(:,1),Hl2(:,2))]); | ||
147 | Tcl = Hl2(:,3); | ||
148 | |||
149 | %omcr = rodrigues([Hr2(:,1:2) cross(Hr2(:,1),Hr2(:,2))]); | ||
150 | %Tcr = Hr2(:,3); | ||
151 | |||
152 | |||
153 | omckk = omcl; %rodrigues([H2(:,1:2) cross(H2(:,1),H2(:,2))]); | ||
154 | Tckk = Tcl; %H2(:,3); | ||
155 | |||
156 | eval(['omc_' num2str(kk) ' = omckk;']); | ||
157 | eval(['Tc_' num2str(kk) ' = Tckk;']); | ||
158 | |||
159 | end; | ||
160 | |||
161 | |||
162 | |||
163 | % Initialisation of the parameters for global minimization: | ||
164 | |||
165 | init_param = [f_init;k_init]; | ||
166 | |||
167 | for kk = 1:n_ima, | ||
168 | eval(['init_param = [init_param; omc_' num2str(kk) '; Tc_' num2str(kk) '];']); | ||
169 | end; | ||
170 | |||
171 | if ~exist('lsqnonlin'), | ||
172 | |||
173 | options = [1 1e-4 1e-4 1e-6 0 0 0 0 0 0 0 0 0 6000 0 1e-8 0.1 0]; | ||
174 | |||
175 | if exist('leastsq'), | ||
176 | sss = ['[param,opt] = leastsq(''multi_error_oulu'',init_param,options,[],n_ima,c_init);']; | ||
177 | else | ||
178 | sss = ['[param,opt] = leastsq2(''multi_error_oulu'',init_param,options,[],n_ima,c_init);']; | ||
179 | end; | ||
180 | |||
181 | else | ||
182 | |||
183 | options = optimset('lsqnonlin'); | ||
184 | options.MaxIter = 6000; | ||
185 | options.Display = 'iter'; | ||
186 | sss = ['[param,opt] = lsqnonlin(''multi_error_oulu'',init_param,[],[],options,n_ima,c_init);']; | ||
187 | |||
188 | end; | ||
189 | |||
190 | |||
191 | fprintf(1,'\nOptimization not including the principal point...\n') | ||
192 | eval(sss); | ||
193 | |||
194 | history = [[init_param;c_init] [param;c_init]]; | ||
195 | |||
196 | sol_no_center = [param;c_init]; | ||
197 | |||
198 | init_param = sol_no_center; | ||
199 | |||
200 | fprintf(1,'\nOptimization including the principal point...\n') | ||
201 | |||
202 | eval(sss); | ||
203 | |||
204 | history = [history param]; | ||
205 | |||
206 | |||
207 | sol_with_center = param; | ||
208 | |||
209 | |||
210 | |||
211 | |||
212 | %%% Extraction of the final intrinsic and extrinsic paramaters (in the no-center case): | ||
213 | |||
214 | solution = sol_no_center; | ||
215 | extract_parameters3D; | ||
216 | |||
217 | fprintf(1,'\n\nCalibration results without principal point estimation:\n\n'); | ||
218 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
219 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
220 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
221 | fprintf(1,['Pixel error: [click on ''sol. without center'']\n']); | ||
222 | |||
223 | |||
224 | |||
225 | |||
226 | % Pick the solution with principal point | ||
227 | %%% NOTE: At that point, the user can choose which solution to pick: with or without | ||
228 | %%% principal point estimation. By default, we pick the solution with principal point. | ||
229 | |||
230 | solution = sol_with_center; | ||
231 | |||
232 | |||
233 | |||
234 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
235 | |||
236 | extract_parameters3D; | ||
237 | |||
238 | |||
239 | fprintf(1,'\n\nCalibration results with principal point estimation:\n\n'); | ||
240 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
241 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
242 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
243 | |||
244 | |||
245 | %%%%%%%%%%%%%%%%%%%% GRAPHICAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%% | ||
246 | |||
247 | graphout_calib3D; | ||
248 | |||
249 | |||
250 | |||
251 | fprintf(1,'Note: If the solution is not satisfactory, select solution without center estimation.\n\n'); | ||
252 | |||
253 | |||
254 | %%%%%%%%%%%%%% Save all the Calibration results: | ||
255 | |||
256 | disp('Save calibration results under Calib_Results.mat'); | ||
257 | |||
258 | string_save = 'save Calib_Results fc kc cc ex x y solution sol_with_center sol_no_center history wintx winty n_ima type_numbering N_slots small_calib_image first_num image_numbers format_image calib_name Hcal Wcal nx ny map dX_default dY_default KK inv_KK dX dY'; | ||
259 | |||
260 | for kk = 1:n_ima, | ||
261 | string_save = [string_save ' X_' num2str(kk) ' x_' num2str(kk) ' y_' num2str(kk) ' ex_' num2str(kk) ' omc_' num2str(kk) ' Tc_' num2str(kk) ' Hl_' num2str(kk) ' nl_sq_x_' num2str(kk) ' nl_sq_y_' num2str(kk) ' Hr_' num2str(kk) ' nr_sq_x_' num2str(kk) ' nr_sq_y_' num2str(kk)]; | ||
262 | end; | ||
263 | |||
264 | eval(string_save); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim_cont.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim_cont.m new file mode 100755 index 0000000..9ff3f0b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim_cont.m | |||
@@ -0,0 +1,142 @@ | |||
1 | % Simplified version of go_calib.m | ||
2 | |||
3 | if ~exist('x_1'), | ||
4 | click_calib; | ||
5 | end; | ||
6 | |||
7 | |||
8 | % Initialisation of the parameters for global minimization: | ||
9 | |||
10 | init_param = [fc;kc]; | ||
11 | |||
12 | for kk = 1:n_ima, | ||
13 | |||
14 | if ~exist(['omc_' num2str(kk)]), | ||
15 | eval(['Hkk = H_' num2str(kk) ';']); | ||
16 | H2 = inv_KK*Hkk; | ||
17 | sc = mean([norm(H2(:,1));norm(H2(:,2))]); | ||
18 | H2 = H2/sc; | ||
19 | omckk = rodrigues([H2(:,1:2) cross(H2(:,1),H2(:,2))]); | ||
20 | Tckk = H2(:,3); | ||
21 | eval(['omc_' num2str(kk) ' = omckk;']); | ||
22 | eval(['Tc_' num2str(kk) ' = Tckk;']); | ||
23 | end; | ||
24 | |||
25 | eval(['init_param = [init_param; omc_' num2str(kk) '; Tc_' num2str(kk) '];']); | ||
26 | end; | ||
27 | |||
28 | init_param = [init_param;cc]; | ||
29 | |||
30 | |||
31 | |||
32 | %-------------------- Main Optimization: | ||
33 | |||
34 | fprintf(1,'\nRe-Optimization...\n') | ||
35 | |||
36 | |||
37 | param = init_param; | ||
38 | change = 1; | ||
39 | |||
40 | iter = 0; | ||
41 | |||
42 | fprintf(1,'Iteration '); | ||
43 | |||
44 | while (change > 1e-6)&(iter < 10), | ||
45 | |||
46 | fprintf(1,'%d...',iter+1); | ||
47 | |||
48 | JJ = []; | ||
49 | ex = []; | ||
50 | |||
51 | c = param(6*n_ima + 4 + 3:6*n_ima + 5 + 3); | ||
52 | f = param(1:2); | ||
53 | k = param(3:6); | ||
54 | |||
55 | for kk = 1:n_ima, | ||
56 | |||
57 | omckk = param(4+6*(kk-1) + 3:6*kk + 3); | ||
58 | |||
59 | Tckk = param(6*kk+1 + 3:6*kk+3 + 3); | ||
60 | |||
61 | eval(['X_kk = X_' num2str(kk) ';']); | ||
62 | eval(['x_kk = x_' num2str(kk) ';']); | ||
63 | |||
64 | Np = size(X_kk,2); | ||
65 | |||
66 | JJkk = zeros(2*Np,n_ima * 6 + 8); | ||
67 | |||
68 | [x,dxdom,dxdT,dxdf,dxdc,dxdk] = project_points(X_kk,omckk,Tckk,f,c,k); | ||
69 | |||
70 | exkk = x_kk - x; | ||
71 | |||
72 | ex = [ex;exkk(:)]; | ||
73 | |||
74 | JJkk(:,1:2) = dxdf; | ||
75 | JJkk(:,3:6) = dxdk; | ||
76 | JJkk(:,4+6*(kk-1) + 3:6*kk + 3) = dxdom; | ||
77 | JJkk(:,6*kk+1 + 3:6*kk+3 + 3) = dxdT; | ||
78 | JJkk(:,6*n_ima + 4 + 3:6*n_ima + 5 + 3) = dxdc; | ||
79 | |||
80 | JJ = [JJ;JJkk]; | ||
81 | |||
82 | end; | ||
83 | |||
84 | param_innov = inv(JJ'*JJ)*(JJ')*ex; | ||
85 | param_up = param + param_innov; | ||
86 | change = norm(param_innov)/norm(param_up); | ||
87 | param = param_up; | ||
88 | iter = iter + 1; | ||
89 | |||
90 | end; | ||
91 | |||
92 | fprintf(1,'\n'); | ||
93 | |||
94 | |||
95 | sol_with_center = param; | ||
96 | |||
97 | solution = sol_with_center; | ||
98 | |||
99 | |||
100 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
101 | |||
102 | extract_parameters; | ||
103 | comp_error_calib; | ||
104 | |||
105 | fprintf(1,'\n\nCalibration results with principal point estimation:\n\n'); | ||
106 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
107 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
108 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
109 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
110 | |||
111 | |||
112 | %%%%%%%%%%%%%%%%%%%% GRAPHICAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%% | ||
113 | |||
114 | graphout_calib; | ||
115 | |||
116 | |||
117 | |||
118 | fprintf(1,'Note: If the solution is not satisfactory, select solution without center estimation.\n\n'); | ||
119 | |||
120 | |||
121 | %%%%%%%%%%%%%% Save all the Calibration results: | ||
122 | |||
123 | disp('Save calibration results under Calib_Results.mat'); | ||
124 | |||
125 | string_save = 'save Calib_Results fc kc cc ex x y solution sol_with_center solution_init history wintx winty n_ima type_numbering N_slots small_calib_image first_num image_numbers format_image calib_name Hcal Wcal nx ny map dX_default dY_default KK inv_KK dX dY'; | ||
126 | |||
127 | for kk = 1:n_ima, | ||
128 | string_save = [string_save ' X_' num2str(kk) ' x_' num2str(kk) ' y_' num2str(kk) ' ex_' num2str(kk) ' omc_' num2str(kk) ' Rc_' num2str(kk) ' Tc_' num2str(kk) ' H_' num2str(kk) ' Hini_' num2str(kk) ' n_sq_x_' num2str(kk) ' n_sq_y_' num2str(kk) ' wintx_' num2str(kk) ' winty_' num2str(kk) ' dX_' num2str(kk) ' dY_' num2str(kk)]; | ||
129 | end; | ||
130 | |||
131 | eval(string_save); | ||
132 | |||
133 | return; | ||
134 | |||
135 | if exist('calib_data.mat'), | ||
136 | ccc = computer; | ||
137 | if ccc(1)=='P', | ||
138 | eval('!del calib_data.mat'); | ||
139 | else | ||
140 | eval('!rm calib_data.mat'); | ||
141 | end; | ||
142 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim_iter.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim_iter.m new file mode 100755 index 0000000..a076214 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/go_calib_optim_iter.m | |||
@@ -0,0 +1,332 @@ | |||
1 | %go_calib_optim_iter | ||
2 | % | ||
3 | %Main calibration function. Computes the intrinsic andextrinsic parameters. | ||
4 | %Runs as a script. | ||
5 | % | ||
6 | %INPUT: x_1,x_2,x_3,...: Feature locations on the images | ||
7 | % X_1,X_2,X_3,...: Corresponding grid coordinates | ||
8 | % | ||
9 | %OUTPUT: fc: Camera focal length | ||
10 | % cc: Principal point coordinates | ||
11 | % kc: Distortion coefficients | ||
12 | % KK: The camera matrix (containing fc and cc) | ||
13 | % omc_1,omc_2,omc_3,...: 3D rotation vectors attached to the grid positions in space | ||
14 | % Tc_1,Tc_2,Tc_3,...: 3D translation vectors attached to the grid positions in space | ||
15 | % Rc_1,Rc_2,Rc_3,...: 3D rotation matrices corresponding to the omc vectors | ||
16 | % | ||
17 | %Method: Minimizes the pixel reprojection error in the least squares sense over the intrinsic | ||
18 | % camera parameters, and the extrinsic parameters (3D locations of the grids in space) | ||
19 | % | ||
20 | %Note: If the intrinsic camera parameters (fc, cc, kc) do not exist before, they are initialized through | ||
21 | % the function init_intrinsic_param.m. Otherwise, the variables in memory are used as initial guesses. | ||
22 | % | ||
23 | %Note: The row vector active_images consists of zeros and ones. To deactivate an image, set the | ||
24 | % corresponding entry in the active_images vector to zero. | ||
25 | % | ||
26 | %VERY IMPORTANT: This function works for 2D and 3D calibration rigs, except for init_intrinsic_param.m | ||
27 | %that is so far implemented to work only with 2D rigs. | ||
28 | %In the future, a more general function will be there. | ||
29 | %For now, if using a 3D calibration rig, quick_init is set to 1 for an easy initialization of the focal length | ||
30 | |||
31 | |||
32 | check_active_images; | ||
33 | |||
34 | |||
35 | |||
36 | quick_init = 0; % Set to 1 for using a quick init (necessary when using 3D rigs) | ||
37 | |||
38 | |||
39 | |||
40 | % Check 3D-ness of the calibration rig: | ||
41 | rig3D = 0; | ||
42 | for kk = ind_active, | ||
43 | eval(['X_kk = X_' num2str(kk) ';']); | ||
44 | if is3D(X_kk), | ||
45 | rig3D = 1; | ||
46 | end; | ||
47 | end; | ||
48 | |||
49 | |||
50 | |||
51 | % If the rig is 3D, then no choice: the only valid initialization is manual! | ||
52 | if rig3D, | ||
53 | quick_init = 1; | ||
54 | end; | ||
55 | |||
56 | |||
57 | |||
58 | |||
59 | alpha = 0.4; % set alpha = 1; for steepest gradient descent | ||
60 | |||
61 | |||
62 | % Conditioning threshold for view rejection | ||
63 | thresh_cond = 1e6; | ||
64 | |||
65 | |||
66 | |||
67 | |||
68 | %% Initialization of the intrinsic parameters (if necessary) | ||
69 | |||
70 | if ~exist('cc'), | ||
71 | fprintf(1,'Initialization of the principal point at the center of the image.\n'); | ||
72 | cc = [(nx-1)/2;(ny-1)/2]; | ||
73 | end; | ||
74 | |||
75 | |||
76 | if ~exist('kc'), | ||
77 | fprintf(1,'Initialization of the image distortion to zero.\n'); | ||
78 | kc = zeros(4,1); | ||
79 | end; | ||
80 | |||
81 | |||
82 | if ~exist('fc')& quick_init, | ||
83 | FOV_angle = 35; % Initial camera field of view in degrees | ||
84 | fprintf(1,['Initialization of the focal length to a FOV of ' num2str(FOV_angle) ' degrees.\n']); | ||
85 | fc = (nx/2)/tan(pi*FOV_angle/360) * ones(2,1); | ||
86 | end; | ||
87 | |||
88 | |||
89 | if ~exist('fc'), | ||
90 | % Initialization of the intrinsic parameters: | ||
91 | fprintf(1,'Initialization of the intrinsic parameters using the vanishing points of planar patterns.\n') | ||
92 | init_intrinsic_param; % The right way to go (if quick_init is not active)! | ||
93 | end; | ||
94 | |||
95 | |||
96 | |||
97 | %% Initialization of the extrinsic parameters for global minimization: | ||
98 | |||
99 | init_param = [fc;kc]; | ||
100 | |||
101 | for kk = 1:n_ima, | ||
102 | |||
103 | if exist(['x_' num2str(kk)]), | ||
104 | |||
105 | eval(['x_kk = x_' num2str(kk) ';']); | ||
106 | eval(['X_kk = X_' num2str(kk) ';']); | ||
107 | |||
108 | if (isnan(x_kk(1,1))), | ||
109 | if active_images(kk), | ||
110 | fprintf(1,'Warning: Cannot calibrate with image %d. Need to extract grid corners first.\n',kk) | ||
111 | fprintf(1,' Set active_images(%d)=1; and run Extract grid corners.\n',kk) | ||
112 | end; | ||
113 | active_images(kk) = 0; | ||
114 | end; | ||
115 | if active_images(kk), | ||
116 | [omckk,Tckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc); | ||
117 | [omckk,Tckk,Rckk,JJ_kk] = compute_extrinsic_refine(omckk,Tckk,x_kk,X_kk,fc,cc,kc,20,thresh_cond); | ||
118 | if cond(JJ_kk)> thresh_cond, | ||
119 | active_images(kk) = 0; | ||
120 | omckk = NaN*ones(3,1); | ||
121 | Tckk = NaN*ones(3,1); | ||
122 | fprintf(1,'\nWarning: View #%d ill-conditioned. This image is now set inactive.\n',kk) | ||
123 | desactivated_images = [desactivated_images kk]; | ||
124 | end; | ||
125 | if isnan(omckk(1,1)), | ||
126 | %fprintf(1,'\nWarning: Desactivating image %d. Re-activate it later by typing:\nactive_images(%d)=1;\nand re-run optimization\n',[kk kk]) | ||
127 | active_images(kk) = 0; | ||
128 | end; | ||
129 | else | ||
130 | omckk = NaN*ones(3,1); | ||
131 | Tckk = NaN*ones(3,1); | ||
132 | end; | ||
133 | |||
134 | else | ||
135 | |||
136 | omckk = NaN*ones(3,1); | ||
137 | Tckk = NaN*ones(3,1); | ||
138 | |||
139 | if active_images(kk), | ||
140 | fprintf(1,'Warning: Cannot calibrate with image %d. Need to extract grid corners first.\n',kk) | ||
141 | fprintf(1,' Set active_images(%d)=1; and run Extract grid corners.\n',kk) | ||
142 | end; | ||
143 | |||
144 | active_images(kk) = 0; | ||
145 | |||
146 | end; | ||
147 | |||
148 | eval(['omc_' num2str(kk) ' = omckk;']); | ||
149 | eval(['Tc_' num2str(kk) ' = Tckk;']); | ||
150 | |||
151 | eval(['init_param = [init_param; omc_' num2str(kk) '; Tc_' num2str(kk) '];']); | ||
152 | |||
153 | end; | ||
154 | |||
155 | |||
156 | check_active_images; | ||
157 | |||
158 | init_param = [init_param;cc]; | ||
159 | |||
160 | %-------------------- Main Optimization: | ||
161 | |||
162 | fprintf(1,'\nMain calibration optimization procedure - Number of images: %d\n',length(ind_active)); | ||
163 | |||
164 | |||
165 | % The following vector helps to select the variables to update: | ||
166 | ind_Jac = find([ones(6,1);reshape(ones(6,1)*active_images,6*n_ima,1);ones(2,1)])'; | ||
167 | |||
168 | param = init_param; | ||
169 | change = 1; | ||
170 | |||
171 | iter = 0; | ||
172 | |||
173 | fprintf(1,'Gradient descent iterations: '); | ||
174 | |||
175 | param_list = param; | ||
176 | |||
177 | MaxIter = 30; | ||
178 | |||
179 | |||
180 | while (change > 1e-6)&(iter < MaxIter), | ||
181 | |||
182 | fprintf(1,'%d...',iter+1); | ||
183 | |||
184 | |||
185 | %% The first step consists of updating the whole vector of knowns (intrinsic + extrinsic of active | ||
186 | %% images) through a one step steepest gradient descent. | ||
187 | |||
188 | JJ = []; | ||
189 | ex = []; | ||
190 | |||
191 | c = param(6*n_ima + 4 + 3:6*n_ima + 5 + 3); | ||
192 | f = param(1:2); | ||
193 | k = param(3:6); | ||
194 | |||
195 | for kk = 1:n_ima, | ||
196 | |||
197 | if active_images(kk), | ||
198 | |||
199 | omckk = param(4+6*(kk-1) + 3:6*kk + 3); | ||
200 | |||
201 | Tckk = param(6*kk+1 + 3:6*kk+3 + 3); | ||
202 | |||
203 | if isnan(omckk(1)), | ||
204 | fprintf(1,'Intrinsic parameters at frame %d do not exist\n',kk); | ||
205 | return; | ||
206 | end; | ||
207 | |||
208 | eval(['X_kk = X_' num2str(kk) ';']); | ||
209 | eval(['x_kk = x_' num2str(kk) ';']); | ||
210 | |||
211 | Np = size(X_kk,2); | ||
212 | |||
213 | JJkk = zeros(2*Np,n_ima * 6 + 8); | ||
214 | |||
215 | [x,dxdom,dxdT,dxdf,dxdc,dxdk] = project_points(X_kk,omckk,Tckk,f,c,k); | ||
216 | |||
217 | exkk = x_kk - x; | ||
218 | |||
219 | ex = [ex;exkk(:)]; | ||
220 | |||
221 | JJkk(:,1:2) = dxdf; | ||
222 | JJkk(:,3:6) = dxdk; | ||
223 | JJkk(:,4+6*(kk-1) + 3:6*kk + 3) = dxdom; | ||
224 | JJkk(:,6*kk+1 + 3:6*kk+3 + 3) = dxdT; | ||
225 | JJkk(:,6*n_ima + 4 + 3:6*n_ima + 5 + 3) = dxdc; | ||
226 | |||
227 | JJ = [JJ;JJkk]; | ||
228 | |||
229 | |||
230 | % Check if this view is ill-conditioned: | ||
231 | JJ_kk = [dxdom dxdT]; | ||
232 | if cond(JJ_kk)> thresh_cond, | ||
233 | active_images(kk) = 0; | ||
234 | fprintf(1,'\nWarning: View #%d ill-conditioned. This image is now set inactive.\n',kk) | ||
235 | desactivated_images = [desactivated_images kk]; | ||
236 | param(4+6*(kk-1) + 3:6*kk+3 + 3) = NaN*ones(6,1); | ||
237 | end; | ||
238 | |||
239 | |||
240 | end; | ||
241 | |||
242 | end; | ||
243 | |||
244 | |||
245 | % List of active images (necessary if changed): | ||
246 | check_active_images; | ||
247 | ind_Jac = find([ones(6,1);reshape(ones(6,1)*active_images,6*n_ima,1);ones(2,1)])'; | ||
248 | |||
249 | |||
250 | JJ = JJ(:,ind_Jac); | ||
251 | |||
252 | JJ2 = JJ'*JJ; | ||
253 | |||
254 | |||
255 | % Smoothing coefficient: | ||
256 | |||
257 | alpha2 = 1-(1-alpha)^(iter+1); %set to 1 to undo any smoothing! | ||
258 | |||
259 | |||
260 | param_innov = alpha2*inv(JJ2)*(JJ')*ex; | ||
261 | param_up = param(ind_Jac) + param_innov; | ||
262 | param(ind_Jac) = param_up; | ||
263 | |||
264 | |||
265 | % New intrinsic parameters: | ||
266 | |||
267 | fc_current = param(1:2); | ||
268 | cc_current = param(6*n_ima + 4 + 3:6*n_ima + 5 + 3); | ||
269 | kc_current = param(3:6); | ||
270 | |||
271 | |||
272 | % Change on the intrinsic parameters: | ||
273 | change = norm([fc_current;cc_current] - [f;c])/norm([fc_current;cc_current]); | ||
274 | |||
275 | |||
276 | %% Second step: (optional) - It makes convergence faster, and the region of convergence LARGER!!! | ||
277 | %% Recompute the extrinsic parameters only using compute_extrinsic.m (this may be useful sometimes) | ||
278 | %% The complete gradient descent method is useful to precisely update the intrinsic parameters. | ||
279 | |||
280 | MaxIter2 = 20; | ||
281 | |||
282 | for kk = 1:n_ima, | ||
283 | if active_images(kk), | ||
284 | omc_current = param(4+6*(kk-1) + 3:6*kk + 3); | ||
285 | Tc_current = param(6*kk+1 + 3:6*kk+3 + 3); | ||
286 | eval(['X_kk = X_' num2str(kk) ';']); | ||
287 | eval(['x_kk = x_' num2str(kk) ';']); | ||
288 | [omc_current,Tc_current] = compute_extrinsic_init(x_kk,X_kk,fc_current,cc_current,kc_current); | ||
289 | [omckk,Tckk,Rckk,JJ_kk] = compute_extrinsic_refine(omc_current,Tc_current,x_kk,X_kk,fc_current,cc_current,kc_current,MaxIter2,thresh_cond); | ||
290 | if cond(JJ_kk)> thresh_cond, | ||
291 | active_images(kk) = 0; | ||
292 | fprintf(1,'\nWarning: View #%d ill-conditioned. This image is now set inactive.\n',kk) | ||
293 | desactivated_images = [desactivated_images kk]; | ||
294 | omckk = NaN*ones(3,1); | ||
295 | Tckk = NaN*ones(3,1); | ||
296 | end; | ||
297 | param(4+6*(kk-1) + 3:6*kk + 3) = omckk; | ||
298 | param(6*kk+1 + 3:6*kk+3 + 3) = Tckk; | ||
299 | end; | ||
300 | end; | ||
301 | |||
302 | %fprintf(1,'\n\nCalibration results after optimization:\n\n'); | ||
303 | %fprintf(1,'focal = [%3.5f %3.5f]\n',fc_current); | ||
304 | %fprintf(1,'center = [%3.5f %3.5f]\n',cc_current); | ||
305 | %fprintf(1,'distortion = [%3.5f %3.5f %3.5f %3.5f]\n\n',kc_current); | ||
306 | |||
307 | |||
308 | param_list = [param_list param]; | ||
309 | |||
310 | iter = iter + 1; | ||
311 | |||
312 | end; | ||
313 | |||
314 | fprintf(1,'\n'); | ||
315 | |||
316 | |||
317 | sol_with_center = param; | ||
318 | |||
319 | solution = param; | ||
320 | |||
321 | |||
322 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
323 | |||
324 | extract_parameters; | ||
325 | comp_error_calib; | ||
326 | |||
327 | fprintf(1,'\n\nCalibration results after optimization:\n\n'); | ||
328 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
329 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
330 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
331 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
332 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/graphout_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/graphout_calib.m new file mode 100755 index 0000000..a3f7040 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/graphout_calib.m | |||
@@ -0,0 +1,12 @@ | |||
1 | |||
2 | |||
3 | %%%%%%%%%%%%%%%%%%%% GRAPHICAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%% | ||
4 | |||
5 | %%%%%%%%%%%%%%%%%%%% SHOW EXTRINSIC RESULTS %%%%%%%%%%%%%%%%%%%%%%%% | ||
6 | |||
7 | ext_calib; | ||
8 | |||
9 | %%%%%%%%%%%%%%%%%%%% REPROJECT ON THE IMAGES %%%%%%%%%%%%%%%%%%%%%%%% | ||
10 | |||
11 | reproject_calib; | ||
12 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/graphout_calib3D.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/graphout_calib3D.m new file mode 100755 index 0000000..b7edf43 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/graphout_calib3D.m | |||
@@ -0,0 +1,153 @@ | |||
1 | |||
2 | |||
3 | %%%%%%%%%%%%%%%%%%%% GRAPHICAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%% | ||
4 | |||
5 | |||
6 | % Color code for each image: | ||
7 | |||
8 | colors = 'brgkcm'; | ||
9 | |||
10 | |||
11 | %%% Show the extrinsic parameters | ||
12 | |||
13 | IP = 8*dX*([0 nx-1 nx-1 0 0 ; 0 0 ny-1 ny-1 0;1 1 1 1 1] - [cc;0]*ones(1,5)) ./ ([fc;1]*ones(1,5)); | ||
14 | |||
15 | |||
16 | figure(4); | ||
17 | [a,b] = view; | ||
18 | |||
19 | figure(4); | ||
20 | plot3(5*[0 dX 0 0 0 0 ],5*[0 0 0 0 0 dX],-5*[0 0 0 dX 0 0 ],'b-','linewidth',2'); | ||
21 | hold on; | ||
22 | plot3(IP(1,:),IP(3,:),-IP(2,:),'r-','linewidth',2); | ||
23 | text(6*dX,0,0,'X_c'); | ||
24 | text(-dX,5*dX,0,'Z_c'); | ||
25 | text(0,0,-6*dX,'Y_c'); | ||
26 | text(-dX,-dX,dX,'O_c'); | ||
27 | |||
28 | |||
29 | for kk = 1:n_ima, | ||
30 | |||
31 | eval(['XX_kk = X_' num2str(kk) ';']); | ||
32 | eval(['omc_kk = omc_' num2str(kk) ';']); | ||
33 | eval(['Tc_kk = Tc_' num2str(kk) ';']); | ||
34 | |||
35 | eval(['nl_sq_x = nl_sq_x_' num2str(kk) ';']); | ||
36 | eval(['nl_sq_y = nl_sq_y_' num2str(kk) ';']); | ||
37 | |||
38 | eval(['nr_sq_x = nr_sq_x_' num2str(kk) ';']); | ||
39 | eval(['nr_sq_y = nr_sq_y_' num2str(kk) ';']); | ||
40 | |||
41 | R_kk = rodrigues(omc_kk); | ||
42 | |||
43 | YY_kk = R_kk * XX_kk + Tc_kk * ones(1,length(XX_kk)); | ||
44 | |||
45 | YYl_kk = YY_kk(:,1:(nl_sq_x+1)*(nl_sq_y+1)); | ||
46 | YYr_kk = YY_kk(:,(nl_sq_x+1)*(nl_sq_y+1)+1:end); | ||
47 | |||
48 | |||
49 | eval(['YYl_' num2str(kk) ' = YYl_kk;']); | ||
50 | eval(['YYr_' num2str(kk) ' = YYr_kk;']); | ||
51 | |||
52 | uu = [-dX;-dY;0]/2; | ||
53 | uu = R_kk * uu + Tc_kk; | ||
54 | |||
55 | YYlx = zeros(nl_sq_x+1,nl_sq_y+1); | ||
56 | YYly = zeros(nl_sq_x+1,nl_sq_y+1); | ||
57 | YYlz = zeros(nl_sq_x+1,nl_sq_y+1); | ||
58 | |||
59 | YYrx = zeros(nr_sq_x+1,nr_sq_y+1); | ||
60 | YYry = zeros(nr_sq_x+1,nr_sq_y+1); | ||
61 | YYrz = zeros(nr_sq_x+1,nr_sq_y+1); | ||
62 | |||
63 | YYlx(:) = YYl_kk(1,:); | ||
64 | YYly(:) = YYl_kk(2,:); | ||
65 | YYlz(:) = YYl_kk(3,:); | ||
66 | |||
67 | YYrx(:) = YYr_kk(1,:); | ||
68 | YYry(:) = YYr_kk(2,:); | ||
69 | YYrz(:) = YYr_kk(3,:); | ||
70 | |||
71 | |||
72 | %keyboard; | ||
73 | |||
74 | figure(4); | ||
75 | hhh= mesh(YYlx,YYlz,-YYly); | ||
76 | set(hhh,'edgecolor',colors(rem(kk-1,6)+1),'linewidth',1); %,'facecolor','none'); | ||
77 | %plot3(YY_kk(1,:),YY_kk(3,:),-YY_kk(2,:),['o' colors(rem(kk-1,6)+1)]); | ||
78 | hhh= mesh(YYrx,YYrz,-YYry); | ||
79 | set(hhh,'edgecolor',colors(rem(kk-1,6)+1),'linewidth',1); | ||
80 | text(uu(1),uu(3),-uu(2),num2str(kk),'fontsize',14,'color',colors(rem(kk-1,6)+1)); | ||
81 | |||
82 | end; | ||
83 | |||
84 | figure(4);rotate3d on; | ||
85 | axis('equal'); | ||
86 | title('Extrinsic parameters'); | ||
87 | %view(60,30); | ||
88 | view(a,b); | ||
89 | hold off; | ||
90 | |||
91 | |||
92 | |||
93 | % Reproject the patterns on the images, and compute the pixel errors: | ||
94 | |||
95 | % Reload the images if necessary | ||
96 | |||
97 | if ~exist('I_1'), | ||
98 | ima_read_calib; | ||
99 | if no_image_file, | ||
100 | return; | ||
101 | end; | ||
102 | end; | ||
103 | |||
104 | |||
105 | ex = []; % Global error vector | ||
106 | x = []; % Detected corners on the image plane | ||
107 | y = []; % Reprojected points | ||
108 | |||
109 | for kk = 1:n_ima, | ||
110 | |||
111 | eval(['omckk = omc_' num2str(kk) ';']); | ||
112 | eval(['Tckk = Tc_' num2str(kk) ';']); | ||
113 | |||
114 | Rkk = rodrigues(omckk); | ||
115 | |||
116 | eval(['y_' num2str(kk) ' = project2_oulu(X_' num2str(kk) ',Rkk,Tckk,fc,cc,kc);']); | ||
117 | |||
118 | eval(['ex_' num2str(kk) ' = x_' num2str(kk) ' -y_' num2str(kk) ';']); | ||
119 | |||
120 | eval(['x_kk = x_' num2str(kk) ';']); | ||
121 | |||
122 | figure(4+kk); | ||
123 | eval(['I = I_' num2str(kk) ';']); | ||
124 | image(I); hold on; | ||
125 | colormap(gray(256)); | ||
126 | title(['Image ' num2str(kk) ' - Image points (+) and reprojected grid points (o)']); | ||
127 | eval(['plot(x_' num2str(kk) '(1,:)+1,x_' num2str(kk) '(2,:)+1,''r+'');']); | ||
128 | eval(['plot(y_' num2str(kk) '(1,:)+1,y_' num2str(kk) '(2,:)+1,''' colors(rem(kk-1,6)+1) 'o'');']); | ||
129 | zoom on; | ||
130 | hold off; | ||
131 | |||
132 | |||
133 | eval(['ex = [ex ex_' num2str(kk) '];']); | ||
134 | eval(['x = [x x_' num2str(kk) '];']); | ||
135 | eval(['y = [y y_' num2str(kk) '];']); | ||
136 | |||
137 | end; | ||
138 | |||
139 | |||
140 | figure(5+n_ima); | ||
141 | for kk = 1:n_ima, | ||
142 | eval(['plot(ex_' num2str(kk) '(1,:),ex_' num2str(kk) '(2,:),''' colors(rem(kk-1,6)+1) '+'');']); | ||
143 | hold on; | ||
144 | end; | ||
145 | hold off; | ||
146 | axis('equal'); | ||
147 | title('Reprojection error (in pixel)'); | ||
148 | xlabel('x'); | ||
149 | ylabel('y'); | ||
150 | |||
151 | err_std = std(ex')'; | ||
152 | |||
153 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/ima_read_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/ima_read_calib.m new file mode 100755 index 0000000..09cef59 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/ima_read_calib.m | |||
@@ -0,0 +1,107 @@ | |||
1 | |||
2 | if ~exist('calib_name'), | ||
3 | data_calib; | ||
4 | end; | ||
5 | |||
6 | check_active_images; | ||
7 | |||
8 | images_read = active_images; | ||
9 | |||
10 | image_numbers = first_num:n_ima-1+first_num; | ||
11 | |||
12 | no_image_file = 0; | ||
13 | |||
14 | i = 1; | ||
15 | |||
16 | while (i <= n_ima), % & (~no_image_file), | ||
17 | |||
18 | if active_images(i), | ||
19 | |||
20 | %fprintf(1,'Loading image %d...\n',i); | ||
21 | |||
22 | if ~type_numbering, | ||
23 | number_ext = num2str(image_numbers(i)); | ||
24 | else | ||
25 | number_ext = sprintf(['%.' num2str(N_slots) 'd'],image_numbers(i)); | ||
26 | end; | ||
27 | |||
28 | ima_name = [calib_name number_ext '.' format_image] | ||
29 | |||
30 | if i == ind_active(1), | ||
31 | fprintf(1,'Loading image '); | ||
32 | end; | ||
33 | |||
34 | if exist(ima_name), | ||
35 | |||
36 | fprintf(1,'%d...',i); | ||
37 | |||
38 | if format_image(1) == 'p', | ||
39 | Ii = double(pgmread(ima_name)); | ||
40 | else | ||
41 | if format_image(1) == 'r', | ||
42 | Ii = readras(ima_name); | ||
43 | else | ||
44 | Ii = double(imread(ima_name)); | ||
45 | end; | ||
46 | end; | ||
47 | |||
48 | if size(Ii,3)>1, | ||
49 | Ii = Ii(:,:,2); | ||
50 | end; | ||
51 | |||
52 | eval(['I_' num2str(i) ' = Ii;']); | ||
53 | |||
54 | else | ||
55 | |||
56 | fprintf(1,'%d...',i); | ||
57 | |||
58 | images_read(i) = 0; | ||
59 | |||
60 | no_image_file = 1; | ||
61 | |||
62 | end; | ||
63 | |||
64 | end; | ||
65 | |||
66 | i = i+1; | ||
67 | |||
68 | end; | ||
69 | |||
70 | |||
71 | if no_image_file, | ||
72 | |||
73 | fprintf(1,'\nWARNING! Cannot load calibration images\n'); | ||
74 | |||
75 | else | ||
76 | |||
77 | fprintf(1,'\n'); | ||
78 | |||
79 | if size(I_1,1)~=480, | ||
80 | small_calib_image = 1; | ||
81 | else | ||
82 | small_calib_image = 0; | ||
83 | end; | ||
84 | |||
85 | [Hcal,Wcal] = size(I_1); % size of the calibration image | ||
86 | |||
87 | [ny,nx] = size(I_1); | ||
88 | |||
89 | clickname = []; | ||
90 | |||
91 | map = gray(256); | ||
92 | |||
93 | %string_save = 'save calib_data n_ima type_numbering N_slots image_numbers format_image calib_name Hcal Wcal nx ny map small_calib_image'; | ||
94 | |||
95 | %eval(string_save); | ||
96 | |||
97 | disp('done'); | ||
98 | %click_calib; | ||
99 | |||
100 | end; | ||
101 | |||
102 | if ~exist('map'), map = gray(256); end; | ||
103 | |||
104 | |||
105 | |||
106 | |||
107 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/init_calib_param.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/init_calib_param.m new file mode 100755 index 0000000..92e14be --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/init_calib_param.m | |||
@@ -0,0 +1,210 @@ | |||
1 | %init_calib_param | ||
2 | % | ||
3 | %Initialization of the intrinsic and extrinsic parameters. | ||
4 | %Runs as a script. | ||
5 | % | ||
6 | %This function is obsolete with init_intrinsic_param called from go_calib_optim | ||
7 | % | ||
8 | %INPUT: x_1,x_2,x_3,...: Feature locations on the images | ||
9 | % X_1,X_2,X_3,...: Corresponding grid coordinates | ||
10 | % | ||
11 | %OUTPUT: fc: Camera focal length | ||
12 | % cc: Principal point coordinates | ||
13 | % kc: Distortion coefficients | ||
14 | % KK: The camera matrix (containing fc and cc) | ||
15 | % omc_1,omc_2,omc_3,...: 3D rotation vectors attached to the grid positions in space | ||
16 | % Tc_1,Tc_2,Tc_3,...: 3D translation vectors attached to the grid positions in space | ||
17 | % Rc_1,Rc_2,Rc_3,...: 3D rotation matrices corresponding to the omc vectors | ||
18 | % | ||
19 | %Method: Compute the planar homographies H_1, H_2, H_3, ... and computes | ||
20 | % the focal length fc from orthogonal vanishing points constraint. | ||
21 | % The principal point cc is assumed at the center of the image. | ||
22 | % Assumes no image distortion (kc = [0;0;0;0]) | ||
23 | % Once the intrinsic parameters are estimated, the extrinsic parameters | ||
24 | % are computed for each image. | ||
25 | % | ||
26 | %Note: The row vector active_images consists of zeros and ones. To deactivate an image, set the | ||
27 | % corresponding entry in the active_images vector to zero. | ||
28 | % | ||
29 | % | ||
30 | %Important functions called within that program: | ||
31 | % | ||
32 | %compute_homography.m: Computes the planar homography between points on the grid in 3D, and the image plane. | ||
33 | % | ||
34 | %compute_extrinsic.m: Computes the location of a grid assuming known intrinsic parameters. | ||
35 | % This function is called at the initialization step. | ||
36 | |||
37 | |||
38 | |||
39 | |||
40 | check_active_images; | ||
41 | |||
42 | if ~exist(['x_' num2str(ind_active(1)) ]), | ||
43 | click_calib; | ||
44 | end; | ||
45 | |||
46 | |||
47 | fprintf(1,'\nInitialization of the calibration parameters - Number of images: %d\n',length(ind_active)); | ||
48 | |||
49 | |||
50 | % Initialize the homographies: | ||
51 | |||
52 | for kk = 1:n_ima, | ||
53 | eval(['x_kk = x_' num2str(kk) ';']); | ||
54 | eval(['X_kk = X_' num2str(kk) ';']); | ||
55 | if (isnan(x_kk(1,1))), | ||
56 | if active_images(kk), | ||
57 | fprintf(1,'WARNING: Cannot calibrate with image %d. Need to extract grid corners first.\n',kk) | ||
58 | fprintf(1,' Set active_images(%d)=1; and run Extract grid corners.\n',kk) | ||
59 | end; | ||
60 | active_images(kk) = 0; | ||
61 | end; | ||
62 | if active_images(kk), | ||
63 | eval(['H_' num2str(kk) ' = compute_homography(x_kk,X_kk(1:2,:));']); | ||
64 | else | ||
65 | eval(['H_' num2str(kk) ' = NaN*ones(3,3);']); | ||
66 | end; | ||
67 | end; | ||
68 | |||
69 | check_active_images; | ||
70 | |||
71 | % initial guess for principal point and distortion: | ||
72 | |||
73 | if ~exist('nx'), [ny,nx] = size(I); end; | ||
74 | |||
75 | c_init = [nx;ny]/2 - 0.5; % initialize at the center of the image | ||
76 | k_init = [0;0;0;0]; % initialize to zero (no distortion) | ||
77 | |||
78 | |||
79 | |||
80 | % Compute explicitely the focal length using all the (mutually orthogonal) vanishing points | ||
81 | % note: The vanihing points are hidden in the planar collineations H_kk | ||
82 | |||
83 | A = []; | ||
84 | b = []; | ||
85 | |||
86 | % matrix that subtract the principal point: | ||
87 | Sub_cc = [1 0 -c_init(1);0 1 -c_init(2);0 0 1]; | ||
88 | |||
89 | for kk=1:n_ima, | ||
90 | |||
91 | if active_images(kk), | ||
92 | |||
93 | eval(['Hkk = H_' num2str(kk) ';']); | ||
94 | |||
95 | Hkk = Sub_cc * Hkk; | ||
96 | |||
97 | % Extract vanishing points (direct and diagonals): | ||
98 | |||
99 | V_hori_pix = Hkk(:,1); | ||
100 | V_vert_pix = Hkk(:,2); | ||
101 | V_diag1_pix = (Hkk(:,1)+Hkk(:,2))/2; | ||
102 | V_diag2_pix = (Hkk(:,1)-Hkk(:,2))/2; | ||
103 | |||
104 | V_hori_pix = V_hori_pix/norm(V_hori_pix); | ||
105 | V_vert_pix = V_vert_pix/norm(V_vert_pix); | ||
106 | V_diag1_pix = V_diag1_pix/norm(V_diag1_pix); | ||
107 | V_diag2_pix = V_diag2_pix/norm(V_diag2_pix); | ||
108 | |||
109 | a1 = V_hori_pix(1); | ||
110 | b1 = V_hori_pix(2); | ||
111 | c1 = V_hori_pix(3); | ||
112 | |||
113 | a2 = V_vert_pix(1); | ||
114 | b2 = V_vert_pix(2); | ||
115 | c2 = V_vert_pix(3); | ||
116 | |||
117 | a3 = V_diag1_pix(1); | ||
118 | b3 = V_diag1_pix(2); | ||
119 | c3 = V_diag1_pix(3); | ||
120 | |||
121 | a4 = V_diag2_pix(1); | ||
122 | b4 = V_diag2_pix(2); | ||
123 | c4 = V_diag2_pix(3); | ||
124 | |||
125 | A_kk = [a1*a2 b1*b2; | ||
126 | a3*a4 b3*b4]; | ||
127 | |||
128 | b_kk = -[c1*c2;c3*c4]; | ||
129 | |||
130 | |||
131 | A = [A;A_kk]; | ||
132 | b = [b;b_kk]; | ||
133 | |||
134 | end; | ||
135 | |||
136 | end; | ||
137 | |||
138 | |||
139 | % use all the vanishing points to estimate focal length: | ||
140 | |||
141 | f_init = sqrt(abs(1./(inv(A'*A)*A'*b))); % if using a two-focal model for initial guess | ||
142 | |||
143 | %f_init = sqrt(b'*(sum(A')') / (b'*b)) * ones(2,1); % if single focal length model is used | ||
144 | |||
145 | |||
146 | % Global calibration matrix (initial guess): | ||
147 | |||
148 | KK = [f_init(1) 0 c_init(1);0 f_init(2) c_init(2); 0 0 1]; | ||
149 | inv_KK = inv(KK); | ||
150 | |||
151 | |||
152 | cc = c_init; | ||
153 | fc = f_init; | ||
154 | kc = k_init; | ||
155 | |||
156 | |||
157 | % Computing of the extrinsic parameters (from the collineations) | ||
158 | |||
159 | for kk = 1:n_ima, | ||
160 | |||
161 | if active_images(kk), | ||
162 | |||
163 | |||
164 | eval(['x_kk = x_' num2str(kk) ';']); | ||
165 | eval(['X_kk = X_' num2str(kk) ';']); | ||
166 | |||
167 | [omckk,Tckk] = compute_extrinsic(x_kk,X_kk,fc,cc,kc); | ||
168 | |||
169 | Rckk = rodrigues(omc_kk); | ||
170 | |||
171 | else | ||
172 | |||
173 | omckk = NaN*ones(3,1); | ||
174 | Tckk = NaN*ones(3,1); | ||
175 | Rckk = NaN*ones(3,3); | ||
176 | |||
177 | end; | ||
178 | |||
179 | eval(['omc_' num2str(kk) ' = omckk;']); | ||
180 | eval(['Rc_' num2str(kk) ' = Rckk;']); | ||
181 | eval(['Tc_' num2str(kk) ' = Tckk;']); | ||
182 | |||
183 | end; | ||
184 | |||
185 | |||
186 | % Initialization of the parameters for global minimization: | ||
187 | |||
188 | init_param = [f_init;k_init]; | ||
189 | |||
190 | for kk = 1:n_ima, | ||
191 | eval(['init_param = [init_param; omc_' num2str(kk) '; Tc_' num2str(kk) '];']); | ||
192 | end; | ||
193 | |||
194 | solution_init = [init_param;c_init]; | ||
195 | |||
196 | solution = solution_init; | ||
197 | |||
198 | comp_error_calib; | ||
199 | |||
200 | fprintf(1,'\n\nCalibration parameters after initialization:\n\n'); | ||
201 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
202 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
203 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
204 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
205 | |||
206 | |||
207 | %%%%%%%%%%%%%%%%%%%% GRAPHICAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%% | ||
208 | |||
209 | %graphout_calib; | ||
210 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/init_intrinsic_param.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/init_intrinsic_param.m new file mode 100755 index 0000000..eac21ba --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/init_intrinsic_param.m | |||
@@ -0,0 +1,153 @@ | |||
1 | %init_intrinsic_param | ||
2 | % | ||
3 | %Initialization of the intrinsic parameters. | ||
4 | %Runs as a script. | ||
5 | % | ||
6 | %INPUT: x_1,x_2,x_3,...: Feature locations on the images | ||
7 | % X_1,X_2,X_3,...: Corresponding grid coordinates | ||
8 | % | ||
9 | %OUTPUT: fc: Camera focal length | ||
10 | % cc: Principal point coordinates | ||
11 | % kc: Distortion coefficients | ||
12 | % KK: The camera matrix (containing fc and cc) | ||
13 | % | ||
14 | %Method: Computes the planar homographies H_1, H_2, H_3, ... and computes | ||
15 | % the focal length fc from orthogonal vanishing points constraint. | ||
16 | % The principal point cc is assumed at the center of the image. | ||
17 | % Assumes no image distortion (kc = [0;0;0;0]) | ||
18 | % | ||
19 | %Note: The row vector active_images consists of zeros and ones. To deactivate an image, set the | ||
20 | % corresponding entry in the active_images vector to zero. | ||
21 | % | ||
22 | % | ||
23 | %Important function called within that program: | ||
24 | % | ||
25 | %compute_homography.m: Computes the planar homography between points on the grid in 3D, and the image plane. | ||
26 | % | ||
27 | % | ||
28 | %VERY IMPORTANT: This function works onyl with 2D rigs. | ||
29 | %In the future, a more general function will be there (working with 3D rigs as well). | ||
30 | |||
31 | |||
32 | |||
33 | check_active_images; | ||
34 | |||
35 | if ~exist(['x_' num2str(ind_active(1)) ]), | ||
36 | click_calib; | ||
37 | end; | ||
38 | |||
39 | |||
40 | fprintf(1,'\nInitialization of the intrinsic parameters - Number of images: %d\n',length(ind_active)); | ||
41 | |||
42 | |||
43 | % Initialize the homographies: | ||
44 | |||
45 | for kk = 1:n_ima, | ||
46 | eval(['x_kk = x_' num2str(kk) ';']); | ||
47 | eval(['X_kk = X_' num2str(kk) ';']); | ||
48 | if (isnan(x_kk(1,1))), | ||
49 | if active_images(kk), | ||
50 | fprintf(1,'WARNING: Cannot calibrate with image %d. Need to extract grid corners first.\n',kk) | ||
51 | fprintf(1,' Set active_images(%d)=1; and run Extract grid corners.\n',kk) | ||
52 | end; | ||
53 | active_images(kk) = 0; | ||
54 | end; | ||
55 | if active_images(kk), | ||
56 | eval(['H_' num2str(kk) ' = compute_homography(x_kk,X_kk(1:2,:));']); | ||
57 | else | ||
58 | eval(['H_' num2str(kk) ' = NaN*ones(3,3);']); | ||
59 | end; | ||
60 | end; | ||
61 | |||
62 | check_active_images; | ||
63 | |||
64 | % initial guess for principal point and distortion: | ||
65 | |||
66 | if ~exist('nx'), [ny,nx] = size(I); end; | ||
67 | |||
68 | c_init = [nx;ny]/2 - 0.5; % initialize at the center of the image | ||
69 | k_init = [0;0;0;0]; % initialize to zero (no distortion) | ||
70 | |||
71 | |||
72 | |||
73 | % Compute explicitely the focal length using all the (mutually orthogonal) vanishing points | ||
74 | % note: The vanihing points are hidden in the planar collineations H_kk | ||
75 | |||
76 | A = []; | ||
77 | b = []; | ||
78 | |||
79 | % matrix that subtract the principal point: | ||
80 | Sub_cc = [1 0 -c_init(1);0 1 -c_init(2);0 0 1]; | ||
81 | |||
82 | for kk=1:n_ima, | ||
83 | |||
84 | if active_images(kk), | ||
85 | |||
86 | eval(['Hkk = H_' num2str(kk) ';']); | ||
87 | |||
88 | Hkk = Sub_cc * Hkk; | ||
89 | |||
90 | % Extract vanishing points (direct and diagonals): | ||
91 | |||
92 | V_hori_pix = Hkk(:,1); | ||
93 | V_vert_pix = Hkk(:,2); | ||
94 | V_diag1_pix = (Hkk(:,1)+Hkk(:,2))/2; | ||
95 | V_diag2_pix = (Hkk(:,1)-Hkk(:,2))/2; | ||
96 | |||
97 | V_hori_pix = V_hori_pix/norm(V_hori_pix); | ||
98 | V_vert_pix = V_vert_pix/norm(V_vert_pix); | ||
99 | V_diag1_pix = V_diag1_pix/norm(V_diag1_pix); | ||
100 | V_diag2_pix = V_diag2_pix/norm(V_diag2_pix); | ||
101 | |||
102 | a1 = V_hori_pix(1); | ||
103 | b1 = V_hori_pix(2); | ||
104 | c1 = V_hori_pix(3); | ||
105 | |||
106 | a2 = V_vert_pix(1); | ||
107 | b2 = V_vert_pix(2); | ||
108 | c2 = V_vert_pix(3); | ||
109 | |||
110 | a3 = V_diag1_pix(1); | ||
111 | b3 = V_diag1_pix(2); | ||
112 | c3 = V_diag1_pix(3); | ||
113 | |||
114 | a4 = V_diag2_pix(1); | ||
115 | b4 = V_diag2_pix(2); | ||
116 | c4 = V_diag2_pix(3); | ||
117 | |||
118 | A_kk = [a1*a2 b1*b2; | ||
119 | a3*a4 b3*b4]; | ||
120 | |||
121 | b_kk = -[c1*c2;c3*c4]; | ||
122 | |||
123 | |||
124 | A = [A;A_kk]; | ||
125 | b = [b;b_kk]; | ||
126 | |||
127 | end; | ||
128 | |||
129 | end; | ||
130 | |||
131 | |||
132 | % use all the vanishing points to estimate focal length: | ||
133 | |||
134 | f_init = sqrt(abs(1./(inv(A'*A)*A'*b))); % if using a two-focal model for initial guess | ||
135 | |||
136 | %f_init = sqrt(b'*(sum(A')') / (b'*b)) * ones(2,1); % if single focal length model is used | ||
137 | |||
138 | |||
139 | % Global calibration matrix (initial guess): | ||
140 | |||
141 | KK = [f_init(1) 0 c_init(1);0 f_init(2) c_init(2); 0 0 1]; | ||
142 | inv_KK = inv(KK); | ||
143 | |||
144 | |||
145 | cc = c_init; | ||
146 | fc = f_init; | ||
147 | kc = k_init; | ||
148 | |||
149 | |||
150 | fprintf(1,'\n\nCalibration parameters after initialization:\n\n'); | ||
151 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
152 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
153 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n\n',kc); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/is3D.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/is3D.m new file mode 100755 index 0000000..ab00b3d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/is3D.m | |||
@@ -0,0 +1,19 @@ | |||
1 | function test = is3D(X), | ||
2 | |||
3 | |||
4 | Np = size(X,2); | ||
5 | |||
6 | %% Check for planarity of the structure: | ||
7 | |||
8 | X_mean = mean(X')'; | ||
9 | |||
10 | Y = X - (X_mean*ones(1,Np)); | ||
11 | |||
12 | YY = Y*Y'; | ||
13 | |||
14 | [U,S,V] = svd(YY); | ||
15 | |||
16 | r = S(3,3)/S(2,2); | ||
17 | |||
18 | test = (r > 1e-3); | ||
19 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loading_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loading_calib.m new file mode 100755 index 0000000..a0f50d2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loading_calib.m | |||
@@ -0,0 +1,10 @@ | |||
1 | if ~exist('Calib_Results.mat'), | ||
2 | fprintf(1,'\nCalibration file Calib_Results.mat not found!\n'); | ||
3 | return; | ||
4 | end; | ||
5 | |||
6 | fprintf(1,'\nLoading calibration results from Calib_Results.mat\n'); | ||
7 | |||
8 | load Calib_Results | ||
9 | |||
10 | fprintf(1,'done\n'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loadinr.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loadinr.m new file mode 100755 index 0000000..91b6f89 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loadinr.m | |||
@@ -0,0 +1,52 @@ | |||
1 | %LOADINR Load an INRIMAGE format file | ||
2 | % | ||
3 | % LOADINR(filename, im) | ||
4 | % | ||
5 | % Load an INRIA image format file and return it as a matrix | ||
6 | % | ||
7 | % SEE ALSO: saveinr | ||
8 | % | ||
9 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
10 | |||
11 | |||
12 | % Peter Corke 1996 | ||
13 | |||
14 | function im = loadinr(fname, im) | ||
15 | |||
16 | fid = fopen(fname, 'r'); | ||
17 | |||
18 | s = fgets(fid); | ||
19 | if strcmp(s(1:12), '#INRIMAGE-4#') == 0, | ||
20 | error('not INRIMAGE format'); | ||
21 | end | ||
22 | |||
23 | % not very complete, only looks for the X/YDIM keys | ||
24 | while 1, | ||
25 | s = fgets(fid); | ||
26 | n = length(s) - 1; | ||
27 | if s(1) == '#', | ||
28 | break | ||
29 | end | ||
30 | if strcmp(s(1:5), 'XDIM='), | ||
31 | cols = str2num(s(6:n)); | ||
32 | end | ||
33 | if strcmp(s(1:5), 'YDIM='), | ||
34 | rows = str2num(s(6:n)); | ||
35 | end | ||
36 | if strcmp(s(1:4), 'CPU='), | ||
37 | if strcmp(s(5:n), 'sun') == 0, | ||
38 | error('not sun data ordering'); | ||
39 | end | ||
40 | end | ||
41 | |||
42 | end | ||
43 | disp(['INRIMAGE format file ' num2str(rows) ' x ' num2str(cols)]) | ||
44 | |||
45 | % now the binary data | ||
46 | fseek(fid, 256, 'bof'); | ||
47 | [im count] = fread(fid, [cols rows], 'float32'); | ||
48 | im = im'; | ||
49 | if count ~= (rows*cols), | ||
50 | error('file too short'); | ||
51 | end | ||
52 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loadpgm.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loadpgm.m new file mode 100755 index 0000000..9386111 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loadpgm.m | |||
@@ -0,0 +1,89 @@ | |||
1 | %LOADPGM Load a PGM image | ||
2 | % | ||
3 | % I = loadpgm(filename) | ||
4 | % | ||
5 | % Returns a matrix containing the image loaded from the PGM format | ||
6 | % file filename. Handles ASCII (P2) and binary (P5) PGM file formats. | ||
7 | % | ||
8 | % If the filename has no extension, and open fails, a '.pgm' will | ||
9 | % be appended. | ||
10 | % | ||
11 | % | ||
12 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
13 | |||
14 | |||
15 | % Peter Corke 1994 | ||
16 | |||
17 | function I = loadpgm(file) | ||
18 | white = [' ' 9 10 13]; % space, tab, lf, cr | ||
19 | white = setstr(white); | ||
20 | |||
21 | fid = fopen(file, 'r'); | ||
22 | if fid < 0, | ||
23 | fid = fopen([file '.pgm'], 'r'); | ||
24 | end | ||
25 | if fid < 0, | ||
26 | error('Couldn''t open file'); | ||
27 | end | ||
28 | |||
29 | magic = fread(fid, 2, 'char'); | ||
30 | while 1 | ||
31 | c = fread(fid,1,'char'); | ||
32 | if c == '#', | ||
33 | fgetl(fid); | ||
34 | elseif ~any(c == white) | ||
35 | fseek(fid, -1, 'cof'); % unputc() | ||
36 | break; | ||
37 | end | ||
38 | end | ||
39 | cols = fscanf(fid, '%d', 1); | ||
40 | while 1 | ||
41 | c = fread(fid,1,'char'); | ||
42 | if c == '#', | ||
43 | fgetl(fid); | ||
44 | elseif ~any(c == white) | ||
45 | fseek(fid, -1, 'cof'); % unputc() | ||
46 | break; | ||
47 | end | ||
48 | end | ||
49 | rows = fscanf(fid, '%d', 1); | ||
50 | while 1 | ||
51 | c = fread(fid,1,'char'); | ||
52 | if c == '#', | ||
53 | fgetl(fid); | ||
54 | elseif ~any(c == white) | ||
55 | fseek(fid, -1, 'cof'); % unputc() | ||
56 | break; | ||
57 | end | ||
58 | end | ||
59 | maxval = fscanf(fid, '%d', 1); | ||
60 | while 1 | ||
61 | c = fread(fid,1,'char'); | ||
62 | if c == '#', | ||
63 | fgetl(fid); | ||
64 | elseif ~any(c == white) | ||
65 | fseek(fid, -1, 'cof'); % unputc() | ||
66 | break; | ||
67 | end | ||
68 | end | ||
69 | if magic(1) == 'P', | ||
70 | if magic(2) == '2', | ||
71 | disp(['ASCII PGM file ' num2str(rows) ' x ' num2str(cols)]) | ||
72 | I = fscanf(fid, '%d', [cols rows])'; | ||
73 | elseif magic(2) == '5', | ||
74 | disp(['Binary PGM file ' num2str(rows) ' x ' num2str(cols)]) | ||
75 | if maxval == 1, | ||
76 | fmt = 'unint1'; | ||
77 | elseif maxval == 15, | ||
78 | fmt = 'uint4'; | ||
79 | elseif maxval == 255, | ||
80 | fmt = 'uint8'; | ||
81 | elseif maxval == 2^32-1, | ||
82 | fmt = 'uint32'; | ||
83 | end | ||
84 | I = fread(fid, [cols rows], fmt)'; | ||
85 | else | ||
86 | disp('Not a PGM file'); | ||
87 | end | ||
88 | end | ||
89 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loadppm.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loadppm.m new file mode 100755 index 0000000..6dd7ca4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/loadppm.m | |||
@@ -0,0 +1,101 @@ | |||
1 | %LOADPPM Load a PPM image | ||
2 | % | ||
3 | % [R,G,B] = loadppm(filename) | ||
4 | % | ||
5 | % Returns a matrix containing the image loaded from the PPM format | ||
6 | % file filename. Handles ASCII (P3) and binary (P6) PPM file formats. | ||
7 | % | ||
8 | % If the filename has no extension, and open fails, a '.ppm' and | ||
9 | % '.pnm' extension will be tried. | ||
10 | % | ||
11 | % SEE ALSO: saveppm loadpgm | ||
12 | % | ||
13 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
14 | |||
15 | |||
16 | % Peter Corke 1994 | ||
17 | |||
18 | function [R,G,B] = loadppm(file) | ||
19 | white = [' ' 9 10 13]; % space, tab, lf, cr | ||
20 | white = setstr(white); | ||
21 | |||
22 | fid = fopen(file, 'r'); | ||
23 | if fid < 0, | ||
24 | fid = fopen([file '.ppm'], 'r'); | ||
25 | end | ||
26 | if fid < 0, | ||
27 | fid = fopen([file '.pnm'], 'r'); | ||
28 | end | ||
29 | if fid < 0, | ||
30 | error('Couldn''t open file'); | ||
31 | end | ||
32 | |||
33 | magic = fread(fid, 2, 'char'); | ||
34 | while 1 | ||
35 | c = fread(fid,1,'char'); | ||
36 | if c == '#', | ||
37 | fgetl(fid); | ||
38 | elseif ~any(c == white) | ||
39 | fseek(fid, -1, 'cof'); % unputc() | ||
40 | break; | ||
41 | end | ||
42 | end | ||
43 | cols = fscanf(fid, '%d', 1); | ||
44 | while 1 | ||
45 | c = fread(fid,1,'char'); | ||
46 | if c == '#', | ||
47 | fgetl(fid); | ||
48 | elseif ~any(c == white) | ||
49 | fseek(fid, -1, 'cof'); % unputc() | ||
50 | break; | ||
51 | end | ||
52 | end | ||
53 | rows = fscanf(fid, '%d', 1); | ||
54 | while 1 | ||
55 | c = fread(fid,1,'char'); | ||
56 | if c == '#', | ||
57 | fgetl(fid); | ||
58 | elseif ~any(c == white) | ||
59 | fseek(fid, -1, 'cof'); % unputc() | ||
60 | break; | ||
61 | end | ||
62 | end | ||
63 | maxval = fscanf(fid, '%d', 1); | ||
64 | while 1 | ||
65 | c = fread(fid,1,'char'); | ||
66 | if c == '#', | ||
67 | fgetl(fid); | ||
68 | elseif ~any(c == white) | ||
69 | fseek(fid, -1, 'cof'); % unputc() | ||
70 | break; | ||
71 | end | ||
72 | end | ||
73 | if magic(1) == 'P', | ||
74 | if magic(2) == '3', | ||
75 | disp(['ASCII PPM file ' num2str(rows) ' x ' num2str(cols)]) | ||
76 | I = fscanf(fid, '%d', [cols*3 rows]); | ||
77 | elseif magic(2) == '6', | ||
78 | disp(['Binary PPM file ' num2str(rows) ' x ' num2str(cols)]) | ||
79 | if maxval == 1, | ||
80 | fmt = 'unint1'; | ||
81 | elseif maxval == 15, | ||
82 | fmt = 'uint4'; | ||
83 | elseif maxval == 255, | ||
84 | fmt = 'uint8'; | ||
85 | elseif maxval == 2^32-1, | ||
86 | fmt = 'uint32'; | ||
87 | end | ||
88 | I = fread(fid, [cols*3 rows], fmt); | ||
89 | else | ||
90 | disp('Not a PPM file'); | ||
91 | end | ||
92 | end | ||
93 | % | ||
94 | % now the matrix has interleaved columns of R, G, B | ||
95 | % | ||
96 | I = I'; | ||
97 | size(I) | ||
98 | R = I(:,1:3:(cols*3)); | ||
99 | G = I(:,2:3:(cols*3)); | ||
100 | B = I(:,3:3:(cols*3)); | ||
101 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/mean_std_robust.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/mean_std_robust.m new file mode 100755 index 0000000..0d18a62 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/mean_std_robust.m | |||
@@ -0,0 +1,7 @@ | |||
1 | function [m,s] = mean_std_robust(x); | ||
2 | |||
3 | x = x(:); | ||
4 | |||
5 | m = median(x); | ||
6 | |||
7 | s = median(abs(x - m))*1.4836; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/multi_error_oulu.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/multi_error_oulu.m new file mode 100755 index 0000000..8657158 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/multi_error_oulu.m | |||
@@ -0,0 +1,49 @@ | |||
1 | function ex = multi_error_oulu(param,n_ima,cc); | ||
2 | |||
3 | global X_1 x_1 X_2 x_2 X_3 x_3 X_4 x_4 X_5 x_5 X_6 x_6 X_7 x_7 X_8 x_8 X_9 x_9 X_10 x_10 X_11 x_11 X_12 x_12 X_13 x_13 X_14 x_14 X_15 x_15 X_16 x_16 X_17 x_17 X_18 x_18 X_19 x_19 X_20 x_20 X_21 x_21 X_22 x_22 X_23 x_23 X_24 x_24 X_25 x_25 X_26 x_26 X_27 x_27 X_28 x_28 X_29 x_29 X_30 x_30 | ||
4 | |||
5 | fc = param(1:2); | ||
6 | kc = param(3:6); | ||
7 | %ppc = param(5:6); | ||
8 | |||
9 | if length(param) > 6*n_ima + 3 + 3, | ||
10 | |||
11 | cc = param(6*n_ima + 4 + 3:6*n_ima + 5 + 3); | ||
12 | |||
13 | if length(param) > 6*n_ima + 5 + 3, | ||
14 | |||
15 | c_d = param(6*n_ima + 6 + 3 :6*n_ima + 7 + 3); | ||
16 | |||
17 | else | ||
18 | |||
19 | c_d = [0;0]; | ||
20 | |||
21 | end; | ||
22 | |||
23 | else | ||
24 | |||
25 | c_d = [0;0]; | ||
26 | |||
27 | end; | ||
28 | |||
29 | |||
30 | |||
31 | ex = []; | ||
32 | |||
33 | %keyboard; | ||
34 | |||
35 | for kk = 1:n_ima, | ||
36 | |||
37 | omckk = param(4+6*(kk-1) + 3:6*kk + 3); | ||
38 | |||
39 | Tckk = param(6*kk+1 + 3:6*kk+3 + 3); | ||
40 | |||
41 | Rkk = rodrigues(omckk); | ||
42 | |||
43 | eval(['ykk = project2_oulu(X_' num2str(kk) ',Rkk,Tckk,fc,cc,kc);']); | ||
44 | |||
45 | eval(['exkk = x_' num2str(kk) ' -ykk;']); | ||
46 | |||
47 | ex = [ex;exkk(:)]; | ||
48 | |||
49 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/normalize.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/normalize.m new file mode 100755 index 0000000..0a37378 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/normalize.m | |||
@@ -0,0 +1,32 @@ | |||
1 | function [xn] = normalize(x_kk,fc,cc,kc), | ||
2 | |||
3 | %normalize | ||
4 | % | ||
5 | %[xn] = normalize(x_kk,fc,cc,kc) | ||
6 | % | ||
7 | %Computes the normalized coordinates xn given the pixel coordinates x_kk | ||
8 | %and the intrinsic camera parameters fc, cc and kc. | ||
9 | % | ||
10 | %INPUT: x_kk: Feature locations on the images | ||
11 | % fc: Camera focal length | ||
12 | % cc: Principal point coordinates | ||
13 | % kc: Distortion coefficients | ||
14 | % | ||
15 | %OUTPUT: xn: Normalized feature locations on the image plane (a 2XN matrix) | ||
16 | % | ||
17 | %Important functions called within that program: | ||
18 | % | ||
19 | %comp_distortion_oulu: undistort pixel coordinates. | ||
20 | |||
21 | |||
22 | |||
23 | % First subtract principal point, and divide by the focal length: | ||
24 | |||
25 | x_distort = [(x_kk(1,:) - cc(1))/fc(1);(x_kk(2,:) - cc(2))/fc(2)]; | ||
26 | |||
27 | |||
28 | %Compensate for lens distortion: | ||
29 | |||
30 | xn = comp_distortion_oulu(x_distort,kc); | ||
31 | |||
32 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/pgmread.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/pgmread.m new file mode 100755 index 0000000..c96ccb7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/pgmread.m | |||
@@ -0,0 +1,26 @@ | |||
1 | function img = pgmread(filename) | ||
2 | % function img = pgmread(filename) | ||
3 | % this is my version of pgmread for the pgm file created by XV. | ||
4 | % | ||
5 | % this program also corrects for the shifts in the image from pm file. | ||
6 | |||
7 | fid = fopen(filename,'r'); | ||
8 | fscanf(fid, 'P5\n'); | ||
9 | cmt = '#'; | ||
10 | while findstr(cmt, '#'), | ||
11 | cmt = fgets(fid); | ||
12 | if length(findstr(cmt, '#')) ~= 1, | ||
13 | YX = sscanf(cmt, '%d %d'); | ||
14 | y = YX(1); x = YX(2); | ||
15 | end | ||
16 | end | ||
17 | |||
18 | %fgets(fid); | ||
19 | |||
20 | %img = fscanf(fid,'%d',size); | ||
21 | %img = img'; | ||
22 | |||
23 | img = fread(fid,[y,x],'uint8'); | ||
24 | img = img'; | ||
25 | fclose(fid); | ||
26 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/project2_oulu.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/project2_oulu.m new file mode 100755 index 0000000..c5c4a34 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/project2_oulu.m | |||
@@ -0,0 +1,53 @@ | |||
1 | function [x] = project2_oulu(X,R,T,f,t,k) | ||
2 | %PROJECT Subsidiary to calib | ||
3 | |||
4 | % (c) Pietro Perona -- March 24, 1994 | ||
5 | % California Institute of Technology | ||
6 | % Pasadena, CA | ||
7 | % | ||
8 | % Renamed because project exists in matlab 5.2!!! | ||
9 | % Now uses the more elaborate intrinsic model from Oulu | ||
10 | |||
11 | |||
12 | |||
13 | [m,n] = size(X); | ||
14 | |||
15 | Y = R*X + T*ones(1,n); | ||
16 | Z = Y(3,:); | ||
17 | |||
18 | f = f(:); %% make a column vector | ||
19 | if length(f)==1, | ||
20 | f = [f f]'; | ||
21 | end; | ||
22 | |||
23 | x = (Y(1:2,:) ./ (ones(2,1) * Z)) ; | ||
24 | |||
25 | |||
26 | radius_2 = x(1,:).^2 + x(2,:).^2; | ||
27 | |||
28 | if length(k) > 1, | ||
29 | |||
30 | radial_distortion = 1 + ones(2,1) * ((k(1) * radius_2) + (k(2) * radius_2.^2)); | ||
31 | |||
32 | if length(k) < 4, | ||
33 | |||
34 | delta_x = zeros(2,n); | ||
35 | |||
36 | else | ||
37 | |||
38 | delta_x = [2*k(3)*x(1,:).*x(2,:) + k(4)*(radius_2 + 2*x(1,:).^2) ; | ||
39 | k(3) * (radius_2 + 2*x(2,:).^2)+2*k(4)*x(1,:).*x(2,:)]; | ||
40 | |||
41 | end; | ||
42 | |||
43 | |||
44 | else | ||
45 | |||
46 | radial_distortion = 1 + ones(2,1) * ((k(1) * radius_2)); | ||
47 | |||
48 | delta_x = zeros(2,n); | ||
49 | |||
50 | end; | ||
51 | |||
52 | |||
53 | x = (x .* radial_distortion + delta_x).* (f * ones(1,n)) + t*ones(1,n); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/project_points.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/project_points.m new file mode 100755 index 0000000..1823490 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/project_points.m | |||
@@ -0,0 +1,276 @@ | |||
1 | function [xp,dxpdom,dxpdT,dxpdf,dxpdc,dxpdk] = project_points(X,om,T,f,c,k) | ||
2 | |||
3 | %project_points.m | ||
4 | % | ||
5 | %[xp,dxpdom,dxpdT,dxpdf,dxpdc,dxpdk] = project_points(X,om,T,f,c,k) | ||
6 | % | ||
7 | %Projects a 3D structure onto the image plane. | ||
8 | % | ||
9 | %INPUT: X: 3D structure in the world coordinate frame (3xN matrix for N points) | ||
10 | % (om,T): Rigid motion parameters between world coordinate frame and camera reference frame | ||
11 | % om: rotation vector (3x1 vector); T: translation vector (3x1 vector) | ||
12 | % f: camera focal length in units of horizontal and vertical pixel units (2x1 vector) | ||
13 | % c: principal point location in pixel units (2x1 vector) | ||
14 | % k: Distortion coefficients (radial and tangential) (4x1 vector) | ||
15 | % | ||
16 | %OUTPUT: xp: Projected pixel coordinates (2xN matrix for N points) | ||
17 | % dxpdom: Derivative of xp with respect to om ((2N)x3 matrix) | ||
18 | % dxpdT: Derivative of xp with respect to T ((2N)x3 matrix) | ||
19 | % dxpdf: Derivative of xp with respect to f ((2N)x2 matrix) | ||
20 | % dxpdc: Derivative of xp with respect to c ((2N)x2 matrix) | ||
21 | % dxpdk: Derivative of xp with respect to k ((2N)x4 matrix) | ||
22 | % | ||
23 | %Definitions: | ||
24 | %Let P be a point in 3D of coordinates X in the world reference frame (stored in the matrix X) | ||
25 | %The coordinate vector of P in the camera reference frame is: Xc = R*X + T | ||
26 | %where R is the rotation matrix corresponding to the rotation vector om: R = rodrigues(om); | ||
27 | %call x, y and z the 3 coordinates of Xc: x = Xc(1); y = Xc(2); z = Xc(3); | ||
28 | %The pinehole projection coordinates of P is [a;b] where a=x/z and b=y/z. | ||
29 | %call r^2 = a^2 + b^2. | ||
30 | %The distorted point coordinates are: xd = [xx;yy] where: | ||
31 | % | ||
32 | %xx = a * (1 + kc(1)*r^2 + kc(2)*r^4) + 2*kc(3)*a*b + kc(4)*(r^2 + 2*a^2); | ||
33 | %yy = b * (1 + kc(1)*r^2 + kc(2)*r^4) + kc(3)*(r^2 + 2*b^2) + 2*kc(4)*a*b; | ||
34 | % | ||
35 | %The left terms correspond to radial distortion, the right terms correspond to tangential distortion | ||
36 | % | ||
37 | %Fianlly, convertion into pixel coordinates: The final pixel coordinates vector xp=[xxp;yyp] where: | ||
38 | % | ||
39 | %xxp = f(1)*xx + c(1) | ||
40 | %yyp = f(2)*yy + c(2) | ||
41 | % | ||
42 | % | ||
43 | %NOTE: About 90 percent of the code takes care fo computing the Jacobian matrices | ||
44 | % | ||
45 | % | ||
46 | %Important function called within that program: | ||
47 | % | ||
48 | %rodrigues.m: Computes the rotation matrix corresponding to a rotation vector | ||
49 | % | ||
50 | %rigid_motion.m: Computes the rigid motion transformation of a given structure | ||
51 | |||
52 | |||
53 | |||
54 | if nargin < 6, | ||
55 | k = zeros(4,1); | ||
56 | if nargin < 5, | ||
57 | c = zeros(2,1); | ||
58 | if nargin < 4, | ||
59 | f = ones(2,1); | ||
60 | if nargin < 3, | ||
61 | T = zeros(3,1); | ||
62 | if nargin < 2, | ||
63 | om = zeros(3,1); | ||
64 | if nargin < 1, | ||
65 | error('Need at least a 3D structure to project (in project_points.m)'); | ||
66 | return; | ||
67 | end; | ||
68 | end; | ||
69 | end; | ||
70 | end; | ||
71 | end; | ||
72 | end; | ||
73 | |||
74 | |||
75 | [m,n] = size(X); | ||
76 | |||
77 | [Y,dYdom,dYdT] = rigid_motion(X,om,T); | ||
78 | |||
79 | |||
80 | inv_Z = 1./Y(3,:); | ||
81 | |||
82 | x = (Y(1:2,:) .* (ones(2,1) * inv_Z)) ; | ||
83 | |||
84 | |||
85 | bb = (-x(1,:) .* inv_Z)'*ones(1,3); | ||
86 | cc = (-x(2,:) .* inv_Z)'*ones(1,3); | ||
87 | |||
88 | |||
89 | dxdom = zeros(2*n,3); | ||
90 | dxdom(1:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdom(1:3:end,:) + bb .* dYdom(3:3:end,:); | ||
91 | dxdom(2:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdom(2:3:end,:) + cc .* dYdom(3:3:end,:); | ||
92 | |||
93 | dxdT = zeros(2*n,3); | ||
94 | dxdT(1:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdT(1:3:end,:) + bb .* dYdT(3:3:end,:); | ||
95 | dxdT(2:2:end,:) = ((inv_Z')*ones(1,3)) .* dYdT(2:3:end,:) + cc .* dYdT(3:3:end,:); | ||
96 | |||
97 | |||
98 | % Add distortion: | ||
99 | |||
100 | r2 = x(1,:).^2 + x(2,:).^2; | ||
101 | |||
102 | |||
103 | |||
104 | dr2dom = 2*((x(1,:)')*ones(1,3)) .* dxdom(1:2:end,:) + 2*((x(2,:)')*ones(1,3)) .* dxdom(2:2:end,:); | ||
105 | dr2dT = 2*((x(1,:)')*ones(1,3)) .* dxdT(1:2:end,:) + 2*((x(2,:)')*ones(1,3)) .* dxdT(2:2:end,:); | ||
106 | |||
107 | |||
108 | r4 = r2.^2; | ||
109 | |||
110 | dr4dom = 2*((r2')*ones(1,3)) .* dr2dom; | ||
111 | dr4dT = 2*((r2')*ones(1,3)) .* dr2dT; | ||
112 | |||
113 | |||
114 | % Radial distortion: | ||
115 | |||
116 | cdist = 1 + k(1) * r2 + k(2) * r4; | ||
117 | |||
118 | dcdistdom = k(1) * dr2dom + k(2) * dr4dom; | ||
119 | dcdistdT = k(1) * dr2dT+ k(2) * dr4dT; | ||
120 | dcdistdk = [ r2' r4' zeros(n,2)]; | ||
121 | |||
122 | |||
123 | xd1 = x .* (ones(2,1)*cdist); | ||
124 | |||
125 | dxd1dom = zeros(2*n,3); | ||
126 | dxd1dom(1:2:end,:) = (x(1,:)'*ones(1,3)) .* dcdistdom; | ||
127 | dxd1dom(2:2:end,:) = (x(2,:)'*ones(1,3)) .* dcdistdom; | ||
128 | coeff = (reshape([cdist;cdist],2*n,1)*ones(1,3)); | ||
129 | dxd1dom = dxd1dom + coeff.* dxdom; | ||
130 | |||
131 | dxd1dT = zeros(2*n,3); | ||
132 | dxd1dT(1:2:end,:) = (x(1,:)'*ones(1,3)) .* dcdistdT; | ||
133 | dxd1dT(2:2:end,:) = (x(2,:)'*ones(1,3)) .* dcdistdT; | ||
134 | dxd1dT = dxd1dT + coeff.* dxdT; | ||
135 | |||
136 | dxd1dk = zeros(2*n,4); | ||
137 | dxd1dk(1:2:end,:) = (x(1,:)'*ones(1,4)) .* dcdistdk; | ||
138 | dxd1dk(2:2:end,:) = (x(2,:)'*ones(1,4)) .* dcdistdk; | ||
139 | |||
140 | |||
141 | |||
142 | % tangential distortion: | ||
143 | |||
144 | a1 = 2.*x(1,:).*x(2,:); | ||
145 | a2 = r2 + 2*x(1,:).^2; | ||
146 | a3 = r2 + 2*x(2,:).^2; | ||
147 | |||
148 | delta_x = [k(3)*a1 + k(4)*a2 ; | ||
149 | k(3) * a3 + k(4)*a1]; | ||
150 | |||
151 | |||
152 | ddelta_xdx = zeros(2*n,2*n); | ||
153 | aa = (2*k(3)*x(2,:)+6*k(4)*x(1,:))'*ones(1,3); | ||
154 | bb = (2*k(3)*x(1,:)+2*k(4)*x(2,:))'*ones(1,3); | ||
155 | cc = (6*k(3)*x(2,:)+2*k(4)*x(1,:))'*ones(1,3); | ||
156 | |||
157 | ddelta_xdom = zeros(2*n,3); | ||
158 | ddelta_xdom(1:2:end,:) = aa .* dxdom(1:2:end,:) + bb .* dxdom(2:2:end,:); | ||
159 | ddelta_xdom(2:2:end,:) = bb .* dxdom(1:2:end,:) + cc .* dxdom(2:2:end,:); | ||
160 | |||
161 | ddelta_xdT = zeros(2*n,3); | ||
162 | ddelta_xdT(1:2:end,:) = aa .* dxdT(1:2:end,:) + bb .* dxdT(2:2:end,:); | ||
163 | ddelta_xdT(2:2:end,:) = bb .* dxdT(1:2:end,:) + cc .* dxdT(2:2:end,:); | ||
164 | |||
165 | ddelta_xdk = zeros(2*n,4); | ||
166 | ddelta_xdk(1:2:end,3) = a1'; | ||
167 | ddelta_xdk(1:2:end,4) = a2'; | ||
168 | ddelta_xdk(2:2:end,3) = a3'; | ||
169 | ddelta_xdk(2:2:end,4) = a1'; | ||
170 | |||
171 | |||
172 | |||
173 | xd2 = xd1 + delta_x; | ||
174 | |||
175 | dxd2dom = dxd1dom + ddelta_xdom ; | ||
176 | dxd2dT = dxd1dT + ddelta_xdT; | ||
177 | dxd2dk = dxd1dk + ddelta_xdk ; | ||
178 | |||
179 | |||
180 | % Pixel coordinates: | ||
181 | |||
182 | xp = xd2 .* (f * ones(1,n)) + c*ones(1,n); | ||
183 | |||
184 | coeff = reshape(f*ones(1,n),2*n,1); | ||
185 | |||
186 | dxpdom = (coeff*ones(1,3)) .* dxd2dom; | ||
187 | dxpdT = (coeff*ones(1,3)) .* dxd2dT; | ||
188 | dxpdk = (coeff*ones(1,4)) .* dxd2dk; | ||
189 | |||
190 | dxpdf = zeros(2*n,2); | ||
191 | dxpdf(1:2:end,1) = xd2(1,:)'; | ||
192 | dxpdf(2:2:end,2) = xd2(2,:)'; | ||
193 | |||
194 | dxpdc = zeros(2*n,2); | ||
195 | dxpdc(1:2:end,1) = ones(n,1); | ||
196 | dxpdc(2:2:end,2) = ones(n,1); | ||
197 | |||
198 | |||
199 | return; | ||
200 | |||
201 | % Test of the Jacobians: | ||
202 | |||
203 | n = 10; | ||
204 | |||
205 | X = 10*randn(3,n); | ||
206 | om = randn(3,1); | ||
207 | T = [10*randn(2,1);40]; | ||
208 | f = 1000*rand(2,1); | ||
209 | c = 1000*randn(2,1); | ||
210 | k = 0.5*randn(4,1); | ||
211 | |||
212 | |||
213 | [x,dxdom,dxdT,dxdf,dxdc,dxdk] = project_points(X,om,T,f,c,k); | ||
214 | |||
215 | |||
216 | % Test on om: NOT OK | ||
217 | |||
218 | dom = 0.000000001 * norm(om)*randn(3,1); | ||
219 | om2 = om + dom; | ||
220 | |||
221 | [x2] = project_points(X,om2,T,f,c,k); | ||
222 | |||
223 | x_pred = x + reshape(dxdom * dom,2,n); | ||
224 | |||
225 | |||
226 | norm(x2-x)/norm(x2 - x_pred) | ||
227 | |||
228 | |||
229 | % Test on T: OK!! | ||
230 | |||
231 | dT = 0.0001 * norm(T)*randn(3,1); | ||
232 | T2 = T + dT; | ||
233 | |||
234 | [x2] = project_points(X,om,T2,f,c,k); | ||
235 | |||
236 | x_pred = x + reshape(dxdT * dT,2,n); | ||
237 | |||
238 | |||
239 | norm(x2-x)/norm(x2 - x_pred) | ||
240 | |||
241 | |||
242 | |||
243 | % Test on f: OK!! | ||
244 | |||
245 | df = 0.001 * norm(f)*randn(2,1); | ||
246 | f2 = f + df; | ||
247 | |||
248 | [x2] = project_points(X,om,T,f2,c,k); | ||
249 | |||
250 | x_pred = x + reshape(dxdf * df,2,n); | ||
251 | |||
252 | |||
253 | norm(x2-x)/norm(x2 - x_pred) | ||
254 | |||
255 | |||
256 | % Test on c: OK!! | ||
257 | |||
258 | dc = 0.01 * norm(c)*randn(2,1); | ||
259 | c2 = c + dc; | ||
260 | |||
261 | [x2] = project_points(X,om,T,f,c2,k); | ||
262 | |||
263 | x_pred = x + reshape(dxdc * dc,2,n); | ||
264 | |||
265 | norm(x2-x)/norm(x2 - x_pred) | ||
266 | |||
267 | % Test on k: OK!! | ||
268 | |||
269 | dk = 0.001 * norm(4)*randn(4,1); | ||
270 | k2 = k + dk; | ||
271 | |||
272 | [x2] = project_points(X,om,T,f,c,k2); | ||
273 | |||
274 | x_pred = x + reshape(dxdk * dk,2,n); | ||
275 | |||
276 | norm(x2-x)/norm(x2 - x_pred) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/projectedGrid.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/projectedGrid.m new file mode 100755 index 0000000..561a7d0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/projectedGrid.m | |||
@@ -0,0 +1,24 @@ | |||
1 | function [XX,H] = projectedGrid ( P1, P2, P3, P4 , nx, ny); | ||
2 | |||
3 | % new formalism using homographies | ||
4 | |||
5 | a00 = [P1;1]; | ||
6 | a10 = [P2;1]; | ||
7 | a11 = [P3;1]; | ||
8 | a01 = [P4;1]; | ||
9 | |||
10 | % Compute the planart collineation: | ||
11 | |||
12 | [H] = compute_collineation (a00, a10, a11, a01); | ||
13 | |||
14 | |||
15 | % Build the grid using the planar collineation: | ||
16 | |||
17 | x_l = ((0:(nx-1))'*ones(1,ny))/(nx-1); | ||
18 | y_l = (ones(nx,1)*(0:(ny-1)))/(ny-1); | ||
19 | |||
20 | pts = [x_l(:) y_l(:) ones(nx*ny,1)]'; | ||
21 | |||
22 | XX = H*pts; | ||
23 | |||
24 | XX = XX(1:2,:) ./ (ones(2,1)*XX(3,:)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/readras.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/readras.m new file mode 100755 index 0000000..fc1820b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/readras.m | |||
@@ -0,0 +1,87 @@ | |||
1 | function [X, map] = readras(filename, ys, ye, xs, xe); | ||
2 | %READRAS Read an image file in sun raster format. | ||
3 | % READRAS('imagefile.ras') reads a "sun.raster" image file. | ||
4 | % [X, map] = READRAS('imagefile.ras') returns both the image and a | ||
5 | % color map, so that | ||
6 | % [X, map] = readras('imagefile.ras'); | ||
7 | % image(X) | ||
8 | % colormap(map) | ||
9 | % axis('equal') | ||
10 | % will display the result with the proper colors. | ||
11 | % NOTE: readras cannot deal with complicated color maps. | ||
12 | % In fact, Matlab doesn't quite allow to work with colormaps | ||
13 | % with more than 64 entries. | ||
14 | % | ||
15 | |||
16 | %% | ||
17 | %% (C) Thomas K. Leung 3/30/93. | ||
18 | %% California Institute of Technology. | ||
19 | %% Modified by Andrea Mennucci to deal with color images | ||
20 | %% | ||
21 | |||
22 | % PC and UNIX version of readras - Jean-Yves Bouguet - Dec. 1998 | ||
23 | |||
24 | dot = max(find(filename == '.')); | ||
25 | suffix = filename(dot+1:dot+3); | ||
26 | |||
27 | if(strcmp(suffix, 'ras')) % raster file format % | ||
28 | fp = fopen(filename, 'rb'); | ||
29 | if(fp<0) error(['Cannot open ' filename '.']), end | ||
30 | |||
31 | %Read and crack the 32-byte header | ||
32 | fseek(fp, 4, -1); | ||
33 | |||
34 | width = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
35 | |||
36 | height = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
37 | |||
38 | depth = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
39 | |||
40 | length = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
41 | |||
42 | type = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
43 | |||
44 | maptype = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
45 | |||
46 | maplen = 2^24 * fread(fp, 1, 'uchar') + 2^16 * fread(fp, 1, 'uchar') + 2^8 * fread(fp, 1, 'uchar') + fread(fp, 1, 'uchar'); | ||
47 | |||
48 | maplen = maplen / 3; | ||
49 | |||
50 | if maptype == 2 % RMT_RAW | ||
51 | map = fread(fp, [maplen, 3], 'uchar')/255; | ||
52 | % if maplen<64, map=[map',zeros(3,64-maplen)]';maplen=64; end; | ||
53 | elseif maptype == 1 % RMT_EQUAL_RGB | ||
54 | map(:,1) = fread(fp, [maplen], 'uchar'); | ||
55 | map(:,2) = fread(fp, [maplen], 'uchar'); | ||
56 | map(:,3) = fread(fp, [maplen], 'uchar'); | ||
57 | %maxmap = max(max(map)); | ||
58 | map = map/255; | ||
59 | if maplen<64, map=[map',zeros(3,64-maplen)]'; maplen=64; end; | ||
60 | else % RMT_NONE | ||
61 | map = []; | ||
62 | end | ||
63 | % if maplen>64, | ||
64 | % map=[map',zeros(3,256-maplen)]'; | ||
65 | % end; | ||
66 | |||
67 | % Read the image | ||
68 | |||
69 | if rem(width,2) == 1 | ||
70 | Xt = fread(fp, [width+1, height], 'uchar'); | ||
71 | X = Xt(1:width, :)'; | ||
72 | else | ||
73 | Xt = fread(fp, [width, height], 'uchar'); | ||
74 | X = Xt'; | ||
75 | end | ||
76 | X = X + 1; | ||
77 | fclose(fp); | ||
78 | else | ||
79 | error('Image file name must end in either ''ras'' or ''rast''.'); | ||
80 | end | ||
81 | |||
82 | |||
83 | if nargin == 5 | ||
84 | |||
85 | X = X(ys:ye, xs:xe); | ||
86 | |||
87 | end \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/recomp_corner_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/recomp_corner_calib.m new file mode 100755 index 0000000..e0af501 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/recomp_corner_calib.m | |||
@@ -0,0 +1,96 @@ | |||
1 | % Re-select te corners after calibration | ||
2 | |||
3 | check_active_images; | ||
4 | |||
5 | if ~exist(['y_' num2str(ind_active(1))]), | ||
6 | fprintf(1,'Need to calibrate once before before recomputing image corners. Maybe need to load Calib_Results.mat file.\n'); | ||
7 | return; | ||
8 | end; | ||
9 | |||
10 | if ~exist(['I_' num2str(ind_active(1))]), | ||
11 | ima_read_calib; | ||
12 | if no_image_file, | ||
13 | disp('Cannot extract corners without images'); | ||
14 | return; | ||
15 | end; | ||
16 | end; | ||
17 | |||
18 | fprintf(1,'\nRe-extraction of the grid corners on the images (after first calibration)\n'); | ||
19 | |||
20 | disp('Window size for corner finder (wintx and winty):'); | ||
21 | wintx = input('wintx ([] = 5) = '); | ||
22 | if isempty(wintx), wintx = 5; end; | ||
23 | wintx = round(wintx); | ||
24 | winty = input('winty ([] = 5) = '); | ||
25 | if isempty(winty), winty = 5; end; | ||
26 | winty = round(winty); | ||
27 | |||
28 | fprintf(1,'Window size = %dx%d\n',2*wintx+1,2*winty+1); | ||
29 | |||
30 | ima_numbers = input('Number(s) of image(s) to process ([] = all images) = '); | ||
31 | |||
32 | if isempty(ima_numbers), | ||
33 | ima_proc = 1:n_ima; | ||
34 | else | ||
35 | ima_proc = ima_numbers; | ||
36 | end; | ||
37 | |||
38 | q_auto = input('Use the projection of 3D grid or manual click ([]=auto, other=manual): '); | ||
39 | |||
40 | fprintf(1,'Processing image '); | ||
41 | |||
42 | for kk = ima_proc; | ||
43 | |||
44 | if active_images(kk), | ||
45 | |||
46 | fprintf(1,'%d...',kk); | ||
47 | |||
48 | if isempty(q_auto), | ||
49 | |||
50 | eval(['I = I_' num2str(kk) ';']); | ||
51 | |||
52 | eval(['y = y_' num2str(kk) ';']); | ||
53 | |||
54 | xc = cornerfinder(y+1,I,winty,wintx); % the four corners | ||
55 | |||
56 | eval(['wintx_' num2str(kk) ' = wintx;']); | ||
57 | eval(['winty_' num2str(kk) ' = winty;']); | ||
58 | |||
59 | eval(['x_' num2str(kk) '= xc - 1;']); | ||
60 | |||
61 | else | ||
62 | |||
63 | fprintf(1,'\n'); | ||
64 | |||
65 | click_ima_calib; | ||
66 | |||
67 | end; | ||
68 | |||
69 | else | ||
70 | |||
71 | if ~exist(['omc_' num2str(kk)]), | ||
72 | |||
73 | eval(['dX_' num2str(kk) ' = NaN;']); | ||
74 | eval(['dY_' num2str(kk) ' = NaN;']); | ||
75 | |||
76 | eval(['wintx_' num2str(kk) ' = NaN;']); | ||
77 | eval(['winty_' num2str(kk) ' = NaN;']); | ||
78 | |||
79 | eval(['x_' num2str(kk) ' = NaN*ones(2,1);']); | ||
80 | eval(['X_' num2str(kk) ' = NaN*ones(3,1);']); | ||
81 | |||
82 | eval(['n_sq_x_' num2str(kk) ' = NaN;']); | ||
83 | eval(['n_sq_y_' num2str(kk) ' = NaN;']); | ||
84 | |||
85 | end; | ||
86 | |||
87 | end; | ||
88 | |||
89 | |||
90 | end; | ||
91 | |||
92 | % Recompute the error: | ||
93 | |||
94 | comp_error_calib; | ||
95 | |||
96 | fprintf(1,'\ndone\n'); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rect.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rect.m new file mode 100755 index 0000000..d8b6366 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rect.m | |||
@@ -0,0 +1,93 @@ | |||
1 | function [Irec] = rect(I,R,f,c,k,KK_new); | ||
2 | |||
3 | |||
4 | % Note: R is the motion of the points in space | ||
5 | % So: X2 = R*X where X: coord in the old reference frame, X2: coord in the new ref frame. | ||
6 | |||
7 | [nr,nc] = size(I); | ||
8 | |||
9 | Irec = 255*ones(nr,nc); | ||
10 | |||
11 | [mx,my] = meshgrid(1:nc, 1:nr); | ||
12 | px = reshape(mx',nc*nr,1); | ||
13 | py = reshape(my',nc*nr,1); | ||
14 | |||
15 | rays = inv(KK_new)*[(px - 1)';(py - 1)';ones(1,length(px))]; | ||
16 | |||
17 | |||
18 | % Rotation: (or affine transformation): | ||
19 | |||
20 | rays2 = R'*rays; | ||
21 | |||
22 | |||
23 | x = [rays2(1,:)./rays2(3,:);rays2(2,:)./rays2(3,:)]; | ||
24 | |||
25 | % Add distortion: | ||
26 | |||
27 | k1 = k(1); | ||
28 | k2 = k(2); | ||
29 | |||
30 | p1 = k(3); | ||
31 | p2 = k(4); | ||
32 | |||
33 | r_2 = sum(x.^2); | ||
34 | |||
35 | delta_x = [2*p1*x(1,:).*x(2,:) + p2*(r_2 + 2*x(1,:).^2) ; | ||
36 | p1 * (r_2 + 2*x(2,:).^2)+2*p2*x(1,:).*x(2,:)]; | ||
37 | |||
38 | xd = (ones(2,1)*( 1 + k1 * r_2 + k2 * r_2.^2)) .* x + delta_x; | ||
39 | |||
40 | |||
41 | % Reconvert in pixels: | ||
42 | |||
43 | px2 = f(1)*xd(1,:)+c(1); | ||
44 | py2 = f(2)*xd(2,:)+c(2); | ||
45 | |||
46 | |||
47 | % Interpolate between the closest pixels: | ||
48 | |||
49 | |||
50 | px_0 = floor(px2); | ||
51 | px_1 = px_0 + 1; | ||
52 | alpha_x = px2 - px_0; | ||
53 | |||
54 | py_0 = floor(py2); | ||
55 | py_1 = py_0 + 1; | ||
56 | alpha_y = py2 - py_0; | ||
57 | |||
58 | good_points = find((px_0 >= 0) & (px_1 <= (nc-1)) & (py_0 >= 0) & (py_1 <= (nr-1))); | ||
59 | |||
60 | I_lu = I(px_0(good_points) * nr + py_0(good_points) + 1); | ||
61 | I_ru = I(px_1(good_points) * nr + py_0(good_points) + 1); | ||
62 | I_ld = I(px_0(good_points) * nr + py_1(good_points) + 1); | ||
63 | I_rd = I(px_1(good_points) * nr + py_1(good_points) + 1); | ||
64 | |||
65 | |||
66 | I_interp = (1 - alpha_y(good_points)).*((1 - alpha_x(good_points)).* I_lu + alpha_x(good_points) .* I_ru) + alpha_y(good_points) .* ((1 - alpha_x(good_points)).* I_ld + alpha_x(good_points) .* I_rd); | ||
67 | |||
68 | |||
69 | Irec((px(good_points)-1)*nr + py(good_points)) = I_interp; | ||
70 | |||
71 | |||
72 | |||
73 | return; | ||
74 | |||
75 | |||
76 | % Convert in indices: | ||
77 | |||
78 | fact = 3; | ||
79 | |||
80 | [XX,YY]= meshgrid(1:nc,1:nr); | ||
81 | [XXi,YYi]= meshgrid(1:1/fact:nc,1:1/fact:nr); | ||
82 | |||
83 | %tic; | ||
84 | Iinterp = interp2(XX,YY,I,XXi,YYi); | ||
85 | %toc | ||
86 | |||
87 | [nri,nci] = size(Iinterp); | ||
88 | |||
89 | |||
90 | ind_col = round(fact*(f(1)*xd(1,:)+c(1)))+1; | ||
91 | ind_row = round(fact*(f(2)*xd(2,:)+c(2)))+1; | ||
92 | |||
93 | good_points = find((ind_col >=1)&(ind_col<=nci)&(ind_row >=1)& (ind_row <=nri)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/reproject_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/reproject_calib.m new file mode 100755 index 0000000..86b13f5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/reproject_calib.m | |||
@@ -0,0 +1,92 @@ | |||
1 | %%%%%%%%%%%%%%%%%%%% REPROJECT ON THE IMAGES %%%%%%%%%%%%%%%%%%%%%%%% | ||
2 | |||
3 | if ~exist('no_image'), | ||
4 | no_image = 0; | ||
5 | end; | ||
6 | |||
7 | check_active_images; | ||
8 | |||
9 | |||
10 | % Color code for each image: | ||
11 | |||
12 | colors = 'brgkcm'; | ||
13 | |||
14 | % Reproject the patterns on the images, and compute the pixel errors: | ||
15 | |||
16 | % Reload the images if necessary | ||
17 | |||
18 | if ~exist(['omc_' num2str(ind_active(1)) ]), | ||
19 | fprintf(1,'Need to calibrate before showing image reprojection. Maybe need to load Calib_Results.mat file.\n'); | ||
20 | return; | ||
21 | end; | ||
22 | |||
23 | if ~no_image, | ||
24 | if ~exist(['I_' num2str(ind_active(1)) ]'), | ||
25 | ima_read_calib; | ||
26 | if no_image_file, | ||
27 | fprintf(1,'WARNING: Do not show the original images\n'); %return; | ||
28 | end; | ||
29 | end; | ||
30 | else | ||
31 | no_image_file = 1; | ||
32 | end; | ||
33 | |||
34 | |||
35 | |||
36 | ima_numbers = input('Number(s) of image(s) to show ([] = all images) = '); | ||
37 | |||
38 | if isempty(ima_numbers), | ||
39 | ima_proc = 1:n_ima; | ||
40 | else | ||
41 | ima_proc = ima_numbers; | ||
42 | end; | ||
43 | |||
44 | |||
45 | figure(5); | ||
46 | for kk = ima_proc, %1:n_ima, | ||
47 | if active_images(kk) & eval(['~isnan(y_' num2str(kk) '(1,1))']), | ||
48 | eval(['plot(ex_' num2str(kk) '(1,:),ex_' num2str(kk) '(2,:),''' colors(rem(kk-1,6)+1) '+'');']); | ||
49 | hold on; | ||
50 | end; | ||
51 | end; | ||
52 | hold off; | ||
53 | axis('equal'); | ||
54 | title('Reprojection error (in pixel)'); | ||
55 | xlabel('x'); | ||
56 | ylabel('y'); | ||
57 | drawnow; | ||
58 | |||
59 | set(5,'Name','error','NumberTitle','off'); | ||
60 | |||
61 | |||
62 | |||
63 | for kk = ima_proc, | ||
64 | |||
65 | if active_images(kk) & eval(['~isnan(y_' num2str(kk) '(1,1))']), | ||
66 | |||
67 | if exist(['I_' num2str(kk)]), | ||
68 | eval(['I = I_' num2str(kk) ';']); | ||
69 | else | ||
70 | I = 255*ones(ny,nx); | ||
71 | end; | ||
72 | |||
73 | figure(5+kk); | ||
74 | image(I); hold on; | ||
75 | colormap(gray(256)); | ||
76 | title(['Image ' num2str(kk) ' - Image points (+) and reprojected grid points (o)']); | ||
77 | eval(['plot(x_' num2str(kk) '(1,:)+1,x_' num2str(kk) '(2,:)+1,''r+'');']); | ||
78 | eval(['plot(y_' num2str(kk) '(1,:)+1,y_' num2str(kk) '(2,:)+1,''' colors(rem(kk-1,6)+1) 'o'');']); | ||
79 | zoom on; | ||
80 | hold off; | ||
81 | drawnow; | ||
82 | |||
83 | set(5+kk,'Name',num2str(kk),'NumberTitle','off'); | ||
84 | |||
85 | end; | ||
86 | |||
87 | end; | ||
88 | |||
89 | |||
90 | err_std = std(ex')'; | ||
91 | |||
92 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rigid_motion.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rigid_motion.m new file mode 100755 index 0000000..473405c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rigid_motion.m | |||
@@ -0,0 +1,66 @@ | |||
1 | function [Y,dYdom,dYdT] = rigid_motion(X,om,T); | ||
2 | |||
3 | %rigid_motion.m | ||
4 | % | ||
5 | %[Y,dYdom,dYdT] = rigid_motion(X,om,T) | ||
6 | % | ||
7 | %Computes the rigid motion transformation Y = R*X+T, where R = rodrigues(om). | ||
8 | % | ||
9 | %INPUT: X: 3D structure in the world coordinate frame (3xN matrix for N points) | ||
10 | % (om,T): Rigid motion parameters between world coordinate frame and camera reference frame | ||
11 | % om: rotation vector (3x1 vector); T: translation vector (3x1 vector) | ||
12 | % | ||
13 | %OUTPUT: Y: 3D coordinates of the structure points in the camera reference frame (3xN matrix for N points) | ||
14 | % dYdom: Derivative of Y with respect to om ((3N)x3 matrix) | ||
15 | % dYdT: Derivative of Y with respect to T ((3N)x3 matrix) | ||
16 | % | ||
17 | %Definitions: | ||
18 | %Let P be a point in 3D of coordinates X in the world reference frame (stored in the matrix X) | ||
19 | %The coordinate vector of P in the camera reference frame is: Y = R*X + T | ||
20 | %where R is the rotation matrix corresponding to the rotation vector om: R = rodrigues(om); | ||
21 | % | ||
22 | %Important function called within that program: | ||
23 | % | ||
24 | %rodrigues.m: Computes the rotation matrix corresponding to a rotation vector | ||
25 | |||
26 | |||
27 | |||
28 | if nargin < 3, | ||
29 | T = zeros(3,1); | ||
30 | if nargin < 2, | ||
31 | om = zeros(3,1); | ||
32 | if nargin < 1, | ||
33 | error('Need at least a 3D structure as input (in rigid_motion.m)'); | ||
34 | return; | ||
35 | end; | ||
36 | end; | ||
37 | end; | ||
38 | |||
39 | |||
40 | [R,dRdom] = rodrigues(om); | ||
41 | |||
42 | [m,n] = size(X); | ||
43 | |||
44 | Y = R*X + T*ones(1,n); | ||
45 | |||
46 | if nargout > 1, | ||
47 | |||
48 | |||
49 | dYdR = zeros(3*n,9); | ||
50 | dYdT = zeros(3*n,3); | ||
51 | |||
52 | dYdR(1:3:end,1:3:end) = X'; | ||
53 | dYdR(2:3:end,2:3:end) = X'; | ||
54 | dYdR(3:3:end,3:3:end) = X'; | ||
55 | |||
56 | dYdT(1:3:end,1) = ones(n,1); | ||
57 | dYdT(2:3:end,2) = ones(n,1); | ||
58 | dYdT(3:3:end,3) = ones(n,1); | ||
59 | |||
60 | dYdom = dYdR * dRdom; | ||
61 | |||
62 | end; | ||
63 | |||
64 | |||
65 | |||
66 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rodrigues.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rodrigues.m new file mode 100755 index 0000000..9d55337 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rodrigues.m | |||
@@ -0,0 +1,217 @@ | |||
1 | function [out,dout]=rodrigues(in) | ||
2 | |||
3 | % RODRIGUES Transform rotation matrix into rotation vector and viceversa. | ||
4 | % | ||
5 | % Sintax: [OUT]=RODRIGUES(IN) | ||
6 | % If IN is a 3x3 rotation matrix then OUT is the | ||
7 | % corresponding 3x1 rotation vector | ||
8 | % if IN is a rotation 3-vector then OUT is the | ||
9 | % corresponding 3x3 rotation matrix | ||
10 | % | ||
11 | |||
12 | %% | ||
13 | %% Copyright (c) March 1993 -- Pietro Perona | ||
14 | %% California Institute of Technology | ||
15 | %% | ||
16 | |||
17 | %% ALL CHECKED BY JEAN-YVES BOUGUET, October 1995. | ||
18 | %% FOR ALL JACOBIAN MATRICES !!! LOOK AT THE TEST AT THE END !! | ||
19 | |||
20 | %% BUG when norm(om)=pi fixed -- April 6th, 1997; | ||
21 | %% Jean-Yves Bouguet | ||
22 | |||
23 | |||
24 | [m,n] = size(in); | ||
25 | %bigeps = 10e+4*eps; | ||
26 | bigeps = 10e+20*eps; | ||
27 | |||
28 | if ((m==1) & (n==3)) | ((m==3) & (n==1)) %% it is a rotation vector | ||
29 | theta = norm(in); | ||
30 | if theta < eps | ||
31 | R = eye(3); | ||
32 | |||
33 | %if nargout > 1, | ||
34 | |||
35 | dRdin = [0 0 0; | ||
36 | 0 0 1; | ||
37 | 0 -1 0; | ||
38 | 0 0 -1; | ||
39 | 0 0 0; | ||
40 | 1 0 0; | ||
41 | 0 1 0; | ||
42 | -1 0 0; | ||
43 | 0 0 0]; | ||
44 | |||
45 | %end; | ||
46 | |||
47 | else | ||
48 | if n==length(in) in=in'; end; %% make it a column vec. if necess. | ||
49 | |||
50 | %m3 = [in,theta] | ||
51 | |||
52 | dm3din = [eye(3);in'/theta]; | ||
53 | |||
54 | omega = in/theta; | ||
55 | |||
56 | %m2 = [omega;theta] | ||
57 | |||
58 | dm2dm3 = [eye(3)/theta -in/theta^2; zeros(1,3) 1]; | ||
59 | |||
60 | alpha = cos(theta); | ||
61 | beta = sin(theta); | ||
62 | gamma = 1-cos(theta); | ||
63 | omegav=[[0 -omega(3) omega(2)];[omega(3) 0 -omega(1)];[-omega(2) omega(1) 0 ]]; | ||
64 | A = omega*omega'; | ||
65 | |||
66 | %m1 = [alpha;beta;gamma;omegav;A]; | ||
67 | |||
68 | dm1dm2 = zeros(21,4); | ||
69 | dm1dm2(1,4) = -sin(theta); | ||
70 | dm1dm2(2,4) = cos(theta); | ||
71 | dm1dm2(3,4) = sin(theta); | ||
72 | dm1dm2(4:12,1:3) = [0 0 0 0 0 1 0 -1 0; | ||
73 | 0 0 -1 0 0 0 1 0 0; | ||
74 | 0 1 0 -1 0 0 0 0 0]'; | ||
75 | |||
76 | w1 = omega(1); | ||
77 | w2 = omega(2); | ||
78 | w3 = omega(3); | ||
79 | |||
80 | dm1dm2(13:21,1) = [2*w1;w2;w3;w2;0;0;w3;0;0]; | ||
81 | dm1dm2(13: 21,2) = [0;w1;0;w1;2*w2;w3;0;w3;0]; | ||
82 | dm1dm2(13:21,3) = [0;0;w1;0;0;w2;w1;w2;2*w3]; | ||
83 | |||
84 | R = eye(3)*alpha + omegav*beta + A*gamma; | ||
85 | |||
86 | dRdm1 = zeros(9,21); | ||
87 | |||
88 | dRdm1([1 5 9],1) = ones(3,1); | ||
89 | dRdm1(:,2) = omegav(:); | ||
90 | dRdm1(:,4:12) = beta*eye(9); | ||
91 | dRdm1(:,3) = A(:); | ||
92 | dRdm1(:,13:21) = gamma*eye(9); | ||
93 | |||
94 | dRdin = dRdm1 * dm1dm2 * dm2dm3 * dm3din; | ||
95 | |||
96 | |||
97 | end; | ||
98 | out = R; | ||
99 | dout = dRdin; | ||
100 | |||
101 | %% it is prob. a rot matr. | ||
102 | elseif ((m==n) & (m==3) & (norm(in' * in - eye(3)) < bigeps)... | ||
103 | & (abs(det(in)-1) < bigeps)) | ||
104 | R = in; | ||
105 | |||
106 | |||
107 | |||
108 | tr = (trace(R)-1)/2; | ||
109 | dtrdR = [1 0 0 0 1 0 0 0 1]/2; | ||
110 | theta = real(acos(tr)); | ||
111 | |||
112 | |||
113 | if sin(theta) >= 1e-5, | ||
114 | |||
115 | dthetadtr = -1/sqrt(1-tr^2); | ||
116 | |||
117 | dthetadR = dthetadtr * dtrdR; | ||
118 | % var1 = [vth;theta]; | ||
119 | vth = 1/(2*sin(theta)); | ||
120 | dvthdtheta = -vth*cos(theta)/sin(theta); | ||
121 | dvar1dtheta = [dvthdtheta;1]; | ||
122 | |||
123 | dvar1dR = dvar1dtheta * dthetadR; | ||
124 | |||
125 | |||
126 | om1 = [R(3,2)-R(2,3), R(1,3)-R(3,1), R(2,1)-R(1,2)]'; | ||
127 | |||
128 | dom1dR = [0 0 0 0 0 1 0 -1 0; | ||
129 | 0 0 -1 0 0 0 1 0 0; | ||
130 | 0 1 0 -1 0 0 0 0 0]; | ||
131 | |||
132 | % var = [om1;vth;theta]; | ||
133 | dvardR = [dom1dR;dvar1dR]; | ||
134 | |||
135 | % var2 = [om;theta]; | ||
136 | om = vth*om1; | ||
137 | domdvar = [vth*eye(3) om1 zeros(3,1)]; | ||
138 | dthetadvar = [0 0 0 0 1]; | ||
139 | dvar2dvar = [domdvar;dthetadvar]; | ||
140 | |||
141 | |||
142 | out = om*theta; | ||
143 | domegadvar2 = [theta*eye(3) om]; | ||
144 | |||
145 | dout = domegadvar2 * dvar2dvar * dvardR; | ||
146 | |||
147 | |||
148 | else | ||
149 | if tr > 0; % case norm(om)=0; | ||
150 | |||
151 | out = [0 0 0]'; | ||
152 | |||
153 | dout = [0 0 0 0 0 1/2 0 -1/2 0; | ||
154 | 0 0 -1/2 0 0 0 1/2 0 0; | ||
155 | 0 1/2 0 -1/2 0 0 0 0 0]; | ||
156 | else % case norm(om)=pi; %% fixed April 6th | ||
157 | |||
158 | |||
159 | out = theta * (sqrt((diag(R)+1)/2).*[1;2*(R(1,2:3)>=0)'-1]); | ||
160 | %keyboard; | ||
161 | |||
162 | if nargout > 1, | ||
163 | fprintf(1,'WARNING!!!! Jacobian domdR undefined!!!\n'); | ||
164 | dout = NaN*ones(3,9); | ||
165 | end; | ||
166 | end; | ||
167 | end; | ||
168 | |||
169 | else | ||
170 | error('Neither a rotation matrix nor a rotation vector were provided'); | ||
171 | end; | ||
172 | |||
173 | return; | ||
174 | |||
175 | %% test of the Jacobians: | ||
176 | |||
177 | %%%% TEST OF dRdom: | ||
178 | om = randn(3,1); | ||
179 | dom = randn(3,1)/1000000; | ||
180 | |||
181 | [R1,dR1] = rodrigues(om); | ||
182 | R2 = rodrigues(om+dom); | ||
183 | |||
184 | R2a = R1 + reshape(dR1 * dom,3,3); | ||
185 | |||
186 | gain = norm(R2 - R1)/norm(R2 - R2a) | ||
187 | |||
188 | %%% TEST OF dOmdR: | ||
189 | om = randn(3,1); | ||
190 | R = rodrigues(om); | ||
191 | dom = randn(3,1)/10000; | ||
192 | dR = rodrigues(om+dom) - R; | ||
193 | |||
194 | [omc,domdR] = rodrigues(R); | ||
195 | [om2] = rodrigues(R+dR); | ||
196 | |||
197 | om_app = omc + domdR*dR(:); | ||
198 | |||
199 | gain = norm(om2 - omc)/norm(om2 - om_app) | ||
200 | |||
201 | |||
202 | %%% OTHER BUG: (FIXED NOW!!!) | ||
203 | |||
204 | omu = randn(3,1); | ||
205 | omu = omu/norm(omu) | ||
206 | om = pi*omu; | ||
207 | [R,dR]= rodrigues(om); | ||
208 | [om2] = rodrigues(R); | ||
209 | [om om2] | ||
210 | |||
211 | %%% NORMAL OPERATION | ||
212 | |||
213 | om = randn(3,1); | ||
214 | [R,dR]= rodrigues(om); | ||
215 | [om2] = rodrigues(R); | ||
216 | [om om2] | ||
217 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rotation.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rotation.m new file mode 100755 index 0000000..87ee2fe --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rotation.m | |||
@@ -0,0 +1,23 @@ | |||
1 | function [] = rotation(st); | ||
2 | |||
3 | if nargin < 1, | ||
4 | st= 1; | ||
5 | end; | ||
6 | |||
7 | |||
8 | fig = gcf; | ||
9 | |||
10 | ax = gca; | ||
11 | |||
12 | vv = get(ax,'view'); | ||
13 | |||
14 | az = vv(1); | ||
15 | el = vv(2); | ||
16 | |||
17 | for azi = az:-abs(st):az-360, | ||
18 | |||
19 | set(ax,'view',[azi el]); | ||
20 | figure(fig); | ||
21 | drawnow; | ||
22 | |||
23 | end; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/run_error_analysis.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/run_error_analysis.m new file mode 100755 index 0000000..095e17e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/run_error_analysis.m | |||
@@ -0,0 +1,65 @@ | |||
1 | %%% Program that launchs the complete | ||
2 | |||
3 | for N_ima_active = 1:30, | ||
4 | |||
5 | error_analysis; | ||
6 | |||
7 | end; | ||
8 | |||
9 | |||
10 | |||
11 | return; | ||
12 | |||
13 | |||
14 | f = []; | ||
15 | f_std = []; | ||
16 | |||
17 | c = []; | ||
18 | c_std = []; | ||
19 | |||
20 | k = []; | ||
21 | k_std = []; | ||
22 | |||
23 | NN = 30; | ||
24 | |||
25 | for rr = 1:NN, | ||
26 | |||
27 | load(['Calib_Accuracies_' num2str(rr)]); | ||
28 | |||
29 | [m1,s1] = mean_std_robust(fc_list(1,:)); | ||
30 | [m2,s2] = mean_std_robust(fc_list(2,:)); | ||
31 | |||
32 | f = [f [m1;m2]]; | ||
33 | f_std = [f_std [s1;s2]]; | ||
34 | |||
35 | [m1,s1] = mean_std_robust(cc_list(1,:)); | ||
36 | [m2,s2] = mean_std_robust(cc_list(2,:)); | ||
37 | |||
38 | c = [c [m1;m2]]; | ||
39 | c_std = [c_std [s1;s2]]; | ||
40 | |||
41 | [m1,s1] = mean_std_robust(kc_list(1,:)); | ||
42 | [m2,s2] = mean_std_robust(kc_list(2,:)); | ||
43 | [m3,s3] = mean_std_robust(kc_list(3,:)); | ||
44 | [m4,s4] = mean_std_robust(kc_list(4,:)); | ||
45 | |||
46 | k = [k [m1;m2;m3;m4]]; | ||
47 | k_std = [k_std [s1;s2;s3;s4]]; | ||
48 | |||
49 | end; | ||
50 | |||
51 | figure(1); | ||
52 | errorbar([1:NN;1:NN]',f',f_std'); | ||
53 | figure(2); | ||
54 | errorbar([1:NN;1:NN]',c',c_std'); | ||
55 | figure(3); | ||
56 | errorbar([1:NN;1:NN;1:NN;1:NN]',k',k_std'); | ||
57 | |||
58 | figure(4); | ||
59 | semilogy(f_std'); | ||
60 | |||
61 | figure(5); | ||
62 | semilogy(c_std'); | ||
63 | |||
64 | figure(6); | ||
65 | semilogy(k_std'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/saveinr.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/saveinr.m new file mode 100755 index 0000000..a176e39 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/saveinr.m | |||
@@ -0,0 +1,46 @@ | |||
1 | %SAVEINR Write an INRIMAGE format file | ||
2 | % | ||
3 | % SAVEINR(filename, im) | ||
4 | % | ||
5 | % Saves the specified image array in a INRIA image format file. | ||
6 | % | ||
7 | % SEE ALSO: loadinr | ||
8 | % | ||
9 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
10 | |||
11 | % Peter Corke 1996 | ||
12 | |||
13 | function saveinr(fname, im) | ||
14 | |||
15 | fid = fopen(fname, 'w'); | ||
16 | [r,c] = size(im'); | ||
17 | |||
18 | % build the header | ||
19 | hdr = []; | ||
20 | s = sprintf('#INRIMAGE-4#{\n'); | ||
21 | hdr = [hdr s]; | ||
22 | s = sprintf('XDIM=%d\n',c); | ||
23 | hdr = [hdr s]; | ||
24 | s = sprintf('YDIM=%d\n',r); | ||
25 | hdr = [hdr s]; | ||
26 | s = sprintf('ZDIM=1\n'); | ||
27 | hdr = [hdr s]; | ||
28 | s = sprintf('VDIM=1\n'); | ||
29 | hdr = [hdr s]; | ||
30 | s = sprintf('TYPE=float\n'); | ||
31 | hdr = [hdr s]; | ||
32 | s = sprintf('PIXSIZE=32\n'); | ||
33 | hdr = [hdr s]; | ||
34 | s = sprintf('SCALE=2**0\n'); | ||
35 | hdr = [hdr s]; | ||
36 | s = sprintf('CPU=sun\n#'); | ||
37 | hdr = [hdr s]; | ||
38 | |||
39 | % make it 256 bytes long and write it | ||
40 | hdr256 = zeros(1,256); | ||
41 | hdr256(1:length(hdr)) = hdr; | ||
42 | fwrite(fid, hdr256, 'uchar'); | ||
43 | |||
44 | % now the binary data | ||
45 | fwrite(fid, im', 'float32'); | ||
46 | fclose(fid) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/savepgm.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/savepgm.m new file mode 100755 index 0000000..397f028 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/savepgm.m | |||
@@ -0,0 +1,22 @@ | |||
1 | %SAVEPGM Write a PGM format file | ||
2 | % | ||
3 | % SAVEPGM(filename, im) | ||
4 | % | ||
5 | % Saves the specified image array in a binary (P5) format PGM image file. | ||
6 | % | ||
7 | % SEE ALSO: loadpgm | ||
8 | % | ||
9 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
10 | |||
11 | |||
12 | % Peter Corke 1994 | ||
13 | |||
14 | function savepgm(fname, im) | ||
15 | |||
16 | fid = fopen(fname, 'w'); | ||
17 | [r,c] = size(im'); | ||
18 | fprintf(fid, 'P5\n'); | ||
19 | fprintf(fid, '%d %d\n', r, c); | ||
20 | fprintf(fid, '255\n'); | ||
21 | fwrite(fid, im', 'uchar'); | ||
22 | fclose(fid) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/saveppm.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/saveppm.m new file mode 100755 index 0000000..0062ee0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/saveppm.m | |||
@@ -0,0 +1,25 @@ | |||
1 | %SAVEPPM Write a PPM format file | ||
2 | % | ||
3 | % SAVEPPM(filename, r, g, b) | ||
4 | % | ||
5 | % Saves the specified red, green and blue planes in a binary (P6) | ||
6 | % format PPM image file. | ||
7 | % | ||
8 | % SEE ALSO: loadppm | ||
9 | % | ||
10 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
11 | |||
12 | |||
13 | % Peter Corke 1994 | ||
14 | |||
15 | function saveppm(fname, R, G, B) | ||
16 | |||
17 | fid = fopen(fname, 'w'); | ||
18 | [r,c] = size(R'); | ||
19 | fprintf(fid, 'P6\n'); | ||
20 | fprintf(fid, '%d %d\n', r, c); | ||
21 | fprintf(fid, '255\n'); | ||
22 | im = [R(:) G(:) B(:)]; | ||
23 | im = reshape(c,r); | ||
24 | fwrite(fid, im, 'uchar'); | ||
25 | fclose(fid) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/saving_calib.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/saving_calib.m new file mode 100755 index 0000000..e0575e0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/saving_calib.m | |||
@@ -0,0 +1,27 @@ | |||
1 | fprintf(1,'\nSaving calibration results under Calib_Results.mat\n'); | ||
2 | |||
3 | check_active_images; | ||
4 | |||
5 | if ~exist('solution_init'), solution_init = []; end; | ||
6 | |||
7 | for kk = 1:n_ima, | ||
8 | if ~exist(['dX_' num2str(kk)]), eval(['dX_' num2str(kk) '= dX;']); end; | ||
9 | if ~exist(['dY_' num2str(kk)]), eval(['dY_' num2str(kk) '= dY;']); end; | ||
10 | end; | ||
11 | |||
12 | if ~exist('param_list'), | ||
13 | param_list = solution; | ||
14 | end; | ||
15 | |||
16 | |||
17 | string_save = 'save Calib_Results param_list active_images ind_active fc kc cc ex x y solution sol_with_center solution_init wintx winty n_ima type_numbering N_slots small_calib_image first_num image_numbers format_image calib_name Hcal Wcal nx ny map dX_default dY_default KK inv_KK dX dY'; | ||
18 | |||
19 | for kk = 1:n_ima, | ||
20 | string_save = [string_save ' X_' num2str(kk) ' x_' num2str(kk) ' y_' num2str(kk) ' ex_' num2str(kk) ' omc_' num2str(kk) ' Rc_' num2str(kk) ' Tc_' num2str(kk) ' H_' num2str(kk) ' n_sq_x_' num2str(kk) ' n_sq_y_' num2str(kk) ' wintx_' num2str(kk) ' winty_' num2str(kk) ' dX_' num2str(kk) ' dY_' num2str(kk)]; | ||
21 | end; | ||
22 | |||
23 | %fprintf(1,'To load later click on Load\n'); | ||
24 | |||
25 | fprintf(1,'done\n'); | ||
26 | |||
27 | eval(string_save); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/script_fit_distortion.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/script_fit_distortion.m new file mode 100755 index 0000000..c5e5430 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/script_fit_distortion.m | |||
@@ -0,0 +1,39 @@ | |||
1 | |||
2 | satis_distort = 0; | ||
3 | |||
4 | disp(['Estimated focal: ' num2str(f_g) ' pixels']); | ||
5 | |||
6 | while ~satis_distort, | ||
7 | |||
8 | k_g = input('Guess for distortion factor kc ([]=0): '); | ||
9 | |||
10 | if isempty(k_g), k_g = 0; end; | ||
11 | |||
12 | xy_corners_undist = comp_distortion2([x' - c_g(1);y'-c_g(2)]/f_g,k_g); | ||
13 | |||
14 | xu = xy_corners_undist(1,:)'; | ||
15 | yu = xy_corners_undist(2,:)'; | ||
16 | |||
17 | [XXu] = projectedGrid ( [xu(1);yu(1)], [xu(2);yu(2)],[xu(3);yu(3)], [xu(4);yu(4)],n_sq_x+1,n_sq_y+1); % The full grid | ||
18 | |||
19 | XX = (ones(2,1)*(1 + k_g * sum(XXu.^2))) .* XXu; | ||
20 | XX(1,:) = f_g*XX(1,:)+c_g(1); | ||
21 | XX(2,:) = f_g*XX(2,:)+c_g(2); | ||
22 | |||
23 | figure(2); | ||
24 | image(I); | ||
25 | colormap(map); | ||
26 | zoom on; | ||
27 | hold on; | ||
28 | %plot(f_g*XXu(1,:)+c_g(1),f_g*XXu(2,:)+c_g(2),'ro'); | ||
29 | plot(XX(1,:),XX(2,:),'r+'); | ||
30 | title('The red crosses should be on the grid corners...'); | ||
31 | hold off; | ||
32 | |||
33 | satis_distort = input('Satisfied with distortion? ([]=no, other=yes) '); | ||
34 | |||
35 | satis_distort = ~isempty(satis_distort); | ||
36 | |||
37 | |||
38 | end; | ||
39 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_no_center.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_no_center.m new file mode 100755 index 0000000..15508e5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_no_center.m | |||
@@ -0,0 +1,19 @@ | |||
1 | %%% Selection of the calibration solution with center estimation | ||
2 | |||
3 | if ~exist('sol_no_center'), | ||
4 | fprintf(1,'Need to calibrate before selecting solution without center. Maybe need to load Calib_Results.mat file.\n'); | ||
5 | return; | ||
6 | end; | ||
7 | |||
8 | solution = sol_no_center; | ||
9 | |||
10 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
11 | |||
12 | extract_parameters; | ||
13 | comp_error_calib; | ||
14 | |||
15 | fprintf(1,'\n\nCalibration results without principal point estimation:\n\n'); | ||
16 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
17 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
18 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
19 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_no_center3D.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_no_center3D.m new file mode 100755 index 0000000..070d81c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_no_center3D.m | |||
@@ -0,0 +1,20 @@ | |||
1 | %%% Selection of the calibration solution with center estimation | ||
2 | |||
3 | solution = sol_no_center; | ||
4 | |||
5 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
6 | |||
7 | extract_parameters3D; | ||
8 | |||
9 | |||
10 | fprintf(1,'\n\nCalibration results without principal point estimation:\n\n'); | ||
11 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
12 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
13 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
14 | |||
15 | |||
16 | %%%%%%%%%%%%%%%%%%%% GRAPHICAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%% | ||
17 | |||
18 | graphout_calib3D; | ||
19 | |||
20 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_with_center.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_with_center.m new file mode 100755 index 0000000..2df9ba8 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_with_center.m | |||
@@ -0,0 +1,19 @@ | |||
1 | %%% Selection of the calibration solution with center estimation | ||
2 | |||
3 | if ~exist('sol_with_center'), | ||
4 | fprintf(1,'Need to calibrate before selecting solution with center. Maybe need to load Calib_Results.mat file.\n'); | ||
5 | return; | ||
6 | end; | ||
7 | |||
8 | solution = sol_with_center; | ||
9 | |||
10 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
11 | |||
12 | extract_parameters; | ||
13 | comp_error_calib; | ||
14 | |||
15 | fprintf(1,'\n\nCalibration results with principal point estimation:\n\n'); | ||
16 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
17 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
18 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
19 | fprintf(1,'Pixel error: err = [ %3.5f %3.5f]\n\n',err_std); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_with_center3D.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_with_center3D.m new file mode 100755 index 0000000..eb6f4bf --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/select_sol_with_center3D.m | |||
@@ -0,0 +1,20 @@ | |||
1 | %%% Selection of the calibration solution with center estimation | ||
2 | |||
3 | solution = sol_with_center; | ||
4 | |||
5 | %%% Extraction of the final intrinsic and extrinsic paramaters: | ||
6 | |||
7 | extract_parameters3D; | ||
8 | |||
9 | |||
10 | fprintf(1,'\n\nCalibration results with principal point estimation:\n\n'); | ||
11 | fprintf(1,'Focal Length: fc = [ %3.5f %3.5f]\n',fc); | ||
12 | fprintf(1,'Principal point: cc = [ %3.5f %3.5f]\n',cc); | ||
13 | fprintf(1,'Distortion: kc = [ %3.5f %3.5f %3.5f %3.5f]\n',kc); | ||
14 | |||
15 | |||
16 | %%%%%%%%%%%%%%%%%%%% GRAPHICAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%% | ||
17 | |||
18 | graphout_calib3D; | ||
19 | |||
20 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/startup.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/startup.m new file mode 100755 index 0000000..aad0fa4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/startup.m | |||
@@ -0,0 +1,9 @@ | |||
1 | % Main camera calibration toolbox: | ||
2 | |||
3 | calib_gui; | ||
4 | |||
5 | %calib_gui; | ||
6 | |||
7 | path(pwd,path); | ||
8 | |||
9 | format compact | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/test_3d.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/test_3d.m new file mode 100755 index 0000000..9f442f4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/test_3d.m | |||
@@ -0,0 +1,80 @@ | |||
1 | Rc_1 = rodrigues(omc_1); | ||
2 | Rc_2 = rodrigues(omc_2); | ||
3 | Rc_3 = rodrigues(omc_3); | ||
4 | Rc_4 = rodrigues(omc_4); | ||
5 | Rc_5 = rodrigues(omc_5); | ||
6 | Rc_6 = rodrigues(omc_6); | ||
7 | Rc_7 = rodrigues(omc_7); | ||
8 | Rc_8 = rodrigues(omc_8); | ||
9 | Rc_9 = rodrigues(omc_9); | ||
10 | |||
11 | Rc_10 = rodrigues(omc_10); | ||
12 | Rc_11 = rodrigues(omc_11); | ||
13 | Rc_12 = rodrigues(omc_12); | ||
14 | Rc_13 = rodrigues(omc_13); | ||
15 | Rc_14 = rodrigues(omc_14); | ||
16 | Rc_15 = rodrigues(omc_15); | ||
17 | Rc_16 = rodrigues(omc_16); | ||
18 | Rc_17 = rodrigues(omc_17); | ||
19 | Rc_18 = rodrigues(omc_18); | ||
20 | |||
21 | |||
22 | |||
23 | RR1 = Rc_1'*Rc_10; % should be rodrigues([0;pi/2;0]) | ||
24 | TT1 = Rc_1'*(Tc_10-Tc_1); % should be [dX*n_sq_x_1;0;0] | ||
25 | |||
26 | Xr_1 = RR1 * X_10 + TT1*ones(1,length(X_10)); | ||
27 | |||
28 | figure(1); | ||
29 | plot3(X_1(1,:),X_1(2,:),X_1(3,:),'r+'); hold on; | ||
30 | plot3(Xr_1(1,:),Xr_1(2,:),Xr_1(3,:),'g+'); | ||
31 | hold off; | ||
32 | axis('equal'); | ||
33 | rotate3d on; | ||
34 | view(0,0); | ||
35 | xlabel('x'); | ||
36 | ylabel('y'); | ||
37 | zlabel('z'); | ||
38 | |||
39 | aaa = []; | ||
40 | |||
41 | RR1 = Rc_1'*Rc_10; % should be rodrigues([0;pi/2;0]) | ||
42 | TT1 = Rc_1'*(Tc_10-Tc_1); % should be [dX*n_sq_x_1;0;0] | ||
43 | err = rodrigues(RR1) - [0;pi/2;0] | ||
44 | aaa = [aaa 2*sin(err(2)/2)*.33*1000]; | ||
45 | |||
46 | RR2 = Rc_2'*Rc_11; % should be rodrigues([0;pi/2;0]) | ||
47 | TT2 = Rc_2'*(Tc_11-Tc_2); % should be [dX*n_sq_x_1;0;0] | ||
48 | err = rodrigues(RR2) - [0;pi/2;0] | ||
49 | aaa = [aaa 2*sin(err(2)/2)*.33*1000]; | ||
50 | |||
51 | RR3 = Rc_3'*Rc_12; % should be rodrigues([0;pi/2;0]) | ||
52 | TT3 = Rc_3'*(Tc_12-Tc_3); % should be [dX*n_sq_x_1;0;0] | ||
53 | err = rodrigues(RR3) - [0;pi/2;0] | ||
54 | aaa = [aaa 2*sin(err(2)/2)*.33*1000]; | ||
55 | |||
56 | RR4 = Rc_4'*Rc_13; % should be rodrigues([0;pi/2;0]) | ||
57 | TT4 = Rc_4'*(Tc_13-Tc_4); % should be [dX*n_sq_x_1;0;0] | ||
58 | err = rodrigues(RR4) - [0;pi/2;0] | ||
59 | aaa = [aaa 2*sin(err(2)/2)*.33*1000]; | ||
60 | |||
61 | RR5 = Rc_5'*Rc_14; % should be rodrigues([0;pi/2;0]) | ||
62 | TT5 = Rc_5'*(Tc_14-Tc_5); % should be [dX*n_sq_x_1;0;0] | ||
63 | err = rodrigues(RR5) - [0;pi/2;0] | ||
64 | aaa = [aaa 2*sin(err(2)/2)*.33*1000]; | ||
65 | |||
66 | RR6 = Rc_6'*Rc_15; % should be rodrigues([0;pi/2;0]) | ||
67 | TT6 = Rc_6'*(Tc_15-Tc_6); % should be [dX*n_sq_x_1;0;0] | ||
68 | err = rodrigues(RR6) - [0;pi/2;0] | ||
69 | aaa = [aaa 2*sin(err(2)/2)*.33*1000]; | ||
70 | |||
71 | RR7 = Rc_7'*Rc_16; % should be rodrigues([0;pi/2;0]) | ||
72 | TT7 = Rc_7'*(Tc_16-Tc_7); % should be [dX*n_sq_x_1;0;0] | ||
73 | err = rodrigues(RR7) - [0;pi/2;0] | ||
74 | aaa = [aaa 2*sin(err(2)/2)*.33*1000]; | ||
75 | |||
76 | RR8 = Rc_8'*Rc_17; % should be rodrigues([0;pi/2;0]) | ||
77 | TT8 = Rc_8'*(Tc_17-Tc_8); % should be [dX*n_sq_x_1;0;0] | ||
78 | err = rodrigues(RR8) - [0;pi/2;0] | ||
79 | aaa = [aaa 2*sin(err(2)/2)*.33*1000]; | ||
80 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/undistort_image.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/undistort_image.m new file mode 100755 index 0000000..6393d78 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/undistort_image.m | |||
@@ -0,0 +1,88 @@ | |||
1 | %%% INPUT THE IMAGE FILE NAME: | ||
2 | |||
3 | dir; | ||
4 | |||
5 | fprintf(1,'\n'); | ||
6 | disp('Program that undistort an image'); | ||
7 | disp('The intrinsic camera parameters are assumed to be known (previously computed)'); | ||
8 | |||
9 | fprintf(1,'\n'); | ||
10 | image_name = input('Image name (full name without extension): ','s'); | ||
11 | |||
12 | format_image2 = '0'; | ||
13 | |||
14 | while format_image2 == '0', | ||
15 | |||
16 | format_image2 = input('Image format: ([]=''r''=''ras'', ''b''=''bmp'', ''t''=''tif'', ''p''=''pgm'', ''j''=''jpg'') ','s'); | ||
17 | |||
18 | if isempty(format_image2), | ||
19 | format_image2 = 'ras'; | ||
20 | end; | ||
21 | |||
22 | if lower(format_image2(1)) == 'b', | ||
23 | format_image2 = 'bmp'; | ||
24 | else | ||
25 | if lower(format_image2(1)) == 't', | ||
26 | format_image2 = 'tif'; | ||
27 | else | ||
28 | if lower(format_image2(1)) == 'p', | ||
29 | format_image2 = 'pgm'; | ||
30 | else | ||
31 | if lower(format_image2(1)) == 'j', | ||
32 | format_image2 = 'jpg'; | ||
33 | else | ||
34 | if lower(format_image2(1)) == 'r', | ||
35 | format_image2 = 'ras'; | ||
36 | else | ||
37 | disp('Invalid image format'); | ||
38 | format_image2 = '0'; % Ask for format once again | ||
39 | end; | ||
40 | end; | ||
41 | end; | ||
42 | end; | ||
43 | end; | ||
44 | end; | ||
45 | |||
46 | ima_name = [image_name '.' format_image]; | ||
47 | |||
48 | |||
49 | |||
50 | %%% READ IN IMAGE: | ||
51 | |||
52 | if format_image(1) == 'p', | ||
53 | I = double(pgmread(ima_name)); | ||
54 | else | ||
55 | if format_image(1) == 'r', | ||
56 | I = readras(ima_name); | ||
57 | else | ||
58 | I = double(imread(ima_name)); | ||
59 | end; | ||
60 | end; | ||
61 | |||
62 | if size(I,3)>1, | ||
63 | I = I(:,:,2); | ||
64 | end; | ||
65 | |||
66 | |||
67 | %% SHOW THE ORIGINAL IMAGE: | ||
68 | |||
69 | figure(2); | ||
70 | image(I); | ||
71 | colormap(gray(256)); | ||
72 | title('Original image (with distortion) - Stored in array I'); | ||
73 | drawnow; | ||
74 | |||
75 | |||
76 | %% UNDISTORT THE IMAGE: | ||
77 | |||
78 | fprintf(1,'Compututing the undistorted image...\n') | ||
79 | |||
80 | [I2] = rect(I,eye(3),fc,cc,kc,KK); | ||
81 | |||
82 | |||
83 | figure(3); | ||
84 | image(I2); | ||
85 | colormap(gray(256)); | ||
86 | title('Undistorted image - Stored in array I2'); | ||
87 | drawnow; | ||
88 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/writeras.m b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/writeras.m new file mode 100755 index 0000000..c7eb7bc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/writeras.m | |||
@@ -0,0 +1,105 @@ | |||
1 | function writeras(filename, image, map); | ||
2 | %WRITERAS Write an image file in sun raster format. | ||
3 | % WRITERAS('imagefile.ras', image_matrix, map) writes a | ||
4 | % "sun.raster" image file. | ||
5 | |||
6 | % Written by Thomas K. Leung 3/30/93. | ||
7 | % @ California Institute of Technology. | ||
8 | |||
9 | |||
10 | % PC and UNIX version of writeras - Jean-Yves Bouguet - Dec. 1998 | ||
11 | |||
12 | dot = max(find(filename == '.')); | ||
13 | suffix = filename(dot+1:dot+3); | ||
14 | |||
15 | if nargin < 3, | ||
16 | map = []; | ||
17 | end; | ||
18 | |||
19 | if(strcmp(suffix, 'ras')) | ||
20 | %Write header | ||
21 | |||
22 | fp = fopen(filename, 'wb'); | ||
23 | if(fp < 0) error(['Cannot open ' filename '.']), end | ||
24 | |||
25 | [height, width] = size(image); | ||
26 | image = image - 1; | ||
27 | mapsize = size(map, 1)*size(map,2); | ||
28 | %fwrite(fp, ... | ||
29 | % [1504078485, width, height, 8, height*width, 1, 1, mapsize], ... | ||
30 | % 'long'); | ||
31 | |||
32 | |||
33 | zero_str = '00000000'; | ||
34 | |||
35 | % MAGIC NUMBER: | ||
36 | |||
37 | |||
38 | fwrite(fp,89,'uchar'); | ||
39 | fwrite(fp,166,'uchar'); | ||
40 | fwrite(fp,106,'uchar'); | ||
41 | fwrite(fp,149,'uchar'); | ||
42 | |||
43 | width_str = dec2hex(width); | ||
44 | width_str = [zero_str(1:8-length(width_str)) width_str]; | ||
45 | |||
46 | for ii = 1:2:7, | ||
47 | fwrite(fp,hex2dec(width_str(ii:ii+1)),'uchar'); | ||
48 | end; | ||
49 | |||
50 | |||
51 | height_str = dec2hex(height); | ||
52 | height_str = [zero_str(1:8-length(height_str)) height_str]; | ||
53 | |||
54 | for ii = 1:2:7, | ||
55 | fwrite(fp,hex2dec(height_str(ii:ii+1)),'uchar'); | ||
56 | end; | ||
57 | |||
58 | fwrite(fp,0,'uchar'); | ||
59 | fwrite(fp,0,'uchar'); | ||
60 | fwrite(fp,0,'uchar'); | ||
61 | fwrite(fp,8,'uchar'); | ||
62 | |||
63 | ll = height*width; | ||
64 | ll_str = dec2hex(ll); | ||
65 | ll_str = [zero_str(1:8-length(ll_str)) ll_str]; | ||
66 | |||
67 | for ii = 1:2:7, | ||
68 | fwrite(fp,hex2dec(ll_str(ii:ii+1)),'uchar'); | ||
69 | end; | ||
70 | |||
71 | fwrite(fp,0,'uchar'); | ||
72 | fwrite(fp,0,'uchar'); | ||
73 | fwrite(fp,0,'uchar'); | ||
74 | fwrite(fp,1,'uchar'); | ||
75 | fwrite(fp,0,'uchar'); | ||
76 | fwrite(fp,0,'uchar'); | ||
77 | fwrite(fp,0,'uchar'); | ||
78 | fwrite(fp,~~mapsize,'uchar'); | ||
79 | |||
80 | mapsize_str = dec2hex(mapsize); | ||
81 | mapsize_str = [zero_str(1:8-length(mapsize_str)) mapsize_str]; | ||
82 | |||
83 | %keyboard; | ||
84 | |||
85 | for ii = 1:2:7, | ||
86 | fwrite(fp,hex2dec(mapsize_str(ii:ii+1)),'uchar'); | ||
87 | end; | ||
88 | |||
89 | |||
90 | if mapsize ~= 0 | ||
91 | map = min(255, fix(255*map)); | ||
92 | fwrite(fp, map, 'uchar'); | ||
93 | end | ||
94 | if rem(width,2) == 1 | ||
95 | image = [image'; zeros(1, height)]'; | ||
96 | top = 255 * ones(size(image)); | ||
97 | fwrite(fp, min(top,image)', 'uchar'); | ||
98 | else | ||
99 | top = 255 * ones(size(image)); | ||
100 | fwrite(fp, min(top,image)', 'uchar'); | ||
101 | end | ||
102 | fclose(fp); | ||
103 | else | ||
104 | error('Image file name must end in either ''ras'' or ''rast''.'); | ||
105 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj2/TOOLBOX_calib.tar b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj2/TOOLBOX_calib.tar new file mode 100755 index 0000000..36d5de9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj2/TOOLBOX_calib.tar | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/Ncut_IC.m b/SD-VBS/common/toolbox/toolbox_basic/common/Ncut_IC.m new file mode 100755 index 0000000..d612780 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/Ncut_IC.m | |||
@@ -0,0 +1,26 @@ | |||
1 | function [v,d] = Ncut_IC(I,nb_radius_IC); | ||
2 | % | ||
3 | % [v,d] = Ncut_IC(I,nb_radius_IC); | ||
4 | % | ||
5 | |||
6 | if nargin<2, | ||
7 | nb_radius_IC = 5; | ||
8 | end | ||
9 | |||
10 | I = I/max(I(:)); | ||
11 | |||
12 | eg_par = [16,2, 21,3]; eg_th = 0; | ||
13 | |||
14 | nv = 11; reg_fac = 0.01; | ||
15 | |||
16 | [ex,ey,egx,egy,eg_par,eg_th,emag,ephase] = quadedgep(I,eg_par,eg_th); | ||
17 | |||
18 | |||
19 | nb_radius_IC= 10; | ||
20 | sample_rate = 0.2; | ||
21 | disp('setupW\n'); | ||
22 | [w_i,w_j] = cimgnbmap(size(I),nb_radius_IC,sample_rate); | ||
23 | w = affinityic(emag,ephase,w_i,w_j); | ||
24 | disp('computeNcut'); | ||
25 | [v,d] = ncut(w,nv,[],reg_fac); | ||
26 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/Ncut_IC_m.m b/SD-VBS/common/toolbox/toolbox_basic/common/Ncut_IC_m.m new file mode 100755 index 0000000..146acf9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/Ncut_IC_m.m | |||
@@ -0,0 +1,42 @@ | |||
1 | function [v,d] = Ncut_IC_m(I,mask,nb_radius_IC,sig_IC); | ||
2 | % | ||
3 | % [v,d] = Ncut_IC_m(I,mask,nb_radius_IC,sig_IC); | ||
4 | % | ||
5 | |||
6 | if nargin<2, | ||
7 | mask = ones(size(I)); | ||
8 | end | ||
9 | |||
10 | if nargin<3, | ||
11 | nb_radius_IC = 10; | ||
12 | end | ||
13 | |||
14 | if nargin<4, | ||
15 | sig_IC = 0.03; | ||
16 | end | ||
17 | |||
18 | %%% normalize the image | ||
19 | I = I/max(I(:)); | ||
20 | |||
21 | %%% edge detecting parameter, [num_ori, sig, win_size, enlongation factor] | ||
22 | eg_par = [6,2, 21,3]; eg_th = 0.01; | ||
23 | |||
24 | %% number of eigenvectors+ regulization factors | ||
25 | nv = 10; reg_fac = 0.0; | ||
26 | |||
27 | %% compute the edge response | ||
28 | [ex,ey,egx,egy,eg_par,eg_th,emag,ephase] = quadedgep(I,eg_par,eg_th); | ||
29 | |||
30 | %%% setup Wij connection pattern | ||
31 | sample_rate = 0.1; | ||
32 | [w_i,w_j] = cimgnbmap(size(I),nb_radius_IC,sample_rate); | ||
33 | |||
34 | %%% compute Wij with IC | ||
35 | emag = mask.*emag; | ||
36 | w = affinityic(emag,ephase,w_i,w_j,sig_IC); | ||
37 | |||
38 | %show_dist_w(I,w); | ||
39 | %%% running Ncut | ||
40 | [v,d] = ncut(w,nv); | ||
41 | |||
42 | v = reshape(v,size(I,1),size(I,2),size(v,2)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/Ncut_IC_m2.m b/SD-VBS/common/toolbox/toolbox_basic/common/Ncut_IC_m2.m new file mode 100755 index 0000000..9668b19 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/Ncut_IC_m2.m | |||
@@ -0,0 +1,51 @@ | |||
1 | function [v,d] = Ncut_IC_m(I,mask,nb_radius_IC,sig_IC); | ||
2 | % | ||
3 | % [v,d] = Ncut_IC_m(I,mask,nb_radius_IC,sig_IC); | ||
4 | % | ||
5 | |||
6 | if nargin<2, | ||
7 | mask = ones(size(I)); | ||
8 | end | ||
9 | |||
10 | if nargin<3, | ||
11 | nb_radius_IC = 10; | ||
12 | end | ||
13 | |||
14 | if nargin<4, | ||
15 | sig_IC = 0.03; | ||
16 | end | ||
17 | |||
18 | %%% normalize the image | ||
19 | I = I/max(I(:)); | ||
20 | |||
21 | %%% edge detecting parameter, [num_ori, sig, win_size, enlongation factor] | ||
22 | eg_par = [6,2, 21,3]; eg_th = 0.01; | ||
23 | |||
24 | %% number of eigenvectors+ regulization factors | ||
25 | nv = 10; reg_fac = 0.0; | ||
26 | |||
27 | %% compute the edge response | ||
28 | [nr,nc,nb] = size(I); | ||
29 | emag = zeros(nr,nc); | ||
30 | ephase = zeros(nr,nc); | ||
31 | for j=1:nb, | ||
32 | [ex,ey,egx,egy,eg_par,eg_th,emag1,ephase1] = quadedgep(I(:,:,j),eg_par,eg_th); | ||
33 | mask = emag1>emag; | ||
34 | ephase = ephase+ mask.*ephase1; | ||
35 | emag = emag + mask.*emag1; | ||
36 | end | ||
37 | |||
38 | |||
39 | %%% setup Wij connection pattern | ||
40 | sample_rate = 0.1; | ||
41 | [w_i,w_j] = cimgnbmap(size(I),nb_radius_IC,sample_rate); | ||
42 | |||
43 | %%% compute Wij with IC | ||
44 | emag = mask.*emag; | ||
45 | w = affinityic(emag,ephase,w_i,w_j,sig_IC); | ||
46 | |||
47 | %show_dist_w(I,w); | ||
48 | %%% running Ncut | ||
49 | [v,d] = ncut(w,nv); | ||
50 | |||
51 | v = reshape(v,size(I,1),size(I,2),size(v,2)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/affinityic.c b/SD-VBS/common/toolbox/toolbox_basic/common/affinityic.c new file mode 100755 index 0000000..e48762a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/affinityic.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /*================================================================ | ||
2 | * function w = affinityic(emag,ephase,pi,pj,sigma) | ||
3 | * Input: | ||
4 | * emag = edge strength at each pixel | ||
5 | * ephase = edge phase at each pixel | ||
6 | * [pi,pj] = index pair representation for MALTAB sparse matrices | ||
7 | * sigma = sigma for IC energy | ||
8 | * Output: | ||
9 | * w = affinity with IC at [pi,pj] | ||
10 | * | ||
11 | |||
12 | % test sequence | ||
13 | f = synimg(10); | ||
14 | [i,j] = cimgnbmap(size(f),2); | ||
15 | [ex,ey,egx,egy] = quadedgep(f); | ||
16 | a = affinityic(ex,ey,egx,egy,i,j) | ||
17 | show_dist_w(f,a); | ||
18 | |||
19 | * Jianbo Shi, Stella X. Yu, Nov 19, 2001. | ||
20 | *=================================================================*/ | ||
21 | |||
22 | # include "mex.h" | ||
23 | # include "math.h" | ||
24 | |||
25 | void mexFunction( | ||
26 | int nargout, | ||
27 | mxArray *out[], | ||
28 | int nargin, | ||
29 | const mxArray *in[] | ||
30 | ) | ||
31 | { | ||
32 | /* declare variables */ | ||
33 | int nr, nc, np, total; | ||
34 | int i, j, k, ix, iy, jx, jy, ii, jj, iip1, jjp1, iip2, jjp2, step; | ||
35 | double sigma, di, dj, a, z, maxori, phase1, phase2, slope; | ||
36 | int *ir, *jc; | ||
37 | unsigned long *pi, *pj; | ||
38 | double *emag, *ephase, *w; | ||
39 | |||
40 | /* check argument */ | ||
41 | if (nargin<4) { | ||
42 | mexErrMsgTxt("Four input arguments required"); | ||
43 | } | ||
44 | if (nargout>1) { | ||
45 | mexErrMsgTxt("Too many output arguments"); | ||
46 | } | ||
47 | |||
48 | /* get edgel information */ | ||
49 | nr = mxGetM(in[0]); | ||
50 | nc = mxGetN(in[0]); | ||
51 | if ( nr*nc ==0 || nr != mxGetM(in[1]) || nc != mxGetN(in[1]) ) { | ||
52 | mexErrMsgTxt("Edge magnitude and phase shall be of the same image size"); | ||
53 | } | ||
54 | emag = mxGetPr(in[0]); | ||
55 | ephase = mxGetPr(in[1]); | ||
56 | np = nr * nc; | ||
57 | |||
58 | /* get new index pair */ | ||
59 | if (!mxIsUint32(in[2]) | !mxIsUint32(in[3])) { | ||
60 | mexErrMsgTxt("Index pair shall be of type UINT32"); | ||
61 | } | ||
62 | if (mxGetM(in[3]) * mxGetN(in[3]) != np + 1) { | ||
63 | mexErrMsgTxt("Wrong index representation"); | ||
64 | } | ||
65 | pi = mxGetData(in[2]); | ||
66 | pj = mxGetData(in[3]); | ||
67 | |||
68 | /* create output */ | ||
69 | out[0] = mxCreateSparse(np,np,pj[np],mxREAL); | ||
70 | if (out[0]==NULL) { | ||
71 | mexErrMsgTxt("Not enough memory for the output matrix"); | ||
72 | } | ||
73 | w = mxGetPr(out[0]); | ||
74 | ir = mxGetIr(out[0]); | ||
75 | jc = mxGetJc(out[0]); | ||
76 | |||
77 | /* find my sigma */ | ||
78 | if (nargin<5) { | ||
79 | sigma = 0; | ||
80 | for (k=0; k<np; k++) { | ||
81 | if (emag[k]>sigma) { sigma = emag[k]; } | ||
82 | } | ||
83 | sigma = sigma / 10; | ||
84 | printf("sigma = %6.5f",sigma); | ||
85 | } else { | ||
86 | sigma = mxGetScalar(in[4]); | ||
87 | } | ||
88 | a = 1.0/ (sigma); | ||
89 | |||
90 | /* computation */ | ||
91 | total = 0; | ||
92 | for (j=0; j<np; j++) { | ||
93 | |||
94 | jc[j] = total; | ||
95 | jx = j / nr; /* col */ | ||
96 | jy = j % nr; /* row */ | ||
97 | |||
98 | for (k=pj[j]; k<pj[j+1]; k++) { | ||
99 | |||
100 | i = pi[k]; | ||
101 | |||
102 | if (i==j) { | ||
103 | maxori = 1; | ||
104 | |||
105 | } else { | ||
106 | |||
107 | ix = i / nr; | ||
108 | iy = i % nr; | ||
109 | |||
110 | /* scan */ | ||
111 | di = (double) (iy - jy); | ||
112 | dj = (double) (ix - jx); | ||
113 | |||
114 | maxori = 0.; | ||
115 | phase1 = ephase[j]; | ||
116 | |||
117 | |||
118 | /* sample in i direction */ | ||
119 | if (abs(di) >= abs(dj)) { | ||
120 | slope = dj / di; | ||
121 | step = (iy>=jy) ? 1 : -1; | ||
122 | |||
123 | iip1 = jy; | ||
124 | jjp1 = jx; | ||
125 | |||
126 | |||
127 | for (ii=0;ii<abs(di);ii++){ | ||
128 | iip2 = iip1 + step; | ||
129 | jjp2 = (int)(0.5 + slope*(iip2-jy) + jx); | ||
130 | |||
131 | phase2 = ephase[iip2+jjp2*nr]; | ||
132 | |||
133 | if (phase1 != phase2) { | ||
134 | z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]); | ||
135 | if (z > maxori){ | ||
136 | maxori = z; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | iip1 = iip2; | ||
141 | jjp1 = jjp2; | ||
142 | phase1 = phase2; | ||
143 | } | ||
144 | |||
145 | /* sample in j direction */ | ||
146 | } else { | ||
147 | slope = di / dj; | ||
148 | step = (ix>=jx) ? 1: -1; | ||
149 | |||
150 | jjp1 = jx; | ||
151 | iip1 = jy; | ||
152 | |||
153 | |||
154 | for (jj=0;jj<abs(dj);jj++){ | ||
155 | jjp2 = jjp1 + step; | ||
156 | iip2 = (int)(0.5+ slope*(jjp2-jx) + jy); | ||
157 | |||
158 | phase2 = ephase[iip2+jjp2*nr]; | ||
159 | |||
160 | if (phase1 != phase2){ | ||
161 | z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]); | ||
162 | if (z > maxori){ | ||
163 | maxori = z; | ||
164 | } | ||
165 | |||
166 | } | ||
167 | |||
168 | iip1 = iip2; | ||
169 | jjp1 = jjp2; | ||
170 | phase1 = phase2; | ||
171 | } | ||
172 | } | ||
173 | |||
174 | maxori = 0.5 * maxori*a; | ||
175 | maxori = exp(-maxori*maxori); | ||
176 | } | ||
177 | ir[total] = i; | ||
178 | |||
179 | w[total] = maxori + 0.005; | ||
180 | total = total + 1; | ||
181 | |||
182 | } /* i */ | ||
183 | } /* j */ | ||
184 | |||
185 | jc[np] = total; | ||
186 | } | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/affinityic.mexa64 b/SD-VBS/common/toolbox/toolbox_basic/common/affinityic.mexa64 new file mode 100755 index 0000000..e60b4d1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/affinityic.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/affinityic.mexglx b/SD-VBS/common/toolbox/toolbox_basic/common/affinityic.mexglx new file mode 100755 index 0000000..5edb5d8 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/affinityic.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/anisodiff.m b/SD-VBS/common/toolbox/toolbox_basic/common/anisodiff.m new file mode 100755 index 0000000..b576d8f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/anisodiff.m | |||
@@ -0,0 +1,20 @@ | |||
1 | function [outimage] = anisodiff(inimage,iterations,K) | ||
2 | % [outimage] = anisodiff(inimage,iterations,K) | ||
3 | % Pietro's anisotropic diffusion routine | ||
4 | |||
5 | lambda = 0.25; | ||
6 | outimage = inimage; [m,n] = size(inimage); | ||
7 | |||
8 | rowC = [1:m]; rowN = [1 1:m-1]; rowS = [2:m m]; | ||
9 | colC = [1:n]; colE = [1 1:n-1]; colW = [2:n n]; | ||
10 | |||
11 | for i=1:iterations, | ||
12 | deltaN = outimage(rowN,colC) - outimage(rowC,colC); | ||
13 | deltaE = outimage(rowC,colE) - outimage(rowC,colC); | ||
14 | |||
15 | fluxN = deltaN .* exp( - ((1/K) * abs(deltaN)).^2 ); | ||
16 | fluxE = deltaE .* exp( - ((1/K) * abs(deltaE)).^2 ); | ||
17 | |||
18 | outimage = outimage + lambda * (fluxN - fluxN(rowS,colC) + fluxE - fluxE(rowC,colW)); | ||
19 | end | ||
20 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/bin.m b/SD-VBS/common/toolbox/toolbox_basic/common/bin.m new file mode 100755 index 0000000..e2c3c90 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/bin.m | |||
@@ -0,0 +1,39 @@ | |||
1 | function [i, nnbins] = bin(x, dx, x0, x1); | ||
2 | % | ||
3 | % [i, nbins] = bin(x, dx, x0, x1); | ||
4 | % | ||
5 | % Returns the vector of indices, starting from 1, | ||
6 | % corresponding to the chosen bin size, dx, | ||
7 | % start x0 and end x1. If x1 is omitted, x1 = max(x) - dx/2. | ||
8 | % If x0 is omitted, x0 = min(x) + dx/2. If dx is omitted, the data | ||
9 | % are divided into 10 classes. Note that outliers are not removed. | ||
10 | % | ||
11 | % Tested under MatLab 4.2, 5.0, and 5.1. | ||
12 | % | ||
13 | |||
14 | % 17.1.97, Oyvind.Breivik@gfi.uib.no. | ||
15 | % | ||
16 | % Oyvind Breivik | ||
17 | % Department of Geophysics | ||
18 | % University of Bergen | ||
19 | % NORWAY | ||
20 | |||
21 | N = 10; % Default is 10 classes | ||
22 | |||
23 | if nargin < 2 | ||
24 | dx = (max(x) - min(x))/N; | ||
25 | end | ||
26 | if nargin < 3 | ||
27 | x0 = min(x) + dx/2; | ||
28 | end | ||
29 | if nargin < 4 | ||
30 | x1 = max(x) - dx/2; | ||
31 | end | ||
32 | nbins = round((x1 - x0)/dx) + 1; | ||
33 | i = round((x - x0)/dx) + 1; | ||
34 | %in = (i >= 1) & (i <= nbins); % Indices are within range [1, nbins]. | ||
35 | %i = i(in); | ||
36 | |||
37 | if nargout > 1 | ||
38 | nnbins = nbins; | ||
39 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/cimgnbmap.c b/SD-VBS/common/toolbox/toolbox_basic/common/cimgnbmap.c new file mode 100755 index 0000000..1595b68 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/cimgnbmap.c | |||
@@ -0,0 +1,189 @@ | |||
1 | /*================================================================ | ||
2 | * function [i,j] = cimgnbmap([nr,nc], nb_r, sample_rate) | ||
3 | * computes the neighbourhood index matrix of an image, | ||
4 | * with each neighbourhood sampled. | ||
5 | * Input: | ||
6 | * [nr,nc] = image size | ||
7 | * nb_r = neighbourhood radius, could be [r_i,r_j] for i,j | ||
8 | * sample_rate = sampling rate, default = 1 | ||
9 | * Output: | ||
10 | * [i,j] = each is a column vector, give indices of neighbour pairs | ||
11 | * UINT32 type | ||
12 | * i is of total length of valid elements, 0 for first row | ||
13 | * j is of length nr * nc + 1 | ||
14 | * | ||
15 | * See also: imgnbmap.c, id2cind.m | ||
16 | * | ||
17 | * Examples: | ||
18 | * [i,j] = imgnbmap(10, 20); % [10,10] are assumed | ||
19 | * | ||
20 | * Stella X. Yu, Nov 12, 2001. | ||
21 | |||
22 | % test sequence: | ||
23 | nr = 15; | ||
24 | nc = 15; | ||
25 | nbr = 1; | ||
26 | [i,j] = cimgnbmap([nr,nc], nbr); | ||
27 | mask = csparse(i,j,ones(length(i),1),nr*nc); | ||
28 | show_dist_w(rand(nr,nc),mask) | ||
29 | |||
30 | *=================================================================*/ | ||
31 | |||
32 | # include "mex.h" | ||
33 | # include "math.h" | ||
34 | |||
35 | void mexFunction( | ||
36 | int nargout, | ||
37 | mxArray *out[], | ||
38 | int nargin, | ||
39 | const mxArray *in[] | ||
40 | ) | ||
41 | { | ||
42 | /* declare variables */ | ||
43 | int nr, nc, np, nb, total; | ||
44 | double *dim, sample_rate; | ||
45 | int r_i, r_j, a1, a2, b1, b2, self, neighbor; | ||
46 | int i, j, k, s, t, nsamp, th_rand, no_sample; | ||
47 | unsigned long *p, *qi, *qj; | ||
48 | |||
49 | /* check argument */ | ||
50 | if (nargin < 2) { | ||
51 | mexErrMsgTxt("Two input arguments required"); | ||
52 | } | ||
53 | if (nargout> 2) { | ||
54 | mexErrMsgTxt("Too many output arguments."); | ||
55 | } | ||
56 | |||
57 | /* get image size */ | ||
58 | i = mxGetM(in[0]); | ||
59 | j = mxGetN(in[0]); | ||
60 | dim = mxGetData(in[0]); | ||
61 | nr = (int)dim[0]; | ||
62 | if (j>1 || i>1) { | ||
63 | nc = (int)dim[1]; | ||
64 | } else { | ||
65 | nc = nr; | ||
66 | } | ||
67 | np = nr * nc; | ||
68 | |||
69 | /* get neighbourhood size */ | ||
70 | i = mxGetM(in[1]); | ||
71 | j = mxGetN(in[1]); | ||
72 | dim = mxGetData(in[1]); | ||
73 | r_i = (int)dim[0]; | ||
74 | if (j>1 || i>1) { | ||
75 | r_j = (int)dim[1]; | ||
76 | } else { | ||
77 | r_j = r_i; | ||
78 | } | ||
79 | if (r_i<0) { r_i = 0; } | ||
80 | if (r_j<0) { r_j = 0; } | ||
81 | |||
82 | /* get sample rate */ | ||
83 | if (nargin==3) { | ||
84 | sample_rate = (mxGetM(in[2])==0) ? 1: mxGetScalar(in[2]); | ||
85 | } else { | ||
86 | sample_rate = 1; | ||
87 | } | ||
88 | /* prepare for random number generator */ | ||
89 | if (sample_rate<1) { | ||
90 | srand( (unsigned)time( NULL ) ); | ||
91 | th_rand = (int)ceil((double)RAND_MAX * sample_rate); | ||
92 | no_sample = 0; | ||
93 | } else { | ||
94 | sample_rate = 1; | ||
95 | th_rand = RAND_MAX; | ||
96 | no_sample = 1; | ||
97 | } | ||
98 | |||
99 | /* figure out neighbourhood size */ | ||
100 | |||
101 | nb = (r_i + r_i + 1) * (r_j + r_j + 1); | ||
102 | if (nb>np) { | ||
103 | nb = np; | ||
104 | } | ||
105 | nb = (int)ceil((double)nb * sample_rate); | ||
106 | |||
107 | /* intermediate data structure */ | ||
108 | p = mxCalloc(np * (nb+1), sizeof(unsigned long)); | ||
109 | if (p==NULL) { | ||
110 | mexErrMsgTxt("Not enough space for my computation."); | ||
111 | } | ||
112 | |||
113 | /* computation */ | ||
114 | total = 0; | ||
115 | for (j=0; j<nc; j++) { | ||
116 | for (i=0; i<nr; i++) { | ||
117 | |||
118 | self = i + j * nr; | ||
119 | |||
120 | /* put self in, otherwise the index is not ordered */ | ||
121 | p[self] = p[self] + 1; | ||
122 | p[self+p[self]*np] = self; | ||
123 | |||
124 | /* j range */ | ||
125 | b1 = j; | ||
126 | b2 = j + r_j; | ||
127 | if (b2>=nc) { b2 = nc-1; } | ||
128 | |||
129 | /* i range */ | ||
130 | a1 = i - r_i; | ||
131 | if (a1<0) { a1 = 0; } | ||
132 | a2 = i + r_i; | ||
133 | if (a2>=nr) { a2 = nr-1; } | ||
134 | |||
135 | /* number of more samples needed */ | ||
136 | nsamp = nb - p[self]; | ||
137 | |||
138 | k = 0; | ||
139 | t = b1; | ||
140 | s = i + 1; | ||
141 | if (s>a2) { | ||
142 | s = a1; | ||
143 | t = t + 1; | ||
144 | } | ||
145 | while (k<nsamp && t<=b2) { | ||
146 | if (no_sample || (rand()<th_rand)) { | ||
147 | k = k + 1; | ||
148 | neighbor = s + t * nr; | ||
149 | |||
150 | p[self] = p[self] + 1; | ||
151 | p[self+p[self]*np] = neighbor; | ||
152 | |||
153 | p[neighbor] = p[neighbor] + 1; | ||
154 | p[neighbor+p[neighbor]*np] = self; | ||
155 | } | ||
156 | s = s + 1; | ||
157 | if (s>a2) { | ||
158 | s = a1; | ||
159 | t = t + 1; | ||
160 | } | ||
161 | } /* k */ | ||
162 | |||
163 | total = total + p[self]; | ||
164 | } /* i */ | ||
165 | } /* j */ | ||
166 | |||
167 | /* i, j */ | ||
168 | out[0] = mxCreateNumericMatrix(total, 1, mxUINT32_CLASS, mxREAL); | ||
169 | out[1] = mxCreateNumericMatrix(np+1, 1, mxUINT32_CLASS, mxREAL); | ||
170 | qi = mxGetData(out[0]); | ||
171 | qj = mxGetData(out[1]); | ||
172 | if (out[0]==NULL || out[1]==NULL) { | ||
173 | mexErrMsgTxt("Not enough space for the output matrix."); | ||
174 | } | ||
175 | |||
176 | total = 0; | ||
177 | for (j=0; j<np; j++) { | ||
178 | qj[j] = total; | ||
179 | s = j + np; | ||
180 | for (t=0; t<p[j]; t++) { | ||
181 | qi[total] = p[s]; | ||
182 | total = total + 1; | ||
183 | s = s + np; | ||
184 | } | ||
185 | } | ||
186 | qj[np] = total; | ||
187 | |||
188 | mxFree(p); | ||
189 | } | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/cimgnbmap.mexa64 b/SD-VBS/common/toolbox/toolbox_basic/common/cimgnbmap.mexa64 new file mode 100755 index 0000000..19eabe1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/cimgnbmap.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/cimgnbmap.mexglx b/SD-VBS/common/toolbox/toolbox_basic/common/cimgnbmap.mexglx new file mode 100755 index 0000000..6f3bb32 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/cimgnbmap.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/density.m b/SD-VBS/common/toolbox/toolbox_basic/common/density.m new file mode 100755 index 0000000..23d88cc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/density.m | |||
@@ -0,0 +1,133 @@ | |||
1 | function [rrho, xxvec, yyvec] = density(x, y, dx, dy, xy0); | ||
2 | % [rho, xvec, yvec] = density(x, y, dx, dy, [x0 x1 y0 y1]); | ||
3 | % | ||
4 | % 2-D probability density plot. | ||
5 | % | ||
6 | % Input: Vectors x and y of equal length. If dx and/or dy are omitted, the | ||
7 | % default is 75 bins in each direction. | ||
8 | % | ||
9 | % If dx or dy is negative, then the variable is taken as the number of | ||
10 | % bins rather than a grid resolution. | ||
11 | % | ||
12 | % The vector containing the limits can be padded with NaNs if only | ||
13 | % certain limits are desired, e g if x0 and y1 are wanted: | ||
14 | % | ||
15 | % density(x, y, [.5 nan nan 45]) | ||
16 | % | ||
17 | % Output: The density matrix RHO together with vectors XVEC and YVEC. | ||
18 | % If no output arguments are specified, DENSITY will plot the density | ||
19 | % function with the prescribed axes using PCOLOR. | ||
20 | % | ||
21 | % Requires bin.m. Tested under MatLab 4.2, 5.0, and 5.1. | ||
22 | % | ||
23 | % See also bin.m for further details about dx, x0, etc, and ffgrid.m. | ||
24 | % | ||
25 | |||
26 | % 1.9.97 Oyvind.Breivik@gfi.uib.no. | ||
27 | % | ||
28 | % Oyvind Breivik | ||
29 | % Department of Geophysics | ||
30 | % University of Bergen | ||
31 | % NORWAY | ||
32 | |||
33 | DX = -75; % Default grid size | ||
34 | |||
35 | x = x(:); | ||
36 | |||
37 | if nargin < 2 | ||
38 | y = x; | ||
39 | end | ||
40 | |||
41 | y = y(:); | ||
42 | |||
43 | xy = NaN*ones(1,4); | ||
44 | |||
45 | if (nargin < 4) | ||
46 | xy0 = min(x); | ||
47 | end | ||
48 | |||
49 | if (nargin == 3 & length(dx) > 1) | ||
50 | xy0 = dx; | ||
51 | dx = DX; | ||
52 | end | ||
53 | |||
54 | if nargin < 3 | ||
55 | dx = DX; | ||
56 | end | ||
57 | |||
58 | if (nargin == 4 & length(dy) > 1) | ||
59 | xy0 = dy; | ||
60 | dy = dx; | ||
61 | end | ||
62 | if nargin < 4 | ||
63 | dy = dx; | ||
64 | end | ||
65 | |||
66 | nxy = length(xy0); | ||
67 | xy(1:nxy) = xy0; | ||
68 | |||
69 | if (isnan(xy(4))) | ||
70 | xy(4) = max(y); | ||
71 | end | ||
72 | if (isnan(xy(3))) | ||
73 | xy(3) = min(y); | ||
74 | end | ||
75 | if (isnan(xy(2))) | ||
76 | xy(2) = max(x); | ||
77 | end | ||
78 | if (isnan(xy(1))) | ||
79 | xy(1) = min(x); | ||
80 | end | ||
81 | x0 = xy(1); x1 = xy(2); y0 = xy(3); y1 = xy(4); | ||
82 | |||
83 | if (dx < 0) | ||
84 | dx = (x1 - x0)/abs(dx); | ||
85 | end | ||
86 | if (dy < 0) | ||
87 | dy = (y1 - y0)/abs(dy); | ||
88 | end | ||
89 | |||
90 | ix = bin(x, dx, x0, x1); | ||
91 | iy = bin(y, dy, y0, y1); % bin data in (x,y)-space | ||
92 | |||
93 | xvec = x0:dx:x1; | ||
94 | yvec = y0:dy:y1; | ||
95 | |||
96 | nx = length(xvec); | ||
97 | ny = length(yvec); | ||
98 | |||
99 | inx = (ix >= 1) & (ix <= nx); | ||
100 | iny = (iy >= 1) & (iy <= ny); | ||
101 | in = (inx & iny); | ||
102 | ix = ix(in); iy = iy(in); | ||
103 | N = length(ix); % how many datapoints are left now? | ||
104 | |||
105 | rho = zeros(length(xvec), length(yvec)) + eps; | ||
106 | |||
107 | for i = 1:N | ||
108 | rho(ix(i), iy(i)) = rho(ix(i), iy(i)) + 1; | ||
109 | end | ||
110 | |||
111 | rho = rho/(N*dx*dy); % Density is n per dx per dy | ||
112 | |||
113 | rho = rho'; % Get in shape | ||
114 | |||
115 | if nargout == 0 | ||
116 | pcolor(xvec, yvec, sqrt(rho)); shading interp; axis image; | ||
117 | colorbar | ||
118 | colormap jet | ||
119 | xlabel(inputname(1)) | ||
120 | ylabel(inputname(2)) | ||
121 | dum = size(rho'); | ||
122 | str = sprintf('%d data points, grid: %dx%d', N, dum(1)-1, dum(2)-1); | ||
123 | title(str); | ||
124 | end | ||
125 | |||
126 | if nargout > 0 | ||
127 | rrho = rho; | ||
128 | end | ||
129 | |||
130 | if nargout > 1 | ||
131 | xxvec = xvec; | ||
132 | yyvec = yvec; | ||
133 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/find_edge.m b/SD-VBS/common/toolbox/toolbox_basic/common/find_edge.m new file mode 100755 index 0000000..4299c29 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/find_edge.m | |||
@@ -0,0 +1,24 @@ | |||
1 | function [edgemap,mag,th] = find_edge(I,sig,mag_thld) | ||
2 | % | ||
3 | % [edgemap,mag,th] = find_edge(I,sig,mag_thld) | ||
4 | % | ||
5 | |||
6 | if nargin<2, | ||
7 | sig = 1; | ||
8 | end | ||
9 | |||
10 | if nargin<3, | ||
11 | mag_thld = 1/30; | ||
12 | end | ||
13 | |||
14 | I = I/max(I(:)); | ||
15 | |||
16 | ismax = 1;r = 1; | ||
17 | |||
18 | [gx,gy] = grad(I,sig); | ||
19 | [th,mag] = cart2pol(gy,gx); | ||
20 | |||
21 | g = cat(3,gy,gx); | ||
22 | edgemap = nonmaxsup(g,ismax,r); | ||
23 | edgemap = edgemap.*(mag>mag_thld); | ||
24 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/grad.m b/SD-VBS/common/toolbox/toolbox_basic/common/grad.m new file mode 100755 index 0000000..05fce39 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/grad.m | |||
@@ -0,0 +1,28 @@ | |||
1 | % gradient of an image | ||
2 | % coordinates (r, c) follow matrix convention; | ||
3 | % the gaussian is truncated at x = +- tail, and there are samples samples | ||
4 | % inbetween, where samples = hsamples * 2 + 1 | ||
5 | |||
6 | function[gr,gc] = gradient(image, hsamples,cm) | ||
7 | |||
8 | if nargin <3, | ||
9 | cm = 'same'; | ||
10 | end | ||
11 | |||
12 | tail=4; | ||
13 | samples = hsamples * 2 + 1; | ||
14 | |||
15 | x = linspace(-tail, tail, samples); | ||
16 | gauss = exp(-x.^2); | ||
17 | n = gauss * ones(samples,1); | ||
18 | gauss = gauss/n; | ||
19 | |||
20 | gaussderiv = -x.*gauss; | ||
21 | n = -gaussderiv*linspace(1,samples,samples)'; | ||
22 | gaussderiv = gaussderiv/n; | ||
23 | |||
24 | %gr = conv2(conv2(image, gaussderiv','valid'), gauss,'valid'); | ||
25 | %gc = conv2(conv2(image, gaussderiv,'valid'), gauss','valid'); | ||
26 | |||
27 | gr = conv2(conv2(image, gaussderiv',cm), gauss,cm); | ||
28 | gc = conv2(conv2(image, gaussderiv,cm), gauss',cm); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/make_filterbank_even2.m b/SD-VBS/common/toolbox/toolbox_basic/common/make_filterbank_even2.m new file mode 100755 index 0000000..937f9bd --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/make_filterbank_even2.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function FB = make_filterbank(num_ori,filter_scales,wsz,enlong) | ||
2 | % | ||
3 | % F = make_filterbank(num_ori,num_scale,wsz) | ||
4 | % | ||
5 | |||
6 | if nargin<4, | ||
7 | enlong = 3; | ||
8 | end | ||
9 | |||
10 | enlong = 2*enlong | ||
11 | |||
12 | % definine filterbank | ||
13 | %num_ori=6; | ||
14 | %num_scale=3; | ||
15 | |||
16 | num_scale = length(filter_scales); | ||
17 | |||
18 | M1=wsz; % size in pixels | ||
19 | M2=M1; | ||
20 | |||
21 | ori_incr=180/num_ori; | ||
22 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
23 | |||
24 | FB=zeros(M1,M2,num_ori,num_scale); | ||
25 | |||
26 | % elongated filter set | ||
27 | counter = 1; | ||
28 | |||
29 | for m=1:num_scale | ||
30 | for n=1:num_ori | ||
31 | % r=12 here is equivalent to Malik's r=3; | ||
32 | f=doog2(filter_scales(m),enlong,ori_offset+(n-1)*ori_incr,M1); | ||
33 | FB(:,:,n,m)=f; | ||
34 | end | ||
35 | end | ||
36 | |||
37 | FB=reshape(FB,M1,M2,num_scale*num_ori); | ||
38 | total_num_filt=size(FB,3); | ||
39 | |||
40 | for j=1:total_num_filt, | ||
41 | F = FB(:,:,j); | ||
42 | a = sum(sum(abs(F))); | ||
43 | FB(:,:,j) = FB(:,:,j)/a; | ||
44 | end | ||
45 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/make_filterbank_odd2.m b/SD-VBS/common/toolbox/toolbox_basic/common/make_filterbank_odd2.m new file mode 100755 index 0000000..0059dca --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/make_filterbank_odd2.m | |||
@@ -0,0 +1,46 @@ | |||
1 | function FB = make_filterbank(num_ori,filter_scales,wsz,enlong) | ||
2 | % | ||
3 | % F = make_filterbank(num_ori,num_scale,wsz) | ||
4 | % | ||
5 | |||
6 | if nargin<4, | ||
7 | enlong = 3; | ||
8 | end | ||
9 | |||
10 | enlong = enlong*2; | ||
11 | |||
12 | % definine filterbank | ||
13 | %num_ori=6; | ||
14 | %num_scale=3; | ||
15 | |||
16 | num_scale = length(filter_scales); | ||
17 | |||
18 | M1=wsz; % size in pixels | ||
19 | M2=M1; | ||
20 | |||
21 | ori_incr=180/num_ori; | ||
22 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
23 | |||
24 | FB=zeros(M1,M2,num_ori,num_scale); | ||
25 | |||
26 | |||
27 | % elongated filter set | ||
28 | counter = 1; | ||
29 | |||
30 | for m=1:num_scale | ||
31 | for n=1:num_ori | ||
32 | % r=12 here is equivalent to Malik's r=3; | ||
33 | f=doog1(filter_scales(m),enlong,ori_offset+(n-1)*ori_incr,M1); | ||
34 | FB(:,:,n,m)=f; | ||
35 | end | ||
36 | end | ||
37 | |||
38 | FB=reshape(FB,M1,M2,num_scale*num_ori); | ||
39 | total_num_filt=size(FB,3); | ||
40 | |||
41 | for j=1:total_num_filt, | ||
42 | F = FB(:,:,j); | ||
43 | a = sum(sum(abs(F))); | ||
44 | FB(:,:,j) = FB(:,:,j)/a; | ||
45 | end | ||
46 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/max_supress2.m b/SD-VBS/common/toolbox/toolbox_basic/common/max_supress2.m new file mode 100755 index 0000000..05b5f11 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/max_supress2.m | |||
@@ -0,0 +1,59 @@ | |||
1 | function NMS = max_supress2(data,ismax); | ||
2 | % | ||
3 | % NMS = max_supress(data,ismax); | ||
4 | % | ||
5 | % data: [nr,nc,nfilter,nscale] | ||
6 | % of filter mag. map | ||
7 | % ismax: 1 local max, 0 local min | ||
8 | % | ||
9 | |||
10 | [nr,nc,nfilter,nscale] = size(data); | ||
11 | |||
12 | % set up the orthognal neighbourhood for each oriented filter | ||
13 | if nfilter == 6, | ||
14 | nbr_template=[1 1 1 0 -1 -1 | ||
15 | 0 1 1 1 1 1]; | ||
16 | else | ||
17 | nbr_template=[1 0 ; | ||
18 | 0 1]; | ||
19 | end | ||
20 | |||
21 | %% for each scale, compute the dominate filter response | ||
22 | canny_dir_I = zeros(nr,nc,nscale); | ||
23 | |||
24 | for m = 1:nscale, | ||
25 | [max_Ori_resp_I,Ori_sca_I] = max(data(:,:,:,m),[],3); | ||
26 | canny_dir_I(:,:,m) = Ori_sca_I; | ||
27 | end | ||
28 | |||
29 | max_Ori_resp_small = max_Ori_resp_I(2:end-1,2:end-1); | ||
30 | canny_dir = canny_dir_I(2:end-1,2:end-1); | ||
31 | |||
32 | %% | ||
33 | |||
34 | NMS = zeros(nr,nc,nscale); | ||
35 | |||
36 | |||
37 | for m = 1:nscale, | ||
38 | |||
39 | [x,y] = meshgrid(2:nc-1,2:nr-1); | ||
40 | xid = x(:)+nbr_template(2,canny_dir(:))'; | ||
41 | yid = y(:)+nbr_template(1,canny_dir(:))'; | ||
42 | id1 = (xid-1)*nr+yid; | ||
43 | |||
44 | xid = x(:)-nbr_template(2,canny_dir(:))'; | ||
45 | yid = y(:)-nbr_template(1,canny_dir(:))'; | ||
46 | id2 = (xid-1)*nr+yid; | ||
47 | if ismax, | ||
48 | a = (max_Ori_resp_small(:)>max_Ori_resp_I(id1(:))) .* (max_Ori_resp_small(:)>max_Ori_resp_I(id2(:))); | ||
49 | NMS(2:end-1,2:end-1,m) = reshape(a,nr-2,nc-2); | ||
50 | NMS(:,:,m) = NMS(:,:,m).*max_Ori_resp_I; | ||
51 | else | ||
52 | a = (max_Ori_resp_small(:)<max_Ori_resp_I(id1(:))) .* (max_Ori_resp_small(:)<max_Ori_resp_I(id2(:))); | ||
53 | NMS(2:end-1,2:end-1,m) = reshape(a,nr-2,nc-2); | ||
54 | end | ||
55 | |||
56 | end | ||
57 | |||
58 | |||
59 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/mgrad.m b/SD-VBS/common/toolbox/toolbox_basic/common/mgrad.m new file mode 100755 index 0000000..1d89c87 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/mgrad.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function [dx,dy] = mgrad(I,w) | ||
2 | % | ||
3 | % [dx,dy] = mgrad(I,w) | ||
4 | % | ||
5 | |||
6 | [nr,nc] = size(I); | ||
7 | |||
8 | dx = zeros(nr,nc);dy = zeros(nr,nc); | ||
9 | |||
10 | dx(:,1:nc-w) = I(:,1:nc-w) - I(:,w+1:nc); | ||
11 | dy(1:nr-w,:) = I(1:nr-w,:) - I(w+1:nr,:); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/mpgread.m b/SD-VBS/common/toolbox/toolbox_basic/common/mpgread.m new file mode 100755 index 0000000..7f964ab --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/mpgread.m | |||
@@ -0,0 +1,25 @@ | |||
1 | %MPGREAD Read an MPEG encoded movie file. | ||
2 | % [M, map] = mpgread('filename', frames) reads the specifed | ||
3 | % MPEG file and translates it into the movie M, and colormap map. | ||
4 | % If a vector frames is specified, then only the frames specified | ||
5 | % in this vector will be placed in M. Otherwise, all frames will | ||
6 | % be placed in M. | ||
7 | % | ||
8 | % M = mpgread('filename', frames, 'indexed') | ||
9 | % Reads an MPEG file into the MATLAB 5.3+ format movie which | ||
10 | % is a structure array. Each element has a cdata field | ||
11 | % containing a uint8 image matrix and a colormap field | ||
12 | % containing the colormap. The frames parameter can be [] to | ||
13 | % indicate that all frames should be read. | ||
14 | % | ||
15 | % M = mpgread('filename', frames, 'truecolor') | ||
16 | % Reads an MPEG file into the MATLAB 5.3+ format movie. Each | ||
17 | % frame in the movie has a truecolor MxNx3 cdata field and | ||
18 | % an empty colormap field. | ||
19 | % | ||
20 | % [R, G, B] = mpgread('filename', frames) will perform the same | ||
21 | % operation as above, except that the decoded MPEG frames will | ||
22 | % be placed into the matrices R, G, B, where R contains the red | ||
23 | % component for each frame, G, the green component, and B, the | ||
24 | % blue component. | ||
25 | % | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/mpgread.mexlx b/SD-VBS/common/toolbox/toolbox_basic/common/mpgread.mexlx new file mode 100755 index 0000000..1190192 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/mpgread.mexlx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/mpgwrite.m b/SD-VBS/common/toolbox/toolbox_basic/common/mpgwrite.m new file mode 100755 index 0000000..b1cb773 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/mpgwrite.m | |||
@@ -0,0 +1,29 @@ | |||
1 | %MPGWRITE Write an MPEG file. | ||
2 | % MPGWRITE(M, map, 'filename', options) Encodes M in MPEG | ||
3 | % format using the specified colormap and writes the result to the | ||
4 | % specified file. The options argument is an optional vector of | ||
5 | % 8 or fewer options where each value has the following meaning: | ||
6 | % 1. REPEAT: | ||
7 | % An integer number of times to repeat the movie | ||
8 | % (default is 1). | ||
9 | % 2. P-SEARCH ALGORITHM: | ||
10 | % 0 = logarithmic (fastest, default value) | ||
11 | % 1 = subsample | ||
12 | % 2 = exhaustive (better, but slow) | ||
13 | % 3. B-SEARCH ALGORITHM: | ||
14 | % 0 = simple (fastest) | ||
15 | % 1 = cross2 (slightly slower, default value) | ||
16 | % 2 = exhaustive (very slow) | ||
17 | % 4. REFERENCE FRAME: | ||
18 | % 0 = original (faster, default) | ||
19 | % 1 = decoded (slower, but results in better quality) | ||
20 | % 5. RANGE IN PIXELS: | ||
21 | % An integer search radius. Default is 10. | ||
22 | % 6. I-FRAME Q-SCALE: | ||
23 | % An integer between 1 and 31. Default is 8. | ||
24 | % 7. P-FRAME Q-SCALE: | ||
25 | % An integer between 1 and 31. Default is 10. | ||
26 | % 8. B-FRAME Q-SCALE: | ||
27 | % An integer between 1 and 31. Default is 25. | ||
28 | % | ||
29 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/mpgwrite.mexlx b/SD-VBS/common/toolbox/toolbox_basic/common/mpgwrite.mexlx new file mode 100755 index 0000000..770bad1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/mpgwrite.mexlx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/ncut.m b/SD-VBS/common/toolbox/toolbox_basic/common/ncut.m new file mode 100755 index 0000000..81fb52f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/ncut.m | |||
@@ -0,0 +1,108 @@ | |||
1 | % function [v,s,na,d] = ncut(a,nv,sigma,offset) | ||
2 | % Input: | ||
3 | % a = affinity matrix, hermitian, could be 3D, or a cell | ||
4 | % nv = number of eigenvectors v | ||
5 | % sigma = refer to EIGS.M, 0 for smallest, default = 'LR' | ||
6 | % offset = vector, each for an affinity matrix, offset for nondirectional repulsion | ||
7 | % W = A - R = (A + offset) - (R + offset) | ||
8 | % Expected value = offset in affinity value * # of neighboors | ||
9 | % Output: | ||
10 | % v = generalized eigenvectors of A and D | ||
11 | % s = eigenvalues | ||
12 | % na = normalized affinity matrix | ||
13 | % d = normalization matrix 1/sqrt(rowsum(a)) | ||
14 | % This version now accepts multiple weight matrices | ||
15 | % the format of cells are good for sparse affinity matrices | ||
16 | |||
17 | % Jianbo Shi | ||
18 | |||
19 | function [v,s,na,d] = ncut(a,nv,sigma,offset) | ||
20 | |||
21 | is_cell = iscell(a); | ||
22 | if is_cell, | ||
23 | nw = length(a); | ||
24 | [nr,nc] = size(a{1}); | ||
25 | else | ||
26 | [nr,nc,nw] = size(a); | ||
27 | end | ||
28 | |||
29 | if nargin<2 | isempty(nv), | ||
30 | nv = min(nr,6); | ||
31 | end | ||
32 | |||
33 | if nargin<3 | isempty(sigma), | ||
34 | sigma = 'LR'; | ||
35 | end | ||
36 | |||
37 | if nargin<4 | isempty(offset), | ||
38 | offset = 0; | ||
39 | end; | ||
40 | offset=offset(:); | ||
41 | j = length(offset); | ||
42 | offset(j+1:nw) = offset(j); | ||
43 | |||
44 | d = 0; | ||
45 | na = sparse(nr,nc); | ||
46 | for j=1:nw, % simultaneous partitioning with multiple weight matrices. | ||
47 | if is_cell, | ||
48 | w = a{j}; | ||
49 | elseif issparse(a), % only supports 2D indexing | ||
50 | w = a; | ||
51 | else | ||
52 | w = a(:,:,j); | ||
53 | end | ||
54 | if j==nw, % to save space | ||
55 | clear a; | ||
56 | end | ||
57 | |||
58 | d = d + sum(abs(w),2) + 2*offset(j); % single equivalent D | ||
59 | |||
60 | % modify matrix a to deal with nondirectional repulsion | ||
61 | wr = real(w); | ||
62 | wr = (sum(abs(wr),2)-sum(wr,2))*0.5 + offset(j); | ||
63 | w = w + spdiags(wr,0,nr,nr); | ||
64 | |||
65 | na = na + w; % single equivalent A | ||
66 | |||
67 | % if you want the rectified individual weight matrix | ||
68 | %if is_cell, | ||
69 | % a{j} = w; | ||
70 | %else | ||
71 | % a(:,:,j) = w; | ||
72 | %end | ||
73 | end | ||
74 | clear w wr | ||
75 | |||
76 | % normalize | ||
77 | d = 1./sqrt(d+eps); | ||
78 | if 1, | ||
79 | na = spmtimesd(na,d,d); | ||
80 | else | ||
81 | d = spdiags(d,0,nr,nr); | ||
82 | na = d * na * d; | ||
83 | end | ||
84 | |||
85 | options.disp = 0; | ||
86 | %options.tol = 1e-10; | ||
87 | %options.maxit = 15; | ||
88 | |||
89 | warning off | ||
90 | [v,s] = eigs(na,nv,sigma,options); | ||
91 | s = real(diag(s)); | ||
92 | warning on | ||
93 | |||
94 | % to make sure positive eigs always come first | ||
95 | % [x,y] = sort(-s); | ||
96 | % s = -x; | ||
97 | % v = v(:,y); | ||
98 | |||
99 | % project back to get the eigenvectors for the pair (a,d) | ||
100 | % a x = lambda d x | ||
101 | % na y = lambda y | ||
102 | % x = d^(-1/2) y | ||
103 | |||
104 | if 1, | ||
105 | v = spdiags(d,0,nr,nr) * v; | ||
106 | else | ||
107 | v = d * v; | ||
108 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/ncut_b.m b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_b.m new file mode 100755 index 0000000..d686981 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_b.m | |||
@@ -0,0 +1,46 @@ | |||
1 | function [v,d] = ncut(A,nv) | ||
2 | % | ||
3 | % [v,d] = ncut(A,nv) | ||
4 | % | ||
5 | % | ||
6 | % computes 'nv' of the normalized cut vectors 'v' from | ||
7 | % matrix 'A' | ||
8 | % | ||
9 | % it computes the largest eigenvectors of | ||
10 | % A*v = \lambda D * v; D = diag(sum(A)); | ||
11 | % | ||
12 | % this is same as solving the smallest eigenvectors of | ||
13 | % (D-A)*v = \lambda D *v; | ||
14 | % | ||
15 | |||
16 | % | ||
17 | % Jianbo Shi | ||
18 | % | ||
19 | |||
20 | ds = sum(A); | ||
21 | ds = ones(size(ds))./sqrt(ds); | ||
22 | |||
23 | for j=1:size(A,1), | ||
24 | A(j,:) = A(j,:).*ds; | ||
25 | end | ||
26 | |||
27 | for j=1:size(A,2); | ||
28 | A(:,j) = A(:,j).*ds'; | ||
29 | end | ||
30 | |||
31 | |||
32 | %disp(sprintf('computing eig values')); | ||
33 | OPTIONS.tol=1e-10; | ||
34 | OPTIONS.maxit=15; | ||
35 | OPTIONS.disp=0; | ||
36 | %tic;toc; | ||
37 | |||
38 | [v,d] = eigs(A,nv,'LM',OPTIONS); | ||
39 | |||
40 | d = abs(diag(d)); | ||
41 | |||
42 | for j=1:nv, | ||
43 | v(:,j) = v(:,j).*ds'; | ||
44 | end | ||
45 | |||
46 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/ncut_bb.m b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_bb.m new file mode 100755 index 0000000..c9b7394 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_bb.m | |||
@@ -0,0 +1,39 @@ | |||
1 | function [v,d] = ncut(A,nv) | ||
2 | % | ||
3 | % [v,d] = ncut(A,nv) | ||
4 | % | ||
5 | % | ||
6 | % computes 'nv' of the normalized cut vectors 'v' from | ||
7 | % matrix 'A' | ||
8 | % | ||
9 | % it computes the largest eigenvectors of | ||
10 | % A*v = \lambda D * v; D = diag(sum(A)); | ||
11 | % | ||
12 | % this is same as solving the smallest eigenvectors of | ||
13 | % (D-A)*v = \lambda D *v; | ||
14 | % | ||
15 | |||
16 | % | ||
17 | % Jianbo Shi | ||
18 | % | ||
19 | |||
20 | ds = sum(A); | ||
21 | ds = ones(size(ds))./sqrt(ds); | ||
22 | |||
23 | D_inv_half = spdiags(ds',0,size(A,1),size(A,2)); | ||
24 | A = D_inv_half*A*D_inv_half; | ||
25 | |||
26 | disp(sprintf('computing eig values')); | ||
27 | OPTIONS.tol=1e-10; | ||
28 | OPTIONS.maxit=15; | ||
29 | OPTIONS.disp=0; | ||
30 | %tic;toc; | ||
31 | |||
32 | [v,d] = eigs(A,nv,OPTIONS); | ||
33 | |||
34 | d = abs(diag(d)); | ||
35 | |||
36 | for j=1:nv, | ||
37 | v(:,j) = v(:,j).*ds'; | ||
38 | end | ||
39 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/ncut_e.m b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_e.m new file mode 100755 index 0000000..e06c8a6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_e.m | |||
@@ -0,0 +1,36 @@ | |||
1 | function [v,d] = ncut(A,nv) | ||
2 | % | ||
3 | % [v,d] = ncut(A,nv) | ||
4 | % | ||
5 | % computes 'nv' of the normalized cut vectors 'v' from | ||
6 | % matrix 'A' | ||
7 | % | ||
8 | |||
9 | % | ||
10 | % Jianbo Shi | ||
11 | % | ||
12 | |||
13 | ds = sum(A); | ||
14 | D = diag(ds); | ||
15 | |||
16 | ds = ones(size(ds))./sqrt(ds); | ||
17 | |||
18 | B = D-A; | ||
19 | |||
20 | for j=1:size(A,1), | ||
21 | B(j,:) = B(j,:).*ds; | ||
22 | end | ||
23 | |||
24 | for j=1:size(A,2); | ||
25 | B(:,j) = B(:,j).*ds'; | ||
26 | end | ||
27 | |||
28 | disp(sprintf('computing eig values')); | ||
29 | tic;[v,d] = eigs(B,nv,'sm');toc; | ||
30 | |||
31 | d = abs(diag(d)); | ||
32 | |||
33 | for j=1:nv, | ||
34 | v(:,j) = v(:,j).*ds'; | ||
35 | end | ||
36 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/ncut_neg.m b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_neg.m new file mode 100755 index 0000000..89a1caa --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_neg.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function [v,d] = ncut(A,nv) | ||
2 | % | ||
3 | % [v,d] = ncut(A,nv) | ||
4 | % | ||
5 | % | ||
6 | % computes 'nv' of the normalized cut vectors 'v' from | ||
7 | % matrix 'A' | ||
8 | % | ||
9 | % it computes the largest eigenvectors of | ||
10 | % A*v = \lambda D * v; D = diag(sum(A)); | ||
11 | % | ||
12 | % this is same as solving the smallest eigenvectors of | ||
13 | % (D-A)*v = \lambda D *v; | ||
14 | % | ||
15 | |||
16 | % | ||
17 | % Jianbo Shi | ||
18 | % | ||
19 | |||
20 | ds = sum(abs(A)); | ||
21 | ds = ones(size(ds))./sqrt(ds); | ||
22 | |||
23 | for j=1:size(A,1), | ||
24 | A(j,:) = A(j,:).*ds; | ||
25 | end | ||
26 | |||
27 | for j=1:size(A,2); | ||
28 | A(:,j) = A(:,j).*ds'; | ||
29 | end | ||
30 | |||
31 | |||
32 | %disp(sprintf('computing eig values')); | ||
33 | OPTIONS.tol=1e-10; | ||
34 | OPTIONS.maxit=15; | ||
35 | OPTIONS.disp=0; | ||
36 | %tic;toc; | ||
37 | |||
38 | [v,d] = eigs(A,nv,OPTIONS); | ||
39 | |||
40 | d = abs(diag(d)); | ||
41 | |||
42 | for j=1:nv, | ||
43 | v(:,j) = v(:,j).*ds'; | ||
44 | end | ||
45 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/ncut_sparse.m b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_sparse.m new file mode 100755 index 0000000..dc6d076 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_sparse.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function [v,d] = ncut(A,nv) | ||
2 | % | ||
3 | % [v,d] = ncut(A,nv) | ||
4 | % | ||
5 | % Assume A is sparse, | ||
6 | % | ||
7 | % computes 'nv' of the normalized cut vectors 'v' from | ||
8 | % matrix 'A' | ||
9 | % | ||
10 | % it computes the largest eigenvectors of | ||
11 | % A*v = \lambda D * v; D = diag(sum(A)); | ||
12 | % | ||
13 | % this is same as solving the smallest eigenvectors of | ||
14 | % (D-A)*v = \lambda D *v; | ||
15 | % | ||
16 | |||
17 | % | ||
18 | % Jianbo Shi | ||
19 | % | ||
20 | |||
21 | ds = sum(abs(A)); | ||
22 | ds = 1./sqrt(ds); | ||
23 | |||
24 | [id_i,id_j,W] = find(A); | ||
25 | A = sparse(id_i,id_j,ds(id_i)'.*ds(id_j)'.*(W)); | ||
26 | |||
27 | %disp(sprintf('computing eig values')); | ||
28 | SIGMA = 'LM'; | ||
29 | %OPTIONS.issym = 0; | ||
30 | OPTIONs.isreal = 1; | ||
31 | OPTIONS.tol=1e-12; | ||
32 | OPTIONS.maxit=25; | ||
33 | OPTIONS.disp=0; | ||
34 | %tic;toc; | ||
35 | |||
36 | tic | ||
37 | [v,d] = eigs(A,nv,SIGMA,OPTIONS); | ||
38 | %,OPTIONS); | ||
39 | toc | ||
40 | d = abs(diag(d)); | ||
41 | |||
42 | for j=1:nv, | ||
43 | v(:,j) = v(:,j).*ds'; | ||
44 | end | ||
45 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/ncut_tmp.m b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_tmp.m new file mode 100755 index 0000000..b623ee4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/ncut_tmp.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function [v,d] = ncut(A,nv) | ||
2 | % | ||
3 | % [v,d] = ncut(A,nv) | ||
4 | % | ||
5 | % | ||
6 | % computes 'nv' of the normalized cut vectors 'v' from | ||
7 | % matrix 'A' | ||
8 | % | ||
9 | % it computes the largest eigenvectors of | ||
10 | % A*v = \lambda D * v; D = diag(sum(A)); | ||
11 | % | ||
12 | % this is same as solving the smallest eigenvectors of | ||
13 | % (D-A)*v = \lambda D *v; | ||
14 | % | ||
15 | |||
16 | % | ||
17 | % Jianbo Shi | ||
18 | % | ||
19 | |||
20 | ds = sum(A); | ||
21 | ds = ones(size(ds))./sqrt(ds); | ||
22 | |||
23 | for j=1:size(A,1), | ||
24 | A(j,:) = A(j,:).*ds; | ||
25 | end | ||
26 | |||
27 | for j=1:size(A,2); | ||
28 | A(:,j) = A(:,j).*ds'; | ||
29 | end | ||
30 | |||
31 | |||
32 | %disp(sprintf('computing eig values')); | ||
33 | OPTIONS.tol=1e-10; | ||
34 | OPTIONS.maxit=15; | ||
35 | OPTIONS.disp=0; | ||
36 | %tic;toc; | ||
37 | |||
38 | [v,d] = eigs(A,nv,OPTIONS); | ||
39 | |||
40 | d = abs(diag(d)); | ||
41 | |||
42 | for j=1:nv, | ||
43 | v(:,j) = v(:,j).*ds'; | ||
44 | end | ||
45 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/ncutd.m b/SD-VBS/common/toolbox/toolbox_basic/common/ncutd.m new file mode 100755 index 0000000..cd5e8a4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/ncutd.m | |||
@@ -0,0 +1,108 @@ | |||
1 | % function [v,s,na,d] = ncutd(a,nv,beta,sigma,offset) | ||
2 | % Input: | ||
3 | % a = (attraction + i*repulsion), both nonnegative but could be asymmetrical | ||
4 | % nv = number of eigenvectors v | ||
5 | % beta = weighting between undirected graph and directed graph | ||
6 | % beta = 1, undirected; beta = 0, directed. Default = 0.5 | ||
7 | % sigma = refer to EIGS.M, 0 for smallest, default = 'LR' | ||
8 | % offset = vector, each for an affinity matrix, | ||
9 | % per_edge offset for nondirectional attraction and repulsion | ||
10 | % Output: | ||
11 | % v = generalized eigenvectors of A and D | ||
12 | % s = eigenvalues | ||
13 | % na = normalized affinity matrix | ||
14 | % d = normalization matrix 1/sqrt(rowsum(a)) | ||
15 | % This version now accepts multiple weight matrices | ||
16 | % the format of cells are good for sparse affinity matrices | ||
17 | |||
18 | % Stella X. Yu, 2001. | ||
19 | |||
20 | function [v,s,na,d] = ncutd(a,nv,beta,sigma,offset) | ||
21 | |||
22 | is_cell = iscell(a); | ||
23 | if is_cell, | ||
24 | nw = length(a); | ||
25 | [nr,nc] = size(a{1}); | ||
26 | else | ||
27 | [nr,nc,nw] = size(a); | ||
28 | end | ||
29 | |||
30 | if nargin<2 | isempty(nv), | ||
31 | nv = min(nr,6); | ||
32 | end | ||
33 | |||
34 | if nargin<3 | isempty(beta), | ||
35 | beta = 0.5; | ||
36 | end | ||
37 | beta = beta *2; | ||
38 | |||
39 | if nargin<4 | isempty(sigma), | ||
40 | sigma = 'LR'; | ||
41 | end | ||
42 | |||
43 | if nargin<5 | isempty(offset), | ||
44 | offset = 0; | ||
45 | end; | ||
46 | offset=offset(:); | ||
47 | j = length(offset); | ||
48 | offset(j+1:nw) = offset(j); | ||
49 | |||
50 | % modify per-edge offset delta to 2 D_{\delta} = offset to D_R | ||
51 | offset = offset * (2*nc); | ||
52 | |||
53 | z = zeros(nr,nw); | ||
54 | na = 0; | ||
55 | for j=1:nw, % simultaneous partitioning with multiple weight matrices. | ||
56 | if is_cell, | ||
57 | w = a{j}; | ||
58 | elseif issparse(a), % only supports 2D indexing | ||
59 | w = a; | ||
60 | else | ||
61 | w = a(:,:,j); | ||
62 | end | ||
63 | |||
64 | wr = real(w); % attraction | ||
65 | wi = imag(w); % repulsion | ||
66 | |||
67 | % if wr has negative numbers, treat as repulsion | ||
68 | % while negative numbers in wi is ignored | ||
69 | aa = wr.*(wr>0); | ||
70 | rr = wi.*(wi>0)-wr.*(wr<0); | ||
71 | |||
72 | % decomposition | ||
73 | au = aa + aa'; | ||
74 | ad = aa - aa'; | ||
75 | ru = rr + rr'; | ||
76 | rd = rr - rr'; | ||
77 | |||
78 | % construct equivalent matrices | ||
79 | x = sum(ru,2); | ||
80 | wr = au - ru + diag(x); | ||
81 | wi = ad + rd; | ||
82 | x = x + sum(au,2); | ||
83 | |||
84 | % re-organize, add in offset and beta | ||
85 | z(:,j) = x + 2 * offset; | ||
86 | na = na + ( beta * (wr + offset) + sqrt(-1)* (2-beta) * wi ); | ||
87 | |||
88 | end | ||
89 | z = sum(z,2); % diag(z) = single equivalent D | ||
90 | |||
91 | % normalize | ||
92 | d = repmat(1./sqrt(z+eps),1,nc); | ||
93 | na = d.*na; | ||
94 | na = na.*d'; | ||
95 | |||
96 | options.disp = 0; | ||
97 | %options.tol = 1e-10; | ||
98 | %options.maxit = 15; | ||
99 | |||
100 | [v,s] = eigs(na,nv,sigma,options); | ||
101 | s = real(diag(s)); | ||
102 | |||
103 | % project back to get the eigenvectors for the pair (a,d) | ||
104 | % a x = lambda d x | ||
105 | % na y = lambda y | ||
106 | % x = d^(-1/2) y | ||
107 | |||
108 | v = v .* d(:,ones(nv,1)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/nonmaxsup.m b/SD-VBS/common/toolbox/toolbox_basic/common/nonmaxsup.m new file mode 100755 index 0000000..d12301c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/nonmaxsup.m | |||
@@ -0,0 +1,81 @@ | |||
1 | % function f = nonmaxsup(g,ismax,r) return extrema boolean map. | ||
2 | % Input: g = image, gradient image pair [x,y], or [x,y,g] in 3D matrix | ||
3 | % ismax (=1)/0 is for non maximum/minimum suppression | ||
4 | % r (=1) is the neighbourhood radius. | ||
5 | % Output: | ||
6 | % f = thinned extrema boolean map, where | ||
7 | % d (||gradient||) / d gradient = 0 | ||
8 | |||
9 | % Stella X. Yu, 2000. | ||
10 | |||
11 | function f = nonmaxsup(g,ismax,r) | ||
12 | |||
13 | if nargin<2, | ||
14 | ismax = 1; | ||
15 | end | ||
16 | |||
17 | if nargin<3, | ||
18 | r = 1; | ||
19 | end | ||
20 | |||
21 | i = size(g,3); | ||
22 | if i==3, | ||
23 | x = g(:,:,1); | ||
24 | y = g(:,:,2); | ||
25 | g = g(:,:,3); | ||
26 | elseif i==2, | ||
27 | x = g(:,:,1); | ||
28 | y = g(:,:,2); | ||
29 | g = x.*x + y.*y; | ||
30 | else | ||
31 | [x,y] = gradient(g); | ||
32 | end | ||
33 | |||
34 | % label angles into 4 directions | ||
35 | a = angle(x - sqrt(-1).*y); % [-pi,pi) | ||
36 | s = ceil((abs(a)+pi/8)./(pi/4)); | ||
37 | s(find(s==5)) = 1; | ||
38 | s(find(isnan(s))) = 1; | ||
39 | |||
40 | % augment the image | ||
41 | [m,n] = size(g); | ||
42 | newm = m + r + r; | ||
43 | i = [g(:,1);g(:,end);g(1,:)';g(end,:)']; % image boundary | ||
44 | if ismax, | ||
45 | v = min(i) - 1; | ||
46 | else | ||
47 | v = max(i) + 1; | ||
48 | end | ||
49 | i = zeros(newm,r) + v; | ||
50 | j = zeros(r,n) + v; | ||
51 | newg = [i, [j; g; j;], i]; | ||
52 | |||
53 | % k = index as the interior of the new image | ||
54 | i = [r+1:newm-r]'+ r*newm; | ||
55 | j = [0:n-1].*newm; | ||
56 | k = i(:,ones(1,n)) + j(ones(1,m),:); | ||
57 | k = k(:); | ||
58 | |||
59 | % unit displacement vectors along gradient directions | ||
60 | d = [newm,newm-1,-1,-1-newm]'; % for 4 directions | ||
61 | d = d(s(:)); | ||
62 | |||
63 | % non maximum suppression | ||
64 | f = ones(m*n,1); | ||
65 | g = g(:); | ||
66 | newd = 0; | ||
67 | |||
68 | if ismax, | ||
69 | for i=1:r, | ||
70 | newd = newd + d; | ||
71 | f = f & (g>newg(k+newd)) & (g>newg(k-newd)); | ||
72 | end | ||
73 | else | ||
74 | for i=1:r, | ||
75 | newd = newd + d; | ||
76 | f = f & (g<newg(k+newd)) & (g<newg(k-newd)); | ||
77 | end | ||
78 | end | ||
79 | |||
80 | f = reshape(f,[m,n]); | ||
81 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/pair_dist.m b/SD-VBS/common/toolbox/toolbox_basic/common/pair_dist.m new file mode 100755 index 0000000..3ea970a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/pair_dist.m | |||
@@ -0,0 +1,14 @@ | |||
1 | function W = pari_hist_dist_fast(data) | ||
2 | % | ||
3 | % W = pari_hist_dist(data) | ||
4 | % | ||
5 | % data: num_im by num_feature | ||
6 | % W : num_im by num_im hist diff | ||
7 | % | ||
8 | |||
9 | [num_im,num_feature] = size(data); | ||
10 | mag = sum(data.*data,2); | ||
11 | |||
12 | W = mag(:,ones(1,num_im)) - 2*data*data'; | ||
13 | mag = mag'; | ||
14 | W = W+mag(ones(num_im,1),:); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/quadedgep.m b/SD-VBS/common/toolbox/toolbox_basic/common/quadedgep.m new file mode 100755 index 0000000..eda33f9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/quadedgep.m | |||
@@ -0,0 +1,106 @@ | |||
1 | % function [x,y,gx,gy,par,threshold,mag,mage,g,FIe,FIo,mago] = quadedgep(I,par,threshold); | ||
2 | % Input: | ||
3 | % I = image | ||
4 | % par = vector for 4 parameters | ||
5 | % [number of filter orientations, number of scales, filter size, elongation] | ||
6 | % To use default values, put 0. | ||
7 | % threshold = threshold on edge strength | ||
8 | % Output: | ||
9 | % [x,y,gx,gy] = locations and gradients of an ordered list of edgels | ||
10 | % x,y could be horizontal or vertical or 45 between pixel sites | ||
11 | % but it is guaranteed that there [floor(y) + (floor(x)-1)*nr] | ||
12 | % is ordered and unique. In other words, each edgel has a unique pixel id. | ||
13 | % par = actual par used | ||
14 | % threshold = actual threshold used | ||
15 | % mag = edge magnitude | ||
16 | % mage = phase map | ||
17 | % g = gradient map at each pixel | ||
18 | % [FIe,FIo] = odd and even filter outputs | ||
19 | % mago = odd filter output of optimum orientation | ||
20 | |||
21 | % Stella X. Yu, 2001 | ||
22 | |||
23 | function [x,y,gx,gy,par,threshold,mag,mage,g,FIe,FIo,mago] = quadedgep(I,par,threshold); | ||
24 | |||
25 | if nargin<3 | isempty(threshold), | ||
26 | threshold = 0.2; | ||
27 | end | ||
28 | |||
29 | [r,c] = size(I); | ||
30 | def_par = [8,1,20,3]; | ||
31 | |||
32 | % take care of parameters, any missing value is substituted by a default value | ||
33 | if nargin<2 | isempty(par), | ||
34 | par = def_par; | ||
35 | end | ||
36 | par(end+1:4)=0; | ||
37 | par = par(:); | ||
38 | j = (par>0); | ||
39 | have_value = [ j, 1-j ]; | ||
40 | j = 1; n_filter = have_value(j,:) * [par(j); def_par(j)]; | ||
41 | j = 2; n_scale = have_value(j,:) * [par(j); def_par(j)]; | ||
42 | j = 3; winsz = have_value(j,:) * [par(j); def_par(j)]; | ||
43 | j = 4; enlong = have_value(j,:) * [par(j); def_par(j)]; | ||
44 | |||
45 | % always make filter size an odd number so that the results will not be skewed | ||
46 | j = winsz/2; | ||
47 | if not(j > fix(j) + 0.1), | ||
48 | winsz = winsz + 1; | ||
49 | end | ||
50 | |||
51 | % filter the image with quadrature filters | ||
52 | FBo = make_filterbank_odd2(n_filter,n_scale,winsz,enlong); | ||
53 | FBe = make_filterbank_even2(n_filter,n_scale,winsz,enlong); | ||
54 | n = ceil(winsz/2); | ||
55 | f = [fliplr(I(:,2:n+1)), I, fliplr(I(:,c-n:c-1))]; | ||
56 | f = [flipud(f(2:n+1,:)); f; flipud(f(r-n:r-1,:))]; | ||
57 | FIo = fft_filt_2(f,FBo,1); | ||
58 | FIo = FIo(n+[1:r],n+[1:c],:); | ||
59 | FIe = fft_filt_2(f,FBe,1); | ||
60 | FIe = FIe(n+[1:r],n+[1:c],:); | ||
61 | |||
62 | % compute the orientation energy and recover a smooth edge map | ||
63 | % pick up the maximum energy across scale and orientation | ||
64 | % even filter's output: as it is the second derivative, zero cross localize the edge | ||
65 | % odd filter's output: orientation | ||
66 | mag = sqrt(sum(FIo.^2,3)+sum(FIe.^2,3)); | ||
67 | mag_a = sqrt(FIo.^2+FIe.^2); | ||
68 | [tmp,max_id] = max(mag_a,[],3); | ||
69 | base_size = r * c; | ||
70 | id = [1:base_size]'; | ||
71 | mage = reshape(FIe(id+(max_id(:)-1)*base_size),[r,c]); | ||
72 | mage = (mage>0) - (mage<0); | ||
73 | |||
74 | ori_incr=pi/n_filter; % to convert jshi's coords to conventional image xy | ||
75 | ori_offset=ori_incr/2; | ||
76 | theta = ori_offset+([1:n_filter]-1)*ori_incr; % orientation detectors | ||
77 | % [gx,gy] are image gradient in image xy coords, winner take all | ||
78 | mago = reshape(FIo(id+(max_id(:)-1)*base_size),[r,c]); | ||
79 | ori = theta(max_id); | ||
80 | ori = ori .* (mago>0) + (ori + pi).*(mago<0); | ||
81 | gy = mag .* cos(ori); | ||
82 | gx = -mag .* sin(ori); | ||
83 | g = cat(3,gx,gy); | ||
84 | |||
85 | % phase map: edges are where the phase changes | ||
86 | mag_th = max(mag(:)) * threshold; | ||
87 | eg = (mag>mag_th); | ||
88 | h = eg & [(mage(2:r,:) ~= mage(1:r-1,:)); zeros(1,c)]; | ||
89 | v = eg & [(mage(:,2:c) ~= mage(:,1:c-1)), zeros(r,1)]; | ||
90 | [y,x] = find(h | v); | ||
91 | k = y + (x-1) * r; | ||
92 | h = h(k); | ||
93 | v = v(k); | ||
94 | y = y + h * 0.5; % i | ||
95 | x = x + v * 0.5; % j | ||
96 | t = h + v * r; | ||
97 | gx = g(k) + g(k+t); | ||
98 | k = k + (r * c); | ||
99 | gy = g(k) + g(k+t); | ||
100 | |||
101 | % display | ||
102 | if 1, | ||
103 | %figure; showmask(I,mage<0); | ||
104 | figure(2); clf;showim(I,1); hold on; quiver(x,y,gx,gy); | ||
105 | %figure; showim(-I,1); hold on; quiver(i,j,ex,ey); | ||
106 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/readpcm.m b/SD-VBS/common/toolbox/toolbox_basic/common/readpcm.m new file mode 100755 index 0000000..ca736da --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/readpcm.m | |||
@@ -0,0 +1,12 @@ | |||
1 | function I = readpcm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d\n',2); | ||
6 | I = fscanf(fid,'%c',A(2)*A(1)); | ||
7 | I = I'; | ||
8 | I = str2num(I); | ||
9 | I = reshape(I,A(2),A(1))'; | ||
10 | |||
11 | |||
12 | fclose(fid); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/readpdm.m b/SD-VBS/common/toolbox/toolbox_basic/common/readpdm.m new file mode 100755 index 0000000..9a1068e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/readpdm.m | |||
@@ -0,0 +1,8 @@ | |||
1 | function I = readpfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2); | ||
6 | I = fscanf(fid,'%d',[A(1),A(2)]); | ||
7 | |||
8 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/readpfm.m b/SD-VBS/common/toolbox/toolbox_basic/common/readpfm.m new file mode 100755 index 0000000..48ecd78 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/readpfm.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function I = readpfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2); | ||
6 | I = fscanf(fid,'%f',[A(1),A(2)]); | ||
7 | |||
8 | %I = fscanf(fid,'%f',A(2)*A(1));I = reshape(I,A(1),A(2)); | ||
9 | |||
10 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/renormalize.m b/SD-VBS/common/toolbox/toolbox_basic/common/renormalize.m new file mode 100755 index 0000000..5d84724 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/renormalize.m | |||
@@ -0,0 +1,32 @@ | |||
1 | function W2 = renormalize(W,nstep) | ||
2 | % | ||
3 | % keep renormalizing until W is almost double | ||
4 | % stocastic | ||
5 | % | ||
6 | |||
7 | if nargin<2, | ||
8 | nstep = 5; | ||
9 | end | ||
10 | |||
11 | n_node = size(W,1); | ||
12 | |||
13 | for j=1:nstep, | ||
14 | fprintf(','); | ||
15 | % normalize row | ||
16 | D = sum(W,2); | ||
17 | D = 1./(D+eps); | ||
18 | W = W.*D(:,ones(1,n_node)); | ||
19 | |||
20 | % normlize column | ||
21 | D = sum(W,1); | ||
22 | D = 1./(D+eps); | ||
23 | W = W.*D(ones(n_node,1),:); | ||
24 | end | ||
25 | fprintf('\n'); | ||
26 | |||
27 | D = sum(W,2); | ||
28 | D = 1./(D+eps); | ||
29 | W2 = W.*D(:,ones(1,n_node)); | ||
30 | |||
31 | |||
32 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/show_edge.m b/SD-VBS/common/toolbox/toolbox_basic/common/show_edge.m new file mode 100755 index 0000000..63b2f98 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/show_edge.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function [id_i,id_j,ids] = show_edge(I,MI,thI); | ||
2 | % | ||
3 | % show_edge(I,MI,thI); | ||
4 | % | ||
5 | |||
6 | [id_i,id_j,tmp] = find(MI); | ||
7 | ids = sub2ind(size(I),id_i,id_j); | ||
8 | clf;im(I);colormap(gray);hold on; | ||
9 | quiver(id_j,id_i,-sin(thI(ids)),cos(thI(ids)),0.5);hold off; | ||
10 | |||
11 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/spmtimesd.c b/SD-VBS/common/toolbox/toolbox_basic/common/spmtimesd.c new file mode 100755 index 0000000..a98dc0a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/spmtimesd.c | |||
@@ -0,0 +1,141 @@ | |||
1 | /*================================================================ | ||
2 | * spmtimesd.c | ||
3 | * This routine computes a sparse matrix times a diagonal matrix | ||
4 | * whose diagonal entries are stored in a full vector. | ||
5 | * | ||
6 | * Examples: | ||
7 | * spmtimesd(m,d,[]) = diag(d) * m, | ||
8 | * spmtimesd(m,[],d) = m * diag(d) | ||
9 | * spmtimesd(m,d1,d2) = diag(d1) * m * diag(d2) | ||
10 | * m could be complex, but d is assumed real | ||
11 | * | ||
12 | * Stella X. Yu's first MEX function, Nov 9, 2001. | ||
13 | |||
14 | % test sequence: | ||
15 | |||
16 | m = 1000; | ||
17 | n = 2000; | ||
18 | a=sparse(rand(m,n)); | ||
19 | d1 = rand(m,1); | ||
20 | d2 = rand(n,1); | ||
21 | tic; b=spmtimesd(a,d1,d2); toc | ||
22 | tic; bb = spdiags(d1,0,m,m) * a * spdiags(d2,0,n,n); toc | ||
23 | e = (bb-b); | ||
24 | max(abs(e(:))) | ||
25 | |||
26 | *=================================================================*/ | ||
27 | |||
28 | # include "mex.h" | ||
29 | |||
30 | void mexFunction( | ||
31 | int nargout, | ||
32 | mxArray *out[], | ||
33 | int nargin, | ||
34 | const mxArray *in[] | ||
35 | ) | ||
36 | { | ||
37 | /* declare variables */ | ||
38 | int i, j, k, m, n, nzmax, cmplx, xm, yn; | ||
39 | int *pir, *pjc, *qir, *qjc; | ||
40 | double *x, *y, *pr, *pi, *qr, *qi; | ||
41 | |||
42 | /* check argument */ | ||
43 | if (nargin != 3) { | ||
44 | mexErrMsgTxt("Three input arguments required"); | ||
45 | } | ||
46 | if (nargout>1) { | ||
47 | mexErrMsgTxt("Too many output arguments."); | ||
48 | } | ||
49 | if (!(mxIsSparse(in[0]))) { | ||
50 | mexErrMsgTxt("Input argument #1 must be of type sparse"); | ||
51 | } | ||
52 | if ( mxIsSparse(in[1]) || mxIsSparse(in[2]) ) { | ||
53 | mexErrMsgTxt("Input argument #2 & #3 must be of type full"); | ||
54 | } | ||
55 | |||
56 | /* computation starts */ | ||
57 | m = mxGetM(in[0]); | ||
58 | n = mxGetN(in[0]); | ||
59 | pr = mxGetPr(in[0]); | ||
60 | pi = mxGetPi(in[0]); | ||
61 | pir = mxGetIr(in[0]); | ||
62 | pjc = mxGetJc(in[0]); | ||
63 | |||
64 | i = mxGetM(in[1]); | ||
65 | j = mxGetN(in[1]); | ||
66 | xm = ((i>j)? i: j); | ||
67 | |||
68 | i = mxGetM(in[2]); | ||
69 | j = mxGetN(in[2]); | ||
70 | yn = ((i>j)? i: j); | ||
71 | |||
72 | if ( xm>0 && xm != m) { | ||
73 | mexErrMsgTxt("Row multiplication dimension mismatch."); | ||
74 | } | ||
75 | if ( yn>0 && yn != n) { | ||
76 | mexErrMsgTxt("Column multiplication dimension mismatch."); | ||
77 | } | ||
78 | |||
79 | |||
80 | nzmax = mxGetNzmax(in[0]); | ||
81 | cmplx = (pi==NULL ? 0 : 1); | ||
82 | out[0] = mxCreateSparse(m,n,nzmax,cmplx); | ||
83 | if (out[0]==NULL) { | ||
84 | mexErrMsgTxt("Not enough space for the output matrix."); | ||
85 | } | ||
86 | |||
87 | qr = mxGetPr(out[0]); | ||
88 | qi = mxGetPi(out[0]); | ||
89 | qir = mxGetIr(out[0]); | ||
90 | qjc = mxGetJc(out[0]); | ||
91 | |||
92 | /* left multiplication */ | ||
93 | x = mxGetPr(in[1]); | ||
94 | if (yn==0) { | ||
95 | for (j=0; j<n; j++) { | ||
96 | qjc[j] = pjc[j]; | ||
97 | for (k=pjc[j]; k<pjc[j+1]; k++) { | ||
98 | i = pir[k]; | ||
99 | qir[k] = i; | ||
100 | qr[k] = x[i] * pr[k]; | ||
101 | if (cmplx) { | ||
102 | qi[k] = x[i] * pi[k]; | ||
103 | } | ||
104 | } | ||
105 | } | ||
106 | qjc[n] = k; | ||
107 | return; | ||
108 | } | ||
109 | |||
110 | /* right multiplication */ | ||
111 | y = mxGetPr(in[2]); | ||
112 | if (xm==0) { | ||
113 | for (j=0; j<n; j++) { | ||
114 | qjc[j] = pjc[j]; | ||
115 | for (k=pjc[j]; k<pjc[j+1]; k++) { | ||
116 | qir[k] = pir[k]; | ||
117 | qr[k] = pr[k] * y[j]; | ||
118 | if (cmplx) { | ||
119 | qi[k] = qi[k] * y[j]; | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | qjc[n] = k; | ||
124 | return; | ||
125 | } | ||
126 | |||
127 | /* both sides */ | ||
128 | for (j=0; j<n; j++) { | ||
129 | qjc[j] = pjc[j]; | ||
130 | for (k=pjc[j]; k<pjc[j+1]; k++) { | ||
131 | i = pir[k]; | ||
132 | qir[k]= i; | ||
133 | qr[k] = x[i] * pr[k] * y[j]; | ||
134 | if (cmplx) { | ||
135 | qi[k] = x[i] * qi[k] * y[j]; | ||
136 | } | ||
137 | } | ||
138 | qjc[n] = k; | ||
139 | } | ||
140 | |||
141 | } | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/spmtimesd.mexa64 b/SD-VBS/common/toolbox/toolbox_basic/common/spmtimesd.mexa64 new file mode 100755 index 0000000..15fa9d2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/spmtimesd.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/spmtimesd.mexglx b/SD-VBS/common/toolbox/toolbox_basic/common/spmtimesd.mexglx new file mode 100755 index 0000000..7478fb0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/spmtimesd.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/tmp.tex b/SD-VBS/common/toolbox/toolbox_basic/common/tmp.tex new file mode 100755 index 0000000..0458ef3 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/tmp.tex | |||
@@ -0,0 +1,16 @@ | |||
1 | Hi Prof. Geiger, | ||
2 | |||
3 | My name is Jianbo Shi. I am a graduate student working | ||
4 | with Jitendra Malik at UC Berkeley. My current research interest is | ||
5 | on the topic of grouping and perceptual organization. I have read | ||
6 | many of your works in this area. | ||
7 | |||
8 | I will be giving a talk at NECI and David Sarnoff Lab on Dec. 8 | ||
9 | and 9 on ``Normalized cuts and its application to motion segmentation''. | ||
10 | I am wondering if I could visit you and your lab on Dec. 10. I would | ||
11 | very much like the opportunity to talk to you on varies topics related | ||
12 | to the problem of grouping. Let me know if that is possible. | ||
13 | |||
14 | -thanks! | ||
15 | |||
16 | -Jianbo | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/vmquant.m b/SD-VBS/common/toolbox/toolbox_basic/common/vmquant.m new file mode 100755 index 0000000..ab4eb28 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/vmquant.m | |||
@@ -0,0 +1,112 @@ | |||
1 | function [im, map] = vmquant(arg1,arg2,arg3,arg4,arg5,arg6,arg7) | ||
2 | %VMQUANT Variance Minimization Color Quantization. | ||
3 | % [X, MAP] = VMQUANT(R,G,B,K,[Qr Qg Qb],DITHER,Qe) or | ||
4 | % VMQUANT(RGB,K,[Qr Qg Qb],DITHER,Qe), where RGB is a 3-D array, | ||
5 | % converts an arbitrary image comprised of RGB triples into an | ||
6 | % indexed image X with color map MAP. K specifies the number | ||
7 | % of desired entries in the target color map, and [Qr Qg Qb] | ||
8 | % specifies the number of quantization bits to assign each color | ||
9 | % axis during color interpolation. DITHER is a string ('dither' or | ||
10 | % 'nodither') that indicates whether or not to perform error propagation | ||
11 | % dither on the output image. Qe specifies the number of bits of | ||
12 | % quantization used in the error calculations. | ||
13 | % | ||
14 | % K is optional and defaults to 256. | ||
15 | % [Qr Qg Qb] is optional and defaults to [5 5 5]. | ||
16 | % DITHER is optional and defaults to 'nodither'. | ||
17 | % Qe is optional and defaults to 8. | ||
18 | % | ||
19 | % See also: RGB2IND, RGB2GRAY, DITHER, IND2RGB, CMUNIQUE, IMAPPROX. | ||
20 | |||
21 | % This is the wrapper function for the MEX file VMQUANTC.C | ||
22 | |||
23 | % Joseph M. Winograd 6-93 | ||
24 | % Copyright (c) 1993 by The MathWorks, Inc. | ||
25 | % $Revision: 5.3 $ $Date: 1996/08/22 22:09:03 $ | ||
26 | |||
27 | % Reference: Xiaolin Wu, "Efficient Statistical Computation for | ||
28 | % Optimal Color Quantization," Graphics Gems II, (ed. James | ||
29 | % Arvo). Academic Press: Boston. 1991. | ||
30 | |||
31 | if nargin < 1, | ||
32 | error('Not enough input arguments.'); | ||
33 | end | ||
34 | |||
35 | threeD = (ndims(arg1)==3); % Determine if input includes a 3-D array | ||
36 | |||
37 | if threeD, | ||
38 | error( nargchk( 1, 5, nargin ) ); | ||
39 | |||
40 | % NOTE: If you change defaults, change them also | ||
41 | % in VMQUANTC.C and recompile the MEX function. | ||
42 | if nargin < 5 | ||
43 | arg5 = 8; % DEFAULT_QE = 8 | ||
44 | end | ||
45 | |||
46 | if nargin < 4 | ||
47 | arg4 = 'n'; % DEFAULT_DITHER = 0 | ||
48 | end | ||
49 | |||
50 | if nargin < 3 | ||
51 | arg3 = [5 5 5]; % DEFAULT_Q = [5 5 5] | ||
52 | end | ||
53 | |||
54 | if nargin < 2 | ||
55 | arg2 = 256; % DEFAULT_K = 256 | ||
56 | end | ||
57 | |||
58 | rout = arg1(:,:,1); | ||
59 | g = arg1(:,:,2); | ||
60 | b = arg1(:,:,3); | ||
61 | |||
62 | if strcmp(lower(arg4(1)),'d') | ||
63 | dith = 1; | ||
64 | else | ||
65 | dith = 0; | ||
66 | end | ||
67 | |||
68 | arg7 = arg5; | ||
69 | arg5 = arg3; | ||
70 | arg4 = arg2; | ||
71 | |||
72 | else | ||
73 | error( nargchk( 3, 7, nargin ) ); | ||
74 | |||
75 | if nargin < 7 | ||
76 | arg7 = 8; % DEFAULT_QE = 8 | ||
77 | end | ||
78 | |||
79 | if nargin < 6 | ||
80 | arg6 = 'n'; % DEFAULT_DITHER = 0 | ||
81 | end | ||
82 | |||
83 | if nargin < 5 | ||
84 | arg5 = [5 5 5]; % DEFAULT_Q = [5 5 5] | ||
85 | end | ||
86 | |||
87 | if nargin < 4 | ||
88 | arg4 = 256; % DEFAULT_K = 256 | ||
89 | end | ||
90 | |||
91 | rout = arg1; | ||
92 | g = arg2; | ||
93 | b = arg3; | ||
94 | |||
95 | if strcmp(lower(arg6(1)),'d') | ||
96 | dith = 1; | ||
97 | else | ||
98 | dith = 0; | ||
99 | end | ||
100 | |||
101 | end | ||
102 | |||
103 | if (~isa(rout,'uint8')) | ||
104 | rout = uint8(round(255*rout)); | ||
105 | end | ||
106 | if (~isa(g,'uint8')) | ||
107 | g = uint8(round(255*g)); | ||
108 | end | ||
109 | if (~isa(b,'uint8')) | ||
110 | b = uint8(round(255*b)); | ||
111 | end | ||
112 | [im,map] = vmquantc( rout, g, b, arg4, arg5, dith, arg7 ); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/vmquantc.mexhp7 b/SD-VBS/common/toolbox/toolbox_basic/common/vmquantc.mexhp7 new file mode 100755 index 0000000..c0e35c4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/vmquantc.mexhp7 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/vmquantc.mexlx b/SD-VBS/common/toolbox/toolbox_basic/common/vmquantc.mexlx new file mode 100755 index 0000000..f79c1d3 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/vmquantc.mexlx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/vmquantc.mexsol b/SD-VBS/common/toolbox/toolbox_basic/common/vmquantc.mexsol new file mode 100755 index 0000000..f711a98 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/vmquantc.mexsol | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/writepdm.m b/SD-VBS/common/toolbox/toolbox_basic/common/writepdm.m new file mode 100755 index 0000000..a8cba11 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/writepdm.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function writepfm(name,I) | ||
2 | % | ||
3 | % writepfm(name,I) | ||
4 | % | ||
5 | [nr,nc] = size(I); | ||
6 | |||
7 | fid = fopen(name, 'w'); | ||
8 | fprintf(fid, '%d %d\n', nr,nc); | ||
9 | fprintf(fid,'%d ',I'); | ||
10 | fclose(fid); | ||
11 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/common/writepfm.m b/SD-VBS/common/toolbox/toolbox_basic/common/writepfm.m new file mode 100755 index 0000000..ee25fdc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/common/writepfm.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function writepfm(name,I) | ||
2 | % | ||
3 | % writepfm(name,I) | ||
4 | % | ||
5 | [nr,nc] = size(I); | ||
6 | |||
7 | fid = fopen(name, 'w'); | ||
8 | fprintf(fid, '%d %d\n', nr,nc); | ||
9 | fprintf(fid,'%f ',I'); | ||
10 | fclose(fid); | ||
11 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/disp/disp_image.m b/SD-VBS/common/toolbox/toolbox_basic/disp/disp_image.m new file mode 100755 index 0000000..4745d8d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/disp/disp_image.m | |||
@@ -0,0 +1,19 @@ | |||
1 | function disp_image(img,sze,index,mask) | ||
2 | |||
3 | %figure(2) | ||
4 | subplot(sze(1),sze(2),index); | ||
5 | |||
6 | if (size(mask) ~= size(mask)), | ||
7 | error(['size of image is ',int2str(size(mask)),' size of mask is ',... | ||
8 | int2str(size(mask))]); | ||
9 | end | ||
10 | |||
11 | img = img-min(min(img)); | ||
12 | I = 0*(img.*(~mask)) + img.*mask; | ||
13 | I = img.*mask; | ||
14 | colormap(gray) | ||
15 | imagesc(I); | ||
16 | %axis('off') | ||
17 | axis('equal'); | ||
18 | axis('square'); | ||
19 | drawnow; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/disp/draw_box.m b/SD-VBS/common/toolbox/toolbox_basic/disp/draw_box.m new file mode 100755 index 0000000..556afdc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/disp/draw_box.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function draw_box(left,right,top,down) | ||
2 | |||
3 | plot([left,right],[top,top]); | ||
4 | hold on | ||
5 | plot([left,right],[down,down]); | ||
6 | hold on | ||
7 | plot([left,left],[top,down]); | ||
8 | hold on | ||
9 | plot([right,right],[top,down]); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/disp/draw_box2.m b/SD-VBS/common/toolbox/toolbox_basic/disp/draw_box2.m new file mode 100755 index 0000000..adec284 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/disp/draw_box2.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function draw_box(center,w_h,j) | ||
2 | |||
3 | |||
4 | center_x = center(1); | ||
5 | center_y = center(2); | ||
6 | |||
7 | %plot(center_x,center_y,'r*'); | ||
8 | %text(center_x,center_y,int2str(j)); | ||
9 | |||
10 | l_x = center_x-w_h(1); | ||
11 | r_x = center_x+w_h(1); | ||
12 | u_y = center_y-w_h(2); | ||
13 | l_y = center_y+w_h(2); | ||
14 | |||
15 | plot([l_x,r_x,r_x,l_x,l_x],[u_y,u_y,l_y,l_y,u_y],'c'); | ||
16 | |||
17 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/disp/im.m b/SD-VBS/common/toolbox/toolbox_basic/disp/im.m new file mode 100755 index 0000000..a707916 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/disp/im.m | |||
@@ -0,0 +1,8 @@ | |||
1 | function im(I) | ||
2 | |||
3 | imagesc(I);drawnow; | ||
4 | pixval on | ||
5 | title(inputname(1)) | ||
6 | %colormap(gray) | ||
7 | %colorbar | ||
8 | axis('image') | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/disp/ims.m b/SD-VBS/common/toolbox/toolbox_basic/disp/ims.m new file mode 100755 index 0000000..2fb5f25 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/disp/ims.m | |||
@@ -0,0 +1,3 @@ | |||
1 | function ims(I,nr,nc) | ||
2 | |||
3 | im(reshape(I,nr,nc)); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/disp/montage2.m b/SD-VBS/common/toolbox/toolbox_basic/disp/montage2.m new file mode 100755 index 0000000..fb04b0b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/disp/montage2.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function montage2(FI) | ||
2 | % montage2(FI) | ||
3 | % display 3D stack of images in a montage | ||
4 | |||
5 | [N1,N2,N3]=size(FI); | ||
6 | Q=zeros(N1,N2,1,N3); | ||
7 | for n=1:N3 | ||
8 | Q(:,:,1,n)=FI(:,:,n); | ||
9 | end | ||
10 | |||
11 | a=min(Q(:)); | ||
12 | b=max(Q(:)); | ||
13 | |||
14 | Q=Q-a; | ||
15 | Q=Q/(b-a); | ||
16 | |||
17 | montage(Q); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/disp/showmask.m b/SD-VBS/common/toolbox/toolbox_basic/disp/showmask.m new file mode 100755 index 0000000..f301638 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/disp/showmask.m | |||
@@ -0,0 +1,20 @@ | |||
1 | function showmask(V,M,display_flag); | ||
2 | % showmask(V,M); | ||
3 | % | ||
4 | % M is a nonneg. mask | ||
5 | |||
6 | V=V-min(V(:)); | ||
7 | V=V/max(V(:)); | ||
8 | V=.25+0.75*V; %brighten things up a bit | ||
9 | |||
10 | M=M-min(M(:)); | ||
11 | M=M/max(M(:)); | ||
12 | |||
13 | H=0.0+zeros(size(V)); | ||
14 | S=M; | ||
15 | RGB=hsv2rgb(H,S,V); | ||
16 | |||
17 | %if nargin>2 | ||
18 | image(RGB) | ||
19 | axis('image') | ||
20 | %end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/disp/showmaskb.m b/SD-VBS/common/toolbox/toolbox_basic/disp/showmaskb.m new file mode 100755 index 0000000..1f67ba2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/disp/showmaskb.m | |||
@@ -0,0 +1,20 @@ | |||
1 | function RGB = showmask(V,M,M2,display_flag); | ||
2 | % showmask(V,M); | ||
3 | % | ||
4 | % M is a nonneg. mask | ||
5 | |||
6 | V=V-min(V(:)); | ||
7 | V=V/max(V(:)); | ||
8 | V=.25+0.75*V; %brighten things up a bit | ||
9 | |||
10 | M=M-min(M(:)); | ||
11 | M=M/max(M(:)); | ||
12 | |||
13 | H=0.6*M2+0*M; | ||
14 | S=min(1,M2+M); | ||
15 | RGB=hsv2rgb(H,S,V); | ||
16 | |||
17 | %if nargin>2 | ||
18 | image(RGB) | ||
19 | axis('image') | ||
20 | %end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/construct_w.m b/SD-VBS/common/toolbox/toolbox_basic/fact/construct_w.m new file mode 100755 index 0000000..372b4b6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/construct_w.m | |||
@@ -0,0 +1,25 @@ | |||
1 | function W = construct_w(centers,Ds,img_center,indexes,frames) | ||
2 | % | ||
3 | % function W = construct_w(centers,Ds,img_center,indexes,frames) | ||
4 | % optional: frames | ||
5 | % | ||
6 | |||
7 | |||
8 | points = length(indexes); | ||
9 | if (nargin == 4), | ||
10 | frames = 0.5*size(centers,2); | ||
11 | end | ||
12 | |||
13 | W = zeros(2*frames,points); | ||
14 | |||
15 | center_x = img_center(1); | ||
16 | center_y = img_center(2); | ||
17 | |||
18 | for j=1:frames, | ||
19 | % x is centers(:,2*j-1) | ||
20 | % y is centers(:,2*j) | ||
21 | % d is Ds(:,2*j-1) | ||
22 | W(j,:) = (centers(indexes,2*j-1) -center_x)'./Ds(indexes,2*j-1)'; | ||
23 | W(j+frames,:) = (centers(indexes,2*j) -center_y)'./Ds(indexes,2*j-1)'; | ||
24 | % W(j+2*frames,:) = ones(1,points)./Ds(indexes,2*j-1)'; | ||
25 | end \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/construct_w2.m b/SD-VBS/common/toolbox/toolbox_basic/fact/construct_w2.m new file mode 100755 index 0000000..b2939b7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/construct_w2.m | |||
@@ -0,0 +1,25 @@ | |||
1 | function W = construct_w2(centers,Ds,img_center,indexes,frames) | ||
2 | % | ||
3 | % function W = construct_w2(centers,Ds,img_center,indexes,frames) | ||
4 | % optional: frames | ||
5 | % | ||
6 | |||
7 | |||
8 | points = length(indexes); | ||
9 | if (nargin == 4), | ||
10 | frames = 0.5*size(centers,2); | ||
11 | end | ||
12 | |||
13 | W = zeros(3*frames,points); | ||
14 | |||
15 | center_x = img_center(1); | ||
16 | center_y = img_center(2); | ||
17 | |||
18 | for j=1:frames, | ||
19 | % x is centers(:,2*j-1) | ||
20 | % y is centers(:,2*j) | ||
21 | % d is Ds(:,2*j-1) | ||
22 | W(j,:) = (centers(indexes,2*j-1) -center_x)'./Ds(indexes,2*j-1)'; | ||
23 | W(j+frames,:) = (centers(indexes,2*j) -center_y)'./Ds(indexes,2*j-1)'; | ||
24 | W(j+2*frames,:) = ones(1,points)./Ds(indexes,2*j-1)'; | ||
25 | end \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/factor.m b/SD-VBS/common/toolbox/toolbox_basic/fact/factor.m new file mode 100755 index 0000000..635c29a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/factor.m | |||
@@ -0,0 +1,50 @@ | |||
1 | % | ||
2 | % Usage: | ||
3 | % | ||
4 | % [R,t,S] = factor(W) | ||
5 | % | ||
6 | % Function to factor a matrix of input data (W) into the camera | ||
7 | % rotation matrix (R), translation (t), and the shape matrix (S). | ||
8 | % Three-dimensional version. Failure of normalization results in | ||
9 | % empty R and S. | ||
10 | |||
11 | function [R,t,S] = factor(W) | ||
12 | |||
13 | pts = size(W,2); | ||
14 | t = W*ones(pts,1)/pts; | ||
15 | W = W - t*ones(1,pts); | ||
16 | |||
17 | % Use SVD to factor W. | ||
18 | [a,b,c] = svd(W,0); | ||
19 | |||
20 | smallb = b(1:3,1:3); % Since W is rank 3, b has only three meaningful values | ||
21 | sqrtb = sqrt(smallb); | ||
22 | Rhat = a(:,1:3) * sqrtb; | ||
23 | Shat = sqrtb * c(:,1:3)'; | ||
24 | |||
25 | G = findG(Rhat); | ||
26 | |||
27 | if size(G,1) == 0, | ||
28 | R = []; | ||
29 | S = []; | ||
30 | else | ||
31 | R = Rhat*G; | ||
32 | S = inv(G)*Shat; | ||
33 | |||
34 | % rotation matrix that aligns the reference frame with the first camera | ||
35 | F = size(R,1)/2; | ||
36 | R1 = R(1,:); | ||
37 | R1 = R1/norm(R1); | ||
38 | R2 = R(F+1,:); | ||
39 | R2 = R2/norm(R2); | ||
40 | R3 = cross(R1,R2); | ||
41 | R3 = R3/norm(R3); | ||
42 | P = [R1; R2; R3]; | ||
43 | P = P'; | ||
44 | |||
45 | R = R*P; | ||
46 | S = inv(P)*S; | ||
47 | end | ||
48 | |||
49 | |||
50 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/factor_test.m b/SD-VBS/common/toolbox/toolbox_basic/fact/factor_test.m new file mode 100755 index 0000000..12ceb95 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/factor_test.m | |||
@@ -0,0 +1,52 @@ | |||
1 | % | ||
2 | % Usage: | ||
3 | % | ||
4 | % [R,t,S] = factor(W) | ||
5 | % | ||
6 | % Function to factor a matrix of input data (W) into the camera | ||
7 | % rotation matrix (R), translation (t), and the shape matrix (S). | ||
8 | % Three-dimensional version. Failure of normalization results in | ||
9 | % empty R and S. | ||
10 | |||
11 | function [R,t,S,C,b] = factor(W) | ||
12 | |||
13 | pts = size(W,2); | ||
14 | t = W*ones(pts,1)/pts; | ||
15 | W = W - t*ones(1,pts); | ||
16 | |||
17 | % Use SVD to factor W. | ||
18 | [a,b,c] = svd(W,0); | ||
19 | |||
20 | figure(3);plot(diag(b)) | ||
21 | |||
22 | smallb = b(1:3,1:3); % Since W is rank 3, b has only three meaningful values | ||
23 | sqrtb = sqrt(smallb); | ||
24 | Rhat = a(:,1:3) * sqrtb; | ||
25 | Shat = sqrtb * c(:,1:3)'; | ||
26 | |||
27 | [G,C] = findg1(Rhat); | ||
28 | |||
29 | if size(G,1) == 0, | ||
30 | R = []; | ||
31 | S = []; | ||
32 | else | ||
33 | R = Rhat*G; | ||
34 | S = inv(G)*Shat; | ||
35 | |||
36 | % rotation matrix that aligns the reference frame with the first camera | ||
37 | F = size(R,1)/2; | ||
38 | R1 = R(1,:); | ||
39 | R1 = R1/norm(R1); | ||
40 | R2 = R(F+1,:); | ||
41 | R2 = R2/norm(R2); | ||
42 | R3 = cross(R1,R2); | ||
43 | R3 = R3/norm(R3); | ||
44 | P = [R1; R2; R3]; | ||
45 | P = P'; | ||
46 | |||
47 | R = R*P; | ||
48 | S = inv(P)*S; | ||
49 | end | ||
50 | |||
51 | |||
52 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/factor_test2.m b/SD-VBS/common/toolbox/toolbox_basic/fact/factor_test2.m new file mode 100755 index 0000000..3520122 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/factor_test2.m | |||
@@ -0,0 +1,52 @@ | |||
1 | % | ||
2 | % Usage: | ||
3 | % | ||
4 | % [R,t,S] = factor(W) | ||
5 | % | ||
6 | % Function to factor a matrix of input data (W) into the camera | ||
7 | % rotation matrix (R), translation (t), and the shape matrix (S). | ||
8 | % Three-dimensional version. Failure of normalization results in | ||
9 | % empty R and S. | ||
10 | |||
11 | function [R,t,S,C,b] = factor(W) | ||
12 | |||
13 | pts = size(W,2); | ||
14 | t = W*ones(pts,1)/pts; | ||
15 | W = W - t*ones(1,pts); | ||
16 | |||
17 | % Use SVD to factor W. | ||
18 | [a,b,c] = svd(W,0); | ||
19 | |||
20 | figure(3);plot(diag(b)) | ||
21 | |||
22 | smallb = b(1:3,1:3); % Since W is rank 3, b has only three meaningful values | ||
23 | sqrtb = sqrt(smallb); | ||
24 | Rhat = a(:,1:3) * sqrtb; | ||
25 | Shat = sqrtb * c(:,1:3)'; | ||
26 | |||
27 | [G,C] = findg2(Rhat); | ||
28 | |||
29 | if size(G,1) == 0, | ||
30 | R = []; | ||
31 | S = []; | ||
32 | else | ||
33 | R = Rhat*G; | ||
34 | S = inv(G)*Shat; | ||
35 | |||
36 | % rotation matrix that aligns the reference frame with the first camera | ||
37 | F = size(R,1)/2; | ||
38 | R1 = R(1,:); | ||
39 | R1 = R1/norm(R1); | ||
40 | R2 = R(F+1,:); | ||
41 | R2 = R2/norm(R2); | ||
42 | R3 = cross(R1,R2); | ||
43 | R3 = R3/norm(R3); | ||
44 | P = [R1; R2; R3]; | ||
45 | P = P'; | ||
46 | |||
47 | R = R*P; | ||
48 | S = inv(P)*S; | ||
49 | end | ||
50 | |||
51 | |||
52 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/factorizaion.tar b/SD-VBS/common/toolbox/toolbox_basic/fact/factorizaion.tar new file mode 100755 index 0000000..133cdff --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/factorizaion.tar | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/findG.m b/SD-VBS/common/toolbox/toolbox_basic/fact/findG.m new file mode 100755 index 0000000..9a6bd73 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/findG.m | |||
@@ -0,0 +1,48 @@ | |||
1 | function G = find3G(Rhat) | ||
2 | |||
3 | % number of frames | ||
4 | F = size(Rhat,1)/2; | ||
5 | |||
6 | % Build matrix Q such that Q * v = [1,...,1,0,...,0] where v is a six | ||
7 | % element vector containg all six distinct elements of the Matrix C | ||
8 | |||
9 | clear Q | ||
10 | for f = 1:F, | ||
11 | g = f + F; | ||
12 | h = g + F; | ||
13 | Q(f,:) = zt(Rhat(f,:), Rhat(f,:)); | ||
14 | Q(g,:) = zt(Rhat(g,:), Rhat(g,:)); | ||
15 | Q(h,:) = zt(Rhat(f,:), Rhat(g,:)); | ||
16 | end | ||
17 | |||
18 | % Solve for v | ||
19 | rhs = [ones(2*F,1); zeros(F,1)]; | ||
20 | v = Q \ rhs; | ||
21 | |||
22 | % C is a symmetric 3x3 matrix such that C = G * transpose(G) | ||
23 | C(1,1) = v(1); | ||
24 | C(1,2) = v(2); | ||
25 | C(1,3) = v(3); | ||
26 | C(2,2) = v(4); | ||
27 | C(2,3) = v(5); | ||
28 | C(3,3) = v(6); | ||
29 | C(2,1) = C(1,2); | ||
30 | C(3,1) = C(1,3); | ||
31 | C(3,2) = C(2,3); | ||
32 | |||
33 | e = eig(C); | ||
34 | disp(e) | ||
35 | |||
36 | if (any(e<= 0)), | ||
37 | G = []; | ||
38 | else | ||
39 | G = sqrtm(C); | ||
40 | end | ||
41 | |||
42 | %neg = 0; | ||
43 | %if e(1) <= 0, neg = 1; end | ||
44 | %if e(2) <= 0, neg = 1; end | ||
45 | %if e(3) <= 0, neg = 1; end | ||
46 | %if neg == 1, G = []; | ||
47 | %else G = sqrtm(C); | ||
48 | %end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/findg1.m b/SD-VBS/common/toolbox/toolbox_basic/fact/findg1.m new file mode 100755 index 0000000..f14ecc6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/findg1.m | |||
@@ -0,0 +1,49 @@ | |||
1 | function [G,C] = find3G(Rhat) | ||
2 | |||
3 | % number of frames | ||
4 | F = size(Rhat,1)/2; | ||
5 | |||
6 | % Build matrix Q such that Q * v = [1,...,1,0,...,0] where v is a six | ||
7 | % element vector containg all six distinct elements of the Matrix C | ||
8 | |||
9 | clear Q | ||
10 | for f = 1:F, | ||
11 | g = f + F; | ||
12 | h = g + F; | ||
13 | Q(f,:) = zt(Rhat(f,:), Rhat(f,:)); | ||
14 | Q(g,:) = zt(Rhat(g,:), Rhat(g,:)); | ||
15 | Q(h,:) = zt(Rhat(f,:), Rhat(g,:)); | ||
16 | end | ||
17 | |||
18 | % Solve for v | ||
19 | rhs = [ones(2*F,1); zeros(F,1)]; | ||
20 | v = Q \ rhs; | ||
21 | |||
22 | % C is a symmetric 3x3 matrix such that C = G * transpose(G) | ||
23 | C(1,1) = v(1); | ||
24 | C(1,2) = v(2); | ||
25 | C(1,3) = v(3); | ||
26 | C(2,2) = v(4); | ||
27 | C(2,3) = v(5); | ||
28 | C(3,3) = v(6); | ||
29 | C(2,1) = C(1,2); | ||
30 | C(3,1) = C(1,3); | ||
31 | C(3,2) = C(2,3); | ||
32 | |||
33 | e = eig(C); | ||
34 | disp(e) | ||
35 | |||
36 | if (any(e<= 0)), | ||
37 | C = C -2*min(e)*eye(3); | ||
38 | G = sqrtm(C); | ||
39 | else | ||
40 | G = sqrtm(C); | ||
41 | end | ||
42 | |||
43 | %neg = 0; | ||
44 | %if e(1) <= 0, neg = 1; end | ||
45 | %if e(2) <= 0, neg = 1; end | ||
46 | %if e(3) <= 0, neg = 1; end | ||
47 | %if neg == 1, G = []; | ||
48 | %else G = sqrtm(C); | ||
49 | %end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/findg2.m b/SD-VBS/common/toolbox/toolbox_basic/fact/findg2.m new file mode 100755 index 0000000..5a84b86 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/findg2.m | |||
@@ -0,0 +1,56 @@ | |||
1 | function [G,C] = find3G(Rhat) | ||
2 | |||
3 | % number of frames | ||
4 | F = size(Rhat,1)/3; | ||
5 | |||
6 | % Build matrix Q such that Q * v = [1,...,1,0,...,0] where v is a six | ||
7 | % element vector containg all six distinct elements of the Matrix C | ||
8 | |||
9 | clear Q | ||
10 | for f = 1:F, | ||
11 | g = f + F; | ||
12 | h = g + F; | ||
13 | j = h + F; | ||
14 | k = j + F; | ||
15 | l = k + F; | ||
16 | Q(f,:) = zt(Rhat(f,:), Rhat(f,:)); | ||
17 | Q(g,:) = zt(Rhat(g,:), Rhat(g,:)); | ||
18 | Q(h,:) = zt(Rhat(h,:), Rhat(h,:)); | ||
19 | Q(j,:) = zt(Rhat(f,:), Rhat(g,:)); | ||
20 | Q(k,:) = zt(Rhat(f,:), Rhat(h,:)); | ||
21 | Q(l,:) = zt(Rhat(g,:), Rhat(h,:)); | ||
22 | end | ||
23 | |||
24 | % Solve for v | ||
25 | rhs = [ones(3*F,1); zeros(3*F,1)]; | ||
26 | v = Q \ rhs; | ||
27 | |||
28 | % C is a symmetric 3x3 matrix such that C = G * transpose(G) | ||
29 | C(1,1) = v(1); | ||
30 | C(1,2) = v(2); | ||
31 | C(1,3) = v(3); | ||
32 | C(2,2) = v(4); | ||
33 | C(2,3) = v(5); | ||
34 | C(3,3) = v(6); | ||
35 | C(2,1) = C(1,2); | ||
36 | C(3,1) = C(1,3); | ||
37 | C(3,2) = C(2,3); | ||
38 | |||
39 | e = eig(C); | ||
40 | disp(e) | ||
41 | |||
42 | |||
43 | if (any(e<= 0)), | ||
44 | C = C -2*min(e)*eye(3); | ||
45 | G = sqrtm(C); | ||
46 | else | ||
47 | G = sqrtm(C); | ||
48 | end | ||
49 | |||
50 | %neg = 0; | ||
51 | %if e(1) <= 0, neg = 1; end | ||
52 | %if e(2) <= 0, neg = 1; end | ||
53 | %if e(3) <= 0, neg = 1; end | ||
54 | %if neg == 1, G = []; | ||
55 | %else G = sqrtm(C); | ||
56 | %end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/hotel.mat b/SD-VBS/common/toolbox/toolbox_basic/fact/hotel.mat new file mode 100755 index 0000000..61ea6c8 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/hotel.mat | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/show_3dpoints.m b/SD-VBS/common/toolbox/toolbox_basic/fact/show_3dpoints.m new file mode 100755 index 0000000..b6edfd5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/show_3dpoints.m | |||
@@ -0,0 +1,22 @@ | |||
1 | function show_3dpoints(S) | ||
2 | |||
3 | |||
4 | for j=1:size(S,2), | ||
5 | x = S(1,j); | ||
6 | y = S(2,j); | ||
7 | z = S(3,j); | ||
8 | plot3(x,y,z,'*'); | ||
9 | hold on; | ||
10 | plot3([x,0],[y,0],[z,0],'r'); | ||
11 | % plot3([x,x],[y,y],[z,0],'r'); | ||
12 | % plot3([x,0],[y,y],[z,z],'r'); plot3([x,x],[y,0],[z,z],'r'); | ||
13 | text(x,y,z,int2str(j)) | ||
14 | % plot3(x,y,0,'co'); | ||
15 | end | ||
16 | |||
17 | grid on | ||
18 | xlabel('x'); | ||
19 | ylabel('y'); | ||
20 | zlabel('z'); | ||
21 | |||
22 | hold off \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/show_S.m b/SD-VBS/common/toolbox/toolbox_basic/fact/show_S.m new file mode 100755 index 0000000..5828696 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/show_S.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function show_S(S,fig) | ||
2 | |||
3 | if (nargin == 1), | ||
4 | figure(1); | ||
5 | else | ||
6 | figure(fig); | ||
7 | end | ||
8 | |||
9 | num_points = size(S,2); | ||
10 | |||
11 | subplot(1,2,1); plot(S(1,:),S(3,:),'cx'); axis('equal');axis('square');hold on | ||
12 | subplot(1,2,2); plot(S(2,:),S(3,:),'cx'); axis('equal');axis('square');hold on | ||
13 | |||
14 | for j=1:num_points, | ||
15 | subplot(1,2,1);text(S(1,j),S(3,j),int2str(j));hold off | ||
16 | subplot(1,2,2);text(S(2,j),S(3,j),int2str(j));hold off | ||
17 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/show_t.m b/SD-VBS/common/toolbox/toolbox_basic/fact/show_t.m new file mode 100755 index 0000000..b475c76 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/show_t.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function show_t(t) | ||
2 | |||
3 | frames = 0.5*length(t); | ||
4 | |||
5 | ts = reshape(t,frames,2); | ||
6 | |||
7 | plot(ts(:,1),ts(:,2)); | ||
8 | hold on | ||
9 | plot(ts(:,1),ts(:,2),'rx'); | ||
10 | hold off; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/show_t3.m b/SD-VBS/common/toolbox/toolbox_basic/fact/show_t3.m new file mode 100755 index 0000000..2766061 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/show_t3.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function show_t(t) | ||
2 | |||
3 | frames = length(t)/3; | ||
4 | |||
5 | ts = reshape(t,frames,3); | ||
6 | |||
7 | plot3(ts(:,1),ts(:,2),ts(:,3)); | ||
8 | hold on | ||
9 | plot3(ts(:,1),ts(:,2),ts(:,3),'rx'); | ||
10 | hold off; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/fact/zt.m b/SD-VBS/common/toolbox/toolbox_basic/fact/zt.m new file mode 100755 index 0000000..3f88d21 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/fact/zt.m | |||
@@ -0,0 +1,6 @@ | |||
1 | % the z' operator described in the paper (returns a row vector) | ||
2 | |||
3 | function v = zt(a, b) | ||
4 | |||
5 | v = [ a(1)*b(1), a(1)*b(2)+a(2)*b(1), a(1)*b(3)+a(3)*b(1), ... | ||
6 | a(2)*b(2), a(2)*b(3)+a(3)*b(2), a(3)*b(3) ]; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/91048.jpg b/SD-VBS/common/toolbox/toolbox_basic/filter/91048.jpg new file mode 100755 index 0000000..6b2313b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/91048.jpg | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/bar2d.m b/SD-VBS/common/toolbox/toolbox_basic/filter/bar2d.m new file mode 100755 index 0000000..76fa819 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/bar2d.m | |||
@@ -0,0 +1,16 @@ | |||
1 | function kernel = bar2d(sigx,sigy,siz,angle) | ||
2 | |||
3 | X = -siz:.1:siz; | ||
4 | G = exp(-0.5*X.^2/sigx^2); | ||
5 | |||
6 | DGG = (1/sigy^2) * ((X/sigy).^2-1) .* exp(- (X/sigy).^2/2); | ||
7 | %DGG = (X.^2/(sqrt(2*pi)*sigy^5) - 1/(sqrt(2*pi)*sigy^2)) .* ... | ||
8 | % exp(-0.5*X.^2/sigy^2); | ||
9 | |||
10 | K = G'*DGG; | ||
11 | K = rotate_J(angle,K); | ||
12 | |||
13 | K = imresize(K,0.1); | ||
14 | K = K-mean(mean(K)); | ||
15 | |||
16 | kernel = K; \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/barrot.m b/SD-VBS/common/toolbox/toolbox_basic/filter/barrot.m new file mode 100755 index 0000000..bd7676e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/barrot.m | |||
@@ -0,0 +1,22 @@ | |||
1 | img1 = gifread('color010.gif'); | ||
2 | img1 = img1(220:350,1:200); | ||
3 | img2 = gifread('color-avg.gif'); | ||
4 | img2 = img2(220:350,1:200); | ||
5 | |||
6 | sigx = 3; | ||
7 | sigy = 3; | ||
8 | siz = 8; | ||
9 | angles = 0:19:179; | ||
10 | |||
11 | [Imag1,Iangle1] = brute_force_angle(img1,sigx,sigy,siz,angles); | ||
12 | [Imag2,Iangle2] = brute_force_angle(img2,sigx,sigy,siz,angles); | ||
13 | |||
14 | tresh = max(max(Imag1))*0.1 | ||
15 | D = angle_diff(Imag1,Iangle1,Imag2,Iangle2,tresh); | ||
16 | subplot(2,2,1); imagesc(Iangle1*180/pi); colorbar; | ||
17 | subplot(2,2,2); imagesc(Iangle2*180/pi); colorbar; | ||
18 | subplot(2,2,3); imagesc(D*180/pi); colorbar; | ||
19 | subplot(2,2,4); imagesc(Imag1); colorbar; | ||
20 | colormap(jet) | ||
21 | |||
22 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/bars.m b/SD-VBS/common/toolbox/toolbox_basic/filter/bars.m new file mode 100755 index 0000000..77eccea --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/bars.m | |||
@@ -0,0 +1,39 @@ | |||
1 | function [filt] = bars(X,Y,ks); | ||
2 | %FIL1 the first filter to use has the following specifications: | ||
3 | % | ||
4 | % real part: 2nd derivative of gaussian along Y | ||
5 | % normal gaussian along X | ||
6 | % This filter is elongated along the X direction | ||
7 | % imag part: hilbert transform of the real part | ||
8 | % | ||
9 | % [filt] = fil1(X,Y,ks); | ||
10 | % X,Y : index matrix obtained by meshgrid | ||
11 | % ks : kernel size | ||
12 | % filt : the output kernel | ||
13 | % | ||
14 | |||
15 | %% | ||
16 | %% (c) Thomas Leung | ||
17 | %% California Institute of Technology | ||
18 | %% Feb 27, 1994. | ||
19 | %% | ||
20 | |||
21 | if(nargin == 2) | ||
22 | ks = 17; | ||
23 | end | ||
24 | |||
25 | sigmay = 2.4 * ks / 17; | ||
26 | sigmax = 3 * sigmay; | ||
27 | |||
28 | fxr = exp(-(X/sigmax).^2/2); | ||
29 | fyr = (1/sigmay^2) * ((Y/sigmay).^2-1) .* exp(- (Y/sigmay).^2/2); | ||
30 | nrm = 1/(sigmax*sigmay*2*pi); | ||
31 | |||
32 | % real part of filter | ||
33 | fr = nrm * fxr .* fyr; | ||
34 | |||
35 | % imag part of filter | ||
36 | filt = hilbert(fr); | ||
37 | |||
38 | |||
39 | return; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/clip_image.m b/SD-VBS/common/toolbox/toolbox_basic/filter/clip_image.m new file mode 100755 index 0000000..bf7b50c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/clip_image.m | |||
@@ -0,0 +1,6 @@ | |||
1 | function J = clip_image(I,w) | ||
2 | |||
3 | [size_y,size_x] = size(I); | ||
4 | |||
5 | J = I(w+1:size_y-w,w+1:size_x-w); | ||
6 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/compute_J_simple.m b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_J_simple.m new file mode 100755 index 0000000..e790601 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_J_simple.m | |||
@@ -0,0 +1,50 @@ | |||
1 | function J = compute_J(I,A,D,base) | ||
2 | %% function J = compute_J(I,A,D) | ||
3 | % | ||
4 | |||
5 | if nargin == 3, | ||
6 | base = -1; | ||
7 | end | ||
8 | |||
9 | [size_y,size_x] = size(I); | ||
10 | [center_x,center_y] = find_center(size_x,size_y); | ||
11 | |||
12 | add_x = round(size_x*0.45); | ||
13 | add_y = round(size_y*0.45); | ||
14 | big_I = base*ones(size_y+2*add_y,size_x+2*add_x); | ||
15 | |||
16 | big_I(add_y+1:add_y+size_y,add_x+1:add_x+size_x) = I; | ||
17 | |||
18 | center_x = add_x+ center_x; | ||
19 | center_y = add_y+ center_y; | ||
20 | [size_y,size_x] = size(big_I); | ||
21 | |||
22 | %a = angle * pi/180; | ||
23 | %A = [cos(a),-sin(a);sin(a),cos(a)]; | ||
24 | |||
25 | [XX,YY] = meshgrid(1:size_x,1:size_y); | ||
26 | |||
27 | x = reshape(XX,size_x*size_y,1); | ||
28 | y = reshape(YY,size_x*size_y,1); | ||
29 | index(:,1) = x-center_x; | ||
30 | |||
31 | %index(:,2) = (size_y+1) - y; | ||
32 | index(:,2) = y-center_y; | ||
33 | |||
34 | position_new = A*index'; | ||
35 | position_new(1,:) = position_new(1,:)+D(1)+center_x; | ||
36 | position_new(2,:) = position_new(2,:)+D(2)+center_y; | ||
37 | %position_new(2,:) = (size_y+1) - position_new(2,:); | ||
38 | |||
39 | position_new_x = reshape(position_new(1,:),size_y,size_x); | ||
40 | position_new_y = reshape(position_new(2,:),size_y,size_x); | ||
41 | |||
42 | J = m_interp4(big_I,position_new_x,position_new_y); | ||
43 | |||
44 | |||
45 | [size_y,size_x] = size(I); | ||
46 | J = J(add_y+1:add_y+size_y,add_x+1:add_x+size_x); | ||
47 | |||
48 | |||
49 | |||
50 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/compute_angle.m b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_angle.m new file mode 100755 index 0000000..7a995af --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_angle.m | |||
@@ -0,0 +1,18 @@ | |||
1 | function [angle,mag,c2,c3] = compute_angle(I) | ||
2 | |||
3 | [g,ga,gb,gc] = compute_g2(I,0); | ||
4 | [h,ha,hb,hc,hd] = compute_h2(I,0); | ||
5 | |||
6 | c2 = 0.5*(ga.^2 - gc.^2) + 0.46875*(ha.^2 - hd.^2) +... | ||
7 | 0.28125*(hb.^2 - hc.^2) + 0.1875*(ha.*hc - hb.*hd); | ||
8 | |||
9 | c3 = -ga.*gb - gb.*gc - 0.9375*(hc.*hd + ha.*hb) -... | ||
10 | 1.6875*hb.*hc - 0.1875*ha.*hd; | ||
11 | |||
12 | [angle,mag] = cart2pol(-c2,-c3); | ||
13 | |||
14 | %angle = angle/2+pi/2; | ||
15 | %angle = (angle>pi).*(angle-2*pi) + (angle<=pi).*angle; | ||
16 | |||
17 | angle = angle/2; | ||
18 | mag = sqrt(mag); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/compute_filter_fft.m b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_filter_fft.m new file mode 100755 index 0000000..359c6ba --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_filter_fft.m | |||
@@ -0,0 +1,84 @@ | |||
1 | function [filter_output,filters] = compute_filter_fft(I,sig,r,sz,num_ori); | ||
2 | % | ||
3 | % | ||
4 | % | ||
5 | |||
6 | ori_incr=180/num_ori; | ||
7 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
8 | |||
9 | as = ori_offset:ori_incr:180+ori_offset-ori_incr; | ||
10 | |||
11 | filter_output = []; | ||
12 | filters = []; | ||
13 | |||
14 | wsz = 2*round(sz(end)) + 1; | ||
15 | M1 = wsz;M2 = wsz; | ||
16 | |||
17 | %%%%% prepare FFT of image %%%%%%%%%%%%% | ||
18 | |||
19 | [N1,N2]=size(I); | ||
20 | tmp=zeros(size(I)+[M1-1 M2-1]); | ||
21 | tmp(1:N1,1:N2)=I; | ||
22 | IF=fft2(tmp); | ||
23 | |||
24 | |||
25 | %%%%%%%%%% filtering stage %%%%%%%%%%% | ||
26 | if size(sig,2)== 1, | ||
27 | |||
28 | for j = 1:length(as), | ||
29 | fprintf('.'); | ||
30 | angle = as(j); | ||
31 | |||
32 | g = mdoog2(sig,r,angle,round(sz)); | ||
33 | |||
34 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
35 | |||
36 | g = g/sum(sum(abs(g))); | ||
37 | |||
38 | filters(:,:,j) = g; | ||
39 | |||
40 | gF = fft2(g,N1+M1-1,N2+M2-1); | ||
41 | IgF = IF.*gF; | ||
42 | Ig = real(ifft2(IgF)); | ||
43 | Ig = Ig(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
44 | |||
45 | %c = conv2(I,g,'valid'); | ||
46 | |||
47 | filter_output(:,:,j) = Ig; | ||
48 | end | ||
49 | else | ||
50 | |||
51 | % there are multiple scales | ||
52 | sigs = sig; | ||
53 | szs = sz; | ||
54 | for k = 1:size(sigs,2), | ||
55 | sig = sigs(k); | ||
56 | sz = szs(end); | ||
57 | fprintf('%d',k); | ||
58 | for j = 1:length(as), | ||
59 | fprintf('.'); | ||
60 | angle = as(j); | ||
61 | |||
62 | g = mdoog2(sig,r,angle,round(sz)); | ||
63 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
64 | g = g/sum(sum(abs(g))); | ||
65 | |||
66 | gF = fft2(g,N1+M1-1,N2+M2-1); | ||
67 | IgF = IF.*gF; | ||
68 | Ig = real(ifft2(IgF)); | ||
69 | Ig = Ig(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
70 | |||
71 | %c = conv2(I,g,'valid'); | ||
72 | %c = conv2(I,g,'same'); | ||
73 | |||
74 | filter_output(:,:,j,k) = Ig; | ||
75 | filters(:,:,j,k) = g; | ||
76 | end | ||
77 | |||
78 | |||
79 | end | ||
80 | |||
81 | end | ||
82 | |||
83 | fprintf('\n'); | ||
84 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/compute_g2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_g2.m new file mode 100755 index 0000000..ac27d00 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_g2.m | |||
@@ -0,0 +1,23 @@ | |||
1 | function [g,ga,gb,gc] = compute_g2(I,angle) | ||
2 | |||
3 | if (nargin == 1), | ||
4 | angle = 0; | ||
5 | end | ||
6 | |||
7 | f1 = [0.0094 0.1148 0.3964 -0.0601 -0.9213 -0.0601 0.3964 0.1148 0.0094]; | ||
8 | f2 = [0.0008 0.0176 0.166 0.6383 1.0 0.6383 0.166 0.0176 0.0008]; | ||
9 | f3 = [-0.0028 -0.048 -0.302 -0.5806 0 0.5806 0.302 0.048 0.0028]; | ||
10 | |||
11 | %ga = conv2(conv2(I,f2,'same'),f1','same'); | ||
12 | %gb = conv2(conv2(I,f3,'same'),f3','same'); | ||
13 | %gc = conv2(conv2(I,f1,'same'),f2','same'); | ||
14 | |||
15 | ga = conv2(conv2(I,f1,'same'),f2','same'); | ||
16 | gb = conv2(conv2(I,f3,'same'),f3','same'); | ||
17 | gc = conv2(conv2(I,f2,'same'),f1','same'); | ||
18 | |||
19 | ka = cos(angle)^2; | ||
20 | kb = -2*cos(angle)*sin(angle); | ||
21 | kc = sin(angle)^2; | ||
22 | |||
23 | g = ka*ga + kb*gb + kc*gc; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/compute_h2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_h2.m new file mode 100755 index 0000000..e4cdcfb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_h2.m | |||
@@ -0,0 +1,27 @@ | |||
1 | function [h,ha,hb,hc,hd] = compute_h2(I,angle) | ||
2 | |||
3 | if (nargin == 1), | ||
4 | angle = 0; | ||
5 | end | ||
6 | |||
7 | f1 = [0.0098 0.0618 -0.0998 -0.7551 0 0.7551 0.0998 -0.0618 -0.0098]; | ||
8 | f2 = [ 0.0008 0.0176 0.166 0.6383 1 0.6383 0.166 0.0176 0.0008]; | ||
9 | f3 = -[-0.002 -0.0354 -0.2225 -0.4277 0 0.4277 0.2225 0.0354 0.002]; | ||
10 | f4 = [0.0048 0.0566 0.1695 -0.1889 -0.7349 -0.1889 0.1695 0.0566 0.0048]; | ||
11 | |||
12 | %ha = conv2(conv2(I,f2,'same'),f1','same'); | ||
13 | %hb = conv2(conv2(I,f3,'same'),f4','same'); | ||
14 | %hc = conv2(conv2(I,f4,'same'),f3','same'); | ||
15 | %hd = conv2(conv2(I,f1,'same'),f2','same'); | ||
16 | |||
17 | ha = conv2(conv2(I,f1,'same'),f2','same'); | ||
18 | hb = conv2(conv2(I,f4,'same'),f3','same'); | ||
19 | hc = conv2(conv2(I,f3,'same'),f4','same'); | ||
20 | hd = conv2(conv2(I,f2,'same'),f1','same'); | ||
21 | |||
22 | ka = cos(angle)^3; | ||
23 | kb = -3*cos(angle)^2*sin(angle); | ||
24 | kc = 3*cos(angle)*sin(angle)^2; | ||
25 | kd = -sin(angle)^3; | ||
26 | |||
27 | h = ka*ha + kb*hb + kc*hc + kd*hd; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/compute_ofilter_fft.m b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_ofilter_fft.m new file mode 100755 index 0000000..41989ed --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/compute_ofilter_fft.m | |||
@@ -0,0 +1,88 @@ | |||
1 | function [filter_output,filters] = compute_odd_filter_fft(I,sig,r,sz,num_ori); | ||
2 | % | ||
3 | % computes the filter response of I to the set of odd symmetric filters | ||
4 | % | ||
5 | % sig = scale(sigma) for the filters | ||
6 | % r = enlongation factor | ||
7 | % sz = the radius of the filters | ||
8 | % num_ori = number of orientations | ||
9 | % | ||
10 | |||
11 | ori_incr=180/num_ori; | ||
12 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
13 | |||
14 | as = ori_offset:ori_incr:180+ori_offset-ori_incr; | ||
15 | |||
16 | filter_output = zeros(size(I,1),size(I,2),num_ori,length(sig)); | ||
17 | filters = []; | ||
18 | |||
19 | wsz = 2*round(sz(end)) + 1; | ||
20 | M1 = wsz;M2 = wsz; | ||
21 | |||
22 | %%%%% prepare FFT of image %%%%%%%%%%%%% | ||
23 | |||
24 | [N1,N2]=size(I); | ||
25 | tmp=zeros(size(I)+[M1-1 M2-1]); | ||
26 | tmp(1:N1,1:N2)=I; | ||
27 | IF=fft2(tmp); | ||
28 | |||
29 | |||
30 | %%%%%%%%%% filtering stage %%%%%%%%%%% | ||
31 | if size(sig,2)== 1, | ||
32 | |||
33 | for j = 1:length(as), | ||
34 | fprintf('.'); | ||
35 | angle = as(j); | ||
36 | |||
37 | g = mk_odd_filter(sig,r,angle,round(sz)); | ||
38 | |||
39 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
40 | |||
41 | g = g/sum(sum(abs(g))); | ||
42 | |||
43 | filters(:,:,j,1) = g; | ||
44 | |||
45 | gF = fft2(g,N1+M1-1,N2+M2-1); | ||
46 | IgF = IF.*gF; | ||
47 | Ig = real(ifft2(IgF)); | ||
48 | Ig = Ig(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
49 | |||
50 | %c = conv2(I,g,'valid'); | ||
51 | |||
52 | filter_output(:,:,j,1) = Ig; | ||
53 | end | ||
54 | else | ||
55 | |||
56 | % there are multiple scales | ||
57 | sigs = sig; | ||
58 | szs = sz; | ||
59 | for k = 1:size(sigs,2), | ||
60 | sig = sigs(k); | ||
61 | sz = szs(end); | ||
62 | fprintf('%d',k); | ||
63 | |||
64 | for j = 1:length(as), | ||
65 | fprintf('.'); | ||
66 | angle = as(j); | ||
67 | |||
68 | g = mk_odd_filter(sig,r,angle,round(sz)); | ||
69 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
70 | g = g/sum(sum(abs(g))); | ||
71 | |||
72 | gF = fft2(g,N1+M1-1,N2+M2-1); | ||
73 | IgF = IF.*gF; | ||
74 | Ig = real(ifft2(IgF)); | ||
75 | Ig = Ig(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
76 | |||
77 | %c = conv2(I,g,'valid'); | ||
78 | %c = conv2(I,g,'same'); | ||
79 | |||
80 | filter_output(:,:,j,k) = Ig; | ||
81 | filters(:,:,j,k) = g; | ||
82 | end | ||
83 | end | ||
84 | |||
85 | end | ||
86 | |||
87 | fprintf('\n'); | ||
88 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/dgauss.m b/SD-VBS/common/toolbox/toolbox_basic/filter/dgauss.m new file mode 100755 index 0000000..207e781 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/dgauss.m | |||
@@ -0,0 +1,16 @@ | |||
1 | function dg = dgauss(sig) | ||
2 | % first derivative of N(sig) | ||
3 | % cutoff after 1% of max | ||
4 | |||
5 | i = 1; | ||
6 | max = 0; | ||
7 | dgi = max; | ||
8 | dg = [dgi]; | ||
9 | while dgi >= 0.01*max | ||
10 | dgi = i / (sqrt(2*pi) * sig^3) * exp(-0.5*i^2/sig^2); | ||
11 | dg = [dgi dg -dgi]; | ||
12 | i = i + 1; | ||
13 | if dgi > max | ||
14 | max = dgi; | ||
15 | end | ||
16 | end; \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/dog1.m b/SD-VBS/common/toolbox/toolbox_basic/filter/dog1.m new file mode 100755 index 0000000..e9f64e6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/dog1.m | |||
@@ -0,0 +1,28 @@ | |||
1 | function G=dog1(sig,N); | ||
2 | % G=dog1(sig,N); | ||
3 | |||
4 | % by Serge Belongie | ||
5 | |||
6 | no_pts=N; % no. of points in x,y grid | ||
7 | |||
8 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); | ||
9 | |||
10 | sigi=0.71*sig; | ||
11 | sigo=1.14*sig; | ||
12 | Ci=diag([sigi,sigi]); | ||
13 | Co=diag([sigo,sigo]); | ||
14 | |||
15 | X=[x(:) y(:)]; | ||
16 | |||
17 | Ga=gaussian(X,[0 0]',Ci); | ||
18 | Ga=reshape(Ga,N,N); | ||
19 | Gb=gaussian(X,[0 0]',Co); | ||
20 | Gb=reshape(Gb,N,N); | ||
21 | |||
22 | a=1; | ||
23 | b=-1; | ||
24 | |||
25 | G = a*Ga + b*Gb; | ||
26 | |||
27 | G=G-mean(G(:)); | ||
28 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/dog2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/dog2.m new file mode 100755 index 0000000..8446198 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/dog2.m | |||
@@ -0,0 +1,31 @@ | |||
1 | function G=dog2(sig,N); | ||
2 | % G=dog2(sig,N); | ||
3 | |||
4 | % by Serge Belongie | ||
5 | |||
6 | no_pts=N; % no. of points in x,y grid | ||
7 | |||
8 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); | ||
9 | |||
10 | sigi=0.62*sig; | ||
11 | sigo=1.6*sig; | ||
12 | C=diag([sig,sig]); | ||
13 | Ci=diag([sigi,sigi]); | ||
14 | Co=diag([sigo,sigo]); | ||
15 | |||
16 | X=[x(:) y(:)]; | ||
17 | |||
18 | Ga=gaussian(X,[0 0]',Ci); | ||
19 | Ga=reshape(Ga,N,N); | ||
20 | Gb=gaussian(X,[0 0]',C); | ||
21 | Gb=reshape(Gb,N,N); | ||
22 | Gc=gaussian(X,[0 0]',Co); | ||
23 | Gc=reshape(Gc,N,N); | ||
24 | |||
25 | a=-1; | ||
26 | b=2; | ||
27 | c=-1; | ||
28 | |||
29 | G = a*Ga + b*Gb + c*Gc; | ||
30 | |||
31 | G=G-mean(G(:)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/doog1.m b/SD-VBS/common/toolbox/toolbox_basic/filter/doog1.m new file mode 100755 index 0000000..dd8e87b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/doog1.m | |||
@@ -0,0 +1,32 @@ | |||
1 | function H=doog1(sig,r,th,N); | ||
2 | % H=doog1(sig,r,th,N); | ||
3 | |||
4 | |||
5 | % by Serge Belongie | ||
6 | |||
7 | no_pts=N; % no. of points in x,y grid | ||
8 | |||
9 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); | ||
10 | |||
11 | phi=pi*th/180; | ||
12 | sigy=sig; | ||
13 | sigx=r*sig; | ||
14 | R=[cos(phi) -sin(phi); sin(phi) cos(phi)]; | ||
15 | C=R*diag([sigx,sigy])*R'; | ||
16 | |||
17 | X=[x(:) y(:)]; | ||
18 | |||
19 | Gb=gaussian(X,[0 0]',C); | ||
20 | Gb=reshape(Gb,N,N); | ||
21 | |||
22 | m=R*[0 sig]'; | ||
23 | |||
24 | a=1; | ||
25 | b=-1; | ||
26 | |||
27 | % make odd-symmetric filter | ||
28 | Ga=gaussian(X,m/2,C); | ||
29 | Ga=reshape(Ga,N,N); | ||
30 | Gb=rot90(Ga,2); | ||
31 | H=a*Ga+b*Gb; | ||
32 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/doog2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/doog2.m new file mode 100755 index 0000000..a0511cb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/doog2.m | |||
@@ -0,0 +1,38 @@ | |||
1 | function G=doog2(sig,r,th,N); | ||
2 | % G=doog2(sig,r,th,N); | ||
3 | % Make difference of offset gaussians kernel | ||
4 | % theta is in degrees | ||
5 | % (see Malik & Perona, J. Opt. Soc. Amer., 1990) | ||
6 | % | ||
7 | % Example: | ||
8 | % >> imagesc(doog2(1,12,0,64,1)) | ||
9 | % >> colormap(gray) | ||
10 | |||
11 | % by Serge Belongie | ||
12 | |||
13 | no_pts=N; % no. of points in x,y grid | ||
14 | |||
15 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); | ||
16 | |||
17 | phi=pi*th/180; | ||
18 | sigy=sig; | ||
19 | sigx=r*sig; | ||
20 | R=[cos(phi) -sin(phi); sin(phi) cos(phi)]; | ||
21 | C=R*diag([sigx,sigy])*R'; | ||
22 | |||
23 | X=[x(:) y(:)]; | ||
24 | |||
25 | Gb=gaussian(X,[0 0]',C); | ||
26 | Gb=reshape(Gb,N,N); | ||
27 | |||
28 | m=R*[0 sig]'; | ||
29 | Ga=gaussian(X,m,C); | ||
30 | Ga=reshape(Ga,N,N); | ||
31 | Gc=rot90(Ga,2); | ||
32 | |||
33 | a=-1; | ||
34 | b=2; | ||
35 | c=-1; | ||
36 | |||
37 | G = a*Ga + b*Gb + c*Gc; | ||
38 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/fft_filt.m b/SD-VBS/common/toolbox/toolbox_basic/filter/fft_filt.m new file mode 100755 index 0000000..25fa5f9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/fft_filt.m | |||
@@ -0,0 +1,82 @@ | |||
1 | % script for fft-based filtering | ||
2 | |||
3 | % set up filterbank | ||
4 | make_filterbank | ||
5 | |||
6 | % prepare FFT of image for filtering | ||
7 | [N1,N2]=size(V); | ||
8 | I=zeros(size(V)+[M1-1 M2-1]); | ||
9 | I(1:N1,1:N2)=V; | ||
10 | IF=fft2(I); | ||
11 | FI=zeros(N1,N2,total_num_filt); | ||
12 | |||
13 | % apply filters | ||
14 | for n=1:total_num_filt | ||
15 | disp(n) | ||
16 | f=rot90(FB(:,:,n),2); | ||
17 | fF=fft2(f,N1+M1-1,N2+M2-1); | ||
18 | IfF=IF.*fF; | ||
19 | If=real(ifft2(IfF)); | ||
20 | If=If(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
21 | FI(:,:,n)=If; | ||
22 | % im(If) | ||
23 | % drawnow | ||
24 | end | ||
25 | |||
26 | %%%% end of filtering part; the remainder is for reconstruction & analysis | ||
27 | break | ||
28 | |||
29 | |||
30 | % use pseudoinverse to reconstruct image from filter projections | ||
31 | fbv=reshape(FB,M1*M2,total_num_filt)'; | ||
32 | fbi=pinv(fbv); | ||
33 | |||
34 | % find principal components | ||
35 | T=reshape(FI,N1*N2,total_num_filt)'; | ||
36 | C=T*T'; | ||
37 | [U,S,junk]=svd(C); | ||
38 | s=diag(S); | ||
39 | |||
40 | % synthesize using some eigenvectors | ||
41 | synth=fbi*U; | ||
42 | k=ceil(sqrt(total_num_filt)); | ||
43 | for n=1:total_num_filt | ||
44 | subplot(k,k,n) | ||
45 | im(reshape(synth(:,n),M1,M2)); | ||
46 | title(num2str(s(n))) | ||
47 | drawnow | ||
48 | end | ||
49 | |||
50 | % synthesize at a point by clicking on coordinates | ||
51 | figure(1) | ||
52 | im(V) | ||
53 | [x,y]=ginput(1); | ||
54 | x=round(x); | ||
55 | y=round(y); | ||
56 | u=squeeze(FI(y,x,:)); | ||
57 | synth=fbi*u; | ||
58 | synth=reshape(synth,M1,M2); | ||
59 | figure(2) | ||
60 | subplot(1,2,1) | ||
61 | im(V) | ||
62 | axis([x-M2/2 x+M2/2 y-M1/2 y+M1/2]) | ||
63 | subplot(1,2,2) | ||
64 | im(synth) | ||
65 | title(num2str(max(synth(:)))); | ||
66 | |||
67 | figure(3) | ||
68 | plot(u,'o-') | ||
69 | |||
70 | % show pseudoinverse filterbank | ||
71 | if 0 | ||
72 | k=ceil(sqrt(total_num_filt)); | ||
73 | for n=1:total_num_filt | ||
74 | subplot(k,k,n) | ||
75 | im(reshape(fbi(:,n),M1,M2)); | ||
76 | axis('off') | ||
77 | drawnow | ||
78 | end | ||
79 | end | ||
80 | |||
81 | |||
82 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/fft_filt_2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/fft_filt_2.m new file mode 100755 index 0000000..9c84e96 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/fft_filt_2.m | |||
@@ -0,0 +1,29 @@ | |||
1 | function FI=fft_filt_2(V,FB,sf); | ||
2 | % FI=fft_filt_2(V,FB,sf); | ||
3 | % fft-based filtering | ||
4 | % requires image to be called "V" | ||
5 | % and filters to be in FB | ||
6 | % sf is the subsampling factor | ||
7 | % | ||
8 | % FI is the result | ||
9 | |||
10 | [M1,M2,N3]=size(FB); | ||
11 | % prepare FFT of image for filtering | ||
12 | [N1,N2]=size(V); | ||
13 | I=zeros(size(V)+[M1-1 M2-1]); | ||
14 | I(1:N1,1:N2)=V; | ||
15 | N1s=length(1:sf:N1); | ||
16 | N2s=length(1:sf:N2); | ||
17 | IF=fft2(I); | ||
18 | FI=zeros(N1s,N2s,N3); | ||
19 | |||
20 | % apply filters | ||
21 | for n=1:N3; | ||
22 | f=rot90(FB(:,:,n),2); | ||
23 | fF=fft2(f,N1+M1-1,N2+M2-1); | ||
24 | IfF=IF.*fF; | ||
25 | If=real(ifft2(IfF)); | ||
26 | If=If(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
27 | FI(:,:,n)=If(1:sf:N1,1:sf:N2); | ||
28 | end | ||
29 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/filter_bank_jshi.tar b/SD-VBS/common/toolbox/toolbox_basic/filter/filter_bank_jshi.tar new file mode 100755 index 0000000..b43b49c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/filter_bank_jshi.tar | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/gauss.m b/SD-VBS/common/toolbox/toolbox_basic/filter/gauss.m new file mode 100755 index 0000000..f0403ed --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/gauss.m | |||
@@ -0,0 +1,16 @@ | |||
1 | function dg = gauss(sig) | ||
2 | % first derivative of N(sig) | ||
3 | % cutoff after 1% of max | ||
4 | |||
5 | i = 1; | ||
6 | max = 0; | ||
7 | dgi = max; | ||
8 | dg = [1/ (sqrt(2*pi) * sig) ]; | ||
9 | while dgi >= 0.01*max | ||
10 | dgi = 1/ (sqrt(2*pi) * sig) * exp(-0.5*i^2/sig^2); | ||
11 | dg = [dgi dg dgi]; | ||
12 | i = i + 1; | ||
13 | if dgi > max | ||
14 | max = dgi; | ||
15 | end | ||
16 | end; \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/gaussian.m b/SD-VBS/common/toolbox/toolbox_basic/filter/gaussian.m new file mode 100755 index 0000000..509b129 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/gaussian.m | |||
@@ -0,0 +1,31 @@ | |||
1 | function p=gaussian(x,m,C); | ||
2 | % p=gaussian(x,m,C); | ||
3 | % | ||
4 | % Evaluate the multi-variate density with mean vector m and covariance | ||
5 | % matrix C for the input vector x. | ||
6 | % | ||
7 | % p=gaussian(X,m,C); | ||
8 | % | ||
9 | % Vectorized version: Here X is a matrix of column vectors, and p is | ||
10 | % a vector of probabilities for each vector. | ||
11 | |||
12 | d=length(m); | ||
13 | |||
14 | if size(x,1)~=d | ||
15 | x=x'; | ||
16 | end | ||
17 | N=size(x,2); | ||
18 | |||
19 | detC = det(C); | ||
20 | if rcond(C)<eps | ||
21 | % fprintf(1,'Covariance matrix close to singular. (gaussian.m)\n'); | ||
22 | p = zeros(N,1); | ||
23 | else | ||
24 | m=m(:); | ||
25 | M=m*ones(1,N); | ||
26 | denom=(2*pi)^(d/2)*sqrt(abs(detC)); | ||
27 | mahal=sum(((x-M)'*inv(C)).*(x-M)',2); % Chris Bregler's trick | ||
28 | numer=exp(-0.5*mahal); | ||
29 | p=numer/denom; | ||
30 | end | ||
31 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/get_diff2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/get_diff2.m new file mode 100755 index 0000000..31f3ac5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/get_diff2.m | |||
@@ -0,0 +1,43 @@ | |||
1 | function [diffI,corr,mag,theta] = get_diff2(I,J,w) | ||
2 | % car: I | ||
3 | % background: J | ||
4 | |||
5 | % half window size | ||
6 | if (nargin == 2) | ||
7 | w = 1; | ||
8 | end | ||
9 | |||
10 | [gIx,gIy] = grad2(I,w); | ||
11 | [gJx,gJy] = grad2(J,w); | ||
12 | gx = gIx; | ||
13 | gy = gIy; | ||
14 | |||
15 | % normalize | ||
16 | sI= sqrt(gIx.*gIx+gIy.*gIy); | ||
17 | sJ= sqrt(gJx.*gJx+gJy.*gJy); | ||
18 | gIx = gIx./sI; | ||
19 | gIy = gIy./sI; | ||
20 | gJx = gJx./sJ; | ||
21 | gJy = gJy./sJ; | ||
22 | |||
23 | theta = cart2pol(gIy,gIx); | ||
24 | corr = gIx.*gJx + gIy.*gJy; | ||
25 | |||
26 | %[gx,gy]= grad((I-J),w);mag = sqrt(gx.*gx+gy.*gy); | ||
27 | %mag = sI; | ||
28 | |||
29 | |||
30 | mag = sqrt((cos(theta).^2).*gy.^2 + (sin(theta).^2).*gx.^2 +... | ||
31 | 2*cos(theta).*sin(theta).*gx.*gy); | ||
32 | |||
33 | % want to look at the grad. mag greater than 10, and corr less than 0.9 | ||
34 | threshold = max(3.5,0.1*max(max(mag(5:size(mag,1)-5,5:size(mag,2)-5)))) | ||
35 | diffI = (abs(corr)<0.85).*(mag>threshold); | ||
36 | |||
37 | |||
38 | |||
39 | |||
40 | |||
41 | |||
42 | |||
43 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/get_diff_free.m b/SD-VBS/common/toolbox/toolbox_basic/filter/get_diff_free.m new file mode 100755 index 0000000..f020fab --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/get_diff_free.m | |||
@@ -0,0 +1,8 @@ | |||
1 | function [diff,corr,mag,angle] = get_diff_free(I,J) | ||
2 | |||
3 | [angle,mag,c2,c3] = compute_angle(I); | ||
4 | [angleJ,magJ] = compute_angle(J); | ||
5 | |||
6 | corr = cos(angle).*cos(angleJ) + sin(angle).*sin(angleJ); | ||
7 | threshold = 0.075*max(max(mag(5:size(mag,1)-5,5:size(mag,2)-5))); | ||
8 | diff = (abs(corr)<0.9).*(mag>threshold); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/grad1.m b/SD-VBS/common/toolbox/toolbox_basic/filter/grad1.m new file mode 100755 index 0000000..eca34db --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/grad1.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function [gx,gy] = grad2(I,ratio) | ||
2 | % | ||
3 | % | ||
4 | |||
5 | kern = dgauss(ratio);kern = kern/sum(abs(kern)); | ||
6 | gkern = gauss(ratio);gkern = gkern/sum(abs(kern)); | ||
7 | |||
8 | gx = conv2(I,kern,'same'); | ||
9 | gx = conv2(gx,gkern','same'); | ||
10 | |||
11 | gy = conv2(conv2(I,kern','same'),gkern,'same'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/grad2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/grad2.m new file mode 100755 index 0000000..ea4ec0e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/grad2.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function [gx,gy] = grad2(I,ratio) | ||
2 | % | ||
3 | % | ||
4 | |||
5 | ddgauss = gradient(dgauss(ratio));ddgauss = ddgauss/sum(abs(ddgauss)); | ||
6 | gkern = gauss(ratio); gkern = gkern/sum(abs(gkern)); | ||
7 | |||
8 | gx = conv2(I,ddgauss,'same'); | ||
9 | gx = conv2(gx,gkern','same'); | ||
10 | |||
11 | gy = conv2(conv2(I,ddgauss','same'),gkern,'same'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/m_interp4.m b/SD-VBS/common/toolbox/toolbox_basic/filter/m_interp4.m new file mode 100755 index 0000000..314f140 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/m_interp4.m | |||
@@ -0,0 +1,49 @@ | |||
1 | function [F,mask] = m_interp4(z,s,t) | ||
2 | %INTERP4 2-D bilinear data interpolation. | ||
3 | % ZI = INTERP4(Z,XI,YI) assumes X = 1:N and Y = 1:M, where | ||
4 | % [M,N] = SIZE(Z). | ||
5 | % | ||
6 | % Copyright (c) 1984-93 by The MathWorks, Inc. | ||
7 | % Clay M. Thompson 4-26-91, revised 7-3-91, 3-22-93 by CMT. | ||
8 | % | ||
9 | % modified to | ||
10 | |||
11 | |||
12 | [nrows,ncols] = size(z); | ||
13 | |||
14 | |||
15 | if any(size(z)<[3 3]), error('Z must be at least 3-by-3.'); end | ||
16 | if size(s)~=size(t), error('XI and YI must be the same size.'); end | ||
17 | |||
18 | % Check for out of range values of s and set to 1 | ||
19 | sout = find((s<1)|(s>ncols)); | ||
20 | if length(sout)>0, s(sout) = ones(size(sout)); end | ||
21 | |||
22 | % Check for out of range values of t and set to 1 | ||
23 | tout = find((t<1)|(t>nrows)); | ||
24 | if length(tout)>0, t(tout) = ones(size(tout)); end | ||
25 | |||
26 | % Matrix element indexing | ||
27 | ndx = floor(t)+floor(s-1)*nrows; | ||
28 | |||
29 | % Compute intepolation parameters, check for boundary value. | ||
30 | d = find(s==ncols); | ||
31 | s(:) = (s - floor(s)); | ||
32 | if length(d)>0, s(d) = s(d)+1; ndx(d) = ndx(d)-nrows; end | ||
33 | |||
34 | % Compute intepolation parameters, check for boundary value. | ||
35 | d = find(t==nrows); | ||
36 | t(:) = (t - floor(t)); | ||
37 | if length(d)>0, t(d) = t(d)+1; ndx(d) = ndx(d)-1; end | ||
38 | d = []; | ||
39 | |||
40 | % Now interpolate, reuse u and v to save memory. | ||
41 | F = ( z(ndx).*(1-t) + z(ndx+1).*t ).*(1-s) + ... | ||
42 | ( z(ndx+nrows).*(1-t) + z(ndx+(nrows+1)).*t ).*s; | ||
43 | |||
44 | mask = ones(size(z)); | ||
45 | |||
46 | % Now set out of range values to zeros. | ||
47 | if length(sout)>0, F(sout) = zeros(size(sout));mask(sout)=zeros(size(sout));end | ||
48 | if length(tout)>0, F(tout) = zeros(size(tout));mask(tout)=zeros(size(tout));end | ||
49 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank.m b/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank.m new file mode 100755 index 0000000..2ff15d2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank.m | |||
@@ -0,0 +1,63 @@ | |||
1 | function FB = make_filterbank(num_ori,num_scale,wsz) | ||
2 | % | ||
3 | % F = make_filterbank(num_ori,num_scale,wsz) | ||
4 | % | ||
5 | |||
6 | |||
7 | % definine filterbank | ||
8 | %num_ori=6; | ||
9 | %num_scale=3; | ||
10 | |||
11 | M1=wsz; % size in pixels | ||
12 | M2=M1; | ||
13 | |||
14 | ori_incr=180/num_ori; | ||
15 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
16 | |||
17 | FBdoog1=zeros(M1,M2,num_scale,num_ori); | ||
18 | FBdoog2=zeros(M1,M2,num_scale,num_ori); | ||
19 | FBdog1=zeros(M1,M2,num_scale); | ||
20 | FBdog2=zeros(M1,M2,num_scale); | ||
21 | |||
22 | % elongated filter set | ||
23 | counter = 1; | ||
24 | filter_scale = 1.0; | ||
25 | filter_scale_step = sqrt(2); | ||
26 | |||
27 | for m=1:num_scale | ||
28 | f=dog1(filter_scale,M1); | ||
29 | FBdog1(:,:,m)=f; | ||
30 | f=dog2(filter_scale,M1); | ||
31 | FBdog2(:,:,m)=f; | ||
32 | counter=counter+1; | ||
33 | for n=1:num_ori | ||
34 | % r=12 here is equivalent to Malik's r=3; | ||
35 | f=doog2(filter_scale,6,ori_offset+(n-1)*ori_incr,M1); | ||
36 | FBdoog2(:,:,m,n)=f; | ||
37 | f=doog1(filter_scale,6,ori_offset+(n-1)*ori_incr,M1); | ||
38 | FBdoog1(:,:,m,n)=f; | ||
39 | end | ||
40 | filter_scale = filter_scale * filter_scale_step; | ||
41 | end | ||
42 | |||
43 | FB=cat(3,3*FBdog1,4.15*FBdog2,2*reshape(FBdoog1,M1,M2,num_scale*num_ori),2*reshape(FBdoog2,M1,M2,num_scale*num_ori)); | ||
44 | total_num_filt=size(FB,3); | ||
45 | |||
46 | nb = size(FB,3); | ||
47 | for j=1:nb, | ||
48 | F = FB(:,:,j); | ||
49 | a = sum(sum(abs(F))); | ||
50 | FB(:,:,j) = FB(:,:,j)/a; | ||
51 | end | ||
52 | |||
53 | |||
54 | if 0 | ||
55 | k=ceil(sqrt(total_num_filt)); | ||
56 | for n=1:total_num_filt | ||
57 | subplot(k,k,n) | ||
58 | im(FB(:,:,n)); | ||
59 | axis('off') | ||
60 | drawnow | ||
61 | end | ||
62 | end | ||
63 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank_23.m b/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank_23.m new file mode 100755 index 0000000..f9fcaa9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank_23.m | |||
@@ -0,0 +1,40 @@ | |||
1 | function [FB,M1,M2,N3]=make_filterbank_23; | ||
2 | % multi-scale even and odd filters | ||
3 | |||
4 | M1=31; % size in pixels | ||
5 | M2=M1; | ||
6 | num_ori=6; | ||
7 | num_scales=3; | ||
8 | num_phases=2; | ||
9 | N3=num_ori*num_scales*num_phases; | ||
10 | FB=zeros(M1,M2,N3); | ||
11 | |||
12 | counter=1; | ||
13 | |||
14 | for m=1:num_scales | ||
15 | for n=1:num_ori | ||
16 | [F1,F2]=quadpair(sqrt(2)^m,3,180*(n-1)/num_ori,M1); | ||
17 | FB(:,:,counter)=F1; | ||
18 | counter=counter+1; | ||
19 | FB(:,:,counter)=F2; | ||
20 | counter=counter+1; | ||
21 | end | ||
22 | end | ||
23 | |||
24 | FB=cat(3,FB,dog2(1,M1),dog2(sqrt(2),M1),dog2(2,M1),dog2(2*sqrt(2),M1)); | ||
25 | |||
26 | N3=size(FB,3); | ||
27 | |||
28 | % stuff for visualizing spectra of filters: | ||
29 | if 0 | ||
30 | FBF=zeros(size(FB)); | ||
31 | |||
32 | for n=1:36 | ||
33 | FBF(:,:,n)=abs(fftshift(fft2(FB(:,:,n)))); | ||
34 | end | ||
35 | |||
36 | montage2(FBF) | ||
37 | |||
38 | im(sum(FBF,3)) | ||
39 | |||
40 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank_even.m b/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank_even.m new file mode 100755 index 0000000..8c8d802 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank_even.m | |||
@@ -0,0 +1,40 @@ | |||
1 | function FB = make_filterbank(num_ori,filter_scales,wsz) | ||
2 | % | ||
3 | % F = make_filterbank(num_ori,num_scale,wsz) | ||
4 | % | ||
5 | |||
6 | |||
7 | % definine filterbank | ||
8 | %num_ori=6; | ||
9 | %num_scale=3; | ||
10 | |||
11 | num_scale = length(filter_scales); | ||
12 | |||
13 | M1=wsz; % size in pixels | ||
14 | M2=M1; | ||
15 | |||
16 | ori_incr=180/num_ori; | ||
17 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
18 | |||
19 | FB=zeros(M1,M2,num_ori,num_scale); | ||
20 | |||
21 | % elongated filter set | ||
22 | counter = 1; | ||
23 | |||
24 | for m=1:num_scale | ||
25 | for n=1:num_ori | ||
26 | % r=12 here is equivalent to Malik's r=3; | ||
27 | f=doog2(filter_scales(m),6,ori_offset+(n-1)*ori_incr,M1); | ||
28 | FB(:,:,n,m)=f; | ||
29 | end | ||
30 | end | ||
31 | |||
32 | FB=reshape(FB,M1,M2,num_scale*num_ori); | ||
33 | total_num_filt=size(FB,3); | ||
34 | |||
35 | for j=1:total_num_filt, | ||
36 | F = FB(:,:,j); | ||
37 | a = sum(sum(abs(F))); | ||
38 | FB(:,:,j) = FB(:,:,j)/a; | ||
39 | end | ||
40 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank_odd.m b/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank_odd.m new file mode 100755 index 0000000..8103598 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/make_filterbank_odd.m | |||
@@ -0,0 +1,41 @@ | |||
1 | function FB = make_filterbank(num_ori,filter_scales,wsz) | ||
2 | % | ||
3 | % F = make_filterbank(num_ori,num_scale,wsz) | ||
4 | % | ||
5 | |||
6 | |||
7 | % definine filterbank | ||
8 | %num_ori=6; | ||
9 | %num_scale=3; | ||
10 | |||
11 | num_scale = length(filter_scales); | ||
12 | |||
13 | M1=wsz; % size in pixels | ||
14 | M2=M1; | ||
15 | |||
16 | ori_incr=180/num_ori; | ||
17 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
18 | |||
19 | FB=zeros(M1,M2,num_ori,num_scale); | ||
20 | |||
21 | |||
22 | % elongated filter set | ||
23 | counter = 1; | ||
24 | |||
25 | for m=1:num_scale | ||
26 | for n=1:num_ori | ||
27 | % r=12 here is equivalent to Malik's r=3; | ||
28 | f=doog1(filter_scales(m),6,ori_offset+(n-1)*ori_incr,M1); | ||
29 | FB(:,:,n,m)=f; | ||
30 | end | ||
31 | end | ||
32 | |||
33 | FB=reshape(FB,M1,M2,num_scale*num_ori); | ||
34 | total_num_filt=size(FB,3); | ||
35 | |||
36 | for j=1:total_num_filt, | ||
37 | F = FB(:,:,j); | ||
38 | a = sum(sum(abs(F))); | ||
39 | FB(:,:,j) = FB(:,:,j)/a; | ||
40 | end | ||
41 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/mdoog2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/mdoog2.m new file mode 100755 index 0000000..25bc2f9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/mdoog2.m | |||
@@ -0,0 +1,36 @@ | |||
1 | function G=doog2(sig,r,th,N); | ||
2 | % [G,H]=doog2(sig,r,th,N); | ||
3 | % Make difference of offset gaussians kernel | ||
4 | % theta is in degrees | ||
5 | % (see Malik & Perona, J. Opt. Soc. Amer., 1990) | ||
6 | % | ||
7 | |||
8 | |||
9 | [x,y]=meshgrid(-N:N,-N:N); | ||
10 | |||
11 | a=-1; | ||
12 | b=2; | ||
13 | c=-1; | ||
14 | |||
15 | ya=sig; | ||
16 | yc=-ya; | ||
17 | yb=0; | ||
18 | sigy=sig; | ||
19 | sigx=r*sig; | ||
20 | |||
21 | Ga=(1/(2*pi*sigx*sigy))*exp(-(((x-0)/sigx).^2+((y-ya)/sigy).^2)); | ||
22 | Gb=(1/(2*pi*sigx*sigy))*exp(-(((x-0)/sigx).^2+((y-yb)/sigy).^2)); | ||
23 | Gc=(1/(2*pi*sigx*sigy))*exp(-(((x-0)/sigx).^2+((y-yc)/sigy).^2)); | ||
24 | |||
25 | Go = a*Ga + b*Gb + c*Gc; | ||
26 | %Ho = imag(hilbert(Go)); | ||
27 | G = Go; | ||
28 | |||
29 | G = mimrotate(Go,th,'bilinear','crop'); | ||
30 | |||
31 | G = G-mean(reshape(G,prod(size(G)),1)); | ||
32 | |||
33 | G = G/sum(sum(abs(G))); | ||
34 | |||
35 | |||
36 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/mimrotate.m b/SD-VBS/common/toolbox/toolbox_basic/filter/mimrotate.m new file mode 100755 index 0000000..7dd31a2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/mimrotate.m | |||
@@ -0,0 +1,119 @@ | |||
1 | function bout = imrotate(arg1,arg2,arg3,arg4) | ||
2 | %IMROTATE Rotate image. | ||
3 | % B = IMROTATE(A,ANGLE,'method') rotates the image A by ANGLE | ||
4 | % degrees. The image returned B will, in general, be larger | ||
5 | % than A. Invalid values on the periphery are set to one | ||
6 | % for indexed images or zero for all other image types. Possible | ||
7 | % interpolation methods are 'nearest','bilinear' or 'bicubic'. | ||
8 | % 'bilinear' is the default for intensity images, otherwise | ||
9 | % 'nearest' is used if no method is given. | ||
10 | % | ||
11 | % B = IMROTATE(A,ANGLE,'crop') or IMROTATE(A,ANGLE,'method','crop') | ||
12 | % crops B to be the same size as A. | ||
13 | % | ||
14 | % Without output arguments, IMROTATE(...) displays the rotated | ||
15 | % image in the current axis. | ||
16 | % | ||
17 | % See also IMRESIZE, IMCROP, ROT90. | ||
18 | |||
19 | % Clay M. Thompson 8-4-92 | ||
20 | % Copyright (c) 1992 by The MathWorks, Inc. | ||
21 | % $Revision: 1.14 $ $Date: 1993/09/01 21:27:38 $ | ||
22 | |||
23 | if nargin<2, error('Requires at least two input parameters.'); end | ||
24 | if nargin<3, | ||
25 | if isgray(arg1), caseid = 'bil'; else caseid = 'nea'; end | ||
26 | docrop = 0; | ||
27 | elseif nargin==3, | ||
28 | if isstr(arg3), | ||
29 | method = [lower(arg3),' ']; % Protect against short method | ||
30 | caseid = method(1:3); | ||
31 | if caseid(1)=='c', % Crop string | ||
32 | if isgray(arg1), caseid = 'bil'; else caseid = 'nea'; end | ||
33 | docrop = 1; | ||
34 | else | ||
35 | docrop = 0; | ||
36 | end | ||
37 | else | ||
38 | error('''METHOD'' must be a string of at least three characters.'); | ||
39 | end | ||
40 | else | ||
41 | if isstr(arg3), | ||
42 | method = [lower(arg3),' ']; % Protect against short method | ||
43 | caseid = method(1:3); | ||
44 | else | ||
45 | error('''METHOD'' must be a string of at least three characters.'); | ||
46 | end | ||
47 | docrop = 1; | ||
48 | end | ||
49 | |||
50 | % Catch and speed up 90 degree rotations | ||
51 | if rem(arg2,90)==0 & nargin<4, | ||
52 | phi = rem(arg2,360); | ||
53 | if phi==90, | ||
54 | b = rot90(arg1); | ||
55 | elseif phi==180, | ||
56 | b = rot90(arg1,2); | ||
57 | elseif phi==270, | ||
58 | b = rot90(arg1,-1); | ||
59 | else | ||
60 | b = arg1; | ||
61 | end | ||
62 | if nargout==0, imshow(b), else bout = b; end | ||
63 | return | ||
64 | end | ||
65 | |||
66 | phi = arg2*pi/180; % Convert to radians | ||
67 | |||
68 | % Rotation matrix | ||
69 | T = [cos(phi) -sin(phi); sin(phi) cos(phi)]; | ||
70 | |||
71 | % Coordinates from center of A | ||
72 | [m,n] = size(arg1); | ||
73 | if ~docrop, % Determine limits for rotated image | ||
74 | siz = ceil(max(abs([(n-1)/2 -(m-1)/2;(n-1)/2 (m-1)/2]*T))/2)*2; | ||
75 | uu = -siz(1):siz(1); vv = -siz(2):siz(2); | ||
76 | else % Cropped image | ||
77 | uu = (1:n)-(n+1)/2; vv = (1:m)-(m+1)/2; | ||
78 | end | ||
79 | nu = length(uu); nv = length(vv); | ||
80 | |||
81 | blk = bestblk([nv nu]); | ||
82 | nblks = floor([nv nu]./blk); nrem = [nv nu] - nblks.*blk; | ||
83 | mblocks = nblks(1); nblocks = nblks(2); | ||
84 | mb = blk(1); nb = blk(2); | ||
85 | |||
86 | rows = 1:blk(1); b = zeros(nv,nu); | ||
87 | for i=0:mblocks, | ||
88 | if i==mblocks, rows = (1:nrem(1)); end | ||
89 | for j=0:nblocks, | ||
90 | if j==0, cols = 1:blk(2); elseif j==nblocks, cols=(1:nrem(2)); end | ||
91 | if ~isempty(rows) & ~isempty(cols) | ||
92 | [u,v] = meshgrid(uu(j*nb+cols),vv(i*mb+rows)); | ||
93 | % Rotate points | ||
94 | uv = [u(:) v(:)]*T'; % Rotate points | ||
95 | u(:) = uv(:,1)+(n+1)/2; v(:) = uv(:,2)+(m+1)/2; | ||
96 | if caseid(1)=='n', % Nearest neighbor interpolation | ||
97 | b(i*mb+rows,j*nb+cols) = interp6(arg1,u,v); | ||
98 | elseif all(caseid=='bil'), % Bilinear interpolation | ||
99 | b(i*mb+rows,j*nb+cols) = interp2(arg1,u,v,'linear'); | ||
100 | elseif all(caseid=='bic'), % Bicubic interpolation | ||
101 | b(i*mb+rows,j*nb+cols) = interp5(arg1,u,v); | ||
102 | else | ||
103 | error(['Unknown interpolation method: ',method]); | ||
104 | end | ||
105 | end | ||
106 | end | ||
107 | end | ||
108 | |||
109 | d = find(isnan(b)); | ||
110 | if length(d)>0, | ||
111 | if isind(arg1), b(d) = ones(size(d)); else b(d) = zeros(size(d)); end | ||
112 | end | ||
113 | |||
114 | if nargout==0, | ||
115 | imshow(b), return | ||
116 | end | ||
117 | bout = b; | ||
118 | |||
119 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/mk_odd_filter.m b/SD-VBS/common/toolbox/toolbox_basic/filter/mk_odd_filter.m new file mode 100755 index 0000000..43ec7d7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/mk_odd_filter.m | |||
@@ -0,0 +1,36 @@ | |||
1 | function G=doog2(sig,r,th,N); | ||
2 | % [G,H]=doog2(sig,r,th,N); | ||
3 | % Make difference of offset gaussians kernel | ||
4 | % theta is in degrees | ||
5 | % (see Malik & Perona, J. Opt. Soc. Amer., 1990) | ||
6 | % | ||
7 | |||
8 | |||
9 | [x,y]=meshgrid(-N:N,-N:N); | ||
10 | |||
11 | a=-1; | ||
12 | b=2; | ||
13 | c=-1; | ||
14 | |||
15 | ya=sig; | ||
16 | yc=-ya; | ||
17 | yb=0; | ||
18 | sigy=sig; | ||
19 | sigx=r*sig; | ||
20 | |||
21 | Ga=(1/(2*pi*sigx*sigy))*exp(-(((x-0)/sigx).^2+((y-ya)/sigy).^2)); | ||
22 | Gb=(1/(2*pi*sigx*sigy))*exp(-(((x-0)/sigx).^2+((y-yb)/sigy).^2)); | ||
23 | Gc=(1/(2*pi*sigx*sigy))*exp(-(((x-0)/sigx).^2+((y-yc)/sigy).^2)); | ||
24 | |||
25 | Go = a*Ga + b*Gb + c*Gc; | ||
26 | Ho = imag(hilbert(Go)); | ||
27 | %G = Ho; | ||
28 | |||
29 | G = mimrotate(Ho,th,'bilinear','crop'); | ||
30 | |||
31 | G = G-mean(reshape(G,prod(size(G)),1)); | ||
32 | |||
33 | G = G/sum(sum(abs(G))); | ||
34 | |||
35 | |||
36 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/mkdog1.m b/SD-VBS/common/toolbox/toolbox_basic/filter/mkdog1.m new file mode 100755 index 0000000..f1225cc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/mkdog1.m | |||
@@ -0,0 +1,20 @@ | |||
1 | function dog1 = mkdog1(sigma_base,size_w) | ||
2 | % | ||
3 | % function dog1 = mkdog1(sigma_base,size_w) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | %scale_base = 1; | ||
8 | scale_base = 3; | ||
9 | |||
10 | a = scale_base; | ||
11 | c = -1*scale_base; | ||
12 | |||
13 | sigma_a = 0.71*sigma_base; | ||
14 | sigma_c = 1.14*sigma_base; | ||
15 | |||
16 | dog1 = a*mkg(0,0,sigma_a,sigma_a,size_w) +... | ||
17 | c*mkg(0,0,sigma_c,sigma_c,size_w); | ||
18 | |||
19 | dog1 = dog1-mean(reshape(dog1,prod(size(dog1)),1)); | ||
20 | dog1 = dog1/sum(sum(abs(dog1))); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/mkdog2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/mkdog2.m new file mode 100755 index 0000000..a78824a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/mkdog2.m | |||
@@ -0,0 +1,22 @@ | |||
1 | function dog2 = mkdog2(sigma_base,size_w) | ||
2 | % | ||
3 | % function dog2 = mkdog2(sigma_base,size_w) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | %scale_base = 1.224; | ||
8 | scale_base = 4.15; | ||
9 | |||
10 | a = scale_base; | ||
11 | b = -2*scale_base; | ||
12 | c = scale_base; | ||
13 | |||
14 | sigma_a = 0.62*sigma_base; | ||
15 | sigma_b = sigma_base; | ||
16 | sigma_c = 1.6*sigma_base; | ||
17 | |||
18 | dog2 = a*mkg(0,0,sigma_a,sigma_a,size_w) +... | ||
19 | b*mkg(0,0,sigma_b,sigma_b,size_w) +... | ||
20 | c*mkg(0,0,sigma_c,sigma_c,size_w); | ||
21 | |||
22 | %dog2 = 255*5.1745*dog2; \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/mkdoog2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/mkdoog2.m new file mode 100755 index 0000000..5db2877 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/mkdoog2.m | |||
@@ -0,0 +1,30 @@ | |||
1 | function doog2 = mkdoog2(sigma_w,r,theta,size_w) | ||
2 | % | ||
3 | % function doog2 = mkdoog2(sigma_w,r,theta,size_w) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | %scale_base = 2.8814; | ||
8 | scale_base = 2; | ||
9 | |||
10 | a = -1*scale_base; | ||
11 | b = 2*scale_base; | ||
12 | c = -1*scale_base; | ||
13 | |||
14 | sigma_x = r*sigma_w; | ||
15 | sigma_y = sigma_w; | ||
16 | |||
17 | ya = sigma_w; | ||
18 | yc = -sigma_w; | ||
19 | yb = 0; | ||
20 | |||
21 | doog2 = a*mkg(0,ya,sigma_x,sigma_y,size_w) +... | ||
22 | b*mkg(0,yb,sigma_x,sigma_y,size_w) +... | ||
23 | c*mkg(0,yc,sigma_x,sigma_y,size_w); | ||
24 | |||
25 | %doog2 = 255*5.1745*doog2; | ||
26 | |||
27 | |||
28 | |||
29 | |||
30 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/mkdoogs.m b/SD-VBS/common/toolbox/toolbox_basic/filter/mkdoogs.m new file mode 100755 index 0000000..e5796bc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/mkdoogs.m | |||
@@ -0,0 +1,15 @@ | |||
1 | function [doogs,index] = mkdoogs(sigma_w,r,theta,theta_to,size_w) | ||
2 | % function doogs = mkdoogs(sigma_w,r,theta,theta_to,size_w) | ||
3 | % | ||
4 | |||
5 | doogs = []; | ||
6 | |||
7 | angle_start = theta*pi/180; | ||
8 | angle_end = theta_to*pi/180; | ||
9 | step = pi/180; | ||
10 | |||
11 | index = 1; | ||
12 | for k=angle_start:step:angle_end, | ||
13 | doogs = [doogs,mkdoog2(sigma_w,r,k,size_w)]; | ||
14 | index = index +1; | ||
15 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/mkg.m b/SD-VBS/common/toolbox/toolbox_basic/filter/mkg.m new file mode 100755 index 0000000..1fb1f7e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/mkg.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function g= mkgaussian(xo,yo,sigma_x,sigma_y,size_w) | ||
2 | % | ||
3 | % function G = mkgaussian(xo,yo,sigma_x,sigma_y,size_w) | ||
4 | % | ||
5 | |||
6 | size_wh = round(0.5*size_w); | ||
7 | [x,y] = meshgrid([-size_wh:1:size_wh],[-size_wh:1:size_wh]); | ||
8 | g = 1/(2*pi*sigma_x*sigma_y)*(exp(-( ((x-xo)/sigma_x).^2 + ((y-yo)/sigma_y).^2))); | ||
9 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/quadpair.m b/SD-VBS/common/toolbox/toolbox_basic/filter/quadpair.m new file mode 100755 index 0000000..96c2a22 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/quadpair.m | |||
@@ -0,0 +1,20 @@ | |||
1 | function [F1,F2]=quadpair(sig,lam,th,N); | ||
2 | % [F1,F2]=quadpair(sig,lam,th,N); | ||
3 | % | ||
4 | % For Thomas' ECCV98 filters, use sig=sqrt(2), lam=4. | ||
5 | |||
6 | %N=31; | ||
7 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); | ||
8 | |||
9 | |||
10 | F1=(4*(y.^2)/(sig^4)-2/(sig^2)).*exp(-(y.^2)/(sig^2)-(x.^2)/(lam^2*sig^2)); | ||
11 | F2=imag(hilbert(F1)); | ||
12 | |||
13 | F1=imrotate(F1,th,'bil','crop'); | ||
14 | F2=imrotate(F2,th,'bil','crop'); | ||
15 | |||
16 | F1=F1-mean(F1(:)); | ||
17 | F2=F2-mean(F2(:)); | ||
18 | |||
19 | F1=F1/norm(F1(:),1); | ||
20 | F2=F2/norm(F2(:),1); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/smooth.m b/SD-VBS/common/toolbox/toolbox_basic/filter/smooth.m new file mode 100755 index 0000000..5ef7579 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/smooth.m | |||
@@ -0,0 +1,24 @@ | |||
1 | % smooth an image | ||
2 | % coordinates (r, c) follow matrix convention; | ||
3 | % the gaussian is truncated at x = +- tail, and there are samples samples | ||
4 | % inbetween, where samples = hsamples * 2 + 1 | ||
5 | |||
6 | function g = smooth(image, hsamples) | ||
7 | |||
8 | tail=4; | ||
9 | samples = hsamples * 2 + 1; | ||
10 | |||
11 | x = linspace(-tail, tail, samples); | ||
12 | gauss = exp(-x.^2); | ||
13 | %s = sum(gauss)/length(x);gauss = gauss-s; | ||
14 | gauss = gauss/sum(abs(gauss)); | ||
15 | |||
16 | n = gauss * ones(samples,1); | ||
17 | gauss = gauss/n; | ||
18 | |||
19 | |||
20 | g = conv2(conv2(image, gauss,'same'), gauss','same'); | ||
21 | %g = conv2(conv2(image, gauss,'valid'), gauss','valid'); | ||
22 | |||
23 | |||
24 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/softkmean.m b/SD-VBS/common/toolbox/toolbox_basic/filter/softkmean.m new file mode 100755 index 0000000..b9b4feb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/softkmean.m | |||
@@ -0,0 +1,56 @@ | |||
1 | function [cluster,var,mix,membership,lG] = softkmeans(data,k,cluster0) | ||
2 | |||
3 | [n,D] = size(data); | ||
4 | var = 1.0; | ||
5 | var0 = ones(k,1)*var; minvar = 0.0001; | ||
6 | mix0 = ones(k,1)/k; minmix = 0.0001; | ||
7 | |||
8 | k = size(var0,1); | ||
9 | [n,D] = size(data); | ||
10 | |||
11 | lGG = []; | ||
12 | ma = -1e20; | ||
13 | in = 0; | ||
14 | |||
15 | if (nargin == 2), | ||
16 | max_data = max(data); | ||
17 | min_data = min(data); | ||
18 | %step = (max_data-min_data)/(k+1); | ||
19 | %cluster0 = [1:k]'*step+min_data; | ||
20 | mag = ones(k,1)*(max_data-min_data); | ||
21 | base = ones(k,1)*min_data; | ||
22 | cluster0 = rand(k,D).*mag + base; | ||
23 | end | ||
24 | |||
25 | %cluster0 | ||
26 | for t = 1:3, | ||
27 | %rndindx = round(rand(1,k)*(n-3))+2; | ||
28 | %cluster0 = (data(rndindx,:)+data(rndindx+1,:)+data(rndindx-1,:))/2; | ||
29 | [cluster,var,mix,membership,lG] = softmeans(cluster0,var0,minvar,mix0,minmix,data); | ||
30 | eval(sprintf('mix_var_cluster_%d = [mix,var,cluster];',t)); | ||
31 | eval(sprintf('lG_%d = lG;',t)); | ||
32 | if ma<lG(size(lG,2)), | ||
33 | ma = lG(size(lG,2)); | ||
34 | in = t; | ||
35 | end | ||
36 | |||
37 | end | ||
38 | |||
39 | eval(sprintf('mix_var_cluster = mix_var_cluster_%d;',in)); | ||
40 | eval(sprintf('lG = lG_%d;',in)); | ||
41 | mix = mix_var_cluster(:,1); | ||
42 | var = mix_var_cluster(:,2); | ||
43 | cluster = mix_var_cluster(:,3:size(mix_var_cluster,2)) | ||
44 | |||
45 | |||
46 | [tmp,vecs2cluster] = max(membership'); | ||
47 | vecs2cluster = vecs2cluster'; | ||
48 | cluster_iCV = inv(var); | ||
49 | |||
50 | |||
51 | |||
52 | |||
53 | |||
54 | |||
55 | |||
56 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/softmeans.m b/SD-VBS/common/toolbox/toolbox_basic/filter/softmeans.m new file mode 100755 index 0000000..b7e5068 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/softmeans.m | |||
@@ -0,0 +1,46 @@ | |||
1 | function [cluster,var,mix,membership,lG] = softmeans(cluster0,var0,minvar,mix0,minmix,data) | ||
2 | |||
3 | [k,D] = size(cluster0); | ||
4 | [n,D] = size(data); | ||
5 | cluster_p = cluster0; var_p = var0; mix_p = mix0; | ||
6 | old_lg = -inf; lG = []; | ||
7 | |||
8 | for iter = 1:30, % max.iterations | ||
9 | % E-Step + comp. incomplete likelihood | ||
10 | |||
11 | H = zeros(n,k); | ||
12 | for j = 1:k, | ||
13 | Hj = (data-(ones(n,1)*cluster_p(j,:))).^2; | ||
14 | if D > 1, Hj = sum(Hj')'; end | ||
15 | H(:,j) = exp(Hj /(-2*var_p(j)))/(sqrt(var_p(j))^D); | ||
16 | end | ||
17 | H = H.*(ones(n,1)*mix_p'); | ||
18 | new_lg = sum(log(sum(H')/(sqrt(2*pi)^D))); | ||
19 | lG = [lG, new_lg]; | ||
20 | if new_lg == old_lg, break; end; old_lg = new_lg; | ||
21 | H = H./(sum(H')'*ones(1,k)); % normalize | ||
22 | |||
23 | % M-Step: | ||
24 | |||
25 | if minmix > 0, | ||
26 | mix_p = sum(H); mix_p = mix_p/sum(mix_p); mix_p = mix_p'; | ||
27 | for j = 1:k, if mix_p(j)<minmix, mix_p(j) = minmix; end; end; | ||
28 | end | ||
29 | cluster_p = (H./(ones(n,1)*sum(H)))'*data; | ||
30 | if minvar > 0, | ||
31 | for j = 1:k, | ||
32 | varj = (data-(ones(n,1)*cluster_p(j,:))).^2; | ||
33 | if D > 1, varj = sum(varj')'; end | ||
34 | var_p(j) = sum(H(:,j).*varj)/(D*sum(H(:,j))); | ||
35 | if var_p(j)<minvar, var_p(j) = minvar; end; | ||
36 | end; | ||
37 | end | ||
38 | |||
39 | % cluster_p = cluster_p./(sqrt(sum(cluster_p'.^2)')*ones(1,D)); | ||
40 | end | ||
41 | |||
42 | cluster = cluster_p; | ||
43 | var = var_p; | ||
44 | mix = mix_p; | ||
45 | membership = H; | ||
46 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter/softmeans2.m b/SD-VBS/common/toolbox/toolbox_basic/filter/softmeans2.m new file mode 100755 index 0000000..8fd194e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter/softmeans2.m | |||
@@ -0,0 +1,39 @@ | |||
1 | function [cluster,var,mix,membership,lG] = softmeans2(var0,minvar,mix0,minmix,data,cluster0,iter) | ||
2 | |||
3 | if (~exist('iter')), | ||
4 | iter = 3; | ||
5 | end | ||
6 | |||
7 | k = size(var0,1); | ||
8 | [n,D] = size(data); | ||
9 | |||
10 | lGG = []; | ||
11 | ma = -1e20; | ||
12 | in = 0; | ||
13 | |||
14 | if (nargin == 5), | ||
15 | max_data = max(data); | ||
16 | min_data = min(data); | ||
17 | step = (max_data-min_data)/(k+1); | ||
18 | cluster0 = [1:k]'*step+min_data; | ||
19 | end | ||
20 | cluster0 | ||
21 | for t = 1:iter, | ||
22 | %rndindx = round(rand(1,k)*(n-3))+2; | ||
23 | %cluster0 = (data(rndindx,:)+data(rndindx+1,:)+data(rndindx-1,:))/2; | ||
24 | [cluster,var,mix,membership,lG] = softmeans(cluster0,var0,minvar,mix0,minmix,data); | ||
25 | eval(sprintf('mix_var_cluster_%d = [mix,var,cluster];',t)); | ||
26 | eval(sprintf('lG_%d = lG;',t)); | ||
27 | if ma<lG(size(lG,2)), | ||
28 | ma = lG(size(lG,2)); | ||
29 | in = t; | ||
30 | end | ||
31 | |||
32 | end | ||
33 | |||
34 | eval(sprintf('mix_var_cluster = mix_var_cluster_%d;',in)); | ||
35 | eval(sprintf('lG = lG_%d;',in)); | ||
36 | mix = mix_var_cluster(:,1); | ||
37 | var = mix_var_cluster(:,2); | ||
38 | cluster = mix_var_cluster(:,3:size(mix_var_cluster,2)) | ||
39 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filterQuad.zip b/SD-VBS/common/toolbox/toolbox_basic/filterQuad.zip new file mode 100755 index 0000000..00e6141 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filterQuad.zip | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/1d_cut.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/1d_cut.m new file mode 100755 index 0000000..46f865b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/1d_cut.m | |||
@@ -0,0 +1,16 @@ | |||
1 | function [x,map] = idcut(data,cmap,nbin) | ||
2 | % | ||
3 | % | ||
4 | % | ||
5 | |||
6 | lc = size(cmap,1); | ||
7 | |||
8 | data = data - min(data); | ||
9 | data = 1+ ((lc-1)*data/max(data)); | ||
10 | |||
11 | r = cmap(data,1); | ||
12 | g = cmap(data,2); | ||
13 | b = cmap(data,3); | ||
14 | |||
15 | [x,map] = vmquant(r,g,b,nbin); | ||
16 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/Bfilter.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/Bfilter.m new file mode 100755 index 0000000..ee086f0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/Bfilter.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function output = Bfilter(img,H) | ||
2 | % | ||
3 | % function output = Bfilter(img,H) | ||
4 | % | ||
5 | |||
6 | sze = size(img); | ||
7 | |||
8 | Y = fft(reshape(img,1,sze(1)*sze(2))); | ||
9 | C = Y.*conj(H); | ||
10 | c = real(ifft(C)); | ||
11 | output = reshape(c,sze(1),sze(2)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/BfilterS.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/BfilterS.m new file mode 100755 index 0000000..4eb4fd6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/BfilterS.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function output = Bfilters(img,H,w) | ||
2 | % | ||
3 | % function output = Bfilter(img,H,w) | ||
4 | % | ||
5 | |||
6 | sze = size(img); | ||
7 | w_h = round(0.5*(w-1)); | ||
8 | |||
9 | Y = fft(reshape(img,1,sze(1)*sze(2))); | ||
10 | C = Y.*conj(H); | ||
11 | c = real(ifft(C)); | ||
12 | o = reshape(c,sze(1),sze(2)); | ||
13 | |||
14 | output(1:w_h(1),1:w_h(2)) = o(sze(1)-w_h(1)+1:sze(1),sze(2)-w_h(2)+1:sze(2)); | ||
15 | output(1:w_h(1),w_h(2)+1:sze(2)) = o(sze(1)-w_h(1)+1:sze(1),1:sze(2)-w_h(2)); | ||
16 | output(w_h(1)+1:sze(1),w_h(2)+1:sze(2)) = o(1:sze(1)-w_h(1),1:sze(2)-w_h(2)); | ||
17 | output(w_h(1)+1:sze(1),1:w_h(2)) = o(1:sze(1)-w_h(1),sze(2)-w_h(2)+1:sze(2)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/Ncut.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/Ncut.m new file mode 100755 index 0000000..30c9b33 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/Ncut.m | |||
@@ -0,0 +1,14 @@ | |||
1 | function [v,d] = ncut(A,nv) | ||
2 | |||
3 | ds = sum(A); | ||
4 | ds = ones(size(ds))./sqrt(ds); | ||
5 | |||
6 | D1 = ds'*ones(1,length(ds)); | ||
7 | A = D1'.*A.*D1; | ||
8 | |||
9 | disp(sprintf('computing eig values')); | ||
10 | tic;[v,d] = eigs(A,nv);toc; | ||
11 | |||
12 | d = abs(diag(d)); | ||
13 | |||
14 | v = D1(:,1:size(v,2)).*v; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/apply_image.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/apply_image.m new file mode 100755 index 0000000..0791aa4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/apply_image.m | |||
@@ -0,0 +1,38 @@ | |||
1 | function [aout1,aout2,aout3,aout4] = apply_image(gx,gy,wc) | ||
2 | % | ||
3 | % aout = apply_image(gx,gy,wc) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | [nr,nc] =size(gx); | ||
8 | |||
9 | w = 2*wc+1; | ||
10 | |||
11 | aout1 = ones(nr,nc); | ||
12 | aout2 = zeros(nr,nc); | ||
13 | aout3 = aout2; | ||
14 | aout4 = aout2; | ||
15 | |||
16 | %mask = smooth(ones(w,w),w); | ||
17 | %sig = w; | ||
18 | %[x,y] = meshgrid(-wc:wc,-wc:wc); | ||
19 | %mask = exp(-(x.*x)/sig).*exp(-(y.*y)/sig); | ||
20 | %mask = mask/sum(sum(mask)); | ||
21 | |||
22 | |||
23 | tmp = ones(w,w); | ||
24 | for j=wc+1:w:nr-wc-1, | ||
25 | for k=wc+1:w:nc-wc-1, | ||
26 | tgx = get_win(gx,[k,j],[wc,wc]); | ||
27 | tgy = get_win(gy,[k,j],[wc,wc]); | ||
28 | %mag = sum(sum(sqrt((mask.*tgx).^2+(mask.*tgy).^2))); | ||
29 | mag = sum(sum(sqrt(tgx.^2 + tgy.^2)))/prod(size(tgy)); | ||
30 | |||
31 | M = is_step(tgx,tgy); | ||
32 | |||
33 | aout1(j-wc:j+wc,k-wc:k+wc) = M(1)*tmp; | ||
34 | aout2(j-wc:j+wc,k-wc:k+wc) = M(2)*tmp; | ||
35 | aout3(j-wc:j+wc,k-wc:k+wc) = M(3)*tmp; | ||
36 | aout4(j-wc:j+wc,k-wc:k+wc) = mag*tmp; | ||
37 | end | ||
38 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/back_proj.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/back_proj.m new file mode 100755 index 0000000..47ac865 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/back_proj.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function BI = back_proj(PFt,vec) | ||
2 | |||
3 | BI = []; | ||
4 | |||
5 | sz1 = sqrt(size(PFt,1)); | ||
6 | |||
7 | for j=1:size(vec,2) | ||
8 | tmp = PFt*vec(:,j); | ||
9 | BI(:,:,j) = reshape(tmp,sz1,sz1); | ||
10 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/backproj_outer.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/backproj_outer.m new file mode 100755 index 0000000..b4560c5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/backproj_outer.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function v = backproj_outer(fvs,u,hb) | ||
2 | % | ||
3 | % given the eigenvecs of the hist.bin. features | ||
4 | % computes the back projection on the eigenvects | ||
5 | % | ||
6 | |||
7 | [nv,np] = size(fvs); | ||
8 | |||
9 | for j=1: \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/backproj_outer_chank.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/backproj_outer_chank.m new file mode 100755 index 0000000..5e3eac9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/backproj_outer_chank.m | |||
@@ -0,0 +1,33 @@ | |||
1 | function v = backproj_outer_chank(fvs,u,d,chank_size) | ||
2 | % | ||
3 | % given the eigenvecs of the hist.bin. features | ||
4 | % computes the back projection on the eigenvects | ||
5 | % | ||
6 | |||
7 | [nv,np] = size(fvs); | ||
8 | [nbins,nv] = size(u); | ||
9 | |||
10 | n_chanks = ceil(np/chank_size); | ||
11 | |||
12 | v = ones(np,nv); | ||
13 | |||
14 | for j=1:n_chanks, | ||
15 | fprintf('<'); | ||
16 | |||
17 | cm = sprintf('load st_%d',j); | ||
18 | eval(cm); | ||
19 | fprintf(sprintf('%d',n_chanks-j)); | ||
20 | |||
21 | v((j-1)*chank_size+1:min(np,j*chank_size),:) = fh'*u; | ||
22 | fprintf('>'); | ||
23 | end | ||
24 | |||
25 | fprintf('\n'); | ||
26 | |||
27 | s = 1./sqrt(d); | ||
28 | |||
29 | for j=1:nv, | ||
30 | v(:,j) = v(:,j)*s(j); | ||
31 | end | ||
32 | |||
33 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/backproj_outer_chank2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/backproj_outer_chank2.m new file mode 100755 index 0000000..084c150 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/backproj_outer_chank2.m | |||
@@ -0,0 +1,36 @@ | |||
1 | function v = backproj_outer_chank(fvs,u,d,chank_size) | ||
2 | % | ||
3 | % given the eigenvecs of the hist.bin. features | ||
4 | % computes the back projection on the eigenvects | ||
5 | % | ||
6 | |||
7 | [nv,np] = size(fvs); | ||
8 | [nbins,nv] = size(u); | ||
9 | |||
10 | n_chanks = ceil(np/chank_size); | ||
11 | |||
12 | v = ones(np,nv); | ||
13 | |||
14 | for j=1:n_chanks, | ||
15 | fprintf('<'); | ||
16 | |||
17 | cm = sprintf('load st_%d',j); | ||
18 | eval(cm); | ||
19 | fprintf(sprintf('%d',n_chanks-j)); | ||
20 | |||
21 | ms = mean(fh'); | ||
22 | fh = fh - ms'*ones(1,size(fh,2)); | ||
23 | |||
24 | v((j-1)*chank_size+1:min(np,j*chank_size),:) = fh'*u; | ||
25 | fprintf('>'); | ||
26 | end | ||
27 | |||
28 | fprintf('\n'); | ||
29 | |||
30 | s = 1./sqrt(d); | ||
31 | |||
32 | for j=1:nv, | ||
33 | v(:,j) = v(:,j)*s(j); | ||
34 | end | ||
35 | |||
36 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/binize.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/binize.m new file mode 100755 index 0000000..d166cd5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/binize.m | |||
@@ -0,0 +1,15 @@ | |||
1 | function [binv,bins] = binize(data,sig,bin_min,bin_max,num_bin) | ||
2 | % | ||
3 | % given an input data, and sigma which describes the uncertainty | ||
4 | % of the data, along with information on the bins, | ||
5 | % return the soft-hist on data | ||
6 | % | ||
7 | |||
8 | ndata = length(data); | ||
9 | |||
10 | bins = linspace(bin_min,bin_max,num_bin+1); | ||
11 | binv = zeros(num_bin,ndata); | ||
12 | |||
13 | for j=1:num_bin, | ||
14 | binv(j,:) = erf((bins(j+1)-data)/sig) - erf((bins(j)-data)/sig); | ||
15 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/binize_old.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/binize_old.m new file mode 100755 index 0000000..d56d263 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/binize_old.m | |||
@@ -0,0 +1,34 @@ | |||
1 | function [binv,bins] = binize(data,sig,bin_min,bin_max,num_bin) | ||
2 | % | ||
3 | % given an input data, and sigma which describes the uncertainty | ||
4 | % of the data, along with information on the bins, | ||
5 | % return the soft-hist on data | ||
6 | % | ||
7 | |||
8 | ndata = length(data); | ||
9 | |||
10 | if 0, | ||
11 | bins = linspace(bin_min,bin_max,num_bin); | ||
12 | binv = zeros(num_bin,ndata); | ||
13 | |||
14 | Largev = 1000; | ||
15 | |||
16 | bins = [-Largev,bins]; | ||
17 | |||
18 | for j=1:num_bin, | ||
19 | binv(j,:) = erf((bins(j+1)-data)/sig) - erf((bins(j)-data)/sig); | ||
20 | end | ||
21 | |||
22 | binv(num_bin,:) = binv(num_bin,:) + erf((Largev-data)/sig) - erf((bins(end)-data)/sig); | ||
23 | bins = bins(2:end); | ||
24 | else | ||
25 | |||
26 | bins = linspace(bin_min,bin_max,num_bin+1); | ||
27 | binv = zeros(num_bin,ndata); | ||
28 | |||
29 | |||
30 | for j=1:num_bin, | ||
31 | binv(j,:) = erf((bins(j+1)-data)/sig) - erf((bins(j)-data)/sig); | ||
32 | end | ||
33 | |||
34 | end \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/binomialfield.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/binomialfield.m new file mode 100755 index 0000000..d83d96d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/binomialfield.m | |||
@@ -0,0 +1,75 @@ | |||
1 | function [x,y,success] = BinomialField(n,sx,sy,ir,numtri); | ||
2 | %BF_HardCore Generates a hard core binomial field | ||
3 | % [x,y,success] = BinomialField(n,sx,sy,ir); | ||
4 | % n : # points (default 100) | ||
5 | % sx : size in x (default 100) | ||
6 | % sy : size in y (default 100) | ||
7 | % ir : inhibition radius (default 0) | ||
8 | % numtri : number of trials (default 200) | ||
9 | % x : x coordinates | ||
10 | % y : y coordinates | ||
11 | % success: whether success or not, useful when producing hard core model | ||
12 | |||
13 | %% | ||
14 | %% (C) Thomas K. Leung | ||
15 | %% University of California at Berkeley | ||
16 | %% April 26, 1995. | ||
17 | %% leungt@cajal.cs.berkeley.edu | ||
18 | %% | ||
19 | |||
20 | %% | ||
21 | %% Generate n points first and then reject those closer to the | ||
22 | %% previous points than ir | ||
23 | %% | ||
24 | |||
25 | if nargin < 1 | ||
26 | n = 100; | ||
27 | sx = 100; | ||
28 | sy = 100; | ||
29 | ir = 0; | ||
30 | numtri = 200; | ||
31 | elseif (nargin == 1 | nargin == 2) | ||
32 | sx = 100; | ||
33 | sy = 100; | ||
34 | ir = 0; | ||
35 | numtri = 200; | ||
36 | elseif (nargin == 3) | ||
37 | ir = 0; | ||
38 | numtri = 200; | ||
39 | elseif (nargin == 4) | ||
40 | numtri = 200; | ||
41 | end | ||
42 | |||
43 | x = zeros(1,n); | ||
44 | y = zeros(1,n); | ||
45 | |||
46 | rand('seed',sum(100*clock)); | ||
47 | x(1) = rand(1) * sx; | ||
48 | y(1) = rand(1) * sy; | ||
49 | |||
50 | success = 1; | ||
51 | |||
52 | I = 2; | ||
53 | trial = 0; | ||
54 | while (I <= n & trial < numtri) | ||
55 | found = 0; | ||
56 | trial = 0; | ||
57 | while (~found & trial < numtri); | ||
58 | tx = rand(1) * sx; | ||
59 | ty = rand(1) * sy; | ||
60 | D = (x(1:(I-1)) - tx).^2 + (y(1:(I-1)) - ty).^2; | ||
61 | if sum(D > (ir^2)) == (I-1) | ||
62 | found = 1; | ||
63 | x(I) = tx; | ||
64 | y(I) = ty; | ||
65 | end | ||
66 | trial = trial + 1; | ||
67 | end | ||
68 | I = I + 1; | ||
69 | end | ||
70 | |||
71 | if trial >= numtri | ||
72 | fprintf(1,'Failed to generate a point in %d trials\n',numtri); | ||
73 | success = 0; | ||
74 | end | ||
75 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize.m new file mode 100755 index 0000000..b03616f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function t1a = colize(t1,I1); | ||
2 | |||
3 | t1a = t1; | ||
4 | |||
5 | t1a = reshape(t1a,size(t1,1)*size(t1,2),1,size(t1,3)); | ||
6 | t1a = squeeze(t1a); | ||
7 | t1a = t1a'; | ||
8 | |||
9 | %I1a = 2*I1(:)';I1a = I1a-mean(I1a(:));t1a = [I1a;t1a]; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_hist.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_hist.m new file mode 100755 index 0000000..9c7b68e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_hist.m | |||
@@ -0,0 +1,29 @@ | |||
1 | function fh = colize_hist(fv,hb) | ||
2 | % (hb = sigs,bin_mins,bin_maxs,nbins) | ||
3 | % | ||
4 | % fv = [nfeature x npoints]; | ||
5 | % fh = [nfeatures*nbins x npoints]; | ||
6 | % | ||
7 | % take a feature matrix, and turn it into histogram bin feature matrix | ||
8 | % | ||
9 | % | ||
10 | |||
11 | [nf,np] = size(fv); | ||
12 | |||
13 | nbins = [0,hb.nbins]; | ||
14 | disp(sprintf('need matrix of %d x %d ',sum(nbins),np)); | ||
15 | |||
16 | fh = zeros(sum(nbins),np); | ||
17 | |||
18 | for k=1:nf, | ||
19 | bin_min = hb.bmins(k); | ||
20 | bin_max = hb.bmaxs(k); | ||
21 | nbin = nbins(k+1); | ||
22 | sig = hb.sigs(k); | ||
23 | fprintf('.'); | ||
24 | b = binize(fv(k,:),sig,bin_min,bin_max,nbin); | ||
25 | fh(sum(nbins(1:k))+1:sum(nbins(1:k+1)),:) = b; | ||
26 | |||
27 | end | ||
28 | |||
29 | fprintf('\n'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_histnb_s.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_histnb_s.m new file mode 100755 index 0000000..61c81ca --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_histnb_s.m | |||
@@ -0,0 +1,47 @@ | |||
1 | function fhs = colize_histnb_s(fh,Is,nw,hw) | ||
2 | % | ||
3 | % fhs = colize_histneigh(fh,fvs,nw) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | [tnbins,np] = size(fh); | ||
8 | |||
9 | [nr,nc] = size(Is); | ||
10 | |||
11 | st_sz = 2*hw + 1; | ||
12 | |||
13 | nr_chank = floor(nr/st_sz); | ||
14 | nc_chank = floor(nc/st_sz); | ||
15 | |||
16 | fhs = zeros(size(fh,1),nr_chank*nc_chank); | ||
17 | |||
18 | idx = 0; | ||
19 | for k=1+hw:st_sz:nc-hw, | ||
20 | |||
21 | fprintf('.'); | ||
22 | sk = max(1,k-nw); | ||
23 | ek = min(nc,k+nw); | ||
24 | |||
25 | |||
26 | % for each column, | ||
27 | for j=1+hw:st_sz:nr-hw, | ||
28 | sj = max(1,j-nw); | ||
29 | ej = min(nr,j+nw); | ||
30 | |||
31 | id = j+(k-1)*nr; | ||
32 | idx = idx+1; | ||
33 | for li=sj:ej, | ||
34 | for lj=sk:ek, | ||
35 | idn = li+(lj-1)*nr; | ||
36 | |||
37 | fhs(:,idx) = fhs(:,idx) + fh(:,idn); | ||
38 | |||
39 | end | ||
40 | end | ||
41 | end | ||
42 | end | ||
43 | |||
44 | fprintf('\n'); | ||
45 | |||
46 | |||
47 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_histnb_sf.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_histnb_sf.m new file mode 100755 index 0000000..d0d60f9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_histnb_sf.m | |||
@@ -0,0 +1,52 @@ | |||
1 | function fhs = colize_histnb_s(fvs,Is,hb,nw,hw) | ||
2 | % | ||
3 | % fhs = colize_histneigh(fvs,Is,hb,nw,hw) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | [nf,np] = size(fvs); | ||
8 | |||
9 | [nr,nc] = size(Is); | ||
10 | |||
11 | st_sz = 2*hw + 1; | ||
12 | |||
13 | nr_chank = floor(nr/st_sz); | ||
14 | nc_chank = floor(nc/st_sz); | ||
15 | |||
16 | tnbins = prod(hb.nbins(1:nf)); | ||
17 | disp(sprintf('allocat memory for %d x %d',tnbins,nr_chank*nc_chank)); | ||
18 | |||
19 | fhs = zeros(tnbins,nr_chank*nc_chank); | ||
20 | |||
21 | idx = 0; | ||
22 | for k=1+hw:st_sz:nc-hw, | ||
23 | |||
24 | fprintf(','); | ||
25 | sk = max(1,k-nw); | ||
26 | ek = min(nc,k+nw); | ||
27 | |||
28 | |||
29 | % for each column, | ||
30 | for j=1+hw:st_sz:nr-hw, | ||
31 | sj = max(1,j-nw); | ||
32 | ej = min(nr,j+nw); | ||
33 | |||
34 | id = j+(k-1)*nr; | ||
35 | idx = idx+1; | ||
36 | |||
37 | %% find idx for the neighboring points | ||
38 | lis = [sj:ej]'*ones(1,ek-sk+1); | ||
39 | ljs = ones(ej-sj+1,1)*[sk:ek]; | ||
40 | idns = lis+(ljs-1)*nr; | ||
41 | |||
42 | fh = colize_joint_hist(fvs(:,idns(:)),hb); | ||
43 | |||
44 | fhs(:,idx) = sum(fh')'; | ||
45 | |||
46 | end | ||
47 | end | ||
48 | |||
49 | fprintf('\n'); | ||
50 | |||
51 | |||
52 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_histneighb.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_histneighb.m new file mode 100755 index 0000000..6189cab --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_histneighb.m | |||
@@ -0,0 +1,37 @@ | |||
1 | function fhs = colize_histneigh(fh,Is,nw) | ||
2 | % | ||
3 | % fhs = colize_histneigh(fh,fvs,nw) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | [tnbins,np] = size(fh); | ||
8 | |||
9 | [nr,nc] = size(Is); | ||
10 | |||
11 | fhs = zeros(size(fh)); | ||
12 | |||
13 | for j=1:nr, | ||
14 | fprintf('.'); | ||
15 | sj = max(1,j-nw); | ||
16 | ej = min(nr,j+nw); | ||
17 | |||
18 | % for each column, | ||
19 | for k=1:nc, | ||
20 | sk = max(1,k-nw); | ||
21 | ek = min(nc,k+nw); | ||
22 | |||
23 | id = j+(k-1)*nr; | ||
24 | |||
25 | for li=sj:ej, | ||
26 | for lj=sk:ek, | ||
27 | idn = li+(lj-1)*nr; | ||
28 | |||
29 | fhs(:,id) = fhs(:,id) + fh(:,idn); | ||
30 | end | ||
31 | end | ||
32 | end | ||
33 | end | ||
34 | fprintf('\n'); | ||
35 | |||
36 | |||
37 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_joint_hist.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_joint_hist.m new file mode 100755 index 0000000..e7844d8 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_joint_hist.m | |||
@@ -0,0 +1,41 @@ | |||
1 | function fh = colize_joint_hist(fv,hb) | ||
2 | % (hb = sigs,bin_mins,bin_maxs,nbins) | ||
3 | % | ||
4 | % take which histogram value and turn it into histogram bin | ||
5 | % | ||
6 | |||
7 | |||
8 | [nf,np] = size(fv); | ||
9 | |||
10 | nbins = [0,hb.nbins]; | ||
11 | %disp(sprintf('need matrix of %d x %d ',prod(hb.nbins),np)); | ||
12 | |||
13 | fh = zeros(hb.nbins(1),hb.nbins(2),np); | ||
14 | |||
15 | k=1; | ||
16 | bin_min = hb.bmins(k); | ||
17 | bin_max = hb.bmaxs(k); | ||
18 | nbin = nbins(k+1); | ||
19 | sig = hb.sigs(k); | ||
20 | %fprintf('.'); | ||
21 | |||
22 | b1 = binize(fv(k,:),sig,bin_min,bin_max,nbin); | ||
23 | k=2; | ||
24 | bin_min = hb.bmins(k); | ||
25 | bin_max = hb.bmaxs(k); | ||
26 | nbin = nbins(k+1); | ||
27 | sig = hb.sigs(k); | ||
28 | %fprintf('.'); | ||
29 | |||
30 | b2 = binize(fv(k,:),sig,bin_min,bin_max,nbin); | ||
31 | |||
32 | |||
33 | for k=1:hb.nbins(1), | ||
34 | for j=1:hb.nbins(2), | ||
35 | fh(k,j,:) = b1(k,:).*b2(j,:); | ||
36 | end | ||
37 | end | ||
38 | |||
39 | %fprintf('\n'); | ||
40 | |||
41 | fh = reshape(fh,hb.nbins(1)*hb.nbins(2),np); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_test.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_test.m new file mode 100755 index 0000000..a9135cc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/colize_test.m | |||
@@ -0,0 +1,19 @@ | |||
1 | function t1a = colize(t1,I1); | ||
2 | |||
3 | if 1, | ||
4 | t1a = t1; | ||
5 | %t1a = 1.2*half_sigmoid(t1,0.3,0.1);; | ||
6 | t1a = reshape(t1a,size(t1,1)*size(t1,2),1,size(t1,3)); | ||
7 | t1a = squeeze(t1a); | ||
8 | t1a = t1a'; | ||
9 | |||
10 | %I1a = I1(:)';I1a = I1a-mean(I1a(:));t1a = [I1a;t1a]; | ||
11 | |||
12 | else | ||
13 | mask = t1>=0; | ||
14 | t1a = abs(t1); | ||
15 | t1a = 0.5-t1a; | ||
16 | t1a = reshape(t1a,size(t1,1)*size(t1,2),1,size(t1,3)); | ||
17 | t1a = squeeze(t1a); | ||
18 | t1a = t1a'; | ||
19 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compact.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compact.m new file mode 100755 index 0000000..9863e0f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compact.m | |||
@@ -0,0 +1,36 @@ | |||
1 | function I = compact(img,ws) | ||
2 | |||
3 | |||
4 | %ws = 2*hws+1; | ||
5 | |||
6 | [sy,sx] = size(img); | ||
7 | |||
8 | rem_x = rem(sx,ws); | ||
9 | rem_y = rem(sy,ws); | ||
10 | |||
11 | fix_x = ceil(sx/ws); | ||
12 | fix_y = ceil(sy/ws); | ||
13 | |||
14 | fprintf('nr = %d, nc = %d\n',fix_y,fix_x); | ||
15 | |||
16 | %startx= 1 + floor(rem_x*0.5)+hws; | ||
17 | %starty= 1 + floor(rem_y*0.5)+hws; | ||
18 | |||
19 | I = zeros(fix_y,fix_x); | ||
20 | |||
21 | yid = 0; | ||
22 | for j=1:ws:sy, | ||
23 | xid = 0; | ||
24 | yid = yid +1; | ||
25 | fprintf('.'); | ||
26 | for k=1:ws:sx, | ||
27 | xid = xid+1; | ||
28 | %I(yid,xid) = median(median(img(j-hws:j+hws,k-hws:k+hws))); | ||
29 | %I(yid,xid) = sum(sum(img(j-hws:j+hws,k-hws:k+hws))); | ||
30 | v = img(j:min(sy,j+ws-1),k:min(sx,k+ws-1)); | ||
31 | %I(yid,xid) = median(reshape(v,prod(size(v)),1)); | ||
32 | I(yid,xid) = median(median(v)); | ||
33 | end | ||
34 | end | ||
35 | fprintf('\n'); | ||
36 | \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_J.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_J.m new file mode 100755 index 0000000..99b8b69 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_J.m | |||
@@ -0,0 +1,31 @@ | |||
1 | function J = compute_J(A,I,size_x,size_y,D) | ||
2 | %% function J = compute_J(A,I,size_x,size_y,D) | ||
3 | % | ||
4 | |||
5 | [center_x,center_y] = find_center(size_x,size_y); | ||
6 | |||
7 | tmp = ones(size_y,1)*[1:size_x]; | ||
8 | index(:,1) = reshape(tmp,size_x*size_y,1)-center_x*ones(size_x*size_y,1); | ||
9 | index(:,2) = reshape(tmp',size_x*size_y,1)-center_y*ones(size_x*size_y,1); | ||
10 | |||
11 | position_new = A*index'+ [D(1),0;0,D(2)]*ones(2,size_x*size_y); | ||
12 | position_new = round(position_new +... | ||
13 | [center_x,0;0,center_y]*ones(2,size_x*size_y)); | ||
14 | % we have to deal with out of boundary ones | ||
15 | % | ||
16 | bad_ones(1,:) = position_new(1,:)<1 | position_new(1,:)>size_x; | ||
17 | bad_ones(2,:) = position_new(2,:)<1 | position_new(2,:)>size_y; | ||
18 | bad = max([bad_ones(1,:);bad_ones(2,:)]); | ||
19 | good = ~bad; | ||
20 | % if new index is out of boundary, then set it to (0,0) | ||
21 | position_new(1,:) = position_new(1,:).*good; | ||
22 | position_new(2,:) = position_new(2,:).*good; | ||
23 | |||
24 | new_index = size_y*(position_new(1,:)-ones(1,size_x*size_y))+... | ||
25 | position_new(2,:); | ||
26 | new_index = max([new_index;ones(1,size_x*size_y)]); | ||
27 | J = I(new_index); | ||
28 | % set the "out of boundary" to zero. | ||
29 | J = J.*good; | ||
30 | J = reshape(J',size_y,size_x); | ||
31 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_Lf.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_Lf.m new file mode 100755 index 0000000..7cda523 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_Lf.m | |||
@@ -0,0 +1,35 @@ | |||
1 | function dists = compute_Lf(F,cts,wz,nr,nc) | ||
2 | |||
3 | gap = 2*wz(1)+1; | ||
4 | hw = wz(1); | ||
5 | |||
6 | nr = nr+1; | ||
7 | nc = nc+1; | ||
8 | |||
9 | dists = zeros(size(cts,1),nr*nc); | ||
10 | for ctj = 1:size(cts,1), | ||
11 | t1 = cutout(F,cts(ctj,:),wz); | ||
12 | |||
13 | rid = 1; | ||
14 | fprintf('>'); | ||
15 | |||
16 | for ri = hw+1:gap:size(F,1)-hw, | ||
17 | %fprintf('[%d]',ri); | ||
18 | cid = 1; | ||
19 | for ci = hw+1:gap:size(F,2)-hw, | ||
20 | %fprintf('(%d)',ci); | ||
21 | t2 = cutout(F,[ci,ri],wz); | ||
22 | |||
23 | dist = abs(mean(t1(:))-mean(t2(:))); | ||
24 | |||
25 | dists(ctj,rid+cid*nr) = max(dist,dists(ctj,rid+cid*nr)); | ||
26 | |||
27 | cid = cid+1; | ||
28 | end | ||
29 | rid = rid+1; | ||
30 | end | ||
31 | |||
32 | %fprintf('\n'); | ||
33 | |||
34 | end | ||
35 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_corr.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_corr.m new file mode 100755 index 0000000..92f9da4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_corr.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function a = compute_corr(f,g) | ||
2 | % | ||
3 | % compute the circular correlation of f and g | ||
4 | % at points around zero | ||
5 | % | ||
6 | % | ||
7 | |||
8 | ff = interp(f,4); | ||
9 | gg = interp(g,4); | ||
10 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_diff.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_diff.m new file mode 100755 index 0000000..72bfe54 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_diff.m | |||
@@ -0,0 +1,36 @@ | |||
1 | function B = compute_diff(Ja,Jfa,hw,hnb); | ||
2 | % | ||
3 | % B = compute_diff(Ja,Jfa,hw,hnb) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | figure(1);%imagesc(Ja);axis('image'); | ||
8 | cs = round(ginput(1)); | ||
9 | |||
10 | B = zeros(2*hnb+1,2*hnb+1); | ||
11 | |||
12 | scales = [1:5];filter_ids = [1:7]; | ||
13 | Jc = get_win(Ja,cs,[hw,hw]); | ||
14 | Jfc= get_win5(Jfa,cs,[hw,hw]); | ||
15 | H2c = hist2d(Jc,Jfc,scales,filter_ids); | ||
16 | |||
17 | figure(2);imagesc(Ja);axis('image');colormap(gray); | ||
18 | hold on; plot(cs(1),cs(2),'g*'); | ||
19 | |||
20 | |||
21 | for ii=-hnb:hnb, | ||
22 | for jj=-hnb:hnb, | ||
23 | J1 = get_win(Ja,cs+4*[jj,ii],[hw,hw]); | ||
24 | Jf1= get_win5(Jfa,cs+4*[jj,ii],[hw,hw]); | ||
25 | figure(2);plot(cs(1)+4*jj,cs(2)+4*ii,'ro');drawnow; | ||
26 | %figure(3);imagesc(J1);drawnow; | ||
27 | |||
28 | H2 = hist2d(J1,Jf1,scales,filter_ids); | ||
29 | d = hist_diff(H2/prod(size(Jc)),H2c/prod(size(Jc))); | ||
30 | disp(sprintf('d=%f',d)); | ||
31 | B(ii+hnb+1,jj+hnb+1) = d; | ||
32 | |||
33 | end | ||
34 | end | ||
35 | |||
36 | figure(2);hold off; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_diff_patch.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_diff_patch.m new file mode 100755 index 0000000..260b93a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_diff_patch.m | |||
@@ -0,0 +1,34 @@ | |||
1 | function a = compute_diff_patch(gx1,gy1,gx2,gy2,I1,I2) | ||
2 | % | ||
3 | % a = compute_diff_patch(gx1,gy1,gx2,gy2,I1,I2) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | %ws = size(gx1); | ||
8 | %mask = smooth(ones(ws),2*max(ws)); | ||
9 | %mask = mask/sum(sum(mask)); | ||
10 | |||
11 | %mag1= sum(sum(sqrt((mask.*gx1).^2 + (mask.*gy1).^2))); | ||
12 | %mag2= sum(sum(sqrt((mask.*gx2).^2 + (mask.*gy2).^2))); | ||
13 | |||
14 | mag1= sum(sum(sqrt((gx1).^2 + (gy1).^2))); | ||
15 | mag2= sum(sum(sqrt((gx2).^2 + (gy2).^2))); | ||
16 | |||
17 | P_tx1 = sigmoid(mag1,400,80); | ||
18 | P_tx2 = sigmoid(mag2,400,80); | ||
19 | |||
20 | diff_I = mean(reshape(I1,prod(size(I1)),1))-... | ||
21 | mean(reshape(I2,prod(size(I2)),1)); | ||
22 | diff_I = abs(diff_I); | ||
23 | |||
24 | s_g1 = [sum(sum(abs(gx1))),sum(sum(abs(gy1)))]; | ||
25 | s_g2 = [sum(sum(abs(gx2))),sum(sum(abs(gy2)))]; | ||
26 | |||
27 | s_g1 = s_g1/(norm(s_g1)); | ||
28 | s_g2 = s_g2/(norm(s_g2)); | ||
29 | |||
30 | a = (1-P_tx1)*(1-P_tx2)*exp(-diff_I/0.1) +... | ||
31 | P_tx1*P_tx2*(dot(s_g1,s_g2)); | ||
32 | |||
33 | |||
34 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_diff_patch2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_diff_patch2.m new file mode 100755 index 0000000..9d2b528 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_diff_patch2.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function [a,phi1,phi2] = compute_diff_patch(gx1,gy1,gx2,gy2,I1,I2) | ||
2 | % | ||
3 | % a = compute_diff_patch(gx1,gy1,gx2,gy2,I1,I2) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | %ws = size(gx1); | ||
8 | %mask = smooth(ones(ws),2*max(ws)); | ||
9 | %mask = mask/sum(sum(mask)); | ||
10 | |||
11 | %mag1= sum(sum(sqrt((mask.*gx1).^2 + (mask.*gy1).^2))); | ||
12 | %mag2= sum(sum(sqrt((mask.*gx2).^2 + (mask.*gy2).^2))); | ||
13 | |||
14 | mag1= sum(sum(sqrt((gx1).^2 + (gy1).^2)))/prod(size(gx1)); | ||
15 | mag2= sum(sum(sqrt((gx2).^2 + (gy2).^2)))/prod(size(gx1)); | ||
16 | |||
17 | P_tx1 = sigmoid(mag1,2,0.5); | ||
18 | P_tx2 = sigmoid(mag2,2,0.5); | ||
19 | |||
20 | diff_I = mean(reshape(I1,prod(size(I1)),1))-... | ||
21 | mean(reshape(I2,prod(size(I2)),1)); | ||
22 | diff_I = abs(diff_I); | ||
23 | |||
24 | [l1,l2,phi1] = mwis(gx1,gy1); | ||
25 | [k1,k2,phi2] = mwis(gx2,gy2); | ||
26 | |||
27 | ratio1 = min([l1,l2])/max([l1,l2]); | ||
28 | ratio2 = min([k1,k2])/max([k1,k2]); | ||
29 | |||
30 | r1 = 1-sigmoid(ratio1,0.35,0.05); | ||
31 | r2 = 1-sigmoid(ratio2,0.35,0.05); | ||
32 | |||
33 | s1 = [cos(phi1),sin(phi1)]; | ||
34 | s2 = [cos(phi2),sin(phi2)]; | ||
35 | |||
36 | angle = acos(abs(dot(s1,s2)))*180/pi; | ||
37 | |||
38 | a1 = (1-P_tx1*P_tx2)*exp(-diff_I/0.1); | ||
39 | a2 = P_tx1*P_tx2*(r1*r2*(90-angle)/90); | ||
40 | a3 = P_tx1*P_tx2*((1-r1*r2)*(1-sigmoid(abs(r1-r2),0.3,0.04))); | ||
41 | |||
42 | a = a1+a2+a3; | ||
43 | |||
44 | |||
45 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_filter.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_filter.m new file mode 100755 index 0000000..04e78e1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_filter.m | |||
@@ -0,0 +1,84 @@ | |||
1 | function [filter_output,filters] = compute_filter(I,sig,r,sz); | ||
2 | % | ||
3 | % | ||
4 | % | ||
5 | |||
6 | ori_incr=180/num_ori; | ||
7 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
8 | |||
9 | as = ori_offset:ori_incr:180+ori_offset-ori_incr; | ||
10 | |||
11 | filter_output = []; | ||
12 | filters = []; | ||
13 | |||
14 | wsz = 2*round(sz) + 1; | ||
15 | M1 = wsz(1);M2 = wsz(2); | ||
16 | |||
17 | %%%%% prepare FFT of image %%%%%%%%%%%%% | ||
18 | |||
19 | [N1,N2]=size(I); | ||
20 | tmp=zeros(size(I)+[M1-1 M2-1]); | ||
21 | tmp(1:N1,1:N2)=I; | ||
22 | IF=fft2(tmp); | ||
23 | |||
24 | |||
25 | %%%%%%%%%% filtering stage %%%%%%%%%%% | ||
26 | if size(sig,2)== 1, | ||
27 | |||
28 | for j = 1:length(as), | ||
29 | fprintf('.'); | ||
30 | angle = as(j); | ||
31 | |||
32 | g = mdoog2(sig,r,angle,round(sz)); | ||
33 | |||
34 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
35 | |||
36 | g = g/sum(sum(abs(g))); | ||
37 | |||
38 | filters(:,:,j) = g; | ||
39 | |||
40 | gF = fft2(g,N1+M1-1,N2+M2-1); | ||
41 | IgF = If.*gF; | ||
42 | Ig = real(ifft2(IgF)); | ||
43 | Ig = Ig(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
44 | |||
45 | %c = conv2(I,g,'valid'); | ||
46 | |||
47 | filter_output(:,:,j) = Ig; | ||
48 | end | ||
49 | else | ||
50 | |||
51 | % there are multiple scales | ||
52 | sigs = sig; | ||
53 | szs = sz; | ||
54 | for k = 1:size(sigs,2), | ||
55 | sig = sigs(k); | ||
56 | sz = szs(k); | ||
57 | fprintf('%d',k); | ||
58 | for j = 1:length(as), | ||
59 | fprintf('.'); | ||
60 | angle = as(j); | ||
61 | |||
62 | g = mdoog2(sig,r,angle,round(sz)); | ||
63 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
64 | g = g/sum(sum(abs(g))); | ||
65 | |||
66 | gF = fft2(g,N1+M1-1,N2+M2-1); | ||
67 | IgF = If.*gF; | ||
68 | Ig = real(ifft2(IgF)); | ||
69 | Ig = Ig(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
70 | |||
71 | %c = conv2(I,g,'valid'); | ||
72 | %c = conv2(I,g,'same'); | ||
73 | |||
74 | filter_output(:,:,j,k) = Ig; | ||
75 | filters(:,:,j,k) = g; | ||
76 | end | ||
77 | |||
78 | |||
79 | end | ||
80 | |||
81 | end | ||
82 | |||
83 | fprintf('\n'); | ||
84 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_filter_fft.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_filter_fft.m new file mode 100755 index 0000000..359c6ba --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/compute_filter_fft.m | |||
@@ -0,0 +1,84 @@ | |||
1 | function [filter_output,filters] = compute_filter_fft(I,sig,r,sz,num_ori); | ||
2 | % | ||
3 | % | ||
4 | % | ||
5 | |||
6 | ori_incr=180/num_ori; | ||
7 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
8 | |||
9 | as = ori_offset:ori_incr:180+ori_offset-ori_incr; | ||
10 | |||
11 | filter_output = []; | ||
12 | filters = []; | ||
13 | |||
14 | wsz = 2*round(sz(end)) + 1; | ||
15 | M1 = wsz;M2 = wsz; | ||
16 | |||
17 | %%%%% prepare FFT of image %%%%%%%%%%%%% | ||
18 | |||
19 | [N1,N2]=size(I); | ||
20 | tmp=zeros(size(I)+[M1-1 M2-1]); | ||
21 | tmp(1:N1,1:N2)=I; | ||
22 | IF=fft2(tmp); | ||
23 | |||
24 | |||
25 | %%%%%%%%%% filtering stage %%%%%%%%%%% | ||
26 | if size(sig,2)== 1, | ||
27 | |||
28 | for j = 1:length(as), | ||
29 | fprintf('.'); | ||
30 | angle = as(j); | ||
31 | |||
32 | g = mdoog2(sig,r,angle,round(sz)); | ||
33 | |||
34 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
35 | |||
36 | g = g/sum(sum(abs(g))); | ||
37 | |||
38 | filters(:,:,j) = g; | ||
39 | |||
40 | gF = fft2(g,N1+M1-1,N2+M2-1); | ||
41 | IgF = IF.*gF; | ||
42 | Ig = real(ifft2(IgF)); | ||
43 | Ig = Ig(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
44 | |||
45 | %c = conv2(I,g,'valid'); | ||
46 | |||
47 | filter_output(:,:,j) = Ig; | ||
48 | end | ||
49 | else | ||
50 | |||
51 | % there are multiple scales | ||
52 | sigs = sig; | ||
53 | szs = sz; | ||
54 | for k = 1:size(sigs,2), | ||
55 | sig = sigs(k); | ||
56 | sz = szs(end); | ||
57 | fprintf('%d',k); | ||
58 | for j = 1:length(as), | ||
59 | fprintf('.'); | ||
60 | angle = as(j); | ||
61 | |||
62 | g = mdoog2(sig,r,angle,round(sz)); | ||
63 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
64 | g = g/sum(sum(abs(g))); | ||
65 | |||
66 | gF = fft2(g,N1+M1-1,N2+M2-1); | ||
67 | IgF = IF.*gF; | ||
68 | Ig = real(ifft2(IgF)); | ||
69 | Ig = Ig(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
70 | |||
71 | %c = conv2(I,g,'valid'); | ||
72 | %c = conv2(I,g,'same'); | ||
73 | |||
74 | filter_output(:,:,j,k) = Ig; | ||
75 | filters(:,:,j,k) = g; | ||
76 | end | ||
77 | |||
78 | |||
79 | end | ||
80 | |||
81 | end | ||
82 | |||
83 | fprintf('\n'); | ||
84 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/conv_trim.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/conv_trim.m new file mode 100755 index 0000000..30d16a9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/conv_trim.m | |||
@@ -0,0 +1,6 @@ | |||
1 | % trims an array to remove meaningless pixels after a convolution with | ||
2 | % an r * c window | ||
3 | |||
4 | function[B] = conv_trim(A, r, c) | ||
5 | |||
6 | B = A(r+1:size(A,1)-r, c+1:size(A,2)-c); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/corr_hist.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/corr_hist.m new file mode 100755 index 0000000..c538dc1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/corr_hist.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function alpha = corr_hist(hists) | ||
2 | |||
3 | [y,x,v] = find(hists); | ||
4 | mx = sum(x.*v)/sum(v); | ||
5 | my = sum(y.*v)/sum(v); | ||
6 | |||
7 | top = sum( (x-mx).*(y-my).*v); | ||
8 | bottom = sqrt(sum( ((x-mx).^2).*v))*sqrt(sum( ((y-my).^2).*v)); | ||
9 | alpha = top/bottom; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/crop_im_fil.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/crop_im_fil.m new file mode 100755 index 0000000..5472171 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/crop_im_fil.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function [J,f,rect] = crop_im_fil(Ja,Jfa,fig_id) | ||
2 | % | ||
3 | % | ||
4 | |||
5 | figure(fig_id); | ||
6 | imagesc(Ja);axis('image'); | ||
7 | |||
8 | [J,rect] = imcrop;rect = round(rect); | ||
9 | J = Ja(rect(2):rect(2)+rect(4),rect(1):rect(1)+rect(3)); | ||
10 | f = Jfa(rect(2):rect(2)+rect(4),rect(1):rect(1)+rect(3),:,:); | ||
11 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/cutoff.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/cutoff.m new file mode 100755 index 0000000..58c6b94 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/cutoff.m | |||
@@ -0,0 +1,13 @@ | |||
1 | function I = cutoff(I,wc) | ||
2 | % | ||
3 | % | ||
4 | |||
5 | nr = size(I,1); | ||
6 | nc = size(I,2); | ||
7 | |||
8 | if ndims(I) == 3, | ||
9 | I = I(wc+1:nr-wc,wc+1:nc-wc,:,:); | ||
10 | else | ||
11 | I = I(wc+1:nr-wc,wc+1:nc-wc,:,:); | ||
12 | end | ||
13 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/cutout.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/cutout.m new file mode 100755 index 0000000..b80f27b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/cutout.m | |||
@@ -0,0 +1,3 @@ | |||
1 | function a = cutout(I,ct,wz); | ||
2 | |||
3 | a = I(ct(2)-wz(2):ct(2)+wz(2),ct(1)-wz(1):ct(1)+wz(1),:); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_Imask.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_Imask.m new file mode 100755 index 0000000..dbd7fdb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_Imask.m | |||
@@ -0,0 +1,20 @@ | |||
1 | function Imasks = disp_Imask(Is,nr,nc,hw,masks) | ||
2 | % | ||
3 | % Imasks = disp_Imask(Is,nr,nc,hw,masks) | ||
4 | % | ||
5 | |||
6 | %hw = 2; %nr = 43;nc=68; | ||
7 | gap = 2*hw+1; | ||
8 | |||
9 | x = [1:nc*gap]; | ||
10 | y = [1:nr*gap]; | ||
11 | |||
12 | xs = (x-hw-1)/gap + 1;ys = (y-hw-1)/gap + 1; | ||
13 | |||
14 | for gid=1:size(masks,3), | ||
15 | tmp = interp2(reshape(masks(:,:,gid),nr,nc),xs,ys'); | ||
16 | |||
17 | Imasks(:,:,gid) = (tmp>0.52).* ((Is).^0.8); | ||
18 | subplot(3,3,gid); | ||
19 | im(Imasks(:,:,gid)); | ||
20 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_diff.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_diff.m new file mode 100755 index 0000000..090c273 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_diff.m | |||
@@ -0,0 +1,37 @@ | |||
1 | function disp_diff(H1,H2) | ||
2 | % | ||
3 | % disp_diff(H1,H2) | ||
4 | % | ||
5 | |||
6 | ns = size(H1,3); | ||
7 | nf = size(H1,4); | ||
8 | |||
9 | H1 = H1/49; | ||
10 | H2 = H2/49; | ||
11 | |||
12 | |||
13 | sI= [1,0,1];sI = exp(-sI); | ||
14 | sI = sI/sum(sI); | ||
15 | |||
16 | for j = 1:ns, | ||
17 | for k = 1:nf, | ||
18 | h1 = H1(:,:,j,k); | ||
19 | h2 = H2(:,:,j,k); | ||
20 | |||
21 | subplot(ns,nf,(j-1)*nf+k); | ||
22 | h1s = conv2(conv2(h1,sI','same'),sI,'same'); | ||
23 | h2s = conv2(conv2(h2,sI','same'),sI,'same'); | ||
24 | |||
25 | [is,js] = find( (h1>0) | (h2>0)); | ||
26 | ids = (js-1)*size(h1,1) + is; | ||
27 | |||
28 | hdiff = abs(h1s-h2s).*((h1>0) | (h2>0)); | ||
29 | |||
30 | xdiff = ((h1(ids)-h2(ids)).*(h1(ids)-h2(ids)))./(h1(ids)+h2(ids)); | ||
31 | |||
32 | xdiffs = ((h1s(ids)-h2s(ids)).*(h1s(ids)-h2s(ids)))./(h1s(ids)+h2s(ids)); | ||
33 | imagesc(hdiff);colorbar;axis('off'); | ||
34 | % title(sprintf('%3.3f, %3.3f',sum(sum(hdiff))/49,sum(sum(abs(h1-h2)))/49));drawnow; | ||
35 | title(sprintf('%3.3f, %3.3f',sum(xdiff),sum(xdiffs)));drawnow | ||
36 | end | ||
37 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_evresult.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_evresult.m new file mode 100755 index 0000000..e07556a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_evresult.m | |||
@@ -0,0 +1,435 @@ | |||
1 | %fn = '134035'; | ||
2 | %fn = '130040'; | ||
3 | %fn = '334074'; | ||
4 | fn = '130065'; | ||
5 | |||
6 | %basedir = 'plaatje_data/olddata/'; | ||
7 | % basedir = 'data/'; nr = 49;nc =30; | ||
8 | |||
9 | basedir = 'plaatje_data/'; | ||
10 | |||
11 | fname = sprintf('%s%s_eigvec.pfm',basedir,fn); | ||
12 | eigv = readpfm(fname); | ||
13 | fname = sprintf('%s%s_eigval.pfm',basedir,fn); | ||
14 | eigval = readpfm(fname); | ||
15 | |||
16 | fname = sprintf('%s%s_ncutvec.pfm',basedir,fn); | ||
17 | ncutv = readpfm(fname); | ||
18 | fname = sprintf('%s%s_ncutval.pfm',basedir,fn); | ||
19 | ncutval = readpfm(fname); | ||
20 | |||
21 | %fname = sprintf('images/130039.pgm'); | ||
22 | fname = sprintf('images/%s.pgm',fn); | ||
23 | I = readpgm(fname); | ||
24 | cutsz = 20; I = cutoff(I,cutsz); | ||
25 | figure(3);im(I);colormap(gray); | ||
26 | |||
27 | new = 0; | ||
28 | |||
29 | if ~new, | ||
30 | |||
31 | %nr = 49;nc = 30; | ||
32 | nr = 30;nc = 49; | ||
33 | |||
34 | %nr = 68;nc = 43; | ||
35 | %nc = 68;nr = 43; | ||
36 | |||
37 | else | ||
38 | |||
39 | fn1 = fn; | ||
40 | fn = 'test'; | ||
41 | fname = sprintf('plaatje_data/%s_gcs.pfm',fn); | ||
42 | gcs = readpfm(fname); | ||
43 | |||
44 | fname = sprintf('plaatje_data/%s_gce.pfm',fn); | ||
45 | gce = readpfm(fname); | ||
46 | |||
47 | fname = sprintf('plaatje_data/%s_grs.pfm',fn); | ||
48 | grs = readpfm(fname); | ||
49 | |||
50 | fname = sprintf('plaatje_data/%s_gre.pfm',fn); | ||
51 | gre = readpfm(fname); | ||
52 | |||
53 | nr = max(gre(:))+1; | ||
54 | nc = max(gce(:))+1; | ||
55 | |||
56 | fn = fn1; | ||
57 | |||
58 | end | ||
59 | |||
60 | figure(6); | ||
61 | for j=1:8, | ||
62 | subplot(3,3,j); | ||
63 | im(reshape(ncutv(:,j+1),nr,nc));colorbar | ||
64 | title(num2str(ncutval(j+1,1))); | ||
65 | end | ||
66 | %cm = sprintf('print -dps ncut_%s',fn);disp(cm);eval(cm); | ||
67 | subplot(3,3,9);im(I);axis('off'); | ||
68 | |||
69 | figure(7);clf | ||
70 | for j=1:12, | ||
71 | subplot(3,4,j); | ||
72 | im(reshape(eigv(:,j),nr,nc));colorbar;%axis('off'); | ||
73 | title(sprintf('%3.4e',eigval(j,1))); | ||
74 | end | ||
75 | %cm = sprintf('print -dps eig_%s',fn);disp(cm);eval(cm); | ||
76 | |||
77 | %%%%%%%%%%% | ||
78 | |||
79 | ev = eigval(:,1); | ||
80 | figure(5);hold off;clf;subplot(1,2,1); | ||
81 | %semilogy((ev(1:end-1) - ev(2:end))./ev(1:end-1),'x-');grid on; | ||
82 | plot((ev(1:end-1) - ev(2:end))./ev(1:end-1),'x-');grid on; | ||
83 | %semilogy(0.01*ones(size(ev(2:end-1))),'r-');semilogy(0.005*ones(size(ev(2:end-1))),'r-');semilogy(0.0025*ones(size(ev(2:end-1))),'r-');grid on;hold off; | ||
84 | subplot(1,2,2); | ||
85 | %semilogy(ev(1:end-1)-ev(2:end),'p-');grid on; | ||
86 | semilogy((ev(1:end-1) - ev(2:end))/ev(1),'x-');grid on; | ||
87 | |||
88 | |||
89 | if 0, | ||
90 | |||
91 | fname = sprintf('plaatje_data/ncutval_%s.pfm',fn); | ||
92 | nval = readpfm(fname); | ||
93 | fname = sprintf('plaatje_data/ncutvec_%s.pfm',fn); | ||
94 | nv = readpfm(fname); | ||
95 | |||
96 | figure(2); | ||
97 | nvv = size(nv,2); | ||
98 | for j=1:min(5,nvv-1), | ||
99 | subplot(1,min(5,nvv-1),j); | ||
100 | ims(nv(:,j+1),nr,nc); | ||
101 | end | ||
102 | |||
103 | |||
104 | %figure(5); | ||
105 | %subplot(2,2,1);plot(eigval(:,1),'x-'); | ||
106 | |||
107 | |||
108 | if 0, | ||
109 | |||
110 | fname = 130039; | ||
111 | for j=0:20, | ||
112 | cm = sprintf('!cp plaatje_data/%d_%d.pfm plaatje_data/test_%d.pfm ',fname,j,j); | ||
113 | disp(cm);eval(cm); | ||
114 | end | ||
115 | |||
116 | %%%%%%%% | ||
117 | fnout = 'test';fn_t = '334003'; | ||
118 | cm = sprintf('!cp plaatje_data/%s_eigval.pfm %s_eigval.pfm',fnout,fn_t); | ||
119 | disp(cm);eval(cm); | ||
120 | cm = sprintf('!cp plaatje_data/%s_eigvec.pfm %s_eigvec.pfm',fnout,fn_t); | ||
121 | disp(cm);eval(cm); | ||
122 | cm = sprintf('!cp plaatje_data/%s_ncutvec.pfm %s_ncutvec.pfm',fnout,fn_t); | ||
123 | disp(cm);eval(cm); | ||
124 | cm = sprintf('!cp plaatje_data/%s_ncutval.pfm %s_ncutval.pfm',fnout,fn_t); | ||
125 | disp(cm);eval(cm); | ||
126 | |||
127 | |||
128 | |||
129 | |||
130 | end | ||
131 | |||
132 | disp_flag = 0; | ||
133 | if disp_flag, | ||
134 | [I1,bnr,bnc] = proj_back_id(ncutv(:,2),gcs,gce,grs,gre); | ||
135 | imvs(I,I1>0.002,bnr,bnc); | ||
136 | end | ||
137 | |||
138 | if 0, | ||
139 | |||
140 | nv = 3; | ||
141 | A = eigv(:,1:nv)*eigv(:,1:nv)'; | ||
142 | [v,d] = ncut(abs(A),min(nv,5)); | ||
143 | |||
144 | figure(3); | ||
145 | for j=1:min(nv,5), | ||
146 | subplot(2,2,j); | ||
147 | ims(v(:,j),nr,nc); | ||
148 | end | ||
149 | |||
150 | end | ||
151 | |||
152 | %%%%%%%% | ||
153 | |||
154 | figure(4);%im(I);colorbar; | ||
155 | hw = 3;st_sz = 2*hw+1; | ||
156 | ct = round(ginput(1)); | ||
157 | ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
158 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
159 | |||
160 | idx = (ct_chank(:,1)-1)*nr + ct_chank(:,2); | ||
161 | |||
162 | figure(5);im(abs(reshape(A(idx,:),nr,nc)));%colorbar; | ||
163 | |||
164 | |||
165 | |||
166 | %%%%% | ||
167 | |||
168 | fname = 'test2'; | ||
169 | fn = sprintf('plaatje_data/ncut_%s.pfm',fname); | ||
170 | ncutv1 = readpfm(fn); | ||
171 | nr = 30; nc=49; | ||
172 | |||
173 | figure(1); | ||
174 | for j=1:min(4,size(ncutv1,2)), | ||
175 | subplot(2,2,j); | ||
176 | ims(ncutv1(:,j+1),nr,nc); | ||
177 | end | ||
178 | |||
179 | |||
180 | |||
181 | %%%%%%%%%% | ||
182 | |||
183 | id = 0; | ||
184 | fn = sprintf('plaatje_data/test_Aa%d.pfm',id); | ||
185 | disp(sprintf('A = readpfm(%s);',fn)); | ||
186 | A = readpfm(fn); | ||
187 | |||
188 | cm = sprintf('[v%d,d%d] = eigs(A,12);',id,id); | ||
189 | disp(cm);eval(cm); | ||
190 | |||
191 | writepfm('test_eigv0.pfm',v0); | ||
192 | writepfm('test_eigva0.pfm',diag(d0)); | ||
193 | |||
194 | |||
195 | |||
196 | |||
197 | vs = zeros(size(v1,1),size(v1,2),6); | ||
198 | ds = zeros(length(d1),6); | ||
199 | |||
200 | for j=0:5, | ||
201 | cm = sprintf('vs(:,:,%d) = v%d;',j+1,j); | ||
202 | disp(cm);eval(cm); | ||
203 | cm = sprintf('d = diag(d%d);',j); | ||
204 | disp(cm);eval(cm); | ||
205 | cm = sprintf('ds(:,%d) = d(:);',j+1); | ||
206 | disp(cm);eval(cm); | ||
207 | |||
208 | |||
209 | end | ||
210 | |||
211 | %save evsum vs ds | ||
212 | |||
213 | figure(1);nr = 49;nc=30;evid = 3; | ||
214 | for j=1:12,subplot(3,4,j);ims(vs(:,j,evid),nr,nc);end | ||
215 | |||
216 | I = readpgm('images/334039.pgm');I = cutoff(I,20); | ||
217 | |||
218 | As = zeros(6,nr*nc); | ||
219 | |||
220 | figure(3);%im(I);colormap(gray); | ||
221 | hw = 3;st_sz = 2*hw+1; | ||
222 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1;ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
223 | idx = (ct_chank(:,1)-1)*nr + ct_chank(:,2); | ||
224 | |||
225 | figure(5); | ||
226 | |||
227 | figure(4);nvs = [6,9,12,12,12,12]; | ||
228 | for evid = 1:5,As(evid,:) = squeeze(vs(idx,1:nvs(evid),evid))*squeeze(vs(:,1:nvs(evid),evid))';end | ||
229 | for evid =1:5,subplot(2,3,evid);im(abs(reshape(As(evid,:),nr,nc)));colorbar;end | ||
230 | subplot(2,3,6);ims(sum(abs(As)),nr,nc);colorbar | ||
231 | |||
232 | %%%%%%%%% | ||
233 | |||
234 | %%%%%% eig of the As over all scales %% | ||
235 | |||
236 | A = zeros(nr*nc,nr*nc); | ||
237 | |||
238 | for evid=1:5, disp(evid); | ||
239 | A = A + abs(squeeze(vs(:,1:nvs(evid),evid))*squeeze(vs(:,1:nvs(evid),evid))'); | ||
240 | end | ||
241 | |||
242 | [v,d] = eigs(A,12); | ||
243 | figure(1); for j=1:12, subplot(3,4,j);ims(v(:,j),nr,nc);end | ||
244 | |||
245 | [vn,dn] = ncut_b(A,12); | ||
246 | figure(3); for j=1:12, subplot(3,4,j);ims(-vn(:,j),nr,nc);end | ||
247 | |||
248 | nv = 6; | ||
249 | A = abs(eigv(:,1:nv)*eigv(:,1:nv)'); | ||
250 | [v,d] = ncut_b(A,nv+1); | ||
251 | figure(1); | ||
252 | nv = 4; | ||
253 | for j=1:nv,subplot(2,nv,j);ims(v(:,j+1),nr,nc);title(sprintf('%3.3e',d(j+1)));end | ||
254 | |||
255 | for j=1:nv,subplot(2,nv,j+nv);ims(eigv(:,j),nr,nc);title(sprintf('%3.3e',eigval(j,1)));end | ||
256 | |||
257 | %%%%%%%%%%%%% | ||
258 | |||
259 | while 1, | ||
260 | figure(3);%im(I);colormap(gray); | ||
261 | hw = 3;st_sz = 2*hw+1; | ||
262 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1;ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
263 | idx = (ct_chank(:,1)-1)*nr + ct_chank(:,2); | ||
264 | |||
265 | figure(1); | ||
266 | ims(exp(-(A(idx,:))/(0.02^2)),nr,nc);colorbar | ||
267 | end | ||
268 | |||
269 | |||
270 | |||
271 | %%%%%%%%%%%%%% | ||
272 | |||
273 | figure(3); | ||
274 | hw = 3;st_sz = 2*hw+1; | ||
275 | np = 20; | ||
276 | ct = round(ginput(np)); | ||
277 | ct_chank =[]; | ||
278 | ct_chank(:,1) = round((ct(:,1)-hw-1)/st_sz) + 1; | ||
279 | ct_chank(:,2) = round((ct(:,2)-hw-1)/st_sz) + 1; | ||
280 | idx = (ct_chank(:,1)-1)*nr + ct_chank(:,2); | ||
281 | |||
282 | %As = readpfm_id('plaatje_data/130040_AX.pfm',idx,2924); | ||
283 | As = readpfm_idf('plaatje_data/tmp/134035_AX3.pfm',idx,nr*nc); | ||
284 | |||
285 | %save dist_data2 As idx ct_chank ct hw nr nc eigv eigval | ||
286 | |||
287 | %load dist_data1a | ||
288 | |||
289 | set(gcf,'DefaultLineLinewidth',5); | ||
290 | |||
291 | minA = min(min(As)); | ||
292 | figure(1);clf; hold off; | ||
293 | set(gcf,'DefaultLineLinewidth',2); | ||
294 | for id = 1:np, | ||
295 | subplot(4,5,id); | ||
296 | %image(2.8e-2*((-minA)+reshape(As(id,:),nr,nc)));axis('image');axis('off');hold on | ||
297 | ims(-As(id,:),nr,nc);axis('off');hold on | ||
298 | plot(ct_chank(id,1)+1,ct_chank(id,2)+1,'rx');hold off; | ||
299 | end | ||
300 | |||
301 | figure(1);clf;hold off; | ||
302 | nvv = 6 | ||
303 | set(gcf,'DefaultLineLinewidth',1); | ||
304 | for id=1:np, | ||
305 | At = abs(eigv(idx(id),1:nvv)*eigv(:,1:nvv)'); | ||
306 | subplot(4,5,id); | ||
307 | %image(2.5e4*reshape(At,nr,nc));axis('image');axis('off');hold on | ||
308 | ims(At,nr,nc);axis('off');hold on; | ||
309 | plot(ct_chank(id,1)+1,ct_chank(id,2)+1,'rx');hold off; | ||
310 | end | ||
311 | |||
312 | |||
313 | print_flag =0; | ||
314 | if print_flag, | ||
315 | fn = '130040'; | ||
316 | |||
317 | figure(4);clf; | ||
318 | colormap(gray); | ||
319 | set(gcf,'DefaultLineLinewidth',7); | ||
320 | |||
321 | for id =1:np, | ||
322 | %image(2.8e-2*((-minA)+reshape(As(id,:),nr,nc)));axis('image');axis('off');hold on | ||
323 | ims(-As(id,:),nr,nc);axis('off'); | ||
324 | hold on;plot(ct_chank(id,1)+1,ct_chank(id,2)+1,'rp');hold off; | ||
325 | cm = sprintf('print -deps dist_x1_%s_%d',fn,id); | ||
326 | disp(cm);eval(cm); | ||
327 | end | ||
328 | |||
329 | nvv = 5; | ||
330 | set(gcf,'DefaultLineLinewidth',7); | ||
331 | figure(4);colormap(gray); | ||
332 | for id=1:np, | ||
333 | At = abs(eigv(idx(id),1:nvv)*eigv(:,1:nvv)'); | ||
334 | %image(1.5e4*reshape(At,nr,nc));axis('image');axis('off');%hold on | ||
335 | ims(At,nr,nc);axis('off');%hold on; | ||
336 | %plot(ct_chank(id,1)+1,ct_chank(id,2)+1,'rp');hold off; | ||
337 | cm = sprintf('print -deps dist_d_%s_%d',fn,id); | ||
338 | disp(cm);eval(cm); | ||
339 | end | ||
340 | |||
341 | % print eigvects | ||
342 | for j=1:size(eigv,2), | ||
343 | ims(eigv(:,j),nr,nc);axis('off'); | ||
344 | cm = sprintf('print -deps eigv_%s_%d',fn,j); | ||
345 | disp(cm);eval(cm); | ||
346 | end | ||
347 | |||
348 | for j=1:size(ncutv,2), | ||
349 | ims(ncutv(:,j),nr,nc);axis('off'); | ||
350 | cm = sprintf('print -deps ncutv_%s_%d',fn,j); | ||
351 | disp(cm);eval(cm); | ||
352 | end | ||
353 | |||
354 | |||
355 | end | ||
356 | |||
357 | basedir ='plaatje_data/newdata/'; | ||
358 | fname = sprintf('%s%s_eigvec.pfm',basedir,fn); | ||
359 | eigv = readpfm(fname); | ||
360 | |||
361 | ix = 1; | ||
362 | figure(5);colormap(gray);clf | ||
363 | for j=1:7, | ||
364 | for k=[2,3,4,6,9,12]; | ||
365 | subplot(7,6,ix); | ||
366 | At = abs(eigv(idx(j),1:k)*eigv(:,1:k)'); | ||
367 | ims(At,nr,nc);axis('off');%colorbar; | ||
368 | if (k==2), | ||
369 | hold on; plot(ct_chank(j,1),ct_chank(j,2),'rp');hold off; | ||
370 | title(num2str(j)); | ||
371 | end | ||
372 | ix = ix+1; | ||
373 | end | ||
374 | end | ||
375 | |||
376 | figure(4);clf;colormap(gray); | ||
377 | set(gcf,'DefaultLineLinewidth',7); | ||
378 | for j=1:20, | ||
379 | for k=[2,3,4,6,9,12]; | ||
380 | |||
381 | At = abs(eigv(idx(j),1:k)*eigv(:,1:k)'); | ||
382 | ims(At,nr,nc);axis('off');%colorbar; | ||
383 | if (k==2), | ||
384 | hold on; plot(ct_chank(j,1),ct_chank(j,2),'rp');hold off; | ||
385 | end | ||
386 | |||
387 | cm = sprintf('print -deps dist_scale_65_%d_%d',j,k); | ||
388 | disp(cm);eval(cm); | ||
389 | |||
390 | end | ||
391 | end | ||
392 | |||
393 | base_dir = 'plaatje_data/'; | ||
394 | |||
395 | % cts are the centers, | ||
396 | |||
397 | wz = [hw,hw]; | ||
398 | gap = 2*hw+1; | ||
399 | dist = zeros(size(cts,1),(nr+1)*(nc+1)); | ||
400 | |||
401 | for j=1:1:24, | ||
402 | fn = sprintf('%s134035_%d.pfm',base_dir,j); | ||
403 | disp(fn); | ||
404 | F = readpfm(fn); | ||
405 | |||
406 | dists = compute_Lf(F,cts,wz,nr,nc); | ||
407 | dist = max(dists,dist); | ||
408 | |||
409 | end | ||
410 | |||
411 | |||
412 | figure(4);clf;colormap(gray); | ||
413 | set(gcf,'DefaultLineLinewidth',7); | ||
414 | |||
415 | ids = [8,1,12,5,10,15]; | ||
416 | |||
417 | for j=1:6, | ||
418 | |||
419 | d = reshape(dist(j,:),nr+1,nc+1); | ||
420 | d = d(1:end-1,2:end); | ||
421 | im(-d);axis('off'); hold on; | ||
422 | plot(ct_chank(ids(j),1),ct_chank(ids(j),2),'p'); | ||
423 | hold off | ||
424 | |||
425 | cm = sprintf('print -deps dist_lf_65_%d',j); | ||
426 | disp(cm);eval(cm); | ||
427 | |||
428 | pause; | ||
429 | end | ||
430 | |||
431 | |||
432 | |||
433 | end | ||
434 | |||
435 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_evresult2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_evresult2.m new file mode 100755 index 0000000..46239ef --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_evresult2.m | |||
@@ -0,0 +1,215 @@ | |||
1 | fn = '130042'; | ||
2 | |||
3 | fname = sprintf('data/%s_eigvec.pfm',fn); | ||
4 | eigv = readpfm(fname); | ||
5 | fname = sprintf('data/%s_eigval.pfm',fn); | ||
6 | eigval = readpfm(fname); | ||
7 | |||
8 | fname = sprintf('data/%s_ncutvec.pfm',fn); | ||
9 | ncutv = readpfm(fname); | ||
10 | fname = sprintf('data/%s_ncutval.pfm',fn); | ||
11 | ncutval = readpfm(fname); | ||
12 | |||
13 | %fname = sprintf('images/130038.pgm'); | ||
14 | fname = sprintf('images/%s.pgm',fn); | ||
15 | I = readpgm(fname); | ||
16 | cutsz = 20; I = cutoff(I,cutsz); | ||
17 | figure(3);im(I);colormap(gray); | ||
18 | |||
19 | new = 0; | ||
20 | |||
21 | if new, | ||
22 | fn1 = fn; | ||
23 | fn = 'test'; | ||
24 | fname = sprintf('data/%s_gcs.pfm',fn); | ||
25 | gcs = readpfm(fname); | ||
26 | |||
27 | fname = sprintf('data/%s_gce.pfm',fn); | ||
28 | gce = readpfm(fname); | ||
29 | |||
30 | fname = sprintf('data/%s_grs.pfm',fn); | ||
31 | grs = readpfm(fname); | ||
32 | |||
33 | fname = sprintf('data/%s_gre.pfm',fn); | ||
34 | gre = readpfm(fname); | ||
35 | |||
36 | nr = max(gre(:))+1; | ||
37 | nc = max(gce(:))+1; | ||
38 | |||
39 | fn = fn1; | ||
40 | |||
41 | else | ||
42 | %nr = 49;nc = 30; | ||
43 | nr = 30;nc = 49; | ||
44 | |||
45 | end | ||
46 | |||
47 | figure(6); | ||
48 | for j=1:8, | ||
49 | subplot(3,3,j); | ||
50 | im(reshape(ncutv(:,j+1),nr,nc));colorbar | ||
51 | title(num2str(ncutval(j+1,1))); | ||
52 | end | ||
53 | %cm = sprintf('print -dps ncut_%s',fn);disp(cm);eval(cm); | ||
54 | subplot(3,3,9);im(I);axis('off'); | ||
55 | |||
56 | figure(7);clf | ||
57 | for j=1:9, | ||
58 | subplot(3,3,j); | ||
59 | im(reshape(eigv(:,j),nr,nc));colorbar;%axis('off'); | ||
60 | title(sprintf('%3.4e',eigval(j,1))); | ||
61 | end | ||
62 | %cm = sprintf('print -dps eig_%s',fn);disp(cm);eval(cm); | ||
63 | |||
64 | |||
65 | |||
66 | if 0, | ||
67 | |||
68 | fname = 130042; | ||
69 | for j=0:30, | ||
70 | cm = sprintf('!cp plaatje_data/%d_%d.pfm data/%d_%d.pfm ',fname,j,fname,j); | ||
71 | disp(cm);eval(cm); | ||
72 | end | ||
73 | |||
74 | %%%%%%%% | ||
75 | fnout = '130042';fn_t = '130042'; | ||
76 | cm = sprintf('!cp data/%s_eigval.pfm %s_eigval.pfm',fnout,fn_t); | ||
77 | disp(cm);eval(cm); | ||
78 | cm = sprintf('!cp data/%s_eigvec.pfm %s_eigvec.pfm',fnout,fn_t); | ||
79 | disp(cm);eval(cm); | ||
80 | cm = sprintf('!cp data/%s_ncutvec.pfm %s_ncutvec.pfm',fnout,fn_t); | ||
81 | disp(cm);eval(cm); | ||
82 | cm = sprintf('!cp data/%s_ncutval.pfm %s_ncutval.pfm',fnout,fn_t); | ||
83 | disp(cm);eval(cm); | ||
84 | |||
85 | |||
86 | |||
87 | |||
88 | end | ||
89 | |||
90 | disp_flag = 0; | ||
91 | if disp_flag, | ||
92 | [I1,bnr,bnc] = proj_back_id(ncutv(:,2),gcs,gce,grs,gre); | ||
93 | imvs(I,I1>0.002,bnr,bnc); | ||
94 | end | ||
95 | |||
96 | if 0, | ||
97 | |||
98 | nv = 3; | ||
99 | A = eigv(:,1:nv)*eigv(:,1:nv)'; | ||
100 | [v,d] = ncut(abs(A),min(nv,5)); | ||
101 | |||
102 | figure(3); | ||
103 | for j=1:min(nv,5), | ||
104 | subplot(2,2,j); | ||
105 | ims(v(:,j),nr,nc); | ||
106 | end | ||
107 | |||
108 | |||
109 | %%%%%%%% | ||
110 | |||
111 | figure(4);%im(I);colorbar; | ||
112 | hw = 3;st_sz = 2*hw+1; | ||
113 | ct = round(ginput(1)); | ||
114 | ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
115 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
116 | |||
117 | idx = (ct_chank(:,1)-1)*nr + ct_chank(:,2); | ||
118 | |||
119 | figure(5);im(abs(reshape(A(idx,:),nr,nc)));%colorbar; | ||
120 | |||
121 | |||
122 | |||
123 | %%%%% | ||
124 | |||
125 | fname = 'test2'; | ||
126 | fn = sprintf('data/ncut_%s.pfm',fname); | ||
127 | ncutv1 = readpfm(fn); | ||
128 | nr = 30; nc=49; | ||
129 | |||
130 | figure(1); | ||
131 | for j=1:min(4,size(ncutv1,2)), | ||
132 | subplot(2,2,j); | ||
133 | ims(ncutv1(:,j+1),nr,nc); | ||
134 | end | ||
135 | |||
136 | |||
137 | |||
138 | %%%%%%%%%% | ||
139 | |||
140 | id = 0; | ||
141 | fn = sprintf('data/test_Aa%d.pfm',id); | ||
142 | disp(sprintf('A = readpfm(%s);',fn)); | ||
143 | A = readpfm(fn); | ||
144 | |||
145 | cm = sprintf('[v%d,d%d] = eigs(A,12);',id,id); | ||
146 | disp(cm);eval(cm); | ||
147 | |||
148 | writepfm('test_eigv0.pfm',v0); | ||
149 | writepfm('test_eigva0.pfm',diag(d0)); | ||
150 | |||
151 | |||
152 | |||
153 | |||
154 | vs = zeros(size(v1,1),size(v1,2),6); | ||
155 | ds = zeros(length(d1),6); | ||
156 | |||
157 | for j=0:5, | ||
158 | cm = sprintf('vs(:,:,%d) = v%d;',j+1,j); | ||
159 | disp(cm);eval(cm); | ||
160 | cm = sprintf('d = diag(d%d);',j); | ||
161 | disp(cm);eval(cm); | ||
162 | cm = sprintf('ds(:,%d) = d(:);',j+1); | ||
163 | disp(cm);eval(cm); | ||
164 | |||
165 | |||
166 | end | ||
167 | |||
168 | %save evsum vs ds | ||
169 | |||
170 | figure(1);nr = 49;nc=30;evid = 3; | ||
171 | for j=1:12,subplot(3,4,j);ims(vs(:,j,evid),nr,nc);end | ||
172 | |||
173 | I = readpgm('images/334039.pgm');I = cutoff(I,20); | ||
174 | |||
175 | As = zeros(6,nr*nc); | ||
176 | |||
177 | figure(3);%im(I);colormap(gray); | ||
178 | hw = 3;st_sz = 2*hw+1; | ||
179 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1;ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
180 | idx = (ct_chank(:,1)-1)*nr + ct_chank(:,2); | ||
181 | |||
182 | figure(5); | ||
183 | |||
184 | figure(4);nvs = [6,9,12,12,12,12]; | ||
185 | for evid = 1:5,As(evid,:) = squeeze(vs(idx,1:nvs(evid),evid))*squeeze(vs(:,1:nvs(evid),evid))';end | ||
186 | for evid =1:5,subplot(2,3,evid);im(abs(reshape(As(evid,:),nr,nc)));colorbar;end | ||
187 | subplot(2,3,6);ims(sum(abs(As)),nr,nc);colorbar | ||
188 | |||
189 | %%%%%%%%% | ||
190 | |||
191 | %%%%%% eig of the As over all scales %% | ||
192 | |||
193 | A = zeros(nr*nc,nr*nc); | ||
194 | |||
195 | for evid=1:5, disp(evid); | ||
196 | A = A + abs(squeeze(vs(:,1:nvs(evid),evid))*squeeze(vs(:,1:nvs(evid),evid))'); | ||
197 | end | ||
198 | |||
199 | [v,d] = eigs(A,12); | ||
200 | figure(1); for j=1:12, subplot(3,4,j);ims(v(:,j),nr,nc);end | ||
201 | |||
202 | [vn,dn] = ncut_b(A,12); | ||
203 | figure(3); for j=1:12, subplot(3,4,j);ims(-vn(:,j),nr,nc);end | ||
204 | |||
205 | nv = 6; | ||
206 | A = abs(eigv(:,1:nv)*eigv(:,1:nv)'); | ||
207 | [v,d] = ncut_b(A,nv+1); | ||
208 | figure(1); | ||
209 | for j=1:nv,subplot(2,nv,j);ims(v(:,j+1),nr,nc);title(sprintf('%3.3e',d(j+1)));end | ||
210 | |||
211 | for j=1:nv,subplot(2,nv,j+nv);ims(eigv(:,j),nr,nc);title(sprintf('%3.3e',eigval(j,1)));end | ||
212 | |||
213 | |||
214 | |||
215 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_evresulthome.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_evresulthome.m new file mode 100755 index 0000000..208b86a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_evresulthome.m | |||
@@ -0,0 +1,237 @@ | |||
1 | fn = '334003'; | ||
2 | |||
3 | fname = sprintf('%s_eigvec.pfm',fn); | ||
4 | disp(sprintf('reading %s',fname)); | ||
5 | eigv = readpfm(fname); | ||
6 | fname = sprintf('%s_eigval.pfm',fn); | ||
7 | eigval = readpfm(fname); | ||
8 | |||
9 | fname = sprintf('%s_ncutvec.pfm',fn); | ||
10 | ncutv = readpfm(fname); | ||
11 | fname = sprintf('%s_ncutval.pfm',fn); | ||
12 | ncutval = readpfm(fname); | ||
13 | |||
14 | %fname = sprintf('images/130038.pgm'); | ||
15 | fname = sprintf('images/%s.pgm',fn); | ||
16 | I = readpgm(fname); | ||
17 | cutsz = 20; I = cutoff(I,cutsz); | ||
18 | figure(3);im(I);colormap(gray); | ||
19 | |||
20 | new = 0; | ||
21 | |||
22 | if new, | ||
23 | fn1 = fn; | ||
24 | fn = 'test'; | ||
25 | fname = sprintf('%s_gcs.pfm',fn); | ||
26 | gcs = readpfm(fname); | ||
27 | |||
28 | fname = sprintf('%s_gce.pfm',fn); | ||
29 | gce = readpfm(fname); | ||
30 | |||
31 | fname = sprintf('%s_grs.pfm',fn); | ||
32 | grs = readpfm(fname); | ||
33 | |||
34 | fname = sprintf('%s_gre.pfm',fn); | ||
35 | gre = readpfm(fname); | ||
36 | |||
37 | nr = max(gre(:))+1; | ||
38 | nc = max(gce(:))+1; | ||
39 | |||
40 | fn = fn1; | ||
41 | |||
42 | else | ||
43 | nr = 49;nc = 30; | ||
44 | %nr = 30;nc = 49; | ||
45 | |||
46 | end | ||
47 | |||
48 | figure(6); | ||
49 | for j=1:8, | ||
50 | subplot(3,3,j); | ||
51 | im(reshape(ncutv(:,j+1),nr,nc));colorbar | ||
52 | title(num2str(ncutval(j+1,1))); | ||
53 | end | ||
54 | %cm = sprintf('print -dps ncut_%s',fn);disp(cm);eval(cm); | ||
55 | %subplot(3,3,9);im(I);axis('off'); | ||
56 | |||
57 | figure(7);clf | ||
58 | for j=1:9, | ||
59 | subplot(3,3,j); | ||
60 | im(reshape(eigv(:,j),nr,nc));colorbar;%axis('off'); | ||
61 | title(sprintf('%3.4e',eigval(j,1))); | ||
62 | end | ||
63 | %cm = sprintf('print -dps eig_%s',fn);disp(cm);eval(cm); | ||
64 | |||
65 | |||
66 | |||
67 | if 0, | ||
68 | |||
69 | fname = 130042; | ||
70 | for j=0:30, | ||
71 | cm = sprintf('!cp plaatje_data/%d_%d.pfm data/%d_%d.pfm ',fname,j,fname,j); | ||
72 | disp(cm);eval(cm); | ||
73 | end | ||
74 | |||
75 | %%%%%%%% | ||
76 | fnout = '130042';fn_t = '130042'; | ||
77 | cm = sprintf('!cp data/%s_eigval.pfm %s_eigval.pfm',fnout,fn_t); | ||
78 | disp(cm);eval(cm); | ||
79 | cm = sprintf('!cp data/%s_eigvec.pfm %s_eigvec.pfm',fnout,fn_t); | ||
80 | disp(cm);eval(cm); | ||
81 | cm = sprintf('!cp data/%s_ncutvec.pfm %s_ncutvec.pfm',fnout,fn_t); | ||
82 | disp(cm);eval(cm); | ||
83 | cm = sprintf('!cp data/%s_ncutval.pfm %s_ncutval.pfm',fnout,fn_t); | ||
84 | disp(cm);eval(cm); | ||
85 | |||
86 | |||
87 | |||
88 | |||
89 | end | ||
90 | |||
91 | disp_flag = 0; | ||
92 | if disp_flag, | ||
93 | [I1,bnr,bnc] = proj_back_id(ncutv(:,2),gcs,gce,grs,gre); | ||
94 | imvs(I,I1>0.002,bnr,bnc); | ||
95 | end | ||
96 | |||
97 | if 0, | ||
98 | |||
99 | nv = 3; | ||
100 | A = eigv(:,1:nv)*eigv(:,1:nv)'; | ||
101 | [v,d] = ncut(abs(A),min(nv,5)); | ||
102 | |||
103 | figure(3); | ||
104 | for j=1:min(nv,5), | ||
105 | subplot(2,2,j); | ||
106 | ims(v(:,j),nr,nc); | ||
107 | end | ||
108 | |||
109 | |||
110 | %%%%%%%% | ||
111 | |||
112 | figure(4);%im(I);colorbar; | ||
113 | hw = 3;st_sz = 2*hw+1; | ||
114 | ct = round(ginput(1)); | ||
115 | ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
116 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
117 | |||
118 | idx = (ct_chank(:,1)-1)*nr + ct_chank(:,2); | ||
119 | |||
120 | figure(5);im(abs(reshape(A(idx,:),nr,nc)));%colorbar; | ||
121 | |||
122 | |||
123 | |||
124 | %%%%% | ||
125 | |||
126 | fname = 'test2'; | ||
127 | fn = sprintf('data/ncut_%s.pfm',fname); | ||
128 | ncutv1 = readpfm(fn); | ||
129 | nr = 30; nc=49; | ||
130 | |||
131 | figure(1); | ||
132 | for j=1:min(4,size(ncutv1,2)), | ||
133 | subplot(2,2,j); | ||
134 | ims(ncutv1(:,j+1),nr,nc); | ||
135 | end | ||
136 | |||
137 | |||
138 | |||
139 | %%%%%%%%%% | ||
140 | |||
141 | id = 0; | ||
142 | fn = sprintf('test_Aa%d.pfm',id); | ||
143 | disp(sprintf('A = readpfm(%s);',fn)); | ||
144 | A = readpfm(fn); | ||
145 | |||
146 | cm = sprintf('[v%d,d%d] = eigs(A,12);',id,id); | ||
147 | disp(cm);eval(cm); | ||
148 | |||
149 | writepfm('test_eigv0.pfm',v0); | ||
150 | writepfm('test_eigva0.pfm',diag(d0)); | ||
151 | |||
152 | |||
153 | |||
154 | |||
155 | vs = zeros(size(v1,1),size(v1,2),6); | ||
156 | ds = zeros(length(d1),6); | ||
157 | |||
158 | for j=0:5, | ||
159 | cm = sprintf('vs(:,:,%d) = v%d;',j+1,j); | ||
160 | disp(cm);eval(cm); | ||
161 | cm = sprintf('d = diag(d%d);',j); | ||
162 | disp(cm);eval(cm); | ||
163 | cm = sprintf('ds(:,%d) = d(:);',j+1); | ||
164 | disp(cm);eval(cm); | ||
165 | |||
166 | |||
167 | end | ||
168 | |||
169 | %save evsum vs ds | ||
170 | |||
171 | figure(1);nr = 49;nc=30;evid = 3; | ||
172 | for j=1:12,subplot(3,4,j);ims(vs(:,j,evid),nr,nc);end | ||
173 | |||
174 | I = readpgm('images/334039.pgm');I = cutoff(I,20); | ||
175 | |||
176 | As = zeros(6,nr*nc); | ||
177 | |||
178 | figure(3);%im(I);colormap(gray); | ||
179 | hw = 3;st_sz = 2*hw+1; | ||
180 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1;ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
181 | idx = (ct_chank(:,1)-1)*nr + ct_chank(:,2); | ||
182 | |||
183 | figure(5); | ||
184 | |||
185 | figure(4);nvs = [6,9,12,12,12,12]; | ||
186 | for evid = 1:5,As(evid,:) = squeeze(vs(idx,1:nvs(evid),evid))*squeeze(vs(:,1:nvs(evid),evid))';end | ||
187 | for evid =1:5,subplot(2,3,evid);im(abs(reshape(As(evid,:),nr,nc)));colorbar;end | ||
188 | subplot(2,3,6);ims(sum(abs(As)),nr,nc);colorbar | ||
189 | |||
190 | %%%%%%%%% | ||
191 | |||
192 | %%%%%% eig of the As over all scales %% | ||
193 | |||
194 | A = zeros(nr*nc,nr*nc); | ||
195 | |||
196 | for evid=1:5, disp(evid); | ||
197 | A = A + abs(squeeze(vs(:,1:nvs(evid),evid))*squeeze(vs(:,1:nvs(evid),evid))'); | ||
198 | end | ||
199 | |||
200 | [v,d] = eigs(A,12); | ||
201 | figure(1); for j=1:12, subplot(3,4,j);ims(v(:,j),nr,nc);end | ||
202 | |||
203 | [vn,dn] = ncut_b(A,12); | ||
204 | figure(3); for j=1:12, subplot(3,4,j);ims(-vn(:,j),nr,nc);end | ||
205 | |||
206 | nv = 6; | ||
207 | A = abs(eigv(:,1:nv)*eigv(:,1:nv)'); | ||
208 | [v,d] = ncut_b(A,nv+1); | ||
209 | figure(1); | ||
210 | for j=1:nv,subplot(2,nv,j);ims(v(:,j+1),nr,nc);title(sprintf('%3.3e',d(j+1)));end | ||
211 | |||
212 | for j=1:nv,subplot(2,nv,j+nv);ims(eigv(:,j),nr,nc);title(sprintf('%3.3e',eigval(j,1)));end | ||
213 | |||
214 | %%%%%%%%%%%%%%%%% | ||
215 | |||
216 | while 1, | ||
217 | figure(3);%im(I);colormap(gray); | ||
218 | hw = 3;st_sz = 2*hw+1; | ||
219 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1;ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
220 | idx = (ct_chank(:,1)-1)*nr + ct_chank(:,2); | ||
221 | |||
222 | figure(1); | ||
223 | ims(exp(-(A(idx,:))/(0.03^2)),nr,nc);colorbar | ||
224 | end | ||
225 | |||
226 | disp_evresulthome; | ||
227 | close(3);close(7);close(6); | ||
228 | A = euclid_dist(ncutv(:,2:6)); | ||
229 | A = exp(-A/(0.05^2)); | ||
230 | |||
231 | [v,d] = eigs(A,9); | ||
232 | |||
233 | figure(2); | ||
234 | for j=1:9,subplot(3,3,j);ims(v(:,j),nr,nc);colorbar;end | ||
235 | |||
236 | |||
237 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_groups.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_groups.m new file mode 100755 index 0000000..d087218 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_groups.m | |||
@@ -0,0 +1,13 @@ | |||
1 | function disp_groups(groups,ids,nr,nc); | ||
2 | |||
3 | np = ids(end); | ||
4 | |||
5 | baseid =1; | ||
6 | for j=1:length(ids), | ||
7 | mask = zeros(np,1); | ||
8 | mask(groups(baseid:ids(j))) = 1+mask(groups(baseid:ids(j))); | ||
9 | |||
10 | subplot(3,3,j); | ||
11 | ims(mask,nr,nc); | ||
12 | baseid = 1+ids(j); | ||
13 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_hist2d.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_hist2d.m new file mode 100755 index 0000000..4313234 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/disp_hist2d.m | |||
@@ -0,0 +1,15 @@ | |||
1 | function H2 = disp_hist2d(J,Jf,scales,filter_ids) | ||
2 | |||
3 | ns = length(scales); | ||
4 | nf = length(filter_ids); | ||
5 | |||
6 | H2 = []; | ||
7 | for j=1:ns, | ||
8 | for k=1:nf, | ||
9 | subplot(ns,nf,(j-1)*nf+k); | ||
10 | H2d = hist_I_f(J,Jf(:,:,filter_ids(k),scales(j))); | ||
11 | imagesc(H2d);axis('image');axis('off');drawnow; | ||
12 | H2(:,:,j,k) = H2d; | ||
13 | end | ||
14 | end | ||
15 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/dist_pair.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/dist_pair.m new file mode 100755 index 0000000..1c7b2cf --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/dist_pair.m | |||
@@ -0,0 +1,28 @@ | |||
1 | function d = dist_pair(idx,fv,hb) | ||
2 | % (hb=sigs,bin_mins,bin_maxs,nbins) | ||
3 | % | ||
4 | % | ||
5 | % computes the pairwise distance between | ||
6 | % a point and everyone else using histogram binized feature | ||
7 | % | ||
8 | |||
9 | |||
10 | [nf,np] = size(fv); | ||
11 | |||
12 | d = zeros(1,np); | ||
13 | nbins = [0,hb.nbins]; | ||
14 | |||
15 | |||
16 | for j=1:nf, | ||
17 | bin_min = hb.bmins(j); | ||
18 | bin_max = hb.bmaxs(j); | ||
19 | nbin = nbins(j+1); | ||
20 | sig = hb.sigs(j); | ||
21 | fprintf(sprintf('|%d',j)); | ||
22 | b = binize(fv(j,:),sig,bin_min,bin_max,nbin); | ||
23 | |||
24 | a = binize(fv(j,idx),sig,bin_min,bin_max,nbin); | ||
25 | |||
26 | d = d + a'*b; | ||
27 | end | ||
28 | fprintf('\n'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/dist_pair_chank.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/dist_pair_chank.m new file mode 100755 index 0000000..96a0d60 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/dist_pair_chank.m | |||
@@ -0,0 +1,25 @@ | |||
1 | function d = dist_pair_chank(a,fvs,chank_size) | ||
2 | % (hb=sigs,bin_mins,bin_maxs,nbins) | ||
3 | % | ||
4 | % | ||
5 | % computes the pairwise distance between | ||
6 | % a point and everyone else using histogram binized feature | ||
7 | % | ||
8 | |||
9 | |||
10 | [nf,np] = size(fvs); | ||
11 | |||
12 | n_chanks = ceil(np/chank_size); | ||
13 | |||
14 | d = []; | ||
15 | for j=1:n_chanks, | ||
16 | fprintf('<'); | ||
17 | |||
18 | cm = sprintf('load st_%d',j); | ||
19 | eval(cm); | ||
20 | fprintf(sprintf('%d',n_chanks-j)); | ||
21 | |||
22 | d = [d,a*fh]; | ||
23 | end | ||
24 | |||
25 | fprintf('\n'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/doog2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/doog2.m new file mode 100755 index 0000000..3ec22c1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/doog2.m | |||
@@ -0,0 +1,43 @@ | |||
1 | function [G]=doog2(sig,r,th,N); | ||
2 | % [G,H]=doog2(sig,r,th,N); | ||
3 | % Make difference of offset gaussians kernel | ||
4 | % theta is in degrees | ||
5 | % (see Malik & Perona, J. Opt. Soc. Amer., 1990) | ||
6 | % | ||
7 | % Example: | ||
8 | % >> imagesc(-doog2(.5,4,15,32)) | ||
9 | % >> colormap(gray) | ||
10 | |||
11 | % by Serge Belongie | ||
12 | |||
13 | no_pts=N; % no. of points in x,y grid | ||
14 | pad_pts=no_pts*sqrt(2); % pad grid dimensions for up to a 45 degree rotation | ||
15 | siz=6; % range of x,y grid | ||
16 | |||
17 | [x,y]=meshgrid(linspace(-siz,siz,pad_pts),linspace(-siz,siz,pad_pts)); | ||
18 | |||
19 | a=-1; | ||
20 | b=2; | ||
21 | c=-1; | ||
22 | |||
23 | ya=sig; | ||
24 | yc=-ya; | ||
25 | yb=0; | ||
26 | sigy=sig; | ||
27 | sigx=r*sig; | ||
28 | |||
29 | Ga=(1/(2*pi*sigx*sigy))*exp(-(((x-0)/sigx).^2+((y-ya)/sigy).^2)); | ||
30 | Gb=(1/(2*pi*sigx*sigy))*exp(-(((x-0)/sigx).^2+((y-yb)/sigy).^2)); | ||
31 | Gc=(1/(2*pi*sigx*sigy))*exp(-(((x-0)/sigx).^2+((y-yc)/sigy).^2)); | ||
32 | |||
33 | Go = a*Ga + b*Gb + c*Gc; | ||
34 | %Ho = imag(hilbert(Go)); | ||
35 | G = Go; | ||
36 | |||
37 | G = mimrotate(Go,th,'bilinear','crop'); | ||
38 | G = imcrop(G,[(pad_pts-no_pts)/2, (pad_pts-no_pts)/2, no_pts, no_pts]); | ||
39 | |||
40 | %H = imrotate(Ho,th,'bilinear','crop'); | ||
41 | %H = imcrop(H,[(pad_pts-no_pts)/2, (pad_pts-no_pts)/2, no_pts, no_pts]); | ||
42 | |||
43 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eig_decomp.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eig_decomp.m new file mode 100755 index 0000000..5209088 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eig_decomp.m | |||
@@ -0,0 +1,15 @@ | |||
1 | function [v,d] = eig_decomp(A) | ||
2 | |||
3 | ds = sum(A); | ||
4 | ds = ones(size(ds))./sqrt(ds); | ||
5 | D1 = ds'*ones(1,length(ds)); | ||
6 | A = D1'.*A.*D1; | ||
7 | |||
8 | disp(sprintf('computing eig values')); | ||
9 | tic;[v,d] = eig(A);toc; | ||
10 | |||
11 | d = abs(diag(d)); | ||
12 | [tmp,idx] = sort(-d); | ||
13 | d = d(idx); | ||
14 | v = v(:,idx); | ||
15 | v = D1.*v; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eig_decomp_v5.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eig_decomp_v5.m new file mode 100755 index 0000000..e0fab2c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eig_decomp_v5.m | |||
@@ -0,0 +1,13 @@ | |||
1 | function [v,d] = eig_decomp_v5(A,nv) | ||
2 | |||
3 | ds = sum(A); | ||
4 | ds = ones(size(ds))./sqrt(ds); | ||
5 | D1 = ds'*ones(1,length(ds)); | ||
6 | A = D1'.*A.*D1; | ||
7 | |||
8 | disp(sprintf('computing eig values')); | ||
9 | tic;[v,d] = eigs(A,nv);toc; | ||
10 | |||
11 | d = abs(diag(d)); | ||
12 | |||
13 | v = D1(:,1:size(v,2)).*v; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eig_proj.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eig_proj.m new file mode 100755 index 0000000..78d5296 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eig_proj.m | |||
@@ -0,0 +1,12 @@ | |||
1 | function v = eig_proj(u,data) | ||
2 | |||
3 | % fd = feature dimension, nv = num. of eigvectors | ||
4 | [fd,nv] = size(u); | ||
5 | |||
6 | [fd2,nd] = size(data); | ||
7 | |||
8 | if (fd ~= fd2), | ||
9 | error(sprintf('size don't match')); | ||
10 | else | ||
11 | v = data'*u; | ||
12 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eigs_decomp.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eigs_decomp.m new file mode 100755 index 0000000..7763124 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/eigs_decomp.m | |||
@@ -0,0 +1,39 @@ | |||
1 | function [v,d,D,Ipara] = eigs_decomp(fn,num_eigs) | ||
2 | % | ||
3 | % function [v,d,D,Ipara] = eigs_decomp(fn,num_eigs) | ||
4 | % | ||
5 | |||
6 | %fn = '2.ppm'; | ||
7 | fn = 'images/130049.pgm'; | ||
8 | |||
9 | |||
10 | % spatial gaussian parameter | ||
11 | xscale = 3; | ||
12 | |||
13 | % half size of the neighbourhood | ||
14 | xnb = 6; | ||
15 | |||
16 | % setting the the HSV gaussian parameter:[h s v] | ||
17 | Iscale = [0.008,0.01,0.01]; | ||
18 | |||
19 | Input_para = [xscale,xnb,Iscale]; | ||
20 | |||
21 | % compute the lower half the association matrix | ||
22 | [A,D,Ipara] = compute_A_ppm(fn,Input_para); | ||
23 | |||
24 | B = A+A'; | ||
25 | clear A; | ||
26 | |||
27 | % eigen decompostion | ||
28 | options.tol = 1e-7; | ||
29 | num_eig_v = 4; | ||
30 | fprintf('doing eigs ...\n'); | ||
31 | [v,d] = eigs(B,num_eig_v,options); | ||
32 | |||
33 | d = diag(d); | ||
34 | |||
35 | % to display the final result | ||
36 | |||
37 | %nr = Ipara(1);nc = Ipara(2); | ||
38 | %k = 1;imagesc(reshape(v(:,k).*D,nc,nr)');colorbar | ||
39 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/euclid_dist.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/euclid_dist.m new file mode 100755 index 0000000..cbf0734 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/euclid_dist.m | |||
@@ -0,0 +1,22 @@ | |||
1 | function [A,mag] = euclid_dist(v) | ||
2 | |||
3 | |||
4 | |||
5 | A = 2*v*v'; | ||
6 | |||
7 | nv = size(v,2); | ||
8 | if (nv>1) | ||
9 | mag = sum((v.*v)')'; | ||
10 | else | ||
11 | mag = v.*v; | ||
12 | end | ||
13 | |||
14 | np = length(mag); | ||
15 | |||
16 | for j=1:np, | ||
17 | A(:,j) = mag-A(:,j); | ||
18 | end | ||
19 | |||
20 | for j=1:np, | ||
21 | A(j,:) = mag' + A(j,:); | ||
22 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/filter_all_files.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/filter_all_files.m new file mode 100755 index 0000000..c1752e5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/filter_all_files.m | |||
@@ -0,0 +1,29 @@ | |||
1 | sigs = [1/sqrt(2),1,sqrt(2),2,2*sqrt(2)];r = 3;szs = round(r*3*sigs); | ||
2 | |||
3 | load filenames; | ||
4 | |||
5 | nfiles = size(filename,1); | ||
6 | |||
7 | for j = 48:nfiles, | ||
8 | fname = ['images/',filename(j,:)]; | ||
9 | fname | ||
10 | I = readpgm(fname); | ||
11 | |||
12 | text_des = compute_filter(I,sigs,r,szs); | ||
13 | |||
14 | data_name = sprintf('filter_%s.mat',filename(j,:)); | ||
15 | cm = sprintf('save %s ',data_name); | ||
16 | |||
17 | disp(cm); | ||
18 | eval(cm); | ||
19 | clear; | ||
20 | |||
21 | sigs = [1/sqrt(2),1,sqrt(2),2,2*sqrt(2)];r = 3;szs = round(r*3*sigs); | ||
22 | |||
23 | load filenames; | ||
24 | |||
25 | nfiles = size(filename,1); | ||
26 | |||
27 | |||
28 | end | ||
29 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/filter_output.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/filter_output.m new file mode 100755 index 0000000..a489c6e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/filter_output.m | |||
@@ -0,0 +1,38 @@ | |||
1 | function If = filter_output(I,sigs,szs,flag); | ||
2 | % | ||
3 | % compute filter output for all orientation and scale, | ||
4 | % | ||
5 | |||
6 | %% flag = 1 if compute oriented filter output | ||
7 | if (~exist('flag')), | ||
8 | flag = 1; | ||
9 | end | ||
10 | |||
11 | |||
12 | If = []; | ||
13 | |||
14 | for j = 1:length(sigs), | ||
15 | sig = sigs(j); | ||
16 | sz = 2*round(4*sig)+1; | ||
17 | |||
18 | g = mkdog1(sig,sz); | ||
19 | fprintf('['); | ||
20 | fprintf('.'); | ||
21 | If(:,:,1,j) = conv2(I,g,'same'); | ||
22 | |||
23 | angles = [0:30:150]; | ||
24 | r = 3; | ||
25 | |||
26 | if flag, | ||
27 | for k = 1:length(angles), | ||
28 | fprintf('.'); | ||
29 | g = mdoog2(sig,r,angles(k),szs(j)); | ||
30 | If(:,:,k+1,j) = conv2(I,g,'same'); | ||
31 | end | ||
32 | end | ||
33 | |||
34 | fprintf(']'); | ||
35 | |||
36 | end | ||
37 | |||
38 | fprintf('\n'); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/find_bst_cut.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/find_bst_cut.m new file mode 100755 index 0000000..c85cc2b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/find_bst_cut.m | |||
@@ -0,0 +1,24 @@ | |||
1 | function [cut_threshold,max_asso] = find_bst_cut(fn_base,para,threshold,Gmask) | ||
2 | |||
3 | basedir = 'plaatje_data/'; | ||
4 | %basedir = './'; | ||
5 | |||
6 | fn = sprintf('%sbst_cut.tex',basedir); | ||
7 | write_command(fn,fn_base,para); | ||
8 | |||
9 | fn= sprintf('%sthreshold_%s.pfm',basedir,fn_base); | ||
10 | writepfm(fn,threshold(:)); | ||
11 | |||
12 | fn= sprintf('%sGmask_%s.pfm',basedir,fn_base); | ||
13 | writepfm(fn,Gmask(:)); | ||
14 | |||
15 | cd plaatje_data | ||
16 | !./find_bestcut | ||
17 | cd /home/barad-dur/vision/malik/jshi/proj/grouping/texture | ||
18 | |||
19 | fn = sprintf('%sbst_asso_%s.pfm',basedir,fn_base); | ||
20 | results = readpfm(fn); | ||
21 | asso = results(1,:); | ||
22 | [max_asso,id] = max(asso); | ||
23 | cut_threshold = threshold(id); | ||
24 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/find_center.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/find_center.m new file mode 100755 index 0000000..b5a127a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/find_center.m | |||
@@ -0,0 +1,4 @@ | |||
1 | function [center_x,center_y] = find_center(size_x,size_y); | ||
2 | |||
3 | center_x = 0.5*(size_x -1)+1; | ||
4 | center_y = 0.5*(size_y -1)+1; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/find_cutpoint.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/find_cutpoint.m new file mode 100755 index 0000000..5cab956 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/find_cutpoint.m | |||
@@ -0,0 +1,13 @@ | |||
1 | function [cutpoints,x] = find_cutpoint(data,cmap,nbin) | ||
2 | % | ||
3 | % [cutpoints,x] = find_cutpoint(data,cmap,nbin) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | x = id_cut(data,cmap,nbin); | ||
8 | |||
9 | cutpoints = zeros(1,nbin); | ||
10 | |||
11 | for j=1:nbin, | ||
12 | cutpoints(j) = max(data(x<=j)); | ||
13 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/gen_filters.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/gen_filters.m new file mode 100755 index 0000000..65e3c3a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/gen_filters.m | |||
@@ -0,0 +1,47 @@ | |||
1 | function filters = gen_filters(sig,r,sz); | ||
2 | |||
3 | |||
4 | as = 0:30:150; | ||
5 | |||
6 | filters = []; | ||
7 | |||
8 | if size(sig,2)== 1, | ||
9 | |||
10 | for j = 1:length(as), | ||
11 | fprintf('.'); | ||
12 | angle = as(j); | ||
13 | |||
14 | g = mdoog2(sig,r,angle,round(sz)); | ||
15 | |||
16 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
17 | |||
18 | g = g/sum(sum(abs(g))); | ||
19 | |||
20 | filters(:,:,j) = g; | ||
21 | end | ||
22 | else | ||
23 | |||
24 | % there are multiple scales | ||
25 | sigs = sig; | ||
26 | szs = sz; | ||
27 | for k = 1:size(sigs,2), | ||
28 | sig = sigs(k); | ||
29 | sz = szs(length(szs)-1); | ||
30 | fprintf('%d',k); | ||
31 | for j = 1:length(as), | ||
32 | fprintf('.'); | ||
33 | angle = as(j); | ||
34 | |||
35 | g = mdoog2(sig,r,angle,round(sz)); | ||
36 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
37 | g = g/sum(sum(abs(g))); | ||
38 | |||
39 | filters(:,:,j,k) = g; | ||
40 | end | ||
41 | |||
42 | |||
43 | end | ||
44 | |||
45 | end | ||
46 | |||
47 | fprintf('\n'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_cumhist.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_cumhist.m new file mode 100755 index 0000000..b05d680 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_cumhist.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function cumhists = get_cumhist(hists) | ||
2 | % | ||
3 | % | ||
4 | % cumhists = get_cumhist(hists) | ||
5 | % | ||
6 | |||
7 | cumhists.inten = cumsum(hists.inten)/sum(hists.inten); | ||
8 | cumhists.text = cumsum(hists.text,1)./(ones(size(hists.text,1),1)*sum(hists.text,1)); | ||
9 | cumhists.mag = cumsum(hists.mag)/sum(hists.mag); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_cumhist_inten.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_cumhist_inten.m new file mode 100755 index 0000000..911b4e6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_cumhist_inten.m | |||
@@ -0,0 +1,7 @@ | |||
1 | function CH_inten = get_cumhist(hists) | ||
2 | % | ||
3 | % | ||
4 | % cumhists = get_cumhist(hists) | ||
5 | % | ||
6 | |||
7 | CH_inten = cumsum(hists)/sum(hists); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_hist.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_hist.m new file mode 100755 index 0000000..d480a3a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_hist.m | |||
@@ -0,0 +1,24 @@ | |||
1 | function [hists,bins] = get_hists(J,Jbar) | ||
2 | % | ||
3 | % | ||
4 | % produce histogram output of the image J and its | ||
5 | % filter outputs Jbar | ||
6 | % | ||
7 | |||
8 | maxval = 60; | ||
9 | bin = [1:4:maxval+1]; | ||
10 | |||
11 | w = size(J); | ||
12 | |||
13 | |||
14 | [hists.inten,bins.inten] = hist(reshape(J,prod(w),1),[1:26:256]); | ||
15 | |||
16 | for j=1:size(Jbar,3), | ||
17 | hists.text(:,j) = hist(reshape(abs(Jbar(:,:,j)),prod(w),1),bin); | ||
18 | end | ||
19 | |||
20 | bins.text = bin; | ||
21 | |||
22 | [hists.mag,bins.mag] = hist(reshape(sum(abs(Jbar),3),prod(w),1),[1:10:161]); | ||
23 | |||
24 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_hist_inten.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_hist_inten.m new file mode 100755 index 0000000..c8357c6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_hist_inten.m | |||
@@ -0,0 +1,15 @@ | |||
1 | function [Hinten,Hbins] = get_hists_inten(J,nbin) | ||
2 | % | ||
3 | % | ||
4 | % produce histogram output of the image J and its | ||
5 | % filter outputs Jbar | ||
6 | % | ||
7 | |||
8 | |||
9 | w = size(J); | ||
10 | |||
11 | [Hinten,Hbins] = hist(reshape(J,prod(w),1),linspace(1,256,nbin)); | ||
12 | |||
13 | |||
14 | |||
15 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_win.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_win.m new file mode 100755 index 0000000..411a694 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_win.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function J = get_win(I,center,wc) | ||
2 | % | ||
3 | % J = get_win(I,center,wc) | ||
4 | % | ||
5 | % center: [x,y] | ||
6 | |||
7 | |||
8 | |||
9 | J = I(center(2)-wc(2):center(2)+wc(2),... | ||
10 | center(1)-wc(1):center(1)+wc(1)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_win5.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_win5.m new file mode 100755 index 0000000..e8404e5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/get_win5.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function J = get_win5(I,center,wc) | ||
2 | % | ||
3 | % J = get_win5(I,center,wc) | ||
4 | % | ||
5 | % center: [x,y] | ||
6 | |||
7 | |||
8 | |||
9 | J = I(center(2)-wc(2):center(2)+wc(2),... | ||
10 | center(1)-wc(1):center(1)+wc(1),:,:); | ||
11 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/grad.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/grad.m new file mode 100755 index 0000000..6da0fbf --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/grad.m | |||
@@ -0,0 +1,24 @@ | |||
1 | % gradient of an image | ||
2 | % coordinates (r, c) follow matrix convention; | ||
3 | % the gaussian is truncated at x = +- tail, and there are samples samples | ||
4 | % inbetween, where samples = hsamples * 2 + 1 | ||
5 | |||
6 | function[gr,gc] = gradient(image, hsamples) | ||
7 | |||
8 | tail=4; | ||
9 | samples = hsamples * 2 + 1; | ||
10 | |||
11 | x = linspace(-tail, tail, samples); | ||
12 | gauss = exp(-x.^2); | ||
13 | n = gauss * ones(samples,1); | ||
14 | gauss = gauss/n; | ||
15 | |||
16 | gaussderiv = -x.*gauss; | ||
17 | n = -gaussderiv*linspace(1,samples,samples)'; | ||
18 | gaussderiv = gaussderiv/n; | ||
19 | |||
20 | gr = conv2(conv2(image, gaussderiv','valid'), gauss,'valid'); | ||
21 | gc = conv2(conv2(image, gaussderiv,'valid'), gauss','valid'); | ||
22 | |||
23 | %gr = conv_trim(gr, hsamples, hsamples); | ||
24 | %gc = conv_trim(gc, hsamples, hsamples); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/half_sigmoid.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/half_sigmoid.m new file mode 100755 index 0000000..c187b6c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/half_sigmoid.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function a = half_sigmoid(x,offset,sig) | ||
2 | % | ||
3 | % a = half_sigmoid(x,offset,sig) | ||
4 | % | ||
5 | % a = ones(size(x))./(1+exp(-(x-offset)/sig)); | ||
6 | % | ||
7 | % keep the sign of a | ||
8 | |||
9 | sign_x = sign(x); | ||
10 | x = abs(x); | ||
11 | |||
12 | a = ones(size(x))./(1+exp(-(x-offset)/sig)); | ||
13 | |||
14 | off = 1/(1+exp(-(0-offset)/sig)); | ||
15 | |||
16 | a = sign_x.*(a-off); | ||
17 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist2d.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist2d.m new file mode 100755 index 0000000..3f4db0c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist2d.m | |||
@@ -0,0 +1,13 @@ | |||
1 | function H2 = hist2d(J,Jf,scales,filter_ids) | ||
2 | |||
3 | ns = length(scales); | ||
4 | nf = length(filter_ids); | ||
5 | |||
6 | H2 = []; | ||
7 | for j=1:ns, | ||
8 | for k=1:nf, | ||
9 | H2d = hist_I_f(J,Jf(:,:,filter_ids(k),scales(j))); | ||
10 | H2(:,:,j,k) = H2d; | ||
11 | end | ||
12 | end | ||
13 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_I_f.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_I_f.m new file mode 100755 index 0000000..a993661 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_I_f.m | |||
@@ -0,0 +1,22 @@ | |||
1 | function h2d = hist_I_f(I,If,binI,binf) | ||
2 | |||
3 | if (nargin == 2), | ||
4 | binI = [0:13:260]; | ||
5 | binf = [-30:2.5*2:30]; | ||
6 | end | ||
7 | |||
8 | %%% make 2d histogram bin | ||
9 | h2d = []; | ||
10 | |||
11 | for j = 2:length(binf), | ||
12 | |||
13 | [id_i,id_j] = find((If>binf(j-1)) & (If<=binf(j))); | ||
14 | if (length(id_i) >0), | ||
15 | h = hist(I(id_i+(id_j-1)*size(I,1)),binI); | ||
16 | else | ||
17 | h = zeros(size(binI)); | ||
18 | end | ||
19 | |||
20 | h2d = [h2d,h']; | ||
21 | end | ||
22 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_diff.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_diff.m new file mode 100755 index 0000000..6976c8f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_diff.m | |||
@@ -0,0 +1,30 @@ | |||
1 | function hdiff = hist_diff(H1,H2) | ||
2 | % | ||
3 | % hdiff = hist_diff(H1,H2) | ||
4 | % | ||
5 | |||
6 | ns = size(H1,3); | ||
7 | nf = size(H1,4); | ||
8 | |||
9 | sI= [1,0,1];sI = exp(-sI); | ||
10 | sI = sI/sum(sI); | ||
11 | |||
12 | hdiff = 0; | ||
13 | for j = 1:ns, | ||
14 | for k = 1:nf, | ||
15 | h1 = H1(:,:,j,k); | ||
16 | h2 = H2(:,:,j,k); | ||
17 | |||
18 | h1s = conv2(conv2(h1,sI','same'),sI,'same'); | ||
19 | h2s = conv2(conv2(h2,sI','same'),sI,'same'); | ||
20 | |||
21 | [is,js] = find( (h1>0) | (h2>0)); | ||
22 | ids = (js-1)*size(h1,1) + is; | ||
23 | |||
24 | xdiffs = ((h1s(ids)-h2s(ids)).*(h1s(ids)-h2s(ids)))./(h1s(ids)+h2s(ids)); | ||
25 | hdiff = hdiff + sum(xdiffs); | ||
26 | |||
27 | end | ||
28 | end | ||
29 | |||
30 | hdiff = hdiff/(ns*nf); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_f.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_f.m new file mode 100755 index 0000000..93f50a9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_f.m | |||
@@ -0,0 +1,28 @@ | |||
1 | function h2d = hist_f(Ifs,f1,s1,f2,s2) | ||
2 | |||
3 | |||
4 | binf = [-30:2.5*2:30]; | ||
5 | |||
6 | |||
7 | %%% make 2d histogram bin | ||
8 | |||
9 | If1 = Ifs(:,:,f1,s1); | ||
10 | If2 = Ifs(:,:,f2,s2); | ||
11 | h2d = []; | ||
12 | |||
13 | binf(1) = -100; | ||
14 | binf(length(binf)) = 100; | ||
15 | |||
16 | for j = 2:length(binf), | ||
17 | |||
18 | [id_i,id_j] = find((If1>binf(j-1)) & (If1<=binf(j))); | ||
19 | if (length(id_i) >0), | ||
20 | |||
21 | h = hist(If2(id_i+(id_j-1)*size(If2,1)),binf); | ||
22 | else | ||
23 | h = zeros(size(binf)); | ||
24 | end | ||
25 | |||
26 | |||
27 | h2d = [h2d,h']; | ||
28 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_in_chank.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_in_chank.m new file mode 100755 index 0000000..74661b2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_in_chank.m | |||
@@ -0,0 +1,33 @@ | |||
1 | function covfh = hist_inner_chank(fv,chank_size,nbin) | ||
2 | % fh = hist_inner_chank(fv,hb,chank_file) | ||
3 | % | ||
4 | % (hb = bin_mins,bin_maxs,nbins) | ||
5 | % | ||
6 | % take which histogram value and turn it into histogram bin | ||
7 | % compute the inner product of the histogram bin features | ||
8 | % | ||
9 | |||
10 | [nf,np] = size(fv); | ||
11 | |||
12 | tbins = nf*nbin; | ||
13 | disp(sprintf('need matrix of %d x %d ',tbins,tbins)); | ||
14 | |||
15 | covfh = zeros(tbins,tbins); | ||
16 | |||
17 | n_chanks = ceil(np/chank_size); | ||
18 | for j=1:n_chanks, | ||
19 | fprintf('<'); | ||
20 | |||
21 | cm = sprintf('load st_%d',j); | ||
22 | eval(cm); | ||
23 | fprintf(sprintf('%d',n_chanks-j)); | ||
24 | |||
25 | %ms = mean(fh'); | ||
26 | %fh = fh- ms'*ones(1,size(fh,2)); | ||
27 | |||
28 | covfh = covfh + fh*fh'; | ||
29 | fprintf('>'); | ||
30 | end | ||
31 | |||
32 | fprintf('\n'); | ||
33 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_inner.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_inner.m new file mode 100755 index 0000000..6fd02b7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hist_inner.m | |||
@@ -0,0 +1,40 @@ | |||
1 | function fh = hist_inner(fv,hb) | ||
2 | % (hb = bin_mins,bin_maxs,nbins) | ||
3 | % | ||
4 | % take which histogram value and turn it into histogram bin | ||
5 | % compute the inner product of the histogram bin features | ||
6 | % | ||
7 | |||
8 | [nf,np] = size(fv); | ||
9 | |||
10 | nbins = [0,hb.nbins]; | ||
11 | |||
12 | disp(sprintf('need matrix of %d x %d ',sum(nbins),sum(nbins))); | ||
13 | |||
14 | fh = zeros(sum(nbins),sum(nbins)); | ||
15 | |||
16 | for j=1:nf, | ||
17 | bin_min = hb.bmins(j); | ||
18 | bin_max = hb.bmaxs(j); | ||
19 | nbin = nbins(j+1); | ||
20 | sig = hb.sigs(j); | ||
21 | fprintf('|'); | ||
22 | b0 = binize(fv(j,:),sig,bin_min,bin_max,nbin); | ||
23 | |||
24 | fh(sum(nbins(1:j))+1:sum(nbins(1:j+1)),sum(nbins(1:j))+1:sum(nbins(1:j+1))) = b0*b0'; | ||
25 | |||
26 | for k=j+1:nf, | ||
27 | bin_min = hb.bmins(k); | ||
28 | bin_max = hb.bmaxs(k); | ||
29 | nbin = nbins(k+1); | ||
30 | sig = hb.sigs(k); | ||
31 | fprintf('.'); | ||
32 | b = binize(fv(k,:),sig,bin_min,bin_max,nbin); | ||
33 | tmp = b0*b'; | ||
34 | |||
35 | fh(sum(nbins(1:j))+1:sum(nbins(1:j+1)),sum(nbins(1:k))+1:sum(nbins(1:k+1))) = tmp; | ||
36 | fh(sum(nbins(1:k))+1:sum(nbins(1:k+1)),sum(nbins(1:j))+1:sum(nbins(1:j+1))) = tmp'; | ||
37 | end | ||
38 | end | ||
39 | |||
40 | fprintf('\n'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/histbin_fv_chank.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/histbin_fv_chank.m new file mode 100755 index 0000000..8cf2a35 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/histbin_fv_chank.m | |||
@@ -0,0 +1,14 @@ | |||
1 | function histbin_fv_chank(fvs,hb,chank_size,fname_base) | ||
2 | |||
3 | [nv,np] = size(fvs); | ||
4 | |||
5 | k =1; | ||
6 | for j=1:chank_size:np, | ||
7 | disp(sprintf('|%d',j)); | ||
8 | fh = colize_hist(fvs(:,j:min(j+chank_size-1,np)),hb); | ||
9 | fname = sprintf('%s_%d.mat',fname_base,k); | ||
10 | cm = sprintf('save %s fh hb;',fname); | ||
11 | disp(cm); | ||
12 | eval(cm); | ||
13 | k = k+1; | ||
14 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hsv2clrs.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hsv2clrs.m new file mode 100755 index 0000000..cfbb8b0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/hsv2clrs.m | |||
@@ -0,0 +1,25 @@ | |||
1 | function [x,y,z] = hsv2clrs(h,s,v) | ||
2 | % | ||
3 | % function [x,y,z] = hsv2clrs(h,s,v) | ||
4 | % if h is 3D matrix, output in 3D x | ||
5 | % | ||
6 | |||
7 | if (size(h,3) == 3), | ||
8 | s = h(:,:,2); | ||
9 | v = h(:,:,3); | ||
10 | h = h(:,:,1); | ||
11 | |||
12 | z = v; | ||
13 | xx = s.*v.*cos(2*pi*h); | ||
14 | y = s.*v.*sin(2*pi*h); | ||
15 | |||
16 | x(:,:,1) = xx; | ||
17 | x(:,:,2) = y; | ||
18 | x(:,:,3) = z; | ||
19 | else | ||
20 | |||
21 | z = v; | ||
22 | x = s.*v.*cos(2*pi*h); | ||
23 | y = s.*v.*sin(2*pi*h); | ||
24 | end | ||
25 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/id_cut.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/id_cut.m new file mode 100755 index 0000000..daf8f2b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/id_cut.m | |||
@@ -0,0 +1,14 @@ | |||
1 | function [x,map] = idcut(data,cmap,nbin) | ||
2 | % | ||
3 | % | ||
4 | % | ||
5 | |||
6 | lc = size(cmap,1); | ||
7 | |||
8 | data = data - min(data); | ||
9 | data = 1+ ((lc-1)*data/max(data)); | ||
10 | |||
11 | r = cmap(data,1);g = cmap(data,2);b = cmap(data,3); | ||
12 | |||
13 | [x,map] = vmquant(r,g,b,nbin); | ||
14 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im.m new file mode 100755 index 0000000..6450120 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im.m | |||
@@ -0,0 +1,3 @@ | |||
1 | function im(I) | ||
2 | |||
3 | imagesc(I);axis('image');drawnow; \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im3.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im3.m new file mode 100755 index 0000000..b49e690 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im3.m | |||
@@ -0,0 +1,3 @@ | |||
1 | function im3(d) | ||
2 | |||
3 | imagesc(reshape(d,size(d,1),size(d,2)*size(d,3))); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im5.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im5.m new file mode 100755 index 0000000..7f1b16d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im5.m | |||
@@ -0,0 +1,16 @@ | |||
1 | function im5(data,nr,nc,mag) | ||
2 | |||
3 | if nargin == 4, | ||
4 | for j=1:size(data,3), | ||
5 | subplot(nr,nc,j); | ||
6 | imagesc(data(:,:,j)./mag);axis('image');axis('off');colorbar;drawnow; | ||
7 | |||
8 | % image(150*data(:,:,j));axis('image');axis('off');colorbar;drawnow; | ||
9 | end | ||
10 | |||
11 | else | ||
12 | for j=1:size(data,3), | ||
13 | subplot(nr,nc,j); | ||
14 | imagesc(data(:,:,j));axis('image');axis('off');colorbar;drawnow; | ||
15 | end | ||
16 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im_vect.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im_vect.m new file mode 100755 index 0000000..d0a5b30 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/im_vect.m | |||
@@ -0,0 +1,20 @@ | |||
1 | function a = im_vect(loca,v,scale); | ||
2 | |||
3 | if ~exist('scale'), | ||
4 | scale = 50; | ||
5 | end | ||
6 | |||
7 | y = loca(1,:); | ||
8 | x = loca(2,:); | ||
9 | |||
10 | x = x - min(x); | ||
11 | y = y - min(y); | ||
12 | |||
13 | max_x = max(x);max_y = max(y); | ||
14 | min_scale = min(max_x,max_y); | ||
15 | |||
16 | x = scale*x/min_scale; | ||
17 | y = scale*y/min_scale; | ||
18 | |||
19 | |||
20 | a = sparse(y+1,x+1,v); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/imrotate.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/imrotate.m new file mode 100755 index 0000000..167fd02 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/imrotate.m | |||
@@ -0,0 +1,119 @@ | |||
1 | function bout = imrotate(arg1,arg2,arg3,arg4) | ||
2 | %IMROTATE Rotate image. | ||
3 | % B = IMROTATE(A,ANGLE,'method') rotates the image A by ANGLE | ||
4 | % degrees. The image returned B will, in general, be larger | ||
5 | % than A. Invalid values on the periphery are set to one | ||
6 | % for indexed images or zero for all other image types. Possible | ||
7 | % interpolation methods are 'nearest','bilinear' or 'bicubic'. | ||
8 | % 'bilinear' is the default for intensity images, otherwise | ||
9 | % 'nearest' is used if no method is given. | ||
10 | % | ||
11 | % B = IMROTATE(A,ANGLE,'crop') or IMROTATE(A,ANGLE,'method','crop') | ||
12 | % crops B to be the same size as A. | ||
13 | % | ||
14 | % Without output arguments, IMROTATE(...) displays the rotated | ||
15 | % image in the current axis. | ||
16 | % | ||
17 | % See also IMRESIZE, IMCROP, ROT90. | ||
18 | |||
19 | % Clay M. Thompson 8-4-92 | ||
20 | % Copyright (c) 1992 by The MathWorks, Inc. | ||
21 | % $Revision: 1.14 $ $Date: 1993/09/01 21:27:38 $ | ||
22 | |||
23 | if nargin<2, error('Requires at least two input parameters.'); end | ||
24 | if nargin<3, | ||
25 | if isgray(arg1), caseid = 'bil'; else caseid = 'nea'; end | ||
26 | docrop = 0; | ||
27 | elseif nargin==3, | ||
28 | if isstr(arg3), | ||
29 | method = [lower(arg3),' ']; % Protect against short method | ||
30 | caseid = method(1:3); | ||
31 | if caseid(1)=='c', % Crop string | ||
32 | if isgray(arg1), caseid = 'bil'; else caseid = 'nea'; end | ||
33 | docrop = 1; | ||
34 | else | ||
35 | docrop = 0; | ||
36 | end | ||
37 | else | ||
38 | error('''METHOD'' must be a string of at least three characters.'); | ||
39 | end | ||
40 | else | ||
41 | if isstr(arg3), | ||
42 | method = [lower(arg3),' ']; % Protect against short method | ||
43 | caseid = method(1:3); | ||
44 | else | ||
45 | error('''METHOD'' must be a string of at least three characters.'); | ||
46 | end | ||
47 | docrop = 1; | ||
48 | end | ||
49 | |||
50 | % Catch and speed up 90 degree rotations | ||
51 | if rem(arg2,90)==0 & nargin<4, | ||
52 | phi = rem(arg2,360); | ||
53 | if phi==90, | ||
54 | b = rot90(arg1); | ||
55 | elseif phi==180, | ||
56 | b = rot90(arg1,2); | ||
57 | elseif phi==270, | ||
58 | b = rot90(arg1,-1); | ||
59 | else | ||
60 | b = arg1; | ||
61 | end | ||
62 | if nargout==0, imshow(b), else bout = b; end | ||
63 | return | ||
64 | end | ||
65 | |||
66 | phi = arg2*pi/180; % Convert to radians | ||
67 | |||
68 | % Rotation matrix | ||
69 | T = [cos(phi) -sin(phi); sin(phi) cos(phi)]; | ||
70 | |||
71 | % Coordinates from center of A | ||
72 | [m,n] = size(arg1); | ||
73 | if ~docrop, % Determine limits for rotated image | ||
74 | siz = ceil(max(abs([(n-1)/2 -(m-1)/2;(n-1)/2 (m-1)/2]*T))/2)*2; | ||
75 | uu = -siz(1):siz(1); vv = -siz(2):siz(2); | ||
76 | else % Cropped image | ||
77 | uu = (1:n)-(n+1)/2; vv = (1:m)-(m+1)/2; | ||
78 | end | ||
79 | nu = length(uu); nv = length(vv); | ||
80 | |||
81 | blk = bestblk([nv nu]); | ||
82 | nblks = floor([nv nu]./blk); nrem = [nv nu] - nblks.*blk; | ||
83 | mblocks = nblks(1); nblocks = nblks(2); | ||
84 | mb = blk(1); nb = blk(2); | ||
85 | |||
86 | rows = 1:blk(1); b = zeros(nv,nu); | ||
87 | for i=0:mblocks, | ||
88 | if i==mblocks, rows = (1:nrem(1)); end | ||
89 | for j=0:nblocks, | ||
90 | if j==0, cols = 1:blk(2); elseif j==nblocks, cols=(1:nrem(2)); end | ||
91 | if ~isempty(rows) & ~isempty(cols) | ||
92 | [u,v] = meshgrid(uu(j*nb+cols),vv(i*mb+rows)); | ||
93 | % Rotate points | ||
94 | uv = [u(:) v(:)]*T'; % Rotate points | ||
95 | u(:) = uv(:,1)+(n+1)/2; v(:) = uv(:,2)+(m+1)/2; | ||
96 | if caseid(1)=='n', % Nearest neighbor interpolation | ||
97 | b(i*mb+rows,j*nb+cols) = interp6(arg1,u,v); | ||
98 | elseif all(caseid=='bil'), % Bilinear interpolation | ||
99 | b(i*mb+rows,j*nb+cols) = interp4(arg1,u,v); | ||
100 | elseif all(caseid=='bic'), % Bicubic interpolation | ||
101 | b(i*mb+rows,j*nb+cols) = interp5(arg1,u,v); | ||
102 | else | ||
103 | error(['Unknown interpolation method: ',method]); | ||
104 | end | ||
105 | end | ||
106 | end | ||
107 | end | ||
108 | |||
109 | d = find(isnan(b)); | ||
110 | if length(d)>0, | ||
111 | if isind(arg1), b(d) = ones(size(d)); else b(d) = zeros(size(d)); end | ||
112 | end | ||
113 | |||
114 | if nargout==0, | ||
115 | imshow(b), return | ||
116 | end | ||
117 | bout = b; | ||
118 | |||
119 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/ims.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/ims.m new file mode 100755 index 0000000..2fb5f25 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/ims.m | |||
@@ -0,0 +1,3 @@ | |||
1 | function ims(I,nr,nc) | ||
2 | |||
3 | im(reshape(I,nr,nc)); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/imvs.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/imvs.m new file mode 100755 index 0000000..21dde73 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/imvs.m | |||
@@ -0,0 +1,4 @@ | |||
1 | function imvs(I,v,nr,nc) | ||
2 | |||
3 | v = reshape(v,nr,nc); | ||
4 | im(I(1:size(v,1),1:size(v,2)).*v); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/is_step.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/is_step.m new file mode 100755 index 0000000..4903778 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/is_step.m | |||
@@ -0,0 +1,33 @@ | |||
1 | function P_step = is_step(gx,gy) | ||
2 | % | ||
3 | % P_step = is_step(gx,gy) | ||
4 | % | ||
5 | % determine wheter gx,gy(which is first | ||
6 | % order filter output) is a response | ||
7 | % to a step function | ||
8 | % | ||
9 | |||
10 | M = zeros(2,2); | ||
11 | M(1,1) = sum(sum(gx.*gx)); | ||
12 | M(2,2) = sum(sum(gy.*gy)); | ||
13 | M(1,2) = sum(sum(gx.*gy)); | ||
14 | M(2,1) = M(1,2); | ||
15 | |||
16 | [v,d] = eig(M); | ||
17 | d = diag(d); | ||
18 | |||
19 | % make the first eig_value to be the smaller one | ||
20 | if (d(2)< d(1)), | ||
21 | tmp = d(1);d(1) = d(2);d(2) = tmp; | ||
22 | tmp = v(:,1); v(:,1) = v(:,2); v(:,2) = tmp; | ||
23 | end | ||
24 | |||
25 | ratio = d(1)/d(2); | ||
26 | threshold = 0; | ||
27 | gx_ratio = sum(sum(gx.*(gx>threshold)))/(sum(sum(abs(gx).*(abs(gx)>threshold)))); | ||
28 | gx_ratio = max(gx_ratio,1-gx_ratio); | ||
29 | |||
30 | gy_ratio = sum(sum(gy.*(gy>threshold)))/(sum(sum(abs(gy).*(abs(gy)>threshold)))); | ||
31 | gy_ratio = max(gy_ratio,1-gy_ratio); | ||
32 | |||
33 | P_step = [ratio,gx_ratio,gy_ratio]; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/ks_2d.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/ks_2d.m new file mode 100755 index 0000000..cfac005 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/ks_2d.m | |||
@@ -0,0 +1,20 @@ | |||
1 | function [na,nb,nc,nd] = ks_2d(cum_filter) | ||
2 | |||
3 | [nb_filters,nb_bins] = size(cum_filter); | ||
4 | |||
5 | T = 1; | ||
6 | |||
7 | for j = [1:nb_filters], | ||
8 | for l = [1:nb_bins], | ||
9 | nc(j,l) = sum(cum_filter(1:j,l)); | ||
10 | nd(j,l) = j*T - nc(j,l); | ||
11 | |||
12 | if (j~= nb_filters), | ||
13 | nb(j,l) = sum(cum_filter(j+1:nb_filters,l)); | ||
14 | na(j,l) = (nb_filters-j)*T-nb(j,l); | ||
15 | else | ||
16 | nb(j,l) = 0; | ||
17 | na(j,l) = 0; | ||
18 | end | ||
19 | end | ||
20 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/load_result.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/load_result.m new file mode 100755 index 0000000..0ff328c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/load_result.m | |||
@@ -0,0 +1,39 @@ | |||
1 | |||
2 | fnameI = '130056'; | ||
3 | |||
4 | cm = sprintf('load filter_%s.pgm.mat',fnameI); | ||
5 | disp(cm); | ||
6 | eval(cm); | ||
7 | |||
8 | text_des= reshape(text_des,size(text_des,1),size(text_des,2),size(text_des,3)*size(text_des,4)); | ||
9 | |||
10 | for j=1:size(text_des,3), | ||
11 | text_des(:,:,j) = abs(text_des(:,:,j)).*(text_des(:,:,j) <0); | ||
12 | end | ||
13 | |||
14 | %text_des = abs(text_des); | ||
15 | |||
16 | |||
17 | %%%% cutoff margins, | ||
18 | margin = 6+10; | ||
19 | |||
20 | Iw = cutoff(I,margin); | ||
21 | |||
22 | |||
23 | T1 = cutoff(text_des,margin); | ||
24 | |||
25 | %%%%% reduce resolution | ||
26 | |||
27 | |||
28 | |||
29 | T1 = reduce_all(T1); | ||
30 | T1 = reduce_all(T1); | ||
31 | |||
32 | im5(T1,5,6); | ||
33 | |||
34 | cm = sprintf('writepnm5(''%s_f.pnm'',%s)',fnameI,'T1/70'); | ||
35 | |||
36 | % disp(cm);eval(cm); | ||
37 | |||
38 | nr = size(T1,1); | ||
39 | nc = size(T1,2); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/m_interp4.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/m_interp4.m new file mode 100755 index 0000000..314f140 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/m_interp4.m | |||
@@ -0,0 +1,49 @@ | |||
1 | function [F,mask] = m_interp4(z,s,t) | ||
2 | %INTERP4 2-D bilinear data interpolation. | ||
3 | % ZI = INTERP4(Z,XI,YI) assumes X = 1:N and Y = 1:M, where | ||
4 | % [M,N] = SIZE(Z). | ||
5 | % | ||
6 | % Copyright (c) 1984-93 by The MathWorks, Inc. | ||
7 | % Clay M. Thompson 4-26-91, revised 7-3-91, 3-22-93 by CMT. | ||
8 | % | ||
9 | % modified to | ||
10 | |||
11 | |||
12 | [nrows,ncols] = size(z); | ||
13 | |||
14 | |||
15 | if any(size(z)<[3 3]), error('Z must be at least 3-by-3.'); end | ||
16 | if size(s)~=size(t), error('XI and YI must be the same size.'); end | ||
17 | |||
18 | % Check for out of range values of s and set to 1 | ||
19 | sout = find((s<1)|(s>ncols)); | ||
20 | if length(sout)>0, s(sout) = ones(size(sout)); end | ||
21 | |||
22 | % Check for out of range values of t and set to 1 | ||
23 | tout = find((t<1)|(t>nrows)); | ||
24 | if length(tout)>0, t(tout) = ones(size(tout)); end | ||
25 | |||
26 | % Matrix element indexing | ||
27 | ndx = floor(t)+floor(s-1)*nrows; | ||
28 | |||
29 | % Compute intepolation parameters, check for boundary value. | ||
30 | d = find(s==ncols); | ||
31 | s(:) = (s - floor(s)); | ||
32 | if length(d)>0, s(d) = s(d)+1; ndx(d) = ndx(d)-nrows; end | ||
33 | |||
34 | % Compute intepolation parameters, check for boundary value. | ||
35 | d = find(t==nrows); | ||
36 | t(:) = (t - floor(t)); | ||
37 | if length(d)>0, t(d) = t(d)+1; ndx(d) = ndx(d)-1; end | ||
38 | d = []; | ||
39 | |||
40 | % Now interpolate, reuse u and v to save memory. | ||
41 | F = ( z(ndx).*(1-t) + z(ndx+1).*t ).*(1-s) + ... | ||
42 | ( z(ndx+nrows).*(1-t) + z(ndx+(nrows+1)).*t ).*s; | ||
43 | |||
44 | mask = ones(size(z)); | ||
45 | |||
46 | % Now set out of range values to zeros. | ||
47 | if length(sout)>0, F(sout) = zeros(size(sout));mask(sout)=zeros(size(sout));end | ||
48 | if length(tout)>0, F(tout) = zeros(size(tout));mask(tout)=zeros(size(tout));end | ||
49 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/make_masks.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/make_masks.m new file mode 100755 index 0000000..d2c8ba7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/make_masks.m | |||
@@ -0,0 +1,12 @@ | |||
1 | function masks = make_masks(groups,ids,nr,nc); | ||
2 | |||
3 | np = ids(end); | ||
4 | |||
5 | baseid =1; | ||
6 | for j=1:length(ids), | ||
7 | mask = zeros(np,1); | ||
8 | mask(groups(baseid:ids(j))) = 1+mask(groups(baseid:ids(j))); | ||
9 | baseid = 1+ids(j); | ||
10 | |||
11 | masks(:,:,j) = mask; | ||
12 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/makefilter.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/makefilter.m new file mode 100755 index 0000000..a768269 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/makefilter.m | |||
@@ -0,0 +1,14 @@ | |||
1 | function H = make_filter(txture,sze,noise) | ||
2 | % function H = make_filter(txture,sze) | ||
3 | % | ||
4 | % | ||
5 | |||
6 | x = zeros(sze); | ||
7 | tx_sze = size(txture); | ||
8 | |||
9 | x(1:tx_sze(1),1:tx_sze(2)) = txture; | ||
10 | x = reshape(x,1,sze(1)*sze(2)); | ||
11 | X = fft(x); | ||
12 | figure(3);plot(abs(X).^2); | ||
13 | |||
14 | H = X./(abs(X).^2+noise); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkf_t1.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkf_t1.m new file mode 100755 index 0000000..256e609 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkf_t1.m | |||
@@ -0,0 +1,22 @@ | |||
1 | function [H,h] = make_filter(txture,sze,noise) | ||
2 | % function H = make_filter(txture,sze) | ||
3 | % | ||
4 | % | ||
5 | |||
6 | tx_sze = size(txture); | ||
7 | x = reshape(txture,1,tx_sze(1)*tx_sze(2)); | ||
8 | X = fft(x); | ||
9 | H = X./(abs(X).^2+noise); | ||
10 | h = reshape(real(ifft(H)),tx_sze(1),tx_sze(2)); | ||
11 | |||
12 | |||
13 | x = zeros(sze); | ||
14 | x(1:tx_sze(1),1:tx_sze(2)) = h; | ||
15 | figure(1);imagesc(x);drawnow; | ||
16 | x = reshape(x,1,sze(1)*sze(2)); | ||
17 | H = fft(x); | ||
18 | |||
19 | h = reshape(real(ifft(H)),sze(1),sze(2)); | ||
20 | figure(2);imagesc(h); | ||
21 | |||
22 | figure(3); mesh(h); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkf_t2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkf_t2.m new file mode 100755 index 0000000..c84482d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkf_t2.m | |||
@@ -0,0 +1,21 @@ | |||
1 | function [H,h] = make_filter(txture,sze,noise) | ||
2 | % function H = make_filter(txture,sze) | ||
3 | % | ||
4 | % | ||
5 | |||
6 | x = zeros(sze); | ||
7 | tx_sze = size(txture); | ||
8 | |||
9 | [center_x,center_y] = find_center(sze(2),sze(1)); | ||
10 | tx_sze_h = round(0.5*tx_sze); | ||
11 | |||
12 | x(center_y-tx_sze_h(1):center_y-tx_sze_h(1)+tx_sze(1)-1,... | ||
13 | center_x-tx_sze_h(2):center_x-tx_sze_h(2)+tx_sze(2)-1) = txture; | ||
14 | figure(1);imagesc(x);drawnow; | ||
15 | x = reshape(x,1,sze(1)*sze(2)); | ||
16 | X = fft(x); | ||
17 | H = X./(abs(X).^2+noise); | ||
18 | h = reshape(real(ifft(H)),sze(1),sze(2)); | ||
19 | figure(2);imagesc(h); | ||
20 | |||
21 | figure(3); mesh(h); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkf_test.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkf_test.m new file mode 100755 index 0000000..e8d0ad8 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkf_test.m | |||
@@ -0,0 +1,43 @@ | |||
1 | function [H,h] = make_filter(txture,sze,n_c,a,b,c) | ||
2 | % function H = make_filter(txture,sze) | ||
3 | % | ||
4 | % | ||
5 | |||
6 | x = zeros(sze); | ||
7 | tx_sze = size(txture); | ||
8 | |||
9 | x(1:tx_sze(1),1:tx_sze(2)) = txture; | ||
10 | %figure(1);imagesc(x);drawnow; | ||
11 | x = reshape(x,1,sze(1)*sze(2)); | ||
12 | X = fft(x); | ||
13 | power = abs(X).^2; | ||
14 | |||
15 | figure(3);plot(power); | ||
16 | len = length(X); | ||
17 | |||
18 | t = [1:0.5*(length(X)-1),0.5*(length(X)-1):-1:1]; | ||
19 | |||
20 | top = max(power); | ||
21 | if (c == -1), | ||
22 | c = top*5.0e-1 | ||
23 | end | ||
24 | if (n_c == -1), | ||
25 | n_c = top*1.0e-1 | ||
26 | end | ||
27 | |||
28 | nois = n_c +c*(exp(-a*(t.^b))); | ||
29 | if (rem(len,2) == 1), | ||
30 | noise = [c+n_c,nois]; | ||
31 | else | ||
32 | noise = [c+n_c,nois,c+n_c]; | ||
33 | end | ||
34 | |||
35 | figure(3);hold on;plot(noise,'r'); | ||
36 | hold off | ||
37 | H = X./(abs(X).^2+noise); | ||
38 | h = reshape(real(ifft(H)),sze(1),sze(2)); | ||
39 | figure(2);imagesc(h); | ||
40 | |||
41 | figure(4);plot(abs(H).^2,'c') | ||
42 | %figure(3); mesh(h); | ||
43 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkg.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkg.m new file mode 100755 index 0000000..1fb1f7e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkg.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function g= mkgaussian(xo,yo,sigma_x,sigma_y,size_w) | ||
2 | % | ||
3 | % function G = mkgaussian(xo,yo,sigma_x,sigma_y,size_w) | ||
4 | % | ||
5 | |||
6 | size_wh = round(0.5*size_w); | ||
7 | [x,y] = meshgrid([-size_wh:1:size_wh],[-size_wh:1:size_wh]); | ||
8 | g = 1/(2*pi*sigma_x*sigma_y)*(exp(-( ((x-xo)/sigma_x).^2 + ((y-yo)/sigma_y).^2))); | ||
9 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkgaussian.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkgaussian.m new file mode 100755 index 0000000..1213757 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkgaussian.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function G = mkgaussian(xo,yo,sigma_x,sigma_y,size_w) | ||
2 | % | ||
3 | % function G = mkgaussian(xo,yo,sigma_x,sigma_y,size_w) | ||
4 | % | ||
5 | |||
6 | size_wh = round(0.5*size_w); | ||
7 | |||
8 | [x,y] = meshgrid([-size_wh:1:size_wh],[-size_wh:1:size_wh]); | ||
9 | |||
10 | G = 0.5/(sigma_x*sigma_y)*(exp(-( ((x-xo)/sigma_x).^2 + ((y-yo)/sigma).^2))); | ||
11 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkmulfilter.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkmulfilter.m new file mode 100755 index 0000000..fe501b5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkmulfilter.m | |||
@@ -0,0 +1,52 @@ | |||
1 | function H = make_multi_filter(templates,num_templates,sze,u,noise) | ||
2 | % function H = make_filter(templates,num_templates,sze,u,noise) | ||
3 | % templates are column vectors of template | ||
4 | % sze is the size of the filter | ||
5 | % u is a vector of specified value | ||
6 | % | ||
7 | % | ||
8 | |||
9 | templates_size_x = size(templates,2)/num_templates; | ||
10 | templates_size_y = size(templates,1); | ||
11 | |||
12 | alpha = 1/num_templates; | ||
13 | |||
14 | X = zeros(num_templates,sze(1)*sze(2)); | ||
15 | Spectrums = zeros(num_templates,sze(1)*sze(2)); | ||
16 | |||
17 | |||
18 | for k =1:num_templates, | ||
19 | tmp = zeros(sze); | ||
20 | tmp(1:templates_size_y,1:templates_size_x) =... | ||
21 | templates(:,(k-1)*templates_size_x+1:k*templates_size_x); | ||
22 | x(k,:) = reshape(tmp,1,sze(1)*sze(2)); | ||
23 | X(k,:) = fft(x(k,:)); | ||
24 | Spectrums(k,:) = conj(X(k,:)).*X(k,:); | ||
25 | end | ||
26 | |||
27 | if num_templates > 1 | ||
28 | sum_Spect = sum(Spectrums)*alpha; | ||
29 | else | ||
30 | sum_Spect = Spectrums; | ||
31 | end | ||
32 | |||
33 | for row = 1:num_templates, | ||
34 | for column = 1:num_templates, | ||
35 | |||
36 | A(row,column) = sum( ((conj(X(row,:)).*X(column,:))./... | ||
37 | (sum_Spect + noise))')/(sze(1)*sze(2)); | ||
38 | end | ||
39 | end | ||
40 | |||
41 | |||
42 | epsilon = inv(A)*u; | ||
43 | |||
44 | top = epsilon(1)*X(1,:); | ||
45 | for k = 2:num_templates, | ||
46 | top = top + epsilon(k)*X(k,:); | ||
47 | end | ||
48 | |||
49 | H = top./(sum_Spect + noise); | ||
50 | |||
51 | |||
52 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkpoog2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkpoog2.m new file mode 100755 index 0000000..4bd0366 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mkpoog2.m | |||
@@ -0,0 +1,29 @@ | |||
1 | function doog2 = mkdoog2(sigma_w,r,theta,size_w) | ||
2 | % | ||
3 | % function doog2 = mkdoog2(sigma_w,r,theta,size_w) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | %scale_base = 2.8814; | ||
8 | scale_base = 2; | ||
9 | |||
10 | a = -1*scale_base; | ||
11 | b = 2*scale_base; | ||
12 | c = -1*scale_base; | ||
13 | |||
14 | sigma_x = r*sigma_w; | ||
15 | sigma_y = sigma_w; | ||
16 | |||
17 | ya = sigma_w; | ||
18 | yc = -sigma_w; | ||
19 | yb = 0; | ||
20 | |||
21 | doog2 = a*mkg(0,ya,sigma_x,sigma_y,size_w) +... | ||
22 | b*mkg(0,yb,sigma_x,sigma_y,size_w) +... | ||
23 | c*mkg(0,yc,sigma_x,sigma_y,size_w); | ||
24 | |||
25 | figure(3);colormap(hsv); | ||
26 | subplot(1,3,1);mesh(a*mkg(0,ya,sigma_x,sigma_y,size_w)); | ||
27 | subplot(1,3,2);mesh(b*mkg(0,yb,sigma_x,sigma_y,size_w)); | ||
28 | subplot(1,3,3);mesh(c*mkg(0,yc,sigma_x,sigma_y,size_w)); | ||
29 | %doog2 = 255*5.1745*doog2; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mksgn.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mksgn.m new file mode 100755 index 0000000..15f947b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mksgn.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function sgn = makesgn(sigma,sigma_x,sze) | ||
2 | |||
3 | |||
4 | size_wh = round(0.5*sze); | ||
5 | [x,y] = meshgrid([-size_wh:1:size_wh],[-size_wh:1:size_wh]); | ||
6 | steps = 1/(1+2*sigma_x); | ||
7 | |||
8 | fx = -1*(x<=-sigma_x) + (x>=sigma_x) + steps*((x+sigma_x).*(x>-sigma_x).*(x<sigma_x)); | ||
9 | |||
10 | sgn = fx.*(exp(- (y.*y)/(2*sigma))); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mksgn2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mksgn2.m new file mode 100755 index 0000000..857360b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mksgn2.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function sgn = makesgn(sigma,sigma_x,sze) | ||
2 | |||
3 | |||
4 | size_wh = round(0.5*sze); | ||
5 | [x,y] = meshgrid([-size_wh(2):1:size_wh(2)],[-size_wh(1):1:size_wh(1)]); | ||
6 | steps = 1/(1+sigma_x); | ||
7 | |||
8 | fx = steps*((x+sigma_x).*(x>-sigma_x).*(x<1)) + steps*(sigma_x-x).*(x<sigma_x).*(x>=1); | ||
9 | sgn = fx.*(exp(- (y.*y)/(2*sigma))); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mreadpfm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mreadpfm.m new file mode 100755 index 0000000..cf68a01 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mreadpfm.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function I = read_pfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2) | ||
6 | I = fscanf(fid,'%f',[A(1),A(2)]); | ||
7 | |||
8 | |||
9 | I = I'; | ||
10 | size(I); | ||
11 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mreadpfm2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mreadpfm2.m new file mode 100755 index 0000000..c01b25b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mreadpfm2.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function I = read_pfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2) | ||
6 | I = fscanf(fid,'%f',[A(1),A(2)]); | ||
7 | |||
8 | |||
9 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mwis.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mwis.m new file mode 100755 index 0000000..95de8f6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mwis.m | |||
@@ -0,0 +1,16 @@ | |||
1 | function [l1,l2,phi] = mwis(gx,gy) | ||
2 | % | ||
3 | % [l1,l2,phi] = mwis(gx,gy) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | sgx = sum(sum(gx.*gx)); | ||
8 | sgxy = sum(sum(gx.*gy)); | ||
9 | sgy = sum(sum(gy.*gy)); | ||
10 | |||
11 | tr = sgx + sgy; | ||
12 | v1 = 0.5*sqrt(tr*tr - 4*(sgx*sgy-sgxy*sgxy)); | ||
13 | l1 = real(0.5*tr+v1); | ||
14 | l2 = real(0.5*tr-v1); | ||
15 | |||
16 | phi= 0.5*atan2(2*sgxy,sgx-sgy); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mwis_image.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mwis_image.m new file mode 100755 index 0000000..c8e8b66 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/mwis_image.m | |||
@@ -0,0 +1,25 @@ | |||
1 | function [l1,l2,phi] = mwis_image(gx,gy,hs) | ||
2 | % | ||
3 | % [l1,l2,phi] = mwis_image(gx,gy) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | if (~exist('hs')), | ||
8 | hs = 10; | ||
9 | end | ||
10 | |||
11 | |||
12 | sgx = gx.*gx; | ||
13 | sgxy = gx.*gy; | ||
14 | sgy = gy.*gy; | ||
15 | |||
16 | ssgx = smooth(sgx,hs); | ||
17 | ssgxy = smooth(sgxy,hs); | ||
18 | ssgy = smooth(sgy,hs); | ||
19 | |||
20 | tr = ssgx + ssgy; | ||
21 | v1 = 0.5*sqrt(tr.*tr - 4*(ssgx.*ssgy-ssgxy.*ssgxy)); | ||
22 | l1 = real(0.5*tr+v1); | ||
23 | l2 = real(0.5*tr-v1); | ||
24 | |||
25 | phi= 0.5*atan2(2*sgxy,sgx-sgy); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/myinterp.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/myinterp.m new file mode 100755 index 0000000..6d8f055 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/myinterp.m | |||
@@ -0,0 +1,18 @@ | |||
1 | function y = myinterp(d,rate) | ||
2 | % | ||
3 | |||
4 | |||
5 | if (size(d,1)>1), | ||
6 | d = [d;d(1)]; | ||
7 | else | ||
8 | d = [d,d(1)]; | ||
9 | end | ||
10 | |||
11 | lgn = length(d); | ||
12 | |||
13 | x = 1:lgn; | ||
14 | xx = linspace(1,lgn,rate*lgn); | ||
15 | |||
16 | y = interp1(x,d,xx,'linear'); | ||
17 | |||
18 | y = y(1:(length(y)-1)); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/ncut_b.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/ncut_b.m new file mode 100755 index 0000000..c9eee35 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/ncut_b.m | |||
@@ -0,0 +1,25 @@ | |||
1 | function [v,d] = ncut(A,nv) | ||
2 | |||
3 | ds = sum(A); | ||
4 | ds = ones(size(ds))./sqrt(ds); | ||
5 | |||
6 | for j=1:size(A,1), | ||
7 | A(j,:) = A(j,:).*ds; | ||
8 | end | ||
9 | |||
10 | for j=1:size(A,2); | ||
11 | A(:,j) = A(:,j).*ds'; | ||
12 | end | ||
13 | |||
14 | %D1 = ds'*ones(1,length(ds)); | ||
15 | %A = D1'.*A.*D1; | ||
16 | |||
17 | disp(sprintf('computing eig values')); | ||
18 | tic;[v,d] = eigs(A,nv);toc; | ||
19 | |||
20 | d = abs(diag(d)); | ||
21 | |||
22 | for j=1:nv, | ||
23 | v(:,j) = v(:,j).*ds'; | ||
24 | end | ||
25 | %v = D1(:,1:size(v,2)).*v; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/new_compute_J.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/new_compute_J.m new file mode 100755 index 0000000..f4e376a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/new_compute_J.m | |||
@@ -0,0 +1,32 @@ | |||
1 | function [JJ,mask] = compute_J(A,D,I,center,window_size_h) | ||
2 | %% function J = compute_J(A,D,I,center,window_size_h) | ||
3 | % | ||
4 | |||
5 | [size_y,size_x] = size(I); | ||
6 | |||
7 | center_x = center(1); | ||
8 | center_y = center(2); | ||
9 | |||
10 | XX = meshgrid(1:size_x,1:size_y); | ||
11 | YY = meshgrid(1:size_y,1:size_x)'; | ||
12 | x = reshape(XX,size_x*size_y,1); | ||
13 | y = reshape(YY,size_x*size_y,1); | ||
14 | index(:,1) = x-center_x; | ||
15 | index(:,2) = y-center_y; | ||
16 | |||
17 | position_new = A*index'+ [D(1),0;0,D(2)]*ones(2,size_x*size_y); | ||
18 | position_new(1,:) = position_new(1,:)+center_x; | ||
19 | position_new(2,:) = position_new(2,:)+center_y; | ||
20 | |||
21 | position_new_x = reshape(position_new(1,:),size_y,size_x); | ||
22 | position_new_y = reshape(position_new(2,:),size_y,size_x); | ||
23 | |||
24 | [J,mask]= m_interp4(I,position_new_x,position_new_y); | ||
25 | |||
26 | JJ = J(center(2)-window_size_h(2):center(2)+window_size_h(2),... | ||
27 | center(1)-window_size_h(1):center(1)+window_size_h(1)); | ||
28 | mask = mask(center(2)-window_size_h(2):center(2)+window_size_h(2),... | ||
29 | center(1)-window_size_h(1):center(1)+window_size_h(1)); | ||
30 | |||
31 | |||
32 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist.m new file mode 100755 index 0000000..f8dbd32 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function A = apply_image(gx,gy,I,wc) | ||
2 | % | ||
3 | % aout = apply_image(gx,gy,wc) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | [nr,nc] =size(gx); | ||
8 | |||
9 | w = 2*wc+1; | ||
10 | |||
11 | s1 = floor(nr/w); | ||
12 | s2 = floor(nc/w); | ||
13 | |||
14 | A = zeros(s1*s2,s1*s2); | ||
15 | |||
16 | yid = 0; | ||
17 | for j=wc+1:w:nr-wc-1, | ||
18 | yid = yid+1; | ||
19 | xid = 0; | ||
20 | for k=wc+1:w:nc-wc-1, | ||
21 | xid = xid + 1; | ||
22 | c1 = [k,j]; | ||
23 | |||
24 | yyid = 0; | ||
25 | fprintf('.'); | ||
26 | for jj=wc+1:w:nr-wc-1, | ||
27 | yyid = yyid+1; | ||
28 | xxid = 0; | ||
29 | for kk=wc+1:w:nc-wc-1, | ||
30 | xxid = xxid + 1; | ||
31 | |||
32 | c2 = [kk,jj]; | ||
33 | |||
34 | a = compute_diff_patch(get_win(gx,c1,[wc,wc]),... | ||
35 | get_win(gy,c1,[wc,wc]),... | ||
36 | get_win(gx,c2,[wc,wc]),... | ||
37 | get_win(gy,c2,[wc,wc]),... | ||
38 | get_win(I,c1,[wc,wc]),... | ||
39 | get_win(I,c2,[wc,wc])); | ||
40 | |||
41 | A(yid+(xid-1)*s1,yyid+(xxid-1)*s1) = a; | ||
42 | end | ||
43 | end | ||
44 | end | ||
45 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist2.m new file mode 100755 index 0000000..dc31469 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist2.m | |||
@@ -0,0 +1,46 @@ | |||
1 | function A = apply_image(gx,gy,I,wc) | ||
2 | % | ||
3 | % aout = apply_image(gx,gy,wc) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | [nr,nc] =size(gx); | ||
8 | |||
9 | w = 2*wc+1; | ||
10 | |||
11 | lws = 4; | ||
12 | |||
13 | s1 = floor(nr/w); | ||
14 | s2 = floor(nc/w); | ||
15 | |||
16 | A = zeros(s1*s2,s1*s2); | ||
17 | |||
18 | for j=1:s1, | ||
19 | for k=1:s2, | ||
20 | c1 = [(wc+1)+(k-1)*w,(wc+1)+(j-1)*w]; | ||
21 | fprintf('.'); | ||
22 | |||
23 | for jj=j-lws:j+lws, | ||
24 | for kk=k-lws:k+lws, | ||
25 | |||
26 | c2 = [(wc+1)+(kk-1)*w,(wc+1)+(jj-1)*w]; | ||
27 | if ( (jj>0) & (kk>0) & (jj <= s1) & (kk <= s2)) | ||
28 | a = compute_diff_patch(get_win(gx,c1,[wc,wc]),... | ||
29 | get_win(gy,c1,[wc,wc]),... | ||
30 | get_win(gx,c2,[wc,wc]),... | ||
31 | get_win(gy,c2,[wc,wc]),... | ||
32 | get_win(I,c1,[wc,wc]),... | ||
33 | get_win(I,c2,[wc,wc])); | ||
34 | |||
35 | dx = abs(k-kk);dy = abs(j-jj); | ||
36 | sp_diff = exp(-sqrt(dx.^2+dy.^2)/4); | ||
37 | A(j+(k-1)*s1,jj+(kk-1)*s1) = a*sp_diff; | ||
38 | |||
39 | end | ||
40 | |||
41 | end | ||
42 | end | ||
43 | end | ||
44 | end | ||
45 | |||
46 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text.m new file mode 100755 index 0000000..5cdb201 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text.m | |||
@@ -0,0 +1,70 @@ | |||
1 | function A = apply_image(I,bars,wc) | ||
2 | % | ||
3 | % aout = apply_image(gx,gy,wc) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | [nr,nc] =size(I); | ||
8 | |||
9 | w = 2*wc+1; | ||
10 | |||
11 | lws = 4; | ||
12 | |||
13 | gap = 10; | ||
14 | |||
15 | s1 = floor((nr-w)/gap); | ||
16 | s2 = floor((nc-w)/gap); | ||
17 | |||
18 | A = zeros(s1*s2,s1*s2); | ||
19 | |||
20 | sigma.text = 0.20; | ||
21 | sigma.mag = 0.20; | ||
22 | sigma.inten = 0.15; | ||
23 | |||
24 | for j=1:s1, | ||
25 | for k=1:s2, | ||
26 | |||
27 | |||
28 | c1 = [(wc+1)+(k-1)*gap,(wc+1)+(j-1)*gap]; | ||
29 | fprintf('.'); | ||
30 | for jj=j-lws:j+lws, | ||
31 | for kk=k-lws:k+lws, | ||
32 | |||
33 | c2 = [(wc+1)+(kk-1)*gap,(wc+1)+(jj-1)*gap]; | ||
34 | if ( (jj>0) & (kk>0) & (jj <= s1) & (kk <= s2)), | ||
35 | |||
36 | J1 = get_win(I,c1,[wc,wc]); | ||
37 | J2 = get_win(I,c2,[wc,wc]); | ||
38 | |||
39 | Jbars1 = get_win5(bars,c1,[wc,wc]); | ||
40 | Jbars2 = get_win5(bars,c2,[wc,wc]); | ||
41 | |||
42 | |||
43 | hists1 = get_hist(J1,Jbars1); | ||
44 | hists2 = get_hist(J2,Jbars2); | ||
45 | |||
46 | cumhists1 = get_cumhist(hists1); | ||
47 | cumhists2 = get_cumhist(hists2); | ||
48 | |||
49 | |||
50 | diff.inten = max(abs(cumhists1.inten-cumhists2.inten)); | ||
51 | diff.mag = max(abs(cumhists1.mag-cumhists2.mag)); | ||
52 | diff.text = max(max(abs(cumhists1.text-cumhists2.text))); | ||
53 | |||
54 | diffs = max([diff.inten/sigma.inten,... | ||
55 | diff.mag/sigma.mag,... | ||
56 | diff.text/sigma.text]); | ||
57 | |||
58 | dx = abs(k-kk);dy = abs(j-jj); | ||
59 | sp_diff = sqrt(dx.^2+dy.^2)/4; | ||
60 | |||
61 | A(j+(k-1)*s1,jj+(kk-1)*s1) = exp(-diffs-sp_diff); | ||
62 | |||
63 | end | ||
64 | |||
65 | end | ||
66 | end | ||
67 | end | ||
68 | end | ||
69 | |||
70 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text2.m new file mode 100755 index 0000000..5c6e0d4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text2.m | |||
@@ -0,0 +1,58 @@ | |||
1 | function A = apply_image(I,bars,wc) | ||
2 | % | ||
3 | % aout = apply_image(gx,gy,wc) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | [nr,nc] =size(I); | ||
8 | |||
9 | w = 2*wc+1; | ||
10 | |||
11 | lws = 4; | ||
12 | |||
13 | gap = 10; | ||
14 | |||
15 | mag = sum(bars,3); | ||
16 | |||
17 | s1 = floor((nr-w)/gap) | ||
18 | s2 = floor((nc-w)/gap) | ||
19 | |||
20 | A = zeros(s1*s2,s1*s2); | ||
21 | |||
22 | sigma.text = 0.20; | ||
23 | sigma.mag = 0.20; | ||
24 | sigma.inten = 0.15; | ||
25 | |||
26 | for j=1:s1, | ||
27 | for k=1:s2, | ||
28 | |||
29 | |||
30 | c1 = [(wc+1)+(k-1)*gap,(wc+1)+(j-1)*gap]; | ||
31 | fprintf('.'); | ||
32 | for jj=j-lws:j+lws, | ||
33 | for kk=k-lws:k+lws, | ||
34 | |||
35 | c2 = [(wc+1)+(kk-1)*gap,(wc+1)+(jj-1)*gap]; | ||
36 | if ( (jj>0) & (kk>0) & (jj <= s1) & (kk <= s2)), | ||
37 | |||
38 | ces = [c1,c2]-wc; | ||
39 | cum = compute_dist_hist(I,mag,bars,[ces,w]); | ||
40 | |||
41 | diffs = max([cum(1)/sigma.inten,... | ||
42 | cum(2)/sigma.mag,... | ||
43 | cum(3)/sigma.text]); | ||
44 | |||
45 | dx = abs(k-kk);dy = abs(j-jj); | ||
46 | sp_diff = sqrt(dx.^2+dy.^2)/4; | ||
47 | |||
48 | A(j+(k-1)*s1,jj+(kk-1)*s1) = exp(-diffs-sp_diff); | ||
49 | |||
50 | end | ||
51 | |||
52 | end | ||
53 | end | ||
54 | |||
55 | end | ||
56 | end | ||
57 | |||
58 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text3.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text3.m new file mode 100755 index 0000000..a7ff408 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text3.m | |||
@@ -0,0 +1,84 @@ | |||
1 | function [A,D] = pair_dist_text3(Cum,cumhists) | ||
2 | % | ||
3 | % A = pair_dist_text3(Cum,cumhists); | ||
4 | % | ||
5 | % | ||
6 | |||
7 | s1 = Cum(2); | ||
8 | s2 = Cum(1); | ||
9 | |||
10 | st = Cum(3) + Cum(4) + 1; | ||
11 | ed = size(cumhists,1); | ||
12 | |||
13 | cumhists = cumhists(st:ed,:); | ||
14 | |||
15 | np = size(cumhists,2); | ||
16 | |||
17 | sigma.text = 0.20; | ||
18 | sigma.mag = 0.20; | ||
19 | sigma.inten = 0.15; | ||
20 | |||
21 | lws = 4; | ||
22 | |||
23 | |||
24 | k = sqrt(2)/2; | ||
25 | M = 8*6; | ||
26 | N = k*sqrt(M); | ||
27 | |||
28 | r1 = 0.001; | ||
29 | r2 = 0.001; | ||
30 | |||
31 | c = N/(1+ (sqrt(1-0.5*(r1*r1+r2*r2)))*(0.25-0.75/N)); | ||
32 | |||
33 | D = zeros(1,s1*s2); | ||
34 | |||
35 | nn = 1; | ||
36 | for j =1:s1, | ||
37 | for k=1:s2, | ||
38 | |||
39 | id = j*s2+k; | ||
40 | |||
41 | cum_filter1 = reshape(cumhists(:,id),8,6)'; | ||
42 | [na1,nb1,nc1,nd1] = ks_2d(cum_filter1); | ||
43 | |||
44 | |||
45 | fprintf('.'); | ||
46 | for jj=j-lws:j+lws, | ||
47 | for kk=k-lws:k+lws, | ||
48 | |||
49 | if ( (jj>0) & (kk>0) & (jj <= s1) & (kk <= s2)), | ||
50 | |||
51 | idn = jj*s2+k; | ||
52 | |||
53 | cum_filter2 = reshape(cumhists(:,idn),8,6)'; | ||
54 | [na2,nb2,nc2,nd2] = ks_2d(cum_filter2); | ||
55 | |||
56 | |||
57 | diffa = abs(na2-na1);diffb =abs(nb2-nb1); | ||
58 | diffc = abs(nc2-nc1);diffd = abs(nd2-nd1); | ||
59 | maxs(1) = max(max(diffa));maxs(2) = max(max(diffb)); | ||
60 | maxs(3) = max(max(diffc));maxs(4) = max(max(diffd)); | ||
61 | |||
62 | |||
63 | maxs = maxs/6; | ||
64 | |||
65 | d = min(1,signif(c*max(maxs))); | ||
66 | |||
67 | ids(nn) = id; | ||
68 | idns(nn) = idn; | ||
69 | B(nn) = d; | ||
70 | |||
71 | D(id) = D(id) + d; | ||
72 | D(idn) = D(idn) + d; | ||
73 | |||
74 | nn = nn+1; | ||
75 | |||
76 | end | ||
77 | |||
78 | end | ||
79 | end | ||
80 | |||
81 | end | ||
82 | end | ||
83 | |||
84 | A = sparse(ids,idns,b); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text4.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text4.m new file mode 100755 index 0000000..83f3d49 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pair_dist_text4.m | |||
@@ -0,0 +1,81 @@ | |||
1 | function [A] = pair_dist_text4(Iw,Cresult,cso) | ||
2 | % | ||
3 | % A = pair_dist_text3(Cum,cumhists); | ||
4 | % | ||
5 | % | ||
6 | |||
7 | %s1 = Cum(2);s2 = Cum(1); | ||
8 | |||
9 | figure(1);hold on;plot(cso(1),cso(2),'bo'); | ||
10 | |||
11 | ws = [10,10]; | ||
12 | |||
13 | lws = 3; | ||
14 | gap = 7; | ||
15 | |||
16 | J1 = get_win(Iw,round(cso),ws); | ||
17 | Jbar1 = abs(get_win5(Cresult,round(cso),ws)); | ||
18 | hists1 = get_hist(J1,Jbar1); | ||
19 | cumhists1 = get_cumhist(hists1); | ||
20 | [na1,nb1,nc1,nd1] = ks_2d(cumhists1.text'); | ||
21 | |||
22 | figure(4);colormap(gray); | ||
23 | imagesc(reshape(abs(Jbar1),size(Jbar1,1),size(Jbar1,2)*size(Jbar1,3))); | ||
24 | colorbar;axis('image');drawnow; | ||
25 | |||
26 | figure(2); | ||
27 | subplot(2,5,1);imagesc(J1);%colormap(gray) | ||
28 | subplot(2,5,2);imagesc(hists1.text');title('hist_1');colorbar; | ||
29 | subplot(2,5,3);imagesc(cumhists1.text');title('cumhist_1');colorbar; | ||
30 | subplot(2,5,4);imagesc(nc1);title('nc_1');colorbar | ||
31 | subplot(2,5,5);imagesc(nb1);title('nb_1');colorbar | ||
32 | drawnow; | ||
33 | |||
34 | k = sqrt(2)/2;M = prod(size(cumhists1.text)); | ||
35 | N = k*sqrt(M); | ||
36 | r1 = 0.001;r2 = 0.001; | ||
37 | con = N/(1+ (sqrt(1-0.5*(r1*r1+r2*r2)))*(0.25-0.75/N)); | ||
38 | |||
39 | jid = 1; | ||
40 | for jj=cso(2)-lws*gap:gap:cso(2)+lws*gap, | ||
41 | kid = 1; | ||
42 | for kk=cso(1)-lws*gap:gap:cso(1)+lws*gap, | ||
43 | |||
44 | figure(1);hold on; plot(kk,jj,'r*');drawnow; | ||
45 | |||
46 | J2 = get_win(Iw,round([kk,jj]),ws); | ||
47 | Jbar2 = get_win5(Cresult,round([kk,jj]),ws); | ||
48 | |||
49 | hists2 = get_hist(J2,Jbar2); | ||
50 | cumhists2 = get_cumhist(hists2); | ||
51 | [na2,nb2,nc2,nd2] = ks_2d(cumhists2.text'); | ||
52 | |||
53 | figure(2); | ||
54 | subplot(2,5,6);imagesc(J2);%colormap(gray) | ||
55 | subplot(2,5,7);imagesc(hists2.text');title('hist_2');colorbar | ||
56 | subplot(2,5,8);imagesc(cumhists2.text');title('cumhist_2');colorbar | ||
57 | |||
58 | diffa = abs(na2-na1);diffb =abs(nb2-nb1); | ||
59 | diffc = abs(nc2-nc1);diffd = abs(nd2-nd1); | ||
60 | |||
61 | maxs(1) = max(max(diffa));maxs(2) = max(max(diffb)); | ||
62 | maxs(3) = max(max(diffc));maxs(4) = max(max(diffd)); | ||
63 | |||
64 | maxs = maxs/6;for j=1:4, sig(j) = signif(con*maxs(j)); end | ||
65 | |||
66 | subplot(2,5,9);imagesc(diffc);title('diff_{nc}');colorbar | ||
67 | subplot(2,5,10);imagesc(diffb);title('diff_{nb} ');colorbar | ||
68 | |||
69 | disp(sprintf('max diff is %f\n',min(sig))); | ||
70 | |||
71 | A(jid,kid) = min(1,min(sig)); | ||
72 | |||
73 | %disp('press return to continue'); | ||
74 | pause(3); | ||
75 | |||
76 | kid = kid+1; | ||
77 | end | ||
78 | jid = jid+1; | ||
79 | end | ||
80 | |||
81 | figure(1);hold off; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/patch_cat.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/patch_cat.m new file mode 100755 index 0000000..0bf22c3 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/patch_cat.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function T = patch_cat(dotfilter,text_des) | ||
2 | |||
3 | T(:,:,2:7,:) = text_des; | ||
4 | clear text_des; | ||
5 | |||
6 | for k=1:size(T,4), | ||
7 | T(:,:,1,k) = dotfilter(:,:,1,k); | ||
8 | end | ||
9 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pgmread.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pgmread.m new file mode 100755 index 0000000..620408a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/pgmread.m | |||
@@ -0,0 +1,15 @@ | |||
1 | function img = pgmread(filename,size) | ||
2 | % function img = pgmread(filename) | ||
3 | % this is my version of pgmread for the pgm file created by XV. | ||
4 | % | ||
5 | % this program also corrects for the shifts in the image from pm file. | ||
6 | |||
7 | fid = fopen(filename,'r'); | ||
8 | |||
9 | for j=1:4, | ||
10 | a = fgetl(fid); | ||
11 | end | ||
12 | |||
13 | img = fscanf(fid,'%d',size); | ||
14 | img = img'; | ||
15 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/poisson.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/poisson.m new file mode 100755 index 0000000..99d49b6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/poisson.m | |||
@@ -0,0 +1,44 @@ | |||
1 | function [x] = Poisson(lambda); | ||
2 | %Poisson generates a random variable with a Poisson | ||
3 | % distribution | ||
4 | % Pr(x = n) = lambda^n exp(-lambda)/n | ||
5 | % | ||
6 | % [x] = Poisson(lambda); | ||
7 | % lambda: the parameter for the Poisson distribution | ||
8 | % (default is 1) | ||
9 | % x : the output number | ||
10 | % | ||
11 | |||
12 | %% | ||
13 | %% (C) Thomas K. Leung | ||
14 | %% University of California at Berkeley | ||
15 | %% Apr 25, 1995. | ||
16 | %% | ||
17 | |||
18 | %%% Notice that in this implementation, we are comparing log(Z) with | ||
19 | %%% T. In fact, we can compare T = exp(-lambda) with Z, which will | ||
20 | %%% save us from computing a large number of log's. However, when | ||
21 | %%% lambda is bigger than 709, exp(-lambda) = 0, which causes problem. | ||
22 | |||
23 | if nargin == 0 | ||
24 | lambda = 1; | ||
25 | end | ||
26 | |||
27 | if lambda < 0 | ||
28 | lambda = 1; | ||
29 | end | ||
30 | |||
31 | P = 0; | ||
32 | N = 0; | ||
33 | T = -lambda; | ||
34 | |||
35 | rand('seed',sum(100*clock)); | ||
36 | |||
37 | while P >= T | ||
38 | Z = rand(1); | ||
39 | P = P + log(Z); | ||
40 | N = N + 1; | ||
41 | end | ||
42 | |||
43 | x = N; | ||
44 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/poissonfield.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/poissonfield.m new file mode 100755 index 0000000..8051867 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/poissonfield.m | |||
@@ -0,0 +1,53 @@ | |||
1 | function [x,y,success] = PoissonField(int,sx,sy,ir); | ||
2 | %BF_HardCore Generates a hard core Poisson field | ||
3 | % [x,y] = Poisson_HC(int,ir,sx,sy); | ||
4 | % int: intensity of the field. (default is 1) | ||
5 | % ir : inhibition radius (default is 1) | ||
6 | % sx : size in x (default 100) | ||
7 | % sy : size in y (default 100) | ||
8 | % x : x coordinates | ||
9 | % y : y coordinates | ||
10 | % | ||
11 | |||
12 | %% | ||
13 | %% (C) Thomas K. Leung | ||
14 | %% University of California at Berkeley | ||
15 | %% April 26, 1995. | ||
16 | %% leungt@cajal.cs.berkeley.edu | ||
17 | %% | ||
18 | |||
19 | %% | ||
20 | %% Generate each point in sequence and reject it if it's too close to | ||
21 | %% previous ones. | ||
22 | %% | ||
23 | |||
24 | if nargin <= 0 | ||
25 | int = 1; | ||
26 | sx = 100; | ||
27 | sy = 100; | ||
28 | ir = 0; | ||
29 | elseif nargin <= 1 | ||
30 | sx = 100; | ||
31 | sy = 100; | ||
32 | ir = 0; | ||
33 | elseif nargin <= 2 | ||
34 | sy = 100; | ||
35 | ir = 0; | ||
36 | elseif nargin <= 3 | ||
37 | ir = 0; | ||
38 | end | ||
39 | |||
40 | %% | ||
41 | %% Notice that the average number of a poisson process is the | ||
42 | %% parameter lambda. This is consistent with our definition of the | ||
43 | %% intensity here. Intensity is the mean number of points inside each | ||
44 | %% unit area. Therefore, in a window of sx by sy, we should get | ||
45 | %% int*sx*sy number of points on average which is the mean number of | ||
46 | %% arrivals in a Poisson Process | ||
47 | %% | ||
48 | |||
49 | [n] = poisson(int * sx * sy); | ||
50 | |||
51 | [x,y,success] = binomialfield(n,sx,sy,ir); | ||
52 | |||
53 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/proj_back.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/proj_back.m new file mode 100755 index 0000000..069fe61 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/proj_back.m | |||
@@ -0,0 +1,24 @@ | |||
1 | function mask = proj_back(I,hw,mask_s) | ||
2 | % | ||
3 | % mask = proj_back(I,hw,mask_s) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | |||
8 | [nr,nc] = size(I2); | ||
9 | |||
10 | hw = 3; | ||
11 | st_sz = 2*hw + 1; | ||
12 | |||
13 | nr_chank = floor(nr/st_sz); | ||
14 | nc_chank = floor(nc/st_sz); | ||
15 | |||
16 | [x,y] = meshgrid(1:nc,1:nr); | ||
17 | |||
18 | ct_chank_x = round((x-hw-1)/st_sz) + 1; | ||
19 | ct_chank_y = round((y-hw-1)/st_sz) + 1; | ||
20 | |||
21 | idx = (ct_chank_x - 1)*nr_chank + ct_chank_y; | ||
22 | |||
23 | mask = full(sparse(y,x,mask_s(idx(:)))); | ||
24 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/proj_back_id.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/proj_back_id.m new file mode 100755 index 0000000..9db322e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/proj_back_id.m | |||
@@ -0,0 +1,19 @@ | |||
1 | function [vbig,bnr,bnc] = proj_back_id(v,gcs,gce,grs,gre) | ||
2 | % | ||
3 | % vbig = proj_back_id(v,gcs,gce,grs,gre) | ||
4 | % | ||
5 | |||
6 | nr = max(gre)+1; | ||
7 | nc = max(gce)+1; | ||
8 | |||
9 | sw = 3; | ||
10 | gap = 2*sw+1; | ||
11 | |||
12 | bnc = nc*gap; | ||
13 | bnr = nr*gap; | ||
14 | |||
15 | [x,y] = meshgrid(1:bnc,1:bnr); | ||
16 | |||
17 | idx = grs(y(:))+1+gcs(x(:))*nr; | ||
18 | |||
19 | vbig = full(sparse(y(:),x(:),v(idx))); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/quant.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/quant.m new file mode 100755 index 0000000..c2dfae6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/quant.m | |||
@@ -0,0 +1,20 @@ | |||
1 | function [x,map] = quant(d1,d2,d3,nbin,ws) | ||
2 | |||
3 | if (~exist('ws')), | ||
4 | ws = [1,1,1]; | ||
5 | end | ||
6 | |||
7 | d1 = d1-min(d1); | ||
8 | d2 = d2-min(d2); | ||
9 | d3 = d3-min(d3); | ||
10 | |||
11 | d1 = d1/max(d1); | ||
12 | d2 = d2/max(d2); | ||
13 | d3 = d3/max(d3); | ||
14 | |||
15 | d1 = d1*ws(1); | ||
16 | d2 = d2*ws(2); | ||
17 | d3 = d3*ws(3); | ||
18 | |||
19 | |||
20 | [x,map] = vmquant(d1,d2,d3,nbin); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpdm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpdm.m new file mode 100755 index 0000000..9a1068e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpdm.m | |||
@@ -0,0 +1,8 @@ | |||
1 | function I = readpfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2); | ||
6 | I = fscanf(fid,'%d',[A(1),A(2)]); | ||
7 | |||
8 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpfm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpfm.m new file mode 100755 index 0000000..48ecd78 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpfm.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function I = readpfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2); | ||
6 | I = fscanf(fid,'%f',[A(1),A(2)]); | ||
7 | |||
8 | %I = fscanf(fid,'%f',A(2)*A(1));I = reshape(I,A(1),A(2)); | ||
9 | |||
10 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpfm_id.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpfm_id.m new file mode 100755 index 0000000..5f5c4f7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpfm_id.m | |||
@@ -0,0 +1,21 @@ | |||
1 | function Is = readpfm(filename,ids,nodes) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2); | ||
6 | |||
7 | Is = zeros(length(ids),nodes); | ||
8 | |||
9 | ix = 1; | ||
10 | for j=1:max(ids), | ||
11 | I = fscanf(fid,'%f',nodes); | ||
12 | if (find(ids==j)), | ||
13 | Is(ix,:) = I(:)'; | ||
14 | ix = ix+1; | ||
15 | fprintf('.'); | ||
16 | end | ||
17 | end | ||
18 | |||
19 | %I = fscanf(fid,'%f',A(2)*A(1));I = reshape(I,A(1),A(2)); | ||
20 | |||
21 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpfm_idf.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpfm_idf.m new file mode 100755 index 0000000..d7916e7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpfm_idf.m | |||
@@ -0,0 +1,29 @@ | |||
1 | function Is = readpfm(filename,ids,nodes) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2); | ||
6 | |||
7 | Is = zeros(length(ids),nodes); | ||
8 | |||
9 | idt = sort(ids); | ||
10 | |||
11 | idh = 1; | ||
12 | |||
13 | ix = 1; | ||
14 | for j=1:length(ids), | ||
15 | |||
16 | gap = idt(j) - idh; | ||
17 | fprintf('%d',gap); | ||
18 | |||
19 | I = fscanf(fid,'%f',nodes*gap); | ||
20 | I = fscanf(fid,'%f',nodes); | ||
21 | |||
22 | Is(find(ids==idt(j)),:) = I(:)'; | ||
23 | fprintf('.'); | ||
24 | |||
25 | idh = idt(j)+1; | ||
26 | end | ||
27 | |||
28 | |||
29 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpgm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpgm.m new file mode 100755 index 0000000..a5fd7f2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpgm.m | |||
@@ -0,0 +1,26 @@ | |||
1 | function img = pgmread(filename) | ||
2 | % function img = pgmread(filename) | ||
3 | % this is my version of pgmread for the pgm file created by XV. | ||
4 | % | ||
5 | % this program also corrects for the shifts in the image from pm file. | ||
6 | |||
7 | |||
8 | fid = fopen(filename,'r'); | ||
9 | fscanf(fid, 'P5\n'); | ||
10 | cmt = '#'; | ||
11 | while findstr(cmt, '#'), | ||
12 | cmt = fgets(fid); | ||
13 | if length(findstr(cmt, '#')) ~= 1, | ||
14 | YX = sscanf(cmt, '%d %d'); | ||
15 | y = YX(1); x = YX(2); | ||
16 | end | ||
17 | end | ||
18 | |||
19 | fgets(fid); | ||
20 | |||
21 | %img = fscanf(fid,'%d',size); | ||
22 | %img = img'; | ||
23 | |||
24 | img = fread(fid,[y,x],'uint8'); | ||
25 | img = img'; | ||
26 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpnm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpnm.m new file mode 100755 index 0000000..ab78c2c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readpnm.m | |||
@@ -0,0 +1,21 @@ | |||
1 | function I = readpnm(name) | ||
2 | |||
3 | fid = fopen(name, 'r'); | ||
4 | fscanf(fid, 'P5\n'); | ||
5 | cmt = '#'; | ||
6 | while findstr(cmt, '#') == 1 | ||
7 | cmt = fgets(fid); | ||
8 | if findstr(cmt, '#') ~= 1 | ||
9 | YX = sscanf(cmt, '%d %d %d'); | ||
10 | y = YX(1); x = YX(2); nb = YX(3); | ||
11 | end | ||
12 | end | ||
13 | fgets(fid); | ||
14 | packed = fscanf(fid,'%f',[nb*y*x]); | ||
15 | |||
16 | for j = 1:nb, | ||
17 | I(:,:,j) = reshape(packed(j:nb:nb*y*x),y,x)'; | ||
18 | end | ||
19 | |||
20 | fclose(fid); | ||
21 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readppm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readppm.m new file mode 100755 index 0000000..300f597 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/readppm.m | |||
@@ -0,0 +1,19 @@ | |||
1 | function [r, g, b] = readppm(name) | ||
2 | |||
3 | fid = fopen(name, 'r'); | ||
4 | fscanf(fid, 'P6\n'); | ||
5 | cmt = '#'; | ||
6 | while findstr(cmt, '#') == 1 | ||
7 | cmt = fgets(fid); | ||
8 | if findstr(cmt, '#') ~= 1 | ||
9 | YX = sscanf(cmt, '%d %d'); | ||
10 | y = YX(1); x = YX(2); | ||
11 | end | ||
12 | end | ||
13 | fgets(fid); | ||
14 | packed = fread(fid,[3*y,x],'uint8')'; | ||
15 | r = packed(:,1:3:3*y); | ||
16 | g = packed(:,2:3:3*y); | ||
17 | b = packed(:,3:3:3*y); | ||
18 | fclose(fid); | ||
19 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/record.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/record.m new file mode 100755 index 0000000..9626038 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/record.m | |||
@@ -0,0 +1,6 @@ | |||
1 | load patch1; | ||
2 | doog2 = mkdoog2(2,10,0,80); | ||
3 | dog2 = rotate_J(90,doog2); | ||
4 | |||
5 | H = mkf_test(dog2,size(patch1),-1,0.00001,2,-1); | ||
6 | o = BfilterS(patch1,H,size(dog));figure(4);colormap(gray);imagesc(o.*(o>0)); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/recursive_cut_tc.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/recursive_cut_tc.m new file mode 100755 index 0000000..a8c9362 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/recursive_cut_tc.m | |||
@@ -0,0 +1,140 @@ | |||
1 | %function [groups,ids] = recursive_cut(ncutv,fn_base) | ||
2 | % | ||
3 | % | ||
4 | % function [groups,ids] = recursive_cut(ncutv,threshold,spthresh) | ||
5 | % | ||
6 | % | ||
7 | |||
8 | ncutv= ncutv_o(:,1:4); | ||
9 | |||
10 | fn_base = fn; | ||
11 | %fn_base = '130040'; | ||
12 | |||
13 | nvv = size(ncutv_o,2); | ||
14 | nbin = 24; | ||
15 | |||
16 | ids = []; | ||
17 | groups = []; | ||
18 | labels = []; | ||
19 | |||
20 | load cmaps | ||
21 | cmap = cmapg; | ||
22 | |||
23 | j = 1; | ||
24 | done = 0; | ||
25 | np = size(ncutv,1); | ||
26 | nv = size(ncutv,2); | ||
27 | |||
28 | %%%%%% find the cut for the first ncut vector | ||
29 | ev_id = 2; | ||
30 | para = [nvv ev_id nr nc 100]; | ||
31 | Gmask = ones(nr,nc); | ||
32 | %threshold = find_cutpoint(ncutv(:,ev_id),cmapg,nbin);threshold = threshold(1:end-1); | ||
33 | threshold = linspace(min(ncutv(:,ev_id)),max(ncutv(:,ev_id)),nbin); | ||
34 | [cut_threshold,max_asso] = find_bst_cut(fn_base,para,threshold,Gmask); | ||
35 | disp(max_asso); | ||
36 | |||
37 | id1 = find(ncutv(:,ev_id)<=cut_threshold); | ||
38 | id2 = find(ncutv(:,ev_id)>cut_threshold); | ||
39 | |||
40 | groups = [groups,id1(:)']; | ||
41 | ids = [ids,length(id1)]; | ||
42 | |||
43 | groups = [groups,id2(:)']; | ||
44 | ids = [ids,length(groups)]; | ||
45 | |||
46 | |||
47 | for j=3:nv, | ||
48 | fprintf('j = %d\n',j); | ||
49 | % expand the current level, | ||
50 | new_groups = []; | ||
51 | new_ids = []; | ||
52 | |||
53 | |||
54 | figure(4);ims(ncutv(:,j),nr,nc);title(num2str(j)); | ||
55 | |||
56 | figure(1);clf | ||
57 | disp_groups(groups,ids,nr,nc); | ||
58 | drawnow; | ||
59 | |||
60 | %figure(3); | ||
61 | % for each leaves, | ||
62 | mx = max(ncutv(:,j))-min(ncutv(:,j)); | ||
63 | %mx = std(ncutv(:,j)); | ||
64 | |||
65 | base_id =1; | ||
66 | for k=1:length(ids), | ||
67 | old_groups = groups(base_id:ids(k)); | ||
68 | |||
69 | v = ncutv(old_groups,j); | ||
70 | change_v = max(v)-min(v); | ||
71 | %change_v = std(v); | ||
72 | n1 = sum(v>(min(v)+0.85*change_v));%n1 = n1/length(old_groups); | ||
73 | n2 = sum(v<=(min(v)+0.15*change_v));%n2 = n2/length(old_groups); | ||
74 | disp(sprintf('n1 = %f, n2 = %f',n1,n2)); | ||
75 | |||
76 | figure(2); | ||
77 | Gmask = zeros(np,1); | ||
78 | Gmask(old_groups) = Gmask(old_groups)+1; | ||
79 | drawnow; | ||
80 | ims(ncutv(:,j).*Gmask,nr,nc); | ||
81 | |||
82 | disp(sprintf('!!!!!!!!!!!!!RATIO= %f',change_v/mx)) | ||
83 | |||
84 | %pause; | ||
85 | |||
86 | if (((change_v/mx)>0.5) & (n1>10) &(n2>10)), | ||
87 | |||
88 | ev_id = j; | ||
89 | |||
90 | %threshold = find_cutpoint(ncutv(old_groups,ev_id),cmapg,nbin);threshold = threshold(1:end-1); | ||
91 | threshold = linspace(min(ncutv(:,ev_id)),max(ncutv(:,ev_id)),nbin); | ||
92 | para = [nvv ev_id nr nc 100]; | ||
93 | [cut_threshold,max_asso] = find_bst_cut(fn_base,para,threshold,Gmask); | ||
94 | |||
95 | disp(max_asso); | ||
96 | |||
97 | if (max_asso>1.2), | ||
98 | id1 = find(ncutv(old_groups,ev_id)<=cut_threshold); | ||
99 | id2 = find(ncutv(old_groups,ev_id)>cut_threshold); | ||
100 | |||
101 | figure(5); | ||
102 | subplot(1,2,1);maskt= zeros(np,1);maskt(old_groups(id1))=1+maskt(old_groups(id1));ims(maskt,nr,nc); | ||
103 | subplot(1,2,2);maskt= zeros(np,1);maskt(old_groups(id2))=1+maskt(old_groups(id2));ims(maskt,nr,nc); | ||
104 | |||
105 | new_groups = [new_groups,old_groups(id1)]; | ||
106 | new_ids = [new_ids,length(new_groups)]; | ||
107 | |||
108 | |||
109 | new_groups = [new_groups,old_groups(id2)]; | ||
110 | new_ids = [new_ids,length(new_groups)]; | ||
111 | else | ||
112 | fprintf(' keep '); | ||
113 | new_groups = [new_groups,old_groups]; | ||
114 | new_ids = [new_ids,length(new_groups)]; | ||
115 | end | ||
116 | |||
117 | else | ||
118 | fprintf(' keep '); | ||
119 | new_groups = [new_groups,old_groups]; | ||
120 | new_ids = [new_ids,length(new_groups)]; | ||
121 | end | ||
122 | fprintf('\n'); | ||
123 | base_id = ids(k) + 1; | ||
124 | end | ||
125 | |||
126 | |||
127 | |||
128 | groups = new_groups; | ||
129 | ids = new_ids; | ||
130 | |||
131 | figure(1);disp_groups(groups,ids,nr,nc); | ||
132 | |||
133 | fprintf('press return\n'); | ||
134 | pause; | ||
135 | j= j+1; | ||
136 | end | ||
137 | |||
138 | fprintf('total group = %d \n',length(ids)); | ||
139 | |||
140 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/reduce_all.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/reduce_all.m new file mode 100755 index 0000000..d7d31f8 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/reduce_all.m | |||
@@ -0,0 +1,8 @@ | |||
1 | function b = reduce_all(a) | ||
2 | |||
3 | numband = size(a,3); | ||
4 | |||
5 | for j=1:numband, | ||
6 | |||
7 | b(:,:,j) = reduce(a(:,:,j)); | ||
8 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/rotate_J.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/rotate_J.m new file mode 100755 index 0000000..12f29b6 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/rotate_J.m | |||
@@ -0,0 +1,30 @@ | |||
1 | function J = compute_J(angle,I) | ||
2 | %% function J = compute_J(angle,I) | ||
3 | % | ||
4 | |||
5 | [size_y,size_x] = size(I); | ||
6 | |||
7 | [center_x,center_y] = find_center(size_x,size_y); | ||
8 | |||
9 | a = angle * pi/180; | ||
10 | A = [cos(a),-sin(a);sin(a),cos(a)]; | ||
11 | |||
12 | [XX,YY] = meshgrid(1:size_x,1:size_y); | ||
13 | |||
14 | x = reshape(XX,size_x*size_y,1); | ||
15 | y = reshape(YY,size_x*size_y,1); | ||
16 | index(:,1) = x-center_x; | ||
17 | index(:,2) = y-center_y; | ||
18 | |||
19 | position_new = A*index'; | ||
20 | position_new(1,:) = position_new(1,:)+center_x; | ||
21 | position_new(2,:) = position_new(2,:)+center_y; | ||
22 | |||
23 | position_new_x = reshape(position_new(1,:),size_y,size_x); | ||
24 | position_new_y = reshape(position_new(2,:),size_y,size_x); | ||
25 | |||
26 | J = m_interp4(I,position_new_x,position_new_y); | ||
27 | |||
28 | |||
29 | |||
30 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/session.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/session.m new file mode 100755 index 0000000..70fabbf --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/session.m | |||
@@ -0,0 +1,4 @@ | |||
1 | t = rj(1:50,19:50); tt = interp4(t,1); | ||
2 | sgn = mksgn2(182,2,[91,9]); | ||
3 | H = mkf_test(sgn,size(tt),1,0.01,2,300); | ||
4 | o = BfilterS(tt,H,size(sgn));figure(1);imagesc(o.*(o>0));axis('equal'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/show_cumhist.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/show_cumhist.m new file mode 100755 index 0000000..fe82e64 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/show_cumhist.m | |||
@@ -0,0 +1,28 @@ | |||
1 | function show_hist(cumhists,bins,fig_id,hold_flag,ct) | ||
2 | %% | ||
3 | % show_hist(cumhists,bins,fig_id,ct) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | if (~exist('ct')), | ||
8 | ct = 'b-o'; | ||
9 | end | ||
10 | |||
11 | figure(fig_id); | ||
12 | |||
13 | subplot(3,3,1);plot(bins.inten,cumhists.inten,ct); | ||
14 | |||
15 | if (hold_flag == 1), hold on;else hold off; end | ||
16 | |||
17 | for j=1:size(cumhists.text,2), | ||
18 | subplot(3,3,1+j); | ||
19 | plot(bins.text,cumhists.text(:,j),ct); | ||
20 | if (hold_flag == 1), hold on;else hold off; end | ||
21 | end | ||
22 | |||
23 | subplot(3,3,8); | ||
24 | plot(bins.mag,cumhists.mag,ct); | ||
25 | if (hold_flag == 1), hold on;else hold off; end | ||
26 | |||
27 | |||
28 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/show_hist.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/show_hist.m new file mode 100755 index 0000000..efaf899 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/show_hist.m | |||
@@ -0,0 +1,23 @@ | |||
1 | function show_hist(hists,bins,fig_id) | ||
2 | %% | ||
3 | % show_hist(hists,bins,fig_id) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | figure(fig_id); | ||
8 | |||
9 | subplot(3,3,1);bar(bins.inten,hists.inten); | ||
10 | |||
11 | %maxval = max(max(max(abs(Jbar)))); | ||
12 | |||
13 | for j=1:size(hists.text,2), | ||
14 | subplot(3,3,1+j);% hist(reshape(abs(Jbar(:,:,j)),prod(w),1),[1:10:maxval+1]); | ||
15 | bar(bins.text,hists.text(:,j)); | ||
16 | end | ||
17 | |||
18 | subplot(3,3,8);%hist(reshape(sum(abs(Jbar),3),prod(w),1),[1:10:161]); | ||
19 | bar(bins.mag,hists.mag); | ||
20 | |||
21 | |||
22 | |||
23 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/showsmm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/showsmm.m new file mode 100755 index 0000000..1833062 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/showsmm.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function [T,A,M2,TAM]=showsmm(L1,L2,phi,maxM); | ||
2 | % [T,A,M]=showsmm(L1,L2,phi,maxM); | ||
3 | |||
4 | if (~exist('maxM')), | ||
5 | % needs to know upper bound on M for given window function in smm | ||
6 | maxM=0.18; % temporary | ||
7 | end | ||
8 | |||
9 | |||
10 | A=1-L2./(L1+eps); | ||
11 | T=2*(phi+pi/2)/(2*pi); | ||
12 | M=L1+L2; | ||
13 | M2=min(M/maxM,1); % keep it from exceeding 1 | ||
14 | %M2 = sigmoid(M,maxM,30); | ||
15 | |||
16 | matlab5on = 1; | ||
17 | |||
18 | if matlab5on == 1, | ||
19 | TAM=hsv2rgb(T,A,M2); | ||
20 | |||
21 | figure(3); | ||
22 | image(TAM); | ||
23 | axis('tightequal'); | ||
24 | else | ||
25 | H = [reshape(T,prod(size(T)),1),... | ||
26 | reshape(A,prod(size(A)),1),... | ||
27 | reshape(M2,prod(size(M2)),1)]; | ||
28 | M = hsv2rgb(H); | ||
29 | [Ic,map] =vmquant(M(:,1),M(:,2),M(:,3),256); | ||
30 | |||
31 | image(reshape(Ic,size(T,1),size(T,2)));colormap(map); | ||
32 | end | ||
33 | |||
34 | if 0 | ||
35 | plot3(A(:).*M(:).*cos(2*pi*T(:)),A(:).*M(:).*sin(2*pi*T(:)),M(:),'.','markersize',15) | ||
36 | axis([-1 1 -1 1 0 1]) | ||
37 | [x,y,z] = cylinder(ones(1,5)); | ||
38 | x=x.*z; | ||
39 | y=y.*z; | ||
40 | hold on | ||
41 | h=mesh(x,y,z); | ||
42 | set(h,'edgecolor',[.2 .2 .2]); | ||
43 | hidden off | ||
44 | hold off | ||
45 | end \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/showsmm_v5.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/showsmm_v5.m new file mode 100755 index 0000000..937303d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/showsmm_v5.m | |||
@@ -0,0 +1,34 @@ | |||
1 | function [T,A,M2,TAM]=showsmm(L1,L2,phi,maxM); | ||
2 | % [T,A,M]=showsmm(L1,L2,phi,maxM); | ||
3 | |||
4 | if (~exist('maxM')), | ||
5 | % needs to know upper bound on M for given window function in smm | ||
6 | maxM=0.18; % temporary | ||
7 | end | ||
8 | |||
9 | |||
10 | A=1-L2./(L1+eps); | ||
11 | T=2*(phi+pi/2)/(2*pi); | ||
12 | M=L1+L2; | ||
13 | M2=min(M/maxM,1); % keep it from exceeding 1 | ||
14 | %M2 = sigmoid(M,maxM,30); | ||
15 | |||
16 | TAM=hsv2rgb(T,A,M2); | ||
17 | |||
18 | figure(3); | ||
19 | image(TAM); | ||
20 | axis('tightequal'); | ||
21 | |||
22 | |||
23 | if 0 | ||
24 | plot3(A(:).*M(:).*cos(2*pi*T(:)),A(:).*M(:).*sin(2*pi*T(:)),M(:),'.','markersize',15) | ||
25 | axis([-1 1 -1 1 0 1]) | ||
26 | [x,y,z] = cylinder(ones(1,5)); | ||
27 | x=x.*z; | ||
28 | y=y.*z; | ||
29 | hold on | ||
30 | h=mesh(x,y,z); | ||
31 | set(h,'edgecolor',[.2 .2 .2]); | ||
32 | hidden off | ||
33 | hold off | ||
34 | end \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/sigmoid.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/sigmoid.m new file mode 100755 index 0000000..996f7fe --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/sigmoid.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function a = sigmoid(x,offset,sig) | ||
2 | % | ||
3 | % a = sigmoid(x,offset,sig) | ||
4 | % | ||
5 | % a = ones(size(x))./(1+exp(-(x-offset)/sig)); | ||
6 | % | ||
7 | |||
8 | |||
9 | a = ones(size(x))./(1+exp(-(x-offset)/sig)); | ||
10 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/signif.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/signif.m new file mode 100755 index 0000000..f49eefc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/signif.m | |||
@@ -0,0 +1,22 @@ | |||
1 | function a = signif(b) | ||
2 | |||
3 | js = [1:101]; | ||
4 | |||
5 | |||
6 | if 0, | ||
7 | d = (-ones(size(js))).^(js-1); | ||
8 | d1 = exp(-2*(js.*js)*b*b); | ||
9 | |||
10 | a = 2*sum(d.*d1); | ||
11 | |||
12 | end | ||
13 | |||
14 | d1 = exp(-2*(js.*js)*b*b); | ||
15 | d2 = 4*(js.*js)*b*b - 1; | ||
16 | |||
17 | a = 2*sum(d1.*d2); | ||
18 | |||
19 | if (b<0.03),a = 1;end | ||
20 | |||
21 | |||
22 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/signif_N.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/signif_N.m new file mode 100755 index 0000000..82dc914 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/signif_N.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function a = signif_N(b,N) | ||
2 | % | ||
3 | % | ||
4 | % | ||
5 | |||
6 | Ne = sqrt(N*0.5); | ||
7 | |||
8 | cof = Ne + 0.155 + 0.24/Ne; | ||
9 | |||
10 | a= signif(cof*b); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/smooth.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/smooth.m new file mode 100755 index 0000000..919b53a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/smooth.m | |||
@@ -0,0 +1,20 @@ | |||
1 | % smooth an image | ||
2 | % coordinates (r, c) follow matrix convention; | ||
3 | % the gaussian is truncated at x = +- tail, and there are samples samples | ||
4 | % inbetween, where samples = hsamples * 2 + 1 | ||
5 | |||
6 | function g = smooth(image, hsamples) | ||
7 | |||
8 | tail=4; | ||
9 | samples = hsamples * 2 + 1; | ||
10 | |||
11 | x = linspace(-tail, tail, samples); | ||
12 | gauss = exp(-x.^2); | ||
13 | n = gauss * ones(samples,1); | ||
14 | gauss = gauss/n; | ||
15 | |||
16 | |||
17 | g = conv2(conv2(image, gauss), gauss'); | ||
18 | |||
19 | g = conv_trim(g, hsamples, hsamples); | ||
20 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/startup.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/startup.m new file mode 100755 index 0000000..8d38803 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/startup.m | |||
@@ -0,0 +1,9 @@ | |||
1 | %path(path,'/usr/sww/matlab-4.2c/toolbox/images'); | ||
2 | home_dir = '/home/barad-dur/vision/malik/jshi/'; | ||
3 | path(path,[home_dir,'matlab/toolbox/io']) | ||
4 | path(path,[home_dir,'matlab/pyramid']); | ||
5 | path(path,[home_dir,'matlab/toolbox/filter']) | ||
6 | path(path,[home_dir,'matlab/toolbox/disp']) | ||
7 | path(path,[home_dir,'matlab/vision/vision94/tracking/']) | ||
8 | path(path,[home_dir,'proj/grouping/laso']); | ||
9 | path(path,[home_dir,'proj/grouping/eig']); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/swarp.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/swarp.m new file mode 100755 index 0000000..60a4530 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/swarp.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function J = swarp(I) | ||
2 | |||
3 | [nr,nc] = size(I); | ||
4 | |||
5 | center_x = round(0.5*nc); | ||
6 | center_y = round(0.5*nr); | ||
7 | |||
8 | J = [I(center_y:nr,center_x:nc),I(center_y:nr,1:center_x-1);... | ||
9 | I(1:center_y-1,center_x:nc),I(1:center_y-1,1:center_x-1)]; \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/swarpback.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/swarpback.m new file mode 100755 index 0000000..513bc25 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/swarpback.m | |||
@@ -0,0 +1,12 @@ | |||
1 | function J = swarpback(I); | ||
2 | |||
3 | [nr,nc] = size(I); | ||
4 | |||
5 | center_x = round(0.5*nc); | ||
6 | center_y = round(0.5*nr); | ||
7 | |||
8 | cx= center_x -1; | ||
9 | cy= center_y -1; | ||
10 | |||
11 | J = [I(nr-cy+1:nr,nc-cx+1:nc),I(nr-cy+1:nr,1:(nc-center_x+1));... | ||
12 | I(1:(nr-center_y+1),nc-cx+1:nc),I(1:(nr-center_y+1),1:(nc-center_x+1))]; \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test.m new file mode 100755 index 0000000..12470eb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test.m | |||
@@ -0,0 +1,110 @@ | |||
1 | |||
2 | fn = '130065'; nr = 30;nc = 49; | ||
3 | %nr = 49;nc = 30; | ||
4 | %nc = 68;nr = 43; | ||
5 | %nr =49;nc = 30; | ||
6 | |||
7 | basedir = 'plaatje_data/newdata/'; | ||
8 | if 1, | ||
9 | fname = sprintf('%s%s_eigvec.pfm',basedir,fn); | ||
10 | eigv = readpfm(fname); | ||
11 | fname = sprintf('%s%s_eigval.pfm',basedir,fn); | ||
12 | eigval = readpfm(fname); | ||
13 | |||
14 | fname = sprintf('%s%s_ncutvec.pfm',basedir,fn); | ||
15 | ncutv = readpfm(fname); | ||
16 | fname = sprintf('%s%s_ncutval.pfm',basedir,fn); | ||
17 | ncutval = readpfm(fname); | ||
18 | else | ||
19 | fname = sprintf('%sncutvec_%s.pfm',basedir,fn); | ||
20 | ncutv = readpfm(fname); | ||
21 | fname = sprintf('%sncutval_%s.pfm',basedir,fn); | ||
22 | ncutval = readpfm(fname); | ||
23 | end | ||
24 | |||
25 | |||
26 | fname = sprintf('images/%s.pgm',fn); | ||
27 | I = readpgm(fname); | ||
28 | cutsz = 20; I = cutoff(I,cutsz); | ||
29 | figure(3);im(I);colormap(gray); | ||
30 | |||
31 | figure(6); | ||
32 | for j=1:min(8,size(ncutv,2)-1), | ||
33 | subplot(3,3,j); | ||
34 | im(reshape(ncutv(:,j+1),nr,nc));colorbar | ||
35 | title(num2str(ncutval(j+1,1))); | ||
36 | end | ||
37 | %cm = sprintf('print -dps ncut_%s',fn);disp(cm);eval(cm); | ||
38 | subplot(3,3,9);im(I);axis('off'); | ||
39 | |||
40 | ev = eigval(:,1); | ||
41 | figure(5);hold off;clf;subplot(1,2,1); | ||
42 | %semilogy((ev(1:end-1) - ev(2:end))./ev(1:end-1),'x-');grid on; | ||
43 | plot((ev(1:end-1) - ev(2:end))./ev(1:end-1),'x-');grid on; | ||
44 | %semilogy(0.01*ones(size(ev(2:end-1))),'r-');semilogy(0.005*ones(size(ev(2:end-1))),'r-');semilogy(0.0025*ones(size(ev(2:end-1))),'r-');grid on;hold off; | ||
45 | subplot(1,2,2); | ||
46 | %semilogy(ev(1:end-1)-ev(2:end),'p-');grid on; | ||
47 | semilogy((ev(1:end-1) - ev(2:end))/ev(1),'x-');grid on; | ||
48 | |||
49 | |||
50 | ncutv_o = ncutv; | ||
51 | |||
52 | recursive_cut_tc; | ||
53 | |||
54 | %[groups,ids] = recursive_cut(ncutv(:,1:4),fn); | ||
55 | |||
56 | masks = make_masks(groups,ids,nr,nc); | ||
57 | |||
58 | cm = sprintf('save masks_%s masks ncutv_o groups ids nr nc',fn); | ||
59 | disp(cm); | ||
60 | |||
61 | eval(cm); | ||
62 | |||
63 | |||
64 | %%%%%%%%%%%%%%%%%% | ||
65 | fn = '130040'; | ||
66 | cm = sprintf('load masks_%s',fn); | ||
67 | disp(cm); | ||
68 | eval(cm); | ||
69 | |||
70 | fn = '130040'; | ||
71 | fname= sprintf('images/%s.pgm',fn); | ||
72 | I = readpgm(fname);cutsz = 20; I = cutoff(I,cutsz); | ||
73 | figure(3);im(I);colormap(gray); | ||
74 | hw = 2; %nr = 43;nc=68; | ||
75 | gap = 2*hw+1; | ||
76 | %nr = 30;nc=49; | ||
77 | Is = I(1:nr*gap,1:nc*gap); | ||
78 | figure(3);im(Is);axis('off'); | ||
79 | |||
80 | %cm = sprintf('print -deps I_%s',fn);disp(cm);eval(cm); | ||
81 | |||
82 | |||
83 | |||
84 | %masks = make_masks(groups,ids,nr,nc); | ||
85 | figure(2);disp_groups(groups,ids,nr,nc); | ||
86 | |||
87 | figure(1); | ||
88 | Imasks = disp_Imask(Is,nr,nc,hw,masks); | ||
89 | |||
90 | for j=1:length(ids), | ||
91 | figure(4);colormap(gray);clf | ||
92 | im(Imasks(:,:,j));axis('off'); | ||
93 | cm = sprintf('print -deps result_cut_%s_%d',fn,j); | ||
94 | disp(cm);eval(cm); | ||
95 | |||
96 | %print -deps result_cut_134011_1 | ||
97 | end | ||
98 | |||
99 | |||
100 | if 0, | ||
101 | |||
102 | %load st_134013 | ||
103 | |||
104 | fn = '134013_t'; | ||
105 | |||
106 | I_max = 250; | ||
107 | tex_max = 40; | ||
108 | |||
109 | writeout_feature(I1,T1,fn,I_max,tex_max); | ||
110 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test1.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test1.m new file mode 100755 index 0000000..691b63e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test1.m | |||
@@ -0,0 +1,175 @@ | |||
1 | |||
2 | fnameI = '130068'; | ||
3 | |||
4 | cm = sprintf('load filter_%s.pgm.mat',fnameI); | ||
5 | disp(cm); | ||
6 | eval(cm); | ||
7 | |||
8 | text_des = abs(text_des); | ||
9 | |||
10 | |||
11 | %%%% cutoff margins, | ||
12 | margin = 6+10; | ||
13 | |||
14 | Iw = cutoff(I,margin); | ||
15 | |||
16 | T1= reshape(text_des,size(text_des,1),size(text_des,2),size(text_des,3)*size(text_des,4)); | ||
17 | T1 = cutoff(T1,margin); | ||
18 | |||
19 | %%%%% reduce resolution | ||
20 | |||
21 | |||
22 | |||
23 | T1 = reduce_all(T1); | ||
24 | T1 = reduce_all(T1); | ||
25 | |||
26 | im5(T1,5,6); | ||
27 | |||
28 | cm = sprintf('writepnm5(''%s_f.pnm'',%s)',fnameI,'T1/70'); | ||
29 | |||
30 | % disp(cm);eval(cm); | ||
31 | |||
32 | nr = size(T1,1); | ||
33 | nc = size(T1,2); | ||
34 | |||
35 | % D = mreadpfm('D_134011_f.pnm.pfm'); | ||
36 | |||
37 | % figure(3);imagesc(reshape(D,nc,nr)');axis('image');colorbar | ||
38 | |||
39 | if 0, | ||
40 | figure(7); | ||
41 | subplot(3,1,1);hist(reshape(I1,prod(size(I1)),1),binI); | ||
42 | subplot(3,1,2);hist(reshape(I2,prod(size(I2)),1),binI); | ||
43 | subplot(3,1,3);hist(reshape(I3,prod(size(I3)),1),binI); | ||
44 | |||
45 | |||
46 | If1 = filter_output(I1,sigs,szs); | ||
47 | If2 = filter_output(I2,sigs,szs); | ||
48 | If3 = filter_output(I3,sigs,szs); | ||
49 | |||
50 | I1a = cutoff(I1,5); If1 = cutoff(If1,5); | ||
51 | I2a = cutoff(I2,5); If2 = cutoff(If2,5); | ||
52 | I3a = cutoff(I3,5); If3 = cutoff(If3,5); | ||
53 | |||
54 | |||
55 | |||
56 | figure(4); | ||
57 | bint = [-0.15:0.02:0.15]; | ||
58 | id = 4; | ||
59 | |||
60 | If = If1; | ||
61 | for j=1:5, | ||
62 | subplot(5,2,2*(j-1)+1); | ||
63 | hist(reshape(If(:,:,id,j)./s1,prod(size(If(:,:,id,j))),1),bint); | ||
64 | end | ||
65 | |||
66 | If = If2; | ||
67 | for j=1:5, | ||
68 | subplot(5,2,2*j); | ||
69 | hist(reshape(If(:,:,id,j)./s2,prod(size(If(:,:,id,j))),1),bint); | ||
70 | end | ||
71 | |||
72 | |||
73 | %%% make 2d histogram bin | ||
74 | figure(5); | ||
75 | idmax = 5; | ||
76 | filt_id = 4; | ||
77 | |||
78 | for id=1:idmax, | ||
79 | |||
80 | subplot(idmax,3,(id-1)*3+1); | ||
81 | h2d1 = hist_I_f(I1a,If1(:,:,filt_id,id),binI,bintex); | ||
82 | imagesc(h2d1);axis('image') | ||
83 | subplot(idmax,3,(id-1)*3+2); | ||
84 | h2d2 = hist_I_f(I2a,If2(:,:,filt_id,id),binI,bintex); | ||
85 | imagesc(h2d2);axis('image') | ||
86 | |||
87 | subplot(idmax,3,id*3); | ||
88 | imagesc(h2d2/sum(sum(h2d2)) + h2d1/sum(sum(h2d1)));axis('image') | ||
89 | colorbar | ||
90 | end | ||
91 | |||
92 | %%%%%%%%%%%%%%%%%%%%% three types %%%%%%%% | ||
93 | figure(4); | ||
94 | idmax = 5; | ||
95 | filt_id = 2; | ||
96 | |||
97 | width = 4; | ||
98 | |||
99 | for id=1:idmax, | ||
100 | |||
101 | subplot(idmax,width,(id-1)*width+1); | ||
102 | h2d1 = hist_I_f(I1a,If1(:,:,filt_id,id),binI,bintex); | ||
103 | h2d1 = h2d1/sum(sum(h2d1)); | ||
104 | imagesc(h2d1);axis('image'); | ||
105 | |||
106 | subplot(idmax,width,(id-1)*width+2); | ||
107 | h2d2 = hist_I_f(I2a,If2(:,:,filt_id,id),binI,bintex); | ||
108 | h2d2 = h2d2/sum(sum(h2d2)); | ||
109 | imagesc(h2d2);axis('image') | ||
110 | |||
111 | subplot(idmax,width,(id-1)*width+3); | ||
112 | h2d3 = hist_I_f(I3a,If3(:,:,filt_id,id),binI,bintex); | ||
113 | h2d3 = h2d3/sum(sum(h2d3)); | ||
114 | imagesc(h2d3);axis('image') | ||
115 | |||
116 | subplot(idmax,width,id*width); | ||
117 | imagesc(h2d1+h2d2+h2d3);axis('image') | ||
118 | colorbar | ||
119 | end | ||
120 | |||
121 | |||
122 | %%%%%%%%%%%% smaller window %%%% | ||
123 | hw = round(4*sigs(1)); | ||
124 | |||
125 | figure(5);%imagesc(I1a);axis('image'); | ||
126 | cs = round(ginput(1)); | ||
127 | |||
128 | J = get_win(I1a,cs,[hw,hw]);figure(7);imagesc(J);axis('image'); | ||
129 | |||
130 | Jf = get_win5(If1,cs,[hw,hw]); | ||
131 | scales = 1:5; nscales = length(scales); | ||
132 | filters = 1:7; nfilters = length(filters); | ||
133 | |||
134 | figure(8); | ||
135 | for j=1:nscales, | ||
136 | for k=1:nfilters, | ||
137 | subplot(nscales,nfilters,(j-1)*nfilters+k); | ||
138 | h2d = hist_I_f(J,Jf(:,:,(j-1)*7+k));h2d = h2d/sum(sum(h2d)); | ||
139 | imagesc(h2d);axis('image');colorbar;axis('off'); | ||
140 | end | ||
141 | end | ||
142 | |||
143 | |||
144 | if 0, | ||
145 | |||
146 | figure(3); | ||
147 | cs = ginput(1); | ||
148 | |||
149 | ws = [15,15]; | ||
150 | J = get_win(I,cs,ws); | ||
151 | figure(6);imagesc(J);axis('image'); | ||
152 | |||
153 | t1 = get_win5(text_des,cs,ws); | ||
154 | |||
155 | t1p = abs(t1); | ||
156 | %t1p = abs(t1); | ||
157 | %t1p = t1.*(t1>0); | ||
158 | |||
159 | figure(2);im5(t1p,5,6); | ||
160 | |||
161 | t1p = reshape(t1p,size(t1p,1)*size(t1p,2),size(t1p,3))'; | ||
162 | |||
163 | t1pm = mean(t1p')'; | ||
164 | t1ps = t1p- t1pm*ones(1,size(t1p,2)); | ||
165 | |||
166 | B = t1ps*t1ps'; | ||
167 | [v,d] = eig(B);d = diag(d); | ||
168 | figure(4);plot(d,'x-'); | ||
169 | |||
170 | figure(5); | ||
171 | subplot(2,2,1);vid = 30;plot(reshape(v(:,vid),6,5),'x-'); | ||
172 | |||
173 | end | ||
174 | |||
175 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test2.m new file mode 100755 index 0000000..c70446a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test2.m | |||
@@ -0,0 +1,220 @@ | |||
1 | |||
2 | %fnameI = '130056'; | ||
3 | %fnameI = '134013'; | ||
4 | fnameI = '134007'; | ||
5 | |||
6 | %%%% flags %%%%%%%%% | ||
7 | read_image = 1; | ||
8 | |||
9 | margin = 10+6; | ||
10 | sigs = [1/sqrt(2),1,sqrt(2),2,2*sqrt(2)]; | ||
11 | r =3; | ||
12 | szs = round(3*r*sigs); | ||
13 | |||
14 | |||
15 | |||
16 | %%% image read %%% | ||
17 | if read_image, | ||
18 | cm = sprintf('I = readpgm(''images/%s.pgm'');',fnameI); | ||
19 | disp(cm); | ||
20 | eval(cm); | ||
21 | |||
22 | Iw = cutoff(I,margin); | ||
23 | figure(1);imagesc(Iw);axis('image'); | ||
24 | end | ||
25 | |||
26 | %%%% image crop %%% | ||
27 | figure(1);J = imcrop; | ||
28 | figure(2);imagesc(J);axis('image');drawnow; | ||
29 | |||
30 | Jf = filter_output(J,sigs,szs); | ||
31 | margin = 5; | ||
32 | Ja = cutoff(J,margin);Jfa = cutoff(Jf,margin); | ||
33 | figure(2);imagesc(Ja);axis('image'); | ||
34 | |||
35 | figure(3); | ||
36 | imagesc(Jfa(:,:,1,3));axis('image');drawnow; | ||
37 | |||
38 | Jfb = reshape(Jfa,size(Jfa,1),size(Jfa,2),size(Jfa,3)*size(Jfa,4)); | ||
39 | mag = sum(abs(Jfb),3); | ||
40 | |||
41 | %%%%%% Joint hist. %%%%%%%%% | ||
42 | |||
43 | filter_id = 1; | ||
44 | scale = 1; | ||
45 | h2d = hist_I_f(Ja,Jfa(:,:,filter_id,scale)); | ||
46 | |||
47 | figure(4); | ||
48 | imagesc(h2d/sum(sum(h2d)));axis('image');colorbar;colormap(hot); | ||
49 | |||
50 | |||
51 | %%%%%%%%%% Jointe hist of cropped area %%%%% | ||
52 | %%% block 1 | ||
53 | fig_id = 1; | ||
54 | [J3,f3,rect] = crop_im_fil(Ja,Jfa,fig_id); | ||
55 | |||
56 | filter_id = 1;scale = 1;H1 = hist_I_f(J1,f1(:,:,filter_id,scale)); | ||
57 | |||
58 | |||
59 | %%% block 2 | ||
60 | fig_id = 1; | ||
61 | [J2,f2,rect] = crop_im_fil(Ja,Jfa,fig_id); | ||
62 | |||
63 | filter_id = 1;scale = 1;H2 = hist_I_f(J2,f2(:,:,filter_id,scale)); | ||
64 | |||
65 | |||
66 | %%%%% disp result %%%%% | ||
67 | |||
68 | scales = [1:5]; | ||
69 | filter_ids = [1:7]; | ||
70 | |||
71 | figure(6);disp_hist2d(J2,f2,scales,filter_ids); | ||
72 | |||
73 | figure(4);disp_hist2d(J1,f1,scales,filter_ids); | ||
74 | |||
75 | %%%%%%%%%%%%%%%%% | ||
76 | %%%%%%%%%%%%%%%%% smaller window | ||
77 | hw = round(4*sigs(1)); | ||
78 | |||
79 | figure(2);%imagesc(Ja);axis('image'); | ||
80 | cs = round(ginput(1)); | ||
81 | |||
82 | J1 = get_win(Ja,cs,[hw,hw]);Jf1 = get_win5(Jfa,cs,[hw,hw]); | ||
83 | figure(4);imagesc(J1);axis('image');drawnow; | ||
84 | scales = [1:5];filter_ids = [1:7]; | ||
85 | figure(9);H2 = disp_hist2d(J1,Jf1,scales,filter_ids); | ||
86 | |||
87 | figure(6); disp_diff(H2,H2o); | ||
88 | |||
89 | |||
90 | %%%%%% difference in the neighbourhood %% | ||
91 | hw = round(4*sigs(1)); | ||
92 | hnb = 3; | ||
93 | |||
94 | B = compute_diff(Ja,Jfa,hw,hnb); | ||
95 | |||
96 | |||
97 | %%%%%%%%%% | ||
98 | |||
99 | if 0, | ||
100 | |||
101 | figure(4); | ||
102 | bint = [-0.15:0.02:0.15]; | ||
103 | id = 4; | ||
104 | |||
105 | If = If1; | ||
106 | for j=1:5, | ||
107 | subplot(5,2,2*(j-1)+1); | ||
108 | hist(reshape(If(:,:,id,j)./s1,prod(size(If(:,:,id,j))),1),bint); | ||
109 | end | ||
110 | |||
111 | If = If2; | ||
112 | for j=1:5, | ||
113 | subplot(5,2,2*j); | ||
114 | hist(reshape(If(:,:,id,j)./s2,prod(size(If(:,:,id,j))),1),bint); | ||
115 | end | ||
116 | |||
117 | |||
118 | %%% make 2d histogram bin | ||
119 | figure(5); | ||
120 | idmax = 5; | ||
121 | filt_id = 4; | ||
122 | |||
123 | for id=1:idmax, | ||
124 | |||
125 | subplot(idmax,3,(id-1)*3+1); | ||
126 | h2d1 = hist_I_f(I1a,If1(:,:,filt_id,id),binI,bintex); | ||
127 | imagesc(h2d1);axis('image') | ||
128 | subplot(idmax,3,(id-1)*3+2); | ||
129 | h2d2 = hist_I_f(I2a,If2(:,:,filt_id,id),binI,bintex); | ||
130 | imagesc(h2d2);axis('image') | ||
131 | |||
132 | subplot(idmax,3,id*3); | ||
133 | imagesc(h2d2/sum(sum(h2d2)) + h2d1/sum(sum(h2d1)));axis('image') | ||
134 | colorbar | ||
135 | end | ||
136 | |||
137 | %%%%%%%%%%%%%%%%%%%%% three types %%%%%%%% | ||
138 | figure(4); | ||
139 | idmax = 5; | ||
140 | filt_id = 2; | ||
141 | |||
142 | width = 4; | ||
143 | |||
144 | for id=1:idmax, | ||
145 | |||
146 | subplot(idmax,width,(id-1)*width+1); | ||
147 | h2d1 = hist_I_f(I1a,If1(:,:,filt_id,id),binI,bintex); | ||
148 | h2d1 = h2d1/sum(sum(h2d1)); | ||
149 | imagesc(h2d1);axis('image'); | ||
150 | |||
151 | subplot(idmax,width,(id-1)*width+2); | ||
152 | h2d2 = hist_I_f(I2a,If2(:,:,filt_id,id),binI,bintex); | ||
153 | h2d2 = h2d2/sum(sum(h2d2)); | ||
154 | imagesc(h2d2);axis('image') | ||
155 | |||
156 | subplot(idmax,width,(id-1)*width+3); | ||
157 | h2d3 = hist_I_f(I3a,If3(:,:,filt_id,id),binI,bintex); | ||
158 | h2d3 = h2d3/sum(sum(h2d3)); | ||
159 | imagesc(h2d3);axis('image') | ||
160 | |||
161 | subplot(idmax,width,id*width); | ||
162 | imagesc(h2d1+h2d2+h2d3);axis('image') | ||
163 | colorbar | ||
164 | end | ||
165 | |||
166 | |||
167 | %%%%%%%%%%%% smaller window %%%% | ||
168 | hw = round(4*sigs(1)); | ||
169 | |||
170 | figure(5);%imagesc(I1a);axis('image'); | ||
171 | cs = round(ginput(1)); | ||
172 | |||
173 | J = get_win(I1a,cs,[hw,hw]);figure(7);imagesc(J);axis('image'); | ||
174 | |||
175 | Jf = get_win5(If1,cs,[hw,hw]); | ||
176 | scales = 1:5; nscales = length(scales); | ||
177 | filters = 1:7; nfilters = length(filters); | ||
178 | |||
179 | figure(8); | ||
180 | for j=1:nscales, | ||
181 | for k=1:nfilters, | ||
182 | subplot(nscales,nfilters,(j-1)*nfilters+k); | ||
183 | h2d = hist_I_f(J,Jf(:,:,(j-1)*7+k));h2d = h2d/sum(sum(h2d)); | ||
184 | imagesc(h2d);axis('image');colorbar;axis('off'); | ||
185 | end | ||
186 | end | ||
187 | |||
188 | |||
189 | if 0, | ||
190 | |||
191 | figure(3); | ||
192 | cs = ginput(1); | ||
193 | |||
194 | ws = [15,15]; | ||
195 | J = get_win(I,cs,ws); | ||
196 | figure(6);imagesc(J);axis('image'); | ||
197 | |||
198 | t1 = get_win5(text_des,cs,ws); | ||
199 | |||
200 | t1p = abs(t1); | ||
201 | %t1p = abs(t1); | ||
202 | %t1p = t1.*(t1>0); | ||
203 | |||
204 | figure(2);im5(t1p,5,6); | ||
205 | |||
206 | t1p = reshape(t1p,size(t1p,1)*size(t1p,2),size(t1p,3))'; | ||
207 | |||
208 | t1pm = mean(t1p')'; | ||
209 | t1ps = t1p- t1pm*ones(1,size(t1p,2)); | ||
210 | |||
211 | B = t1ps*t1ps'; | ||
212 | [v,d] = eig(B);d = diag(d); | ||
213 | figure(4);plot(d,'x-'); | ||
214 | |||
215 | figure(5); | ||
216 | subplot(2,2,1);vid = 30;plot(reshape(v(:,vid),6,5),'x-'); | ||
217 | |||
218 | end | ||
219 | |||
220 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test3.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test3.m new file mode 100755 index 0000000..040ed3d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test3.m | |||
@@ -0,0 +1,4 @@ | |||
1 | fn = 'lightsmall.ppm'; | ||
2 | nr = Ipara(1);nc = Ipara(2); | ||
3 | |||
4 | k = 1;imagesc(reshape(v(:,k).*D,nc,nr)');colorbar \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_best_cut.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_best_cut.m new file mode 100755 index 0000000..e04c6ff --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_best_cut.m | |||
@@ -0,0 +1,12 @@ | |||
1 | |||
2 | fn_base = '134035'; | ||
3 | ev_id = 4; | ||
4 | para = [12 ev_id nr nc 100]; | ||
5 | Gmask = ones(nr,nc); | ||
6 | threshold = find_cutpoint(ncutv(:,ev_id),cmapg,12); | ||
7 | threshold = threshold(1:end-1); | ||
8 | |||
9 | cut_threshold = find_bst_cut(fn_base,para,threshold,Gmask); | ||
10 | |||
11 | figure(8);ims(ncutv(:,ev_id)<cut_threshold,nr,nc); | ||
12 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex.m new file mode 100755 index 0000000..fc420c7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex.m | |||
@@ -0,0 +1,361 @@ | |||
1 | |||
2 | setup_flag = 0; | ||
3 | cut_window_flag = 0; | ||
4 | run_flag = 0; | ||
5 | other_flag = 0; | ||
6 | test_flag = 1; | ||
7 | |||
8 | |||
9 | %%%%%%%%%%%%%%%%% | ||
10 | if setup_flag == 1, | ||
11 | I = readpgm('images/134035.pgm'); | ||
12 | |||
13 | sigs = [1/sqrt(2),1,sqrt(2),2,2*sqrt(2)];r = 3;szs = round(r*3*sigs); | ||
14 | %[text_des,TIw] = compute_filter(I,sigs,r,szs); | ||
15 | load filter_134035 | ||
16 | |||
17 | text_des = cutoff(text_des,szs(1)); | ||
18 | |||
19 | I_max = 255; | ||
20 | tex_max = 75; | ||
21 | |||
22 | TIw = cutoff(I,szs(1)); | ||
23 | figure(1);im(TIw); | ||
24 | |||
25 | fts = gen_filters(sigs,r,szs); | ||
26 | Ft = reshape(fts,size(fts,1)*size(fts,2),size(fts,3)*size(fts,4)); | ||
27 | Ft = Ft'; | ||
28 | PFt1 = pinv(Ft); | ||
29 | |||
30 | temp1 = zeros(2*szs(end-1)+1,2*szs(end-1)+1); | ||
31 | temp1(szs(end-1)+1,szs(end-1)+1) = 1; | ||
32 | Ft2 = [Ft;temp1(:)']; | ||
33 | PFt2 = pinv(Ft2); | ||
34 | |||
35 | |||
36 | end | ||
37 | |||
38 | %%%%%%%%%% | ||
39 | |||
40 | if cut_window_flag == 1, | ||
41 | wz = [30,30]; | ||
42 | |||
43 | figure(1); | ||
44 | ct0 = round(ginput(1)) | ||
45 | |||
46 | t0 = cutout(text_des,ct0,wz)/tex_max; | ||
47 | I0 = cutout(TIw,ct0,wz)/I_max; | ||
48 | |||
49 | figure(3);im(I0);colorbar | ||
50 | |||
51 | figure(1); | ||
52 | ct1 = round(ginput(1)) | ||
53 | |||
54 | t1 = cutout(text_des,ct1,wz)/tex_max; | ||
55 | I1 = cutout(TIw,ct1,wz)/I_max; | ||
56 | |||
57 | figure(3);im(I1);colorbar | ||
58 | |||
59 | %%%%%%%%%% | ||
60 | ts = cat(2,t0,t1); | ||
61 | Is = [I0,I1]; | ||
62 | |||
63 | figure(3); | ||
64 | subplot(2,1,1);im(Is); | ||
65 | subplot(2,1,2);im(ts(:,:,15)); | ||
66 | |||
67 | end | ||
68 | |||
69 | %%%%%% | ||
70 | |||
71 | if run_flag == 1, | ||
72 | |||
73 | neigs = 15; | ||
74 | |||
75 | fv1 = colize(t1,I1); | ||
76 | cov1 = fv1*fv1'; | ||
77 | |||
78 | [u1,s1] = eigs(cov1,neigs); s1= diag(s1); | ||
79 | figure(4);plot(s1,'p-'); | ||
80 | |||
81 | fv0 = colize(t0,I0); | ||
82 | cov0 = fv0*fv0'; | ||
83 | |||
84 | [u0,s0] = eigs(cov0,neigs); s0 = diag(s0); | ||
85 | figure(4);hold on; plot(s0,'rp-'); | ||
86 | |||
87 | fvs = colize(ts,Is); | ||
88 | covs = fvs*fvs'; | ||
89 | |||
90 | [us,ss] = eigs(covs,neigs); ss = diag(ss); | ||
91 | figure(4);plot(ss,'mo-'); | ||
92 | hold off | ||
93 | |||
94 | figure(5); | ||
95 | subplot(2,2,1); | ||
96 | im(cov0);colorbar; | ||
97 | subplot(2,2,2); | ||
98 | im(cov1);colorbar; | ||
99 | subplot(2,2,3); | ||
100 | im(covs);colorbar; | ||
101 | |||
102 | %%%%%%%%%%%%%%%%% | ||
103 | |||
104 | ivss = ones(size(ss))./sqrt(ss); | ||
105 | ivs0 = ones(size(s0))./sqrt(s0); | ||
106 | ivs1 = ones(size(s1))./sqrt(s1); | ||
107 | |||
108 | |||
109 | nv = 10; | ||
110 | v1 = fv1'*u1(:,1:nv)*diag(ivs1(1:nv)); | ||
111 | v0 = fv0'*u0(:,1:nv)*diag(ivs0(1:nv)); | ||
112 | vs = fvs'*us(:,1:nv)*diag(ivss(1:nv)); | ||
113 | |||
114 | |||
115 | %v1s = v1(1:50:3721,:); | ||
116 | vss = vs(1:10:7442,:); | ||
117 | |||
118 | if nv == 1, | ||
119 | mag = vss'; | ||
120 | else | ||
121 | mag = sum(vss'.*vss'); | ||
122 | end | ||
123 | |||
124 | tmp1 = vss*vss'; | ||
125 | figure(5);im(abs(tmp1)); | ||
126 | |||
127 | if 0, | ||
128 | magx = mag'*ones(1,length(mag)); | ||
129 | magy = ones(length(mag),1)*mag; | ||
130 | tmp2 = tmp1./(magx+magy); | ||
131 | figure(5);subplot(1,2,2);im(abs(tmp2));colorbar;axis('off'); | ||
132 | end | ||
133 | |||
134 | |||
135 | end | ||
136 | |||
137 | %%%%%%%%%%%%%%%%%%% | ||
138 | if other_flag == 1, | ||
139 | |||
140 | %tmp1 = PFt*us(:,1); | ||
141 | %im(reshape(tmp1,sqrt(length(tmp1)),sqrt(length(tmp1)))); | ||
142 | PFt = PFt1; | ||
143 | |||
144 | |||
145 | BI0 = back_proj(PFt,u0); | ||
146 | BI1 = back_proj(PFt,u1); | ||
147 | BIs = back_proj(PFt,us); | ||
148 | |||
149 | figure(7);im5(BI0(:,:,1:9),3,3); | ||
150 | figure(8);im5(BI1(:,:,1:9),3,3); | ||
151 | figure(9);im5(BIs(:,:,1:9),3,3); | ||
152 | |||
153 | |||
154 | %%%%%%%%%% | ||
155 | figure(12); | ||
156 | im(Is); | ||
157 | |||
158 | figure(13); | ||
159 | clf | ||
160 | x1 = vs(1:3721,2); | ||
161 | y1 = vs(1:3721,3); | ||
162 | subplot(1,2,1); | ||
163 | plot(x1,y1,'.') | ||
164 | axis('image');hold on; | ||
165 | subplot(1,2,2); | ||
166 | x2 = vs(3722:end,2); | ||
167 | y2 = vs(3722:end,3); | ||
168 | plot(x2,y2,'.') | ||
169 | axis('image');hold on; | ||
170 | |||
171 | |||
172 | figure(11); | ||
173 | subplot(1,2,1);hist0 = im_vect([y1,x1]',ones(size(y1))); | ||
174 | im(hist0);axis('xy'); | ||
175 | subplot(1,2,2);hist1 = im_vect([y2,x2]',ones(size(y1))); | ||
176 | im(hist1);axis('xy'); | ||
177 | |||
178 | end | ||
179 | |||
180 | if test_flag == 1, | ||
181 | figure(12); | ||
182 | ct = round(ginput(1)); | ||
183 | idx = (ct(1)-1)*size(Is,1) + ct(2); | ||
184 | |||
185 | cl = 'r'; | ||
186 | figure(13);subplot(1,2,1); | ||
187 | plot(vs(idx,2),vs(idx,3),[cl,'o']); | ||
188 | subplot(1,2,2);plot(vs(idx,2),vs(idx,3),[cl,'o']); | ||
189 | |||
190 | tmp = vs(idx,1:2)*vs(:,1:2)';tmp = reshape(tmp,size(Is,1),size(Is,2)); | ||
191 | figure(14);subplot(3,2,1); | ||
192 | im(abs(tmp));colorbar | ||
193 | subplot(3,2,2); im((Is+0.5).*(abs(tmp)>0.2*max(max(abs(tmp))))); | ||
194 | |||
195 | tmp = vs(idx,1:3)*vs(:,1:3)';tmp = reshape(tmp,size(Is,1),size(Is,2)); | ||
196 | subplot(3,2,3); | ||
197 | im(abs(tmp));colorbar; | ||
198 | subplot(3,2,4); im((Is+0.5).*(abs(tmp)>0.2*max(max(abs(tmp))))); | ||
199 | |||
200 | tmp = vs(idx,1:5)*vs(:,1:5)'; | ||
201 | tmp = reshape(tmp,size(Is,1),size(Is,2)); | ||
202 | subplot(3,2,5); | ||
203 | im(abs(tmp));colorbar | ||
204 | subplot(3,2,6); im((Is+0.5).*(abs(tmp)>0.2*max(max(abs(tmp))))); | ||
205 | |||
206 | end | ||
207 | |||
208 | |||
209 | %%%%%%%%%% | ||
210 | test_tmp = 0; | ||
211 | if test_tmp, | ||
212 | x = -10:0.02:20; | ||
213 | sig = 4; | ||
214 | d = exp(-(x.^2)/sig); | ||
215 | figure(2);plot(x,d); | ||
216 | |||
217 | ers = []; | ||
218 | for j=0:0.5:10, | ||
219 | d1 = exp(-(x-j).^2/sig); | ||
220 | hold on | ||
221 | plot(x,d1,'r'); | ||
222 | ers = [ers,sum((d1-d).^2)]; | ||
223 | end | ||
224 | hold off; | ||
225 | |||
226 | figure(1);plot(ers(end)-ers); | ||
227 | |||
228 | |||
229 | |||
230 | |||
231 | |||
232 | |||
233 | end | ||
234 | |||
235 | %%%%%%%%%%%%%%%%%%% | ||
236 | |||
237 | |||
238 | fvs = colize(ts,Is); | ||
239 | |||
240 | nf = 24;np = 0.5*7442; | ||
241 | hb.sigs = 0.02*ones(1,nf); | ||
242 | hb.bmins= -0.6*ones(1,nf); | ||
243 | hb.bmaxs= 0.6*ones(1,nf); | ||
244 | hb.nbins= 20*ones(1,nf); | ||
245 | |||
246 | %fh = colize_hist(fvs(1:nf,1:10:end),hb); | ||
247 | |||
248 | fh2 = hist_inner(fvs(1:nf,1:np),hb); | ||
249 | |||
250 | [u,d] = eigs(fh2,60); d = diag(d); | ||
251 | |||
252 | %%%%%%%%%%%% | ||
253 | figure(12); | ||
254 | ct = round(ginput(1)); | ||
255 | idx = (ct(:,1)-1)*size(Is,1) + ct(:,2); | ||
256 | |||
257 | dist = dist_pair(idx,fvs(1:nf,:),hb); | ||
258 | figure(4); | ||
259 | im(reshape(dist,size(Is,1),size(Is,2)));colorbar | ||
260 | |||
261 | |||
262 | %%%%%%%%% | ||
263 | figure(12); | ||
264 | ct = round(ginput(1)); | ||
265 | idx = (ct(:,1)-1)*size(Is,1) + ct(:,2); | ||
266 | |||
267 | a = colize_hist(fvs(1:nf,idx'),hb); | ||
268 | |||
269 | figure(5); | ||
270 | cl = 'brgm'; | ||
271 | for j=1:length(idx); | ||
272 | plot(a(:,j),cl(j)); | ||
273 | hold on; | ||
274 | end | ||
275 | hold off | ||
276 | |||
277 | %%%%%%%%%%% | ||
278 | |||
279 | %% use chanked feature vectors | ||
280 | |||
281 | chank_size = 1000; | ||
282 | fname = 'st'; | ||
283 | histbin_fv_chank(fvs(1:nf,:),hb,chank_size,fname); | ||
284 | |||
285 | |||
286 | covfh2 = hist_in_chank(fvs(1:nf,:),chank_size,hb.nbins(1)); | ||
287 | [u2,d2] = eigs(covfh2,60); d2 = diag(d2); | ||
288 | |||
289 | figure(4); | ||
290 | semilogy(d,'p-'); | ||
291 | |||
292 | figure(3);imagesc(u); | ||
293 | |||
294 | back_v = backproj_outer_chank(fvs,u,d,chank_size); | ||
295 | |||
296 | back_v2 = backproj_outer_chank2(fvs,u,d,chank_size); | ||
297 | |||
298 | |||
299 | %%%%%%%%%% | ||
300 | figure(2); | ||
301 | for j = 1:16, | ||
302 | subplot(4,4,j); | ||
303 | im(reshape(back_v(:,j),size(Is,1),size(Is,2))); | ||
304 | axis('off');title(num2str(j)); | ||
305 | end | ||
306 | |||
307 | binv = linspace(-0.6,0.6,20); | ||
308 | |||
309 | figure(4); | ||
310 | for j=1:16, | ||
311 | subplot(4,4,j); | ||
312 | imagesc(reshape(u(:,j),20,24));title(num2str(j));drawnow; | ||
313 | end | ||
314 | |||
315 | |||
316 | figure(6); | ||
317 | for j=1:16, | ||
318 | subplot(4,4,j); | ||
319 | plot(binv,(reshape(u(:,j),20,24)));title(num2str(j));drawnow; | ||
320 | end | ||
321 | |||
322 | |||
323 | %%%%%%%%%% | ||
324 | figure(12); | ||
325 | ct = round(ginput(1)); | ||
326 | idx = (ct(:,1)-1)*size(Is,1) + ct(:,2); | ||
327 | |||
328 | figure(5); | ||
329 | for j = 1:7*2, | ||
330 | subplot(7,2,j); | ||
331 | nvv = 2*j; | ||
332 | dist = back_v(idx,1:nvv)*back_v(:,1:nvv)'; | ||
333 | im(reshape(abs(dist).^2,size(Is,1),size(Is,2)));colorbar | ||
334 | axis('off');title(num2str(nvv)); | ||
335 | end | ||
336 | |||
337 | |||
338 | a = colize_hist(fvs(1:nf,idx'),hb)'; | ||
339 | |||
340 | dist_raw = dist_pair_chank(a,fvs,chank_size); | ||
341 | figure(3);im(reshape(dist_raw.^2,size(Is,1),size(Is,2))); | ||
342 | |||
343 | |||
344 | |||
345 | %%%%%%%%%%%%%% | ||
346 | figure(12); | ||
347 | ct_t3 = round(ginput(5)); | ||
348 | idx_t3 = (ct_t3(:,1)-1)*size(Is,1) + ct_t3(:,2); | ||
349 | |||
350 | a1 = colize_hist(fvs(1:nf,idx_t1'),hb)'; | ||
351 | a2 = colize_hist(fvs(1:nf,idx_t2'),hb)'; | ||
352 | a3 = colize_hist(fvs(1:nf,idx_t3'),hb)'; | ||
353 | |||
354 | |||
355 | %%%%%%%%%%% | ||
356 | figure(1); | ||
357 | for j=1:9, | ||
358 | subplot(3,3,j); | ||
359 | hist(back_v(:,j)) | ||
360 | end | ||
361 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex2.m new file mode 100755 index 0000000..9e1d3e7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex2.m | |||
@@ -0,0 +1,136 @@ | |||
1 | |||
2 | %%%%%%%%% test histogram on gray levels %%%%%%%%%%%%% | ||
3 | |||
4 | %load st | ||
5 | %fvs = colize(Is,Is); | ||
6 | |||
7 | nf = 1;np = 7442;nbins = 10; | ||
8 | |||
9 | hb.sigs = 0.02*ones(1,nf); | ||
10 | hb.bmins= 0*ones(1,nf); | ||
11 | hb.bmaxs= 1*ones(1,nf); | ||
12 | hb.nbins= nbins*ones(1,nf); | ||
13 | |||
14 | fh = colize_hist(fvs(1:nf,1:np),hb); | ||
15 | |||
16 | fh_inner = fh*fh'; | ||
17 | |||
18 | nv = nbins-1; | ||
19 | |||
20 | [u,d] = eigs(fh_inner,nv); d = diag(d); | ||
21 | |||
22 | figure(6); | ||
23 | for j=1:nv, | ||
24 | subplot(4,4,j); | ||
25 | plot(u(:,j)); | ||
26 | title(num2str(j)); | ||
27 | end | ||
28 | |||
29 | s = 1./sqrt(d); | ||
30 | |||
31 | back_v = (fh'*u(:,1:nv)).*(ones(np,1)*s(1:nv)'); | ||
32 | |||
33 | figure(7); | ||
34 | for j=1:nv, | ||
35 | subplot(4,4,j); | ||
36 | im(reshape(back_v(:,j),size(Is,1),size(Is,2)));axis('off'); | ||
37 | title(num2str(j)); | ||
38 | end | ||
39 | |||
40 | figure(1); | ||
41 | plot(d,'p-'); | ||
42 | figure(2); | ||
43 | im(u); | ||
44 | |||
45 | |||
46 | %%%%%%%%% try the joint x-I histogram bin %%%%%%%%%%%%% | ||
47 | |||
48 | x = [1:size(Is,1)]'*ones(1,size(Is,2)); | ||
49 | x = reshape(x,size(Is,1),size(Is,2)); | ||
50 | |||
51 | joint_f(:,:,1) = x; | ||
52 | joint_f(:,:,2) = Is; | ||
53 | |||
54 | fvs = colize(joint_f,Is); | ||
55 | |||
56 | nf = 2;np = 7442;nbins = [5,10]; | ||
57 | |||
58 | hb.sigs = [4,0.02].*ones(1,nf); | ||
59 | hb.bmins= [1,0].*ones(1,nf); | ||
60 | hb.bmaxs= [size(Is,1),1].*ones(1,nf); | ||
61 | hb.nbins= nbins.*ones(1,nf); | ||
62 | |||
63 | fh = colize_joint_hist(fvs,hb); | ||
64 | fh = reshape(fh,50,np); | ||
65 | |||
66 | fh_inner = fh*fh'; | ||
67 | |||
68 | nv = 30; | ||
69 | |||
70 | [u,d] = eigs(fh_inner,nv); d = diag(d); | ||
71 | |||
72 | figure(3); | ||
73 | for j=1:min(16,nv), | ||
74 | subplot(4,4,j); | ||
75 | im(reshape(u(:,j),5,10));axis('off'); | ||
76 | title(num2str(j)); | ||
77 | end | ||
78 | |||
79 | s = 1./sqrt(d); | ||
80 | |||
81 | back_v = (fh'*u(:,1:nv)).*(ones(np,1)*s(1:nv)'); | ||
82 | |||
83 | figure(4); | ||
84 | for j=1:min(16,nv), | ||
85 | subplot(4,4,j); | ||
86 | im(reshape(back_v(:,j),size(Is,1),size(Is,2)));axis('off'); | ||
87 | title(num2str(j)); | ||
88 | end | ||
89 | |||
90 | |||
91 | %%%%%%%% | ||
92 | |||
93 | |||
94 | joint_f = []; | ||
95 | |||
96 | joint_f(:,:,1) = Is; | ||
97 | joint_f(:,:,2) = ts(:,:,1); | ||
98 | |||
99 | fvs = colize(joint_f,Is); | ||
100 | |||
101 | nf = 2;np = 7442;nbins = [10,10]; | ||
102 | |||
103 | hb.sigs = [0.02,0.02].*ones(1,nf); | ||
104 | hb.bmins= [0,-0.6].*ones(1,nf); | ||
105 | hb.bmaxs= [1,0.6].*ones(1,nf); | ||
106 | hb.nbins= nbins.*ones(1,nf); | ||
107 | |||
108 | fh = colize_joint_hist(fvs,hb); | ||
109 | |||
110 | fh = reshape(fh,size(fh,1)*size(fh,2),np); | ||
111 | |||
112 | fh_inner = fh*fh'; | ||
113 | |||
114 | nv = 30; | ||
115 | |||
116 | [u,d] = eigs(fh_inner,nv); d = diag(d); | ||
117 | |||
118 | figure(3); | ||
119 | for j=1:min(16,nv), | ||
120 | subplot(4,4,j); | ||
121 | im(reshape(u(:,j),10,10));axis('off'); | ||
122 | title(num2str(j)); | ||
123 | end | ||
124 | |||
125 | s = 1./sqrt(d); | ||
126 | |||
127 | back_v = (fh'*u(:,1:nv)).*(ones(np,1)*s(1:nv)'); | ||
128 | |||
129 | figure(4); | ||
130 | for j=1:min(16,nv), | ||
131 | subplot(4,4,j); | ||
132 | im(reshape(back_v(:,j),size(Is,1),size(Is,2)));axis('off'); | ||
133 | title(num2str(j)); | ||
134 | end | ||
135 | |||
136 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex3.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex3.m new file mode 100755 index 0000000..2f245ec --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex3.m | |||
@@ -0,0 +1,169 @@ | |||
1 | %%%%%%%%% test histogram on gray levels %%%%%%%%%%%%% | ||
2 | |||
3 | %load st | ||
4 | |||
5 | nf = 24;np = 7442;nbins = 10; | ||
6 | fvs = colize(ts(:,:,1:nf),Is); | ||
7 | |||
8 | hb.sigs = 0.02*ones(1,nf); | ||
9 | hb.bmins= -0.6*ones(1,nf); | ||
10 | hb.bmaxs= 0.6*ones(1,nf); | ||
11 | hb.nbins= nbins*ones(1,nf); | ||
12 | |||
13 | fh = colize_hist(fvs(1:nf,1:np),hb); | ||
14 | |||
15 | nw = 4; | ||
16 | fhs = colize_histneighb(fh,Is,nw); | ||
17 | |||
18 | %%%%%%%%%%%%%%%%%% | ||
19 | figure(12); | ||
20 | ct = round(ginput(1)); | ||
21 | idx = (ct(:,1)-1)*size(Is,1) + ct(:,2); | ||
22 | |||
23 | figure(1); | ||
24 | subplot(1,2,1); | ||
25 | imagesc(reshape(fhs(:,idx),nbins,nf)) | ||
26 | subplot(1,2,2); | ||
27 | imagesc(reshape(fh(:,idx),nbins,nf)) | ||
28 | %%%%%%%%%% | ||
29 | |||
30 | fh = fhs; | ||
31 | fhs = sqrt(fhs); | ||
32 | |||
33 | fh_inner = fhs*fhs'; | ||
34 | |||
35 | nv = 30; | ||
36 | |||
37 | [u,d] = eigs(fh_inner,nv); d = diag(d); | ||
38 | |||
39 | figure(3); | ||
40 | for j=1:min(16,nv), | ||
41 | subplot(4,4,j); | ||
42 | %plot(u(:,j)); | ||
43 | im(reshape(u(:,j),nbins,nf)); | ||
44 | title(num2str(j)); | ||
45 | end | ||
46 | |||
47 | s = 1./sqrt(d); | ||
48 | |||
49 | back_v = (fhs'*u(:,1:nv)).*(ones(np,1)*s(1:nv)'); | ||
50 | |||
51 | figure(4); | ||
52 | for j=1:min(16,nv), | ||
53 | subplot(4,4,j); | ||
54 | im(reshape(back_v(:,j),size(Is,1),size(Is,2)));axis('off'); | ||
55 | title(num2str(j)); | ||
56 | end | ||
57 | |||
58 | figure(1); | ||
59 | semilogy(d,'p-'); | ||
60 | %figure(2);imagesc(u); | ||
61 | |||
62 | %%%%%%%%% | ||
63 | figure(12); | ||
64 | ct = round(ginput(1)); | ||
65 | idx = (ct(:,1)-1)*size(Is,1) + ct(:,2); | ||
66 | |||
67 | figure(5); | ||
68 | for j = 1:min(14,nv), | ||
69 | subplot(7,2,j); | ||
70 | nvv = j; | ||
71 | dist = back_v(idx,1:nvv)*back_v(:,1:nvv)'; | ||
72 | im(reshape(abs(dist).^2,size(Is,1),size(Is,2)));colorbar | ||
73 | axis('off');title(num2str(nvv)); | ||
74 | end | ||
75 | |||
76 | |||
77 | |||
78 | |||
79 | %%%%%%%%% try the joint x-I histogram bin %%%%%%%%%%%%% | ||
80 | |||
81 | x = [1:size(Is,1)]'*ones(1,size(Is,2)); | ||
82 | x = reshape(x,size(Is,1),size(Is,2)); | ||
83 | |||
84 | joint_f(:,:,1) = x; | ||
85 | joint_f(:,:,2) = Is; | ||
86 | |||
87 | fvs = colize(joint_f,Is); | ||
88 | |||
89 | nf = 2;np = 7442;nbins = [5,10]; | ||
90 | |||
91 | hb.sigs = [4,0.02].*ones(1,nf); | ||
92 | hb.bmins= [1,0].*ones(1,nf); | ||
93 | hb.bmaxs= [size(Is,1),1].*ones(1,nf); | ||
94 | hb.nbins= nbins.*ones(1,nf); | ||
95 | |||
96 | fh = colize_joint_hist(fvs,hb); | ||
97 | fh = reshape(fh,50,np); | ||
98 | |||
99 | fh_inner = fh*fh'; | ||
100 | |||
101 | nv = 30; | ||
102 | |||
103 | [u,d] = eigs(fh_inner,nv); d = diag(d); | ||
104 | |||
105 | figure(3); | ||
106 | for j=1:min(16,nv), | ||
107 | subplot(4,4,j); | ||
108 | im(reshape(u(:,j),5,10));axis('off'); | ||
109 | title(num2str(j)); | ||
110 | end | ||
111 | |||
112 | s = 1./sqrt(d); | ||
113 | |||
114 | back_v = (fh'*u(:,1:nv)).*(ones(np,1)*s(1:nv)'); | ||
115 | |||
116 | figure(4); | ||
117 | for j=1:min(16,nv), | ||
118 | subplot(4,4,j); | ||
119 | im(reshape(back_v(:,j),size(Is,1),size(Is,2)));axis('off'); | ||
120 | title(num2str(j)); | ||
121 | end | ||
122 | |||
123 | |||
124 | %%%%%%%% | ||
125 | |||
126 | |||
127 | joint_f = []; | ||
128 | |||
129 | joint_f(:,:,1) = Is; | ||
130 | joint_f(:,:,2) = ts(:,:,1); | ||
131 | |||
132 | fvs = colize(joint_f,Is); | ||
133 | |||
134 | nf = 2;np = 7442;nbins = [10,10]; | ||
135 | |||
136 | hb.sigs = [0.02,0.02].*ones(1,nf); | ||
137 | hb.bmins= [0,-0.6].*ones(1,nf); | ||
138 | hb.bmaxs= [1,0.6].*ones(1,nf); | ||
139 | hb.nbins= nbins.*ones(1,nf); | ||
140 | |||
141 | fh = colize_joint_hist(fvs,hb); | ||
142 | |||
143 | fh = reshape(fh,size(fh,1)*size(fh,2),np); | ||
144 | |||
145 | fh_inner = fh*fh'; | ||
146 | |||
147 | nv = 30; | ||
148 | |||
149 | [u,d] = eigs(fh_inner,nv); d = diag(d); | ||
150 | |||
151 | figure(3); | ||
152 | for j=1:min(16,nv), | ||
153 | subplot(4,4,j); | ||
154 | im(reshape(u(:,j),10,10));axis('off'); | ||
155 | title(num2str(j)); | ||
156 | end | ||
157 | |||
158 | s = 1./sqrt(d); | ||
159 | |||
160 | back_v = (fh'*u(:,1:nv)).*(ones(np,1)*s(1:nv)'); | ||
161 | |||
162 | figure(4); | ||
163 | for j=1:min(16,nv), | ||
164 | subplot(4,4,j); | ||
165 | im(reshape(back_v(:,j),size(Is,1),size(Is,2)));axis('off'); | ||
166 | title(num2str(j)); | ||
167 | end | ||
168 | |||
169 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex4.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex4.m new file mode 100755 index 0000000..61f1307 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex4.m | |||
@@ -0,0 +1,353 @@ | |||
1 | |||
2 | setup_flag = 0; | ||
3 | cut_window_flag = 0; | ||
4 | run_flag = 0; | ||
5 | other_flag = 0; | ||
6 | test_flag = 1; | ||
7 | |||
8 | |||
9 | %%%%%%%%%%%%%%%%% | ||
10 | if setup_flag == 1, | ||
11 | % = readpgm('images/134035.pgm'); | ||
12 | |||
13 | load st3 | ||
14 | |||
15 | I_max = 255; | ||
16 | tex_max = 40; | ||
17 | |||
18 | I2 = min(1,I2/I_max); | ||
19 | t2 = t2/tex_max; | ||
20 | t2 = t2.*(t2<=1) + 1*(t2>1); | ||
21 | t2 = t2.*(t2>=-1) + (-1)*(t2<-1); | ||
22 | |||
23 | |||
24 | end | ||
25 | |||
26 | %%%%%%%%%% | ||
27 | |||
28 | %% for a given sampling rate, get the index for window center | ||
29 | %% | ||
30 | |||
31 | [nr,nc] = size(I2); | ||
32 | |||
33 | hw = 3; | ||
34 | st_sz = 2*hw + 1; | ||
35 | |||
36 | nr_chank = floor(nr/st_sz); | ||
37 | nc_chank = floor(nc/st_sz); | ||
38 | |||
39 | id_chank = []; | ||
40 | for k=1+hw:st_sz:nc-hw, | ||
41 | for j=1+hw:st_sz:nr-hw, | ||
42 | id = j+(k-1)*nr; | ||
43 | id_chank = [id_chank,id]; | ||
44 | end | ||
45 | end | ||
46 | |||
47 | %%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
48 | |||
49 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
50 | %%%%% F1 difference %%%%% | ||
51 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
52 | |||
53 | fvs = 2*I2(:)'; fvs = fvs -1; | ||
54 | |||
55 | nf = 1; | ||
56 | hb.sigs = 0.02*ones(1,nf); | ||
57 | hb.bmins= -1*ones(1,nf); | ||
58 | hb.bmaxs= 1*ones(1,nf); | ||
59 | hb.nbins= 10*ones(1,nf); | ||
60 | |||
61 | fh = colize_hist(fvs(1:nf,:),hb); | ||
62 | fhs = colize_histnb_s(fh,I2,nw,hw); | ||
63 | |||
64 | A = fhs'*fhs; | ||
65 | figure(1);im(A);colorbar; | ||
66 | |||
67 | B = A; | ||
68 | |||
69 | %% display %%% | ||
70 | figure(12); | ||
71 | ct = round(ginput(1)); | ||
72 | ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
73 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
74 | |||
75 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
76 | |||
77 | figure(3); | ||
78 | im(reshape(A(idx,:),nr_chank,nc_chank));colorbar; | ||
79 | |||
80 | |||
81 | |||
82 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
83 | %%%%% F2 difference %%%%% | ||
84 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
85 | |||
86 | nw = 4;hw =3; | ||
87 | |||
88 | tnf = size(t2,3); | ||
89 | fst = 1; | ||
90 | r_id = 1; | ||
91 | for j=1:fst:tnf, | ||
92 | nf = fst; | ||
93 | hb.sigs = 0.02*ones(1,nf); hb.bmins= -1*ones(1,nf); | ||
94 | hb.bmaxs= 1*ones(1,nf); hb.nbins= 10*ones(1,nf); | ||
95 | |||
96 | fvs = colize(t2(:,:,j:j+fst-1),I2); | ||
97 | fh = colize_hist(fvs,hb); | ||
98 | fhs = colize_histnb_s(fh,I2,nw,hw); | ||
99 | A = fhs'*fhs; | ||
100 | cm = sprintf('save F%d A fhs',r_id+1); | ||
101 | disp(cm);eval(cm); | ||
102 | clear fh; | ||
103 | |||
104 | B = B + A; | ||
105 | |||
106 | clear A; | ||
107 | |||
108 | |||
109 | r_id = r_id +1; | ||
110 | end | ||
111 | |||
112 | |||
113 | %%%%%% debug + display %%%%%%%% | ||
114 | |||
115 | figure(6); | ||
116 | for j=2:30, | ||
117 | subplot(5,6,j); | ||
118 | im(t2(:,:,j-1));axis('off');title(num2str(j-1)); | ||
119 | end | ||
120 | subplot(5,6,1);im(I2);axis('off'); | ||
121 | |||
122 | |||
123 | figure(6); | ||
124 | B = zeros(size(A)); | ||
125 | for j = 1:31, | ||
126 | %subplot(5,6,j); | ||
127 | cm = sprintf('load F%d;',j); | ||
128 | disp(cm);eval(cm); | ||
129 | |||
130 | fhs1 = sqrt(fhs); A = fhs1'*fhs1; | ||
131 | % im(reshape(A(idx,:),nr_chank,nc_chank));axis('off');title(num2str(j-1));colorbar; | ||
132 | B = B+A; | ||
133 | end | ||
134 | |||
135 | |||
136 | %%%%%% disp dist. %%%%%% | ||
137 | figure(12); | ||
138 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
139 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
140 | |||
141 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
142 | |||
143 | figure(2); | ||
144 | im(reshape(B(idx,:),nr_chank,nc_chank));axis('off');title('B');colorbar; | ||
145 | |||
146 | |||
147 | |||
148 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
149 | %%%%% F3 features %%%%%% | ||
150 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
151 | |||
152 | %%% Joint Intensity with filters %%%% | ||
153 | |||
154 | |||
155 | tnf = size(t2,3); | ||
156 | |||
157 | plaatjeon = 1; | ||
158 | if plaatjeon, | ||
159 | for j=7:tnf, | ||
160 | cm = sprintf('!touch /disks/plaatje/scratch/jshi/FJ%d.mat',j); | ||
161 | disp(cm); | ||
162 | eval(cm);cm = sprintf('!ln -s /disks/plaatje/scratch/jshi/FJ%d.mat .',j); | ||
163 | disp(cm);eval(cm); | ||
164 | end | ||
165 | else | ||
166 | for j=1:1, | ||
167 | cm = sprintf('!touch ~/store/st/FJ%d.mat',j); | ||
168 | disp(cm);eval(cm); | ||
169 | cm = sprintf('!ln -s ~/store/st/FJ%d.mat .',j); | ||
170 | disp(cm);eval(cm); | ||
171 | end | ||
172 | end | ||
173 | |||
174 | for j=7:tnf, | ||
175 | nf = 2; | ||
176 | hb.sigs = 0.02*ones(1,nf); hb.bmins= -1*ones(1,nf); | ||
177 | hb.bmaxs= 1*ones(1,nf); hb.nbins= 10*ones(1,nf); | ||
178 | |||
179 | fvs = colize(cat(3,t2(:,:,j),I2)); | ||
180 | |||
181 | fhs = colize_histnb_sf(fvs,I2,hb,nw,hw); | ||
182 | fhs = sqrt(fhs); | ||
183 | A = fhs'*fhs; | ||
184 | cm = sprintf('save FJ%d A fhs',j); | ||
185 | disp(cm);eval(cm); | ||
186 | |||
187 | end | ||
188 | |||
189 | %%%% reload data %%%%%%%%%%%%%% | ||
190 | B = zeros(size(A)); | ||
191 | |||
192 | figure(3); | ||
193 | |||
194 | for j=1:tnf, | ||
195 | cm = sprintf('load FJ%d;',j); | ||
196 | disp(cm);eval(cm); | ||
197 | |||
198 | subplot(5,6,j); | ||
199 | im(reshape(A(idx,:),nr_chank,nc_chank));axis('off');title(num2str(j)); | ||
200 | |||
201 | B = B + A; | ||
202 | end | ||
203 | |||
204 | figure(2);im(reshape(B(idx,:),nr_chank,nc_chank));axis('off');title('B'); | ||
205 | |||
206 | |||
207 | %%%%%% disp dist. %%%%%% | ||
208 | figure(12); | ||
209 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
210 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
211 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
212 | |||
213 | figure(2); | ||
214 | im(reshape(A(idx,:),nr_chank,nc_chank));axis('off');colorbar; | ||
215 | |||
216 | %%%%%% disp Joint Hist %%%%%%%%% | ||
217 | |||
218 | figure(12); | ||
219 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
220 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
221 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
222 | |||
223 | figure(1); | ||
224 | im(reshape(fhs(:,idx),10,10));axis('off');colorbar; | ||
225 | |||
226 | |||
227 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
228 | %%%%% F4: Joint filters %%%%%% | ||
229 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
230 | |||
231 | |||
232 | |||
233 | tnf = size(t2,3); | ||
234 | |||
235 | nw = 4;hw =3; | ||
236 | |||
237 | for scale=1:5, | ||
238 | for angle = 1:3, | ||
239 | cm = sprintf('!touch /disks/plaatje/scratch/jshi/FFJ_%d_%d_%d_%d.mat',angle,angle+3,scale,scale); | ||
240 | disp(cm);eval(cm); | ||
241 | cm = sprintf('!ln -s /disks/plaatje/scratch/jshi/FFJ_%d_%d_%d_%d.mat .',angle,angle+3,scale,scale); | ||
242 | disp(cm);eval(cm); | ||
243 | end | ||
244 | end | ||
245 | |||
246 | |||
247 | for scale = 1:5, | ||
248 | for angle = 1:3, | ||
249 | nf = 2; | ||
250 | hb.sigs = 0.02*ones(1,nf); hb.bmins= -1*ones(1,nf); | ||
251 | hb.bmaxs= 1*ones(1,nf); hb.nbins= 10*ones(1,nf); | ||
252 | |||
253 | fvs = colize(cat(3,t2(:,:,(scale-1)*6+angle),... | ||
254 | t2(:,:,(scale-1)*6+angle+3))); | ||
255 | |||
256 | fhs = colize_histnb_sf(fvs,I2,hb,nw,hw); | ||
257 | fhs = sqrt(fhs); | ||
258 | A = fhs'*fhs; | ||
259 | cm = sprintf('save FFJ_%d_%d_%d_%d A fhs',angle,angle+3,scale,scale); | ||
260 | disp(cm);eval(cm); | ||
261 | end | ||
262 | end | ||
263 | |||
264 | |||
265 | %%%%%%%%% load results %%%%%%%%%%% | ||
266 | %B = zeros(size(A)); | ||
267 | |||
268 | figure(3); | ||
269 | for scale=1:5, | ||
270 | for angle = 1:3, | ||
271 | cm = sprintf('load FFJ_%d_%d_%d_%d.mat',angle,angle+3,scale,scale); | ||
272 | disp(cm);eval(cm); | ||
273 | |||
274 | subplot(3,5,scale+(angle-1)*5); | ||
275 | im(reshape(A(idx,:),nr_chank,nc_chank)); | ||
276 | axis('off');title(sprintf('%d-%d,%d',angle,angle+3,scale)); | ||
277 | |||
278 | %B = B + A; | ||
279 | end | ||
280 | end | ||
281 | |||
282 | |||
283 | |||
284 | |||
285 | %%% disp results | ||
286 | |||
287 | angle = 1;scale = 1; | ||
288 | cm = sprintf('load FFJ_%d_%d_%d_%d.mat',angle,angle+3,scale,scale); | ||
289 | disp(cm);eval(cm); | ||
290 | |||
291 | |||
292 | |||
293 | figure(12); | ||
294 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
295 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
296 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
297 | |||
298 | %figure(1);im(reshape(fhs(:,idx),10,10));axis('off');%colorbar; | ||
299 | %figure(2);im(reshape(A(idx,:),nr_chank,nc_chank));%axis('off');%title('B'); | ||
300 | figure(4);im(reshape(B(idx,:),nr_chank,nc_chank));%axis('off');%title('B'); | ||
301 | |||
302 | |||
303 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
304 | |||
305 | %%%%%% reduction %%%%%%%%%%%%%%%%% | ||
306 | nv = 50; | ||
307 | [uB,dB] = eigs(B,nv);dB = diag(dB); | ||
308 | |||
309 | figure(1);subplot(2,1,1);plot(dB,'p-'); | ||
310 | subplot(2,1,2);semilogy(dB,'p-'); | ||
311 | |||
312 | figure(2); | ||
313 | |||
314 | for j=1:20, | ||
315 | subplot(4,5,j); | ||
316 | im(reshape(uB(:,j),nr_chank,nc_chank));axis('off');colorbar;title(num2str(j)); | ||
317 | end | ||
318 | |||
319 | |||
320 | %%%%% Ncut without reduction %%%% | ||
321 | [uNu,dNu] = eig_decomp_v5(B,20); | ||
322 | |||
323 | figure(1);subplot(2,1,1);plot(dNu,'p-'); | ||
324 | subplot(2,1,2);semilogy(dNu,'p-'); | ||
325 | |||
326 | figure(2); | ||
327 | for j=2:6, | ||
328 | subplot(1,5,j-1); | ||
329 | im(reshape(-uNu(:,j),nr_chank,nc_chank));axis('off');colorbar;title(num2str(j)); | ||
330 | end | ||
331 | |||
332 | %%%%%% Ncut with reduction %%%%%%%%% | ||
333 | nvv = 6; | ||
334 | B1 = uB(:,1:nvv)*uB(:,1:nvv)'; | ||
335 | |||
336 | |||
337 | [uN,dN] = eig_decomp_v5(abs(B1),20); | ||
338 | |||
339 | figure(1);subplot(2,1,1);plot(dN,'p-'); | ||
340 | subplot(2,1,2);semilogy(dN,'p-'); | ||
341 | |||
342 | figure(3); | ||
343 | for j=2:6, | ||
344 | subplot(1,5,j-1); | ||
345 | im(reshape(uN(:,j),nr_chank,nc_chank));axis('off');colorbar;title(num2str(j)); | ||
346 | end | ||
347 | |||
348 | |||
349 | %%%%%% | ||
350 | |||
351 | |||
352 | |||
353 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex5.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex5.m new file mode 100755 index 0000000..2b86e0a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_evtex5.m | |||
@@ -0,0 +1,446 @@ | |||
1 | |||
2 | setup_flag = 0; | ||
3 | cut_window_flag = 0; | ||
4 | run_flag = 0; | ||
5 | other_flag = 0; | ||
6 | test_flag = 1; | ||
7 | |||
8 | |||
9 | %%%%%%%%%%%%%%%%% | ||
10 | if setup_flag == 1, | ||
11 | |||
12 | sigs = [1/sqrt(2),1,sqrt(2),2,2*sqrt(2)];r = 3;szs = round(r*3*sigs); | ||
13 | szs = szs(length(szs))*ones(1,length(szs)); | ||
14 | num_ori = 6; | ||
15 | |||
16 | compute_flag = 0; | ||
17 | if compute_flag, | ||
18 | fnames = [134002,134007,134011,134013,130065,130038,130039,130040,130042,... | ||
19 | 130045,130046,130056,130068]; | ||
20 | |||
21 | for j=1:length(fnames), | ||
22 | fname = sprintf('images/%d.pgm',fnames(j)); | ||
23 | |||
24 | cm = sprintf('!touch /disks/plaatje/scratch/jshi/Fe_%d.mat',fnames(j)); | ||
25 | disp(cm);eval(cm); | ||
26 | |||
27 | cm = sprintf('!ln -s /disks/plaatje/scratch/jshi/Fe_%d.mat .',fnames(j)); | ||
28 | disp(cm);eval(cm); | ||
29 | |||
30 | disp(fname); | ||
31 | I = readpgm(fname);figure(3);im(I);title(num2str(fname));drawnow; | ||
32 | [text_des,filters] = compute_filter_fft(I,sigs,r,szs,num_ori); | ||
33 | |||
34 | cm = sprintf('save Fe_%d text_des filters fname sigs r szs num_ori',fnames(j)); | ||
35 | disp(cm);eval(cm); | ||
36 | |||
37 | clear text_des filters I | ||
38 | end | ||
39 | |||
40 | end | ||
41 | else | ||
42 | %%%%%%%%%%%%% | ||
43 | fname = 134013; | ||
44 | |||
45 | Iname = sprintf('images/%d.pgm',fname); | ||
46 | I = readpgm(Iname); | ||
47 | |||
48 | cm = sprintf('load Fe_%d.mat',fname); | ||
49 | disp(cm);eval(cm); | ||
50 | |||
51 | figure(1);im(I); | ||
52 | |||
53 | |||
54 | cutsz =20; | ||
55 | I = cutoff(I,cutsz);figure(1);im(I); | ||
56 | text_des = cutoff(text_des,cutsz); | ||
57 | |||
58 | figure(2); | ||
59 | for j =1:30, | ||
60 | subplot(5,6,j);im(text_des(:,:,j));axis('off'); | ||
61 | end | ||
62 | |||
63 | I1 = I(20:200,70:240); | ||
64 | T1 = text_des(20:200,70:240,:); | ||
65 | |||
66 | save st_134013 I1 T1 fname sigs szs r num_ori | ||
67 | |||
68 | end | ||
69 | |||
70 | |||
71 | |||
72 | %%%%%%%%%%% normalization %%%%%%%%%%% | ||
73 | |||
74 | |||
75 | I_max = 250; | ||
76 | tex_max = 40; | ||
77 | |||
78 | I1 = min(1,I1/I_max); | ||
79 | T1 = T1/tex_max; | ||
80 | T1 = T1.*(T1<=1) + 1*(T1>1); | ||
81 | T1 = T1.*(T1>=-1) + (-1)*(T1<-1); | ||
82 | |||
83 | |||
84 | end | ||
85 | |||
86 | %%%%%%%%%% | ||
87 | |||
88 | %% for a given sampling rate, get the index for window center | ||
89 | %% | ||
90 | |||
91 | [nr,nc] = size(I1); | ||
92 | |||
93 | hw = 3; | ||
94 | st_sz = 2*hw + 1; | ||
95 | |||
96 | nr_chank = floor(nr/st_sz); | ||
97 | nc_chank = floor(nc/st_sz); | ||
98 | |||
99 | id_chank = []; | ||
100 | for k=1+hw:st_sz:nc-hw, | ||
101 | for j=1+hw:st_sz:nr-hw, | ||
102 | id = j+(k-1)*nr; | ||
103 | id_chank = [id_chank,id]; | ||
104 | end | ||
105 | end | ||
106 | |||
107 | %%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
108 | |||
109 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
110 | %%%%% F1 difference %%%%% | ||
111 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
112 | |||
113 | fvs = 2*I1(:)'; fvs = fvs -1; | ||
114 | |||
115 | nf = 1; | ||
116 | hb.sigs = 0.02*ones(1,nf); | ||
117 | hb.bmins= -1*ones(1,nf); | ||
118 | hb.bmaxs= 1*ones(1,nf); | ||
119 | hb.nbins= 10*ones(1,nf); | ||
120 | nw = 4;hw =3; | ||
121 | |||
122 | fh = colize_hist(fvs(1:nf,:),hb); | ||
123 | fhs = colize_histnb_s(fh,I1,nw,hw); | ||
124 | |||
125 | fhs = sqrt(fhs); | ||
126 | A = fhs'*fhs; | ||
127 | figure(2);im(A);colorbar; | ||
128 | |||
129 | B = A; | ||
130 | |||
131 | %% display %%% | ||
132 | figure(2); | ||
133 | ct = round(ginput(1)); | ||
134 | ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
135 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
136 | |||
137 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
138 | |||
139 | figure(3);im(reshape(A(idx,:),nr_chank,nc_chank));colorbar; | ||
140 | |||
141 | subplot(1,2,1);im(reshape(A1(idx,:),nr_chank,nc_chank));colorbar; | ||
142 | subplot(1,2,2);im(reshape(A2(idx,:),nr_chank,nc_chank));colorbar; | ||
143 | |||
144 | |||
145 | %%%%%%%%%% | ||
146 | save_flag = 0; | ||
147 | |||
148 | fn = 134013; | ||
149 | |||
150 | if save_flag, | ||
151 | cm = sprintf('save F1_%d fhs hw nw nr_chank nc_chank',fn); | ||
152 | disp(cm);eval(cm); | ||
153 | |||
154 | end | ||
155 | |||
156 | load_flag = 1; | ||
157 | if load_flag, | ||
158 | cm = sprintf('load F1_%d',fn); | ||
159 | disp(cm);eval(cm); | ||
160 | |||
161 | A=fhs'*fhs; | ||
162 | end | ||
163 | |||
164 | |||
165 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
166 | %%%%% F2 difference %%%%% | ||
167 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
168 | |||
169 | nw = 4;hw =3; | ||
170 | |||
171 | tnf = size(T1,3); | ||
172 | fst = 1; | ||
173 | |||
174 | for j=1:fst:1, | ||
175 | nf = fst; | ||
176 | hb.sigs = 0.02*ones(1,nf); hb.bmins= -1*ones(1,nf); | ||
177 | hb.bmaxs= 1*ones(1,nf); hb.nbins= 15*ones(1,nf); | ||
178 | |||
179 | fvs = colize(T1(:,:,j:j+fst-1),I1); | ||
180 | fh = colize_hist(fvs,hb); | ||
181 | fhs = colize_histnb_s(fh,I1,nw,hw); | ||
182 | fhs = sqrt(fhs); | ||
183 | |||
184 | A = fhs'*fhs; | ||
185 | |||
186 | cm = sprintf('save F2_%d_%d fhs hw nw nr_chank nc_chank',j,fn); | ||
187 | disp(cm);eval(cm); | ||
188 | clear fh; | ||
189 | |||
190 | B = B + A; | ||
191 | |||
192 | clear A; | ||
193 | |||
194 | end | ||
195 | |||
196 | |||
197 | %%%%%% debug + display %%%%%%%% | ||
198 | |||
199 | figure(6); | ||
200 | for j=2:30, | ||
201 | subplot(5,6,j); | ||
202 | im(T1(:,:,j-1));axis('off');title(num2str(j-1)); | ||
203 | end | ||
204 | subplot(5,6,1);im(I1);axis('off'); | ||
205 | |||
206 | |||
207 | figure(6); | ||
208 | B = zeros(size(A)); | ||
209 | for j = 1:31, | ||
210 | %subplot(5,6,j); | ||
211 | cm = sprintf('load F%d;',j); | ||
212 | disp(cm);eval(cm); | ||
213 | |||
214 | fhs1 = sqrt(fhs); A = fhs1'*fhs1; | ||
215 | % im(reshape(A(idx,:),nr_chank,nc_chank));axis('off');title(num2str(j-1));colorbar; | ||
216 | B = B+A; | ||
217 | end | ||
218 | |||
219 | |||
220 | %%%%%% disp dist. %%%%%% | ||
221 | weight= 5; | ||
222 | A = weight*B+B2; | ||
223 | |||
224 | figure(1); | ||
225 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
226 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
227 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
228 | |||
229 | figure(2); | ||
230 | im(reshape(A(idx,:),nr_chank,nc_chank));axis('off');colorbar; %title('B'); | ||
231 | %figure(3); | ||
232 | |||
233 | |||
234 | save_flag = 0; | ||
235 | if save_flag , | ||
236 | B2 = B; | ||
237 | save tmp B2 nr_chank nc_chank | ||
238 | end | ||
239 | |||
240 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
241 | %%%%% F3 features %%%%%% | ||
242 | %%%%%%%%%%%%%%%%%%%%%%%%%% | ||
243 | |||
244 | %%% Joint Intensity with filters %%%% | ||
245 | |||
246 | |||
247 | tnf = size(T1,3); | ||
248 | |||
249 | plaatjeon = 1; | ||
250 | if plaatjeon, | ||
251 | for j=7:tnf, | ||
252 | cm = sprintf('!touch /disks/plaatje/scratch/jshi/FJ%d.mat',j); | ||
253 | disp(cm); | ||
254 | eval(cm);cm = sprintf('!ln -s /disks/plaatje/scratch/jshi/FJ%d.mat .',j); | ||
255 | disp(cm);eval(cm); | ||
256 | end | ||
257 | else | ||
258 | for j=1:1, | ||
259 | cm = sprintf('!touch ~/store/st/FJ%d.mat',j); | ||
260 | disp(cm);eval(cm); | ||
261 | cm = sprintf('!ln -s ~/store/st/FJ%d.mat .',j); | ||
262 | disp(cm);eval(cm); | ||
263 | end | ||
264 | end | ||
265 | |||
266 | for j=7:tnf, | ||
267 | nf = 2; | ||
268 | hb.sigs = 0.02*ones(1,nf); hb.bmins= -1*ones(1,nf); | ||
269 | hb.bmaxs= 1*ones(1,nf); hb.nbins= 10*ones(1,nf); | ||
270 | |||
271 | fvs = colize(cat(3,T1(:,:,j),I1)); | ||
272 | |||
273 | fhs = colize_histnb_sf(fvs,I1,hb,nw,hw); | ||
274 | fhs = sqrt(fhs); | ||
275 | A = fhs'*fhs; | ||
276 | cm = sprintf('save FJ%d A fhs',j); | ||
277 | disp(cm);eval(cm); | ||
278 | |||
279 | end | ||
280 | |||
281 | %%%% reload data %%%%%%%%%%%%%% | ||
282 | B = zeros(size(A)); | ||
283 | |||
284 | figure(3); | ||
285 | |||
286 | for j=1:tnf, | ||
287 | cm = sprintf('load FJ%d;',j); | ||
288 | disp(cm);eval(cm); | ||
289 | |||
290 | subplot(5,6,j); | ||
291 | im(reshape(A(idx,:),nr_chank,nc_chank));axis('off');title(num2str(j)); | ||
292 | |||
293 | B = B + A; | ||
294 | end | ||
295 | |||
296 | figure(2);im(reshape(B(idx,:),nr_chank,nc_chank));axis('off');title('B'); | ||
297 | |||
298 | |||
299 | %%%%%% disp dist. %%%%%% | ||
300 | figure(12); | ||
301 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
302 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
303 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
304 | |||
305 | figure(2); | ||
306 | im(reshape(A(idx,:),nr_chank,nc_chank));axis('off');colorbar; | ||
307 | |||
308 | %%%%%% disp Joint Hist %%%%%%%%% | ||
309 | |||
310 | figure(12); | ||
311 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
312 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
313 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
314 | |||
315 | figure(1); | ||
316 | im(reshape(fhs(:,idx),10,10));axis('off');colorbar; | ||
317 | |||
318 | |||
319 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
320 | %%%%% F4: Joint filters %%%%%% | ||
321 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
322 | |||
323 | |||
324 | |||
325 | tnf = size(T1,3); | ||
326 | |||
327 | nw = 4;hw =3; | ||
328 | |||
329 | for scale=1:5, | ||
330 | for angle = 1:3, | ||
331 | cm = sprintf('!touch /disks/plaatje/scratch/jshi/FFJ_%d_%d_%d_%d.mat',angle,angle+3,scale,scale); | ||
332 | disp(cm);eval(cm); | ||
333 | cm = sprintf('!ln -s /disks/plaatje/scratch/jshi/FFJ_%d_%d_%d_%d.mat .',angle,angle+3,scale,scale); | ||
334 | disp(cm);eval(cm); | ||
335 | end | ||
336 | end | ||
337 | |||
338 | |||
339 | for scale = 1:5, | ||
340 | for angle = 1:3, | ||
341 | nf = 2; | ||
342 | hb.sigs = 0.02*ones(1,nf); hb.bmins= -1*ones(1,nf); | ||
343 | hb.bmaxs= 1*ones(1,nf); hb.nbins= 10*ones(1,nf); | ||
344 | |||
345 | fvs = colize(cat(3,T1(:,:,(scale-1)*6+angle),... | ||
346 | T1(:,:,(scale-1)*6+angle+3))); | ||
347 | |||
348 | fhs = colize_histnb_sf(fvs,I1,hb,nw,hw); | ||
349 | fhs = sqrt(fhs); | ||
350 | A = fhs'*fhs; | ||
351 | cm = sprintf('save FFJ_%d_%d_%d_%d A fhs',angle,angle+3,scale,scale); | ||
352 | disp(cm);eval(cm); | ||
353 | end | ||
354 | end | ||
355 | |||
356 | |||
357 | %%%%%%%%% load results %%%%%%%%%%% | ||
358 | %B = zeros(size(A)); | ||
359 | |||
360 | figure(3); | ||
361 | for scale=1:5, | ||
362 | for angle = 1:3, | ||
363 | cm = sprintf('load FFJ_%d_%d_%d_%d.mat',angle,angle+3,scale,scale); | ||
364 | disp(cm);eval(cm); | ||
365 | |||
366 | subplot(3,5,scale+(angle-1)*5); | ||
367 | im(reshape(A(idx,:),nr_chank,nc_chank)); | ||
368 | axis('off');title(sprintf('%d-%d,%d',angle,angle+3,scale)); | ||
369 | |||
370 | %B = B + A; | ||
371 | end | ||
372 | end | ||
373 | |||
374 | |||
375 | |||
376 | |||
377 | %%% disp results | ||
378 | |||
379 | angle = 1;scale = 1; | ||
380 | cm = sprintf('load FFJ_%d_%d_%d_%d.mat',angle,angle+3,scale,scale); | ||
381 | disp(cm);eval(cm); | ||
382 | |||
383 | |||
384 | |||
385 | figure(12); | ||
386 | ct = round(ginput(1));ct_chank(1) = round((ct(1)-hw-1)/st_sz) + 1; | ||
387 | ct_chank(2) = round((ct(2)-hw-1)/st_sz) + 1; | ||
388 | idx = (ct_chank(:,1)-1)*nr_chank + ct_chank(:,2); | ||
389 | |||
390 | %figure(1);im(reshape(fhs(:,idx),10,10));axis('off');%colorbar; | ||
391 | %figure(2);im(reshape(A(idx,:),nr_chank,nc_chank));%axis('off');%title('B'); | ||
392 | figure(4);im(reshape(B(idx,:),nr_chank,nc_chank));%axis('off');%title('B'); | ||
393 | |||
394 | |||
395 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
396 | |||
397 | %%%%%% reduction %%%%%%%%%%%%%%%%% | ||
398 | nv = 50; | ||
399 | [uA,dA] = eigs(A,nv);dA = diag(dA); | ||
400 | |||
401 | figure(4);suAplot(2,1,1);plot(dA,'p-'); | ||
402 | subplot(2,1,2);semilogy(dA,'p-'); | ||
403 | |||
404 | figure(3); | ||
405 | |||
406 | for j=1:20, | ||
407 | subplot(4,5,j); | ||
408 | im(reshape(uA(:,j),nr_chank,nc_chank));axis('off');colorbar;title(num2str(j)); | ||
409 | end | ||
410 | |||
411 | |||
412 | %%%%% Ncut without reduction %%%% | ||
413 | |||
414 | [uNu,dNu] = eig_decomp_v5(A,20); | ||
415 | |||
416 | figure(4);subplot(2,1,1);plot(dNu,'p-'); | ||
417 | subplot(2,1,2);semilogy(dNu,'p-'); | ||
418 | |||
419 | figure(3); | ||
420 | for j=2:6, | ||
421 | subplot(1,5,j-1); | ||
422 | im(reshape(-uNu(:,j),nr_chank,nc_chank));axis('off');colorbar;title(num2str(j)); | ||
423 | end | ||
424 | |||
425 | %%%%%% Ncut with reduction %%%%%%%%% | ||
426 | nvv = 7; | ||
427 | A1 = uA(:,1:nvv)*uA(:,1:nvv)'; | ||
428 | |||
429 | |||
430 | [uN,dN] = eig_decomp_v5(abs(A1),20); | ||
431 | |||
432 | figure(1);subplot(2,1,1);plot(dN,'p-'); | ||
433 | subplot(2,1,2);semilogy(dN,'p-'); | ||
434 | |||
435 | figure(3); | ||
436 | for j=2:6, | ||
437 | subplot(1,5,j-1); | ||
438 | im(reshape(uN(:,j),nr_chank,nc_chank));axis('off');colorbar;title(num2str(j)); | ||
439 | end | ||
440 | |||
441 | |||
442 | %%%%%% | ||
443 | |||
444 | |||
445 | |||
446 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_motion.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_motion.m new file mode 100755 index 0000000..91c97f9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_motion.m | |||
@@ -0,0 +1,117 @@ | |||
1 | |||
2 | im_sz = [40,40]; | ||
3 | |||
4 | ob_szh = [6,3]; | ||
5 | |||
6 | ob_c = [15,12]; | ||
7 | |||
8 | bg_color = 0.2; | ||
9 | |||
10 | ob_color = 0.8; | ||
11 | |||
12 | mag = 0.2; | ||
13 | |||
14 | I_bg = bg_color + mag*randn(im_sz); | ||
15 | |||
16 | I_obj = ob_color + mag*randn(2*ob_szh+1); | ||
17 | |||
18 | |||
19 | w = 3; | ||
20 | |||
21 | v5 = 1; | ||
22 | Js = []; | ||
23 | |||
24 | if ~v5, | ||
25 | for j=1:5, | ||
26 | fc = sprintf('J%d = I_bg;',j); | ||
27 | eval(fc); | ||
28 | |||
29 | fc = sprintf('J%d(ob_c(1)-ob_szh(1):ob_c(1)+ob_szh(1),ob_c(2)-ob_szh(2):ob_c(2)+ob_szh(2)) = I_obj;',j); | ||
30 | eval(fc); | ||
31 | |||
32 | ob_c = ob_c+[0,2]; | ||
33 | end | ||
34 | else | ||
35 | nf = 4; | ||
36 | for j = 1:nf, | ||
37 | |||
38 | J = I_bg; | ||
39 | J(ob_c(1)-ob_szh(1):ob_c(1)+ob_szh(1),ob_c(2)-ob_szh(2):ob_c(2)+ob_szh(2)) = I_obj; | ||
40 | |||
41 | if (j==1), | ||
42 | [gy,gx] = grad(J,w); | ||
43 | end | ||
44 | |||
45 | ob_c = ob_c+[0,2]; | ||
46 | |||
47 | Jw = cutoff(J,w); | ||
48 | Js(:,:,j) = Jw; | ||
49 | end | ||
50 | |||
51 | end | ||
52 | |||
53 | [nr,nc] = size(gx); | ||
54 | |||
55 | for j=1:nf, | ||
56 | subplot(1,nf,j); | ||
57 | imagesc(Js(:,:,j));axis('tightequal'); | ||
58 | end | ||
59 | |||
60 | |||
61 | writepnm5('test_motion.pnm',Js); | ||
62 | writepnm5('test_motion_gx.pnm',gx); | ||
63 | writepnm5('test_motion_gy.pnm',gy); | ||
64 | %imagesc(J1);colorbar; | ||
65 | |||
66 | |||
67 | inpara = [2,5,0.5,1,0.5]; | ||
68 | |||
69 | [A,D,Ipara] = cas('test_motion',inpara); | ||
70 | |||
71 | B= A+ A'; | ||
72 | clear A; | ||
73 | |||
74 | %BB = B(1:19^2,19^2+(1:19^2)); | ||
75 | %imagesc(BB); | ||
76 | |||
77 | [v,d] = eigs(B);d = diag(d); | ||
78 | |||
79 | k = 2; | ||
80 | |||
81 | figure(1); | ||
82 | %nf = 5; | ||
83 | |||
84 | nr = nr-5; | ||
85 | nc = nc-5; | ||
86 | |||
87 | n = nr* nc; | ||
88 | |||
89 | for j =1:nf, | ||
90 | subplot(1,nf,j); | ||
91 | imagesc(reshape(v((j-1)*n+(1:n),k).*D(1:n),nr,nc)');axis('tightequal'); | ||
92 | end | ||
93 | |||
94 | %%%%% | ||
95 | |||
96 | |||
97 | figure(3); | ||
98 | T = readpnm('test_motion.pnm'); | ||
99 | nf = size(T,3); | ||
100 | for j=1:nf, | ||
101 | subplot(1,nf,j); | ||
102 | imagesc(T(:,:,j));axis('tightequal'); | ||
103 | end | ||
104 | |||
105 | |||
106 | figure(2); | ||
107 | Gx = readpnm('test_motion_gx.pnm'); | ||
108 | |||
109 | [nr,nc] =size(Gx); | ||
110 | n = nr*nc; | ||
111 | |||
112 | imagesc(reshape(B(n+1:2*n,6*nc+7),nc,nr)');colorbar | ||
113 | |||
114 | |||
115 | |||
116 | |||
117 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_motion2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_motion2.m new file mode 100755 index 0000000..2959fa8 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_motion2.m | |||
@@ -0,0 +1,127 @@ | |||
1 | |||
2 | im_sz = [40,40]; | ||
3 | |||
4 | ob_szh = [4,3]; | ||
5 | |||
6 | ob_c = [12,12]; | ||
7 | |||
8 | ob_co = [30,28]; | ||
9 | |||
10 | bg_color = 0.2; | ||
11 | |||
12 | ob_color = 0.8; | ||
13 | |||
14 | mag = 0.2; | ||
15 | |||
16 | I_bg = bg_color + mag*randn(im_sz); | ||
17 | |||
18 | I_obj = ob_color + mag*randn(2*ob_szh+1); | ||
19 | |||
20 | |||
21 | w = 3; | ||
22 | |||
23 | v5 = 1; | ||
24 | Js = []; | ||
25 | |||
26 | if ~v5, | ||
27 | for j=1:5, | ||
28 | fc = sprintf('J%d = I_bg;',j); | ||
29 | eval(fc); | ||
30 | |||
31 | fc = sprintf('J%d(ob_c(1)-ob_szh(1):ob_c(1)+ob_szh(1),ob_c(2)-ob_szh(2):ob_c(2)+ob_szh(2)) = I_obj;',j); | ||
32 | eval(fc); | ||
33 | |||
34 | fc = sprintf('J%d(ob_co(1)-ob_szh(1):ob_co(1)+ob_szh(1),ob_co(2)-ob_szh(2):ob_co(2)+ob_szh(2)) = I_obj;',j); | ||
35 | eval(fc); | ||
36 | |||
37 | ob_c = ob_c+[0,2]; | ||
38 | ob_co = ob_co-[0,2]; | ||
39 | |||
40 | end | ||
41 | else | ||
42 | nf = 4; | ||
43 | for j = 1:nf, | ||
44 | |||
45 | J = I_bg; | ||
46 | J(ob_c(1)-ob_szh(1):ob_c(1)+ob_szh(1),ob_c(2)-ob_szh(2):ob_c(2)+ob_szh(2)) = I_obj; | ||
47 | J(ob_co(1)-ob_szh(1):ob_co(1)+ob_szh(1),ob_co(2)-ob_szh(2):ob_co(2)+ob_szh(2)) = I_obj; | ||
48 | |||
49 | if (j==1), | ||
50 | [gy,gx] = grad(J,w); | ||
51 | end | ||
52 | |||
53 | ob_c = ob_c+[0,2]; | ||
54 | ob_co = ob_co-[0,2]; | ||
55 | |||
56 | Jw = cutoff(J,w); | ||
57 | Js(:,:,j) = Jw; | ||
58 | end | ||
59 | |||
60 | end | ||
61 | |||
62 | [nr,nc] = size(gx); | ||
63 | |||
64 | for j=1:nf, | ||
65 | subplot(1,nf,j); | ||
66 | imagesc(Js(:,:,j));axis('tightequal'); | ||
67 | end | ||
68 | |||
69 | |||
70 | writepnm5('test_motion.pnm',Js); | ||
71 | writepnm5('test_motion_gx.pnm',gx); | ||
72 | writepnm5('test_motion_gy.pnm',gy); | ||
73 | %imagesc(J1);colorbar; | ||
74 | |||
75 | |||
76 | inpara = [2,5,0.5,1,0.5]; | ||
77 | |||
78 | [A,D,Ipara] = cas('test_motion',inpara); | ||
79 | |||
80 | B= A+ A'; | ||
81 | clear A; | ||
82 | |||
83 | %BB = B(1:19^2,19^2+(1:19^2)); | ||
84 | %imagesc(BB); | ||
85 | |||
86 | [v,d] = eigs(B);d = diag(d); | ||
87 | |||
88 | k = 2; | ||
89 | |||
90 | figure(1); | ||
91 | %nf = 5; | ||
92 | |||
93 | nr = nr-5; | ||
94 | nc = nc-5; | ||
95 | |||
96 | n = nr* nc; | ||
97 | |||
98 | for j =1:nf, | ||
99 | subplot(1,nf,j); | ||
100 | imagesc(reshape(v((j-1)*n+(1:n),k).*D(1:n),nr,nc)');axis('tightequal'); | ||
101 | end | ||
102 | |||
103 | %%%%% | ||
104 | |||
105 | |||
106 | figure(3); | ||
107 | T = readpnm('test_motion.pnm'); | ||
108 | nf = size(T,3); | ||
109 | for j=1:nf, | ||
110 | subplot(1,nf,j); | ||
111 | imagesc(T(:,:,j));axis('tightequal'); | ||
112 | end | ||
113 | |||
114 | |||
115 | figure(2); | ||
116 | Gx = readpnm('test_motion_gx.pnm'); | ||
117 | |||
118 | [nr,nc] =size(Gx); | ||
119 | n = nr*nc; | ||
120 | |||
121 | imagesc(reshape(B(n+1:2*n,6*nc+7),nc,nr)');colorbar | ||
122 | |||
123 | |||
124 | %%%%%%%%%%%%% | ||
125 | |||
126 | |||
127 | K = zeros(im_sz); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_period.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_period.m new file mode 100755 index 0000000..2994d15 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_period.m | |||
@@ -0,0 +1,58 @@ | |||
1 | flag = 2; | ||
2 | |||
3 | if flag ==1, | ||
4 | |||
5 | ws = [50,50]; | ||
6 | |||
7 | figure(1);J = get_win(I,ginput(1),ws); | ||
8 | figure(4);imagesc(J); | ||
9 | |||
10 | J = J - mean(mean(reshape(J,prod(size(J)),1))); | ||
11 | X = fftshift(fft2(J)); | ||
12 | |||
13 | figure(3);imagesc(abs(X));colorbar | ||
14 | figure(2);mesh(abs(X)); | ||
15 | |||
16 | else | ||
17 | |||
18 | fn = '1.pgm'; | ||
19 | |||
20 | % spatial gaussian parameter | ||
21 | xscale = 1; | ||
22 | |||
23 | % half size of the neighbourhood | ||
24 | xnb = 5; | ||
25 | |||
26 | % setting the the HSV gaussian parameter:[h s v] | ||
27 | Iscale = [0.01]; | ||
28 | |||
29 | Input_para = [xscale,xnb,Iscale]; | ||
30 | |||
31 | % compute the lower half the association matrix | ||
32 | [A,D,Ipara] = compute_A_pgm(fn,Input_para); | ||
33 | |||
34 | nr = Ipara(1);nc = Ipara(2); | ||
35 | |||
36 | B = A+A'; | ||
37 | clear A; | ||
38 | |||
39 | |||
40 | % eigen decompostion | ||
41 | options.tol = 1e-4; | ||
42 | num_eig_v = 10; | ||
43 | fprintf('doing eigs ...\n'); | ||
44 | [v,d] = eigs(B,num_eig_v,options); | ||
45 | |||
46 | k = 1;imagesc(reshape(v(:,k).*D,nc,nr)');colorbar | ||
47 | |||
48 | |||
49 | end | ||
50 | |||
51 | |||
52 | |||
53 | |||
54 | |||
55 | |||
56 | |||
57 | |||
58 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_text.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_text.m new file mode 100755 index 0000000..4cc5759 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/test_text.m | |||
@@ -0,0 +1,434 @@ | |||
1 | |||
2 | |||
3 | %case = 1; | ||
4 | |||
5 | read_flag = 1; | ||
6 | compute_flag = 0; | ||
7 | load_flag = 0; | ||
8 | decomp_flag = 0; | ||
9 | hist_flag = 0; | ||
10 | |||
11 | test_real = 0; | ||
12 | |||
13 | |||
14 | if read_flag, | ||
15 | if caseid == 1, | ||
16 | ifn = 'images/130049.pgm'; | ||
17 | elseif caseid == 2, | ||
18 | ifn = 'images/130055.pgm'; | ||
19 | elseif caseid == 3, | ||
20 | ifn = 'images/130056.pgm'; | ||
21 | elseif caseid == 4, | ||
22 | ifn = 'images/130057.pgm'; | ||
23 | elseif caseid == 5, | ||
24 | ifn = 'images/130060.pgm'; | ||
25 | elseif caseid == 6, | ||
26 | ifn = 'images/130061.pgm'; | ||
27 | elseif caseid == 7, | ||
28 | ifn = 'images/130062.pgm'; | ||
29 | elseif caseid == 8, | ||
30 | ifn = 'images/130065.pgm'; | ||
31 | elseif caseid == 9, | ||
32 | ifn = 'images/130066.pgm'; | ||
33 | elseif caseid == 10, | ||
34 | ifn = 'images/130068.pgm'; | ||
35 | elseif caseid == 11, | ||
36 | ifn = 'images/130070.pgm'; | ||
37 | else | ||
38 | ifn = 'images/130070.pgm'; | ||
39 | end | ||
40 | |||
41 | I = readpgm(ifn); | ||
42 | figure(1); | ||
43 | imagesc(I);colormap(gray);drawnow; | ||
44 | axis('tightequal'); | ||
45 | |||
46 | end | ||
47 | |||
48 | %%%%% load %%% | ||
49 | |||
50 | if load_flag, | ||
51 | fn = sprintf('load cresult_%d;',caseid); | ||
52 | eval(fn); | ||
53 | end | ||
54 | |||
55 | |||
56 | %%%%%%%%%%%%% compute %%%%%%%%%%% | ||
57 | sig = 0.5; | ||
58 | r = 3; | ||
59 | sz = 15; | ||
60 | Iw = cutoff(I,0.5*sz); | ||
61 | figure(1);imagesc(Iw); | ||
62 | axis('image'); | ||
63 | |||
64 | if compute_flag, | ||
65 | |||
66 | as = 0:30:150; | ||
67 | |||
68 | Cresult = []; | ||
69 | |||
70 | for j = 1:length(as), | ||
71 | fprintf('.'); | ||
72 | angle = as(j); | ||
73 | |||
74 | g = doog2( sig,r,angle,sz); | ||
75 | |||
76 | g = g - mean(reshape(g,prod(size(g)),1)); | ||
77 | |||
78 | g = g/sum(sum(abs(g))); | ||
79 | |||
80 | c = conv2(I,g,'valid'); | ||
81 | |||
82 | Cresult(:,:,j) = c; | ||
83 | end | ||
84 | |||
85 | |||
86 | fprintf('\n'); | ||
87 | |||
88 | |||
89 | figure(2); | ||
90 | |||
91 | subplot(2,3,1); | ||
92 | imagesc(Cresult(:,:,1).^2);axis('tightequal');colorbar | ||
93 | |||
94 | subplot(2,3,2); | ||
95 | imagesc(Cresult(:,:,2).^2);axis('tightequal');colorbar | ||
96 | |||
97 | subplot(2,3,3); | ||
98 | imagesc(Cresult(:,:,3).^2);axis('tightequal');colorbar | ||
99 | |||
100 | subplot(2,3,4); | ||
101 | imagesc(Cresult(:,:,4).^2);axis('tightequal');colorbar | ||
102 | |||
103 | subplot(2,3,5); | ||
104 | imagesc(Cresult(:,:,5).^2);axis('tightequal');colorbar | ||
105 | |||
106 | subplot(2,3,6); | ||
107 | imagesc(Cresult(:,:,6).^2);axis('tightequal');colorbar | ||
108 | |||
109 | Cs = []; | ||
110 | Mcs = []; | ||
111 | for j=1:length(as), | ||
112 | Cs(:,:,j) = reduce(reduce(abs(Cresult(:,:,j)))); | ||
113 | |||
114 | Mcs = [Mcs,max(max(Cs(:,:,j)))]; | ||
115 | |||
116 | end | ||
117 | |||
118 | ms = max(Mcs); | ||
119 | |||
120 | figure(3); | ||
121 | for j=1:6, | ||
122 | fn = sprintf('Cs(:,:,%d) = Cs(:,:,%d)/ms;',j,j); | ||
123 | eval(fn); | ||
124 | fn = sprintf('subplot(2,3,%d);imagesc(Cs(:,:,%d));',j,j); | ||
125 | eval(fn);axis('tightequal');colorbar | ||
126 | end | ||
127 | |||
128 | fn = sprintf('save cresult_%d.mat Cresult Cs',caseid); | ||
129 | disp(fn); | ||
130 | eval(fn); | ||
131 | |||
132 | end | ||
133 | |||
134 | %%%%%%%%%%%%%%%%% decomp %%%%%%%%%%%% | ||
135 | |||
136 | |||
137 | if decomp_flag, | ||
138 | |||
139 | %writepnm5('txt_2.pnm',Cs); | ||
140 | |||
141 | %writepnm5('130068.pnm',Cs); | ||
142 | |||
143 | |||
144 | %I_scale = 0.0025; | ||
145 | %X_scale = 3^2; | ||
146 | %[A,D,Ipara] = compute_A_sparmul2(10,I_scale,X_scale); | ||
147 | |||
148 | |||
149 | I_scale = 0.02; | ||
150 | X_scale = 2; | ||
151 | [A,D,Ipara] = compute_A_pnm('130068.pnm',[X_scale,I_scale]); | ||
152 | |||
153 | nr = Ipara(1);nc = Ipara(2); | ||
154 | imagesc(reshape(D,nc,nr)');colorbar; | ||
155 | |||
156 | B = A+A';clear A; | ||
157 | |||
158 | options.tol = 1e-7; | ||
159 | |||
160 | [v,d] = eigs(B,9,options); | ||
161 | |||
162 | figure(4); | ||
163 | k = 1; imagesc(reshape(v(:,k).*D,nc,nr)'); | ||
164 | |||
165 | end | ||
166 | |||
167 | |||
168 | %%%% histogram %%%% | ||
169 | |||
170 | %hist_flag = 1; | ||
171 | |||
172 | %figure(1);imagesc(Iw);axis('image'); | ||
173 | if hist_flag ==1, | ||
174 | |||
175 | |||
176 | ws = [12,12]; | ||
177 | |||
178 | figure(7); | ||
179 | |||
180 | cs = ginput(1); | ||
181 | |||
182 | cs = 10*(cs-1)+w/2; | ||
183 | |||
184 | %cs(1,:) = w+(floor((cs(1,:)-w)/gap)*gap); | ||
185 | %cs(2,:) = w+(floor((cs(2,:)-w)/gap)*gap); | ||
186 | |||
187 | J = get_win(Iw,cs(1,:),ws); | ||
188 | Jbar = get_win5(Cresult,cs(1,:),ws); | ||
189 | |||
190 | |||
191 | figure(2); | ||
192 | subplot(3,3,1);imagesc(J);colorbar | ||
193 | for j=1:6,subplot(3,3,1+j);imagesc(abs(Jbar(:,:,j)));colorbar; end | ||
194 | |||
195 | [hists,bins] = get_hist(J,Jbar);show_hist(hists,bins,4); | ||
196 | cumhists = get_cumhist(hists);show_cumhist(cumhists,bins,6,1,'b-o'); | ||
197 | |||
198 | J2 = get_win(Iw,cs(2,:),ws); | ||
199 | Jbar2 = get_win5(Cresult,cs(2,:),ws); | ||
200 | |||
201 | figure(3); | ||
202 | subplot(3,3,1);imagesc(J2);colorbar | ||
203 | for j=1:6,subplot(3,3,1+j);imagesc(abs(Jbar2(:,:,j)));colorbar; end | ||
204 | |||
205 | [hists2,bins2] = get_hist(J2,Jbar2);show_hist(hists2,bins2,5); | ||
206 | cumhists2 = get_cumhist(hists2);show_cumhist(cumhists2,bins2,6,0,'r-*'); | ||
207 | |||
208 | diff.inten = max(abs(cumhists.inten-cumhists2.inten)); | ||
209 | diff.mag = max(abs(cumhists.mag-cumhists2.mag)); | ||
210 | diff.text = max(max(abs(cumhists.text-cumhists2.text))); | ||
211 | |||
212 | figure(7); | ||
213 | |||
214 | disp([diff.inten,diff.mag,diff.text]); | ||
215 | maxdiff = max([diff.inten,diff.mag,diff.text]); | ||
216 | disp(1-sigmoid(diff.inten,0.22,0.02)); | ||
217 | |||
218 | |||
219 | if 0, | ||
220 | %A = pair_dist_text(Iw,Cresult,15); | ||
221 | |||
222 | r =4;w = 22;gap = 5;sig_x= 20.0; | ||
223 | inpara = [r,w,gap,sig_x,0.16,0.2,0.2]; | ||
224 | [Cum,tm] = cAh(Iw,mag,abs(Cresult),inpara); | ||
225 | |||
226 | [Cum,Nb,Nc] = cAh4(Iw,mag,abs(Cresult),inpara); | ||
227 | |||
228 | |||
229 | B = A+ A';clear A; | ||
230 | |||
231 | figure(1); | ||
232 | c = ginput(1); | ||
233 | cx = floor(c(1)/gap); | ||
234 | cy = floor(c(2)/gap); | ||
235 | [cx,cy] | ||
236 | figure(7) | ||
237 | imagesc(reshape(B(cy*Cum(1)+cx,:),Cum(1),Cum(2))');colorbar | ||
238 | |||
239 | |||
240 | cutoff = [0.22,0.2,0.2]; | ||
241 | sig_hist = [0.02,0.04,0.05]; | ||
242 | |||
243 | inpara2 = [r,5,cutoff,sig_hist]; | ||
244 | [A,D] = compute_A_hist3(tm,Cum,inpara2); | ||
245 | |||
246 | B = A+A';clear A; | ||
247 | imagesc(reshape(D,Cum(1),Cum(2))'); | ||
248 | |||
249 | |||
250 | [v,d] = eigs(B); | ||
251 | |||
252 | |||
253 | end | ||
254 | |||
255 | end | ||
256 | |||
257 | %%%%%%%%%%%%% trans_texture %%%%%%%%%%%% | ||
258 | trans_text = 0; | ||
259 | |||
260 | |||
261 | if trans_text, | ||
262 | figure(1); | ||
263 | cs = ginput(1); | ||
264 | |||
265 | ws = [40,40]; | ||
266 | |||
267 | J = get_win(Iw,cs(1,:),ws); | ||
268 | Jbar = get_win5(Cresult,cs(1,:),ws); | ||
269 | Jmag = get_win(mag,cs(1,:),ws); | ||
270 | |||
271 | figure(3);imagesc(J); | ||
272 | |||
273 | figure(3); | ||
274 | for j=1:6, | ||
275 | subplot(2,3,j);imagesc(abs(Jbar(:,:,j)));axis('image');colorbar; | ||
276 | end | ||
277 | |||
278 | f= abs(Jbar(40,38,:)); | ||
279 | g= abs(Jbar(40,47,:)); | ||
280 | |||
281 | dot(f,g)/max(dot(f,f),dot(g,g)) | ||
282 | |||
283 | ff = myinterp(f,10); gg = myinterp(g,10); | ||
284 | dot(ff,gg)/max(dot(ff,ff),dot(gg,gg)) | ||
285 | |||
286 | cum = mc_corr(ff,gg,[-6,6]); | ||
287 | max(cum)/max(dot(f,f),dot(g,g)) | ||
288 | |||
289 | |||
290 | center = [40,35]; | ||
291 | |||
292 | f = squeeze(abs(Jbar(center(1),center(2),:))); | ||
293 | ff = myinterp(f,10); | ||
294 | mag_ff = dot(ff,ff); | ||
295 | mag_c = Jmag(center(1),center(2)); | ||
296 | dy = 0; | ||
297 | |||
298 | cor_cofs = []; | ||
299 | mags = []; | ||
300 | for dx = -15:15, | ||
301 | g = squeeze(abs(Jbar(center(1)+dy,center(2)+dx,:))); | ||
302 | gg = myinterp(g,10); | ||
303 | |||
304 | cum = mc_corr(ff,gg,[-6,6]); | ||
305 | cor_cofs = [cor_cofs,max(cum)/max(mag_ff,dot(gg,gg))]; | ||
306 | |||
307 | mags =[mags,max(mag_c,Jmag(center(1)+dy,center(2)+dx,:))]; | ||
308 | |||
309 | end | ||
310 | |||
311 | simulation_on =0; | ||
312 | |||
313 | if simulation_on, | ||
314 | |||
315 | sz = [161,161] | ||
316 | SI = zeros(sz); | ||
317 | |||
318 | for i=2:18:sz(1), | ||
319 | SI(i:i+2,:) = 1+SI(i:i+2,:); | ||
320 | end | ||
321 | |||
322 | imagesc(SI);axis('image'); | ||
323 | |||
324 | tmp1 = mimrotate(SI,90,'nearest','crop'); | ||
325 | tmp2 = mimrotate(SI,45,'nearest','crop'); | ||
326 | |||
327 | ly = round(0.7*sz(1)); | ||
328 | lx = round(0.7*sz(1)); | ||
329 | sy = round(0.16*sz(1)); | ||
330 | sx = round(0.2*sz(2)); | ||
331 | TI = [tmp1(1:ly,1:lx),tmp2(sy+1:sy+ly,sy+1:sy+round(0.4*lx))]; | ||
332 | |||
333 | TI = TI+0.04*randn(size(TI)); | ||
334 | |||
335 | %sig = 1/sqrt(2);r = 3;sz = round(r*3*sig); | ||
336 | |||
337 | sigs = [1/sqrt(2),1,sqrt(2),2,2*sqrt(2)];r = 3;szs = round(r*3*sigs); | ||
338 | [text_des,TIw] = compute_filter(TI,sigs,r,szs); | ||
339 | figure(2);imagesc(TIw);axis('image'); | ||
340 | |||
341 | figure(3); | ||
342 | im5(abs(text_des),2,3); | ||
343 | |||
344 | text_des = abs(text_des); | ||
345 | |||
346 | text_des = T1; | ||
347 | |||
348 | numband = size(text_des,3); r = 10; | ||
349 | sig_x = 90; sig_inten = 0.15; sig_tex = 0.01;w_inten = 0.03; | ||
350 | para = [numband,r,sig_x,sig_inten,sig_tex,w_inten]; | ||
351 | |||
352 | [A,D,Ipara] = compute_A_text(TIw,text_des,para); | ||
353 | nr = Ipara(1);nc = Ipara(2); | ||
354 | B = A+A'; clear A; | ||
355 | |||
356 | figure(2); | ||
357 | cs = ginput(1); | ||
358 | cs = round(cs);id = cs(2)*nc+cs(1); | ||
359 | |||
360 | figure(4); | ||
361 | imagesc(reshape(B(id,:),nc,nr)');axis('image');colorbar | ||
362 | |||
363 | [v,d] = eigs(B); | ||
364 | figure(4);imagesc(reshape(D.*v(:,1),nc,nr)');axis('image'); | ||
365 | |||
366 | end | ||
367 | |||
368 | end | ||
369 | |||
370 | if test_real == 1, | ||
371 | sigs = [1/sqrt(2),1,sqrt(2),2,2*sqrt(2)];r = 3;szs = round(r*3*sigs); | ||
372 | text_des = compute_filter(I,sigs,r,szs); | ||
373 | |||
374 | text_des = abs(text_des); | ||
375 | %save filter_3.mat | ||
376 | |||
377 | %%%% cutoff margins, | ||
378 | margin = 6+10; | ||
379 | |||
380 | Iw = cutoff(I,margin); | ||
381 | |||
382 | T1= reshape(text_des,size(text_des,1),size(text_des,2),size(text_des,3)*size(text_des,4)); | ||
383 | T1 = cutoff(T1,margin); | ||
384 | |||
385 | %%%%% reduce resolution | ||
386 | |||
387 | Iwp = compact(Iw,4); | ||
388 | |||
389 | T1 = reduce_all(T1); | ||
390 | T1 = reduce_all(T1); | ||
391 | |||
392 | % T1 = T1/70; | ||
393 | |||
394 | % writepnm5('test6_image.pnm',Iwp);writepnm5('test6_filter.pnm',T1); | ||
395 | |||
396 | numband = size(T1,3); r = 2; | ||
397 | sig_x = 20; sig_inten = 0.15; sig_tex = 0.01;w_inten = 0.01; | ||
398 | para = [numband,r,sig_x,sig_inten,sig_tex,w_inten]; | ||
399 | |||
400 | [A,D,Ipara] = compute_A_text(Iwp,T1,para); | ||
401 | nr = Ipara(1);nc = Ipara(2); | ||
402 | figure(4);imagesc(reshape(D,nc,nr)');axis('image'); | ||
403 | drawnow; | ||
404 | |||
405 | |||
406 | numband = 6; | ||
407 | r = 5; sig_x = 20.0; | ||
408 | sig_tex = 0.01; w_inten = 0.01; w = 2; | ||
409 | para = [numband,r,sig_x,sig_tex,w_inten,w,size(T1,2)]; | ||
410 | |||
411 | [A,D,Ipara] = compute_A_text2(Iw,T1(:,:,1:numband)/70,para); | ||
412 | nr = Ipara(1);nc = Ipara(2); | ||
413 | |||
414 | |||
415 | |||
416 | B = A+A'; clear A; | ||
417 | |||
418 | |||
419 | figure(2); | ||
420 | cs = ginput(1); | ||
421 | cs = floor(cs/4)+1;id = cs(2)*nc+cs(1); | ||
422 | |||
423 | figure(4); | ||
424 | imagesc(reshape(B(id,:),nc,nr)');axis('image');colorbar | ||
425 | |||
426 | |||
427 | end | ||
428 | |||
429 | |||
430 | |||
431 | |||
432 | |||
433 | |||
434 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp.m new file mode 100755 index 0000000..b932912 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp.m | |||
@@ -0,0 +1,68 @@ | |||
1 | |||
2 | sw = 3; | ||
3 | gap = 2*sw+1; | ||
4 | |||
5 | nw = 6; | ||
6 | |||
7 | for j=1:20, | ||
8 | l = max(0,j-1-nw); | ||
9 | % l = max(0,j-1-2*nw); | ||
10 | rs(j) = ceil((l-sw)/gap) + 1; | ||
11 | l = min(20,j-1+nw); | ||
12 | % l = min(20,j-1); | ||
13 | re(j) = floor((l-sw)/gap) +1; | ||
14 | end | ||
15 | |||
16 | plot([1:20],rs,'p-',[1:20],re,'rp-') | ||
17 | |||
18 | |||
19 | %%%%%%%% | ||
20 | |||
21 | bin_max = 1.0; | ||
22 | bin_min = -1.0; | ||
23 | num_bin = 30; | ||
24 | sig = 0.2; | ||
25 | |||
26 | data = 0.482; | ||
27 | |||
28 | inc = (bin_max-bin_min)/num_bin; | ||
29 | |||
30 | bs = -100; | ||
31 | be = bin_min+inc; | ||
32 | b = []; | ||
33 | |||
34 | for j=1:num_bin, | ||
35 | |||
36 | b(j) = tmp1(bs,be,data,sig); | ||
37 | bs = be; | ||
38 | be= be+inc; | ||
39 | end | ||
40 | plot(b,'p-'); | ||
41 | |||
42 | |||
43 | |||
44 | bmin = -1; | ||
45 | |||
46 | inc = 0.2; | ||
47 | a = 0.1; | ||
48 | b = -1250; | ||
49 | ovs = 625; | ||
50 | |||
51 | bs = bmin; | ||
52 | be = bs+inc; | ||
53 | |||
54 | data = -0.482; | ||
55 | |||
56 | for j=1:10, | ||
57 | tmp = bs-data; | ||
58 | fs = exp(-(tmp*tmp*ovs)); | ||
59 | ks = b*tmp; | ||
60 | |||
61 | tmp = be-data; | ||
62 | fe = exp(-(tmp*tmp*ovs)); | ||
63 | ke = b*tmp; | ||
64 | |||
65 | bin(j) = fs*(2+a*ks) + fe*(2-a*ke); | ||
66 | bs = be; | ||
67 | be = be+inc; | ||
68 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp1.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp1.m new file mode 100755 index 0000000..db5dbc1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp1.m | |||
@@ -0,0 +1,25 @@ | |||
1 | function d = tmp1(bs,be,data,sig) | ||
2 | |||
3 | sig = sig^2; | ||
4 | |||
5 | if 1, | ||
6 | a = (bs+be)*0.5; | ||
7 | d = (a-bs)*(exp(-(bs-data)^2/sig) + exp(-(a-data)^2/sig)) + ... | ||
8 | (be-a)*(exp(-(a-data)^2/sig) + exp(-(be-data)^2/sig)); | ||
9 | d = d*2/sqrt(pi); | ||
10 | else | ||
11 | |||
12 | a = (be-bs)/2; | ||
13 | |||
14 | h1 = exp(-(bs-data)^2/sig); | ||
15 | h2 = exp(-(be-data)^2/sig); | ||
16 | |||
17 | k1 = -2*(bs-data)/sig; | ||
18 | k2 = -2*(be-data)/sig; | ||
19 | |||
20 | d = a*(h1*(2+2*a*k1) + h2*(2-2*a*k2)); | ||
21 | d = d*2/sqrt(pi); | ||
22 | |||
23 | end | ||
24 | |||
25 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp2.m new file mode 100755 index 0000000..b361cdc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp2.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function d = tmp2(bs,be,data,sig) | ||
2 | |||
3 | sig = sig^2; | ||
4 | |||
5 | |||
6 | a = (be-bs)/2; | ||
7 | |||
8 | h1 = exp(-(bs-data)^2/sig); | ||
9 | h2 = exp(-(be-data)^2/sig); | ||
10 | |||
11 | k1 = -2*(bs-data)/sig; | ||
12 | k2 = -2*(be-data)/sig; | ||
13 | |||
14 | d = (h1*(2+2*a*k1) + h2*(2-2*a*k2)); | ||
15 | %d = a*d*2/sqrt(pi); | ||
16 | |||
17 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp3.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp3.m new file mode 100755 index 0000000..c1bffd9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/tmp3.m | |||
@@ -0,0 +1,126 @@ | |||
1 | function result = erfcore(x,jint) | ||
2 | %ERFCORE Core algorithm for error functions. | ||
3 | % erf(x) = erfcore(x,0) | ||
4 | % erfc(x) = erfcore(x,1) | ||
5 | % erfcx(x) = exp(x^2)*erfc(x) = erfcore(x,2) | ||
6 | |||
7 | % C. Moler, 2-1-91. | ||
8 | % Copyright (c) 1984-96 by The MathWorks, Inc. | ||
9 | % $Revision: 5.7 $ $Date: 1996/10/28 20:57:59 $ | ||
10 | |||
11 | % This is a translation of a FORTRAN program by W. J. Cody, | ||
12 | % Argonne National Laboratory, NETLIB/SPECFUN, March 19, 1990. | ||
13 | % The main computation evaluates near-minimax approximations | ||
14 | % from "Rational Chebyshev approximations for the error function" | ||
15 | % by W. J. Cody, Math. Comp., 1969, PP. 631-638. | ||
16 | |||
17 | if ~isreal(x), | ||
18 | error('Input argument must be real.') | ||
19 | end | ||
20 | result = repmat(NaN,size(x)); | ||
21 | % | ||
22 | % evaluate erf for |x| <= 0.46875 | ||
23 | % | ||
24 | xbreak = 0.46875; | ||
25 | k = find(abs(x) <= xbreak); | ||
26 | if ~isempty(k) | ||
27 | a = [3.16112374387056560e00; 1.13864154151050156e02; | ||
28 | 3.77485237685302021e02; 3.20937758913846947e03; | ||
29 | 1.85777706184603153e-1]; | ||
30 | b = [2.36012909523441209e01; 2.44024637934444173e02; | ||
31 | 1.28261652607737228e03; 2.84423683343917062e03]; | ||
32 | |||
33 | y = abs(x(k)); | ||
34 | z = y .* y; | ||
35 | xnum = a(5)*z; | ||
36 | xden = z; | ||
37 | for i = 1:3 | ||
38 | xnum = (xnum + a(i)) .* z; | ||
39 | xden = (xden + b(i)) .* z; | ||
40 | end | ||
41 | result(k) = x(k) .* (xnum + a(4)) ./ (xden + b(4)); | ||
42 | if jint ~= 0, result(k) = 1 - result(k); end | ||
43 | if jint == 2, result(k) = exp(z) .* result(k); end | ||
44 | end | ||
45 | % | ||
46 | % evaluate erfc for 0.46875 <= |x| <= 4.0 | ||
47 | % | ||
48 | k = find((abs(x) > xbreak) & (abs(x) <= 2.)); | ||
49 | if ~isempty(k) | ||
50 | c = [5.64188496988670089e-1; 8.88314979438837594e00; | ||
51 | 6.61191906371416295e01; 2.98635138197400131e02; | ||
52 | 8.81952221241769090e02; 1.71204761263407058e03; | ||
53 | 2.05107837782607147e03; 1.23033935479799725e03; | ||
54 | 2.15311535474403846e-8]; | ||
55 | d = [1.57449261107098347e01; 1.17693950891312499e02; | ||
56 | 5.37181101862009858e02; 1.62138957456669019e03; | ||
57 | 3.29079923573345963e03; 4.36261909014324716e03; | ||
58 | 3.43936767414372164e03; 1.23033935480374942e03]; | ||
59 | |||
60 | y = abs(x(k)); | ||
61 | xnum = c(9)*y; | ||
62 | xden = y; | ||
63 | for i = 1:7 | ||
64 | xnum = (xnum + c(i)) .* y; | ||
65 | xden = (xden + d(i)) .* y; | ||
66 | end | ||
67 | result(k) = (xnum + c(8)) ./ (xden + d(8)); | ||
68 | if jint ~= 2 | ||
69 | z = fix(y*16)/16; | ||
70 | del = (y-z).*(y+z); | ||
71 | result(k) = exp(-z.*z) .* exp(-del) .* result(k); | ||
72 | end | ||
73 | end | ||
74 | % | ||
75 | % evaluate erfc for |x| > 4.0 | ||
76 | % | ||
77 | k = find(abs(x) > 2.0); | ||
78 | if ~isempty(k) | ||
79 | if 0, | ||
80 | p = [3.05326634961232344e-1; 3.60344899949804439e-1; | ||
81 | 1.25781726111229246e-1; 1.60837851487422766e-2; | ||
82 | 6.58749161529837803e-4; 1.63153871373020978e-2]; | ||
83 | q = [2.56852019228982242e00; 1.87295284992346047e00; | ||
84 | 5.27905102951428412e-1; 6.05183413124413191e-2; | ||
85 | 2.33520497626869185e-3]; | ||
86 | |||
87 | y = abs(x(k)); | ||
88 | z = 1 ./ (y .* y); | ||
89 | xnum = p(6).*z; | ||
90 | xden = z; | ||
91 | for i = 1:4 | ||
92 | xnum = (xnum + p(i)) .* z; | ||
93 | xden = (xden + q(i)) .* z; | ||
94 | end | ||
95 | result(k) = z .* (xnum + p(5)) ./ (xden + q(5)); | ||
96 | result(k) = (1/sqrt(pi) - result(k)) ./ y; | ||
97 | if jint ~= 2 | ||
98 | z = fix(y*16)/16; | ||
99 | del = (y-z).*(y+z); | ||
100 | result(k) = exp(-z.*z) .* exp(-del) .* result(k); | ||
101 | k = find(~isfinite(result)); | ||
102 | result(k) = 0*k; | ||
103 | end | ||
104 | end | ||
105 | result(k) = 0; | ||
106 | end | ||
107 | % | ||
108 | % fix up for negative argument, erf, etc. | ||
109 | % | ||
110 | if jint == 0 | ||
111 | k = find(x > xbreak); | ||
112 | result(k) = (0.5 - result(k)) + 0.5; | ||
113 | k = find(x < -xbreak); | ||
114 | result(k) = (-0.5 + result(k)) - 0.5; | ||
115 | elseif jint == 1 | ||
116 | k = find(x < -xbreak); | ||
117 | result(k) = 2. - result(k); | ||
118 | else % jint must = 2 | ||
119 | k = find(x < -xbreak); | ||
120 | z = fix(x(k)*16)/16; | ||
121 | del = (x(k)-z).*(x(k)+z); | ||
122 | y = exp(z.*z) .* exp(del); | ||
123 | result(k) = (y+y) - result(k); | ||
124 | end | ||
125 | |||
126 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/true_loc.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/true_loc.m new file mode 100755 index 0000000..7bf060f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/true_loc.m | |||
@@ -0,0 +1,23 @@ | |||
1 | function a = true_loc(loca,g,scale); | ||
2 | |||
3 | if ~exist('scale'), | ||
4 | scale = 50; | ||
5 | end | ||
6 | |||
7 | y = loca(1,:); | ||
8 | x = loca(2,:); | ||
9 | |||
10 | min_x = min(x); | ||
11 | min_y = min(y); | ||
12 | |||
13 | x = x - min_x; | ||
14 | y = y - min_y; | ||
15 | |||
16 | max_x = max(x);max_y = max(y); | ||
17 | min_scale = min(max_x,max_y); | ||
18 | |||
19 | a(1) = (g(1)-1)*min_scale/(scale); | ||
20 | a(2) = (g(2)-1)*min_scale/(scale); | ||
21 | |||
22 | a(1) = a(1) + min_x; | ||
23 | a(2) = a(2) + min_y; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/vmquant.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/vmquant.m new file mode 100755 index 0000000..ab4eb28 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/vmquant.m | |||
@@ -0,0 +1,112 @@ | |||
1 | function [im, map] = vmquant(arg1,arg2,arg3,arg4,arg5,arg6,arg7) | ||
2 | %VMQUANT Variance Minimization Color Quantization. | ||
3 | % [X, MAP] = VMQUANT(R,G,B,K,[Qr Qg Qb],DITHER,Qe) or | ||
4 | % VMQUANT(RGB,K,[Qr Qg Qb],DITHER,Qe), where RGB is a 3-D array, | ||
5 | % converts an arbitrary image comprised of RGB triples into an | ||
6 | % indexed image X with color map MAP. K specifies the number | ||
7 | % of desired entries in the target color map, and [Qr Qg Qb] | ||
8 | % specifies the number of quantization bits to assign each color | ||
9 | % axis during color interpolation. DITHER is a string ('dither' or | ||
10 | % 'nodither') that indicates whether or not to perform error propagation | ||
11 | % dither on the output image. Qe specifies the number of bits of | ||
12 | % quantization used in the error calculations. | ||
13 | % | ||
14 | % K is optional and defaults to 256. | ||
15 | % [Qr Qg Qb] is optional and defaults to [5 5 5]. | ||
16 | % DITHER is optional and defaults to 'nodither'. | ||
17 | % Qe is optional and defaults to 8. | ||
18 | % | ||
19 | % See also: RGB2IND, RGB2GRAY, DITHER, IND2RGB, CMUNIQUE, IMAPPROX. | ||
20 | |||
21 | % This is the wrapper function for the MEX file VMQUANTC.C | ||
22 | |||
23 | % Joseph M. Winograd 6-93 | ||
24 | % Copyright (c) 1993 by The MathWorks, Inc. | ||
25 | % $Revision: 5.3 $ $Date: 1996/08/22 22:09:03 $ | ||
26 | |||
27 | % Reference: Xiaolin Wu, "Efficient Statistical Computation for | ||
28 | % Optimal Color Quantization," Graphics Gems II, (ed. James | ||
29 | % Arvo). Academic Press: Boston. 1991. | ||
30 | |||
31 | if nargin < 1, | ||
32 | error('Not enough input arguments.'); | ||
33 | end | ||
34 | |||
35 | threeD = (ndims(arg1)==3); % Determine if input includes a 3-D array | ||
36 | |||
37 | if threeD, | ||
38 | error( nargchk( 1, 5, nargin ) ); | ||
39 | |||
40 | % NOTE: If you change defaults, change them also | ||
41 | % in VMQUANTC.C and recompile the MEX function. | ||
42 | if nargin < 5 | ||
43 | arg5 = 8; % DEFAULT_QE = 8 | ||
44 | end | ||
45 | |||
46 | if nargin < 4 | ||
47 | arg4 = 'n'; % DEFAULT_DITHER = 0 | ||
48 | end | ||
49 | |||
50 | if nargin < 3 | ||
51 | arg3 = [5 5 5]; % DEFAULT_Q = [5 5 5] | ||
52 | end | ||
53 | |||
54 | if nargin < 2 | ||
55 | arg2 = 256; % DEFAULT_K = 256 | ||
56 | end | ||
57 | |||
58 | rout = arg1(:,:,1); | ||
59 | g = arg1(:,:,2); | ||
60 | b = arg1(:,:,3); | ||
61 | |||
62 | if strcmp(lower(arg4(1)),'d') | ||
63 | dith = 1; | ||
64 | else | ||
65 | dith = 0; | ||
66 | end | ||
67 | |||
68 | arg7 = arg5; | ||
69 | arg5 = arg3; | ||
70 | arg4 = arg2; | ||
71 | |||
72 | else | ||
73 | error( nargchk( 3, 7, nargin ) ); | ||
74 | |||
75 | if nargin < 7 | ||
76 | arg7 = 8; % DEFAULT_QE = 8 | ||
77 | end | ||
78 | |||
79 | if nargin < 6 | ||
80 | arg6 = 'n'; % DEFAULT_DITHER = 0 | ||
81 | end | ||
82 | |||
83 | if nargin < 5 | ||
84 | arg5 = [5 5 5]; % DEFAULT_Q = [5 5 5] | ||
85 | end | ||
86 | |||
87 | if nargin < 4 | ||
88 | arg4 = 256; % DEFAULT_K = 256 | ||
89 | end | ||
90 | |||
91 | rout = arg1; | ||
92 | g = arg2; | ||
93 | b = arg3; | ||
94 | |||
95 | if strcmp(lower(arg6(1)),'d') | ||
96 | dith = 1; | ||
97 | else | ||
98 | dith = 0; | ||
99 | end | ||
100 | |||
101 | end | ||
102 | |||
103 | if (~isa(rout,'uint8')) | ||
104 | rout = uint8(round(255*rout)); | ||
105 | end | ||
106 | if (~isa(g,'uint8')) | ||
107 | g = uint8(round(255*g)); | ||
108 | end | ||
109 | if (~isa(b,'uint8')) | ||
110 | b = uint8(round(255*b)); | ||
111 | end | ||
112 | [im,map] = vmquantc( rout, g, b, arg4, arg5, dith, arg7 ); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/wismm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/wismm.m new file mode 100755 index 0000000..915e07d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/wismm.m | |||
@@ -0,0 +1,26 @@ | |||
1 | function [L1,L2,phi,Txx,Txy,Tyy]=wismm(X,N); | ||
2 | % [L1,L2,phi,T11,T12,T22]=wismm(X,N); | ||
3 | % Calculate windowed image second moment matrices for image X and return | ||
4 | % the following values: | ||
5 | % | ||
6 | % L1 is the larger eigenvalue (lambda_1) | ||
7 | % L2 is the smaller eigenvalue (lambda_2) | ||
8 | % phi is the angle of the 1st eigenvector (phi) | ||
9 | |||
10 | [G1,G2]=gradient(X); | ||
11 | |||
12 | GGTxx=G1.^2; | ||
13 | GGTxy=G1.*G2; | ||
14 | GGTyy=G2.^2; | ||
15 | |||
16 | Txx=gaussN(GGTxx,N); | ||
17 | Txy=gaussN(GGTxy,N); | ||
18 | Tyy=gaussN(GGTyy,N); | ||
19 | |||
20 | tr=Txx+Tyy; | ||
21 | V1=0.5*sqrt(tr.^2-4*(Txx.*Tyy-Txy.^2)); | ||
22 | |||
23 | L1=real(0.5*tr+V1); | ||
24 | L2=real(0.5*tr-V1); | ||
25 | phi=0.5*atan2(2*Txy,Txx-Tyy); | ||
26 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/wismm2.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/wismm2.m new file mode 100755 index 0000000..ae62ce9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/wismm2.m | |||
@@ -0,0 +1,66 @@ | |||
1 | function [L1,L2,phi,aniso,pol,con,window_sizes]=wismm2(V); | ||
2 | % [L1,L2,phi,aniso,pol,con,window_sizes]=wismm2(V); | ||
3 | % Calculate windowed image second moment matrices for image V and return | ||
4 | % the following values: | ||
5 | % | ||
6 | % L1 is the larger eigenvalue (lambda_1) | ||
7 | % L2 is the smaller eigenvalue (lambda_2) | ||
8 | % phi is the angle of the 1st eigenvector (phi) | ||
9 | % | ||
10 | |||
11 | [Gx,Gy]=gradient(V); | ||
12 | |||
13 | GGTxx=Gx.^2; | ||
14 | GGTxy=Gx.*Gy; | ||
15 | GGTyy=Gy.^2; | ||
16 | |||
17 | [r,c]=size(V); | ||
18 | |||
19 | min_window_size=3; | ||
20 | max_window_size=3*round(min(r,c)/16); | ||
21 | if (-1)^max_window_size==1 | ||
22 | max_window_size=max_window_size+1; | ||
23 | end | ||
24 | window_step_size=2; | ||
25 | |||
26 | window_sizes=min_window_size:2:max_window_size; | ||
27 | max_count=length(window_sizes); | ||
28 | |||
29 | L1=zeros(r,c,max_count); | ||
30 | L2=zeros(r,c,max_count); | ||
31 | phi=zeros(r,c,max_count); | ||
32 | pol=zeros(r,c,max_count); | ||
33 | con=zeros(r,c,max_count); | ||
34 | |||
35 | fprintf(1,'Integration window size: '); | ||
36 | counter=1; | ||
37 | for n=window_sizes | ||
38 | fprintf(1,'%d ',n); | ||
39 | Txx=gaussN(GGTxx,n); | ||
40 | Txy=gaussN(GGTxy,n); | ||
41 | Tyy=gaussN(GGTyy,n); | ||
42 | |||
43 | tr=Txx+Tyy; | ||
44 | V1=0.5*sqrt(tr.^2-4*(Txx.*Tyy-Txy.^2)); | ||
45 | V1=real(V1); | ||
46 | |||
47 | L1(:,:,counter)=0.5*tr+V1; | ||
48 | L2(:,:,counter)=0.5*tr-V1; | ||
49 | phi(:,:,counter)=0.5*atan2(2*Txy,Txx-Tyy); | ||
50 | |||
51 | % do polarity stuff here | ||
52 | grad_smooth_x=gaussN(Gx,n); | ||
53 | grad_smooth_y=gaussN(Gy,n); | ||
54 | grad_smooth_mag=sqrt(grad_smooth_x.^2+grad_smooth_y.^2); | ||
55 | grad_mag=sqrt(Gx.^2+Gy.^2); | ||
56 | grad_mag_smooth=gaussN(grad_mag,n); | ||
57 | pol(:,:,counter)=grad_smooth_mag./(grad_mag_smooth+eps); | ||
58 | |||
59 | % contrast calculation | ||
60 | con(:,:,counter)=tr; | ||
61 | counter=counter+1; | ||
62 | end | ||
63 | fprintf(1,'\n') | ||
64 | |||
65 | aniso=1-L2./(L1+eps); | ||
66 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/wismm3.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/wismm3.m new file mode 100755 index 0000000..89c56ef --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/wismm3.m | |||
@@ -0,0 +1,71 @@ | |||
1 | function [L1,L2,phi,aniso,pol,con,window_sizes]=wismm3(V); | ||
2 | % [L1,L2,phi,aniso,pol,con,window_sizes]=wismm3(V); | ||
3 | % Calculate windowed image second moment matrices for image V and return | ||
4 | % the following values: | ||
5 | % | ||
6 | % L1 is the larger eigenvalue (lambda_1) | ||
7 | % L2 is the smaller eigenvalue (lambda_2) | ||
8 | % phi is the angle of the 1st eigenvector (phi) | ||
9 | % | ||
10 | |||
11 | [Gx,Gy]=gradient(V); | ||
12 | |||
13 | GGTxx=Gx.^2; | ||
14 | GGTxy=Gx.*Gy; | ||
15 | GGTyy=Gy.^2; | ||
16 | |||
17 | [r,c]=size(V); | ||
18 | |||
19 | min_window_size=3; | ||
20 | max_window_size=3*round(min(r,c)/16); | ||
21 | if (-1)^max_window_size==1 | ||
22 | max_window_size=max_window_size+1; | ||
23 | end | ||
24 | window_step_size=2; | ||
25 | |||
26 | window_sizes=min_window_size:2:max_window_size; | ||
27 | max_count=length(window_sizes); | ||
28 | |||
29 | L1=zeros(r,c,max_count); | ||
30 | L2=zeros(r,c,max_count); | ||
31 | phi=zeros(r,c,max_count); | ||
32 | pol=zeros(r,c,max_count); | ||
33 | con=zeros(r,c,max_count); | ||
34 | |||
35 | fprintf(1,'Integration window size: '); | ||
36 | counter=1; | ||
37 | for n=window_sizes | ||
38 | fprintf(1,'%d ',n); | ||
39 | Txx=gaussN(GGTxx,n); | ||
40 | Txy=gaussN(GGTxy,n); | ||
41 | Tyy=gaussN(GGTyy,n); | ||
42 | |||
43 | tr=Txx+Tyy; | ||
44 | V1=0.5*sqrt(tr.^2-4*(Txx.*Tyy-Txy.^2)); | ||
45 | V1=real(V1); | ||
46 | |||
47 | L1(:,:,counter)=0.5*tr+V1; | ||
48 | L2(:,:,counter)=0.5*tr-V1; | ||
49 | phi(:,:,counter)=0.5*atan2(2*Txy,Txx-Tyy); | ||
50 | |||
51 | % do polarity stuff here | ||
52 | [P,angle_vector]=polarity(Gx,Gy,n); | ||
53 | quant_bound=angle_vector(2)/2; | ||
54 | % (quantize angle and pull corresponding polarity out of P) | ||
55 | % (perhaps use set-theoretic functions for masking out P???) | ||
56 | for m=1:length(angle_vector); | ||
57 | a=angle_vector(end-m+1); | ||
58 | old_pol=pol(:,:,counter); | ||
59 | Pmask=abs(cos(phi(:,:,counter)-a))>=cos(quant_bound); | ||
60 | Pmask=Pmask&(old_pol==0); % prevent pileup on quant. boundaries | ||
61 | pol(:,:,counter)=old_pol+(Pmask.*P(:,:,m)); | ||
62 | end | ||
63 | |||
64 | % contrast calculation | ||
65 | con(:,:,counter)=tr; | ||
66 | counter=counter+1; | ||
67 | end | ||
68 | fprintf(1,'\n') | ||
69 | |||
70 | aniso=1-L2./(L1+eps); | ||
71 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/write_command.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/write_command.m new file mode 100755 index 0000000..954a39e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/write_command.m | |||
@@ -0,0 +1,8 @@ | |||
1 | function write_command(fname,fn_base,para) | ||
2 | |||
3 | fid = fopen(fname,'w'); | ||
4 | |||
5 | fprintf(fid,'%s ',fn_base); | ||
6 | fprintf(fid,'%d ',para); | ||
7 | fprintf(fid,'\nrun\n'); | ||
8 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/write_test.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/write_test.m new file mode 100755 index 0000000..e444c27 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/write_test.m | |||
@@ -0,0 +1,38 @@ | |||
1 | |||
2 | I_max = 250; | ||
3 | tex_max = 30; | ||
4 | |||
5 | %fnames = [130038,130039,130042,130056,130057]; | ||
6 | %fnames = [334074 334031 334044 334003 334065 334000 334039 334018 334002 334029] | ||
7 | |||
8 | fnames = 130057; | ||
9 | |||
10 | for j=1:length(fnames), | ||
11 | fname = sprintf('images/%d.pgm',fnames(j)); | ||
12 | |||
13 | sigs = [1/sqrt(2),1,sqrt(2),2,2*sqrt(2)];r = 3;szs = round(r*3*sigs); | ||
14 | szs = szs(length(szs))*ones(1,length(szs)); | ||
15 | num_ori = 6; | ||
16 | |||
17 | I = readpgm(fname); | ||
18 | [text_des,filters] = compute_filter_fft(I,sigs,r,szs,num_ori); | ||
19 | |||
20 | outname = sprintf('plaatje_data/%d',fnames(j)); | ||
21 | |||
22 | cutsz =20; | ||
23 | I = cutoff(I,cutsz);%figure(1);im(I); | ||
24 | text_des = cutoff(text_des,cutsz); | ||
25 | |||
26 | writeout_feature(I,text_des(:,:,:),outname,I_max,tex_max); | ||
27 | |||
28 | |||
29 | if 0, | ||
30 | for j=0:30, | ||
31 | cm = sprintf('!mv plaatje_data/134013.pfm_%d.pfm plaatje_data/134013_%d.pfm',j,j); | ||
32 | disp(cm);eval(cm); | ||
33 | end | ||
34 | end | ||
35 | |||
36 | end | ||
37 | |||
38 | exit | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writeout_feature.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writeout_feature.m new file mode 100755 index 0000000..5376d5f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writeout_feature.m | |||
@@ -0,0 +1,40 @@ | |||
1 | function writeout_feature(I,tex,fname,I_max,tex_max) | ||
2 | % | ||
3 | % writeout_feature(I,tex,fname,I_max,tex_max) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | |||
8 | %%%%% print out image | ||
9 | I_min = min(I(:)); | ||
10 | |||
11 | I = I-I_min; | ||
12 | I = min(1,I/(I_max-I_min)); | ||
13 | |||
14 | I = 2*I-1; | ||
15 | |||
16 | j = 0; | ||
17 | fn = sprintf('%s_%d.pfm',fname,j); | ||
18 | cm = sprintf('writepfm: I->%s',fn); | ||
19 | disp(cm); | ||
20 | writepfm(fn,I); | ||
21 | |||
22 | |||
23 | %%% print out texture | ||
24 | nf = size(tex,3) | ||
25 | |||
26 | for j=1:nf, | ||
27 | |||
28 | fn = sprintf('%s_%d.pfm',fname,j); | ||
29 | cm = sprintf('writepfm:tex_%d->%s',j,fn); | ||
30 | disp(cm); | ||
31 | |||
32 | tex(:,:,j) = tex(:,:,j)/tex_max;fprintf('.'); | ||
33 | tex(:,:,j) = tex(:,:,j).*(tex(:,:,j)<=1) + 1*(tex(:,:,j)>1);fprintf('.') | ||
34 | tex(:,:,j) = tex(:,:,j).*(tex(:,:,j)>=-1) + (-1)*(tex(:,:,j)<-1);fprintf('.'); | ||
35 | |||
36 | writepfm(fn,tex(:,:,j)); | ||
37 | |||
38 | |||
39 | end | ||
40 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepfm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepfm.m new file mode 100755 index 0000000..a831970 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepfm.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function writepfm(name,I) | ||
2 | % | ||
3 | % writepfm(name,I) | ||
4 | % | ||
5 | [nr,nc] = size(I); | ||
6 | |||
7 | fid = fopen(name, 'w'); | ||
8 | fprintf(fid, '%d %d\n', nr,nc); | ||
9 | fprintf(fid,'%f ',I); | ||
10 | fclose(fid); | ||
11 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepgm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepgm.m new file mode 100755 index 0000000..113cb18 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepgm.m | |||
@@ -0,0 +1,8 @@ | |||
1 | function I = writepgm(name,I) | ||
2 | |||
3 | [y,x] = size(I); | ||
4 | |||
5 | fid = fopen(name, 'w'); | ||
6 | fprintf(fid, 'P5\n%d %d\n255\n', x,y); | ||
7 | fwrite(fid, I', 'uint8'); | ||
8 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepmm.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepmm.m new file mode 100755 index 0000000..675df93 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepmm.m | |||
@@ -0,0 +1,14 @@ | |||
1 | function writepmm(name,I) | ||
2 | % | ||
3 | % writepmm(name,I) | ||
4 | % | ||
5 | |||
6 | [nr,nc,nb] = size(I); | ||
7 | |||
8 | fid = fopen(name,'w'); | ||
9 | |||
10 | fprintf(fid, 'P5\n%d %d %d\n255\n', nc,nr,nb); | ||
11 | |||
12 | fprintf(fid,'%f ',I); | ||
13 | fclose(fid); | ||
14 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepnm5.m b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepnm5.m new file mode 100755 index 0000000..633fba9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filter_hist/writepnm5.m | |||
@@ -0,0 +1,26 @@ | |||
1 | function writepnm5(name,I) | ||
2 | % | ||
3 | % writepnm5(name,I) | ||
4 | % | ||
5 | % I is a mul-band image | ||
6 | % | ||
7 | |||
8 | [nr,nc,nb] = size(I); | ||
9 | |||
10 | fid = fopen(name,'w'); | ||
11 | |||
12 | fprintf(fid, 'P5\n%d %d %d\n255\n', nc,nr,nb); | ||
13 | |||
14 | n = nr*nc; | ||
15 | |||
16 | J = []; | ||
17 | |||
18 | for j=1:nb, | ||
19 | J = [J,reshape(I(:,:,j)',n,1)]; | ||
20 | end | ||
21 | |||
22 | J = reshape(J',nb*n,1); | ||
23 | |||
24 | fprintf(fid,'%f ',J); | ||
25 | fclose(fid); | ||
26 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/doog1.m b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/doog1.m new file mode 100755 index 0000000..dd8e87b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/doog1.m | |||
@@ -0,0 +1,32 @@ | |||
1 | function H=doog1(sig,r,th,N); | ||
2 | % H=doog1(sig,r,th,N); | ||
3 | |||
4 | |||
5 | % by Serge Belongie | ||
6 | |||
7 | no_pts=N; % no. of points in x,y grid | ||
8 | |||
9 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); | ||
10 | |||
11 | phi=pi*th/180; | ||
12 | sigy=sig; | ||
13 | sigx=r*sig; | ||
14 | R=[cos(phi) -sin(phi); sin(phi) cos(phi)]; | ||
15 | C=R*diag([sigx,sigy])*R'; | ||
16 | |||
17 | X=[x(:) y(:)]; | ||
18 | |||
19 | Gb=gaussian(X,[0 0]',C); | ||
20 | Gb=reshape(Gb,N,N); | ||
21 | |||
22 | m=R*[0 sig]'; | ||
23 | |||
24 | a=1; | ||
25 | b=-1; | ||
26 | |||
27 | % make odd-symmetric filter | ||
28 | Ga=gaussian(X,m/2,C); | ||
29 | Ga=reshape(Ga,N,N); | ||
30 | Gb=rot90(Ga,2); | ||
31 | H=a*Ga+b*Gb; | ||
32 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/doog2.m b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/doog2.m new file mode 100755 index 0000000..a0511cb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/doog2.m | |||
@@ -0,0 +1,38 @@ | |||
1 | function G=doog2(sig,r,th,N); | ||
2 | % G=doog2(sig,r,th,N); | ||
3 | % Make difference of offset gaussians kernel | ||
4 | % theta is in degrees | ||
5 | % (see Malik & Perona, J. Opt. Soc. Amer., 1990) | ||
6 | % | ||
7 | % Example: | ||
8 | % >> imagesc(doog2(1,12,0,64,1)) | ||
9 | % >> colormap(gray) | ||
10 | |||
11 | % by Serge Belongie | ||
12 | |||
13 | no_pts=N; % no. of points in x,y grid | ||
14 | |||
15 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); | ||
16 | |||
17 | phi=pi*th/180; | ||
18 | sigy=sig; | ||
19 | sigx=r*sig; | ||
20 | R=[cos(phi) -sin(phi); sin(phi) cos(phi)]; | ||
21 | C=R*diag([sigx,sigy])*R'; | ||
22 | |||
23 | X=[x(:) y(:)]; | ||
24 | |||
25 | Gb=gaussian(X,[0 0]',C); | ||
26 | Gb=reshape(Gb,N,N); | ||
27 | |||
28 | m=R*[0 sig]'; | ||
29 | Ga=gaussian(X,m,C); | ||
30 | Ga=reshape(Ga,N,N); | ||
31 | Gc=rot90(Ga,2); | ||
32 | |||
33 | a=-1; | ||
34 | b=2; | ||
35 | c=-1; | ||
36 | |||
37 | G = a*Ga + b*Gb + c*Gc; | ||
38 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/make_filterbank_even2.m b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/make_filterbank_even2.m new file mode 100755 index 0000000..f7f4527 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/make_filterbank_even2.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function FB = make_filterbank(num_ori,filter_scales,wsz,enlong) | ||
2 | % | ||
3 | % F = make_filterbank(num_ori,num_scale,wsz) | ||
4 | % | ||
5 | |||
6 | if nargin<4, | ||
7 | enlong = 3; | ||
8 | end | ||
9 | |||
10 | enlong = 2*enlong; | ||
11 | |||
12 | % definine filterbank | ||
13 | %num_ori=6; | ||
14 | %num_scale=3; | ||
15 | |||
16 | num_scale = length(filter_scales); | ||
17 | |||
18 | M1=wsz; % size in pixels | ||
19 | M2=M1; | ||
20 | |||
21 | ori_incr=180/num_ori; | ||
22 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
23 | |||
24 | FB=zeros(M1,M2,num_ori,num_scale); | ||
25 | |||
26 | % elongated filter set | ||
27 | counter = 1; | ||
28 | |||
29 | for m=1:num_scale | ||
30 | for n=1:num_ori | ||
31 | % r=12 here is equivalent to Malik's r=3; | ||
32 | f=doog2(filter_scales(m),enlong,ori_offset+(n-1)*ori_incr,M1); | ||
33 | FB(:,:,n,m)=f; | ||
34 | end | ||
35 | end | ||
36 | |||
37 | FB=reshape(FB,M1,M2,num_scale*num_ori); | ||
38 | total_num_filt=size(FB,3); | ||
39 | |||
40 | for j=1:total_num_filt, | ||
41 | F = FB(:,:,j); | ||
42 | a = sum(sum(abs(F))); | ||
43 | FB(:,:,j) = FB(:,:,j)/a; | ||
44 | end | ||
45 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/make_filterbank_odd2.m b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/make_filterbank_odd2.m new file mode 100755 index 0000000..0059dca --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/make_filterbank_odd2.m | |||
@@ -0,0 +1,46 @@ | |||
1 | function FB = make_filterbank(num_ori,filter_scales,wsz,enlong) | ||
2 | % | ||
3 | % F = make_filterbank(num_ori,num_scale,wsz) | ||
4 | % | ||
5 | |||
6 | if nargin<4, | ||
7 | enlong = 3; | ||
8 | end | ||
9 | |||
10 | enlong = enlong*2; | ||
11 | |||
12 | % definine filterbank | ||
13 | %num_ori=6; | ||
14 | %num_scale=3; | ||
15 | |||
16 | num_scale = length(filter_scales); | ||
17 | |||
18 | M1=wsz; % size in pixels | ||
19 | M2=M1; | ||
20 | |||
21 | ori_incr=180/num_ori; | ||
22 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
23 | |||
24 | FB=zeros(M1,M2,num_ori,num_scale); | ||
25 | |||
26 | |||
27 | % elongated filter set | ||
28 | counter = 1; | ||
29 | |||
30 | for m=1:num_scale | ||
31 | for n=1:num_ori | ||
32 | % r=12 here is equivalent to Malik's r=3; | ||
33 | f=doog1(filter_scales(m),enlong,ori_offset+(n-1)*ori_incr,M1); | ||
34 | FB(:,:,n,m)=f; | ||
35 | end | ||
36 | end | ||
37 | |||
38 | FB=reshape(FB,M1,M2,num_scale*num_ori); | ||
39 | total_num_filt=size(FB,3); | ||
40 | |||
41 | for j=1:total_num_filt, | ||
42 | F = FB(:,:,j); | ||
43 | a = sum(sum(abs(F))); | ||
44 | FB(:,:,j) = FB(:,:,j)/a; | ||
45 | end | ||
46 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/quadedgep2.m b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/quadedgep2.m new file mode 100755 index 0000000..5041377 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/filtersQuad/quadedgep2.m | |||
@@ -0,0 +1,188 @@ | |||
1 | % function [xs,ys,gx,gy,par,threshold,mag,mage,g,FIe,FIo,mago] = quadedgep(I,par,threshold); | ||
2 | % Input: | ||
3 | % I = image | ||
4 | % par = vector for 4 parameters | ||
5 | % [number of filter orientations, number of scale, filter size, elongation] | ||
6 | % To use default values, put 0. | ||
7 | % threshold = threshold on edge strength | ||
8 | % Output: | ||
9 | % [x,y,gx,gy] = locations and gradients of an ordered list of edgels | ||
10 | % x,y could be horizontal or vertical or 45 between pixel sites | ||
11 | % but it is guaranteed that there [floor(y) + (floor(x)-1)*nr] | ||
12 | % is ordered and unique. In other words, each edgel has a unique pixel id. | ||
13 | % par = actual par used | ||
14 | % threshold = actual threshold used | ||
15 | % mag = edge magnitude | ||
16 | % mage = phase map | ||
17 | % g = gradient map at each pixel | ||
18 | % [FIe,FIo] = odd and even filter outputs | ||
19 | % mago = odd filter output of optimum orientation | ||
20 | |||
21 | % Stella X. Yu, 2001 | ||
22 | |||
23 | % This is the multi scale version of the filtering | ||
24 | % For the moment the parameters are defined by default. See line 34 | ||
25 | |||
26 | |||
27 | function [x,y,gx,gy,par,threshold,mag_s,mage,g,FIe,FIo,mago] = quadedgep2(I,par,data,threshold); | ||
28 | |||
29 | |||
30 | if nargin<4 | isempty(threshold), | ||
31 | threshold = 0.1; | ||
32 | end | ||
33 | |||
34 | [r,c] = size(I); | ||
35 | def_par = [4,30,3]; | ||
36 | |||
37 | display_on = 1; | ||
38 | |||
39 | % take care of parameters, any missing value is substituted by a default value | ||
40 | if nargin<2 | isempty(par), | ||
41 | par = def_par; | ||
42 | end | ||
43 | % par(end+1:4)=0; | ||
44 | % par = par(:); | ||
45 | % j = (par>0); | ||
46 | % have_value = [ j, 1-j ]; | ||
47 | % j = 1; n_filter = have_value(j,:) * [par(j); def_par(j)]; | ||
48 | % j = 2; n_scale = have_value(j,:) * [par(j); def_par(j)]; | ||
49 | % j = 3; winsz = have_value(j,:) * [par(j); def_par(j)]; | ||
50 | % j = 4; enlong = have_value(j,:) * [par(j); def_par(j)]; | ||
51 | |||
52 | n_ori = par(1); %if it doesn't work, par<-def_par | ||
53 | |||
54 | winsz = par(2); | ||
55 | enlong = par(3); | ||
56 | |||
57 | % always make filter size an odd number so that the results will not be skewed | ||
58 | j = winsz/2; | ||
59 | if not(j > fix(j) + 0.1), | ||
60 | winsz = winsz + 1; | ||
61 | end | ||
62 | |||
63 | % filter the image with quadrature filters | ||
64 | if (isempty(data.W.scales)) | ||
65 | error ('no scales entered'); | ||
66 | end | ||
67 | |||
68 | n_scale=length(data.W.scales); | ||
69 | filter_scales=data.W.scales; | ||
70 | % | ||
71 | % if strcmp(data.dataWpp.mode,'multiscale') | ||
72 | % n_scale=size((data.dataWpp.scales),2); | ||
73 | % filter_scales=data.dataWpp.scales; | ||
74 | % else | ||
75 | % filter_scales=data.dataWpp.scales(1); | ||
76 | % n_scale=1; | ||
77 | % end | ||
78 | % if n_scale>0&&strcmp(data.dataWpp.mode,'multiscale') | ||
79 | % if (~isempty(data.dataWpp.scales)) | ||
80 | % filter_scales=data.dataWpp.scales; | ||
81 | % else | ||
82 | % filter_scales=zeros(1,n_scale); | ||
83 | % | ||
84 | % for i=1:n_scale, | ||
85 | % filter_scales(i)=(sqrt(2))^(i-1); | ||
86 | % end | ||
87 | % data.dataWpp.scales=filter_scales; | ||
88 | % end | ||
89 | % else filter_scale=1; | ||
90 | % data.dataWpp.scales=filter_scales; | ||
91 | % end | ||
92 | % | ||
93 | % %%%%%%% juste pour que ca tourne | ||
94 | % if ~strcmp(data.dataWpp.mode,'multiscale') | ||
95 | % filter_scales=data.dataWpp.scales(1); | ||
96 | % n_scale=4; | ||
97 | % end | ||
98 | % %%%%%%%%%%%% | ||
99 | |||
100 | FBo = make_filterbank_odd2(n_ori,filter_scales,winsz,enlong); | ||
101 | FBe = make_filterbank_even2(n_ori,filter_scales,winsz,enlong); | ||
102 | n = ceil(winsz/2); | ||
103 | f = [fliplr(I(:,2:n+1)), I, fliplr(I(:,c-n:c-1))]; | ||
104 | f = [flipud(f(2:n+1,:)); f; flipud(f(r-n:r-1,:))]; | ||
105 | FIo = fft_filt_2(f,FBo,1); | ||
106 | FIo = FIo(n+[1:r],n+[1:c],:); | ||
107 | FIe = fft_filt_2(f,FBe,1); | ||
108 | FIe = FIe(n+[1:r],n+[1:c],:); | ||
109 | |||
110 | % compute the orientation energy and recover a smooth edge map | ||
111 | % pick up the maximum energy across scale and orientation | ||
112 | % even filter's output: as it is the second derivative, zero cross localize the edge | ||
113 | % odd filter's output: orientation | ||
114 | |||
115 | [nr,nc,nb] = size(FIe); | ||
116 | |||
117 | FIe = reshape(FIe, nr,nc,n_ori,length(filter_scales)); | ||
118 | FIo = reshape(FIo, nr,nc,n_ori,length(filter_scales)); | ||
119 | |||
120 | mag_s = zeros(nr,nc,n_scale); | ||
121 | mag_a = zeros(nr,nc,n_ori); | ||
122 | |||
123 | mage = zeros(nr,nc,n_scale); | ||
124 | mago = zeros(nr,nc,n_scale); | ||
125 | mage = zeros(nr,nc,n_scale); | ||
126 | mago = zeros(nr,nc,n_scale); | ||
127 | |||
128 | |||
129 | |||
130 | for i = 1:n_scale, | ||
131 | mag_s(:,:,i) = sqrt(sum(FIo(:,:,:,i).^2,3)+sum(FIe(:,:,:,i).^2,3)); | ||
132 | mag_a = sqrt(FIo(:,:,:,i).^2+FIe(:,:,:,i).^2); | ||
133 | [tmp,max_id] = max(mag_a,[],3); | ||
134 | |||
135 | base_size = nr * nc; | ||
136 | id = [1:base_size]'; | ||
137 | mage(:,:,i) = reshape(FIe(id+(max_id(:)-1)*base_size+(i-1)*base_size*n_ori),[nr,nc]); | ||
138 | mago(:,:,i) = reshape(FIo(id+(max_id(:)-1)*base_size+(i-1)*base_size*n_ori),[nr,nc]); | ||
139 | |||
140 | mage(:,:,i) = (mage(:,:,i)>0) - (mage(:,:,i)<0); | ||
141 | |||
142 | if display_on, | ||
143 | ori_incr=pi/n_ori; % to convert jshi's coords to conventional image xy | ||
144 | ori_offset=ori_incr/2; | ||
145 | theta = ori_offset+([1:n_ori]-1)*ori_incr; % orientation detectors | ||
146 | % [gx,gy] are image gradient in image xy coords, winner take all | ||
147 | |||
148 | ori = theta(max_id); | ||
149 | ori = ori .* (mago(:,:,i)>0) + (ori + pi).*(mago(:,:,i)<0); | ||
150 | gy{i} = mag_s(:,:,i) .* cos(ori); | ||
151 | gx{i} = -mag_s(:,:,i) .* sin(ori); | ||
152 | g = cat(3,gx{i},gy{i}); | ||
153 | |||
154 | % phase map: edges are where the phase changes | ||
155 | mag_th = max(max(mag_s(:,:,i))) * threshold; | ||
156 | eg = (mag_s(:,:,i)>mag_th); | ||
157 | h = eg & [(mage(2:r,:,i) ~= mage(1:r-1,:,i)); zeros(1,nc)]; | ||
158 | v = eg & [(mage(:,2:c,i) ~= mage(:,1:c-1,i)), zeros(nr,1)]; | ||
159 | [y{i},x{i}] = find(h | v); | ||
160 | k = y{i} + (x{i}-1) * nr; | ||
161 | h = h(k); | ||
162 | v = v(k); | ||
163 | y{i} = y{i} + h * 0.5; % i | ||
164 | x{i} = x{i} + v * 0.5; % j | ||
165 | t = h + v * nr; | ||
166 | gx{i} = g(k) + g(k+t); | ||
167 | k = k + (nr * nc); | ||
168 | gy{i} = g(k) + g(k+t); | ||
169 | |||
170 | % figure(1); | ||
171 | % clf; | ||
172 | % imagesc(I);colormap(gray); | ||
173 | % hold on; | ||
174 | % quiver(x,y,gx,gy); hold off; | ||
175 | % title(sprintf('scale = %d, press return',i)); | ||
176 | |||
177 | % pause; | ||
178 | 0; | ||
179 | else | ||
180 | x = []; | ||
181 | y = []; | ||
182 | gx = []; | ||
183 | gy =[]; | ||
184 | g= []; | ||
185 | end | ||
186 | end | ||
187 | |||
188 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/convert422.m b/SD-VBS/common/toolbox/toolbox_basic/io/convert422.m new file mode 100755 index 0000000..919e82e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/convert422.m | |||
@@ -0,0 +1,27 @@ | |||
1 | image_current = '/hid/jshi'; | ||
2 | |||
3 | image_dir = 'vr05_5 '; | ||
4 | pg_path = '/hid/jshi/422toppm/422toppm'; | ||
5 | |||
6 | cm = sprintf('cd %s',image_dir); | ||
7 | disp(cm); | ||
8 | eval(cm); | ||
9 | |||
10 | d = dir('seq*'); | ||
11 | filename = char(sort({d.name})); | ||
12 | |||
13 | for j=1:size(filename), | ||
14 | cm = sprintf('!%s %s',pg_path,deblank(filename(j,:))); | ||
15 | disp(cm); | ||
16 | eval(cm); | ||
17 | end | ||
18 | |||
19 | |||
20 | %%% change back | ||
21 | cm = sprintf('cd %s',image_current); | ||
22 | disp(cm);eval(cm); | ||
23 | |||
24 | |||
25 | if 0, | ||
26 | deblank(filename(f,:)); | ||
27 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/im_vd.m b/SD-VBS/common/toolbox/toolbox_basic/io/im_vd.m new file mode 100755 index 0000000..590cd9b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/im_vd.m | |||
@@ -0,0 +1,6 @@ | |||
1 | function J = im_vd(I); | ||
2 | |||
3 | J(:,:,1) = I(1:2:end,1:2:end); | ||
4 | J(:,:,2) = I(2:2:end,1:2:end); | ||
5 | |||
6 | montage2(J); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/imread2.m b/SD-VBS/common/toolbox/toolbox_basic/io/imread2.m new file mode 100755 index 0000000..27a5e4b --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/imread2.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function I = imread2(fname,im_dir); | ||
2 | % | ||
3 | % I = imread2(fname,im_dir); | ||
4 | % | ||
5 | |||
6 | cur_dir = pwd; | ||
7 | |||
8 | if nargin>1, | ||
9 | cd(im_dir); | ||
10 | end | ||
11 | |||
12 | %%% put on the necessary extension | ||
13 | d = dir(fname); | ||
14 | |||
15 | if isempty(d), | ||
16 | d = dir([fname,'*']); | ||
17 | end | ||
18 | |||
19 | if isempty(d), | ||
20 | I = []; | ||
21 | else | ||
22 | |||
23 | fname = d.name; | ||
24 | |||
25 | %%% find extension | ||
26 | k = findstr(fname,'.'); | ||
27 | ext = fname(k(end)+1:end); | ||
28 | |||
29 | if (ext == 'bz2'), | ||
30 | cm = sprintf('!bzip2 -d %s',fname); | ||
31 | disp(cm);eval(cm); | ||
32 | I = imread2(fname(1:k(end-1)-1)); | ||
33 | cm = sprintf('!bzip2 %s',fname(1:k(end)-1)); | ||
34 | disp(cm);eval(cm); | ||
35 | elseif (ext == 'ppm'); | ||
36 | I = readppm(fname); | ||
37 | elseif (ext == 'pgm'); | ||
38 | I = readpgm(fname); | ||
39 | else | ||
40 | I = imread(fname); | ||
41 | I = double(I)/255; | ||
42 | end | ||
43 | end | ||
44 | |||
45 | cd(cur_dir); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/peek_pgm_size.m b/SD-VBS/common/toolbox/toolbox_basic/io/peek_pgm_size.m new file mode 100755 index 0000000..13e54cd --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/peek_pgm_size.m | |||
@@ -0,0 +1,19 @@ | |||
1 | function [nr,nc] = peek_pgm_size(filename) | ||
2 | % function [nr,nc] = peek_pgm_size(filename) | ||
3 | % this is my version of pgmread for the pgm file created by XV. | ||
4 | % | ||
5 | % this program also corrects for the shifts in the image from pm file. | ||
6 | |||
7 | |||
8 | fid = fopen(filename,'r'); | ||
9 | fscanf(fid, 'P5\n'); | ||
10 | cmt = '#'; | ||
11 | while findstr(cmt, '#'), | ||
12 | cmt = fgets(fid); | ||
13 | if length(findstr(cmt, '#')) ~= 1, | ||
14 | YX = sscanf(cmt, '%d %d'); | ||
15 | nc = YX(1); nr = YX(2); | ||
16 | end | ||
17 | end | ||
18 | |||
19 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/pgmread.m b/SD-VBS/common/toolbox/toolbox_basic/io/pgmread.m new file mode 100755 index 0000000..49a35a8 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/pgmread.m | |||
@@ -0,0 +1,24 @@ | |||
1 | function [img,header] = pgmread(filename) | ||
2 | % | ||
3 | % [img,header] = pgmread(filename) | ||
4 | |||
5 | [fid, msg] = fopen(filename, 'r'); | ||
6 | if fid == -1, | ||
7 | error(msg) | ||
8 | end | ||
9 | |||
10 | head = []; | ||
11 | good = 0; | ||
12 | while (good == 0) , | ||
13 | l = fgetl(fid); | ||
14 | if (length(l) == 3), | ||
15 | if (l == '255'), | ||
16 | good = 1; | ||
17 | sze = sscanf(header,'%d'); | ||
18 | end | ||
19 | end | ||
20 | header= l; | ||
21 | end | ||
22 | |||
23 | img = fread(fid, sze', 'uchar')'; | ||
24 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/ppmtojpg.m b/SD-VBS/common/toolbox/toolbox_basic/io/ppmtojpg.m new file mode 100755 index 0000000..ce47e45 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/ppmtojpg.m | |||
@@ -0,0 +1,25 @@ | |||
1 | function []= ppm2jpg(fname,dlm,ori) | ||
2 | % | ||
3 | % ppm2jpg(fname,dlm,ori) | ||
4 | % | ||
5 | % dlm =1, remove the file extension from fname | ||
6 | % before convert | ||
7 | % ori =1, transpose the image | ||
8 | % | ||
9 | |||
10 | if dlm, | ||
11 | dlm = findstr(fname,'.'); | ||
12 | fname = fname(1:dlm(end)-1); | ||
13 | end | ||
14 | |||
15 | fname_1 = sprintf('%s.ppm',fname); | ||
16 | I = readppm(fname_1); | ||
17 | |||
18 | if ori == 1, | ||
19 | I = permute(I,[2 1 3]); | ||
20 | end | ||
21 | |||
22 | |||
23 | fname_2 = sprintf('%s.jpg',fname); | ||
24 | imwrite(I,fname_2,'jpeg','Quality',90); | ||
25 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read422.m b/SD-VBS/common/toolbox/toolbox_basic/io/read422.m new file mode 100755 index 0000000..31a27f9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read422.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function I = read422(fname,nc); | ||
2 | % | ||
3 | % I = read422(fname,width); | ||
4 | % | ||
5 | % read in a .422 file, need to pass image width, default = 640 | ||
6 | % | ||
7 | |||
8 | % assume image width = 640 | ||
9 | if nargin<2, | ||
10 | nc = 640; | ||
11 | end | ||
12 | |||
13 | %% find the image size | ||
14 | fid = fopen(fname); | ||
15 | fseek(fid,0,1); | ||
16 | fsize = ftell(fid); | ||
17 | |||
18 | nr = fsize/nc/2; | ||
19 | fseek(fid,0,-1); | ||
20 | |||
21 | %% read in Ybr data | ||
22 | data = fread(fid,fsize,'uchar'); | ||
23 | |||
24 | %%% extract Y, Cb, Cr | ||
25 | Y1 = data(1:2:end); Y1 = reshape(Y1,nc,nr)'; | ||
26 | Cb1 = data(2:4:end); Cb1 = reshape(Cb1,nc/2,nr)'; | ||
27 | Cr1 = data(4:4:end); Cr1 = reshape(Cr1,nc/2,nr)'; | ||
28 | |||
29 | Cb = zeros(size(Y1)); | ||
30 | Cr = zeros(size(Y1)); | ||
31 | |||
32 | Cb(:,1:2:end) = Cb1; Cb(:,2:2:end) = Cb1; | ||
33 | %Cb(:,2:2:end) = 0.5*(Cb1+[Cb1(:,2:end),Cb1(:,end)]); | ||
34 | |||
35 | Cr(:,1:2:end) = Cr1; Cr(:,2:2:end) = Cr1; | ||
36 | %Cr(:,2:2:end) = 0.5*(Cr1+[Cr1(:,2:end),Cr1(:,end)]); | ||
37 | |||
38 | %%% convert to r,g,b | ||
39 | r = 1.164*(Y1-16.0) + 1.596*(Cr-128.0); | ||
40 | g = 1.164*(Y1-16.0) - 0.813*(Cr-128.0) - 0.391*(Cb-128.0); | ||
41 | b = 1.164*(Y1-16.0) + 2.018*(Cb-128.0); | ||
42 | |||
43 | I = cat(3,r,g,b); | ||
44 | I = max(0,min(I,255)); | ||
45 | I = I/255; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read422f.m b/SD-VBS/common/toolbox/toolbox_basic/io/read422f.m new file mode 100755 index 0000000..0063000 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read422f.m | |||
@@ -0,0 +1,50 @@ | |||
1 | function I = read422(fname,nc); | ||
2 | % | ||
3 | % I = read422(fname,width); | ||
4 | % | ||
5 | % read in a .422 file, need to pass image width, default = 640 | ||
6 | % | ||
7 | |||
8 | % assume image width = 640 | ||
9 | if nargin<2, | ||
10 | nc = 640; | ||
11 | end | ||
12 | |||
13 | %% find the image size | ||
14 | fid = fopen(fname); | ||
15 | fseek(fid,0,1); | ||
16 | fsize = ftell(fid); | ||
17 | |||
18 | nr = fsize/nc/2; | ||
19 | |||
20 | fseek(fid,0,-1); | ||
21 | |||
22 | %% read in Ybr data | ||
23 | data = fread(fid,fsize,'uchar'); | ||
24 | |||
25 | %%% extract Y, Cb, Cr | ||
26 | Y1 = data(1:2:end); Y1 = reshape(Y1,nc,nr)'; | ||
27 | Cb1 = data(2:4:end); Cb1 = reshape(Cb1,nc/2,nr)'; | ||
28 | Cr1 = data(4:4:end); Cr1 = reshape(Cr1,nc/2,nr)'; | ||
29 | |||
30 | Cb = zeros(size(Y1)); | ||
31 | Cr = zeros(size(Y1)); | ||
32 | |||
33 | Cb(:,1:2:end) = Cb1; Cb(:,2:2:end) = Cb1; | ||
34 | %Cb(:,2:2:end) = 0.5*(Cb1+[Cb1(:,2:end),Cb1(:,end)]); | ||
35 | |||
36 | Cr(:,1:2:end) = Cr1; Cr(:,2:2:end) = Cr1; | ||
37 | %Cr(:,2:2:end) = 0.5*(Cr1+[Cr1(:,2:end),Cr1(:,end)]); | ||
38 | |||
39 | %%% convert to r,g,b | ||
40 | r = 1.164*(Y1-16.0) + 1.596*(Cr-128.0); | ||
41 | g = 1.164*(Y1-16.0) - 0.813*(Cr-128.0) - 0.391*(Cb-128.0); | ||
42 | b = 1.164*(Y1-16.0) + 2.018*(Cb-128.0); | ||
43 | |||
44 | r = flipud(max(0,min(r,255))); | ||
45 | g = flipud(max(0,min(g,255))); | ||
46 | b = flipud(max(0,min(b,255))); | ||
47 | |||
48 | I = cat(3,r,g,b); | ||
49 | |||
50 | I = permute(I/255,[2,1,3]); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read_cimgs.m b/SD-VBS/common/toolbox/toolbox_basic/io/read_cimgs.m new file mode 100755 index 0000000..d5df7f5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read_cimgs.m | |||
@@ -0,0 +1,40 @@ | |||
1 | function Is = read_imgs(homedir,imgdir,prename,postname,digits,startid,endid,step_img) | ||
2 | % | ||
3 | % Is = read_imgs(homedir,imgdir,prename,postname,digits,startid,endid,step_img) | ||
4 | % | ||
5 | |||
6 | |||
7 | |||
8 | command = ['%s%s%s%.',num2str(digits),'d%s']; | ||
9 | |||
10 | fname = sprintf(command,homedir,imgdir,prename,startid,postname); | ||
11 | disp(fname); | ||
12 | if (strcmp('.ppm',postname)), | ||
13 | I1 = readppm(fname); | ||
14 | else | ||
15 | I1 = imread(fname); | ||
16 | end | ||
17 | |||
18 | |||
19 | Is = zeros(size(I1,1),size(I1,2),size(I1,3),1+floor((endid-startid)/step_img)); | ||
20 | Is(:,:,:,1) = I1; | ||
21 | im_id = 1; | ||
22 | for j = startid+step_img:step_img:endid, | ||
23 | command = ['%s%s%s%.',num2str(digits),'d%s']; | ||
24 | fname = sprintf(command,homedir,imgdir,prename,j,postname); | ||
25 | disp(fname); | ||
26 | im_id = im_id+1; | ||
27 | |||
28 | if (strcmp('.ppm',postname)), | ||
29 | Is(:,:,:,im_id) = readppm(fname); | ||
30 | else | ||
31 | a = imread(fname); | ||
32 | Is(:,:,:,im_id) = a; | ||
33 | end | ||
34 | end | ||
35 | |||
36 | |||
37 | |||
38 | |||
39 | |||
40 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read_ev_pgm.m b/SD-VBS/common/toolbox/toolbox_basic/io/read_ev_pgm.m new file mode 100755 index 0000000..3f7b69d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read_ev_pgm.m | |||
@@ -0,0 +1,26 @@ | |||
1 | function [evs,ev_info] = read_ev_pgm(basename,start_id,end_id,neigs) | ||
2 | % | ||
3 | % evs = read_ev_pgm(basename,start_id,end_id,neigs) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | fname = sprintf('%s_ev_%.2d.%.2d.pgm',basename,start_id,1) | ||
8 | [nr,nc] = peek_pgm_size(fname); | ||
9 | |||
10 | evs = zeros(nr,nc,neigs-1,start_id-end_id+1); | ||
11 | ev_info = zeros(4,neigs-1,start_id-end_id+1); | ||
12 | |||
13 | for j=start_id:end_id, | ||
14 | for k=1:neigs-1, | ||
15 | |||
16 | fname = sprintf('%s_ev_%.2d.%.2d.pgm',basename,j,k); | ||
17 | [I,info] = readpgm_evinfo(fname); | ||
18 | |||
19 | if (length(info)<4) | ||
20 | info = [0;0;0;0]; | ||
21 | end | ||
22 | |||
23 | evs(:,:,k,j-start_id+1) = I; | ||
24 | ev_info(:,k,j-start_id+1) = info'; | ||
25 | end | ||
26 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read_ev_pgm2.m b/SD-VBS/common/toolbox/toolbox_basic/io/read_ev_pgm2.m new file mode 100755 index 0000000..b0cc3f9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read_ev_pgm2.m | |||
@@ -0,0 +1,29 @@ | |||
1 | function [evs,ev_info] = read_ev_pgm2(basename,start_id,end_id,neigs) | ||
2 | % | ||
3 | % evs = read_ev_pgm(basename,start_id,end_id,neigs) | ||
4 | % | ||
5 | % read_ev_pgm.m modified by SXY in Feb. 2001. | ||
6 | % The first eigenvector is also included | ||
7 | |||
8 | fname = sprintf('%s_ev_%.2d.%.2d.pgm',basename,start_id,1) | ||
9 | [nr,nc] = peek_pgm_size(fname); | ||
10 | |||
11 | evs = zeros(nr,nc,neigs,start_id-end_id+1); | ||
12 | ev_info = zeros(4,neigs,start_id-end_id+1); | ||
13 | |||
14 | for j=start_id:end_id, | ||
15 | |||
16 | for k=1:neigs, | ||
17 | |||
18 | fname = sprintf('%s_ev_%.2d.%.2d.pgm',basename,j,k-1); | ||
19 | |||
20 | [I,info] = readpgm_evinfo(fname); | ||
21 | |||
22 | if (length(info)<4) | ||
23 | info = [0;0;0;0]; | ||
24 | end | ||
25 | |||
26 | evs(:,:,k,j-start_id+1) = I; | ||
27 | ev_info(:,k,j-start_id+1) = info'; | ||
28 | end | ||
29 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read_ev_pgm_real.m b/SD-VBS/common/toolbox/toolbox_basic/io/read_ev_pgm_real.m new file mode 100755 index 0000000..d985679 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read_ev_pgm_real.m | |||
@@ -0,0 +1,30 @@ | |||
1 | function [evs,ev_info] = read_ev_pgm(basename,start_id,end_id,neigs) | ||
2 | % | ||
3 | % evs = read_ev_pgm(basename,start_id,end_id,neigs) | ||
4 | % | ||
5 | % | ||
6 | |||
7 | fname = sprintf('%s_ev_%.2d.%.2d.pgm',basename,start_id,1); | ||
8 | [nr,nc] = peek_pgm_size(fname); | ||
9 | |||
10 | evs = zeros(nr,nc,neigs-1,start_id-end_id+1); | ||
11 | ev_info = zeros(4,neigs-1,start_id-end_id+1); | ||
12 | |||
13 | for j=start_id:end_id, | ||
14 | for k=1:neigs, | ||
15 | |||
16 | fname = sprintf('%s_ev_%.2d.%.2d.pgm',basename,j,k-1); | ||
17 | [I,info] = readpgm_evinfo(fname); | ||
18 | |||
19 | evs(:,:,k,j-start_id+1) = I; | ||
20 | ev_info(:,k,j-start_id+1) = info'; | ||
21 | end | ||
22 | end | ||
23 | |||
24 | evs = squeeze(evs); | ||
25 | |||
26 | for j=1:neigs, | ||
27 | evs(:,:,j) = (evs(:,:,j)/ev_info(3,j)) +ev_info(1,j); | ||
28 | %evs(:,:,j) = evs(:,:,j)/norm(reshape(evs(:,:,j),nr*nc,1)); | ||
29 | end | ||
30 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read_imgs.m b/SD-VBS/common/toolbox/toolbox_basic/io/read_imgs.m new file mode 100755 index 0000000..f84486c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read_imgs.m | |||
@@ -0,0 +1,47 @@ | |||
1 | function Is = read_imgs(homedir,imgdir,prename,postname,digits,startid,endid,step_img) | ||
2 | % | ||
3 | % Is = read_imgs(homedir,imgdir,prename,postname,digits,startid,endid,step_img) | ||
4 | % | ||
5 | |||
6 | |||
7 | |||
8 | command = ['%s%s%s%.',num2str(digits),'d%s']; | ||
9 | |||
10 | fname = sprintf(command,homedir,imgdir,prename,startid,postname); | ||
11 | disp(fname); | ||
12 | if (strcmp('.pgm',postname)), | ||
13 | I1 = readpgm(fname); | ||
14 | elseif (strcmp('.ppm',postname)) | ||
15 | a = readppm(fname); | ||
16 | I1 = sum(a,3); | ||
17 | else | ||
18 | a = imread(fname); a = sum(double(a),3); | ||
19 | I1 = a; | ||
20 | end | ||
21 | |||
22 | |||
23 | Is = zeros(size(I1,1),size(I1,2),1+floor((endid-startid)/step_img)); | ||
24 | Is(:,:,1) = I1; | ||
25 | im_id = 1; | ||
26 | for j = startid+step_img:step_img:endid, | ||
27 | command = ['%s%s%s%.',num2str(digits),'d%s']; | ||
28 | fname = sprintf(command,homedir,imgdir,prename,j,postname); | ||
29 | disp(fname); | ||
30 | im_id = im_id+1; | ||
31 | |||
32 | if (strcmp('.pgm',postname)), | ||
33 | Is(:,:,im_id) = readpgm(fname); | ||
34 | elseif (strcmp('.ppm',postname)) | ||
35 | a = readppm(fname); | ||
36 | Is(:,:,im_id) = sum(a,3); | ||
37 | else | ||
38 | a = imread(fname); a = sum(double(a),3); | ||
39 | Is(:,:,im_id) = a; | ||
40 | end | ||
41 | end | ||
42 | |||
43 | |||
44 | |||
45 | |||
46 | |||
47 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read_pmm.m b/SD-VBS/common/toolbox/toolbox_basic/io/read_pmm.m new file mode 100755 index 0000000..9e2eed1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read_pmm.m | |||
@@ -0,0 +1,12 @@ | |||
1 | function I = read_pmm(fname) | ||
2 | |||
3 | fid = fopen(fname,'r'); | ||
4 | |||
5 | [A] = fscanf(fid,'%d\n',3); | ||
6 | |||
7 | I = fscanf(fid,'%d',prod(A)); | ||
8 | |||
9 | |||
10 | I = reshape(I,A(2),A(1))'; | ||
11 | |||
12 | I = squeeze(I); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read_scan.m b/SD-VBS/common/toolbox/toolbox_basic/io/read_scan.m new file mode 100755 index 0000000..6ad818a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read_scan.m | |||
@@ -0,0 +1,42 @@ | |||
1 | function [img,sizeinfo] = pgmread(filename) | ||
2 | % function img = pgmread(filename) | ||
3 | % this is my version of pgmread for the pgm file created by XV. | ||
4 | % | ||
5 | % this program also corrects for the shifts in the image from pm file. | ||
6 | |||
7 | |||
8 | fname_header = sprintf('%s.h01',filename); | ||
9 | fname_data = sprintf('%s.i01',filename); | ||
10 | |||
11 | fid = fopen(fname_header,'r'); | ||
12 | |||
13 | |||
14 | done = 0; | ||
15 | while done~=3, | ||
16 | cmt = fgets(fid) | ||
17 | if (findstr(cmt,'!matrix size[1]')), | ||
18 | nc = sscanf(cmt,'!matrix size[1] :=%d'); | ||
19 | done = done+1; | ||
20 | elseif (findstr(cmt,'!matrix size[2]')), | ||
21 | nr = sscanf(cmt,'!matrix size[2] :=%d'); | ||
22 | done = done+1; | ||
23 | elseif (findstr(cmt,'!matrix size[3]')), | ||
24 | ns = sscanf(cmt,'!matrix size[3] :=%d'); | ||
25 | done = done+1; | ||
26 | end | ||
27 | end | ||
28 | fclose(fid); | ||
29 | |||
30 | fid = fopen(fname_data,'r'); | ||
31 | |||
32 | %img = fscanf(fid,'%d',size); | ||
33 | %img = img'; | ||
34 | |||
35 | img = fread(fid,nc*nr*ns,'uint8'); | ||
36 | img = reshape(img,nc,nr,ns); | ||
37 | |||
38 | sizeinfo(1) = nr; | ||
39 | sizeinfo(2) = nc; | ||
40 | sizeinfo(3) = ns; | ||
41 | |||
42 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/read_seg_file.m b/SD-VBS/common/toolbox/toolbox_basic/io/read_seg_file.m new file mode 100755 index 0000000..a056ebc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/read_seg_file.m | |||
@@ -0,0 +1,36 @@ | |||
1 | function [seg_map,seg] = read_seg(filename) | ||
2 | % | ||
3 | % function seg = read_seg(filename) | ||
4 | % | ||
5 | |||
6 | fid = fopen(filename,'r'); | ||
7 | if (fid < 0), | ||
8 | error(sprintf('can not find file: %s',filename)); | ||
9 | end | ||
10 | |||
11 | header_done =0; | ||
12 | while ~header_done, | ||
13 | |||
14 | cmt = fgets(fid); | ||
15 | if length(findstr(cmt,'#')) ~=1, | ||
16 | header_done = 1; | ||
17 | cmt = fgets(fid); | ||
18 | nc = sscanf(cmt,'width %d\n'); | ||
19 | cmt = fgets(fid); | ||
20 | nr = sscanf(cmt,'height %d\n'); | ||
21 | cmt = fgets(fid); | ||
22 | mseg = sscanf(cmt,'segments %d\n'); | ||
23 | cmt = fgets(fid); | ||
24 | end | ||
25 | end | ||
26 | |||
27 | seg = fscanf(fid,'%d',100*nr); | ||
28 | tmp = length(seg(:))/4; | ||
29 | seg = reshape(seg,4,tmp)'; | ||
30 | |||
31 | seg_map = zeros(nr,nc); | ||
32 | |||
33 | for j=1:tmp, | ||
34 | seg_map(seg(j,2)+1,1+seg(j,3):1+seg(j,4)) = seg(j,1); | ||
35 | end | ||
36 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readlines.m b/SD-VBS/common/toolbox/toolbox_basic/io/readlines.m new file mode 100755 index 0000000..90bc944 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readlines.m | |||
@@ -0,0 +1,30 @@ | |||
1 | function [lines,indexes] = readlines(fname) | ||
2 | % | ||
3 | % [lines,indexes] = readlines(fname) | ||
4 | % Read Edges points from .Ins file produced by "getlines" | ||
5 | % lines: a num_pointsx2 matrix of the edge points | ||
6 | % indexes: the braking point the lines | ||
7 | % | ||
8 | |||
9 | fid = fopen(fname,'r'); | ||
10 | |||
11 | done = 0; | ||
12 | lines = []; | ||
13 | indexes = []; | ||
14 | |||
15 | first_line = fscanf(fid,'%s',1); | ||
16 | |||
17 | while (~done), | ||
18 | num_lines = sscanf(first_line(3:length(first_line)),'%d'); | ||
19 | disp(num_lines); | ||
20 | indexes = [indexes,num_lines]; | ||
21 | a = fscanf(fid,'%f',[2,num_lines]); | ||
22 | lines = [lines;a']; | ||
23 | |||
24 | first_line = fscanf(fid,'%s',1); | ||
25 | if (first_line == []), | ||
26 | done = 1; | ||
27 | end | ||
28 | end | ||
29 | |||
30 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readpdm3.m b/SD-VBS/common/toolbox/toolbox_basic/io/readpdm3.m new file mode 100755 index 0000000..c21fc48 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readpdm3.m | |||
@@ -0,0 +1,16 @@ | |||
1 | function I = readpdm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',3) | ||
6 | A(3) = max(1,A(3)); | ||
7 | |||
8 | I = fscanf(fid,'%d',[A(1)*A(2)*A(3)]); | ||
9 | |||
10 | %I = fscanf(fid,'%f',A(2)*A(1));I = reshape(I,A(1),A(2)); | ||
11 | |||
12 | I = reshape(I,A(2),A(1),A(3)); | ||
13 | |||
14 | I = permute(I,[2,1,3]); | ||
15 | |||
16 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readpdmc.m b/SD-VBS/common/toolbox/toolbox_basic/io/readpdmc.m new file mode 100755 index 0000000..37910b9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readpdmc.m | |||
@@ -0,0 +1,12 @@ | |||
1 | function I = readpfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2); | ||
6 | I = fscanf(fid,'%d',[A(2),A(1)]); | ||
7 | %I = fscanf(fid,'%d',[300,1000]); | ||
8 | I = I'; | ||
9 | |||
10 | %I = fscanf(fid,'%f',A(2)*A(1));I = reshape(I,A(1),A(2)); | ||
11 | |||
12 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readpfm.m b/SD-VBS/common/toolbox/toolbox_basic/io/readpfm.m new file mode 100755 index 0000000..48ecd78 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readpfm.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function I = readpfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2); | ||
6 | I = fscanf(fid,'%f',[A(1),A(2)]); | ||
7 | |||
8 | %I = fscanf(fid,'%f',A(2)*A(1));I = reshape(I,A(1),A(2)); | ||
9 | |||
10 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readpfm3.m b/SD-VBS/common/toolbox/toolbox_basic/io/readpfm3.m new file mode 100755 index 0000000..15ba959 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readpfm3.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function I = readpfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',3); | ||
6 | A(3) = max(1,A(3)); | ||
7 | |||
8 | I = fscanf(fid,'%f',[A(1)*A(2)*A(3)]); | ||
9 | |||
10 | %I = fscanf(fid,'%f',A(2)*A(1));I = reshape(I,A(1),A(2)); | ||
11 | |||
12 | I = reshape(I,A(2),A(1),A(3)); | ||
13 | I = permute(I,[2,1,3]); | ||
14 | |||
15 | I = squeeze(I); | ||
16 | |||
17 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readpfmc.m b/SD-VBS/common/toolbox/toolbox_basic/io/readpfmc.m new file mode 100755 index 0000000..2039002 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readpfmc.m | |||
@@ -0,0 +1,11 @@ | |||
1 | function I = readpfm(filename) | ||
2 | |||
3 | fid = fopen(filename,'r'); | ||
4 | |||
5 | A = fscanf(fid,'%d',2); | ||
6 | I = fscanf(fid,'%f',[A(2),A(1)]); | ||
7 | I = I'; | ||
8 | |||
9 | %I = fscanf(fid,'%f',A(2)*A(1));I = reshape(I,A(1),A(2)); | ||
10 | |||
11 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readpgm.m b/SD-VBS/common/toolbox/toolbox_basic/io/readpgm.m new file mode 100755 index 0000000..7aaf998 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readpgm.m | |||
@@ -0,0 +1,30 @@ | |||
1 | function img = pgmread(filename) | ||
2 | % function img = pgmread(filename) | ||
3 | % this is my version of pgmread for the pgm file created by XV. | ||
4 | % | ||
5 | % this program also corrects for the shifts in the image from pm file. | ||
6 | |||
7 | |||
8 | fid = fopen(filename,'r'); | ||
9 | if (fid < 0), | ||
10 | error(sprintf('can not find file: %s',filename)); | ||
11 | end | ||
12 | |||
13 | fscanf(fid, 'P5\n'); | ||
14 | cmt = '#'; | ||
15 | while findstr(cmt, '#'), | ||
16 | cmt = fgets(fid); | ||
17 | if length(findstr(cmt, '#')) ~= 1, | ||
18 | YX = sscanf(cmt, '%d %d'); | ||
19 | y = YX(1); x = YX(2); | ||
20 | end | ||
21 | end | ||
22 | |||
23 | fgets(fid); | ||
24 | |||
25 | %img = fscanf(fid,'%d',size); | ||
26 | %img = img'; | ||
27 | |||
28 | img = fread(fid,[y,x],'uint8'); | ||
29 | img = img'; | ||
30 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readpgm_evinfo.m b/SD-VBS/common/toolbox/toolbox_basic/io/readpgm_evinfo.m new file mode 100755 index 0000000..69f80ba --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readpgm_evinfo.m | |||
@@ -0,0 +1,35 @@ | |||
1 | function [img,ev_info] = pgmread_evinfo(filename) | ||
2 | % function img = pgmread(filename) | ||
3 | % this is my version of pgmread for the pgm file created by XV. | ||
4 | % | ||
5 | % return the information in line # | ||
6 | |||
7 | |||
8 | fid = fopen(filename,'r'); | ||
9 | |||
10 | if (fid <0), | ||
11 | error(sprintf('can not find file %s',filename)); | ||
12 | end | ||
13 | |||
14 | fscanf(fid, 'P5\n'); | ||
15 | cmt = '#'; | ||
16 | while findstr(cmt, '#'), | ||
17 | cmt = fgets(fid); | ||
18 | if findstr(cmt,'#'), | ||
19 | ev_info = sscanf(cmt,'# minv: %f, maxv: %f, scale: %f, eigval: %f'); | ||
20 | end | ||
21 | if length(findstr(cmt, '#')) ~= 1, | ||
22 | YX = sscanf(cmt, '%d %d'); | ||
23 | y = YX(1); x = YX(2); | ||
24 | end | ||
25 | end | ||
26 | |||
27 | fgets(fid); | ||
28 | |||
29 | %img = fscanf(fid,'%d',size); | ||
30 | %img = img'; | ||
31 | |||
32 | img = fread(fid,[y,x],'uint8'); | ||
33 | img = img'; | ||
34 | fclose(fid); | ||
35 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readpmm.m b/SD-VBS/common/toolbox/toolbox_basic/io/readpmm.m new file mode 100755 index 0000000..88fe907 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readpmm.m | |||
@@ -0,0 +1,22 @@ | |||
1 | function I=readpmm(name) | ||
2 | % | ||
3 | % I=writepmm(name) | ||
4 | % | ||
5 | % I is a mul-band image | ||
6 | % | ||
7 | fid = fopen(name,'r'); | ||
8 | |||
9 | if (fid <0), | ||
10 | error(sprintf('can not find file %s',name)); | ||
11 | end | ||
12 | |||
13 | a = fscanf(fid,'%d',3); | ||
14 | nr = a(1);nc = a(2);nb = a(3); | ||
15 | |||
16 | |||
17 | I = fscanf(fid, '%f\n', nr*nc*nb); | ||
18 | |||
19 | I = reshape(I,nc,nr,nb)'; | ||
20 | I = squeeze(I); | ||
21 | |||
22 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/readppm.m b/SD-VBS/common/toolbox/toolbox_basic/io/readppm.m new file mode 100755 index 0000000..b9dd566 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/readppm.m | |||
@@ -0,0 +1,23 @@ | |||
1 | function [I,r, g, b] = readppm(name) | ||
2 | |||
3 | fid = fopen(name, 'r'); | ||
4 | fscanf(fid, 'P6\n'); | ||
5 | cmt = '#'; | ||
6 | while findstr(cmt, '#'), | ||
7 | cmt = fgets(fid); | ||
8 | if length(findstr(cmt, '#')) ~= 1 | ||
9 | YX = sscanf(cmt, '%d %d'); | ||
10 | y = YX(1); x = YX(2); | ||
11 | end | ||
12 | end | ||
13 | fgets(fid); | ||
14 | packed = fread(fid,[3*y,x],'uint8')'; | ||
15 | r = packed(:,1:3:3*y); | ||
16 | g = packed(:,2:3:3*y); | ||
17 | b = packed(:,3:3:3*y); | ||
18 | fclose(fid); | ||
19 | |||
20 | I(:,:,1) = r; | ||
21 | I(:,:,2) = g; | ||
22 | I(:,:,3) = b; | ||
23 | I = I/255; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/writepgm.m b/SD-VBS/common/toolbox/toolbox_basic/io/writepgm.m new file mode 100755 index 0000000..113cb18 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/writepgm.m | |||
@@ -0,0 +1,8 @@ | |||
1 | function I = writepgm(name,I) | ||
2 | |||
3 | [y,x] = size(I); | ||
4 | |||
5 | fid = fopen(name, 'w'); | ||
6 | fprintf(fid, 'P5\n%d %d\n255\n', x,y); | ||
7 | fwrite(fid, I', 'uint8'); | ||
8 | fclose(fid); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/io/writeppm.m b/SD-VBS/common/toolbox/toolbox_basic/io/writeppm.m new file mode 100755 index 0000000..3d2fed1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/io/writeppm.m | |||
@@ -0,0 +1,14 @@ | |||
1 | function writeppm(name,I) | ||
2 | |||
3 | [y,x,nb] = size(I); | ||
4 | |||
5 | fid = fopen(name, 'w'); | ||
6 | fprintf(fid, 'P6\n%d %d\n255\n', x,y); | ||
7 | |||
8 | I1 = reshape(I(:,:,1)',1,x*y); | ||
9 | I2 = reshape(I(:,:,2)',1,x*y); | ||
10 | I3 = reshape(I(:,:,3)',1,x*y); | ||
11 | |||
12 | fwrite(fid, [I1;I2;I3], 'uint8'); | ||
13 | fclose(fid); | ||
14 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/Contents.m b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/Contents.m new file mode 100755 index 0000000..3ddb232 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/Contents.m | |||
@@ -0,0 +1,26 @@ | |||
1 | %Functions related to the assignment problem. | ||
2 | %Version 1.0, 25-May-1999. | ||
3 | % | ||
4 | %Copyright (c) 1995-1999 Niclas Borlin, Dept. of Computing Science, | ||
5 | % Umea University, SE-901 87 UMEA, Sweden. | ||
6 | % Niclas.Borlin@cs.umu.se. | ||
7 | % www.cs.umu.se/~niclas. | ||
8 | % | ||
9 | %All standard disclaimers apply. | ||
10 | % | ||
11 | %You are free to use this code as you wish. If you use it for a | ||
12 | %publication or in a commercial package, please include an | ||
13 | %acknowledgement and/or at least send me an email. (Looks good in my CV :-). | ||
14 | % | ||
15 | %Main functions: | ||
16 | % hungarian - calculate a solution of the square assignment | ||
17 | % problem. See HELP for a reference. | ||
18 | % condass - calculate a condition number of the solution to the | ||
19 | % assignment problem. See HELP for a reference. | ||
20 | % allcosts - calculate the costs of all possible assignments. | ||
21 | % allperms - calculate all possible permutations/assignments of a | ||
22 | % given problem size. | ||
23 | % | ||
24 | %Test/demo functions. | ||
25 | % demo - short demonstration of the main functions. | ||
26 | % test - test/verification of the main functions. | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/allcosts.m b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/allcosts.m new file mode 100755 index 0000000..ffdb8b9 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/allcosts.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function [c,p]=allcosts(C) | ||
2 | %ALLCOSTS Calculate all costs for an assignment problem. | ||
3 | % | ||
4 | %[c,p]=allcosts(C) | ||
5 | %c returns the costs, p the corresponding permutations. | ||
6 | |||
7 | % v1.0 95-07-18. Niclas Borlin, niclas@cs.umu.se. | ||
8 | |||
9 | p=allperm(size(C,1)); | ||
10 | |||
11 | c=zeros(size(p,1),1); | ||
12 | |||
13 | I=eye(size(C,1)); | ||
14 | |||
15 | for i=1:size(p,1) | ||
16 | c(i)=sum(C(logical(sparse(p(i,:),1:size(C,1),1)))); | ||
17 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/allperm.m b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/allperm.m new file mode 100755 index 0000000..b8d419e --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/allperm.m | |||
@@ -0,0 +1,17 @@ | |||
1 | function p=allperm(n) | ||
2 | %ALLPERM All permutation matrix. | ||
3 | % | ||
4 | %p=allperm(n) | ||
5 | %Returns a matrix with all permutations of 1:n stored row-wise. | ||
6 | |||
7 | % v1.0 95-07-18. Niclas Borlin, niclas@cs.umu.se. | ||
8 | |||
9 | if (n<=1) | ||
10 | p=1; | ||
11 | else | ||
12 | q=allperm(n-1); | ||
13 | p=[]; | ||
14 | for i=1:n | ||
15 | p=[p;i*ones(size(q,1),1) q+(q>=i)]; | ||
16 | end | ||
17 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/condass.m b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/condass.m new file mode 100755 index 0000000..82552e7 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/condass.m | |||
@@ -0,0 +1,54 @@ | |||
1 | function [k,C1,T1,C2,T2]=condass(A) | ||
2 | %CONDASS Calculate condition number of the assigment problem. | ||
3 | % | ||
4 | %[k,C1,T1,C2,T2]=condass(A) | ||
5 | %A - A square cost matrix. | ||
6 | %k - The condition number of the assigment problem. | ||
7 | %C1 - The best assigment. | ||
8 | %T1 - The lowest cost. | ||
9 | %C2 - The second best assignment. | ||
10 | %T2 - The second lowest cost. | ||
11 | % | ||
12 | %The condition number is calculated as the relative difference between | ||
13 | %the best and second best solutions, as described in Nystrom, Soderkvist, | ||
14 | %and Wedin, "A Note on some Identification Problems Arising in Roentgen | ||
15 | %Stereo Photogrammetric Analysis", J of Biomechanics, 27(10):1291-1294, | ||
16 | %1994. | ||
17 | |||
18 | % v1.0 96-09-14. Niclas Borlin, niclas@cs.umu.se. | ||
19 | |||
20 | % A substantial effort was put into this code. If you use it for a | ||
21 | % publication or otherwise, please include an acknowledgement and notify | ||
22 | % me by email. /Niclas | ||
23 | |||
24 | % Create a large number used to block selected assignments. | ||
25 | big=sum(sum(A))+1; | ||
26 | |||
27 | % Get best assigment. | ||
28 | [C1,T1]=hungarian(A); | ||
29 | |||
30 | % Initialize second best solution. | ||
31 | T2=inf; | ||
32 | C2=zeros(size(C1)); | ||
33 | |||
34 | % Create a work matrix. | ||
35 | B=A; | ||
36 | for i=1:length(C1) | ||
37 | % Block assigment in column i. | ||
38 | B(C1(i),i)=big; | ||
39 | % Get best assigment with this one blocked. | ||
40 | [C,T]=hungarian(B); | ||
41 | if (T<T2) | ||
42 | % Remember it if it's the best so far. | ||
43 | T2=T; | ||
44 | C2=C; | ||
45 | end | ||
46 | % Remove blocking in column i. | ||
47 | B(C1(i),i)=A(C1(i),i); | ||
48 | end | ||
49 | |||
50 | % Calculate difference... | ||
51 | mu=T2-T1; | ||
52 | |||
53 | % ...and condition number. | ||
54 | k=T1/mu; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/demo.m b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/demo.m new file mode 100755 index 0000000..2d34e37 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/demo.m | |||
@@ -0,0 +1,38 @@ | |||
1 | A=magic(10); | ||
2 | B=A(4:7,4:7); | ||
3 | disp('Cost matrix:') | ||
4 | disp(B) | ||
5 | disp('Calculating best assignment...'); | ||
6 | [c,t]=hungarian(B); | ||
7 | disp('Best assignment (as row indices):') | ||
8 | disp(c) | ||
9 | disp('Best assignment (as logical matrix):') | ||
10 | disp(logical(full(sparse(c,1:4,1)))) | ||
11 | disp('Lowest cost:') | ||
12 | disp(t) | ||
13 | |||
14 | disp(sprintf('\nCalculating condition number for solution...')); | ||
15 | [k,c1,t1,c2,t2]=condass(B); | ||
16 | disp('Lowest cost (should be same as above): ') | ||
17 | disp(t1) | ||
18 | disp('corresponding assignment (should be same as above):') | ||
19 | disp(c1) | ||
20 | disp('Second lowest cost: ') | ||
21 | disp(t2) | ||
22 | disp('corresponding assignment:') | ||
23 | disp(c2) | ||
24 | disp('Condition number for solution:') | ||
25 | disp(k) | ||
26 | |||
27 | disp(sprintf('\nCalculating all possible costs...')); | ||
28 | [c,p]=allcosts(B); | ||
29 | % Sort by cost. | ||
30 | [y,i]=sort(c); | ||
31 | disp('The three lowest costs:') | ||
32 | disp(c(i(1:3))) | ||
33 | disp('Corresponding assignments:') | ||
34 | disp(p(i(1:3),:)) | ||
35 | disp('The three highest costs:') | ||
36 | disp(c(i(end+[-2:0]))) | ||
37 | disp('Corresponding assignments:') | ||
38 | disp(p(i(end+[-2:0]),:)) | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/hungarian.m b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/hungarian.m new file mode 100755 index 0000000..0b493f0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/hungarian.m | |||
@@ -0,0 +1,464 @@ | |||
1 | function [C,T]=hungarian(A) | ||
2 | %HUNGARIAN Solve the Assignment problem using the Hungarian method. | ||
3 | % | ||
4 | %[C,T]=hungarian(A) | ||
5 | %A - a square cost matrix. | ||
6 | %C - the optimal assignment. | ||
7 | %T - the cost of the optimal assignment. | ||
8 | |||
9 | % Adapted from the FORTRAN IV code in Carpaneto and Toth, "Algorithm 548: | ||
10 | % Solution of the assignment problem [H]", ACM Transactions on | ||
11 | % Mathematical Software, 6(1):104-111, 1980. | ||
12 | |||
13 | % v1.0 96-06-14. Niclas Borlin, niclas@cs.umu.se. | ||
14 | % Department of Computing Science, Umeå University, | ||
15 | % Sweden. | ||
16 | % All standard disclaimers apply. | ||
17 | |||
18 | % A substantial effort was put into this code. If you use it for a | ||
19 | % publication or otherwise, please include an acknowledgement or at least | ||
20 | % notify me by email. /Niclas | ||
21 | |||
22 | [m,n]=size(A); | ||
23 | |||
24 | if (m~=n) | ||
25 | error('HUNGARIAN: Cost matrix must be square!'); | ||
26 | end | ||
27 | |||
28 | % Save original cost matrix. | ||
29 | orig=A; | ||
30 | |||
31 | % Reduce matrix. | ||
32 | A=hminired(A); | ||
33 | |||
34 | % Do an initial assignment. | ||
35 | [A,C,U]=hminiass(A); | ||
36 | |||
37 | % Repeat while we have unassigned rows. | ||
38 | while (U(n+1)) | ||
39 | % Start with no path, no unchecked zeros, and no unexplored rows. | ||
40 | LR=zeros(1,n); | ||
41 | LC=zeros(1,n); | ||
42 | CH=zeros(1,n); | ||
43 | RH=[zeros(1,n) -1]; | ||
44 | |||
45 | % No labelled columns. | ||
46 | SLC=[]; | ||
47 | |||
48 | % Start path in first unassigned row. | ||
49 | r=U(n+1); | ||
50 | % Mark row with end-of-path label. | ||
51 | LR(r)=-1; | ||
52 | % Insert row first in labelled row set. | ||
53 | SLR=r; | ||
54 | |||
55 | % Repeat until we manage to find an assignable zero. | ||
56 | while (1) | ||
57 | % If there are free zeros in row r | ||
58 | if (A(r,n+1)~=0) | ||
59 | % ...get column of first free zero. | ||
60 | l=-A(r,n+1); | ||
61 | |||
62 | % If there are more free zeros in row r and row r in not | ||
63 | % yet marked as unexplored.. | ||
64 | if (A(r,l)~=0 & RH(r)==0) | ||
65 | % Insert row r first in unexplored list. | ||
66 | RH(r)=RH(n+1); | ||
67 | RH(n+1)=r; | ||
68 | |||
69 | % Mark in which column the next unexplored zero in this row | ||
70 | % is. | ||
71 | CH(r)=-A(r,l); | ||
72 | end | ||
73 | else | ||
74 | % If all rows are explored.. | ||
75 | if (RH(n+1)<=0) | ||
76 | % Reduce matrix. | ||
77 | [A,CH,RH]=hmreduce(A,CH,RH,LC,LR,SLC,SLR); | ||
78 | end | ||
79 | |||
80 | % Re-start with first unexplored row. | ||
81 | r=RH(n+1); | ||
82 | % Get column of next free zero in row r. | ||
83 | l=CH(r); | ||
84 | % Advance "column of next free zero". | ||
85 | CH(r)=-A(r,l); | ||
86 | % If this zero is last in the list.. | ||
87 | if (A(r,l)==0) | ||
88 | % ...remove row r from unexplored list. | ||
89 | RH(n+1)=RH(r); | ||
90 | RH(r)=0; | ||
91 | end | ||
92 | end | ||
93 | |||
94 | % While the column l is labelled, i.e. in path. | ||
95 | while (LC(l)~=0) | ||
96 | % If row r is explored.. | ||
97 | if (RH(r)==0) | ||
98 | % If all rows are explored.. | ||
99 | if (RH(n+1)<=0) | ||
100 | % Reduce cost matrix. | ||
101 | [A,CH,RH]=hmreduce(A,CH,RH,LC,LR,SLC,SLR); | ||
102 | end | ||
103 | |||
104 | % Re-start with first unexplored row. | ||
105 | r=RH(n+1); | ||
106 | end | ||
107 | |||
108 | % Get column of next free zero in row r. | ||
109 | l=CH(r); | ||
110 | |||
111 | % Advance "column of next free zero". | ||
112 | CH(r)=-A(r,l); | ||
113 | |||
114 | % If this zero is last in list.. | ||
115 | if(A(r,l)==0) | ||
116 | % ...remove row r from unexplored list. | ||
117 | RH(n+1)=RH(r); | ||
118 | RH(r)=0; | ||
119 | end | ||
120 | end | ||
121 | |||
122 | % If the column found is unassigned.. | ||
123 | if (C(l)==0) | ||
124 | % Flip all zeros along the path in LR,LC. | ||
125 | [A,C,U]=hmflip(A,C,LC,LR,U,l,r); | ||
126 | % ...and exit to continue with next unassigned row. | ||
127 | break; | ||
128 | else | ||
129 | % ...else add zero to path. | ||
130 | |||
131 | % Label column l with row r. | ||
132 | LC(l)=r; | ||
133 | |||
134 | % Add l to the set of labelled columns. | ||
135 | SLC=[SLC l]; | ||
136 | |||
137 | % Continue with the row assigned to column l. | ||
138 | r=C(l); | ||
139 | |||
140 | % Label row r with column l. | ||
141 | LR(r)=l; | ||
142 | |||
143 | % Add r to the set of labelled rows. | ||
144 | SLR=[SLR r]; | ||
145 | end | ||
146 | end | ||
147 | end | ||
148 | |||
149 | % Calculate the total cost. | ||
150 | T=sum(orig(logical(sparse(C,1:size(orig,2),1)))); | ||
151 | |||
152 | |||
153 | function A=hminired(A) | ||
154 | %HMINIRED Initial reduction of cost matrix for the Hungarian method. | ||
155 | % | ||
156 | %B=assredin(A) | ||
157 | %A - the unreduced cost matris. | ||
158 | %B - the reduced cost matrix with linked zeros in each row. | ||
159 | |||
160 | % v1.0 96-06-13. Niclas Borlin, niclas@cs.umu.se. | ||
161 | |||
162 | [m,n]=size(A); | ||
163 | |||
164 | % Subtract column-minimum values from each column. | ||
165 | colMin=min(A); | ||
166 | A=A-colMin(ones(n,1),:); | ||
167 | |||
168 | % Subtract row-minimum values from each row. | ||
169 | rowMin=min(A')'; | ||
170 | A=A-rowMin(:,ones(1,n)); | ||
171 | |||
172 | % Get positions of all zeros. | ||
173 | [i,j]=find(A==0); | ||
174 | |||
175 | % Extend A to give room for row zero list header column. | ||
176 | A(1,n+1)=0; | ||
177 | for k=1:n | ||
178 | % Get all column in this row. | ||
179 | cols=j(k==i)'; | ||
180 | % Insert pointers in matrix. | ||
181 | A(k,[n+1 cols])=[-cols 0]; | ||
182 | end | ||
183 | |||
184 | |||
185 | function [A,C,U]=hminiass(A) | ||
186 | %HMINIASS Initial assignment of the Hungarian method. | ||
187 | % | ||
188 | %[B,C,U]=hminiass(A) | ||
189 | %A - the reduced cost matrix. | ||
190 | %B - the reduced cost matrix, with assigned zeros removed from lists. | ||
191 | %C - a vector. C(J)=I means row I is assigned to column J, | ||
192 | % i.e. there is an assigned zero in position I,J. | ||
193 | %U - a vector with a linked list of unassigned rows. | ||
194 | |||
195 | % v1.0 96-06-14. Niclas Borlin, niclas@cs.umu.se. | ||
196 | |||
197 | [n,np1]=size(A); | ||
198 | |||
199 | % Initalize return vectors. | ||
200 | C=zeros(1,n); | ||
201 | U=zeros(1,n+1); | ||
202 | |||
203 | % Initialize last/next zero "pointers". | ||
204 | LZ=zeros(1,n); | ||
205 | NZ=zeros(1,n); | ||
206 | |||
207 | for i=1:n | ||
208 | % Set j to first unassigned zero in row i. | ||
209 | lj=n+1; | ||
210 | j=-A(i,lj); | ||
211 | |||
212 | % Repeat until we have no more zeros (j==0) or we find a zero | ||
213 | % in an unassigned column (c(j)==0). | ||
214 | |||
215 | while (C(j)~=0) | ||
216 | % Advance lj and j in zero list. | ||
217 | lj=j; | ||
218 | j=-A(i,lj); | ||
219 | |||
220 | % Stop if we hit end of list. | ||
221 | if (j==0) | ||
222 | break; | ||
223 | end | ||
224 | end | ||
225 | |||
226 | if (j~=0) | ||
227 | % We found a zero in an unassigned column. | ||
228 | |||
229 | % Assign row i to column j. | ||
230 | C(j)=i; | ||
231 | |||
232 | % Remove A(i,j) from unassigned zero list. | ||
233 | A(i,lj)=A(i,j); | ||
234 | |||
235 | % Update next/last unassigned zero pointers. | ||
236 | NZ(i)=-A(i,j); | ||
237 | LZ(i)=lj; | ||
238 | |||
239 | % Indicate A(i,j) is an assigned zero. | ||
240 | A(i,j)=0; | ||
241 | else | ||
242 | % We found no zero in an unassigned column. | ||
243 | |||
244 | % Check all zeros in this row. | ||
245 | |||
246 | lj=n+1; | ||
247 | j=-A(i,lj); | ||
248 | |||
249 | % Check all zeros in this row for a suitable zero in another row. | ||
250 | while (j~=0) | ||
251 | % Check the in the row assigned to this column. | ||
252 | r=C(j); | ||
253 | |||
254 | % Pick up last/next pointers. | ||
255 | lm=LZ(r); | ||
256 | m=NZ(r); | ||
257 | |||
258 | % Check all unchecked zeros in free list of this row. | ||
259 | while (m~=0) | ||
260 | % Stop if we find an unassigned column. | ||
261 | if (C(m)==0) | ||
262 | break; | ||
263 | end | ||
264 | |||
265 | % Advance one step in list. | ||
266 | lm=m; | ||
267 | m=-A(r,lm); | ||
268 | end | ||
269 | |||
270 | if (m==0) | ||
271 | % We failed on row r. Continue with next zero on row i. | ||
272 | lj=j; | ||
273 | j=-A(i,lj); | ||
274 | else | ||
275 | % We found a zero in an unassigned column. | ||
276 | |||
277 | % Replace zero at (r,m) in unassigned list with zero at (r,j) | ||
278 | A(r,lm)=-j; | ||
279 | A(r,j)=A(r,m); | ||
280 | |||
281 | % Update last/next pointers in row r. | ||
282 | NZ(r)=-A(r,m); | ||
283 | LZ(r)=j; | ||
284 | |||
285 | % Mark A(r,m) as an assigned zero in the matrix . . . | ||
286 | A(r,m)=0; | ||
287 | |||
288 | % ...and in the assignment vector. | ||
289 | C(m)=r; | ||
290 | |||
291 | % Remove A(i,j) from unassigned list. | ||
292 | A(i,lj)=A(i,j); | ||
293 | |||
294 | % Update last/next pointers in row r. | ||
295 | NZ(i)=-A(i,j); | ||
296 | LZ(i)=lj; | ||
297 | |||
298 | % Mark A(r,m) as an assigned zero in the matrix . . . | ||
299 | A(i,j)=0; | ||
300 | |||
301 | % ...and in the assignment vector. | ||
302 | C(j)=i; | ||
303 | |||
304 | % Stop search. | ||
305 | break; | ||
306 | end | ||
307 | end | ||
308 | end | ||
309 | end | ||
310 | |||
311 | % Create vector with list of unassigned rows. | ||
312 | |||
313 | % Mark all rows have assignment. | ||
314 | r=zeros(1,n); | ||
315 | rows=C(C~=0); | ||
316 | r(rows)=rows; | ||
317 | empty=find(r==0); | ||
318 | |||
319 | % Create vector with linked list of unassigned rows. | ||
320 | U=zeros(1,n+1); | ||
321 | U([n+1 empty])=[empty 0]; | ||
322 | |||
323 | |||
324 | function [A,C,U]=hmflip(A,C,LC,LR,U,l,r) | ||
325 | %HMFLIP Flip assignment state of all zeros along a path. | ||
326 | % | ||
327 | %[A,C,U]=hmflip(A,C,LC,LR,U,l,r) | ||
328 | %Input: | ||
329 | %A - the cost matrix. | ||
330 | %C - the assignment vector. | ||
331 | %LC - the column label vector. | ||
332 | %LR - the row label vector. | ||
333 | %U - the | ||
334 | %r,l - position of last zero in path. | ||
335 | %Output: | ||
336 | %A - updated cost matrix. | ||
337 | %C - updated assignment vector. | ||
338 | %U - updated unassigned row list vector. | ||
339 | |||
340 | % v1.0 96-06-14. Niclas Borlin, niclas@cs.umu.se. | ||
341 | |||
342 | n=size(A,1); | ||
343 | |||
344 | while (1) | ||
345 | % Move assignment in column l to row r. | ||
346 | C(l)=r; | ||
347 | |||
348 | % Find zero to be removed from zero list.. | ||
349 | |||
350 | % Find zero before this. | ||
351 | m=find(A(r,:)==-l); | ||
352 | |||
353 | % Link past this zero. | ||
354 | A(r,m)=A(r,l); | ||
355 | |||
356 | A(r,l)=0; | ||
357 | |||
358 | % If this was the first zero of the path.. | ||
359 | if (LR(r)<0) | ||
360 | ...remove row from unassigned row list and return. | ||
361 | U(n+1)=U(r); | ||
362 | U(r)=0; | ||
363 | return; | ||
364 | else | ||
365 | |||
366 | % Move back in this row along the path and get column of next zero. | ||
367 | l=LR(r); | ||
368 | |||
369 | % Insert zero at (r,l) first in zero list. | ||
370 | A(r,l)=A(r,n+1); | ||
371 | A(r,n+1)=-l; | ||
372 | |||
373 | % Continue back along the column to get row of next zero in path. | ||
374 | r=LC(l); | ||
375 | end | ||
376 | end | ||
377 | |||
378 | |||
379 | function [A,CH,RH]=hmreduce(A,CH,RH,LC,LR,SLC,SLR) | ||
380 | %HMREDUCE Reduce parts of cost matrix in the Hungerian method. | ||
381 | % | ||
382 | %[A,CH,RH]=hmreduce(A,CH,RH,LC,LR,SLC,SLR) | ||
383 | %Input: | ||
384 | %A - Cost matrix. | ||
385 | %CH - vector of column of 'next zeros' in each row. | ||
386 | %RH - vector with list of unexplored rows. | ||
387 | %LC - column labels. | ||
388 | %RC - row labels. | ||
389 | %SLC - set of column labels. | ||
390 | %SLR - set of row labels. | ||
391 | % | ||
392 | %Output: | ||
393 | %A - Reduced cost matrix. | ||
394 | %CH - Updated vector of 'next zeros' in each row. | ||
395 | %RH - Updated vector of unexplored rows. | ||
396 | |||
397 | % v1.0 96-06-14. Niclas Borlin, niclas@cs.umu.se. | ||
398 | |||
399 | n=size(A,1); | ||
400 | |||
401 | % Find which rows are covered, i.e. unlabelled. | ||
402 | coveredRows=LR==0; | ||
403 | |||
404 | % Find which columns are covered, i.e. labelled. | ||
405 | coveredCols=LC~=0; | ||
406 | |||
407 | r=find(~coveredRows); | ||
408 | c=find(~coveredCols); | ||
409 | |||
410 | % Get minimum of uncovered elements. | ||
411 | m=min(min(A(r,c))); | ||
412 | |||
413 | % Subtract minimum from all uncovered elements. | ||
414 | A(r,c)=A(r,c)-m; | ||
415 | |||
416 | % Check all uncovered columns.. | ||
417 | for j=c | ||
418 | % ...and uncovered rows in path order.. | ||
419 | for i=SLR | ||
420 | % If this is a (new) zero.. | ||
421 | if (A(i,j)==0) | ||
422 | % If the row is not in unexplored list.. | ||
423 | if (RH(i)==0) | ||
424 | % ...insert it first in unexplored list. | ||
425 | RH(i)=RH(n+1); | ||
426 | RH(n+1)=i; | ||
427 | % Mark this zero as "next free" in this row. | ||
428 | CH(i)=j; | ||
429 | end | ||
430 | % Find last unassigned zero on row I. | ||
431 | row=A(i,:); | ||
432 | colsInList=-row(row<0); | ||
433 | if (length(colsInList)==0) | ||
434 | % No zeros in the list. | ||
435 | l=n+1; | ||
436 | else | ||
437 | l=colsInList(row(colsInList)==0); | ||
438 | end | ||
439 | % Append this zero to end of list. | ||
440 | A(i,l)=-j; | ||
441 | end | ||
442 | end | ||
443 | end | ||
444 | |||
445 | % Add minimum to all doubly covered elements. | ||
446 | r=find(coveredRows); | ||
447 | c=find(coveredCols); | ||
448 | |||
449 | % Take care of the zeros we will remove. | ||
450 | [i,j]=find(A(r,c)<=0); | ||
451 | |||
452 | i=r(i); | ||
453 | j=c(j); | ||
454 | |||
455 | for k=1:length(i) | ||
456 | % Find zero before this in this row. | ||
457 | lj=find(A(i(k),:)==-j(k)); | ||
458 | % Link past it. | ||
459 | A(i(k),lj)=A(i(k),j(k)); | ||
460 | % Mark it as assigned. | ||
461 | A(i(k),j(k))=0; | ||
462 | end | ||
463 | |||
464 | A(r,c)=A(r,c)+m; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/test.m b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/test.m new file mode 100755 index 0000000..4c238f2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/matching/pub/contrib/v5/optim/assignprob/test.m | |||
@@ -0,0 +1,87 @@ | |||
1 | disp('Testing hungarian...'); | ||
2 | A=magic(10); | ||
3 | B=A(4:7,4:7); | ||
4 | [c,t]=hungarian(B); | ||
5 | if (any(c~=[2 1 3 4])) | ||
6 | disp('Wrong coupling!'); | ||
7 | elseif (t~=77) | ||
8 | disp('Wrong solution!'); | ||
9 | else | ||
10 | disp('Hungarian appears OK.'); | ||
11 | end | ||
12 | |||
13 | disp('Testing condass...'); | ||
14 | [k,c1,t1,c2,t2]=condass(B); | ||
15 | if (any(c1~=[2 1 3 4])) | ||
16 | disp('Wrong best coupling!'); | ||
17 | elseif (t1~=77) | ||
18 | disp('Wrong lowest cost!'); | ||
19 | elseif (any(c2~=[1 2 3 4])) | ||
20 | disp('Wrong second best coupling!'); | ||
21 | elseif (t2~=102) | ||
22 | disp('Wrong second lowest cost!'); | ||
23 | elseif (k~=t1/(t2-t1)) | ||
24 | disp('Wrong condition number!'); | ||
25 | else | ||
26 | disp('condass appears OK.'); | ||
27 | end | ||
28 | |||
29 | disp('Testing allcosts...'); | ||
30 | [c,p]=allcosts(B); | ||
31 | cTrue=[ 102 | ||
32 | 127 | ||
33 | 202 | ||
34 | 227 | ||
35 | 222 | ||
36 | 222 | ||
37 | 77 | ||
38 | 102 | ||
39 | 182 | ||
40 | 202 | ||
41 | 202 | ||
42 | 197 | ||
43 | 177 | ||
44 | 202 | ||
45 | 182 | ||
46 | 202 | ||
47 | 302 | ||
48 | 297 | ||
49 | 202 | ||
50 | 202 | ||
51 | 207 | ||
52 | 202 | ||
53 | 307 | ||
54 | 302 | ||
55 | ]; | ||
56 | pTrue=[ 1 2 3 4 | ||
57 | 1 2 4 3 | ||
58 | 1 3 2 4 | ||
59 | 1 3 4 2 | ||
60 | 1 4 2 3 | ||
61 | 1 4 3 2 | ||
62 | 2 1 3 4 | ||
63 | 2 1 4 3 | ||
64 | 2 3 1 4 | ||
65 | 2 3 4 1 | ||
66 | 2 4 1 3 | ||
67 | 2 4 3 1 | ||
68 | 3 1 2 4 | ||
69 | 3 1 4 2 | ||
70 | 3 2 1 4 | ||
71 | 3 2 4 1 | ||
72 | 3 4 1 2 | ||
73 | 3 4 2 1 | ||
74 | 4 1 2 3 | ||
75 | 4 1 3 2 | ||
76 | 4 2 1 3 | ||
77 | 4 2 3 1 | ||
78 | 4 3 1 2 | ||
79 | 4 3 2 1 | ||
80 | ]; | ||
81 | if (any(c~=cTrue)) | ||
82 | disp('Wrong costs!'); | ||
83 | elseif (any(p~=pTrue)) | ||
84 | disp('Wrong permutations!'); | ||
85 | else | ||
86 | disp('allcosts appears OK.'); | ||
87 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/pyramid/091399fbn-jets.3.jpg b/SD-VBS/common/toolbox/toolbox_basic/pyramid/091399fbn-jets.3.jpg new file mode 100755 index 0000000..b91732f --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/pyramid/091399fbn-jets.3.jpg | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/pyramid/expand.m b/SD-VBS/common/toolbox/toolbox_basic/pyramid/expand.m new file mode 100755 index 0000000..64e9fda --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/pyramid/expand.m | |||
@@ -0,0 +1,8 @@ | |||
1 | function J = expand(I) | ||
2 | % | ||
3 | % | ||
4 | |||
5 | [sy,sx] = size(I); | ||
6 | [x,y] = meshgrid(1:2*sx+1,1:2*sy+1); | ||
7 | |||
8 | nx = \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/pyramid/gauss_lowpass.m b/SD-VBS/common/toolbox/toolbox_basic/pyramid/gauss_lowpass.m new file mode 100755 index 0000000..87ad4f1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/pyramid/gauss_lowpass.m | |||
@@ -0,0 +1,9 @@ | |||
1 | function J = gauss_lowpass(I,a) | ||
2 | |||
3 | if (nargin < 2), | ||
4 | a = 0.4; | ||
5 | end | ||
6 | |||
7 | w = gen_w(a); | ||
8 | |||
9 | J = conv2(conv2(I,w,'same'),w','same'); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/pyramid/gen_w.m b/SD-VBS/common/toolbox/toolbox_basic/pyramid/gen_w.m new file mode 100755 index 0000000..b255751 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/pyramid/gen_w.m | |||
@@ -0,0 +1,12 @@ | |||
1 | function w = gen_w(a) | ||
2 | % | ||
3 | |||
4 | if (nargin == 0), | ||
5 | a = 0.4; | ||
6 | end | ||
7 | |||
8 | w(3) = a; | ||
9 | w(1) = 1/4 - a/2; | ||
10 | w(5) = 1/4 - a/2; | ||
11 | w(2) = 1/4; | ||
12 | w(4) = 1/4; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/pyramid/reduce.m b/SD-VBS/common/toolbox/toolbox_basic/pyramid/reduce.m new file mode 100755 index 0000000..6837e8a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/pyramid/reduce.m | |||
@@ -0,0 +1,7 @@ | |||
1 | function J = reduce(I) | ||
2 | |||
3 | [nr,nc,nb] = size(I); | ||
4 | for j=1:nb, | ||
5 | tmp = gauss_lowpass(I(:,:,j)); | ||
6 | J(:,:,j) = tmp(1:2:nr,1:2:nc); | ||
7 | end \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/pyramid/session.m b/SD-VBS/common/toolbox/toolbox_basic/pyramid/session.m new file mode 100755 index 0000000..9d0aa6c --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/pyramid/session.m | |||
@@ -0,0 +1,26 @@ | |||
1 | %image_dir = '/home/barad-dur/d/malik/jshi/'; | ||
2 | %I = gifread([image_dir,'tape9/t9a1_L.40.gif']); | ||
3 | I = pgmread('car100x100_0001'); | ||
4 | Io = I; | ||
5 | |||
6 | B = []; | ||
7 | |||
8 | done = 0; | ||
9 | st = 2; | ||
10 | sz = size(I); | ||
11 | while (~done), | ||
12 | w = max(1,round(0.05*size(I,1))); | ||
13 | b = zeros(round(0.5*size(Io)),round(0.5*size(I))); | ||
14 | %J = smooth(I,w); | ||
15 | %I = J(1:st:size(J,1),1:st:size(J,2)); | ||
16 | I = reduce(I); | ||
17 | sz = [sz;size(I)]; | ||
18 | b(1:size(I,1),1:size(I,2)) = I; | ||
19 | disp(int2str(size(I,1))); | ||
20 | |||
21 | B = [B,b]; | ||
22 | |||
23 | if (size(I,1) < 8), | ||
24 | done = 1; | ||
25 | end | ||
26 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/pyramid/startup.m b/SD-VBS/common/toolbox/toolbox_basic/pyramid/startup.m new file mode 100755 index 0000000..f86f9f4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/pyramid/startup.m | |||
@@ -0,0 +1,5 @@ | |||
1 | home_dir = '/home/nef0/malik/jshi/jshi/matlab/'; | ||
2 | path([home_dir,'toolbox/io'], path) | ||
3 | path([home_dir,'toolbox/filter'],path) | ||
4 | path(path,[home_dir,'vision/vision94/tracking/']) | ||
5 | clear home_dir \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/remap_angle.m b/SD-VBS/common/toolbox/toolbox_basic/remap_angle.m new file mode 100755 index 0000000..09f8cff --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/remap_angle.m | |||
@@ -0,0 +1,4 @@ | |||
1 | function a = remap_angle(theta,min,max) | ||
2 | |||
3 | a = (theta<=min).*(theta+pi) + (theta>=max).*(theta-pi) +... | ||
4 | ((theta>min)&(theta<max)).*theta; \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/spmtimesd.c b/SD-VBS/common/toolbox/toolbox_basic/spmtimesd.c new file mode 100755 index 0000000..a98dc0a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/spmtimesd.c | |||
@@ -0,0 +1,141 @@ | |||
1 | /*================================================================ | ||
2 | * spmtimesd.c | ||
3 | * This routine computes a sparse matrix times a diagonal matrix | ||
4 | * whose diagonal entries are stored in a full vector. | ||
5 | * | ||
6 | * Examples: | ||
7 | * spmtimesd(m,d,[]) = diag(d) * m, | ||
8 | * spmtimesd(m,[],d) = m * diag(d) | ||
9 | * spmtimesd(m,d1,d2) = diag(d1) * m * diag(d2) | ||
10 | * m could be complex, but d is assumed real | ||
11 | * | ||
12 | * Stella X. Yu's first MEX function, Nov 9, 2001. | ||
13 | |||
14 | % test sequence: | ||
15 | |||
16 | m = 1000; | ||
17 | n = 2000; | ||
18 | a=sparse(rand(m,n)); | ||
19 | d1 = rand(m,1); | ||
20 | d2 = rand(n,1); | ||
21 | tic; b=spmtimesd(a,d1,d2); toc | ||
22 | tic; bb = spdiags(d1,0,m,m) * a * spdiags(d2,0,n,n); toc | ||
23 | e = (bb-b); | ||
24 | max(abs(e(:))) | ||
25 | |||
26 | *=================================================================*/ | ||
27 | |||
28 | # include "mex.h" | ||
29 | |||
30 | void mexFunction( | ||
31 | int nargout, | ||
32 | mxArray *out[], | ||
33 | int nargin, | ||
34 | const mxArray *in[] | ||
35 | ) | ||
36 | { | ||
37 | /* declare variables */ | ||
38 | int i, j, k, m, n, nzmax, cmplx, xm, yn; | ||
39 | int *pir, *pjc, *qir, *qjc; | ||
40 | double *x, *y, *pr, *pi, *qr, *qi; | ||
41 | |||
42 | /* check argument */ | ||
43 | if (nargin != 3) { | ||
44 | mexErrMsgTxt("Three input arguments required"); | ||
45 | } | ||
46 | if (nargout>1) { | ||
47 | mexErrMsgTxt("Too many output arguments."); | ||
48 | } | ||
49 | if (!(mxIsSparse(in[0]))) { | ||
50 | mexErrMsgTxt("Input argument #1 must be of type sparse"); | ||
51 | } | ||
52 | if ( mxIsSparse(in[1]) || mxIsSparse(in[2]) ) { | ||
53 | mexErrMsgTxt("Input argument #2 & #3 must be of type full"); | ||
54 | } | ||
55 | |||
56 | /* computation starts */ | ||
57 | m = mxGetM(in[0]); | ||
58 | n = mxGetN(in[0]); | ||
59 | pr = mxGetPr(in[0]); | ||
60 | pi = mxGetPi(in[0]); | ||
61 | pir = mxGetIr(in[0]); | ||
62 | pjc = mxGetJc(in[0]); | ||
63 | |||
64 | i = mxGetM(in[1]); | ||
65 | j = mxGetN(in[1]); | ||
66 | xm = ((i>j)? i: j); | ||
67 | |||
68 | i = mxGetM(in[2]); | ||
69 | j = mxGetN(in[2]); | ||
70 | yn = ((i>j)? i: j); | ||
71 | |||
72 | if ( xm>0 && xm != m) { | ||
73 | mexErrMsgTxt("Row multiplication dimension mismatch."); | ||
74 | } | ||
75 | if ( yn>0 && yn != n) { | ||
76 | mexErrMsgTxt("Column multiplication dimension mismatch."); | ||
77 | } | ||
78 | |||
79 | |||
80 | nzmax = mxGetNzmax(in[0]); | ||
81 | cmplx = (pi==NULL ? 0 : 1); | ||
82 | out[0] = mxCreateSparse(m,n,nzmax,cmplx); | ||
83 | if (out[0]==NULL) { | ||
84 | mexErrMsgTxt("Not enough space for the output matrix."); | ||
85 | } | ||
86 | |||
87 | qr = mxGetPr(out[0]); | ||
88 | qi = mxGetPi(out[0]); | ||
89 | qir = mxGetIr(out[0]); | ||
90 | qjc = mxGetJc(out[0]); | ||
91 | |||
92 | /* left multiplication */ | ||
93 | x = mxGetPr(in[1]); | ||
94 | if (yn==0) { | ||
95 | for (j=0; j<n; j++) { | ||
96 | qjc[j] = pjc[j]; | ||
97 | for (k=pjc[j]; k<pjc[j+1]; k++) { | ||
98 | i = pir[k]; | ||
99 | qir[k] = i; | ||
100 | qr[k] = x[i] * pr[k]; | ||
101 | if (cmplx) { | ||
102 | qi[k] = x[i] * pi[k]; | ||
103 | } | ||
104 | } | ||
105 | } | ||
106 | qjc[n] = k; | ||
107 | return; | ||
108 | } | ||
109 | |||
110 | /* right multiplication */ | ||
111 | y = mxGetPr(in[2]); | ||
112 | if (xm==0) { | ||
113 | for (j=0; j<n; j++) { | ||
114 | qjc[j] = pjc[j]; | ||
115 | for (k=pjc[j]; k<pjc[j+1]; k++) { | ||
116 | qir[k] = pir[k]; | ||
117 | qr[k] = pr[k] * y[j]; | ||
118 | if (cmplx) { | ||
119 | qi[k] = qi[k] * y[j]; | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | qjc[n] = k; | ||
124 | return; | ||
125 | } | ||
126 | |||
127 | /* both sides */ | ||
128 | for (j=0; j<n; j++) { | ||
129 | qjc[j] = pjc[j]; | ||
130 | for (k=pjc[j]; k<pjc[j+1]; k++) { | ||
131 | i = pir[k]; | ||
132 | qir[k]= i; | ||
133 | qr[k] = x[i] * pr[k] * y[j]; | ||
134 | if (cmplx) { | ||
135 | qi[k] = x[i] * qi[k] * y[j]; | ||
136 | } | ||
137 | } | ||
138 | qjc[n] = k; | ||
139 | } | ||
140 | |||
141 | } | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/afromncut.m b/SD-VBS/common/toolbox/toolbox_basic/stella/afromncut.m new file mode 100755 index 0000000..ec014d0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/afromncut.m | |||
@@ -0,0 +1,73 @@ | |||
1 | % function a = afromncut(v,s,d,visimg,no_rep,pixel_loc) | ||
2 | % Input: | ||
3 | % v = eigenvectors of d*a*d, starting from the second. | ||
4 | % (the first is all one over some constant determined by d) | ||
5 | % s = eigenvalues | ||
6 | % d = normalization matrix 1/sqrt(rowsum(abs(a))) | ||
7 | % visimg = 1/0 if each eigenvector is/not 2D (so v is 3D) | ||
8 | % no_rep = 1 (default), affinity has attraction only | ||
9 | % if 1, the first column of v is the second eigenvector | ||
10 | % if 0, the first column of v is the first eigenvector. | ||
11 | % pixel_loc = nx1 matrix, each is a pixel location | ||
12 | % Output: | ||
13 | % a = diag(1/d) * na * diag(1/d); | ||
14 | % If pixel_loc = []; a is returned, if not out of memory | ||
15 | % otherwise, only rows of a at pixel_loc are returned. | ||
16 | % | ||
17 | % This routine is used to estimate the original affinity matrix | ||
18 | % through the first few eigenvectors and its normalization matrix. | ||
19 | |||
20 | % A test sequence includes: | ||
21 | % a = randsym(5); | ||
22 | % [na,d] = normalize(a); | ||
23 | % [v,s] = ncut(a,5); | ||
24 | % v = v(:,2:end); s = s(2:end); | ||
25 | % aa = afromncut(v,s,d); | ||
26 | % max(abs(aa(:) - a(:))) | ||
27 | |||
28 | % Stella X. Yu, 2000. | ||
29 | |||
30 | function a = afromncut(v,s,d,visimg,no_rep,pixel_loc) | ||
31 | |||
32 | [nr,nc,nv] = size(v); | ||
33 | if nargin<4 | isempty(visimg), | ||
34 | visimg = (nv>1); | ||
35 | end | ||
36 | |||
37 | if nargin<5 | isempty(no_rep), | ||
38 | no_rep = 1; | ||
39 | end | ||
40 | |||
41 | if visimg, | ||
42 | nr = nr * nc; | ||
43 | else | ||
44 | nv = nc; | ||
45 | end | ||
46 | |||
47 | if nargin<6 | isempty(pixel_loc), | ||
48 | pixel_loc = 1:nr; | ||
49 | end | ||
50 | |||
51 | % D^(1/2) | ||
52 | d = 1./(d(:)+eps); | ||
53 | |||
54 | % first recover the first eigenvector | ||
55 | if no_rep, | ||
56 | u = (1/norm(d)) + zeros(nr,1); | ||
57 | s = [1;s(:)]; | ||
58 | nv = nv + 1; | ||
59 | else | ||
60 | u = []; | ||
61 | end | ||
62 | |||
63 | % the full set of generalized eigenvectors | ||
64 | v = [u, reshape(v,[nr,nv-no_rep])]; | ||
65 | |||
66 | % This is the real D, row sum | ||
67 | d = d.^2; | ||
68 | |||
69 | % an equivalent way to compute v = diag(d) * v; | ||
70 | v = v .* d(:,ones(nv,1)); % to avoid using a big matrix diag(d) | ||
71 | |||
72 | % synthesis | ||
73 | a = v(pixel_loc,:)*diag(s)*v'; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/dispimg.m b/SD-VBS/common/toolbox/toolbox_basic/stella/dispimg.m new file mode 100755 index 0000000..4e419a0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/dispimg.m | |||
@@ -0,0 +1,65 @@ | |||
1 | % function dispimg(g,fmt,lgd,cmap) display multiple images in one figure. | ||
2 | % Input: | ||
3 | % g = a cell and fmt is a 1x2 vector specifying the layout. | ||
4 | % lgd = a string cell for the title of each image. | ||
5 | % cmap = the colormap (default is the gray, -1 for the inverted gray). | ||
6 | % ishori = a vector of 1/0 to display real and imag parts horizontally / vertically | ||
7 | |||
8 | % Stella X. Yu, 2000. | ||
9 | |||
10 | function dispimg(g,fmt,lgd,cmap,ishori); | ||
11 | |||
12 | cellg = iscell(g); | ||
13 | if cellg, | ||
14 | num_fig = length(g); | ||
15 | else | ||
16 | num_fig = size(g,3); | ||
17 | end; | ||
18 | |||
19 | if nargin<2 | isempty(fmt), | ||
20 | m = ceil(sqrt(num_fig)); | ||
21 | n = ceil(num_fig / m); | ||
22 | else | ||
23 | m = fmt(1); | ||
24 | n = fmt(2); | ||
25 | end | ||
26 | |||
27 | if nargin<3 | isempty(lgd), | ||
28 | lgd = 1:num_fig; | ||
29 | end | ||
30 | if isnumeric(lgd), | ||
31 | lgd = cellstr(num2str(lgd(:),3)); | ||
32 | end | ||
33 | i = size(lgd); | ||
34 | if i(1)==1, | ||
35 | lgd = [lgd, cell(1,num_fig-i(2))]; | ||
36 | else | ||
37 | lgd = [lgd; cell(num_fig-i(1),1)]; | ||
38 | end | ||
39 | |||
40 | if nargin<5 | isempty(ishori), | ||
41 | ishori = ones(num_fig,1); | ||
42 | end | ||
43 | ishori(end+1:num_fig) = ishori(end); | ||
44 | |||
45 | for k=1:num_fig, | ||
46 | subplot(m,n,k); | ||
47 | if cellg, | ||
48 | showim(g{k},[],ishori(k)); | ||
49 | else | ||
50 | showim(g(:,:,k),[],ishori(k)); | ||
51 | end | ||
52 | title(lgd{k}); | ||
53 | end | ||
54 | |||
55 | if nargin<4 | isempty(cmap), | ||
56 | cmap = gray; | ||
57 | end | ||
58 | if length(cmap)==1, | ||
59 | if cmap==1, | ||
60 | cmap = gray; | ||
61 | else | ||
62 | cmap = flipud(gray); | ||
63 | end | ||
64 | end | ||
65 | colormap(cmap); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/firstncut.m b/SD-VBS/common/toolbox/toolbox_basic/stella/firstncut.m new file mode 100755 index 0000000..a22077d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/firstncut.m | |||
@@ -0,0 +1,67 @@ | |||
1 | % function [v,s,d] = firstncut(base_name,rec_num) | ||
2 | % Input: | ||
3 | % base_name = image name | ||
4 | % rec_num = parameter record number | ||
5 | % Output: | ||
6 | % v = eigenvectors | ||
7 | % s = eigenvalues | ||
8 | % d = normalization matrix d = 1/sqrt(rowsum(abs(a))) | ||
9 | % Convert Jianbo Shi's Ncut Ccode results from images to matlab matrices. | ||
10 | |||
11 | % Stella X. Yu, 2000. | ||
12 | |||
13 | function [v,s,d] = firstncut(base_name,rec_num); | ||
14 | |||
15 | if nargin<2 | isempty(rec_num), | ||
16 | rec_num = 1; | ||
17 | end | ||
18 | |||
19 | cur_dir = pwd; | ||
20 | globalenvar; | ||
21 | cd(IMAGE_DIR); | ||
22 | cd(base_name); | ||
23 | feval([base_name,'_par']); | ||
24 | j = length(p); | ||
25 | if rec_num>j, | ||
26 | disp(sprintf('parameter record number %d out of range %d, check %s!',rec_num,j,[base_name,'_par.m'])); | ||
27 | Qlabel = []; | ||
28 | v = []; | ||
29 | s = []; | ||
30 | ev_info = []; | ||
31 | return; | ||
32 | end | ||
33 | nv = p(rec_num).num_eigvecs; | ||
34 | no_rep = (p(rec_num).offset<1e-6); | ||
35 | |||
36 | % read the image | ||
37 | cm=sprintf('I = readppm(''%s.ppm'');',base_name); | ||
38 | eval(cm); | ||
39 | |||
40 | % read eigenvectors | ||
41 | base_name_hist = sprintf('%s_%d_IC',base_name,rec_num); | ||
42 | if no_rep, | ||
43 | [v,ev_info] = read_ev_pgm(base_name_hist,1,1,nv); | ||
44 | else | ||
45 | [v,ev_info] = read_ev_pgm2(base_name_hist,1,1,nv); | ||
46 | end | ||
47 | s = ev_info(4,:)'; | ||
48 | |||
49 | % read the normalization matrix | ||
50 | d = readpfmc(sprintf('%s_%d_D_IC.pfm',base_name,rec_num)); | ||
51 | cd(cur_dir); | ||
52 | |||
53 | % D^(1/2) | ||
54 | dd = (1./(d(:)+eps)); | ||
55 | |||
56 | % recover real eigenvectors | ||
57 | for j = 1:nv-no_rep, | ||
58 | vmin = ev_info(1,j); | ||
59 | vmax = ev_info(2,j); | ||
60 | y = v(:,:,j).*((vmax - vmin)/256) + vmin; | ||
61 | %validity check: x = D^(1/2)y should be normalized | ||
62 | x = norm(y(:).*dd); | ||
63 | v(:,:,j) = y./x; | ||
64 | end | ||
65 | |||
66 | dispimg(cat(3,mean(I,3),v),[],[{'image'};cellstr(num2str(s,3))]); | ||
67 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/getfnames.m b/SD-VBS/common/toolbox/toolbox_basic/stella/getfnames.m new file mode 100755 index 0000000..4990451 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/getfnames.m | |||
@@ -0,0 +1,47 @@ | |||
1 | % function [fn,dn] = getfnames(direc,opt) | ||
2 | % Input: | ||
3 | % direc = directory | ||
4 | % opt = wildcat | ||
5 | % Output: | ||
6 | % fn = a cell with all filenames under direc and with opt | ||
7 | % dn = a cell with all directory names under direc and with opt | ||
8 | % For example, getfnames('19990910','*.jpg'); | ||
9 | % Set IS_PC according to your platform in globalenvar.m | ||
10 | |||
11 | % Stella X. Yu, 2000. | ||
12 | |||
13 | function [fn,dn] = getfnames(direc,opt) | ||
14 | |||
15 | if (nargin<1 | isempty(direc)), | ||
16 | direc = '.'; | ||
17 | end | ||
18 | |||
19 | if nargin<2 | isempty(opt), | ||
20 | opt = []; | ||
21 | end | ||
22 | |||
23 | fn = {}; | ||
24 | dn = {}; | ||
25 | |||
26 | cur_dir = pwd; | ||
27 | cd(direc); | ||
28 | s = dir(opt); | ||
29 | if isempty(s), | ||
30 | disp('getfnames: no data'); | ||
31 | return; | ||
32 | end | ||
33 | |||
34 | n = length(s); | ||
35 | i = 1; | ||
36 | j = 1; | ||
37 | for k=1:n, | ||
38 | if s(k).isdir, | ||
39 | dn{j,1} = s(k).name; | ||
40 | j = j + 1; | ||
41 | else | ||
42 | fn{i,1} = s(k).name; | ||
43 | i = i + 1; | ||
44 | end | ||
45 | end | ||
46 | cd(cur_dir) | ||
47 | %[fn{1:n,1}]=deal(s.name); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/getimage2.m b/SD-VBS/common/toolbox/toolbox_basic/stella/getimage2.m new file mode 100755 index 0000000..945ddd2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/getimage2.m | |||
@@ -0,0 +1,46 @@ | |||
1 | % function f = getimage2(imagefile) returns a normalized intensity image. | ||
2 | % If the file postfix is not given, then I will search any possible image file | ||
3 | % under the IMAGE_DIR. | ||
4 | |||
5 | % Stella X. Yu, March 1999 | ||
6 | |||
7 | function f = getimage2(imagefile) | ||
8 | |||
9 | if exist(imagefile)==2, | ||
10 | g = {imagefile}; | ||
11 | else | ||
12 | g = {}; | ||
13 | end | ||
14 | globalenvar; | ||
15 | g = [g; getfnames(IMAGE_DIR,[imagefile,'.*'])]; | ||
16 | |||
17 | j = 1; | ||
18 | for i=1:length(g), | ||
19 | k = findstr(g{i},'.'); | ||
20 | gp = g{i}(k(end)+1:end); | ||
21 | if strcmp(gp,'ppm'), | ||
22 | f = double(readppm(g{i})); | ||
23 | j = 0; | ||
24 | elseif sum(strcmp(gp,{'jpg','tif','jpeg','tiff','bmp','png','hdf','pcx','xwd'}))>0, | ||
25 | f = double(imread(g{i})); | ||
26 | j = 0; | ||
27 | end | ||
28 | if j==0, | ||
29 | disp(sprintf('This is an image read from %s under %s',g{i},IMAGE_DIR)); | ||
30 | break; | ||
31 | end | ||
32 | end | ||
33 | if j, | ||
34 | f = []; | ||
35 | disp('Image not found'); | ||
36 | return; | ||
37 | end | ||
38 | |||
39 | if size(f,3)>1, | ||
40 | %f = sum(f,3)./3; | ||
41 | f = rgb2ntsc(f); | ||
42 | f = f(:,:,1); | ||
43 | end | ||
44 | minf = min(f(:)); | ||
45 | maxf = max(f(:)); | ||
46 | f = (f - minf) ./ (maxf - minf); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/globalenvar.m b/SD-VBS/common/toolbox/toolbox_basic/stella/globalenvar.m new file mode 100755 index 0000000..1e61b61 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/globalenvar.m | |||
@@ -0,0 +1,6 @@ | |||
1 | % globalenvar | ||
2 | |||
3 | HOME_DIR = '/hid/jshi/Research/walking/stella'; | ||
4 | IMAGE_DIR = '/hid/jshi/test_images'; | ||
5 | C_DIR = '/hid/jshi/Research/Ncut_code_C'; | ||
6 | IS_PC = 0; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/jshincut.m b/SD-VBS/common/toolbox/toolbox_basic/stella/jshincut.m new file mode 100755 index 0000000..d0f11cb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/jshincut.m | |||
@@ -0,0 +1,94 @@ | |||
1 | % function [par, rec_num] = jshincut(par,image_dir) | ||
2 | % Input: | ||
3 | % par = a structure with parameters for command_ncut.tex | ||
4 | % image_dir = the directory where the imagefile is stored | ||
5 | % Output: | ||
6 | % par = parameters used | ||
7 | % rec_num = record number in the NCut database | ||
8 | % Jianbo Shi's ncut_IC is applied to the image | ||
9 | % (If there is no .ppm format for the named image, | ||
10 | % conversion from related files would be attempted.) | ||
11 | % Results are organized according to the parameters. | ||
12 | % Example: jshincut('240018s'); | ||
13 | % See also: jshincutdefpar; ncutcheckin | ||
14 | % Set IS_PC according to your platform in globalenvar.m | ||
15 | |||
16 | % Stella X. Yu, 2000. | ||
17 | |||
18 | function [par,rec_num] = jshincut(par,image_dir) | ||
19 | |||
20 | rec = jshincutdefpar; | ||
21 | |||
22 | fields = fieldnames(rec); | ||
23 | nf = length(fields); | ||
24 | |||
25 | if ischar(par), | ||
26 | imagename = par; | ||
27 | par = rec; | ||
28 | par.fname_base = imagename; | ||
29 | end | ||
30 | |||
31 | globalenvar; | ||
32 | |||
33 | if nargin<2 | isempty(image_dir), | ||
34 | image_dir = IMAGE_DIR; | ||
35 | end | ||
36 | |||
37 | imagename = getfield(par,fields{1}); | ||
38 | for i=2:nf, | ||
39 | t = getfield(par,fields{i}); | ||
40 | if isempty(t), | ||
41 | par = setfield(par,fields{i},getfield(rec,fields{i})); | ||
42 | end | ||
43 | end | ||
44 | |||
45 | % dir and filename | ||
46 | catchar = {'/','\'}; | ||
47 | catchar = catchar{IS_PC+1}; | ||
48 | |||
49 | % first check if there is a ppm file for this image | ||
50 | if not(exist([image_dir,catchar,imagename,'.ppm'])), | ||
51 | j = getfnames(image_dir,[imagename,'.*']); | ||
52 | if isempty(j), | ||
53 | disp('Image not found.'); | ||
54 | return; | ||
55 | end | ||
56 | k = 0; | ||
57 | for i=1:length(j), | ||
58 | k = k + not(isempty(im2ppm(j{i},image_dir))); | ||
59 | if k==1, | ||
60 | disp(sprintf('%s -> %s.ppm succeeded.',j{i},imagename)); | ||
61 | break; | ||
62 | end | ||
63 | end | ||
64 | if k==0, | ||
65 | disp('Sorry. Attempt to convert your named image into ppm format failed.'); | ||
66 | return; | ||
67 | end | ||
68 | end | ||
69 | |||
70 | cd(C_DIR); | ||
71 | |||
72 | % generate command_ncut.tex file | ||
73 | fn = 'command_ncut.tex'; | ||
74 | fid = fopen(fn,'w'); | ||
75 | fprintf(fid,'%21s\t%s%c%s\n',fields{1},image_dir,catchar,imagename); | ||
76 | for i=2:nf, | ||
77 | t = getfield(par,fields{i}); | ||
78 | if isnumeric(t), | ||
79 | t = num2str(t); | ||
80 | end | ||
81 | fprintf(fid,['%21s\t%s\n'],fields{i},t); | ||
82 | end | ||
83 | fclose(fid); | ||
84 | %disp('You can check and modify command_ncut.tex before I run ncut_IC on it. Good?');pause(1); | ||
85 | |||
86 | % run ncut_IC | ||
87 | unix(['.',catchar,'ncut_IC']); | ||
88 | cd(HOME_DIR); | ||
89 | |||
90 | % check in | ||
91 | copyfile([C_DIR,catchar,fn],[image_dir,catchar,fn]); | ||
92 | rec_num = ncutcheckin(fn,image_dir,image_dir); | ||
93 | %delete([image_dir,catchar,imagename,'.ppm']); | ||
94 | %delete([image_dir,catchar,fn]); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/jshincutdefpar.m b/SD-VBS/common/toolbox/toolbox_basic/stella/jshincutdefpar.m new file mode 100755 index 0000000..4f07192 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/jshincutdefpar.m | |||
@@ -0,0 +1,20 @@ | |||
1 | % function rec = jshincutdefpar; | ||
2 | % default parameter setting for Jianbo Shi's NCut C codes | ||
3 | |||
4 | % Stella X. Yu, 2000. | ||
5 | |||
6 | function rec = jshincutdefpar; | ||
7 | |||
8 | rec.fname_base = '240018s'; | ||
9 | rec.fname_ext = 'ppm'; | ||
10 | rec.num_eigvecs = 15; | ||
11 | rec.spatial_neighborhood_x=20; | ||
12 | rec.sigma_x= 10; | ||
13 | rec.sig_I= -0.16; | ||
14 | rec.sig_IC= 0.01; | ||
15 | rec.hr= 2; | ||
16 | rec.eig_blk_sze= 3; | ||
17 | rec.power_D= 1; | ||
18 | rec.offset = 0.0; | ||
19 | rec.sig_filter = 1.0; | ||
20 | rec.elong_filter = 3.0; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/ncutcheckin.m b/SD-VBS/common/toolbox/toolbox_basic/stella/ncutcheckin.m new file mode 100755 index 0000000..cd82ee5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/ncutcheckin.m | |||
@@ -0,0 +1,136 @@ | |||
1 | % function rec_num = ncutcheckin(fn,sdir,tdir) | ||
2 | % Input: | ||
3 | % fn = parameter file name, default = 'command_ncut.tex' | ||
4 | % sdir = source dir for fn as well as data files | ||
5 | % tdir = target dir to check in, both default = IMAGE_DIR | ||
6 | % Output: | ||
7 | % rec_num = the number of current parameter records | ||
8 | % The imagefile_par.m is updated if fn contains a new | ||
9 | % parameter set. Data files are tagged and copied over to | ||
10 | % a subdir under tdir. | ||
11 | % Example: ncutcheckin; | ||
12 | % Set IS_PC, IMAGE_DIR according to your platform in globalenvar.m | ||
13 | |||
14 | % Stella X. Yu, 2000. | ||
15 | |||
16 | function rec_num = ncutcheckin(fn,sdir,tdir) | ||
17 | |||
18 | globalenvar; | ||
19 | |||
20 | cur_dir = pwd; | ||
21 | |||
22 | if nargin<1 | isempty(fn), | ||
23 | fn = 'command_ncut.tex'; | ||
24 | end | ||
25 | |||
26 | if nargin<2 | isempty(sdir), | ||
27 | sdir = IMAGE_DIR; | ||
28 | end | ||
29 | |||
30 | if nargin<3 | isempty(tdir), | ||
31 | tdir = IMAGE_DIR; | ||
32 | end | ||
33 | |||
34 | rec = jshincutdefpar; | ||
35 | |||
36 | % first, generate a parameter record from fn | ||
37 | cd(sdir); | ||
38 | [names,values] = textread(fn,'%s %s','commentstyle','shell'); | ||
39 | n = length(names); | ||
40 | s = rec; | ||
41 | for i=1:n, | ||
42 | j = str2num(values{i}); | ||
43 | if isempty(j), | ||
44 | s = setfield(s,names{i},values{i}); | ||
45 | else | ||
46 | s = setfield(s,names{i},j); | ||
47 | end | ||
48 | end | ||
49 | cd(cur_dir); | ||
50 | |||
51 | % special care to extract the image file name | ||
52 | imagename = getfield(s,names{1}); | ||
53 | catchar = {'/','\'}; | ||
54 | catchar = catchar{IS_PC + 1}; | ||
55 | k = max([0,findstr(imagename,catchar)]); | ||
56 | imagename = imagename(k+1:end); | ||
57 | s = setfield(s,names{1},imagename); | ||
58 | |||
59 | % second, check if the target dir contains a profile for the image | ||
60 | cd(tdir); | ||
61 | if not(exist(imagename,'dir')), | ||
62 | mkdir(imagename); | ||
63 | cd(cur_dir); | ||
64 | j = [catchar,imagename,'.',getfield(s,names{2})]; | ||
65 | copyfile([sdir,j],[tdir,catchar,imagename,j]); | ||
66 | cd(tdir); | ||
67 | end | ||
68 | cd(imagename); | ||
69 | j = [imagename,'_par']; | ||
70 | if not(exist(j)), | ||
71 | rec_num = 1; | ||
72 | p = s; | ||
73 | else | ||
74 | % load par file | ||
75 | feval(j); | ||
76 | rec_num = length(p); | ||
77 | i = 1; | ||
78 | while (i<=rec_num), | ||
79 | k = 0; | ||
80 | for j=1:n, | ||
81 | k = k + sum(getfield(s,names{j})-getfield(p(i),names{j})); | ||
82 | end | ||
83 | if k==0, | ||
84 | if not(isempty(input(['Data already existed as record # ',num2str(i),... | ||
85 | '\nPress any non-return key to Overwrite'],'s'))), | ||
86 | break; | ||
87 | else | ||
88 | rec_num = i; % have checked in already, no update | ||
89 | cd(cur_dir); | ||
90 | return; | ||
91 | end | ||
92 | else | ||
93 | i = i + 1; | ||
94 | end | ||
95 | end | ||
96 | rec_num = i; % new parameter setting | ||
97 | p(rec_num)=s; | ||
98 | end | ||
99 | tdir = [tdir,catchar,imagename]; | ||
100 | cd(cur_dir); | ||
101 | |||
102 | % third, check in data files | ||
103 | % leave .ppm and _edgecon, _phase files | ||
104 | % if not(exist([tdir,catchar,imagename,'.ppm'])), | ||
105 | % copyfile([sdir,catchar,imagename,'.ppm'],[tdir,catchar,imagename,'.ppm']); | ||
106 | % end | ||
107 | |||
108 | % IC files only | ||
109 | dn = getfnames(sdir,[imagename,'*_IC*.*']); | ||
110 | header = sprintf('%s%c%s_%d_',tdir,catchar,imagename,rec_num); | ||
111 | j = length(imagename)+2; | ||
112 | k = length(dn); | ||
113 | for i=1:k, | ||
114 | copyfile([sdir,catchar,dn{i}],[header,dn{i}(j:end)]); | ||
115 | delete([sdir,catchar,dn{i}]); | ||
116 | end | ||
117 | disp(sprintf('%d files checked in as record #%d',k,rec_num)); | ||
118 | |||
119 | |||
120 | % finally, update parameter file | ||
121 | cd(tdir); | ||
122 | fid = fopen([imagename,'_par.m'],'w'); | ||
123 | fprintf(fid,'%% Last checked in at %s\n\n',datestr(now)); | ||
124 | for i=1:rec_num, | ||
125 | for j=1:n, | ||
126 | k = getfield(p(i),names{j}); | ||
127 | if ischar(k), | ||
128 | fprintf(fid,'p(%d).%s=\''%s\'';\n',i,names{j},k); | ||
129 | else | ||
130 | fprintf(fid,'p(%d).%s=%s;\n',i,names{j},num2str(k)); | ||
131 | end | ||
132 | end | ||
133 | fprintf(fid,'\n'); | ||
134 | end | ||
135 | fclose(fid); | ||
136 | cd(cur_dir); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/openfigure.m b/SD-VBS/common/toolbox/toolbox_basic/stella/openfigure.m new file mode 100755 index 0000000..e677014 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/openfigure.m | |||
@@ -0,0 +1,52 @@ | |||
1 | % function openfigure(m,n,caption,isnew) | ||
2 | function h = openfigure(m,n,caption,isnew) | ||
3 | |||
4 | if nargin<3, | ||
5 | caption = ' '; | ||
6 | end | ||
7 | |||
8 | if nargin<4, | ||
9 | isnew = 1; | ||
10 | end | ||
11 | |||
12 | if (m<=0 | n<=0) | ||
13 | return; | ||
14 | end | ||
15 | |||
16 | if isnew, | ||
17 | h = figure; colormap(gray); | ||
18 | else | ||
19 | h = gcf; | ||
20 | end | ||
21 | clf | ||
22 | |||
23 | subplot('position',[0,0,0.1,0.1]); axis off; | ||
24 | text(0.1,0.15,sprintf('S. X. Yu, %s',date),'FontSize',8); | ||
25 | |||
26 | subplot('position',[0,0.96,0.1,0.1]); axis off; | ||
27 | text(0.1,0.15,caption,'FontSize',8); | ||
28 | |||
29 | subplot(m,n,1); | ||
30 | %return | ||
31 | |||
32 | if (m==1 & n==1), | ||
33 | return; | ||
34 | end | ||
35 | |||
36 | %set(gcf,'PaperPosition',[0.25, 8, 8,2.5*m]); | ||
37 | % set(gcf,'PaperPosition',[0.25,0.25,8,10.5]); | ||
38 | %return | ||
39 | |||
40 | if (m<=n), | ||
41 | set(gcf,'PaperOrientation','landscape','PaperPosition',[0.25,0.25,10.5,8]); | ||
42 | else | ||
43 | set(gcf,'PaperPosition',[0.25,0.25,8,10.5]); | ||
44 | end | ||
45 | |||
46 | % comment on PaperPosition | ||
47 | % [a,b,c,d] | ||
48 | % (a,b) is the coordinate of the lower-left corner of the figure | ||
49 | % (a,b) = (0,0) is the lower-left corner of the paper | ||
50 | % (c,d) is the coordinate of the upper-right corner of the figure relative to the lower-left corner of the figure | ||
51 | % Therefore, c>=a, d>=b | ||
52 | % Full paper position would be [0,0,8.5,11] in inches | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/showim.m b/SD-VBS/common/toolbox/toolbox_basic/stella/showim.m new file mode 100755 index 0000000..10db297 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/showim.m | |||
@@ -0,0 +1,36 @@ | |||
1 | % function showim(f,cmap) display real or complex image. | ||
2 | % When it is complex, the real part and imaginary part | ||
3 | % are displayed as [real,imag] in one image. | ||
4 | % cmap is the colormap. default = gray, -1 = inverted gray. | ||
5 | |||
6 | % Stella X. Yu, 2000. | ||
7 | |||
8 | function showim(f,cmap,ishori) | ||
9 | |||
10 | if not(isreal(f)), | ||
11 | i = [real(f(:)); imag(f(:))]; | ||
12 | j = [min(i), max(i)]; | ||
13 | [nr,nc] = size(f); | ||
14 | if nargin<3 | isempty(ishori), | ||
15 | ishori = nr>nc; | ||
16 | end | ||
17 | if ishori, | ||
18 | i = zeros(nr,1); | ||
19 | f = [real(f), [i+j(1),i+j(2)], imag(f)]; | ||
20 | else | ||
21 | i = zeros(1,nc); | ||
22 | f = [real(f); [i+j(1);i+j(2)]; imag(f)]; | ||
23 | end | ||
24 | end | ||
25 | imagesc(f); axis off; axis image; | ||
26 | |||
27 | if nargin<2 | isempty(cmap), | ||
28 | return; | ||
29 | end | ||
30 | |||
31 | if cmap==1, | ||
32 | cmap = gray; | ||
33 | elseif cmap==-1, | ||
34 | cmap = flipud(gray); | ||
35 | end | ||
36 | colormap(cmap); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/showncut.m b/SD-VBS/common/toolbox/toolbox_basic/stella/showncut.m new file mode 100755 index 0000000..b1fe1f4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/showncut.m | |||
@@ -0,0 +1,92 @@ | |||
1 | % function [g,lgd,v,s,dd] = showncut(fn,rec_num) | ||
2 | % Input: | ||
3 | % fn = file / image name | ||
4 | % rec_num = Ncut record number | ||
5 | % Output: | ||
6 | % g = a cell contains 1D, 2D and 3D embeddings | ||
7 | % lgd = legend for g | ||
8 | % v = eigenvectors | ||
9 | % s = eigenvalues | ||
10 | % dd = normalization matrix = 1/sqrt(rowsum(abs(a))) | ||
11 | % an image is displayed | ||
12 | |||
13 | function [g,lgd,v,s,dd] = showncut(fn,rec_num) | ||
14 | |||
15 | globalenvar; cd(IMAGE_DIR);cd(fn); feval([fn,'_par']);cd(HOME_DIR); | ||
16 | par = p(rec_num); | ||
17 | no_rep = (par.offset<1e-6); | ||
18 | |||
19 | [v,s,dd] = firstncut(fn,rec_num); | ||
20 | [m,n,nc] = size(v); | ||
21 | |||
22 | % generate images for display | ||
23 | nr = 5; | ||
24 | num_plots = nc * nr; | ||
25 | g = cell(num_plots,1); | ||
26 | lgd = g; | ||
27 | names = {'r','\theta','\phi'}; | ||
28 | x = cell(3,1); | ||
29 | for j=1:nc, | ||
30 | g{j} = v(:,:,j); | ||
31 | lgd{j} = sprintf('%s_{%d} = %1.2f','\lambda', j+no_rep, s(j)); | ||
32 | |||
33 | if j<nc, | ||
34 | [x{2},x{1}] = cart2pol(v(:,:,j),v(:,:,j+1)); | ||
35 | k = j; | ||
36 | for t=1:2, | ||
37 | k = k + nc; | ||
38 | g{k} = x{t}; | ||
39 | lgd{k} = sprintf('%s_{%d,%d}',names{t},j+[0:1]+no_rep); | ||
40 | end | ||
41 | |||
42 | if j<nc-1, | ||
43 | [x{2},x{3},x{1}] = cart2sph(v(:,:,j),v(:,:,j+1),v(:,:,j+2)); | ||
44 | for t=[1,3], % theta must be the same as 2D embedding, so ignore it | ||
45 | k = k + nc; | ||
46 | g{k} = x{t}; | ||
47 | lgd{k} = sprintf('%s_{%d,%d,%d}',names{t},j+[0:2]+no_rep); | ||
48 | end | ||
49 | end | ||
50 | end | ||
51 | end | ||
52 | |||
53 | % fill in slots by image f and affinity pattern | ||
54 | j = nc + nc; g{j} = getimage2(fn); lgd{j} = sprintf('%d x %d image',m,n); | ||
55 | j = nr * nc; g{j} = readpcm([fn,'_phase.pfm']); lgd{j} = 'phase'; | ||
56 | j = j - 1; g{j} = exp(-(readpfmc([fn,'_edgecon.pfm'])/(255*par.sig_IC)).^2); lgd{j} = 'IC'; | ||
57 | |||
58 | i = round(m*[1;3]./4); | ||
59 | %i = i([1,1,2,2]); | ||
60 | j = round(n*[1;3]./4); | ||
61 | %j = j([1,2,1,2]); | ||
62 | k = m * (j-1) + i; | ||
63 | |||
64 | a = afromncut(v,s,dd,1,no_rep,k); | ||
65 | |||
66 | y = [4*nc-1, 4*nc, 5*nc-1, 5*nc, 6*nc-1, 6*nc]; | ||
67 | for t=1:length(k), | ||
68 | g{y(t)} = reshape(a(t,:),[m,n]); | ||
69 | lgd{y(t)} = sprintf('a at (%d,%d)',i(t),j(t)); | ||
70 | end | ||
71 | |||
72 | % find parameters | ||
73 | fg_title = sprintf('%s: %s=%d, %s=%d, %s=%3.2f, %s=%3.2f',... | ||
74 | par.fname_base,... | ||
75 | 'r_x', par.spatial_neighborhood_x,... | ||
76 | '\sigma_x',par.sigma_x,... | ||
77 | '\sigma_{IC}',par.sig_IC,... | ||
78 | 'repulsion',par.offset); | ||
79 | |||
80 | openfigure(nr,nc,fg_title,0); | ||
81 | dispimg(g,[nr,nc],lgd); | ||
82 | |||
83 | % fix | ||
84 | subplot(nr,nc,nc*3); | ||
85 | plot(s,'ro'); title('\lambda'); | ||
86 | axis square; axis tight; set(gca,'XTick',[]); | ||
87 | for t=1:length(k), | ||
88 | subplot(nr,nc,y(t)); | ||
89 | hold on; | ||
90 | text(j(t),i(t),'+'); | ||
91 | end | ||
92 | hold off | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/startup.m b/SD-VBS/common/toolbox/toolbox_basic/stella/startup.m new file mode 100755 index 0000000..262429a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/startup.m | |||
@@ -0,0 +1,18 @@ | |||
1 | globalenvar; | ||
2 | |||
3 | addpath(IMAGE_DIR); | ||
4 | |||
5 | addpath(HOME_DIR); | ||
6 | |||
7 | path(path,['/hid/jshi/Research/walking']); | ||
8 | |||
9 | tb_dir = ['/hid/jshi/matlab/toolbox/']; | ||
10 | |||
11 | path(path,[tb_dir,'io']); | ||
12 | path(path,[tb_dir,'filter']); | ||
13 | path(path,[tb_dir,'filter_hist']); | ||
14 | path(path,[tb_dir,'disp']); | ||
15 | path(path,[tb_dir,'common']); | ||
16 | path(path,[tb_dir,'textons']); | ||
17 | path(path,[tb_dir,'pyramid']); | ||
18 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/test_ncutm.m b/SD-VBS/common/toolbox/toolbox_basic/stella/test_ncutm.m new file mode 100755 index 0000000..c9b46ab --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/test_ncutm.m | |||
@@ -0,0 +1,38 @@ | |||
1 | fn = 'walk1'; | ||
2 | |||
3 | repulsion_test = 1; | ||
4 | |||
5 | if 1, | ||
6 | f = getimage2(fn); | ||
7 | par = jshincutdefpar; | ||
8 | par.fname_base = fn; | ||
9 | par.spatial_neighborhood_x = 10; | ||
10 | par.sigma_x = 3 * par.spatial_neighborhood_x; | ||
11 | par.sig_IC = 0.03; | ||
12 | par.num_eigvecs = 10; | ||
13 | par.offset = 0.00; | ||
14 | par.sig_filter = 1.0; | ||
15 | par.elong_filter = 3.0; | ||
16 | [par,rec_num] = jshincut(par); | ||
17 | [g,lgd,v,s,dd] = showncut(fn,rec_num); | ||
18 | |||
19 | if repulsion_test, | ||
20 | par.offset = 0.1; | ||
21 | [par,rec_num] = jshincut(par); | ||
22 | figure; | ||
23 | [g,lgd,v,s,dd] = showncut(fn,rec_num); | ||
24 | end | ||
25 | end | ||
26 | |||
27 | if 0, | ||
28 | x = v(:,:,1); | ||
29 | y = v(:,:,2); | ||
30 | figure; | ||
31 | subplot(2,1,1); plot(x(:),y(:),'ro'); | ||
32 | r = sqrt(x.^2+y.^2); | ||
33 | x = x./r; | ||
34 | y = y./r; | ||
35 | subplot(2,1,2); im([x,y]*[x,y]'); | ||
36 | % mask = (x>0) & y>0; | ||
37 | % showmask(f,mask); | ||
38 | end | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/tars/TOOLBOX_calib.tar b/SD-VBS/common/toolbox/toolbox_basic/tars/TOOLBOX_calib.tar new file mode 100755 index 0000000..bb418a5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/tars/TOOLBOX_calib.tar | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/textons/dist2.m b/SD-VBS/common/toolbox/toolbox_basic/textons/dist2.m new file mode 100755 index 0000000..f2d93e1 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/textons/dist2.m | |||
@@ -0,0 +1,27 @@ | |||
1 | function n2 = dist2(x, c) | ||
2 | %DIST2 Calculates squared distance between two sets of points. | ||
3 | % | ||
4 | % Description | ||
5 | % D = DIST2(X, C) takes two matrices of vectors and calculates the | ||
6 | % squared Euclidean distance between them. Both matrices must be of | ||
7 | % the same column dimension. If X has M rows and N columns, and C has | ||
8 | % L rows and N columns, then the result has M rows and L columns. The | ||
9 | % I, Jth entry is the squared distance from the Ith row of X to the | ||
10 | % Jth row of C. | ||
11 | % | ||
12 | % See also | ||
13 | % GMMACTIV, KMEANS, RBFFWD | ||
14 | % | ||
15 | |||
16 | % Copyright (c) Christopher M Bishop, Ian T Nabney (1996, 1997) | ||
17 | |||
18 | [ndata, dimx] = size(x); | ||
19 | [ncentres, dimc] = size(c); | ||
20 | if dimx ~= dimc | ||
21 | error('Data dimension does not match dimension of centres') | ||
22 | end | ||
23 | |||
24 | n2 = (ones(ncentres, 1) * sum((x.^2)', 1))' + ... | ||
25 | ones(ndata, 1) * sum((c.^2)',1) - ... | ||
26 | 2.*(x*(c')); | ||
27 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons.m b/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons.m new file mode 100755 index 0000000..e7fa4b0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons.m | |||
@@ -0,0 +1,46 @@ | |||
1 | function [centers,label,post,d2]=find_textons(FIw,ncenters,centers_in,n_iter); | ||
2 | % [centers,label,post,d2]=find_textons(FIw,ncenters,centers_in,n_iter); | ||
3 | % | ||
4 | % find textons using kmeans for windowed portion FIw of filtered image | ||
5 | % | ||
6 | % to start with centers pulled randomly from image, set centers_in=[] | ||
7 | |||
8 | % define number of textons | ||
9 | %ncenters=25; | ||
10 | |||
11 | [N1,N2,N3]=size(FIw); | ||
12 | % reshape filtered image stack into a long array of feature vectors | ||
13 | fv=reshape(FIw,N1*N2,N3); | ||
14 | % (each row is a feature vector) | ||
15 | |||
16 | %centers=.01^2*randn(ncenters,N3); | ||
17 | % take centers randomly from within image | ||
18 | if isempty(centers_in) | ||
19 | rndnum=1+floor(N1*N2*rand(1,ncenters)); | ||
20 | centers_in=fv(rndnum,:); | ||
21 | end | ||
22 | |||
23 | options = foptions; | ||
24 | options(1)=1; % Prints out error values. | ||
25 | options(5) = 0; | ||
26 | if nargin<4 | ||
27 | n_iter=15; | ||
28 | end | ||
29 | options(14) = n_iter; % Number of iterations. | ||
30 | |||
31 | [centers,options,d2,post]=kmeans2(centers_in,fv,options); | ||
32 | |||
33 | % reshuffle the centers so that the one closest to the origin | ||
34 | % (featureless) comes last | ||
35 | norms=sum(centers.^2,2); | ||
36 | [sortval,sortind]=sort(-norms); | ||
37 | centers=centers(sortind,:); | ||
38 | d2=reshape(d2,N1,N2,ncenters); | ||
39 | post=reshape(post,N1,N2,ncenters); | ||
40 | d2=d2(:,:,sortind); | ||
41 | post=post(:,:,sortind); | ||
42 | |||
43 | |||
44 | % retrieve cluster number assigned to each feature vector | ||
45 | [minval,label]=min(d2,[],3); | ||
46 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons1.m b/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons1.m new file mode 100755 index 0000000..b192015 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/textons/find_textons1.m | |||
@@ -0,0 +1,37 @@ | |||
1 | function [centers,label,post,d2]=find_textons(fv,ncenters,centers_in,n_iter); | ||
2 | % [centers,label,post,d2]=find_textons(FIw,ncenters,centers_in,n_iter); | ||
3 | % | ||
4 | % find textons using kmeans for windowed portion FIw of filtered image | ||
5 | % | ||
6 | % to start with centers pulled randomly from image, set centers_in=[] | ||
7 | |||
8 | [N1,N2] =size(fv); | ||
9 | |||
10 | % take centers randomly from within image | ||
11 | if isempty(centers_in) | ||
12 | rndnum=1+floor(N1*rand(1,ncenters)); | ||
13 | centers_in=fv(rndnum,:); | ||
14 | end | ||
15 | |||
16 | options = foptions; | ||
17 | options(1)=1; % Prints out error values. | ||
18 | options(5) = 0; | ||
19 | if nargin<4 | ||
20 | n_iter=15; | ||
21 | end | ||
22 | options(14) = n_iter; % Number of iterations. | ||
23 | |||
24 | [centers,options,d2,post]=kmeans2(centers_in,fv,options); | ||
25 | |||
26 | |||
27 | % retrieve cluster number assigned to each feature vector | ||
28 | [minval,label]=min(d2,[],2); | ||
29 | |||
30 | |||
31 | h = hist(label(:),[1:max(label(:))]); | ||
32 | a = h>0; | ||
33 | a = cumsum(a); | ||
34 | |||
35 | [nr,nc] = size(label); | ||
36 | label = reshape(a(label(:)),nr,nc); | ||
37 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/textons/kmeans2.m b/SD-VBS/common/toolbox/toolbox_basic/textons/kmeans2.m new file mode 100755 index 0000000..0bd87b2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/textons/kmeans2.m | |||
@@ -0,0 +1,127 @@ | |||
1 | function [centres, options, d2, post, errlog] = kmeans2(centres, data, options) | ||
2 | %KMEANS Trains a k means cluster model. | ||
3 | % | ||
4 | % Description | ||
5 | % CENTRES = KMEANS(CENTRES, DATA, OPTIONS) uses the batch K-means | ||
6 | % algorithm to set the centres of a cluster model. The matrix DATA | ||
7 | % represents the data which is being clustered, with each row | ||
8 | % corresponding to a vector. The sum of squares error function is used. | ||
9 | % The point at which a local minimum is achieved is returned as | ||
10 | % CENTRES. The error value at that point is returned in OPTIONS(8). | ||
11 | % | ||
12 | % [CENTRES, OPTIONS, POST, ERRLOG] = KMEANS(CENTRES, DATA, OPTIONS) | ||
13 | % also returns the cluster number (in a one-of-N encoding) for each | ||
14 | % data point in POST and a log of the error values after each cycle in | ||
15 | % ERRLOG. The optional parameters have the following | ||
16 | % interpretations. | ||
17 | % | ||
18 | % OPTIONS(1) is set to 1 to display error values; also logs error | ||
19 | % values in the return argument ERRLOG. If OPTIONS(1) is set to 0, then | ||
20 | % only warning messages are displayed. If OPTIONS(1) is -1, then | ||
21 | % nothing is displayed. | ||
22 | % | ||
23 | % OPTIONS(2) is a measure of the absolute precision required for the | ||
24 | % value of CENTRES at the solution. If the absolute difference between | ||
25 | % the values of CENTRES between two successive steps is less than | ||
26 | % OPTIONS(2), then this condition is satisfied. | ||
27 | % | ||
28 | % OPTIONS(3) is a measure of the precision required of the error | ||
29 | % function at the solution. If the absolute difference between the | ||
30 | % error functions between two successive steps is less than OPTIONS(3), | ||
31 | % then this condition is satisfied. Both this and the previous | ||
32 | % condition must be satisfied for termination. | ||
33 | % | ||
34 | % OPTIONS(14) is the maximum number of iterations; default 100. | ||
35 | % | ||
36 | % See also | ||
37 | % GMMINIT, GMMEM | ||
38 | % | ||
39 | |||
40 | % Copyright (c) Christopher M Bishop, Ian T Nabney (1996, 1997) | ||
41 | |||
42 | [ndata, data_dim] = size(data); | ||
43 | [ncentres, dim] = size(centres); | ||
44 | |||
45 | if dim ~= data_dim | ||
46 | error('Data dimension does not match dimension of centres') | ||
47 | end | ||
48 | |||
49 | if (ncentres > ndata) | ||
50 | error('More centres than data') | ||
51 | end | ||
52 | |||
53 | % Sort out the options | ||
54 | if (options(14)) | ||
55 | niters = options(14); | ||
56 | else | ||
57 | niters = 100; | ||
58 | end | ||
59 | |||
60 | store = 0; | ||
61 | if (nargout > 3) | ||
62 | store = 1; | ||
63 | errlog = zeros(1, niters); | ||
64 | end | ||
65 | |||
66 | % Check if centres and posteriors need to be initialised from data | ||
67 | if (options(5) == 1) | ||
68 | % Do the initialisation | ||
69 | perm = randperm(ndata); | ||
70 | perm = perm(1:ncentres); | ||
71 | |||
72 | % Assign first ncentres (permuted) data points as centres | ||
73 | centres = data(perm, :); | ||
74 | end | ||
75 | % Matrix to make unit vectors easy to construct | ||
76 | id = eye(ncentres); | ||
77 | |||
78 | % Main loop of algorithm | ||
79 | for n = 1:niters | ||
80 | |||
81 | % Save old centres to check for termination | ||
82 | old_centres = centres; | ||
83 | |||
84 | % Calculate posteriors based on existing centres | ||
85 | d2 = dist2(data, centres); | ||
86 | % Assign each point to nearest centre | ||
87 | [minvals, index] = min(d2', [], 1); | ||
88 | post = id(index,:); | ||
89 | |||
90 | num_points = sum(post, 1); | ||
91 | % Adjust the centres based on new posteriors | ||
92 | for j = 1:ncentres | ||
93 | if (num_points(j) > 0) | ||
94 | centres(j,:) = sum(data(find(post(:,j)),:), 1)/num_points(j); | ||
95 | end | ||
96 | end | ||
97 | |||
98 | % Error value is total squared distance from cluster centres | ||
99 | e = sum(minvals); | ||
100 | tmp = sort(minvals); | ||
101 | e95 = sqrt(tmp(round(length(tmp) * 0.95))); | ||
102 | erms = sqrt(e/ndata); | ||
103 | if store | ||
104 | errlog(n) = e; | ||
105 | end | ||
106 | if options(1) > 0 | ||
107 | fprintf(1, ' Cycle %4d RMS Error %11.6f 95-tier Error %11.6f\n', n, erms,e95); | ||
108 | end | ||
109 | |||
110 | if n > 1 | ||
111 | % Test for termination | ||
112 | if max(max(abs(centres - old_centres))) < options(2) & ... | ||
113 | abs(old_e - e) < options(3) | ||
114 | options(8) = e; | ||
115 | return; | ||
116 | end | ||
117 | end | ||
118 | old_e = e; | ||
119 | end | ||
120 | |||
121 | % If we get here, then we haven't terminated in the given number of | ||
122 | % iterations. | ||
123 | options(8) = e; | ||
124 | %if (options(1) >= 0) | ||
125 | % disp('Warning: Maximum number of iterations has been exceeded'); | ||
126 | %end | ||
127 | |||