Single View Reconstruction

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 pt3D = stereo(im1, im2, C1, C2)

% digitisasi pada image 1
figure(1)
imshow(im1)
title(‘Digitisasi pada citra 1’),xlabel(‘klik kanan untuk berhenti’);
[u1,v1] = digipts;

% digitisasi pada image 2
figure(2)
imshow(im2),title(‘Digitisasi pada citra 2’),xlabel(‘klik kanan untuk berhenti’);
[u2,v2] = digipts;

% periksa apakah jumlah titik di kedua image sudah sama
if length(u1) ~= length(u2)
fprintf(1, ‘Jumlah titik tidak sama\n’);
end

% ubah koordinat 2 dimensi ke koordinat 3 dimensi
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

pt3D = b’;
end;

Figure stereo1.jpg dan stereo2.jpg, beserta urutan input titik koordinat :

stereo1.jpg

stereo2.jpg

Hasil running program pada image stereo1.jpg dan stereo2.jpg:

Koordinat 3D dari setiap titik sudut yang ada pada image

pt3D =  -287.6162  156.9360  141.5711
	-173.5534  165.7478  142.0923
	-177.2151   17.7231  140.6427
	-290.0588   13.4128  143.3063
	-285.0746  161.0821    5.2950
	 287.2649   10.8720    7.0309
	-175.7786   11.6773    5.9158
	 -85.1953  -67.4566   -0.1560
	 -68.3756 -145.9679  124.5706
	  21.7703 -179.7757   -4.7074
	-136.9644 -207.1403   -0.7542
	 136.5516  -91.5688   62.0922
	 208.7959  -91.9443   61.3812
	 209.9973 -157.9312   56.4162
	 137.4747 -155.6894   59.2162
	 131.8408  -95.6491   -3.6480
	 138.2456 -156.7457   -9.2397
	201.3252  -164.9430   -8.6116

Panjang sisi bangun ruang 1 :

	143.5545  114.4039  148.0771  112.9574
	112.9574  134.8701  111.4948  136.3277
	136.3277  150.2361  136.3629  143.5545
	114.3362  136.3629  114.4039  136.3277
	136.3277  148.0771  134.8701  151.6129
	151.6129  111.4948  150.2361  114.3362

Panjang sisi bangun ruang 2 :

	148.3363  161.1895  155.1706
	148.3363  155.4117  148.9695
	155.1706  161.1247  148.9695
	161.1895  161.1247  155.4117

Panjang sisi bangun ruang 3 :

	64.1917   72.2487   66.1844   72.6112
	72.6112   65.9772   63.6131   68.4684
	68.4684   61.6854   66.0350   64.1917
	77.8465   66.0350   72.2487   68.4684
	68.4684   66.1844   65.9772   72.4292
	72.4292   63.6131   61.6854   77.8465

Listing keseluruhan dari program:

function portofolio7()
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,:));

% set label koordinat
text(100,0,0,‘x’);
text(0,100,0,‘y’);
text(0,0,100,‘z’);

% buat koordinat 3D
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;

% fungsi untuk objek kubus
function cube(cubepts3D)

% estimasi koordinat titik sudut yang tidak nampak
cubepts3D(8,: ) = – cubepts3D(4,: ) + cubepts3D(6,: ) + cubepts3D(2,: );

% tentukan sisi dari kubus dari nomor2 titik sudut

    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];

% hubungkan setiap titik sudut dengan titik sudut tetangga
patch(‘Faces’,cubefaces,‘Vertices’,cubepts3D, ‘FaceColor’, ‘none’)

% hitung panjang setiap sisi kubus
fprintf(1, ‘Length matrix of face sides\n’);
[slengths, nface] = sidelengths(cubepts3D, cubefaces)
end

% fungsi untuk objek limas
function tetrahedron(tetrahedronpts3D)
% tentukan sisi limas dari nomor2 titik sudut

    tetrahedronfaces = [1 2 3
	                1 2 4
	                1 3 4
	                2 3 4];

% hubungkan setiap titik sudut dengan titik sudut tetangga
patch(‘Faces’,tetrahedronfaces,‘Vertices’,tetrahedronpts3D, ‘FaceColor’, ‘none’)

% hitung panjang setiap sisi limas
fprintf(1, ‘Length matrix of face sides\n’);
[slengths, nface] = sidelengths(tetrahedronpts3D, tetrahedronfaces)
end

% fungsi untuk menghitung panjang setiap sisi bangun ruang
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

end

Listing program digipts.m :

% DIGIPTS – digitise points in an image
%
% Function to digitise points in an image. Points are digitised by clicking
% with the left mouse button. Clicking any other button terminates the
% function. Each location digitised is marked with a red ‘+’.
%
% Usage: [u,v] = digipts
%
% where u and v are nx1 arrays of x and y coordinate values digitised in
% the image.
%
% This function uses the cross-hair cursor provided by GINPUT. This is
% much more useable than IMPIXEL
% Peter Kovesi
% School of Computer Science & Software Engineering
% The University of Western Australia
% pk @ csse uwa edu au
% http://www.csse.uwa.edu.au/~pk
%
% May 2002

function [u,v] = digipts
hold on
u = []; v = [];
but = 1;
while but == 1
[x y but] = ginput(1);
if but == 1
u = [u;x];
v = [v;y];
plot(u,v,‘r+’);
end
end

hold off

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s