Is it possible to perform texture mapping within MATLAB?

154 views (last 30 days)
I would like to map a 2-D image onto a 3-D surface by transforming color data so that it conforms to the surface. I would like to know how to display an image in a 3-D coordinate system with the image lying flat on the xy-plane.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Nov 2018
Texture mapping allows you to apply a "texture", such as bumps or wood grain, to a MATLAB surface graphics object without performing the geometric modeling necessary to actually create a surface with these features. The color data can also be any image, such as a scanned photograph.
MATLAB texture maps a surface by assigning the texture color data to the surface's "Cdata" property. While a surface's color is always determined by the values contained in its "Cdata" property, texture mapping differs in that the dimensions of the "Cdata" property value can be different from the surface's "Zdata" property value. This allows you to apply an image of arbitrary size to any surface. MATLAB interpolates texture color data so that it is mapped to the entire surface.
You must set the surface's "FaceColor" property to texturemap before setting its "Cdata" property value to an arbitrarily sized array. You can do this in one of two ways:
1. Use the surface object creation function, which allows you to specify object properties when you create the surface.
2. Use the SET function to change the "FaceColor" property of an existing surface, and to specify new data for "Cdata".
The following example shows how to map the mandrill image to a cylindrical surface. By default, MATLAB maps the image to the entire surface. However, this example pads the image data so that the mandrill is displayed on half of the cylinder, thus making it easier to recognize the image.
load mandrill
colormap(map)
[x,y,z] = cylinder;
Xhalf = [ones(480,375)*max(max(X))/2, X, ...
ones(480,125)*max(max(X))/2];
surface(x,y,z, 'FaceColor','texturemap',...
'EdgeColor','none','Cdata',flipud(Xhalf))
view(3)
The size of the mandrill data is 480-by-500, so an additional 500 columns of data are added to make the image occupy half of the cylinder. The columns are added before and after the existing data so as to orient the image correctly for the default 3-D view.
The values used to pad the data are set to one-half of the maximum value of the mandrill data. This sets the color of the half cylinder that does not contain the mandrill to the color that is in the middle of the mandrill's color map (map).
In addition to setting the "FaceColor" property to "texturemap", the "EdgeColor" property is set to "none" in order to remove the grid lines.
Since image data is normally displayed with "ij" axis numbering, the mandrill data is reversed in the vertical direction using the FLIPUD function. (See the axis reference page for more information.)
To produce the same picture using high-level graphics functions, you must obtain the handle of the surface and change the relevant properties:
[x,y,z] = cylinder;
h = surf(x,y,z);
set(h, 'FaceColor','texturemap','EdgeColor','none','Cdata',flipud(Xhalf))
Also, the WARP function in the Image Processing Toolbox displays an image as texture-mapped surface. You can then use the ROTATE3D function to get the view you want. Here is a short example:
f = imread('flowers.tif');
warp(f);
rotate3d
Then, drag with the mouse to get the view you want.
For more information regarding this and other HG properties, see the following:
  5 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!