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

Mean, Median , Variance, Standard deviation and Mode


A=[10 10 20 40 60; 12 1 2 3 4; 7 8 9 11 4 ; 3 4 5 9 10];

A =

    10    10    20    40    60
    12     1     2     3     4
     7     8     9    11     4
     3     4     5     9    10

%MATLAB code to find the mean, median, variance, standard deviation and mode for an image
%A=imread('pout.tif');
%Convert the image to type ‘double’.
%A=double(A);


[r,c]=size(A);

% MEAN:
 The average of sum of all the values in the matrix.
%To find the mean for each column
colmean=sum(A)/r;
display(colmean);


%To calculate the mean of the matrix
totmean=sum(A(:))/(r*c);
display(totmean);


%To calculate the variance and standard-deviation column-wise
myvar=zeros([c,1]);
mystd=zeros([c,1]);
for i = 1: c
    diff=A(:,i)-colmean(i);
    myvar(i)=sum(diff.^2)/(r-1);
    mystd(i)=sqrt(myvar(i));
end

display(myvar);
display(mystd);





%To calculate the variance and standard deviation

totdiff=(A-totmean).^2;
totsum=sum(totdiff(:));
nele=(r*c)-1;
totvar=totsum/nele;
display(totvar);
totstd=sqrt(totvar);
display(totstd);










%MEDIAN

 %To find the median of the matrix row-wise
 n=r/2;
 B=sort(A,1);
 if(mod(r,2)==0)
      rowmedian=(B(round(n),:)+B(round(n)+1,:))/2;
 else
     rowmedian=B(round(n),:);
 end
 display(rowmedian);

 %To find the median of the matrix column-wise
 n=c/2;
 B=sort(A,2);
 if(mod(c,2)==0)

     colmedian=(B(:,round(n))+B(:,round(n)+1))/2;
 else
     colmedian=B(:,round(n));
 end
 display(colmedian);




























%To find the total median of the image
 C=reshape(A,[],1);
 min1=min(C);
 max1=max(C);

 E=sort(A(:));
 num=round((r*c)/2);

 if( (mod(r,2)==0) || (mod(c,2)==0) )
     totmedian=(E(num)+E(num+1))/2;
    
 else
     totmedian=E(num);
 end
 display(totmedian);



    
%To find the mode of the matrix
   D=hist(C,min1:max1);
   D1=find(D==max(D));
   mymode=D1(1)+min1-1;
   display(mymode);
 






like button Like "IMAGE PROCESSING" page

Reverse the words position in a string


This can be done in two ways.
First Method:
 a.Find the length of the string
 b. Use MATLAB built in function strtok to separate the first word and the remaining sentence.
For instance, the input string is ‘Lets Learn together... Happy Reading’.
[Token,remain]=strtok(input_string);
The variable token will contain ‘Lets
And the variable remain will contain ‘Learn together… Happy Reading’.
Use while loop to extract all the strings.

c. In the first iteration, the string in the token variable is concatenated with a empty variable ‘t’.
Now t has ‘Hello’.
In the second iteration, token has ‘Learn’ and remain has ‘together… Happy Reading’.
Now the string ‘Learn’ is concatenated with the string t.

d. Here to preserve the space ,the syntax [string2, ‘  ’,string1] is used.
The length of the input_string gets reduced during each iteration and finally when all the strings are extracted, the loop is terminated.

MATLAB CODE:
%To reverse the words position in a string
str='Lets Learn together... Happy Reading';
%str=fliplr(str);
len=length(str);
t='';

 while(len>0)
     [token,str]=strtok(str);
     len=length(str);
     %token=fliplr(token);
     %t=[token,' ',t];
     t=[' ',token,t];
        
 end
 display(t);


Output:

t =

 Reading Happy together... Learn Lets



Second  Method:

This  differs  in the way of concatenation.
The input string position is reversed. And inside the loop after extracting each token, it is again reversed.
Then  the  string ‘t’ is concatenated with the token. The first method itself sufficient and just know the other way also.
like button Like "IMAGE PROCESSING" page

Pop-Up Menu


A very simple example to show how the pop-up menu / Drop down list in MATLAB works.



MATLAB CODE:
function Popup_menu
global filename Img  I T;
scz=get(0,'ScreenSize');

figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 800 500],'MenuBar','None','NumberTitle','off','Name','PopUpMenu Example','Resize','off');
axes('Position',[0 0 .7 1],'xtick',[],'ytick',[]);

directory=dir('*.jpg');
files={directory.name}';

%Get the File Name
%Position =[X Y length width]
uicontrol('style','text','Position',[565,453 60 20]','String','Filename:');
%The file names are the options here.
uicontrol('Style','popupmenu','position',[628 445 170 30],'Value',1,'String',files,'Callback',@displayfile);

%Select the Conversion type
uicontrol('style','text','Position',[565,415 90 20]','String','Image 
Conversion:');

%The options for image conversion are : Original Image, Convert to grayscale,
Convert to binary, Edge detected Image.
uicontrol('Style','popupmenu','position',[565 375 200 30],'Value',1,'String',{'Convert to GrayScale','Convert to Binary','Original Image','Edge detected Image'},'Callback',@convert);

%Select the edge type
T=uicontrol('Style','popupmenu','position',[565 305 200 30],'Value',1,'String',{'Canny','Sobel','Prewitt','log'},'callback',@findedge);
F=uicontrol('style','text','Position',[565,345 90 20]','String','Edge Type:');

%These components can be made visible or invisible
set(T,'Visible','off');
set(F,'Visible','off');

function displayfile(obj,eve)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        Img=imread(filename);
        imshow(Img);
end

%Switch case
%Value 1 performs the conversion of the orginal image to grayscale image
%Value 2 performs the conversion of the orginal image to binary image
%Value 3 shows the orginal image
%Value 4 performs the edge detection of the image based on the type.




    function convert(obj,eve)
        ptr=get(obj,'value');
        switch ptr
            case {1}
                set(T,'Visible','off');
                set(F,'Visible','off');
                I=rgb2gray(Img);
                imshow(I);
            case {2}
                set(T,'Visible','off');
                set(F,'Visible','off');
                I=im2bw(Img);
                imshow(I);
            case {3}
                set(T,'Visible','off');
                set(F,'Visible','off');
                imshow(Img);
            case {4}
            set(T,'Visible','on');
            set(F,'Visible','on');
   
                           
        end
    end

                                                              
                                                             
                                                              


                                      
                                 
                                      
                                     


       
  %To perform edge detection based on the type
    function findedge(obj,event)
            ptr=get(obj,'value');
            B=rgb2gray(Img);
            switch ptr
                case {1}
                    imshow(~edge(B,'canny'));
                case {2}
                    imshow(~edge(B,'sobel'));
                case {3}
                    imshow(~edge(B,'Prewitt'));
                case {4}
                    imshow(~edge(B,'log'));
                   
            end
           
           
        end
               
   
end




like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com