由于二值图点的RGB是0或者255,可以根据一个点A的RGB值 与周围的8个点的RBG 比较 设定一个值N(0<N<8) 当A的RGB值与周围8个点的RGB相等数小于N时 此点为噪点改变其RGB值即可 .附上一段代码 没具体测试。procedure ClearNoise(Bitmap:TBitmap;N:Integer); var piexl : tcolor; nearDots,x,y,RGBz : integer; begin bitmap.Canvas.Pixels[0,0]:=rgb(255, 255, 255); bitmap.Canvas.Pixels[bitmap.Width - 1,bitmap.Height - 1]:=rgb(255, 255, 255); for x := 1 to bitmap.Width -2 do for y := 1 to bitmap.Height-2 do begin piexl := bitmap.Canvas.Pixels[x,y]; RGBz := getrvalue(piexl); if (RGBz = 0) then begin nearDots := 0; //判断周围8个点RGB是否相等 if (getrvalue(bitmap.Canvas.Pixels[x - 1, y - 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x, y - 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x + 1, y - 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x - 1, y]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x + 1, y]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x - 1, y + 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x, y + 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x + 1, y + 1] )= RGBz) then nearDots:=nearDots+1 ; if (nearDots < N) then bitmap.Canvas.Pixels[x,y]:=rgb(255, 255, 255) end else bitmap.Canvas.Pixels[x,y]:=rgb(255, 255, 255); end; end;
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) Image1: TImage; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; procedure ClearNoise(Bitmap:TBitmap;N:Integer); implementation{$R *.dfm} procedure ClearNoise(Bitmap:TBitmap;N:Integer); var piexl : tcolor; nearDots,x,y,RGBz : integer; begin bitmap.Canvas.Pixels[0,0]:=rgb(255, 255, 255); bitmap.Canvas.Pixels[bitmap.Width - 1,bitmap.Height - 1]:=rgb(255, 255, 255); for x := 1 to bitmap.Width -2 do for y := 1 to bitmap.Height-2 do begin piexl := bitmap.Canvas.Pixels[x,y]; RGBz := getrvalue(piexl); if (RGBz = 0) then begin nearDots := 0; //判断周围8个点RGB是否相等 if (getrvalue(bitmap.Canvas.Pixels[x - 1, y - 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x, y - 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x + 1, y - 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x - 1, y]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x + 1, y]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x - 1, y + 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x, y + 1]) = RGBz) then nearDots:=nearDots+1 ; if (getrvalue(bitmap.Canvas.Pixels[x + 1, y + 1] )= RGBz) then nearDots:=nearDots+1 ; if (nearDots < N) then bitmap.Canvas.Pixels[x,y]:=rgb(255, 255, 255) end else bitmap.Canvas.Pixels[x,y]:=rgb(255, 255, 255); end; end;procedure TForm1.Button1Click(Sender: TObject); begin ClearNoise(Image1.Picture.Bitmap,1); ClearNoise(Image1.Picture.Bitmap,2); ClearNoise(Image1.Picture.Bitmap,3); ClearNoise(Image1.Picture.Bitmap,4); end;end. 效果不行,
var
piexl : tcolor;
nearDots,x,y,RGBz : integer;
begin
bitmap.Canvas.Pixels[0,0]:=rgb(255, 255, 255);
bitmap.Canvas.Pixels[bitmap.Width - 1,bitmap.Height - 1]:=rgb(255, 255, 255);
for x := 1 to bitmap.Width -2 do
for y := 1 to bitmap.Height-2 do
begin
piexl := bitmap.Canvas.Pixels[x,y];
RGBz := getrvalue(piexl);
if (RGBz = 0) then
begin
nearDots := 0; //判断周围8个点RGB是否相等
if (getrvalue(bitmap.Canvas.Pixels[x - 1, y - 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x, y - 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x + 1, y - 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x - 1, y]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x + 1, y]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x - 1, y + 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x, y + 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x + 1, y + 1] )= RGBz) then nearDots:=nearDots+1 ;
if (nearDots < N) then bitmap.Canvas.Pixels[x,y]:=rgb(255, 255, 255)
end
else
bitmap.Canvas.Pixels[x,y]:=rgb(255, 255, 255);
end;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
procedure ClearNoise(Bitmap:TBitmap;N:Integer);
implementation{$R *.dfm}
procedure ClearNoise(Bitmap:TBitmap;N:Integer);
var
piexl : tcolor;
nearDots,x,y,RGBz : integer;
begin
bitmap.Canvas.Pixels[0,0]:=rgb(255, 255, 255);
bitmap.Canvas.Pixels[bitmap.Width - 1,bitmap.Height - 1]:=rgb(255, 255, 255);
for x := 1 to bitmap.Width -2 do
for y := 1 to bitmap.Height-2 do
begin
piexl := bitmap.Canvas.Pixels[x,y];
RGBz := getrvalue(piexl);
if (RGBz = 0) then
begin
nearDots := 0; //判断周围8个点RGB是否相等
if (getrvalue(bitmap.Canvas.Pixels[x - 1, y - 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x, y - 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x + 1, y - 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x - 1, y]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x + 1, y]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x - 1, y + 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x, y + 1]) = RGBz) then nearDots:=nearDots+1 ;
if (getrvalue(bitmap.Canvas.Pixels[x + 1, y + 1] )= RGBz) then nearDots:=nearDots+1 ;
if (nearDots < N) then bitmap.Canvas.Pixels[x,y]:=rgb(255, 255, 255)
end
else
bitmap.Canvas.Pixels[x,y]:=rgb(255, 255, 255);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ClearNoise(Image1.Picture.Bitmap,1);
ClearNoise(Image1.Picture.Bitmap,2);
ClearNoise(Image1.Picture.Bitmap,3);
ClearNoise(Image1.Picture.Bitmap,4);
end;end.
效果不行,