2017-02-19 115 views
0

在不同的线程,我发现这段代码,以使该中有一个梯度(plot circle with gradient gray scale color in matlab)一个圆圈:创建一个圆圈用梯度

N = 200; %// this decides the size of image 
[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 
padsize = 50; %// decides the boundary width 
out = padarray(out,[padsize padsize],0); 
figure, imshow(out) %// show image 

现在我想更换梯度一个固定的递减值向量,以便每个半径都有它自己的值。

预先感谢这个

+1

那么做*你*做了吗?你只是发布别人的作品。另外,请问一个问题,你没有这样做。英文问题用问号表示,并且可以收到答案。请阅读[问]。 – Adriaan

+0

请提供您提到的其他线索的链接。 – Shai

+0

添加链接。对不起,我是新来的,仍然在学习...... – KayPi

回答

1

这里是从矢量替换值的元素一个优雅的方式:

假设你的载体是:V = 283:-1:0
我使用降序进行演示。
值283是sqrt(2)*N(应该是边界平方中的最大半径)。从原来的职位

代码差异:

  1. 鸿沟out通过max(out(:)) - 出的范围设定为[0, 1]
  2. 将长度乘以V(减1) - 将范围设置为[0, length(V)-1]
  3. 使用圆而不是uint8(转换为uint8夹值为255)。
  4. 使用向量V作为查找表 - 用值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拍摄。

enter image description here

0

任何帮助,尝试

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 
+0

谢谢!我想我没有很好地描述这个问题。我正在尝试根据长度=圆半径的矢量进行渐变。它的工作方式是用矢量中的值替换由中心某一偏心度的所有值。但它的代码看起来不太优雅... – KayPi