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

Recent Posts

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

Converting RGB image to Binary Image without using im2bw function

         In the first example, image is filled with primary colors (RGB). So I am finding the sum of the values in the pixel position. If the sum is greater than zero then the value will be 1(white) otherwise zero (black).

       In the second example, the following steps are needed to convert a RGB image to binary image.

  1. Convert the RGB image into grayscale image.
  2. Find the threshold value. If the value at the pixel position is greater than the threshold value then the value will be 1(white) else zero (black).

    function mybinary
    global GIm T1;
    A=imread('shapes.bmp');
    figure,imshow(A);
    title('Original image');
    B=zeros(size(A,1),size(A,2));
    for l=1:size(A,1)
        for m=1:size(A,2)
            if(sum(A(l,m,:))>0)
                B(l,m)=1;
            end
        end
    end
    B=logical(B);
    figure,imshow(B);





    Im=imread('gantrycrane.png');
    figure,imshow(Im);
    title('Original Image');
    %0.2989 * R + 0.5870 * G + 0.1140 * B
    GIm=uint8(zeros(size(Im,1),size(Im,2)));
    for m=1:size(Im,1)
        for n=1:size(Im,2)
            GIm(m,n)=0.2989*Im(m,n,1)+0.5870*Im(m,n,2)+0.1140*Im(m,n,3);
        end
    end

    we can perform the grayscale conversion without using the for loop:

    %GIm=0.2989*Im(:,:,1)+0.5870*Im(:,:,2)+0.1140*Im(:,:,3);




    
    
    ssz = get(0,'ScreenSize');
    T.fg=figure('Visible','on','Name','IMAGE THRESHOLDING','NumberTitle','off','Position', ssz);
    T.holder=axes('units','pixels','Position',[ssz(3)/35 ssz(4)/4 ssz(3)-(ssz(3)/3) ssz(4)-(ssz(4)/3)]);
    imshow(GIm);
    set(T.holder,'xtick',[],'ytick',[])
    T.slid=uicontrol('Style','Slider','Visible','on','Value',1,'Max',255,'Min',0,'Sliderstep',[1 1],'Position',[ssz(3)/35 ssz(4)/5 ssz(3)-(ssz(3)/3) 20],'Callback', @tresher);
    T.ent=uicontrol('Style','pushbutton','Visible','on','String','THRESHOLD VALUE','Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/8) 105 30]);
    T.ed=uicontrol('Style','edit','Visible','on','String','0','Value',1,'Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/6) 90 20]);
          function tresher(object,~)
            val=get(object,'value');
            in=GIm;
            T1=Imthreshold1(in,val);
            T.view1=imshow(T1);
            set(T.holder,'xtick',[],'ytick',[])
           
            set(T.ed,'String',val);
          end
       
      function Im=Imthreshold1(Image,Tvalue)
    sz=size(Image);
    mybin=zeros(size(Image));
    for i=1:sz(1)
        for j=1:sz(2)
            if(Image(i,j)>Tvalue)
                mybin(i,j)=1;
               
            end
        end
    end


    Instead of this for loop, the equivalent one line code is:

    %mybin(find(Image>Tvalue))=1;

    Explanation:
    The output of find(Image>Tvalue) will be the values that are greater than Tvalue.



    For instance,
    consider a matrix,

    >> A=[1,2,3,4;2,4,6,8;3,6,9,12];
    >> A

    A =

         1     2     3     4
         2     4     6     8
         3     6     9    12

    >> find(mod(A,2)==0)

    ans =

         2
         4
         5
         6
         8
        10
        11
        12




    
    
    Im=logical(mybin);
    end
    end



like button Like "IMAGE PROCESSING" page

YIQ Image to RGB Image

MATLAB code:
%YIQ to RGB

%R=Y+0.956*I+0.621*Q
%G=Y-0.272*I-0.647*Q
%B=Y-1.106*I+1.703*Q
RGB=uint8(zeros(size(YIQ)));
for i=1:size(YIQ,1)
    for j=1:size(YIQ,2)
          RGB(i,j,1)=YIQ(i,j,1)+0.956*YIQ(i,j,2)+0.621*YIQ(i,j,3);
          RGB(i,j,2)=YIQ(i,j,1)-0.272*YIQ(i,j,2)-0.647*YIQ(i,j,3);
          RGB(i,j,3)=YIQ(i,j,1)-1.106*YIQ(i,j,2)+1.703*YIQ(i,j,3);
    end
end

figure,imshow(RGB);
title('RGB Image');

like button Like "IMAGE PROCESSING" page

RGB Image to YIQ Image

YIQ  builds the basis for the NTSC (National Television System Commitee) format.
The Component division of YIQ :
Y=0.30R+0.59G+0.11B
I=0.60R-0.28G-0.32B
Q=0.21R-0.52G+0.31B


MATLAB code:
Im=imread('peppers.png');


figure,imshow(Im);
title('Original Image')

%y=0.2989 * R + 0.5870 * G + 0.1140 * B 

%I=0.60*R - 0.28*G-0.32*B
%Q=0.21*R -0.52*G+0.31*B
YIQ=uint8(zeros(size(Im)));
for i=1:size(Im,1)
    for j=1:size(Im,2)
        YIQ(i,j,1)=0.2989*Im(i,j,1)+0.5870*Im(i,j,2)+0.1140*Im(i,j,3);
        YIQ(i,j,2)=0.596*Im(i,j,1)-0.274*Im(i,j,2)-0.322*Im(i,j,3);
        YIQ(i,j,3)=0.211*Im(i,j,1)-0.523*Im(i,j,2)+0.312*Im(i,j,3);
    end
end
figure,imshow(YIQ);
title('YIQ Image');

like button Like "IMAGE PROCESSING" page

RGB Image to Grayscale Image without using rgb2gray function

A gray-scale image is composed of different shades of grey color.
A true color image can be converted to a gray scale image by preserving the luminance(brightness) of the image.

Here the RGB image is a combination of RED, BLUE AND GREEN colors.
The RGB image is 3 dimensional. In an image ,
at a particular position say  ( i,j)
Image(i,j,1) gives the value of RED pixel.
Image(i,j,2) gives the value of BLUE pixel.
Image(i,j,3) gives the value of GREEN pixel.
The combination of these primary colors are normalized with R+G+B=1;
This gives the neutral white color.

The grayscale image is obtained from the RGB image by combining 30% of RED , 60% of GREEN and 11% of BLUE.
This gives the brightness information of the image. The resulting image will be two dimensional. The value 0 represents black and the value 255 represents white. The range will be between black and white values.


MATLAB CODE:

Im=imread('peppers.png');

figure,imshow(Im);
title('Original Image');

%0.2989 * R + 0.5870 * G + 0.1140 * B
GIm=uint8(zeros(size(Im,1),size(Im,2)));
for i=1:size(Im,1)
      for j=1:size(Im,2)
          GIm(i,j)=0.2989*Im(i,j,1)+0.5870*Im(i,j,2)+0.1140*Im(i,j,3);
      end
end

%Without using for loop:
%GIm=0.2989*Im(:,:,1)+0.5870*Im(:,:,2)+0.1140*Im(:,:,3);


figure,imshow(GIm);
title('Grayscale Image');












like button Like "IMAGE PROCESSING" page
Next Post Home
Related Posts Plugin for WordPress, Blogger...
Twitter Bird Gadget Google ping Hypersmash.com