Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw a texture-mapped triangle in MATLAB?

I have a triangle in (u,v) coordinates in an image. I would like to draw this triangle at 3D coordinates (X,Y,Z) texture-mapped with the triangle in the image.

Here, u,v,X,Y,Z are all vectors with three elements representing the three corners of the triangle.

I have a very ugly, slow and unsatisfactory solution in which I:

  1. extract a rectangular part of the image
  2. transform it to 3D space with the transformation defined by the three points
  3. draw it with surface
  4. finally masking out everything that is not part of the triangle with AlphaData

Surely there must be an easier way of doing this?

like image 793
Petter Avatar asked Dec 13 '22 23:12

Petter


1 Answers

I have what I think is a better solution for you involving two steps. First, it extracts a rectangular part of your image, half of which is the triangular section to be used as a texture map and half of which will be ignored. Then this texture map is applied to a 3-D surface object whose points are adjusted to render it as a triangle instead of a quadrilateral.

For the example I will show here, I will use the following values for your various parameters, assuming you have a triangle whose points are labeled as the "origin" (triangle vertex), point "A", and point "B" in the image space (as in the first image below):

x = [0.1 0.9 0.8];   % [xorigin xA xB] coordinates in 3-D space
y = [0.9 0.1 0.8];   % [yorigin yA yB] coordinates in 3-D space
z = [0.1 0.1 0.9];   % [zorigin zA zB] coordinates in 3-D space
origin = [150 350];  % Vertex of triangle in image space
U = [300 -50];       % Vector from origin to point A in image space
V = [50 -250];       % Vector from origin to point B in image space
img = imread('peppers.png');  % Sample image for texture map


Extracting the texture map via projective transformation:

This step uses the Image Processing Toolbox functions maketform and imtransform to perform a projective transformation of the part of the image containing the triangle you want to use as a texture map. Note that since images have to be rectangular, an additional triangular section defined by points (O,B,C) has to be included.

enter image description here

The triangular part of the image you want will be in the lower right half of the image, while the additional triangular "filler" part will be in the upper left. Note that this additional triangle can extend outside of the image, which will cause part of it to be filled with black by default. Here's the code to perform the projective transform illustrated above:

A = origin+U;  % Point A
B = origin+V;  % Point B
C = B-U;       % Point C
[nRows, nCols, nPages] = size(img);  % Image dimensions
inputCorners = [origin; ...          % Corner coordinates of input space
                A; ...
                B; ...
                C];
outputCorners = [1 nRows; ...        % Corner coordinates of output space
                 nCols nRows; ...
                 nCols 1; ...
                 1 1];
tform = maketform('projective', ...  % Make the transformation structure
                  inputCorners, ...
                  outputCorners);
triTexture = imtransform(img,tform, 'bicubic', ...  % Transform the image
                         'xdata', [1 nCols], ...
                         'ydata', [1 nRows], ...
                         'size', [nRows nCols]);

Note that this code will create a final image triTexture that is the same size as the input image img.


Plotting the triangular texture-mapped surface:

Plotting the surface is now quite simple, assuming you've ordered the values in your x,y,z variables such that the coordinates for the origin point are in the first indices, the coordinates for point A are in the second indices, and the coordinates for point B are in the third indices. You can now create new sets of 2-by-2 surface coordinates X,Y,Z that contain two copies of point B, which causes only half of the surface to be rendered (i.e. the half containing the desired triangular image as a texture map). Here's the code to do this:

index = [3 3; 1 2];  % Index used to create 2-by-2 surface coordinates
X = x(index);        % x coordinates of surface
Y = y(index);        % y coordinates of surface
Z = z(index);        % z coordinates of surface
hSurface = surf(X, Y, Z, triTexture, ...  % Plot texture-mapped surface
                'FaceColor', 'texturemap', ...
                'EdgeColor', 'none');
axis equal            % Use equal scaling on axes
axis([0 1 0 1 0 1]);  % Set axes limits
xlabel('x-axis');     % x-axis label
ylabel('y-axis');     % y-axis label
zlabel('z-axis');     % z-axis label

And here's the resulting texture-mapped triangular surface it creates, with an inset added to show that the texture map contains the correct triangular part of the original image:

enter image description here

like image 53
gnovice Avatar answered Dec 21 '22 23:12

gnovice