Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
3D to 2D screen coordinate transformation

Subject: 3D to 2D screen coordinate transformation

From: Cristobal Curio

Date: 7 Oct, 2002 13:20:58

Message: 1 of 2

Hello,


I need help with some graphics problem. I try to figure out the real
2D Coordinates of a 3D plot on a snapshot screen. I have looked at
the "viewmtx" commands and examples. I can not interprete the
normalized coordinate results. My goal is to overlay the calculated
2d projection coordinates onto a snapshot image of defined size in
pixel units.
Is there a way to avoid calculating intersection points of lines
between the 3D coordinates and the camera position with the viewing
plane to get the "ground truth".
Below I include some sample code I experimented with and which might
help understanding my problem.
I appreciate any help.
Thanks for your consideration,


Cris Curio


%map3Dto2D.m
% this is a little demo program.
% a cube, centered around the origin should be watched from different
% perspectives. The camera parameters are changed manually.
% Also the target points do vary.
% Generally, is the another way of caculating the 2d projected 3d
points
% onto the snapshot of the 3D scene?
% It looks like viewmtx gives me something close to that but still the
% normalization and alignment to real 2D pixels seems unclear to me.


clear all
close all


H_wnd.h1 = figure(1) ;


set( H_wnd.h1, 'Color', [1 1 1] ) ;
set( H_wnd.h1, 'Renderer', 'OpenGl' ) ;


%size of the simulation window in pixels units
xsi = 192 ;
ysi = 144 ;


set( H_wnd.h1, 'Position', [ 20 40 xsi ysi] ) ;
set( gca, 'Units', 'pixel' ) ;
set( gca, 'Position', [ 1 1 xsi ysi] ) ;


% some loops for trying different target positions and target points
% of the camera
for sh = -2:2:2
  for ang = 0:2*6*pi/360:pi


    figure( H_wnd.h1 ) ;
    colormap gray


    % some cube centered around the origin
    x = 2.*( [ 0 1 1 0 0 0 1 1 0 0 1 1 1 1 0 0 ] - .5
) ;
    y = 1.*( [ 0 0 1 1 0 0 0 1 1 0 0 0 1 1 1 1 ] - .5
) ;
    z = 1.*( [ 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 ] - .5
) ;


    plot3( x, y, z, 'r' ) ;
    xlabel( 'x' ) ;
    ylabel( 'y' ) ;
    zlabel( 'z' ) ;


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%% set ego camera in scene %%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    start_y = -8 ;
    height = 0 ;
    camera_view_angle = 30 ;


    ego_pos_t = [ sin(ang)*start_y cos(ang)*start_y height ] ;
    target_point = [ 0 0 sh ] ;
    view_dir_t = target_point - ego_pos_t ;


    set( gca, 'CameraPositionMode', 'manual' ) ;
    set( gca, 'CameraPosition', ego_pos_t ) ;
    set( gca, 'CameraTargetMode', 'manual' ) ;
    set( gca, 'CameraTarget', view_dir_t ) ;
    set( gca, 'CameraViewAngleMode','manual' ) ;
    set( gca, 'CameraViewAngle', camera_view_angle ) ;


    camproj( 'perspective' ) ;
    
    axis vis3d
    axis on
    axis equal
    
    % render all stuff now
    drawnow
    
    % find intersection of line from 3d points with camera position
    % and the hopefully the correctly assumed viewing plane
    a_vek = ego_pos_t + view_dir_t ;
    n = norm(view_dir_t).*view_dir_t ;
    X_d = [ x; y; z ] - repmat( ego_pos_t', 1, length(x) ) ;
    t = -( n*ego_pos_t' + n*a_vek' )./( n*X_d ) ;
    % calculate the viewing plane points intersection points
    for k = 1:length(t)
      render( :, k ) = ego_pos_t' + t(k).*X_d(:,k) ;
    end
    
    % get the vector notation of the viewing plane with two orthogonal
    % vectors
    u = cross( view_dir_t, [ 0 0 1 ] ) ;
    un = 1 / norm(u).*u ;
    u_units = norm( view_dir_t ) * tan( ( .5*pi*camera_view_angle /
180 ) ) ;
    
    v = cross( un, view_dir_t ) ;
    vn = 1 / norm( v ).*v ;
    v_units = u_units ;
    
    % projection of render points onto viewing plane, coordinates in
the
    % span of the viewing plane
    % These should be the normalized i

Subject: 3D to 2D screen coordinate transformation

From: Tjeerd Dijkstra

Date: 31 Oct, 2002 15:20:17

Message: 2 of 2

Hello,


I found it necessary to explicitly set XLim, YLim and Zlim, to
overrule Matlab's automatic scaling.


Hope this helps,

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us