2017-02-19 110 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