How do I draw overlapping circles wich change color?

1 次查看(过去 30 天)
It shoul look like this

回答(2 个)

Image Analyst
Image Analyst 2020-7-11
You can just create an rgb image, and write each circle into the color plane of your choice
redChannel = 255 * ones(rows, columns, 'uint8');
greenChannel = 255 * ones(rows, columns, 'uint8');
blueChannel = 255 * ones(rows, columns, 'uint8');
% Now write the circles into whatever color channel you want.
% Use the FAQ https://matlab.fandom.com/wiki/FAQ#How_do_I_create_a_circle.3F
% Then concatenate them into an RGB image
rgbImage = cat(3, redChannel, greenChannel, blueChannel);

DGM
DGM 2022-4-24
Here's one way:
% image setup
sz = [600 600]; % image size
cim = sz/2; % [y x] center of image
r1 = 100; % radius of small circle
r2 = 150; % radius of large circle
% object positions (small circles)
Pth = [0 90 180 270]; % position angle
Pr = 150; % position radius
cx = Pr*cosd(Pth) + cim(2);
cy = sz(1)-(Pr*sind(Pth) + cim(1));
% create color layers
B = ones(sz);
for k = 1:numel(Pth)
B = B - drawhardcirc(sz,[cy(k) cx(k)],r1);
end
R = 1-drawhardcirc(sz,cim,r2);
G = 1-xor(R,B);
% note that we're working with complements,since the background is white
% the four small circles are negative features in B, not R
% the large circle is a negative feature in R, not B
imshow([R G B])
outpict = cat(3,R,G,B);
imshow(outpict)
% draw circle
function circ = drawhardcirc(sz,c,r)
xx = 1:sz(2);
yy = (1:sz(1)).';
circ = sqrt((xx-c(2)).^2 + (yy-c(1)).^2);
circ = circ <= r;
end
See also:

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by