Otsu Thresholding Source Code

//Otsu Thresholding
function TLVQ.GetTValue(bmp:TBitmap):integer;
var
x,y,min,nmin,max,nmax,intens:integer;
T,TNext:double;
begin
//initialize
min:=255;
max:=0;
//cari intensitas tertinggi dan terendah dalam image
for y:= 0 to bmp.Height-1 do
begin
for x:=0 to bmp.Width-1 do
begin
//nilai r,g,b sama pada image greyscale
intens := GetRValue(bmp.Canvas.Pixels[x,y]);
if(intens>max)then max:=intens;
if(intens end;
end;
//hitung T awal
T:=0.5*(min+max);
while (true) do
begin
//
nmin:=0;nmax:=0;max:=0;min:=0;
for y:= 0 to bmp.Height-1 do
begin
for x:=0 to bmp.Width-1 do
begin
//nilai r,g,b sama pada image greyscale
intens := GetRValue(bmp.Canvas.Pixels[x,y]);
if(intens>=T)then
begin
max:= max + intens;
inc(nmax);
end
else
begin
min:= min + intens;
inc(nmin);
end;
end;
end;
TNext := 0.5*((min/nmin)+(max/nmax));
if abs(T-TNext)<0.5 then
break;
T := TNext;
end;
result := Round(T);
end;

0 comments: