Atau barangkali ada yg bisa bantu translate, ini codenya, pas googling nemu ini
- Code: Select all
procedure TEnrollForm.Threshold(Image:TBitmap);
const level=255;
var
histogram: array[0..255] of integer;
PH: PByteArray;
TotalMean, Variance, maxVariance, zerothCumuMoment, firstCumuMoment : real;
i,j,k: integer;
p: PByteArray;
threshold:byte;
area: Word;
begin
for i:=0 to level do
begin
histogram[i]:=0;
end;
for i:=0 to Image.Height-1 do
begin
PH:=Image.ScanLine[i];
for j:= 0 to Image.Width-1 do
begin
inc(histogram[PH[3*j]]);
end;
end;
threshold:=0;
totalMean := 0;
maxVariance := 0;
firstCumuMoment := 0;
zerothCumuMoment := 0;
area := Image.Height * Image.Width;
for k:= 0 to level do
TotalMean := TotalMean + (k * histogram[k] / area);
for k:= 0 to level do
begin
zerothCumuMoment := zerothCumuMoment + histogram[k] / area;
firstCumuMoment := firstCumuMoment + (k * histogram[k] / area);
variance := totalMean * zerothCumuMoment - firstCumuMoment;
variance := variance * variance;
if ((zerothCumuMoment <> 0) and (zerothCumuMoment <> 1)) then
begin
variance := variance /(zerothCumuMoment * (1 - zerothCumuMoment));
if (maxVariance < variance) then
begin
maxVariance := variance;
threshold := k;
end;
end;
end;
for i:=0 to Image.Height-1 do begin
p:= Image.ScanLine[i];
for j:=0 to Image.Width-1 do begin
for k:=0 to 2 do
if (p[3*j])and (p[3*j+1]) and (p[3*j+2])>threshold then
p[3*j+k]:= 255
else
p[3*j+k]:= 0;
end;
end;
ImgThres.Picture.Bitmap:=Image;
end;


