Grey Level Segmentation

download Grey Level Segmentation

of 22

Transcript of Grey Level Segmentation

  • 8/10/2019 Grey Level Segmentation

    1/22

    stanbul Teknik niversitesiElektrik Elektronik Fakltesi

    Telekomnikasyon MhendisliiBlm

    TEL 432 E

    DIGITAL IMAGE

    PROCESSING

    -Homework 4-

    Batuhan Osmanoglu040010250

  • 8/10/2019 Grey Level Segmentation

    2/22

    Segmentation Algorithms

    Introduction

    Five segmentation methods are employed on 3 images such as: face, hand-written

    text and sky. Original gray level images are given below.

    Face: Text: Sky:

    Histograms of these images are;

  • 8/10/2019 Grey Level Segmentation

    3/22

  • 8/10/2019 Grey Level Segmentation

    4/22

    The first segmentation method is Mean Gray Level. Then, we will continue with,

    Two Peaks, Edge Pixels, Iterative Selection and Percentage of Black Pixels.

    Mean Gray Level

    Mean Gray Level Algorithm is simply applied by summing up all the pixel valuesin the image and then taking the mean of it to obtain the threshold. MatLab code andoutput images are given below.

    Image Face Image Text Image Sky ImageThreshold Value 71 189 126

    Outputs

  • 8/10/2019 Grey Level Segmentation

    5/22

  • 8/10/2019 Grey Level Segmentation

    6/22

    MatLab Code

    %TEL 432E HW 4%MEAN GRAY LEVEL%Segmentation

    imface = imread('face.jpg');imtext = imread('text.jpg');imsky = imread('sky.jpg');

    %FACE%calculate mean grey level[rmax cmax]=size(imface);T=0;T=sum(sum(imface));T=T/(rmax*cmax)imface(find(imface=T))=1;

    %Text%calculate mean grey level[rmax cmax]=size(imtext);T=0;T=sum(sum(imtext));T=T/(rmax*cmax)imtext(find(imtext=T))=1;

    %Sky%calculate mean grey level[rmax cmax]=size(imsky);T=0;T=sum(sum(imsky));T=T/(rmax*cmax)imsky(find(imsky=T))=1;

    figure(1);colormap('gray'); imagesc(imface);figure(2);colormap('gray'); imagesc(imtext);

    figure(3);colormap('gray'); imagesc(imsky);

  • 8/10/2019 Grey Level Segmentation

    7/22

    Two PeaksMethod of Two Peaks is employed by finding two local maximum points in the

    histogram and defining a threshold separating them.

    Image Face Image Text Image Sky ImageThreshold Value 55 129 153

    Outputs

  • 8/10/2019 Grey Level Segmentation

    8/22

  • 8/10/2019 Grey Level Segmentation

    9/22

    MatLab Code

    %TEL 432E HW 4%Two Peaks%Segmentation

    imface = imread('face.jpg');imtext = imread('text.jpg');imsky = imread('sky.jpg');

    %FACEhistog=hist(double(imface),256);histogram=sum(histog');figure(2);plot(histogram,'r');title('face-histogram');locmax=find(histogram==max(histogram));T=zeros(1,256);for hi=1:256

    T(hi)=histogram(hi)*(hi-locmax)^2;endlocmaxx=find(T==max(T));tresh=(locmax+locmaxx)/2imface(find(imface=tresh))=1;

    %Texthistog=hist(double(imtext),256);histogram=sum(histog');figure(4);plot(histogram,'r');title('text-histogram');

    locmax=find(histogram==max(histogram));T=zeros(1,256);for hi=1:256

    T(hi)=histogram(hi)*(hi-locmax)^2;endlocmaxx=find(T==max(T));tresh=(locmax+locmaxx)/2imtext(find(imtext=tresh))=1;

    %Skyhistog=hist(double(imsky),256);histogram=sum(histog');figure(6);plot(histogram,'r');title('sky-histogram');locmax=find(histogram==max(histogram));T=zeros(1,256);for hi=1:256

    T(hi)=histogram(hi)*(hi-locmax)^2;

  • 8/10/2019 Grey Level Segmentation

    10/22

    endlocmaxx=find(T==max(T));tresh=(locmax+locmaxx)/2imsky(find(imsky=tresh))=1;

    figure(1);colormap('gray'); imagesc(imface);title('faceimage');

    figure(3);colormap('gray'); imagesc(imtext);title('textimage');

    figure(5);colormap('gray'); imagesc(imsky);title('skyimage');

    Edge Pixels

    Laplacian is calculated for each pixel and then histogram of pixels with largelaplacians is created. Using this new histogram a threshold can be detected using any ofthe previous methods.

    Image Face Image Text Image Sky ImageThreshold Value 54 109 82

    Outputs

  • 8/10/2019 Grey Level Segmentation

    11/22

  • 8/10/2019 Grey Level Segmentation

    12/22

    MatLab Code

    %TEL 432E HW 4%EDGE PIXELS

    %Segmentation

    imface = imread('face.jpg');imtext = imread('text.jpg');imsky = imread('sky.jpg');

    %FACE%calculate laplacian[rmax cmax]=size(imface);L=zeros(size(imface));for r=2:rmax-1

    for c=2:cmax-1L(r,c)=double(imface(r-

    1,c))+double(imface(r+1,c))+double(imface(r,c-1))+double(imface(r,c+1))-4*double(imface(r,c));

    endendef=rmax*cmax*0.85;%number of eightyfive percent of pixelsLL=zeros(size(L));%new image with only eighty five percent and aboveki=0;while ki

  • 8/10/2019 Grey Level Segmentation

    13/22

    L(r,c)=double(imtext(r-1,c))+double(imtext(r+1,c))+double(imtext(r,c-1))+double(imtext(r,c+1))-4*double(imtext(r,c));

    endendef=rmax*cmax*0.85;%number of eightyfive percent of pixelsLL=zeros(size(L));%new image with only eighty five percent and aboveki=0;while ki

  • 8/10/2019 Grey Level Segmentation

    14/22

    T=T/(ef)imsky(find(imsky=T))=1;

    figure(1);colormap('gray'); imagesc(imface);figure(2);colormap('gray'); imagesc(imtext);figure(3);colormap('gray'); imagesc(imsky);

    Iterative Threshold Method

    In this method a threshold is iteratively calculated and refined by consecutive passes through the image.

    Image Face Image Text Image Sky ImageThreshold Value 76 152 135

    Outputs

  • 8/10/2019 Grey Level Segmentation

    15/22

  • 8/10/2019 Grey Level Segmentation

    16/22

    MatLab Code

    %TEL 432E HW 4%Iterative Selection%Segmentation

    clear all; clc;

    imface = imread('face.jpg');imtext = imread('text.jpg');imsky = imread('sky.jpg');

    tresholds=zeros(1,3);%FACE%calculate mean grey level[rmax cmax]=size(imface);T=0;T=sum(sum(imface));T=T/(rmax*cmax)Tnew =-1;while round(T)~=round(Tnew) %not equal

    if Tnew~=-1T=Tnew;

    endTlow=

    sum(sum(imface(find(imface=T)))

    Tnew=(Tlow+Thigh)/2end

    imface(find(imface=T))=1;figure(1);colormap('gray'); imagesc(imface);tresholds(1,1)=T;%Text%calculate mean grey level[rmax cmax]=size(imtext);

    T=0;T=sum(sum(imtext));T=T/(rmax*cmax)Tnew =-1;while round(T)~=round(Tnew) %not equal

    if Tnew~=-1T=Tnew;

    end

  • 8/10/2019 Grey Level Segmentation

    17/22

    Tlow=sum(sum(imtext(find(imtext=T)))

    Tnew=(Tlow+Thigh)/2end

    imtext(find(imtext=T))=1;

    figure(2);colormap('gray'); imagesc(imtext);tresholds(1,2)=T;%Sky%calculate mean grey level[rmax cmax]=size(imsky);T=0;T=sum(sum(imsky));T=T/(rmax*cmax)Tnew =-1;while round(T)~=round(Tnew) %not equal

    if Tnew~=-1T=Tnew;

    endTlow=

    sum(sum(imsky(find(imsky=T)))Tnew=(Tlow+Thigh)/2

    end

    imsky(find(imsky=T))=1;

    figure(3);colormap('gray'); imagesc(imsky);tresholds(1,3)=T;thresholds

    Percentage of Black Pixels

    Assuming that percentage of black pixels is a constant for some types of images,lower pixel values up to the number of assumed pixels are segmented as background or

    black.

    Image Face Image Text Image Sky ImageThreshold 94 178 111

    Percentage ofBlack Pixels

    60 20 50

  • 8/10/2019 Grey Level Segmentation

    18/22

    Outputs

  • 8/10/2019 Grey Level Segmentation

    19/22

    MatLab Code

    %TEL 432E HW 4%Percentage of Black Pixels%Segmentation

    clear all; clc;

    imface = imread('face.jpg');imtext = imread('text.jpg');imsky = imread('sky.jpg');tresholds=zeros(2,3);%FACE%Assumption of black pixel percentagep=60;%percentage of Black pixels[rmax cmax]=size(imface);pxn=rmax*cmax; %number of pixelsT=0;while max(size(find(imface==0))) < p*pxn/100

    T=T+1;imface(find(imface==T))=0;

    endimface(find(imface>T))=1;tresholds(1,1)=T;tresholds(2,1)=p;

  • 8/10/2019 Grey Level Segmentation

    20/22

    figure(1);colormap('gray'); imagesc(imface);

    %Text%Assumption of black pixel percentagep=20;%percentage of Black pixels[rmax cmax]=size(imtext);pxn=rmax*cmax; %number of pixelsT=0;while max(size(find(imtext==0))) < p*pxn/100

    T=T+1;imtext(find(imtext==T))=0;

    endimtext(find(imtext>T))=1;tresholds(1,2)=T;tresholds(2,2)=p;

    figure(2);colormap('gray'); imagesc(imtext);

    %Sky%Assumption of black pixel percentagep=50;%percentage of Black pixels[rmax cmax]=size(imsky);pxn=rmax*cmax; %number of pixelsT=0;while max(size(find(imsky==0))) < p*pxn/100

    T=T+1;imsky(find(imsky==T))=0;

    endimsky(find(imsky>T))=1;tresholds(1,3)=T;tresholds(2,3)=p;

    figure(3);colormap('gray'); imagesc(imsky);thresholds

  • 8/10/2019 Grey Level Segmentation

    21/22

    Gray Level Histogram

    Gray Level Histogram method is based on minimizing the ratio of between classesvariance to total variance of gray level pixels.

    Total gray level variance is the variance of two separate Gaussian functions,representing the background and object pixels. In case there are two pixel groups withGaussian distribution, it is not a great deal to calculate the overall variance of the wholeimage, denoted by t

    2.

    Moreover, it is possible to calculate the variance of the object and the background pixels separately, for any predefined threshold. This is denoted by w

    2 and called withinclass variance. This term, within class variance is a measure of standard deviation of

    background and object pixels. As a matter of fact, if it is possible to perfectly separateobject and background pixels than we will have two separate Gaussian functions with twoexact within class variance. Even though it is an important measure in ANOVA (Analysis

    of Variance), we are not going to deal with this term in Gray Level Histogram Method.Variations of the mean values for each class from the mean of whole image are

    named as between classes variance. This is a value that we want to minimize. The nameimplies that it is the between classes variance. We would like to have no relation

    between two classes to be able to make a perfect distinction. Therefore, we are going to pick up the best threshold value, which minimizes the ratio of b2/ t2 where b2 represents between classes variance.

    Overall variance t2 is going to be calculated from the image, using histogramvalues. On the other hand, it is not that easy to calculate between classes variance. Anexpression to calculate

    b

    2 is;

    200

    2 )( iib =

    In this formula 0 and i represents the mean values of overall and pixels up togray level i. Other terms, w 0 and w i are calculated using the expression below:

    00

    0 1 == =

    ii

    t

    i

    p

    And;

    i

    t

    it

    t T i

    i pi =

    ==

    = 0000 1

    Where p i is the probability of gray level i and t is the mean of the whole image.

    Given these expressions one can calculate each b2/ t

    2 ratio for any t. Value t thatgives the minimum result for that ratio is to be selected as the optimal threshold.

  • 8/10/2019 Grey Level Segmentation

    22/22

    Sources:

    1. Advanced Methods in Grey Level Segmentation.2. Gray Levels and Histogram , December 2004, Ruye Wang

    http://fourier.eng.hmc.edu/e161/lectures/digital_image/node9.html

    3.

    Analysis of Variance between groups, December 2004,http://www.physics.csbsju.edu/stats/anova.html