diff options
Diffstat (limited to 'SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rect.m')
-rwxr-xr-x | SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rect.m | 93 |
1 files changed, 93 insertions, 0 deletions
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)); | ||