How to draw a circle with a red annulus and green center?

6 ビュー (過去 30 日間)
M
M 2016 年 8 月 3 日
コメント済み: M 2016 年 8 月 4 日
I'm trying to draw a circle with a red annulus and green center using RGB in MATLAB R2014b. However, since I'm currently drawing the red circle first, then layering the smaller green circle over it, the green turns to yellow. How do I either prevent the colors from adding (so the green shows as green) or draw the red annulus only (instead of the full circle)?
xpix = 1140;
ypix = 912;
rad = 150;
RGB(1:xpix,1:ypix,1:3)=0;
[x y] = find(RGB==0);
xc = ceil(xpix/2);
yc = ceil(ypix/2);
for i=1:20
radius = (rad-20*i).^2;
r = find(((x-xc).^2+(y-yc).^2) <=radius);
for j=1:size(r,1)
if(mod(i,2)==0)
RGB(x(r(j)),y(r(j)),1)=255; % 1=red
elseif (mod(i,10)==1)
RGB(x(r(j)),y(r(j)),2)=255; % 2=green
end
end
end
imshow(RGB);
imwrite(RGB,'filter_1.jpg','BitDepth',8);
This is what I currently have:
  1 件のコメント
Image Analyst
Image Analyst 2016 年 8 月 4 日
Explain what you're doing. What is the point with the mod() calls? What's with the for loops?

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

採用された回答

Robert
Robert 2016 年 8 月 4 日
How about a simpler implementation without the loops:
xpix = 1140;
ypix = 912;
rad = 150;
% Use zeros to initialize RGB. No risk of modifying an existing RGB. Use
% ypix for the rows and xpix for the columns to make x the horizontal size
% and y the vertical size.
RGB = zeros(ypix,xpix,3,'uint8');
[x,y] = meshgrid(1:xpix,1:ypix);
xc = (xpix+1)/2;
yc = (ypix+1)/2;
r = sqrt((x-xc).^2+(y-yc).^2);
ii = r <= rad/2; % Inside half our radius
RGB(:,:,2)=255*ii; % Green
ii = r <= rad & ~ii; % Inside our radius but not already made green
RGB(:,:,1)=255*ii; % Red
imshow(RGB);
  1 件のコメント
M
M 2016 年 8 月 4 日
Perfect - thanks so much Robert!

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by