
Пояснительная записка / 8 ПРИЛОЖЕНИЕ А
.docПриложение А
(обязательное)
Программа удаления точечных шумов на изображении
Текст программы
Всего листов - 12
2011
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtDlgs, Menus, StdCtrls, ExtCtrls, Spin, Buttons, Gauges;
type
TForm1 = class(TForm)
ScrollBox1: TScrollBox;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
Image1: TImage;
OpenPictureDialog1: TOpenPictureDialog;
ScrollBox2: TScrollBox;
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
Gauge1: TGauge;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
SpeedButton6: TSpeedButton;
SpeedButton7: TSpeedButton;
SpeedButton8: TSpeedButton;
SpeedButton9: TSpeedButton;
SpeedButton10: TSpeedButton;
Image2: TImage;
SpeedButton11: TSpeedButton;
SpeedButton12: TSpeedButton;
ComboBox1: TComboBox;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
ListBox1: TListBox;
Panel2: TPanel;
Panel3: TPanel;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
SavePictureDialog1: TSavePictureDialog;
procedure ClearPic();
procedure N2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ComboBox1Change(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure SpeedButton5Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure SpeedButton6Click(Sender: TObject);
procedure Image1Click(Sender: TObject);
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure SpeedButton10Click(Sender: TObject);
procedure SpeedButton9Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N9Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
var
buf1, buf2: TBitmap;
OW, OH: integer;
X0, Y0, Sx, Sy, iks, igr: integer;
R: TRect;
RBegin: boolean;
Clearing: boolean;
Proc: byte;
col: array of TColor;
col1: TColor;
//------------------------------------------------------------------------------
procedure Tform1.ClearPic();
var
i, j, k, x, y: integer;
ssaR, ssaG, ssaB: integer;
cr, cg, cb: byte ;
xx1, xx2, yy1, yy2: integer;
ci, ck: integer;
begin
if RadioButton1.Checked then
begin
xx2 := R.Right- OW div 2;
yy2 := R.Bottom- OH div 2;
end;
if RadioButton2.Checked then
begin
xx2 := R.Right ;
yy2 := R.Bottom ;
end;
if proc = 3 then
begin
if RadioButton1.Checked then
begin
xx1 := R.Left- OW div 2 ;
yy1 := R.Top- OH div 2 ;
end;
if RadioButton2.Checked then
begin
xx1 := R.Left ;
yy1 := R.Top ;
end;
Gauge1.Progress:=0;
Gauge1.MinValue:=0;
Gauge1.MaxValue:= (xx2-xx1)*(yy2-yy1);
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
buf1.Canvas.CopyRect(buf1.Canvas.ClipRect,image1.Picture.Bitmap.Canvas,buf1.Canvas.ClipRect);
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
end;
if proc = 2 then
begin
xx1 := Sx ;
yy1 := Sy ;
end;
proc:= 0; // play
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
Clearing := true;
for x:=xx1 to xx2 do
for y:=yy1 to yy2 do
begin
application.ProcessMessages;
if SpeedButton3.Down then
begin
Image1.Picture.Bitmap.Canvas.Draw(0,0,buf1);
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
Sx := R.Left ;
Sy := R.Top ;
proc:=3;
exit;
end;
if SpeedButton2.Down then
begin
Sx:=x;
Sy:=yy1;
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
proc:=2;
exit;
end;
//filtr
if RadioButton1.Checked then
begin
ck:=0;
if length(col)>0 then
for ci:=0 to High(col) do
if col[ci] = image1.Picture.Bitmap.Canvas.Pixels[x + (OW div 2 ),y + (OH div 2 )] then
ck := ck + 1;
if ck = 0 then
begin
ssaR := 0;
ssaG := 0;
ssaB := 0;
k := 0;
for i:= x to x+ OW-1 do
for j:= y to y+OH -1 do
begin
if not ((i= x + (OW div 2 )) and (j= y + (OH div 2 ))) or
(buf1.Canvas.Pixels[i,j]<> -1) then
begin
ssaR:=ssaR+ GetRValue(image1.Picture.Bitmap.Canvas.Pixels[i,j]);
ssaG:=ssaG+ GetGValue(image1.Picture.Bitmap.Canvas.Pixels[i,j]);
ssaB:=ssaB+ GetBValue(image1.Picture.Bitmap.Canvas.Pixels[i,j]);
k:=k+1;
end;
end;
ssaR:=ssaR div k;
ssaG:=ssaG div k;
ssaB:=ssaB div k;
cr:= getRvalue (image1.Picture.Bitmap.Canvas.Pixels[x + (OW div 2 ),y + (OH div 2 )]);
cg:= getGvalue (image1.Picture.Bitmap.Canvas.Pixels[x + (OW div 2 ),y + (OH div 2 )]);
cb:= getBvalue (image1.Picture.Bitmap.Canvas.Pixels[x + (OW div 2 ),y + (OH div 2 )]) ;
if (abs(cr-ssar)> strtoint(edit1.Text)) or (abs(cg-ssag)> strtoint(edit1.Text)) or(abs(cb-ssab)> strtoint(edit1.Text)) then
image1.Picture.Bitmap.Canvas.Pixels[x + (OW div 2 ),y + (OH div 2 )]:= RGB(ssar,ssag,ssab);
end; //end ck
end;//end filtr
if (RadioButton2.Checked) and (length(col)<>0) then
begin
for ci:=0 to High(col) do
if image1.Picture.Bitmap.Canvas.Pixels[x,y] = col[ci] then
image1.Picture.Bitmap.Canvas.Pixels[x,y] := col1;
end;
Gauge1.Progress:= Gauge1.Progress+1;
end;
buf2.Canvas.CopyRect(buf2.Canvas.ClipRect,image1.Picture.Bitmap.Canvas,buf2.Canvas.ClipRect);
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
proc:=3;
SpeedButton3.Down := true;
form1.SpeedButton3Click(self);
end;
//------------------------------------------------------------------------------
procedure TForm1.N2Click(Sender: TObject);
begin
if not OpenPictureDialog1.Execute then
exit;
image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
image1.Picture.Bitmap.Canvas.DrawFocusRect(r);
buf1.Width:=image1.Picture.Width;
buf1.Height:=image1.Picture.Height;
buf1.Canvas.CopyRect(buf1.Canvas.ClipRect,image1.Picture.Bitmap.Canvas,buf1.Canvas.ClipRect);
buf2.Width:=image1.Picture.Width;
buf2.Height:=image1.Picture.Height;
buf2.Canvas.CopyRect(buf2.Canvas.ClipRect,image1.Picture.Bitmap.Canvas,buf2.Canvas.ClipRect);
proc:=3;
setlength(col,0);
listbox1.Clear;
panel2.Caption:='no';
end;
//------------------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
begin
Clearing := false;
proc:=3; //stop
buf1:=TBitmap.Create;
buf2:=TBitmap.Create;
OW:=3;
OH:=3;
end;
//------------------------------------------------------------------------------
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if SpeedButton7.Down then
begin
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
X0 := X;
Y0 := Y;
Sx:=X;
Sy:=Y;
R.TopLeft := Point(X,Y);
R.BottomRight := Point(X,Y);
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
RBegin := true;
end;
iks:=X;
igr:=Y;
end;
//------------------------------------------------------------------------------
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if SpeedButton7.Down then
if RBegin then
begin
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
if X0 < X then
begin
R.Left := X0;
R.Right := X;
end
else
begin
R.Left := X;
R.Right := X0;
end;
if Y0 < Y then
begin
R.Top := Y0;
R.Bottom := Y;
end
else
begin
R.Top := Y;
R.Bottom := Y0;
end;
if R.Left < 0 then
R.Left := 0;
if R.Top < 0 then
R.Top := 0;
if R.Right > Image1.Picture.Bitmap.Width then
R.Right := Image1.Picture.Bitmap.Width;
if R.Bottom > Image1.Picture.Bitmap.Height then
R.Bottom := Image1.Picture.Bitmap.Height;
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
end;
image2.Canvas.CopyRect(image2.Canvas.ClipRect,image1.Picture.Bitmap.Canvas,rect(X-10,Y-10,X+10,Y+10));
if image2.Canvas.Pixels[50,50] = 255 then
image2.Canvas.Pen.Color:= rgb(0,255,0) else image2.Canvas.Pen.Color:= rgb(0,0,255);
image2.Canvas.MoveTo(0,52);
image2.Canvas.LineTo(100,52);
image2.Canvas.MoveTo(52,0);
image2.Canvas.LineTo(52,100);
end;
//------------------------------------------------------------------------------
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if SpeedButton7.Down then
begin
RBegin := false;
Clearing := false;
end;
end;
//------------------------------------------------------------------------------
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if ComboBox1.ItemIndex = 0 then
begin
OW:=3;
OH:=3;
end;
if ComboBox1.ItemIndex = 1 then
begin
OW:=5;
OH:=5;
end;
if ComboBox1.ItemIndex = 2 then
begin
OW:=7;
OH:=7;
end;
if ComboBox1.ItemIndex = 1 then
begin
OW:=9;
OH:=9;
end;
end;
//------------------------------------------------------------------------------
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if ((edit1.Text) = '') or (edit2.Text = '') or (edit2.Text = '') then
begin
Application.MessageBox('Неверное значение порога', '', MB_ICONEXCLAMATION);
SpeedButton3.Down:=true;
exit;
end;
if (strtoint(edit1.Text) > 256) or (strtoint(edit2.Text) > 256)
or (strtoint(edit3.Text) > 256) then
begin
Application.MessageBox('Неверное значение порога', '', MB_ICONEXCLAMATION);
SpeedButton3.Down:=true;
exit;
end;
if proc <> 0 then
begin
Edit1.Enabled:=false;
Edit2.Enabled:=false;
Edit3.Enabled:=false;
Combobox1.Enabled:=false;
SpeedButton4.Enabled:=false;
SpeedButton5.Enabled:=false;
SpeedButton6.Enabled:=false;
SpeedButton7.Enabled:=false;
SpeedButton8.Enabled:=false;
SpeedButton9.Enabled:=false;
SpeedButton10.Enabled:=false;
SpeedButton11.Enabled:=false;
SpeedButton12.Enabled:=false;
RadioButton1.Enabled:=false;
RadioButton2.Enabled:=false;
form1.ClearPic;
end;
end;
//------------------------------------------------------------------------------
procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
Edit1.Enabled:=true;
Edit2.Enabled:=true;
Edit3.Enabled:=true;
Combobox1.Enabled:=true;
SpeedButton4.Enabled:=true;
SpeedButton5.Enabled:=true;
SpeedButton6.Enabled:=true;
SpeedButton7.Enabled:=true;
SpeedButton8.Enabled:=true;
SpeedButton9.Enabled:=true;
SpeedButton10.Enabled:=true;
SpeedButton11.Enabled:=true;
SpeedButton12.Enabled:=true;
RadioButton1.Enabled:=true;
RadioButton2.Enabled:=true;
if proc = 2 then
begin
Image1.Picture.Bitmap.Canvas.Draw(0,0,buf1);
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
Sx := R.Left ;
Sy := R.Top ;
proc:=3;
end;
end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
Image1.Picture.Bitmap.Canvas.Draw(0,0,buf1);
Image1.Picture.Bitmap.Canvas.DrawFocusRect(r);
end;
procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
Image1.Picture.Bitmap.Canvas.Draw(0,0,buf2);
Image1.Picture.Bitmap.Canvas.DrawFocusRect(r);
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
//pause
Edit1.Enabled:=true;
Edit2.Enabled:=true;
Edit3.Enabled:=true;
Combobox1.Enabled:=true;
SpeedButton4.Enabled:=false;
SpeedButton5.Enabled:=false;
SpeedButton6.Enabled:=false;
SpeedButton7.Enabled:=false;
SpeedButton8.Enabled:=true;
SpeedButton9.Enabled:=true;
SpeedButton10.Enabled:=true;
SpeedButton11.Enabled:=true;
SpeedButton12.Enabled:=true;
RadioButton1.Enabled:=true;
RadioButton2.Enabled:=true;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8,#13]) then
key := #0;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if proc = 2 then
Action := caNone;
end;
procedure TForm1.SpeedButton6Click(Sender: TObject);
begin
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
R.Left := 0;
R.Top := 0;
R.Right := Image1.Picture.Bitmap.Width;
R.Bottom := Image1.Picture.Bitmap.Height;
Image1.Picture.Bitmap.Canvas.DrawFocusRect(R);
end;
procedure TForm1.Image1Click(Sender: TObject);
var
c: TColor;
i:integer;
begin
if SpeedButton8.Down then
begin
c:=image1.Picture.Bitmap.Canvas.Pixels[iks,igr] ;
if SpeedButton12.Down then
begin
if Length(col)>0 then
for i:= 0 to high(col) do
if col[i] = c then
exit;
setlength(col,length(col)+1);
col[High(col)]:=c;
listbox1.Items.Add('R '+inttostr(getRValue(c))+ ' G ' +inttostr(getGValue(c))+ ' B '+inttostr(getBValue(c)));
listbox1.ItemIndex:=High(col);
form1.ListBox1Click(self);
end;
if SpeedButton11.Down then
begin
Col1 := c;
panel3.Color := c;
panel3.Caption:=('R '+inttostr(getRValue(c))+ ' G ' +inttostr(getGValue(c))+ ' B '+inttostr(getBValue(c)));
end;
end;
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
Listbox1.Canvas.Brush.Color:=col[index];
Listbox1.Canvas.Rectangle(rect.Left,rect.Top+2,rect.Left+10,rect.Top+10);
Listbox1.Canvas.Brush.Color:= clWhite;
end;
procedure TForm1.SpeedButton10Click(Sender: TObject);
begin
setlength(col,0);
listbox1.Clear;
panel2.Caption := 'no';
end;
procedure TForm1.SpeedButton9Click(Sender: TObject);
var
i, k: integer;
begin
if listbox1.ItemIndex <> -1 then
begin
k:= listbox1.ItemIndex;
listbox1.Items.Strings[listbox1.ItemIndex]:=listbox1.Items.Strings[high(col)];
col[listbox1.ItemIndex]:=col[high(col)];
listbox1.Items.Delete(high(col));
setlength(col,length(col)-1);
if high(col) >= k then
listbox1.ItemIndex:=k
else
if high(col) >= k-1 then
listbox1.ItemIndex:=k-1;
form1.ListBox1Click(self);
end;
end;
//------------------------------------------------------------------------------
procedure TForm1.ListBox1Click(Sender: TObject);
begin
if listbox1.ItemIndex<> -1 then
begin
panel2.Color := col[listbox1.ItemIndex];
panel2.Caption := listbox1.Items.Strings[listbox1.ItemIndex];
end
else
begin
panel2.Color := clSkyBlue;
panel2.Caption := 'no';
end;
end;
procedure TForm1.N3Click(Sender: TObject);
begin
if SavePictureDialog1.Execute then
Image1.Picture.Bitmap.SaveToFile(SavePictureDialog1.FileName);
end;
procedure TForm1.N9Click(Sender: TObject);
begin
frAbout.ShowModal;
end;
end.