gpt4 book ai didi

matlab - 在 MATLAB 中以一种颜色绘制 2D 高斯,但透明度级别不同

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:32 26 4
gpt4 key购买 nike

我想在 MATLAB 中绘制高斯波函数的二维表示。我希望 2D 图为一种颜色(绿色),远离高斯中心变得透明。

当我使用 imagesc 时(就像在下一个代码中一样),我在黑色方 block 上得到了一个高斯分布(如下图所示)。

我不想要黑色背景,我希望高斯是一种颜色,但远离中心变得透明,这样我就不会得到黑色方 block ,只有绿色圆圈(在白色背景上)圆从中心变透明的地方。

我该怎么做???

close all;clc

figure
xlim_min=-4;
xlim_max=4;
ylim_min=-4;
ylim_max=4;

ylim([ylim_min ylim_max])
xlim([xlim_min xlim_max])

x=-1:0.001:1;
y=-1:0.001:1;
[X,Y]=meshgrid(x,y);

c1=10;
c1_new=c1*0.3;
x_offset=0;
y_offset=0;

w_function=0.5*0.25*exp(-c1_new*((X+x_offset).^2+...
(Y+y_offset).^2));

imagesc(x,y,w_function);

ylim([ylim_min ylim_max])
xlim([xlim_min xlim_max])

enter image description here

最佳答案

要获得透明度,请使用 AlphaData and AlphaDataMapping properties的图像。在下文中,我明确计算透明度 (alpha) 作为数据值的仿射函数,以指定最小和最大透明度值;并且我将 AlphaDataMapping 设置为 none,以便在不进行任何修改的情况下使用这些值。

所以,更换你的线路

imagesc(x,y,w_function);

通过

min_alpha = .2; % desired minimum alpha
max_alpha = 1; % desired maximum alpha
alpha = min_alpha + (max_alpha-min_alpha)/max(w_function(:))*w_function; % compute alpha
imagesc(x,y,w_function,'AlphaData',alpha,'AlphaDataMapping','none'); % image with alpha

注意蓝色部分比黄色部分更透明(更小的 alpha)。

enter image description here


要使颜色与外部区域混合平滑,请设置 0 的最小 alpha 并在更大的网格上定义函数。如果需要,您还可以更改颜色图。由于透明度已经发生变化,您可能希望颜色图中的颜色保持不变。

close all;clc

figure
xlim_min=-4;
xlim_max=4;
ylim_min=-4;
ylim_max=4;

ylim([ylim_min ylim_max])
xlim([xlim_min xlim_max])

x=-2:0.001:2; % large enough that the function approximately ...
y=-2:0.001:2; % ... reaches 0 within this rectangle
[X,Y]=meshgrid(x,y);

c1=10;
c1_new=c1*0.3;
x_offset=0;
y_offset=0;

w_function=0.5*0.25*exp(-c1_new*((X+x_offset).^2+...
(Y+y_offset).^2));

min_alpha = 0; % desired minimum alpha: set to 0
max_alpha = 1; % desired maximum alpha
alpha = min_alpha + (max_alpha-min_alpha)/max(w_function(:))*w_function; % compute alpha
imagesc(x,y,w_function,'AlphaData',alpha,'AlphaDataMapping','none'); % image with alpha
cm = [0 .7 0]; % define colormap: single color, dark green
colormap(cm) % apply colormap

ylim([ylim_min ylim_max])
xlim([xlim_min xlim_max])

enter image description here

关于matlab - 在 MATLAB 中以一种颜色绘制 2D 高斯,但透明度级别不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47285775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com