2,245 views (last 30 days)

Show older comments

I know the radius and the center coordinate of a circle

I want to fill this circle with black color

how can I do this?

Walter Roberson
on 13 Sep 2013

Image Analyst
on 24 Mar 2021

Shahriar
on 9 Nov 2013

Very simple. The following will draw a filled circle at (1,1) with red color. Change it as you wish.

plot(1, 1, '.r', 'MarkerSize',69)

Gavin Walch
on 8 Nov 2019

Thank you! I found this solution to be the best(and simplest) solution for my need

Image Analyst
on 13 Sep 2013

To create a 2D logical image of a solid circle (a disc), you can use code like this:

% Create a logical image of a circle with specified

% diameter, center, and image size.

% First create the image.

imageSizeX = 640;

imageSizeY = 480;

[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);

% Next create the circle in the image.

centerX = 320;

centerY = 240;

radius = 100;

circlePixels = (rowsInImage - centerY).^2 ...

+ (columnsInImage - centerX).^2 <= radius.^2;

% circlePixels is a 2D "logical" array.

% Now, display it.

image(circlePixels) ;

colormap([0 0 0; 1 1 1]);

title('Binary image of a circle');

Image Analyst
on 23 Jan 2020

If it's a gray scale image, just use the code above to create a binary image with all the circle(s) on it, then set everywhere in the binary image mask to whatever gray level you want:

grayImage(binaryImage) = 255; % Set to white.

Chad Greene
on 20 Nov 2014

circles(x,y,radius,'color','black')

Image Analyst
on 6 Feb 2021

Now there is a viscircles function built in to the Image Processing Toolbox

viscircles([x, y], radius);

This can handle vectors of centers and radii, in addition to just one.

ABDULRAHMAN HAJE KARIM ALNAJAR
on 8 Aug 2018

Edited: Walter Roberson
on 10 Nov 2019

Simply, use the following command:

I = insertShape(I,'FilledCircle',[x y r],'color',[1 1 1],'LineWidth',5);

[x y] is the centre coordinates r is the radius

Note, you need Computer Vision Toolbox to run this command.

Walter Roberson
on 10 Nov 2019

vatankhah
on 13 Sep 2013

Edited: vatankhah
on 13 Sep 2013

Image Analyst
on 13 Sep 2013

That's what rectangle() does, believe it or not. Look at the examples in the link Walter gave you.

Anton
on 24 Sep 2014

Edited: Walter Roberson
on 10 Nov 2019

Use "area" command. Typically used to fill area under y=f(x) curve.

Using Rectangle command has 2 inconveniences: 1) They use figure axes, not the graph axes (see this submission for help http://www.mathworks.co.uk/matlabcentral/fileexchange/30347-sigplot/content/sigplot/sigplot/BasicFunctions/dsxy2figxy.m )

2) a consequence of 1 in fact: if you zoom or move the graph your circle will remain in "old" position and scale, which is annoying.

This is how you draw a filled circle of radius R at (x,y) in the axis of your graph using "area" command:

Ang = 0:0.01:2*pi; %angle from 0 to 2pi with increment of 0.01 rad. CircX=R*cos(Ang); CircY=R*sin(Ang);

h=area(X+CircX,Y+CircY);

set(h,'FaceColor',[.7 0 0]) %colour of your circle in RGB, [0 0 0] black; [1 1 1] white set(h,'LineStyle','none')

% unfortunately you have to remove the line: because circle is not a function strictly speaking. % "area" command tries to fill area below your function which leaves a line-artefact connecting your circle with X axis.

Walter Roberson
on 10 Nov 2019

? rectangle() uses data units, which are axes relative. It does not use figure units.

annotation() uses figure units though.

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

Start Hunting!