【问题标题】:How can I plot an image (.jpg) in MATLAB in both 2-D and 3-D?如何在 MATLAB 中以 2-D 和 3-D 绘制图像 (.jpg)?
【发布时间】:2011-04-12 18:44:28
【问题描述】:

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

我还将绘制一个 3-D 球体,我希望图像也显示在原点。

【问题讨论】:

    标签: image matlab 3d plot


    【解决方案1】:

    对于二维绘图...

    函数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
    


    对于 3-D 绘图...

    IMAGE 函数不再适用,因为除非从正上方(即从正 z 轴)查看轴,否则不会显示图像。在这种情况下,您必须使用SURF 函数创建一个 3-D 表面,并将图像纹理映射到其上。这是一个例子:

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

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

    【讨论】:

    • 很好的答案。我可以调整图像大小以适合散点图吗?
    • 如何将图像投影到 2D 平面上,同时以 3D 形式显示动态生成的数据?
    猜你喜欢
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多