Matlab 创建具有渐变的圆
在另一个线程中,我发现了这段代码,用于生成一个带有渐变的圆(): 现在我想用一个固定的递减向量替换这个梯度,这样每个半径都有它自己的值 提前感谢您在此方面提供的任何帮助请尝试Matlab 创建具有渐变的圆,matlab,Matlab,在另一个线程中,我发现了这段代码,用于生成一个带有渐变的圆(): 现在我想用一个固定的递减向量替换这个梯度,这样每个半径都有它自己的值 提前感谢您在此方面提供的任何帮助请尝试 R = sqrt(X.^2+Y.^2); out = uint8(255*R); padsize = 50; %// decides the bounary width out = padarray(out,[padsize padsize],0); figure, imshow(out) %// show image
R = sqrt(X.^2+Y.^2);
out = uint8(255*R);
padsize = 50; %// decides the bounary width
out = padarray(out,[padsize padsize],0);
figure, imshow(out) %// show image
以下是一种用向量值替换元素的优雅方法: 假设向量为:
V=283:-1:0
我使用降序值进行演示。
值283为
sqrt(2)*N
(假定为边界正方形中的最大半径)
与原始帖子的代码差异:
out
除以max(out(:)
-将out的范围设置为[0,1]
V的长度(减1)-将out的范围设置为[0,长度(V)-1]
uint8
(转换为uint8
将值钳制为255
)V
作为查找表-将out的每个元素替换为V
的值,以代替值N = 200; %// this decides the size of image
%V = round(sqrt(2)*N):-1:0;
%Assume this is your vector.
V = 283:-1:0;
[X,Y] = meshgrid(-1:1/N:1, -1:1/N:1) ;
nrm = sqrt(X.^2 + Y.^2);
%out = uint8(255*(nrm/min(nrm(:,1)))); %// output image
%1. Divide by max(out(:)) - set the range of out to [0, 1].
%2. Multiply by length of V (minus 1) - set the range of out to [0, length(V)-1].
%3. Use round instead of uint8 (converting to uint8 clamps value to 255).
out = nrm/min(nrm(:,1));
out = round(out/max(out(:)) * (length(V)-1));
%4. Use vector V as a Look Up Table - replace each elements of out with value of V in place of the value.
out = V(out+1);
padsize = 50; %// decides the boundary width
out = padarray(out,[padsize padsize],0);
%figure, imshow(out) %// show image
figure, imagesc(out);impixelinfo;colormap hsv %// use imagesc for emphasis values.
如您所见,值取自向量V
那么你做了什么?你只是在发布别人的作品。另外,请问一个问题,你没有这样做。英语中的问题用问号表示,可以收到答案。请阅读。请提供指向您提到的其他线程的链接。已添加链接。对不起,我是新来的,还在学习……你能把你的“价值向量”添加到你的帖子里吗?谢谢!我想我没有很好地描述这个问题。我试着根据一个向量做梯度,向量的长度=圆的半径。它的工作原理是用向量中的值替换从中心到某个偏心距的所有值。但是代码看起来不是很优雅。。。
N = 200; %// this decides the size of image
%V = round(sqrt(2)*N):-1:0;
%Assume this is your vector.
V = 283:-1:0;
[X,Y] = meshgrid(-1:1/N:1, -1:1/N:1) ;
nrm = sqrt(X.^2 + Y.^2);
%out = uint8(255*(nrm/min(nrm(:,1)))); %// output image
%1. Divide by max(out(:)) - set the range of out to [0, 1].
%2. Multiply by length of V (minus 1) - set the range of out to [0, length(V)-1].
%3. Use round instead of uint8 (converting to uint8 clamps value to 255).
out = nrm/min(nrm(:,1));
out = round(out/max(out(:)) * (length(V)-1));
%4. Use vector V as a Look Up Table - replace each elements of out with value of V in place of the value.
out = V(out+1);
padsize = 50; %// decides the boundary width
out = padarray(out,[padsize padsize],0);
%figure, imshow(out) %// show image
figure, imagesc(out);impixelinfo;colormap hsv %// use imagesc for emphasis values.