开发者

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

开发者 https://www.devze.com 2023-01-16 12:01 出处:网络
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 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.


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

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


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');

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消