Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot an image (.jpg) in MATLAB in both 2-D and 3-D?

I have a 2-D scatter plot and at the origin I want to display an image (not a colorful square, but an actual picture). Is there any way to do this?

I also will be plotting a 3-D sphere in which I would like an image to be displayed at the origin as well.

like image 869
dewalla Avatar asked Sep 15 '10 15:09

dewalla


1 Answers

For 2-D plots...

The function IMAGE is what you're looking for. Here's an example:

img = imread('peppers.png');             %# Load a sample image
scatter(rand(1,20)-0.5,rand(1,20)-0.5);  %# Plot some random data
hold on;                                 %# Add to the plot
image([-0.1 0.1],[0.1 -0.1],img);        %# Plot the image

alt text


For 3-D plots...

The IMAGE function is no longer appropriate, as the image will not be displayed unless the axis is viewed from directly above (i.e. from along the positive z-axis). In this case you will have to create a surface in 3-D using the SURF function and texture map the image onto it. Here's an example:

[xSphere,ySphere,zSphere] = sphere(16);          %# Points on a sphere
scatter3(xSphere(:),ySphere(:),zSphere(:),'.');  %# Plot the points
axis equal;   %# Make the axes scales match
hold on;      %# Add to the plot
xlabel('x');
ylabel('y');
zlabel('z');
img = imread('peppers.png');     %# Load a sample image
xImage = [-0.5 0.5; -0.5 0.5];   %# The x data for the image corners
yImage = [0 0; 0 0];             %# The y data for the image corners
zImage = [0.5 0.5; -0.5 -0.5];   %# The z data for the image corners
surf(xImage,yImage,zImage,...    %# Plot the surface
     'CData',img,...
     'FaceColor','texturemap');

alt text

Note that this surface is fixed in space, so the image will not always be directly facing the camera as you rotate the axes. If you want the texture-mapped surface to automatically rotate so that it is always perpendicular to the line of sight of the camera, it's a much more involved process.

like image 135
gnovice Avatar answered Oct 19 '22 23:10

gnovice