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

前端 未结 1 1261
一生所求
一生所求 2020-12-03 01:34

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 pl

相关标签:
1条回答
  • 2020-12-03 02:27

    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.

    0 讨论(0)
提交回复
热议问题