如何在MATLAB中以2D和3D方式绘制图像(.jpg)?

16

我有一个二维散点图,在原点位置想要显示一张图片(不是一个彩色的正方形,而是一张实际的图片)。有没有办法做到这一点?

我还将绘制一个三维球体,在原点位置也希望显示一张图片。

1个回答

34

对于二维图表...

IMAGE 函数是您要寻找的。这里有一个例子:

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


对于3D图像...

使用IMAGE函数已经不再适用,因为除非从正上方(即沿正z轴方向)查看轴,否则图像将无法显示。在这种情况下,您需要使用SURF函数创建一个三维曲面,并将图像映射到其上。以下是一个示例:

[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

请注意,这个表面在空间中是固定的,因此当您旋转轴时,图像不会始终直接面向相机。如果您希望贴有纹理的表面自动旋转,使其始终垂直于相机的视线,那么这将是一个更加复杂的过程。


非常好的回答。我可以调整图像大小以适应散点图吗? - SolessChong
如何在二维平面上投影图像,同时展示动态生成的三维数据? - Diego

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接