How do i draw a filled circle in a color matrix? (random color and size)

A have a gray square of 500x500px.
whiteback=ones(500);
gray=128;
back=gray*whiteback;
How do a draw a circle of random grayscale and random size somewhere (random) in my matrix (square)? The whole circle needs to be filled. Not just a line.
Thx

 Accepted Answer

7 Comments

yes. And I don't see anything in the question that the faq doesn't answer.
I cannot use this, this creates a meshgrid, i have a matrix of 500x500px. I also need to add a circle when its picture of 500x500px.
And i need to change the color of the circle.
Using the code shown first in that FAQ, and assuming the existing matrix is named YourSquare, and the grayscale has been stored in YourColor:
YourSquare(circlePixels) = YourColor;
meshgrid is used only for the calculation of coordinates, not for generating the matrix itself.
If it makes you feel better, use bsxfun
xL = size(back,1); yL = size(back,2);
Xcenter = 1 + floor(xL * rand());
Ycenter = 1 + floor(yL * rand());
ThisColor = floor(256 * rand());
ThisRadius = max(xL,yL) / 2 * rand();
circlePixels = bsxfun(@(x,y) (x - Xcenter).^2 + (y - YCenter).^2 <= ThisRadius.^2, (1:xL).', 1:yL);
back(circlePixels) = ThisColor;
There is no essential difference between this and the code in the FAQ: using bsxfun() is sometimes computationally faster than using meshgrid() but they work very nearly the same way.
It gives me this error:
??? Undefined function or variable 'YCenter'.
Error in ==> @(x,y)(x-Xcenter).^2+(y-YCenter).^2<=ThisRadius.^2
Error in ==> OPDRACHT at 15
circlePixels = bsxfun(@(x,y) (x - Xcenter).^2 + (y - YCenter).^2 <=
ThisRadius.^2, (1:xL).', 1:yL);
I'm kind of a noob :P. But i'll try to figure it out myself

Sign in to comment.

More Answers (1)

You can try adapting my circle demo. It puts up a bunch of circles, but of course you can adapt it so that it displays only one by taking out the for loop. To change the color, you can use a colormap on a grayscale image, or you can convert it to a color image with cat(3,r,g,b) where you've set the circle in each color plane to be the intensity that it needs to have for that color plane.
% M-file to place multiple small circles inside a big circle.
% Clean up
close all;
clc;
% Initialize some parameters.
numberOfSmallCircles = 25; % Number of small circles
smallCircleOutsideValue = 0.2;
smallCircleInsideValue = 0.8;
smallCircleRadius = 25; % small circle radius
bigImageWidth = 500;
bigImageHeight = 500; % square area 0f 500*500
bigCircleRadius = 250; % big circle radius
% Initialize an image to hold one single big circle.
bigCircleImage = zeros(bigImageHeight, bigImageWidth);
[x, y] = meshgrid(1:bigImageWidth, 1:bigImageHeight);
bigCircleImage((x - bigImageWidth/2).^2 + (y - bigImageHeight/2).^2 <= bigCircleRadius.^2) = 1;
% Display it in the upper left plot.
subplot(3,2,1);
imshow(bigCircleImage, []);
title('Big Circle Mask');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Initialize an image to hold one single small circle.
smallCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
[x, y] = meshgrid(1:smallCircleRadius*2, 1:smallCircleRadius*2);
singleCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
singleCircleImage((x - smallCircleRadius).^2 + (y - smallCircleRadius).^2 <= smallCircleRadius.^2) = smallCircleInsideValue;
% Display it in the upper right plot.
subplot(3,2,2);
imshow(singleCircleImage, []);
title('Single Small Circle (scaled to fit)');
singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
% Get random coordinates in the big image where
% we will place the upper left corner of the small circle.
widthThatWillFit = bigImageWidth - 2 * smallCircleRadius;
heightThatWillFit = bigImageHeight - 2 * smallCircleRadius;
smallUpperLeftX = widthThatWillFit * rand(numberOfSmallCircles, 1);
smallUpperLeftY = heightThatWillFit * rand(numberOfSmallCircles, 1);
% Initialize an output image to hold many small overlapping circles.
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
% Place the small circles one by one.
for k = 1 : numberOfSmallCircles
% Find the square in the big image where we're going to add a small circle.
x1 = int16(smallUpperLeftX(k));
y1 = int16(smallUpperLeftY(k));
x2 = int16(x1 + singleWidth - 1);
y2 = int16(y1 + singleHeight - 1);
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
end
% Make outside the circles the outside color.
manySmallCircles(manySmallCircles == 0) = smallCircleOutsideValue;
% Display it in the lower left plot.
subplot(3,2,3);
imshow(manySmallCircles);
title('Many Small Overlapping Circles');
% Multiply the big circle mask by the many small circles image to clip
% those small circles that lie outside the big circle.
maskedByBigCircle = bigCircleImage .* manySmallCircles;
% Display it in the lower right plot.
subplot(3,2,4);
imshow(maskedByBigCircle);
title('Many Small Circles Masked by Big Circle');
% Take the histogram and display it in the bottom row.
subplot(3,2,5);
hist(maskedByBigCircle(:));

4 Comments

I'm trying to make somethink of this code.
But i can't, its to complicated.
I'm to big of a noob.
Is it possible to give me just to part where it makes a circle within my matrix?
Sure, just look at my code posted up above. I even used your variable names so as not to confuse you.
Sorry about that. It's really not that complicated and has code from the FAQ in there, and has lots of comments and very descriptive variable names so I thought you'd be able to follow it. The essential code are the lines after the comment "% Initialize an image to hold one single big circle." Or you can use Walter's code. But even your code will get complicated after that because I'm sure that's not the entire project - just displaying a circle. I'm sure you're going to do something after that, which will require more lines of code. What is it?

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!