Экранные формы
Рисунок 1 – Загрузка изображения
Рисунок 2 – Алгоритмы обнаружения границ на изображении (оператор Робертса, оператор Собела, разностный оператор)
Листинг программы
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, ExtDlgs, ExtCtrls, Math, Child, ComCtrls;
type
TForm_Main = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
N901: TMenuItem;
N1801: TMenuItem;
N2601: TMenuItem;
N10: TMenuItem;
Cascade1: TMenuItem;
Tile1: TMenuItem;
OpenPictureDialog1: TOpenPictureDialog;
SavePictureDialog1: TSavePictureDialog;
N11: TMenuItem;
N12: TMenuItem;
N13: TMenuItem;
N14: TMenuItem;
N15: TMenuItem;
N16: TMenuItem;
N17: TMenuItem;
N18: TMenuItem;
N19: TMenuItem;
N20: TMenuItem;
N21: TMenuItem;
N22: TMenuItem;
N23: TMenuItem;
StatusBar1: TStatusBar;
tuvtynfwbz1: TMenuItem;
N24: TMenuItem;
N25: TMenuItem;
N26: TMenuItem;
N27: TMenuItem;
N28: TMenuItem;
N29: TMenuItem;
procedure N2Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure Cascade1Click(Sender: TObject);
procedure Tile1Click(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure N8Click(Sender: TObject);
procedure N901Click(Sender: TObject);
procedure N1801Click(Sender: TObject);
procedure N2601Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Gray(FCh,tCh: TForm_Child);
procedure N12Click(Sender: TObject);
procedure N15Click(Sender: TObject);
procedure N16Click(Sender: TObject);
procedure N19Click(Sender: TObject);
procedure N20Click(Sender: TObject);
procedure N21Click(Sender: TObject);
procedure N22Click(Sender: TObject);
procedure N23Click(Sender: TObject);
procedure N24Click(Sender: TObject);
procedure N25Click(Sender: TObject);
procedure N27Click(Sender: TObject);
procedure N28Click(Sender: TObject);
procedure N29Click(Sender: TObject);
private
Mass: array [0..255] of integer;
moda: integer;
public
end;
var
Form_Main: TForm_Main;
MyRect,MyRect2: TRect;
implementation
uses Gist;
{$R *.dfm}
//открыть изображение
procedure TForm_Main.N2Click(Sender: TObject);
var FChild: TForm_Child;
begin
if (OpenPictureDialog1.Execute=true) and (FileExists(OpenPictureDialog1.FileName)=true) then
begin
FChild:=TForm_Child.Create(Application);
FChild.Caption:=OpenPictureDialog1.FileName;
FChild.Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
FChild.Image1.Width:=FChild.Image1.Picture.Bitmap.Width;
FChild.Image1.Height:=FChild.Image1.Picture.Bitmap.Height;
FChild.ClientWidth:=FChild.Image1.Picture.Bitmap.Width;
FChild.ClientHeight:=FChild.Image1.Picture.Bitmap.Height;
end;
end;
//сохранить
procedure TForm_Main.N3Click(Sender: TObject);
var tempChild: TForm_Child;
begin
if (SavePictureDialog1.Execute=true) then
begin
tempChild:=Tform_Child(ActiveMDIChild);
tempChild.Image1.Picture.Bitmap.SaveToFile(SavePictureDialog1.FileName+'.bmp');
end;
end;
//преобразование в полутоновое
procedure TForm_Main.Gray(FCh,tCh: TForm_Child);
var
i,j: integer;
color: TColor;
Gray: byte;
begin
for i:=0 to 255 do Mass[i]:=0;
for i:=0 to (tCh.Image1.Picture.Width-1) do
for j:=0 to (tCh.Image1.Picture.Height-1) do
begin
color:=tCh.Image1.Canvas.Pixels[i,j];
Gray:=Round((0.30*GetRValue(color))+(0.59*GetGValue(color))+(0.11*GetBValue(color)));
FCh.Image1.Canvas.Pixels[i,j]:=RGB(Gray,Gray,Gray);
Mass[Gray]:=Mass[Gray]+1;
end;
end;
//полутоновое
procedure TForm_Main.N7Click(Sender: TObject);
var
FChild,tempChild: TForm_Child;
begin
if not (ActiveMDIChild=Nil) then
begin
if ActiveMDIChild is TForm_Child then
begin
tempChild:=Tform_Child(ActiveMDIChild);
FChild:=TForm_Child.Create(Application);
FChild.Caption:='Новое окно '+IntToStr(MDIChildCount);
FChild.ClientHeight:=tempChild.ClientHeight;
FChild.ClientWidth:=tempChild.ClientWidth;
FChild.Image1.Width:=tempChild.Image1.Width;
FChild.Image1.Height:=tempChild.Image1.Height;
Gray(FChild,tempChild);
end;
end;
end;
//вырезать
procedure TForm_Main.N8Click(Sender: TObject);
var
FChild,tempChild: TForm_Child;
sizeX,sizeY,sx1,sy1: integer;
i,j: integer;
LFrom,LTo: TColor;
begin
if not (ActiveMDIChild=Nil) then
begin
if ActiveMDIChild is TForm_Child then
begin
tempChild:=Tform_Child(ActiveMDIChild);
if (StrToInt(tempChild.EditSelect.Text)=1) then
begin
FChild:=TForm_Child.Create(Application);
FChild.Caption:='Вырезано '+IntToStr(MDIChildCount);
FChild.ClientHeight:=MyRect.Bottom-MyRect.Top;
FChild.ClientWidth:=MyRect.Right-MyRect.Left;
FChild.Image1.Width:=FChild.ClientWidth;
FChild.Image1.Height:=FChild.ClientHeight;
MyRect2:=Rect(0,0,FChild.ClientWidth,FChild.ClientHeight);
FChild.Image1.Canvas.CopyRect(MyRect2,tempChild.Image1.Canvas,MyRect);
tempChild.Image1.canvas.Pen.Width := 1;
tempChild.Image1.canvas.Pen.Color:=clBlack;
tempChild.Image1.canvas.Pen.Mode := pmBlack;
tempChild.Image1.canvas.Pen.Style:=psSolid;
tempChild.Image1.canvas.Brush.Style:=bsSolid;
tempChild.Image1.canvas.Brush.Color:=clBlack;
tempChild.Image1.Canvas.Rectangle(x1,y1,lx,ly);
end;
end;
end;
end;
//поворот 90
procedure TForm_Main.N901Click(Sender: TObject);
var
FChild,tempChild: TForm_Child;
sizeX,sizeY: integer;
i,j: integer;
begin
if not (ActiveMDIChild=Nil) then
begin
if ActiveMDIChild is TForm_Child then
begin
tempChild:=Tform_Child(ActiveMDIChild);
FChild:=TForm_Child.Create(Application);
FChild.Caption:='Поворот '+IntToStr(MDIChildCount+1);
FChild.ClientHeight:=tempChild.ClientWidth;
FChild.ClientWidth:=tempChild.ClientHeight;
FChild.Image1.Width:=tempChild.Image1.Height;
FChild.Image1.Height:=tempChild.Image1.Width;
end;
for i:=0 to (tempChild.Image1.Picture.Width) do
for j:=0 to (tempChild.Image1.Picture.Height) do
FChild.Image1.Canvas.Pixels[FChild.Image1.Width-j,i]:=tempChild.Image1.Canvas.Pixels[i,j];
end;
end;
//поворот 180
procedure TForm_Main.N1801Click(Sender: TObject);
var
FChild,tempChild: TForm_Child;
sizeX,sizeY: integer;
i,j: integer;
begin
if not (ActiveMDIChild=Nil) then
begin
if ActiveMDIChild is TForm_Child then
begin
tempChild:=Tform_Child(ActiveMDIChild);
FChild:=TForm_Child.Create(Application);
FChild.Caption:='Поворот '+IntToStr(MDIChildCount+1);
FChild.ClientHeight:=tempChild.ClientHeight;
FChild.ClientWidth:=tempChild.ClientWidth;
FChild.Image1.Width:=tempChild.Image1.Width;
FChild.Image1.Height:=tempChild.Image1.Height;
end;
for i:=0 to (tempChild.Image1.Picture.Width) do
for j:=0 to (tempChild.Image1.Picture.Height) do
FChild.Image1.Canvas.Pixels[FChild.Image1.Width-i,FChild.Image1.Height-j]:=tempChild.Image1.Canvas.Pixels[i,j];
end;
end;
//поворот 270
procedure TForm_Main.N2601Click(Sender: TObject);
var
FChild,tempChild: TForm_Child;
sizeX,sizeY: integer;
i,j: integer;
begin
if not (ActiveMDIChild=Nil) then
begin
if ActiveMDIChild is TForm_Child then
begin
tempChild:=Tform_Child(ActiveMDIChild);
FChild:=TForm_Child.Create(Application);
FChild.Caption:='Поворот '+IntToStr(MDIChildCount);
FChild.ClientHeight:=tempChild.ClientWidth;
FChild.ClientWidth:=tempChild.ClientHeight;
FChild.Image1.Width:=tempChild.Image1.Height;
FChild.Image1.Height:=tempChild.Image1.Width;
end;
for i:=0 to (tempChild.Image1.Picture.Width) do
for j:=0 to (tempChild.Image1.Picture.Height) do
FChild.Image1.Canvas.Pixels[j,FChild.Image1.Height-i]:=tempChild.Image1.Canvas.Pixels[i,j];
end;
end;
//закрыть все окна
procedure TForm_Main.N4Click(Sender: TObject);
var i: integer;
begin
for i:=0 to MDIChildCount-1 do MDIChildren[i].Close;
end;
procedure TForm_Main.Button1Click(Sender: TObject);
begin
form_gist.Show;
end;
procedure TForm_Main.Cascade1Click(Sender: TObject);
begin
Cascade;
end;
procedure TForm_Main.Tile1Click(Sender: TObject);
begin
Tile;
end;
//оператор Робертса
procedure TForm_Main.N27Click(Sender: TObject);
var
i,j,k,l,temp,temp2: integer;
color: TColor;
maska,maska2: array [0..2,0..2] of integer;
FChild,tempChild: TForm_Child;
begin
maska[0,0]:=0; maska[0,1]:=0; maska[0,2]:=0;
maska[1,0]:=0; maska[1,1]:=1; maska[1,2]:=0;
maska[2,0]:=0; maska[2,1]:=0; maska[2,2]:=-1;
maska2[0,0]:=0; maska2[0,1]:=0; maska2[0,2]:=0;
maska2[1,0]:=0; maska2[1,1]:=1; maska2[1,2]:=0;
maska2[2,0]:=0; maska2[2,1]:=0; maska2[2,2]:=-1;
tempChild:=Tform_Child(ActiveMDIChild);
FChild:=TForm_Child.Create(Application);
FChild.Caption:='Оператор Робертса '+IntToStr(MDIChildCount);
FChild.ClientHeight:=tempChild.ClientHeight;
FChild.ClientWidth:=tempChild.ClientWidth;
FChild.Image1.Width:=tempChild.Image1.Width;
FChild.Image1.Height:=tempChild.Image1.Height;
for i:=1 to (tempChild.Image1.Picture.Width-2) do
for j:=1 to (tempChild.Image1.Picture.Height-2) do
begin
temp:=0; temp2:=0;
for k:=0 to 2 do
for l:=0 to 2 do
begin
color:=tempChild.Image1.Canvas.Pixels[i+k-1,j+l-1];
temp:=temp+maska[k,l]*Round((0.30*GetRValue(color))+(0.59*GetGValue(color))+(0.11*GetBValue(color)));
temp2:=temp2+maska2[l,k]*Round((0.30*GetRValue(color))+(0.59*GetGValue(color))+(0.11*GetBValue(color)));
end;
temp:=round(temp/1);
temp2:=round(temp2/1);
temp:=round(sqrt(power(temp,2)+power(temp2,2)));
FChild.Image1.Canvas.Pixels[i,j]:=RGB(255-temp,255-temp,255-temp);
end;
end;
//оператор Собела
procedure TForm_Main.N28Click(Sender: TObject);
var
i,j,k,l,temp,temp2: integer;
color: TColor;
maska,maska2: array [0..2,0..2] of integer;
FChild,tempChild: TForm_Child;
begin
maska[0,0]:=1; maska[0,1]:=0; maska[0,2]:=-1;
maska[1,0]:=2; maska[1,1]:=0; maska[1,2]:=-2;
maska[2,0]:=1; maska[2,1]:=0; maska[2,2]:=-1;
maska2[0,0]:=-1; maska2[0,1]:=-2; maska2[0,2]:=-1;
maska2[1,0]:=0; maska2[1,1]:=0; maska2[1,2]:=0;
maska2[2,0]:=1; maska2[2,1]:=2; maska2[2,2]:=1;
tempChild:=Tform_Child(ActiveMDIChild);
FChild:=TForm_Child.Create(Application);
FChild.Caption:='Оператор Собела '+IntToStr(MDIChildCount);
FChild.ClientHeight:=tempChild.ClientHeight;
FChild.ClientWidth:=tempChild.ClientWidth;
FChild.Image1.Width:=tempChild.Image1.Width;
FChild.Image1.Height:=tempChild.Image1.Height;
for i:=1 to (tempChild.Image1.Picture.Width-2) do
for j:=1 to (tempChild.Image1.Picture.Height-2) do
begin
temp:=0; temp2:=0;
for k:=0 to 2 do
for l:=0 to 2 do
begin
color:=tempChild.Image1.Canvas.Pixels[i+k-1,j+l-1];
temp:=temp+maska[k,l]*Round((0.30*GetRValue(color))+(0.59*GetGValue(color))+(0.11*GetBValue(color)));
temp2:=temp2+maska2[l,k]*Round((0.30*GetRValue(color))+(0.59*GetGValue(color))+(0.11*GetBValue(color)));
end;
temp:=round(temp/4);
temp2:=round(temp2/4);
temp:=round(sqrt(power(temp,2)+power(temp2,2)));
FChild.Image1.Canvas.Pixels[i,j]:=RGB(255-temp,255-temp,255-temp);
end;
end;
//разностный оператор
procedure TForm_Main.N29Click(Sender: TObject);
var
i,j,k,l,temp,temp2: integer;
color: TColor;
maska,maska2: array [0..2,0..2] of integer;
FChild,tempChild: TForm_Child;
begin
maska[0,0]:=0; maska[0,1]:=0; maska[0,2]:=0;
maska[1,0]:=0; maska[1,1]:=1; maska[1,2]:=-1;
maska[2,0]:=0; maska[2,1]:=1; maska[2,2]:=-1;
maska2[0,0]:=0; maska2[0,1]:=0; maska2[0,2]:=0;
maska2[1,0]:=0; maska2[1,1]:=-1; maska2[1,2]:=-1;
maska2[2,0]:=0; maska2[2,1]:=1; maska2[2,2]:=1;
tempChild:=Tform_Child(ActiveMDIChild);
FChild:=TForm_Child.Create(Application);
FChild.Caption:='Разностный оператор '+IntToStr(MDIChildCount);
FChild.ClientHeight:=tempChild.ClientHeight;
FChild.ClientWidth:=tempChild.ClientWidth;
FChild.Image1.Width:=tempChild.Image1.Width;
FChild.Image1.Height:=tempChild.Image1.Height;
for i:=1 to (tempChild.Image1.Picture.Width-2) do
for j:=1 to (tempChild.Image1.Picture.Height-2) do
begin
temp:=0; temp2:=0;
for k:=0 to 2 do
for l:=0 to 2 do
begin
color:=tempChild.Image1.Canvas.Pixels[i+k-1,j+l-1];
temp:=temp+maska[k,l]*Round((0.30*GetRValue(color))+(0.59*GetGValue(color))+(0.11*GetBValue(color)));
temp2:=temp2+maska2[l,k]*Round((0.30*GetRValue(color))+(0.59*GetGValue(color))+(0.11*GetBValue(color)));
end;
temp:=round(temp/2);
temp2:=round(temp2/2);
temp:=round(sqrt(power(temp,2)+power(temp2,2)));
FChild.Image1.Canvas.Pixels[i,j]:=RGB(255-temp,255-temp,255-temp);
end;
end;
end.