6 System.SysUtils, System.Types, System.UITypes, System.Classes,
8 FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Ani, FMX.Layouts,
9 FMX.Gestures, FMX.Graphics,
10 FMX.TabControl, FMX.StdCtrls, System.Actions, FMX.ActnList, FMX.StdActns,
11 FMX.MediaLibrary.Actions, FMX.Objects, FMX.Controls.Presentation, FMX.Edit,
12 FMX.Media, Unit2, Math, FMX.ListBox;
16 StyleBook1: TStyleBook;
17 ToolbarHolder: TLayout;
19 ToolbarPopupAnimation: TFloatAnimation;
21 ToolbarApplyButton: TButton;
22 ToolbarCloseButton: TButton;
23 ToolbarAddButton: TButton;
24 TabControl1: TTabControl;
41 RadioButton1: TRadioButton;
42 RadioButton2: TRadioButton;
43 CameraComponent1: TCameraComponent;
45 SpinEditButton1: TSpinEditButton;
46 SpinEditButton2: TSpinEditButton;
47 SpinEditButton3: TSpinEditButton;
62 procedure ToolbarCloseButtonClick(Sender: TObject);
63 procedure FormGesture(Sender: TObject; const EventInfo: TGestureEventInfo;
64 var Handled: Boolean);
65 procedure FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
67 procedure Button1Click(Sender: TObject);
68 procedure Button2Click(Sender: TObject);
69 procedure Button4Click(Sender: TObject);
70 procedure FormCreate(Sender: TObject);
71 procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
72 Shift: TShiftState; X, Y: Single);
73 procedure FormDestroy(Sender: TObject);
74 procedure CameraComponent1SampleBufferReady(Sender: TObject;
76 procedure Button3Click(Sender: TObject);
77 procedure Button5Click(Sender: TObject);
78 procedure Button6Click(Sender: TObject);
79 procedure Button7Click(Sender: TObject);
81 FGestureOrigin: TPointF;
82 FGestureInProgress: Boolean;
86 Fourier, recg: TFourier;
89 procedure ShowToolbar(AShow: Boolean);
90 procedure detectImage;
91 procedure recognition;
92 function SingleSortS(item1, item2: TFmxObject): integer;
93 function SingleSortL(item1, item2: TFmxObject): integer;
105 procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
108 if Key = vkEscape then
109 ShowToolbar(not ToolbarPopup.IsOpen);
112 procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
113 Shift: TShiftState; X, Y: Single);
115 if Sender = Image1 then
117 Fourier.select(X, Y);
127 procedure TForm1.recognition;
131 a, b: array of Single;
132 estima: array of Single;
137 SetLength(a, 4 * recg.numDescriptor);
138 SetLength(b, 4 * recg.numDescriptor);
139 SetLength(estima, recg.numEntry);
140 Image3.Canvas.BeginScene;
141 Image3.Canvas.FillRect(Image3.BoundsRect, 0, 0, [], 1);
142 Image3.Canvas.DrawRect(Image3.BoundsRect, 0, 0, [], 1);
144 for i := 0 to Count - 1 do
145 Image3.Canvas.DrawLine(PointF(X[i - 1], Y[i - 1]), PointF(X[i], Y[i]), 1);
146 Image3.Canvas.EndScene;
148 for i := 0 to recg.numDescriptor - 1 do
149 with recg.model[recg.rIndex] do
151 a[cnt] := coReal1[i];
152 a[recg.numDescriptor + cnt] := coImag1[i];
153 a[2 * recg.numDescriptor + cnt] := coReal2[i];
154 a[3 * recg.numDescriptor + cnt] := coImag2[i];
157 for n := 0 to Fourier.numEntry - 1 do
160 for i := 0 to Fourier.numDescriptor - 1 do
162 b[cnt] := Fourier.model[n].coImag1[i];
163 b[recg.numDescriptor + cnt] := Fourier.model[n].coImag1[i];
164 b[2 * recg.numDescriptor + cnt] := Fourier.model[n].coReal2[i];
165 b[3 * recg.numDescriptor + cnt] := Fourier.model[n].coImag2[i];
168 if RadioButton1.IsChecked = true then
171 for i := 0 to 4 * recg.numDescriptor - 1 do
172 dist := dist + (a[i] - b[i]) * (a[i] - b[i]);
173 estima[n] := Sqrt(dist);
176 estima[n] := recg.Correlation(a, b, 4 * recg.numDescriptor);
178 ListBox1.Items.Clear;
179 for i := 0 to recg.numEntry - 1 do
181 j := ListBox1.Items.Add('(' + Fourier.model[i].name + ')' +
183 ListBox1.ListItems[j].TagFloat := estima[i];
185 if RadioButton1.IsChecked = true then
186 ListBox1.Sort(SingleSortS)
188 ListBox1.Sort(SingleSortL);
189 for i := ListBox1.Items.Count - 1 downto 5 do
190 ListBox1.Items.Delete(i);
196 procedure TForm1.ToolbarCloseButtonClick(Sender: TObject);
198 Application.Terminate;
201 procedure TForm1.Button1Click(Sender: TObject);
203 CameraComponent1.Active := true;
207 procedure TForm1.Button2Click(Sender: TObject);
209 CameraComponent1.Active := false;
211 TabControl1.TabIndex := 0;
214 procedure TForm1.Button3Click(Sender: TObject);
216 Fourier.model[Fourier.rIndex].name := Edit4.Text;
220 procedure TForm1.Button4Click(Sender: TObject);
222 Fourier.numDescriptor := Edit5.Text.ToInteger;
223 if Fourier.numDescriptor > 50 then
225 Fourier.numDescriptor := 50;
229 thBinary := Edit3.Text.ToInteger;
230 recg.minWidth := Edit1.Text.ToInteger;
231 recg.minHeight := Edit2.Text.ToInteger;
232 Image4.Bitmap.Assign(back);
233 recg.BinaryGray(Image4.Bitmap, thBinary, true);
234 recg.DetectArea(Image4.Bitmap);
235 recg.numDescriptor := Fourier.numDescriptor;
236 TabControl1.TabIndex := 2;
239 procedure TForm1.Button5Click(Sender: TObject);
243 ListBox2.Items.Clear;
244 for i := 0 to Fourier.numEntry - 1 do
245 ListBox2.Items.Add(Fourier.model[i].name + ' / ' + i.ToString);
248 procedure TForm1.Button6Click(Sender: TObject);
250 Fourier.saveModels('default.fo');
253 procedure TForm1.Button7Click(Sender: TObject);
255 Fourier.loadModels('default.fo');
256 Button5Click(Sender);
259 procedure TForm1.CameraComponent1SampleBufferReady(Sender: TObject;
262 CameraComponent1.SampleBufferToBitmap(Image1.Bitmap, true);
265 procedure TForm1.detectImage;
269 bmp.Assign(Image1.Bitmap);
276 thBinary := Edit3.Text.ToInteger;
278 Fourier.minWidth := Edit1.Text.ToInteger;
279 Fourier.minHeight := Edit2.Text.ToInteger;
280 Fourier.BinaryGray(bmp, thBinary, true);
281 Fourier.DetectArea(bmp);
282 Image1.Bitmap.Assign(bmp);
285 function TForm1.SingleSortL(item1, item2: TFmxObject): integer;
289 s := TListBoxItem(item1).TagFloat - TListBoxItem(item2).TagFloat;
298 function TForm1.SingleSortS(item1, item2: TFmxObject): integer;
302 s := TListBoxItem(item1).TagFloat - TListBoxItem(item2).TagFloat;
311 procedure TForm1.FormCreate(Sender: TObject);
313 bmp := TBitmap.Create;
314 buf := TBitmap.Create;
315 back := TBitmap.Create;
316 cap := not Image1.Bitmap.IsEmpty;
317 Fourier := TFourier.Create;
318 Fourier.color := TAlphaColors.Blue;
319 recg := TFourier.Create;
320 recg.color := TAlphaColors.Red;
321 back.Assign(Image4.Bitmap);
324 procedure TForm1.FormDestroy(Sender: TObject);
333 procedure TForm1.FormGesture(Sender: TObject;
334 const EventInfo: TGestureEventInfo; var Handled: Boolean);
338 if EventInfo.GestureID = igiPan then
340 if (TInteractiveGestureFlag.gfBegin in EventInfo.Flags) and
341 ((Sender = ToolbarPopup) or (EventInfo.Location.Y > (ClientHeight - 70)))
344 FGestureOrigin := EventInfo.Location;
345 FGestureInProgress := true;
348 if FGestureInProgress and (TInteractiveGestureFlag.gfEnd in EventInfo.Flags)
351 FGestureInProgress := false;
352 DX := EventInfo.Location.X - FGestureOrigin.X;
353 DY := EventInfo.Location.Y - FGestureOrigin.Y;
354 if (Abs(DY) > Abs(DX)) then
360 procedure TForm1.ShowToolbar(AShow: Boolean);
362 ToolbarPopup.Width := ClientWidth;
363 ToolbarPopup.PlacementRectangle.Rect :=
364 TRectF.Create(0, ClientHeight - ToolbarPopup.Height, ClientWidth - 1,
366 ToolbarPopupAnimation.StartValue := ToolbarPopup.Height;
367 ToolbarPopupAnimation.StopValue := 0;
369 ToolbarPopup.IsOpen := AShow;