MATLAB Answers

May I get the matlab code for Otsu's method of Thresholding (Gray Scale Image)?

조회 수: 143(최근 30일)
Raj Gopal
Raj Gopal 6 Dec 2012
Using Otsu's method

  댓글 수: 0

댓글을 달려면 로그인하십시오.

답변(2개)

Thomas
Thomas 6 Dec 2012
The graythresh function uses Otsu's method, which chooses the threshold to minimize the intraclass variance of the black and white pixels.
another file exchange submission with the code is avialbel at

  댓글 수: 2

Raj Gopal
Raj Gopal 6 Dec 2012
I needed the matlab code for the otsu's algorithm of thresholding.
Thomas
Thomas 6 Dec 2012
it is in the link in the file exchange submission.., asking for code is a very good way of getting your question ignored by regular contributors.. I am attaching the code form the file exchange submission this time..
this is the main otsu.m function
function [k]=otsu(a)
b=a;
[c,d]=size(b);
b=reshape(b,[],1);
[m,n]=size(b);
weightb=zeros(3,256);
weightf=zeros(3,256);
r=1;
l=1;
for T=0:255
[wb,wf,mb,mf,vrb,vrf]=cal(T,b,m);
weightb(1,r)=wb;
weightb(2,r)=mb;
weightb(3,r)=vrb;
r=r+1;
weightf(1,l)=wf;
weightf(2,l)=mf;
weightf(3,l)=vrf;
l=l+1;
end
%Within class variance
wcv=zeros(1,256);
for g=1:256
wcv(1,g)=((weightb(1,g)*weightb(3,g))+((weightf(1,g)*weightf(3,g))));
end
% min(wcv)
[threshold_value,val]=min(wcv);
tval=(val-1)/256;
% b=imresize(b,[c d])
a=im2bw(a,tval);
k= medfilt2(a,[25 25]);
end
This is the cal.m function thta calculate wb,wf,mb and so on
function [wb,wf,mb,mf,vrb,vrf]=cal(i,b,m)
% weight
wb=0;
wf=0;
mb=0;
mf=0;
b=double(b);
vb=0;vf=0;
vrb=0;
vrf=0;
for s=1:m
if(b(s,1)<(i))
wb=wb+1;
mb=mb+b(s,1);
else
wf=wf+1;
mf=mf+b(s,1);
end
end
%mean
if(wb==0)
mb=0;
mf=mf/wf;
elseif(wf==0)
wf=0;
mb=mb/wb;
else
mb=mb/wb;
mf=mf/wf;
end
%weight
wb=wb/m;
wf=wf/m;
%variance
for t=1:m
if(b(t,1)<(i))
vrb=vrb+((b(t,1)-mb)^2);
vb=vb+1;
else
vrf=vrf+((b(t,1)-mf)^2);
vf=vf+1;
end
end
if(vb==0)
vrb=0;
vrf=vrf/vf;
elseif(vf==0)
vrf=0;
vrb=vrb/vb;
else
vrb=vrb/vb;
vrf=vrf/vf;
end

댓글을 달려면 로그인하십시오.


태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by