这是我以前写的边缘检测的例子,效果还可以,不知道满意否?
procedure TMainForm.gausslaplaceClick(Sender: TObject); //¸高斯-拉普拉斯算子
var
bmp1,bmp2:Tbitmap;
p,a1,a2,a3,a4,a5
bytearray;
i,j: integer;
begin
// self.DoubleBuffered:=true;
bmp1:=Tbitmap.Create;
bmp2:=Tbitmap.Create;
bmp1.Assign(backbmp);
bmp1.PixelFormat:=pf24bit;
bmp1.Width:=ChildForm.image1.Picture.Graphic.Width;
bmp1.Height:=ChildForm.image1.Picture.Graphic.Height;
bmp2.Assign(backbmp);
bmp2.PixelFormat:=pf24bit;
for j:=2 to bmp1.Height-3 do
begin
p:=bmp1.ScanLine[j];
a1:=bmp2.ScanLine[j-2];
a2:=bmp2.ScanLine[j-1];
a3:=bmp2.ScanLine[j];
a4:=bmp2.ScanLine[j+1];
a5:=bmp2.ScanLine[j+2];
for i:=2 to bmp1.Width-3 do
begin
p[3*i+2]:=min(255,max(0,(-(2*a1[3*(i-2)+2]+4*a1[3*(i-1)+2]+4*a1[3*i+2]+4*a1[3*(i+1)+2]+2*a1[3*(i+2)+2])
-4*a2[3*(i-2)+2]+8*a2[3*i+2]-4*a2[3*(i+2)+2]
-4*a3[3*(i-2)+2]+8*a3[3*(i-1)+2]+24*a3[3*i+2]+8*a3[3*(i+1)+2]-4*a3[3*(i+2)+2]
-4*a4[3*(i-2)+2]+8*a4[3*i+2]-4*a4[3*(i+2)+2]
-(2*a5[3*(i-2)+2]+4*a5[3*(i-1)+2]+4*a5[3*i+2]+4*a5[3*(i+1)+2]+2*a5[3*(i+2)+2]))));
p[3*i+1]:=min(255,max(0,(-(2*a1[3*(i-2)+1]+4*a1[3*(i-1)+1]+4*a1[3*i+1]+4*a1[3*(i+1)+1]+2*a1[3*(i+2)+1])
-4*a2[3*(i-2)+1]+8*a2[3*i+1]-4*a2[3*(i+2)+1]
-4*a3[3*(i-2)+1]+8*a3[3*(i-1)+1]+24*a3[3*i+1]+8*a3[3*(i+1)+1]-4*a3[3*(i+2)+1]
-4*a4[3*(i-2)+1]+8*a4[3*i+1]-4*a4[3*(i+2)+1]
-(2*a5[3*(i-2)+1]+4*a5[3*(i-1)+1]+4*a5[3*i+1]+4*a5[3*(i+1)+1]+2*a5[3*(i+2)+1]))));
p[3*i]:=min(255,max(0,(-(2*a1[3*(i-2)]+4*a1[3*(i-1)]+4*a1[3*i]+4*a1[3*(i+1)]+2*a1[3*(i+2)])
-4*a2[3*(i-2)]+8*a2[3*i]-4*a2[3*(i+2)]
-4*a3[3*(i-2)]+8*a3[3*(i-1)]+24*a3[3*i]+8*a3[3*(i+1)]-4*a3[3*(i+2)]
-4*a4[3*(i-2)]+8*a4[3*i]-4*a4[3*(i+2)]
-(2*a5[3*(i-2)]+4*a5[3*(i-1)]+4*a5[3*i]+4*a5[3*(i+1)]+2*a5[3*(i+2)]))));
end;
end;
ChildForm.image1.Picture.Bitmap.Assign(bmp1);
// JP1:=Tjpegimage.Create;
// jp1.Assign(bmp1);
Bmp1.Free;
// bmpnumber:=bmpnumber+1;
// jp1.SaveToFile('c:/temp/'+inttostr(bmpnumber)+'.jpg');
// jp1.Free;
end;
procedure TMainForm.prewittClick(Sender: TObject); //prewitt算子
var
bmp1,bmp2:Tbitmap;
p1,p3,p2,p4
bytearray;
i,j: integer;
r,g,b:integer;
begin
self.DoubleBuffered:=true;
bmp1:=Tbitmap.Create;
bmp2:=Tbitmap.Create;
bmp1.Assign(backbmp);
bmp1.PixelFormat:=pf24bit;
bmp1.Width:=ChildForm.image1.Picture.Graphic.Width;
bmp1.Height:=ChildForm.image1.Picture.Graphic.Height;
bmp2.Assign(bmp1);
bmp2.PixelFormat:=pf24bit;
for j:=1 to bmp1.Height-2 do
begin
p1:=bmp1.ScanLine[j]; //²sobal算子 // -1 -1 -1
// 0 0 0
p2:=bmp2.ScanLine[j-1]; // 1 1 1
p3:=bmp2.ScanLine[j]; //和 算子 取较大输出
p4:=bmp2.ScanLine[j+1]; //1 0 -1
for i:=1 to bmp1.Width-2 do //1 0 -1
begin //1 0 -1
r:=min(255,max(0,((-p2[3*(i-1)+2]-p2[3*i+2]-p2[3*(i+1)+2]-0*p3[3*(i-1)+2]+0*p3[3*i+2]-0*p3[3*(i+1)+2]+p4[3*(i-1)+2]+p4[3*i+2]+p4[3*(i+1)+2]))));
g:=min(255,max(0,((-p2[3*(i-1)+1]-p2[3*i+1]-p2[3*(i+1)+1]-0*p3[3*(i-1)+1]+0*p3[3*i+1]-0*p3[3*(i+1)+1]+p4[3*(i-1)+1]+p4[3*i+1]+p4[3*(i+1)+1]))));
b:=min(255,max(0,((-p2[3*(i-1)]-p2[3*i]-p2[3*(i+1)]-0*p3[3*(i-1)]+0*p3[3*i]-0*p3[3*(i+1)]+p4[3*(i-1)]+p4[3*i+2]+p4[3*(i+1)]))));
begin
p1[3*i+2]:=min(255,max(0,((p2[3*(i-1)+2]-0*p2[3*i+2]-p2[3*(i+1)+2]+p3[3*(i-1)+2]+0*p3[3*i+2]-p3[3*(i+1)+2]+p4[3*(i-1)+2]+0*p4[3*i+2]-p4[3*(i+1)+2]))));
p1[3*i+1]:=min(255,max(0,((p2[3*(i-1)+1]-0*p2[3*i+1]-p2[3*(i+1)+1]+p3[3*(i-1)+1]+0*p3[3*i+1]-p3[3*(i+1)+1]+p4[3*(i-1)+1]+0*p4[3*i+1]-p4[3*(i+1)+1]))));
p1[3*i]:= min(255,max(0,((p2[3*(i-1)]-0*p2[3*i]-p2[3*(i+1)]+p3[3*(i-1)]+0*p3[3*i]-p3[3*(i+1)]+p4[3*(i-1)]+0*p4[3*i]-p4[3*(i+1)]))));
p1[3*i+2]:=(max(r,p1[3*i+2]));
p1[3*i+1]:=(max(g,p1[3*i+1]));
p1[3*i]:=(max(b,p1[3*i]));
end;
end;
end;
ChildForm.image1.Picture.Bitmap.Assign(bmp1);
bmp1.free;
end;