Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Circle

      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


Check some optical Illusion concept on circle using MATLAB: http://angeljohnsy.blogspot.com/2014/06/optical-illusion-circle.html

If you find any difficulty in the code, Mail me, I will send the code.


like button Like "IMAGE PROCESSING" page

8 comments:

lrq3000 said... Reply to comment

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.

Ahmed Daw said... Reply to comment

can I ask u?

Ahmed Daw said... Reply to comment

really I need ur help?

Emmanual Robin said... Reply to comment

how to fill circle with the color inside and outside with different color

Emmanual Robin said... Reply to comment

how will i fill the circle with the different colors with inside as well as outside?

Aaron Angel said... Reply to comment

@Emmanual Robin

Use 'bwlabel' function and 'label2rgb' function.

Unknown said... Reply to comment

how to find the centroid of a hand and its fingertip from the centroid ....can you help me n coding

Zaàfoury Rabàà said... Reply to comment

how to draw a circle trigonometry around an image in matlab

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Related Posts Plugin for WordPress, Blogger...
Google ping Hypersmash.com