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

How to draw in MATLAB

I used the function ‘getline’ to get the points from the user. Using plot function the lines are drawn.

MATLAB CODE:
scz=get(0,'ScreenSize');

fig=figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Draw Lines','Resize','off');



[x, y] = getline(fig);
plot(x,y);
axis([0 1 0 1]);

Draw lines
                    
To draw a rectangle:

x=zeros([1 5]);
y=zeros([1 5]);

r=getrect(fig);
x(:)=r(1);
y(:)=r(2);
x(2:3)=r(1)+r(3);
y(3:4)=r(2)+r(4);



plot(x,y);
axis([0 1 0 1]);

Draw Rectangle

The function ‘getrect’ returns the [xmin ymin width height].
We need 4 points to draw a rectangle.
The first point is (x,y) , here it is (xmin,ymin).
The second point is(x+width,y)
Third point is (x+width, y+height)
Fourth point is (x,y+height)
Since I am using the plot function I need to connect the 1st and the fourth point.
So I need a fifth point i.e (x,y) which completes the rectangle.
like button Like "IMAGE PROCESSING" page

Otsu’s thresholding without using MATLAB function graythresh


                To perform the thresholding I followed these steps:
a.       Reshape the 2 dimensional grayscale image to 1 dimensional.
b.      Find the histogram of the image using  ‘hist’ function.
c.       Initialize a matrix with values from 0 to 255
d.      Find the weight , mean and the variance for the foreground and background
e.      calculate weight of foreground* variance of foreground + weight of background* variance of background.
f.       Find the minimum value.
MATLAB CODE:
%To threshold image without using graythresh function
function mygraythresh
global H Index;
B=imread('tire.tif');

Here I converted the 2d matrix to 1d matrix.
V=reshape(B,[],1);

The histogram of the values from 0 to 255 is stored.
For instance, G(1) contains the number of occurrence of the value zero in the image.
G=hist(V,0:255);
H=reshape(G,[],1);
 'index' is a 1 dimensional matrix ranging between 0 and 255
 Ind=0:255;
 Index=reshape(Ind,[],1);
 result=zeros(size([1 256]));

To avoid many for loops I used only 1 for loop and a function to calculate the weight, mean and variance.

Let me explain the foreground and the background for a value of ‘i’.
if ‘i’ value is 5 then the foreground values will be 0,1,2,3,4,5
and the background values will be 6 to 255.

for i=0:255
     [wbk,varbk]=calculate(1,i);
     [wfg,varfg]=calculate(i+1,255);
    
After calculating the weights and the variance, the final computation is stored in the array ‘result’.
result(i+1)=(wbk*varbk)+(wfg*varfg);
    
    
 end
 %Find the minimum value in the array.                   [threshold_value,val]=min(result);
    
     tval=(val-1)/256;
     
Now convert the image to binary with the calculated threshold value.
bin_im=im2bw(B,tval);
     figure,imshow(bin_im);
 function [weight,var]=calculate(m,n)
%Weight Calculation
     weight=sum(H(m:n))/sum(H);
    
%Mean Calculation
     value=H(m:n).*Index(m:n);
     total=sum(value);
     mean=total/sum(H(m:n));
     if(isnan(mean)==1)
         mean=0;
     end
%Variance calculation.
    value2=(Index(m:n)-mean).^2;
     numer=sum(value2.*H(m:n));
     var=numer/sum(H(m:n));
     if(isnan(var)==1)
         var=0;
     end
    
 end
 end
 
                     
                   
                       
      
     
Threshold value:0.3242

like button Like "IMAGE PROCESSING" page

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