Wednesday, February 29, 2012

Camera Simulation on 3D Data

I used third party ply_read() Matlab function to read the 3D data. I simulate the camera by changing the camera position and rotation in predefined trajectories.

Matlab Code

% USE THIRD PARTY FUNCTION TO READ 3D DATA
[Tri, Pts] = ply_read('bunny.ply', 'tri');
[elements, var] = ply_read('bunny.ply');
conf = elements.vertex.confidence;

c = size(Pts, 2); % NO OF VERTICES
f = 0.05; % F = 50mm
cam_pos = [0; 0; -0.5];
theta_x = 0; theta_y = 0; theta_z = 0;  % CAMERA ROTATION PARAMETERS
video_im = uint8(zeros(301, 301, 1, 301)); % OUTPUT IMAGE FRAMES

% MOVE CAMERA TOWARDS OBJECT IN SINE TRAJECTORY
dist1=-0.5 + sin(linspace(0,pi,100))/10; 
dist4=-0.5 - sin(linspace(0,pi,200));
% ROTATE 360 DEGREES WITH RESPECT TO Y AXIS
dist2=linspace(0,2*pi,200);
% ROTATE 360 DEGREES WITH RESPECT TO X AXIS
dist3=linspace(0,2*pi,200);
% CONCATINATED LIST
dist=[dist1,dist2,dist3];

for i = 1 : length(dist)
% CHANGE TRANSFORMATION PARAMETERS
    if(i <= length(dist1))
        cam_pos=[0;0;dist(i)];
    elseif(i <= length(dist2) + length(dist1))
        theta_y = dist(i);
    else
        theta_x = dist(i);
        theta_y = dist(i);
        theta_z = dist(i);
        cam_pos=[0;0;dist4(i-300)];
    end
% CAMERA TRANSOFRMATION MATRIX (ROTATION)
    Rx = [1 0 0; 0 cos(theta_x) -1*sin(theta_x); 0 sin(theta_x) cos(theta_x)];
    Ry = [cos(theta_y) 0 sin(theta_y); 0 1 0; -1*sin(theta_y) 0 cos(theta_y)];
    Rz = [cos(theta_z) -1*sin(theta_z) 0; sin(theta_z) cos(theta_z) 0; 0 0 1];

    eff_obj_data = Pts - repmat(cam_pos, 1, c);
    D = Rx * Ry * Rz * (eff_obj_data);
    
% APPLY TRANSFORMATION
    P = [f*eye(3) zeros(3,1)]; % CAMERA MATRIX
    im = P*[D; f*ones(1, c)];
    scaled_im = im(1 : 2, :);
    % SCALING THE IMAGE TO THE RANGE
    scaled_im(1, :) = (im(1, :)  - min(im(1, :)))./abs(im(3, :)).*600;
    scaled_im(2, :) = (im(2, :)  - min(im(2, :)))./abs(im(3, :)).*600;
    scaled_im = ceil(scaled_im) + 1;

% INTENSITY MAPPING
    im_out = zeros(301, 301);
    for j = 1 : c
        if(scaled_im(2, j) < 302 && scaled_im(1, j) < 301)
            im_out(302 - scaled_im(2, j), scaled_im(1, j)) = conf(j);
        end
    end
% COLLECTING FRAMES
video_im(:, :, :, i) = uint8(im_out.*200);
end

mov = immovie(video_im, gray);
movie2avi(mov, 'out.avi'); % SAVE VIDEO
implay(mov);

Output


No comments:

Post a Comment