How can I draw a filled circle?

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?

3 件のコメント

Bhalli7195
Bhalli7195 2016 年 9 月 15 日
simply drop in some ink into your blank circle....
Praveen Pawar
Praveen Pawar 2017 年 1 月 9 日
what a great idea You are genius...
abdalaziz alkassm
abdalaziz alkassm 2020 年 7 月 17 日
Using fill(x,y,'b')

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

 採用された回答

Walter Roberson
Walter Roberson 2013 年 9 月 13 日

8 投票

6 件のコメント

Paul Martin
Paul Martin 2021 年 3 月 23 日
Brilliant! I've been slavishly drawing circles as line segments with (sin, cos) functions for years. I'd never have stumbled on this function. Thank you Walter Roberson.
Walter Roberson
Walter Roberson 2021 年 3 月 24 日
see also viscircles from Image Processing Toolbox
Image Analyst
Image Analyst 2021 年 3 月 24 日
I don't believe viscircles() offers a 'filled' option yet to make the entire disc one solid filled color. That would be a good enhancement. I believe it only draws the perimeter.
Will Reeves
Will Reeves 2022 年 2 月 15 日
Yes please.. + 1 for that request. please implement "FaceColor" for a viscircles property! I need to draw thousands of circles in a plot...
Will Reeves
Will Reeves 2023 年 6 月 21 日
Also, just for completness.. Is there a "quality setting" somewhere for rectangle (when curvature=1) that draws a circle instead of a load of straight lines?
for example:
DGM
DGM 2025 年 7 月 9 日
As far as I know, rectangle() will use 4 vertices when curvature is 0, and 100 vertices otherwise. There is no resolution setting.

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

その他の回答 (7 件)

Shahriar
Shahriar 2013 年 11 月 9 日

11 投票

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)

6 件のコメント

Sathish Kumar
Sathish Kumar 2014 年 5 月 2 日
Good idea. But zooming in the plot will not have any effect on the size of the marker.
Gavin Walch
Gavin Walch 2019 年 11 月 8 日
Thank you! I found this solution to be the best(and simplest) solution for my need
Patriek Bruurs
Patriek Bruurs 2022 年 10 月 6 日
I've trouble with this solution as the you cannot control directly the radius of the circle. It's even worse if you zoom or change the size of the plot as the size of the marker on screen remains the same.
Image Analyst
Image Analyst 2022 年 10 月 6 日
@Sathish Kumar and @Patriek Bruurs, that's correct. The marker size is relative to your screen or figure size or something. It is not relative to the scale used for your x and y axes. So zooming in and out will probably not change the marker size, and the marker size has nothing at all to do with the spatial calibration scale of your x and y. So for example if you wanted a big spot you'd use a markersize of, say, 100 regardless if your x and y axes went from 0-1 or from 0 to a billion. The marker size is NOT the diameter of the spot in units of your graph. So if your axis went from 0 to a billion you'd still use a marker size of 100, not a marker size of 30 million. Also zooming in or out of the axes does not change the marker size - you can imagine scenarios where if it did, it would be a disaster, with the marker covering up some small region you wanted to scrutinize.
Walter Roberson
Walter Roberson 2022 年 10 月 7 日
I did some measurements on my screen last night. To within measurement error, the spot size produced by plot() grew linearly -- for example spot size 1000 was twice as large as spot size 500. But I was not able to figure out what the units were; the units did not make sense in inches or mm or points. For example marker size 1000 was approximately 66.4 mm diameter -- between 66.1 and 66.8 (I cannot get my digital calipers flat on the screen and it is a judgement call about where exactly the bounds are.) (Admittedly, due to measurement error, it just might be the case that the spot size is slightly nonlinear.)
My display is a larger one, but is below the bounds at which MATLAB starts lying about resolutions
Md. Zubaer Ahammed
Md. Zubaer Ahammed 2025 年 7 月 8 日
Thank you. It helped me.

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

Image Analyst
Image Analyst 2013 年 9 月 13 日

6 投票

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');

10 件のコメント

Image Analyst
Image Analyst 2013 年 9 月 13 日
vantankhah's "Answer" moved here to this comment, since he was referring to the code I posted.
Sorry but I couldn't understand that code, could you please give me a code by an example. how can I use rectangle() command to fill a circle?
Image Analyst
Image Analyst 2013 年 9 月 13 日
You never did look in the help at the examples, did you. OK, here it is:
rectangle('Position',[1,2,5,5],...
'Curvature',[1,1], 'FaceColor','r')
axis square;
R G
R G 2017 年 6 月 7 日
it works and I don't know why, anyway, I love you so much !!!!
Image Analyst
Image Analyst 2017 年 6 月 7 日
If you have the Image Processing Toolbox, there is now a function called viscircles() that you may find helpful in displaying circles if you have a list of centers and radii.
Gerhard Schreiner
Gerhard Schreiner 2020 年 1 月 20 日
@Image Analyst
Unfortunately there's no option to draw filled circles...
Image Analyst
Image Analyst 2020 年 1 月 20 日
Gerhard, if you want to put a filled circle in the overlay, use plot(x, y, 'ro', 'MarkerSize', 100) or rectangle().
If you want to burn it into the image then see the FAQ.
Manuel Lorite
Manuel Lorite 2020 年 1 月 23 日
I have also the same problem, I have a list of centers (2 columns) and another of radii that I have obtained from an image. Now, I want to create the same image but just with the filled circles that I have already obtained
Image Analyst
Image Analyst 2020 年 1 月 23 日
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.
Sharne Fernandes
Sharne Fernandes 2022 年 8 月 21 日
@Image Analyst, could you suggest to me a way of adding more than 1 circles using these steps?
Thank you
Walter Roberson
Walter Roberson 2022 年 8 月 23 日
% 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 = [100 180 300];
centerY = [150 30 400];
radius = [100 20 50];
circlePixels = any((rowsInImage(:) - centerY).^2 ...
+ (columnsInImage(:) - centerX).^2 <= radius.^2, 2);
circlePixels = reshape(circlePixels, imageSizeY, imageSizeX);
% circlePixels is a 2D "logical" array.
% Now, display it.
image(circlePixels) ;
colormap([0 0 0; 1 1 1]);
hold on
scatter(centerX, centerY, 'r+');
hold off
title('Binary image of circles');

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

Chad Greene
Chad Greene 2014 年 11 月 20 日

4 投票

A few FEX submissions make it easy:
circles(x,y,radius,'color','black')

2 件のコメント

Stevie P
Stevie P 2021 年 2 月 6 日
This is the easiest and most versatile answer!
Image Analyst
Image Analyst 2021 年 2 月 6 日
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
ABDULRAHMAN HAJE KARIM ALNAJAR 2018 年 8 月 8 日
編集済み: Walter Roberson 2019 年 11 月 10 日

1 投票

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.

1 件のコメント

Walter Roberson
Walter Roberson 2019 年 11 月 10 日
This is a good routine to use if you have a matrix that you want to draw a circle into. It is not, however, a good routine to draw a circle on the display.

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

vatankhah
vatankhah 2013 年 9 月 13 日
編集済み: vatankhah 2013 年 9 月 13 日

0 投票

thanks for the answer but I want just a simple command in matlab to fill a circle that is plotted with specified radius and center coordinate

3 件のコメント

Image Analyst
Image Analyst 2013 年 9 月 13 日
That's what rectangle() does, believe it or not. Look at the examples in the link Walter gave you.
Will Reeves
Will Reeves 2023 年 6 月 21 日
編集済み: Will Reeves 2023 年 6 月 21 日
... but it seems as if this is fairly course angular resolution and are therefore not really circles. Please can this adjusted in a future release so that you can chose the angular precision?
Image Analyst
Image Analyst 2023 年 6 月 21 日
@Will Reeves remember we're dealing with digital computers that use discrete values and don't have infinite precision. If you're really going to zoom way, way in, so much so that the circle has "kinks" in it because it's made up as very short line segments, you can make those line segments as small as you want by increasing the angular resolution in the code in the FAQ:

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

vatankhah
vatankhah 2013 年 9 月 14 日

0 投票

thanks a lot for your help, it was useful
Anton
Anton 2014 年 9 月 24 日
編集済み: Walter Roberson 2019 年 11 月 10 日

0 投票

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.

1 件のコメント

Walter Roberson
Walter Roberson 2019 年 11 月 10 日
? rectangle() uses data units, which are axes relative. It does not use figure units.
annotation() uses figure units though.

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

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

質問済み:

2013 年 9 月 13 日

コメント済み:

DGM
2025 年 7 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by