creating an image using a matrix

5 ビュー (過去 30 日間)
Raldi
Raldi 2011 年 12 月 8 日
lets say we have a 50x50 matrix. All of its values are zero. I wanted to create a way to add ones only to those places of the matrix that would create a sircle if i used imshow(). So i thought if yi,yj are the coordinates for each cell i maybe could use a function like (i-yi)^2+(j-yj)^2=r^2 to find each cell that i need. Does anyone has any ideas on that or maybe a better way?

採用された回答

Sven
Sven 2011 年 12 月 8 日
Raldi, try this:
% Define the image size and where you want the circle
rows = 50;
cols = 50;
radius = 20;
center = [25 25]; % In [X,Y] coordinates
% Make the circle
[xMat,yMat] = meshgrid(1:cols,1:rows);
distFromCenter = sqrt((xMat-center(1)).^2 + (yMat-center(2)).^2);
circleMat = distFromCenter<=radius;
figure, imshow(circleMat)
UPDATE to match your comments:
% To get just the border
B = circleMat & ~bwmorph(circleMat,'erode',1);
figure, imshow(B)
  4 件のコメント
Raldi
Raldi 2011 年 12 月 8 日
ok did it thanks Sven
Sven
Sven 2011 年 12 月 8 日
Cool, hit "accept" if it's answered then :)

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

その他の回答 (3 件)

Sean de Wolski
Sean de Wolski 2011 年 12 月 8 日
Or:
r = 11; %radius
cx = 25; %xcenter
cy = 25;
sz = 50; %mat size
C = bsxfun(@(x,y)hypot(x-cx,y-cy)<=r,(1:sz)',1:sz);
imshow(C)
  1 件のコメント
Raldi
Raldi 2011 年 12 月 8 日
Thanks Sean, with a few modifications i did what i wanted.

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


Andrei Bobrov
Andrei Bobrov 2011 年 12 月 8 日
A = single(zeros(50));
A(ceil(m/2),ceil(n/2)) = 1
d = bwdist(A);
r = 20;
A(abs(d-r)<.5)=1;
imagesc(A)
  1 件のコメント
Raldi
Raldi 2011 年 12 月 8 日
thanks Andrei.

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


Alex Taylor
Alex Taylor 2011 年 12 月 8 日
The createMask method of the ROI tools was designed for the case in which you want to generate a binary image that is one where a specific ROI tool lies over an image or axes.
For example:
imshow(zeros(50));
h = imellipse(gca,[5 5 40 40]);
mask = h.createMask();
imshow(mask)
You can interactively or programmatically (as done above) any of the ROI tools, and then call the createMask method. In the case above, imellipse expects the bounding rectangle of an ellipse as the position argument. Don't know if that helps, but I thought it might be relevant to this problem.

カテゴリ

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