summaryrefslogtreecommitdiffstats
path: root/SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rect.m
diff options
context:
space:
mode:
Diffstat (limited to 'SD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rect.m')
-rwxr-xr-xSD-VBS/common/toolbox/toolbox_basic/calib_bouguetj/rect.m93
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 @@
1function [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
9Irec = 255*ones(nr,nc);
10
11[mx,my] = meshgrid(1:nc, 1:nr);
12px = reshape(mx',nc*nr,1);
13py = reshape(my',nc*nr,1);
14
15rays = inv(KK_new)*[(px - 1)';(py - 1)';ones(1,length(px))];
16
17
18% Rotation: (or affine transformation):
19
20rays2 = R'*rays;
21
22
23x = [rays2(1,:)./rays2(3,:);rays2(2,:)./rays2(3,:)];
24
25% Add distortion:
26
27k1 = k(1);
28k2 = k(2);
29
30p1 = k(3);
31p2 = k(4);
32
33r_2 = sum(x.^2);
34
35delta_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
38xd = (ones(2,1)*( 1 + k1 * r_2 + k2 * r_2.^2)) .* x + delta_x;
39
40
41% Reconvert in pixels:
42
43px2 = f(1)*xd(1,:)+c(1);
44py2 = f(2)*xd(2,:)+c(2);
45
46
47% Interpolate between the closest pixels:
48
49
50px_0 = floor(px2);
51px_1 = px_0 + 1;
52alpha_x = px2 - px_0;
53
54py_0 = floor(py2);
55py_1 = py_0 + 1;
56alpha_y = py2 - py_0;
57
58good_points = find((px_0 >= 0) & (px_1 <= (nc-1)) & (py_0 >= 0) & (py_1 <= (nr-1)));
59
60I_lu = I(px_0(good_points) * nr + py_0(good_points) + 1);
61I_ru = I(px_1(good_points) * nr + py_0(good_points) + 1);
62I_ld = I(px_0(good_points) * nr + py_1(good_points) + 1);
63I_rd = I(px_1(good_points) * nr + py_1(good_points) + 1);
64
65
66I_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
69Irec((px(good_points)-1)*nr + py(good_points)) = I_interp;
70
71
72
73return;
74
75
76% Convert in indices:
77
78fact = 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;
84Iinterp = interp2(XX,YY,I,XXi,YYi);
85%toc
86
87[nri,nci] = size(Iinterp);
88
89
90ind_col = round(fact*(f(1)*xd(1,:)+c(1)))+1;
91ind_row = round(fact*(f(2)*xd(2,:)+c(2)))+1;
92
93good_points = find((ind_col >=1)&(ind_col<=nci)&(ind_row >=1)& (ind_row <=nri));