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

0 comments:

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com