Otsu algorithm (two)

Source: Internet
Author: User

% using the Otsu algorithm for binary images

Close All;clear ALL;CLC

%%

I = Imread (' c.jpg ');

Scoreimage0=rgb2gray (I);

Scoreimage=double (SCOREIMAGE0);

[Height,length]=size (Scoreimage);

Totalpixel=height*length;

% Maxpixnumber=max (max (scoreimage));

This place is not calculated for future convenience, but the default maximum is 255.

Pixelcount=zeros (1,256);% counts the number of individual pixel values

% 0-256

For I=1:length

For J=1:height

Number=scoreimage (j,i) +1;

Pixelcount (number) =pixelcount (number) +1;

End

End

% probability

Pf=pixelcount/totalpixel;

% forward cumulative probability density function

Cpf=zeros (1,256);

CPF (1) =PF (1);

For i=2:256

CPF (i) =CPF (i-1) +PF (i);

End

% post-cumulative probability density function

Bpf=zeros (1,256);

BPF (=PF) (256);

For J=256:-1:2

BPF (J-1) =BPF (j) +PF (j-1);

End

% forward Cumulative expectations

Meanforward=zeros (1,256);

Meanforward (1) = 1;

For i=2:256

Meanforward (i) =meanforward (i-1) * (CPF (i-1)/CPF (i)) + (PF (i)/CPF (i)) *i;

End

% back to cumulative expectation

Meanback=zeros (1,256);

Meanback (Max (scoreimage)) =max (Max (scoreimage));

For I=max (Max (scoreimage)): -1:2

Meanback (i-1) =meanback (i) * (BPF (i)/BPF (i-1)) + (PF (i-1)/BPF (i-1)) * (i-1);

End

Delta=cpf.*bpf.* (meanforward-meanback). ^2;

[Value,index]=max (delta);

% Returns a logical image

binariedimage=scoreimage>index;

Figure

Subplot (1,2,1); Imshow (SCOREIMAGE0); Title (' original image ');

Subplot (1,2,2); Imshow (binariedimage); Title (' Processing chart ');

Here is the original: http://blog.sina.com.cn/s/blog_98ddf7cb0101chu3.html

Otsu algorithm (two)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.