Posted by: megabangetz on: May 18, 2008
Rekontruksi 3D
Listing program stereo.m
% STEREO
%
% Usage: pt3D = stereo(im1, im2, c1, c2)
%
% Where: im1 and im2 are the two stereo images
% C1 and C2 are the calibration matrices
% for these two images respectively
%
% The function will prompt you to digitise some points in the first image
% (finishing by clicking the right button). The function then
% prompts you to digitise the equivalent points (which you must digitise
% in exactly the same sequence) in the second image.
% The function then solves the stereo equations
% and returns the 3D coordinates of the points in pt3D.
function [XYZ, uv1, uv2] = stereo(im1, im2, C1, C2)
fprintf(1, ‘Digitise some points in figure 1\n’);
figure(1)
imshow(im1);
[u1,v1] = digipts;
uv1 = [u1,v1]‘;
fprintf(1, ‘Digitise some points in figure 2\n’);
figure(2)
imshow(im2);
[u2,v2] = digipts;
uv2 = [u2,v2]‘;
% check if same number of points are selected
if length(u1) ~= length(u2)
fprintf(1, ‘Same number of points not selected\n’);
end
for i = 1:length(u1)
a = [C1(1:2,1:3) - [u1(i)*C1(3,1:3); v1(i)*C1(3,1:3)];
C2(1:2,1:3) – [u2(i)*C2(3,1:3); v2(i)*C2(3,1:3)]];
c = [u1(i) - C1(1,4);
v1(i) - C1(2,4);
u2(i) - C2(1,4);
v2(i) - C2(2,4)];
b(:, i) = a \ c;
end
XYZ = b’;
Image ’stereo1.jpg’
Image ”stereo2.jpg’
Dengan menjalankan program stereo.m, maka didapatkan koordinat 3D dari titik-titik sudut ketiga bangun ruang pada gambar diatas sebagai berikut:
pt3D =
-287.7542 156.7465 143.2885
-176.6269 162.0598 144.3627
-175.6786 19.8716 141.7267
-290.0593 13.4079 144.3193
-287.5655 157.5390 10.9533
-286.3338 13.6666 9.4036
-172.4321 17.1203 3.2873
-83.8519 -66.6201 1.3714
-66.1821 -141.0900 122.3533
20.2680 -179.4214 -6.0719
-134.6197 -204.2349 -2.2669
136.6957 -93.6632 63.9256
207.1254 -91.0740 63.7992
201.4685 -162.9965 60.2827
138.5328 -155.1594 59.2890
132.6638 -94.1806 -4.4145
134.7488 -160.4601 -9.8006
206.4264 -163.0088 -8.9430
Panjang sisi balok
slengths =
143.3608 111.2594 142.2157 114.5926
114.5926 138.5048 114.1181 134.9674
134.9674 143.8860 132.3377 143.3608
114.7735 132.3377 111.2594 134.9674
134.9674 142.2157 138.5048 145.3295
145.3295 114.1181 143.8860 114.7735
nface =
4 1 2 3 4
4 3 7 6 4
4 6 5 1 4
8 5 1 2 8
8 2 3 7 8
8 7 6 5 8
Panjang sisi limas
slengths =
143.1594 159.4866 153.6897
143.1594 155.5672 146.7257
153.6897 156.9089 146.7257
159.4866 156.9089 155.5672
nface =
1 2 3 1
1 2 4 1
1 3 4 1
2 3 4 2
Panjang sisi kubus
slengths =
61.6981 70.4773 72.2303 63.4295
63.4295 69.4031 71.7281 69.3959
69.3959 66.5307 68.4608 61.6981
70.7170 68.4608 70.4773 69.3959
69.3959 72.2303 69.4031 66.8054
66.8054 71.7281 66.5307 70.7170
nface =
4 1 2 3 4
4 3 7 6 4
4 6 5 1 4
8 5 1 2 8
8 2 3 7 8
8 7 6 5 8
Untuk menampilkan dua view yang berbeda dari rekonstruksi 3D kubus, balok dan limas dengan koordinat titik sudut estimasi dari kubus dan balok yang tidak kelihatan, juga sumbu koordinat yang mengindikasikan bidang dasar, jalankan fungsi rekon.m
Listing fungsi rekon.m
function rekon()
im1 = imread(‘stereo1.jpg’ );
im2 = imread(‘stereo2.jpg’ );
C1 = [0.6596 -0.7391 -0.0615 363.4235;
-0.1851 -0.1387 -0.9437 342.7417;
0.0005 0.0003 -0.0003 1.0000];
C2 = [0.9234 -0.2221 -0.0257 347.7796;
-0.0741 -0.2278 -0.9168 339.8960;
0.0002 0.0004 -0.0002 1.0000];
pt3D = stereo(im1, im2, C1, C2)
figure(3)
cube(pt3D(1:7,:));
tetrahedron(pt3D(8:11,:));
cube(pt3D(12:18,:));
% label coordinate axes
text(100,0,0,’x');
text(0,100,0,’y');
text(0,0,100,’z');
% draw in a set of coordinate axes
axislength = 100*eye(3);
for i=1:3
line([0, axislength(i,1)], [0, axislength(i,2)], [0, axislength(i,3)]);
end
axis equal; box on; rotate3D on; grid on;
end
function cube(cubepts3D)
% determine hidden vertex
cubepts3D(8,: ) = – cubepts3D(4,: ) + cubepts3D(6,: ) + cubepts3D(2,: );
% define faces from standard numbering
cubefaces = [4 1 2 3
4 3 7 6
4 6 5 1
8 5 1 2
8 2 3 7
8 7 6 5];
% draw ‘patcheds’ from vertice and face matrix
patch(‘Faces’,cubefaces,’Vertices’,cubepts3D, ‘FaceColor’, ‘none’)
fprintf(1, ‘Length matrix of face sides\n’);
[slengths, nface] = sidelengths(cubepts3D, cubefaces)
end
function tetrahedron(tetrahedronpts3D)
% define faces from standard numbering
tetrahedronfaces = [1 2 3
1 2 4
1 3 4
2 3 4];
% draw ‘patcheds’ from vertice and face matrix
patch(‘Faces’,tetrahedronfaces,’Vertices’,tetrahedronpts3D, ‘FaceColor’, ‘none’)
fprintf(1, ‘Length matrix of face sides\n’);
[slengths, nface] = sidelengths(tetrahedronpts3D, tetrahedronfaces)
end
function [slengths, nface] = sidelengths(pt3D, face)
[rows, cols] = size(face);
nface = [face face(:,1)];
for i=1:cols
for j=1:rows
slengths(j,i) = norm(pt3D(nface(j,i),:)-pt3D(nface(j,i+1),:));
end
end
end
Hasilnya (dilihat dari 2 sisi yang berbeda):