generation of 2D array of circular ring

7 ビュー (過去 30 日間)
JK
JK 2023 年 7 月 8 日
編集済み: DGM 2023 年 7 月 8 日
I am looking for a way to generate 2D array of circular ring in retangular coordinate.
  1. outer diameter (D)
  2. inner diameter (d)
  3. value of the ring = 1
  4. outside of D & inside of d = 0
  5. center of the ring = (x,y)
thanks,
John

採用された回答

DGM
DGM 2023 年 7 月 8 日
編集済み: DGM 2023 年 7 月 8 日
In this case, I'm going to do an antialiased image instead of a binary image.
sz = [300 400]; % image size [y x]
c = [150 200]; % circle center [y x]
dmin = 175; % diameters
dmaj = 250;
maskinner = drawcircle(sz,c,dmin/2);
maskouter = drawcircle(sz,c,dmaj/2);
annmask = maskouter.*(1-maskinner);
imshow(annmask,'border','tight')
% draw smooth circle
function circ = drawcircle(sz,c,r)
xx = 1:sz(2);
yy = (1:sz(1)).';
circ = sqrt((xx-c(2)).^2 + (yy-c(1)).^2); % no quick tricks this time
circ = min(max((1+r-circ)/2,0),1);
end

その他の回答 (1 件)

Jayant
Jayant 2023 年 7 月 8 日
Here are the steps how you can write a function which takes D, d, x, y, width and height as its input and gives a 2D array as output.
  1. Create a ringArray of (width, height) dimesion.
  2. Traverse the ringArray. For each element (i, j), calculate the euclidean distance from the centre(x,y).
  3. If the distance>=d or <=D, the assign 1, else assign 0.
  4. The function returns the ringArray which is the required 2D array.
  1 件のコメント
DGM
DGM 2023 年 7 月 8 日
編集済み: DGM 2023 年 7 月 8 日
No loops necessary.
sz = [300 400]; % image size [y x]
c = [150 200]; % circle center [y x]
dmin = 175; % diameters
dmaj = 250;
xx = 1:sz(2);
yy = (1:sz(1)).';
rr = (xx-c(2)).^2 + (yy-c(1)).^2; % squared euclidean distance
annmask = (rr <= (dmaj/2)^2) & (rr >= (dmin/2)^2); % compare against square radii
imshow(annmask,'border','tight')
The bit with using squared distance simply avoids doing an expensive sqrt() on the entire array, so it's an easy way to make simple comparisons like this faster.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by