• MATLAB在三维坐标中显示图片 并 使得图片部分透明


    要画一个光路图,本来可以用proe,但是鼠标不好用,有些操作也忘了,用MATLAB画了个。下面是用到的图片。

    但是三维坐标中显示彩色图片的目标没有搞定,做了个灰度图,然后用仿射程序将彩色图片贴到了二维灰度图中。


    clear all;clc;close all;
    
    im1 = rgb2gray(imread('F:sup.jpg'));% 掩模,第一张图,白色的部分在后面的surf中透明化了
    im1 = imresize(im1,[64 64]);
    
    im2 = rgb2gray(imread('F:im3.jpg'));% 样品,第二张图
    im2(:,end) = [];
    im2 = imresize(im2,[64,64]);
    im2 = flipud(im2);
    
    
    im3 = rgb2gray(imread('F:diff.jpg'));% 衍射图样,第三张图
    im3(513:end,:) = [];
    im3(:,513:end) = [];
    im3 = flipud(im3);
    
    % figure;
    % subplot(121);imshow(im1);
    % subplot(122);imshow(im4);
    mask = PIEmask(256,200,128,128);
    mask = imresize(mask,[64 64]);
    %%
    figure('color','white');
    
    N = size(im3);
    [x z] = meshgrid(-N/2:N/2-1);
    y = 0*ones(N,N);
    a = surf(x,y,z,  double(im3) );   % 画衍射图
    set(a,'linestyle','none');  %隐藏网格
    colormap(gray);
    
    hold on;
    N = size(im2);
    [x z] = meshgrid(-N/2:N/2-1);
    y = -200*ones(N,N);
    b = surf(x,y,z,  double(im2) );  % 画样品图
    set(b,'linestyle','none');  %隐藏网格
    colormap(gray);
    
    hold on;
    N = size(im1);
    [x z] = meshgrid(-N/2:N/2-1);
    y = -210*ones(N,N);
    % c = surf(x,y,z,  double(im1) );
    c = surf(x,y,z,  double(im1),'FaceAlpha','flat','AlphaDataMapping','scaled','AlphaData',1-mask);  % 画掩模图,并将中间部分透明化
    % alpha(0.5)
    set(c,'linestyle','none');  %隐藏网格
    colormap(gray);
    %%  后面的部分就是画红色的‘光’了
    [yy zz] = meshgrid(-500:-210,-32:32);
    xx = 32*ones(size(yy));
    
    p = surf(xx,yy,zz,double(255*ones(size(zz))),'FaceAlpha','flat',...
    'AlphaDataMapping','scaled',...
    'AlphaData',0.2*ones(size(zz)),...
    'FaceColor','red');
    set(p,'linestyle','none');  %隐藏网格
    
    p = surf(-xx,yy,zz,double(255*ones(size(zz))),'FaceAlpha','flat',...
    'AlphaDataMapping','scaled',...
    'AlphaData',0.2*ones(size(zz)),...
    'FaceColor','red');
    set(p,'linestyle','none');  %隐藏网格
    %==========================================
    [xx yy] = meshgrid(-32:32,-500:-210);
    zz = 32.*ones(size(xx));
    
    p = surf(xx,yy,zz,double(255*ones(size(zz))),'FaceAlpha','flat',...
    'AlphaDataMapping','scaled',...
    'AlphaData',0.2*ones(size(zz)),...
    'FaceColor','red');
    set(p,'linestyle','none');  %隐藏网格
    
    p = surf(xx,yy,-zz,double(255*ones(size(zz))),'FaceAlpha','flat',...
    'AlphaDataMapping','scaled',...
    'AlphaData',0.2*ones(size(zz)),...
    'FaceColor','red');
    set(p,'linestyle','none');  %隐藏网格
    %%
    t = 0:0.01:2*pi;
    x = 25*cos(t');x = repmat(x,1,300);
    z = 25*sin(t');z = repmat(z,1,300);
    
    y = linspace(-210,-200,300);
    y = repmat(y,629,1);
    
    p = surf(x,y,z,x*0,'FaceAlpha','flat',...
    'AlphaDataMapping','scaled',...
    'AlphaData',0.5*ones(size(z)),...
    'FaceColor','red');
    set(p,'linestyle','none');  %隐藏网格
    %%
    s1 = -25*sqrt(2)/2;
    s2 = -256;
    t1 = linspace(s1,s2,100);
    x = [];
    t2 = linspace(-s1,-s2,100);
    for k = 1:100;
        tmp = (linspace(t1(k),t2(k),200))';
        x = [x tmp];
    end
    
    y = linspace(-200,0,100);
    y = repmat(y,200,1);
    z = linspace(25*sqrt(2)/2,255,100);
    z = repmat(z,200,1);
    
    p = surf(x,y,z,x*0,'FaceAlpha','flat',...
    'AlphaDataMapping','scaled',...
    'AlphaData',0.2*ones(size(z)),...
    'FaceColor','red');
    set(p,'linestyle','none');  %隐藏网格
    
    p = surf(x,y,-z,x*0,'FaceAlpha','flat',...
    'AlphaDataMapping','scaled',...
    'AlphaData',0.2*ones(size(z)),...
    'FaceColor','red');
    set(p,'linestyle','none');  %隐藏网格
    
    p = surf(z,y,x,x*0,'FaceAlpha','flat',...
    'AlphaDataMapping','scaled',...
    'AlphaData',0.15*ones(size(z)),...
    'FaceColor','red');
    set(p,'linestyle','none');  %隐藏网格
    
    p = surf(-z,y,x,x*0,'FaceAlpha','flat',...
    'AlphaDataMapping','scaled',...
    'AlphaData',0.15*ones(size(z)),...
    'FaceColor','red');
    set(p,'linestyle','none');  %隐藏网格
    %%
    
    grid off;
    axis off;
    
    view([150 -256 50]);
    % view([150 -100 0]);
    daspect([10,3,10]);%调节坐标轴比例
    

     效果如下

    之后又用了个仿射程序把衍射图样彩色化了

  • 相关阅读:
    判断是否是微信浏览器
    弹性盒模型
    一个发光的搜索边框(纯CSS3)
    小练习
    js控制div是否显示
    遮罩弹窗
    布局
    CSS构造表单
    CSS 滤镜(IE浏览器专属其他浏览器不支持)
    Css中光标,DHTML,缩放的使用
  • 原文地址:https://www.cnblogs.com/shepherd2015/p/5402070.html
Copyright © 2020-2023  润新知