Did you come here by searching for ‘How to draw a circle in MATLAB’ then definitely at the end of the article you will draw a circle.
Let me first discuss how to draw a circle and then we will see about concentric circles.
The equation of the circle: (x-x1).^2 +(y-y1).^2=r^2.
where x and y are the centre of the circle and r is the radius.
Here I am not using any plot function, just the equation of the circle.
Initialize the image with 255 and find the centre of the image. The variable ‘sz’ changes the size of the image. The image is a square matrix. Then ‘rad’ contains the radius of the circle.
sz=300;
rad=100;
clear RGB
RGB(1:sz,1:sz,1:3)=255;
I am storing the x co-ordinates in x and y co-ordinates in y.
[x y]= find(RGB==255);
‘xc’ and ‘yc’ contains the midpoint of the circle.
xc=ceil((sz+1)/2);
yc=ceil((sz+1)/2);
r=rad.^2;
Here I am finding the points which satisfy the equation. To avoid for loop I am using the ‘find’ function.
d=find(((x-xc).^2+(y-yc).^2) <= r);
The points that are stored in d contains the index value for x and y that satisfies the equation.
for i=1:size(d,1)
RGB(x(d(i)),y(d(i)),:)=0;
end
![]() |
Before edge detection |
The rest of the procedure is same. I found the edge and I strengthen the edge by dilating.
B=rgb2gray(RGB);
ED=edge(B);
SE=strel('disk',1);
cir=~imdilate(ED,SE);
figure,imshow(cir);
Concentric circles:
The procedure is same but I use loop to produce ‘n’ no.of circles.
for i=1:8
radius=(rad-10*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)),:)=255;
end
if(mod(i,3)==1)
RGB(x(r(j)),y(r(j)),:)=0;
end
end
end
![]() |
Before edge detection |
![]() |
final image |
If you find any difficulty in the code, Mail me, I will send the code.
10 comments:
Hello,
Thank you for this great tutorial.
You can simplify the rendering of the solution by replacing the loop for i=1:size(d,1) ... simply by: RGB(d)=0; since d already contains the index of the element at the intersection of x and y.
can I ask u?
really I need ur help?
how to fill circle with the color inside and outside with different color
how will i fill the circle with the different colors with inside as well as outside?
@Emmanual Robin
Use 'bwlabel' function and 'label2rgb' function.
how to find the centroid of a hand and its fingertip from the centroid ....can you help me n coding
how to draw a circle trigonometry around an image in matlab
hello how to find number of circles in annual tree rings
plz send me this code to detect concentric circles
mail id:shivushetty25@gmail.com
Enjoyed Reading? Share Your Views