如何在 MATLAB 中以 2-D 和 3-D 形式绘制图像 (.jpg)? [英] How can I plot an image (.jpg) in MATLAB in both 2-D and 3-D?

查看:26
本文介绍了如何在 MATLAB 中以 2-D 和 3-D 形式绘制图像 (.jpg)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维散点图,在原点我想显示一个图像(不是彩色方块,而是实际图片).有没有办法做到这一点?

我还将绘制一个 3D 球体,我希望在该球体中的原点也显示图像.

解决方案

对于二维绘图...

函数不再适用,因为图像会除非从正上方(即沿 z 轴正方向)查看轴,否则不会显示.在这种情况下,您必须使用 SURF 函数和纹理将图像映射到它上面.举个例子:

[xSphere,ySphere,zSphere] = sphere(16);%# 球体上的点scatter3(xSphere(:),ySphere(:),zSphere(:),'.');%# 绘制点轴相等;%# 使坐标轴刻度匹配坚持,稍等;%# 添加到绘图中xlabel('x');ylabel('y');zlabel('z');img = imread('peppers.png');%# 加载示例图像xImage = [-0.5 0.5;-0.5 0.5];%# 图像角的 x 数据yImage = [0 0;0 0];%# 图像角点的 y 数据zImage = [0.5 0.5;-0.5 -0.5];%# 图像角点的 z 数据surf(xImage,yImage,zImage,... %# 绘制曲面'CData',img,...'人脸颜色','纹理贴图');

请注意,此表面在空间中是固定的,因此当您旋转轴时,图像并不总是直接面对相机.如果您希望纹理贴图表面自动旋转,使其始终垂直于相机的视线,这是一个复杂得多的过程.

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


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

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.

这篇关于如何在 MATLAB 中以 2-D 和 3-D 形式绘制图像 (.jpg)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆