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