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: https://www.imageeprocessing.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

Rainbow

Last night I was watching dragon tales, a cartoon series. I saw a scene with rainbow and my mind started to think to implement that rainbow in MATLAB. To my surprise, what I learned in the tenth grade mathematics came in handy, the equation of a circle.  Many times our lower class basic mathematics will come throughout our life. As a result I have started to read tenth grade mathematics once again ;-)
I followed the same procedure, 'How to draw a circle'. Instead of finding the midpoint for x co-ordinate I took the image size, so that I may get a half a circle.
sz=400;
rad=200;
clear RGB
RGB(1:sz,1:sz,1:3)=255;
RGB=uint8(RGB);
[x y z]= find(RGB==255);

xc=sz;
yc=ceil((sz+1)/2);




for i=1:12
    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



B=rgb2gray(RGB);


D=edge(B);
SE=strel('disk',2);
D1=~imdilate(D,SE);

I colored the image based on label

[BW ,L]=bwlabel(D1,8);

    mycolor(:,1)=[128;128;255];
    mycolor(:,2)=[215;0;0];
    mycolor(:,3)=[255;128;0];
    mycolor(:,4)=[255;255;0];
    mycolor(:,5)=[0;128;64];
    mycolor(:,6)=[10;160;220];
    mycolor(:,7)=[0;0;70];
    mycolor(:,8)=[65;0;128];
    mycolor(:,9)=[128;128;255];
for n=1:9
[r c]=find(BW==n);

for i=1:size(r,1)
            RGB(r(i,1),c(i,1),:)=mycolor(:,n);
end
end
imshow(RGB);


like button Like "IMAGE PROCESSING" page

Checkerboard

                 I came across the matlab built in function checkerboard. And I tried to implement my own code.
First I declared the size of each square and then the number of rows and columns. Then I declared two matrixes initialized with zeros and ones. Using mod 2 I changed the colors or ones and zeros alternatively.
MATLAB CODE:
%Size of the square
sz=45;
%Number of rows
xvalue=8;
%Number of columns
yvalue=8;
%Intialize matrix A with zeros and matrix B with ones
A=zeros([sz sz]);
B=ones([sz sz]);
clear C
 m=sz;
 n=1;
 num=2;
for i=1:xvalue
    n1=1;
    m1=sz;
   
    for j=1:yvalue
        if(mod(num,2)==0)
        C(n:m,n1:m1)=A;
        num=num+1;
        else
        C(n:m,n1:m1)=B;
        num=num+1;
        end
       
        m1=m1+sz;
        n1=n1+sz;
    end
    if(mod(yvalue,2)==0)
    num=num+1;
    end
    n=n+sz;
    m=m+sz;
end
imshow(C)

8X8 size:45

8X20 size:15
              
like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com