summaryrefslogtreecommitdiffstats
path: root/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rect.m
diff options
context:
space:
mode:
Diffstat (limited to 'SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rect.m')
-rwxr-xr-xSD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/rect.m127
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 @@
1function [Irec] = rect(I,R,f,c,k,alpha,KK_new);
2
3
4if 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;
19end;
20
21
22if 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;
29end;
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
37if ~exist('KK_new'),
38 KK_new = [f(1) alpha_c*fc(1) c(1);0 f(2) c(2);0 0 1];
39end;
40
41
42[nr,nc] = size(I);
43
44Irec = 255*ones(nr,nc);
45
46[mx,my] = meshgrid(1:nc, 1:nr);
47px = reshape(mx',nc*nr,1);
48py = reshape(my',nc*nr,1);
49
50rays = inv(KK_new)*[(px - 1)';(py - 1)';ones(1,length(px))];
51
52
53% Rotation: (or affine transformation):
54
55rays2 = R'*rays;
56
57x = [rays2(1,:)./rays2(3,:);rays2(2,:)./rays2(3,:)];
58
59
60% Add distortion:
61
62k1 = k(1);
63k2 = k(2);
64
65p1 = k(3);
66p2 = k(4);
67
68r_2 = sum(x.^2);
69
70delta_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
73xd = (ones(2,1)*( 1 + k1 * r_2 + k2 * r_2.^2)) .* x + delta_x;
74
75
76% Reconvert in pixels:
77
78px2 = f(1)*(xd(1,:)+alpha_c*xd(2,:))+c(1);
79py2 = f(2)*xd(2,:)+c(2);
80
81
82% Interpolate between the closest pixels:
83
84px_0 = floor(px2);
85px_1 = px_0 + 1;
86alpha_x = px2 - px_0;
87
88py_0 = floor(py2);
89py_1 = py_0 + 1;
90alpha_y = py2 - py_0;
91
92good_points = find((px_0 >= 0) & (px_1 <= (nc-1)) & (py_0 >= 0) & (py_1 <= (nr-1)));
93
94I_lu = I(px_0(good_points) * nr + py_0(good_points) + 1);
95I_ru = I(px_1(good_points) * nr + py_0(good_points) + 1);
96I_ld = I(px_0(good_points) * nr + py_1(good_points) + 1);
97I_rd = I(px_1(good_points) * nr + py_1(good_points) + 1);
98
99
100I_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
103Irec((px(good_points)-1)*nr + py(good_points)) = I_interp;
104
105
106
107return;
108
109
110% Convert in indices:
111
112fact = 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;
118Iinterp = interp2(XX,YY,I,XXi,YYi);
119%toc
120
121[nri,nci] = size(Iinterp);
122
123
124ind_col = round(fact*(f(1)*xd(1,:)+c(1)))+1;
125ind_row = round(fact*(f(2)*xd(2,:)+c(2)))+1;
126
127good_points = find((ind_col >=1)&(ind_col<=nci)&(ind_row >=1)& (ind_row <=nri));