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

Number of occurrences in cell array

To find the number of occurrence of a string in an array:

Consider a cell array
c={'one';'two';'three';'nine';'five';'two';'six';'one'; 'two';'five';};


The number of times the string ‘one’ occurs in the array

sum(strcmp('one',c))

ans =

     2

To find the number of occurrence of all the elements in the array:


%c={'one';'two';'three';'nine';'five';'two'; 'six';'one'; 'two';'five';}; %dim=1

c='one','one','two','two','three','three','three','four','five','six','five','five'};%dim=2;

dim=2;
%Initialize the 'cell_array'
cell_array=cell([2 size(c,dim)]);
display(c);
inc=1;
for i=1:size(c,dim)
    %Compare the elements in the 'cell_array' with the elements in 'c'
   if(strcmp(cell_array(1,:),c(i))==0)
       %If the element is not present, then add it to 'cell_array'.
       cell_array(1,inc)=c(i);
       %Find the number of occurence of the element
     num=   sum(strcmp(c(i),c));
     cell_array(2,inc)=num2cell(num);
     inc=inc+1;
   else
       %Delete if the element is already present in the 'cell_array'.
       cell_array(:,inc)='';
      
      
   end
  
  
end

display(cell_array);



The result shows the elements in the first row and the number of occurrences in the second row.
like button Like "IMAGE PROCESSING" page

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
Previous Post Next Post Home
Google ping Hypersmash.com