先转为24bit,这样方便一些:
高斯滤波代码:
procedure TMainForm.GaotemplateClick(Sender: TObject)
var
bmp1, bmp2: Tbitmap;
p1, p2, p3, p4: pbytearray;
i, j: integer;
begin
self.DoubleBuffered := true
bmp1 := Tbitmap.Create;
bmp2 := Tbitmap.Create;
bmp1.Assign(ChildForm.image1.Picture.Bitmap);
bmp1.PixelFormat := pf24bit;
bmp1.Width := ChildForm.image1.Picture.Graphic.Width;
bmp1.Height := ChildForm.image1.Picture.Graphic.Height;
bmp2.Assign(ChildForm.image1.Picture.Bitmap);
bmp2.PixelFormat := pf24bit;
for j := 1 to bmp1.Height - 2 do
begin
p1 := bmp1.ScanLine[j];
p2 := bmp2.ScanLine[j - 1];
p3 := bmp2.ScanLine[j];
p4 := bmp2.ScanLine[j + 1];
for i := 1 to bmp1.Width - 2 do
begin
p1[3 * i + 2] := (p2[3 * (i - 1) + 2] + 2 * p2[3 * i + 2] +
p2[3
* (i + 1)
+ 2] + 2 * p3[3 * (i - 1) + 2] + 4 * p3[3 * i + 2] + 2
*
p3[3
* (i + 1)
+
2] + p4[3 * (i - 1) + 2] + 2 * p4[3 * i + 2] + p4[3 *
(i +
1) + 2])
shr
4;
p1[3 * i + 1] := (p2[3 * (i - 1) + 1] + 2 * p2[3 * i + 1] +
p2[3
* (i + 1)
+ 1] + 2 * p3[3 * (i - 1) + 1] + 4 * p3[3 * i + 1] + 2
*
p3[3
* (i + 1)
+
1] + p4[3 * (i - 1) + 1] + 2 * p4[3 * i + 1] + p4[3 *
(i +
1) + 1])
shr
4;
p1[3 * i] := (p2[3 * (i - 1)] + 2 * p2[3 * i] + p2[3 * (i +
1)] +
2 * p3[3
* (i - 1)] + 4 * p3[3 * i] + 2 * p3[3 * (i + 1)] + p4[3 *
(i -
1)] + 2 *
p4[3 * i] + p4[3 * (i + 1)]) shr 4;
end;
end;
ChildForm.Image1.Picture.Bitmap.Assign(Bmp1);
Bmp1.Free;
bmp2.Free;
end;