2014-02-24 20:18:30 +01:00
|
|
|
// *****************************************************************
|
|
|
|
// Delphi-OpenCV Demo
|
|
|
|
// Copyright (C) 2013 Project Delphi-OpenCV
|
|
|
|
// ****************************************************************
|
|
|
|
// Contributor:
|
|
|
|
// Laentir Valetov
|
|
|
|
// email:laex@bk.ru
|
|
|
|
// ****************************************************************
|
|
|
|
// You may retrieve the latest version of this file at the GitHub,
|
|
|
|
// located at git://github.com/Laex/Delphi-OpenCV.git
|
|
|
|
// ****************************************************************
|
|
|
|
// The contents of this file are used with permission, subject to
|
|
|
|
// the Mozilla Public License Version 1.1 (the "License"); you may
|
|
|
|
// not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at
|
|
|
|
// http://www.mozilla.org/MPL/MPL-1_1Final.html
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an
|
|
|
|
// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
// implied. See the License for the specific language governing
|
|
|
|
// rights and limitations under the License.
|
|
|
|
// *******************************************************************
|
2013-09-12 12:50:55 +02:00
|
|
|
|
|
|
|
unit uOCVTypes;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
Uses
|
|
|
|
System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
System.Generics.Collections,
|
|
|
|
core.types_c;
|
|
|
|
|
|
|
|
Type
|
2014-05-08 22:52:42 +02:00
|
|
|
IocvImage = interface
|
|
|
|
['{84567F57-A399-4179-AA0F-6F8A2788F89B}']
|
|
|
|
function GetIplImage: pIplImage;
|
|
|
|
function GetisGray: Boolean;
|
|
|
|
function GrayImage: IocvImage;
|
|
|
|
function Clone: IocvImage;
|
|
|
|
property IpImage: pIplImage Read GetIplImage;
|
|
|
|
property isGray: Boolean read GetisGray;
|
|
|
|
end;
|
2014-05-07 13:14:32 +02:00
|
|
|
|
2014-05-08 22:52:42 +02:00
|
|
|
TocvImage = class(TInterfacedObject, IocvImage)
|
|
|
|
private
|
|
|
|
FImage: pIplImage;
|
|
|
|
protected
|
|
|
|
function GetIplImage: pIplImage;
|
|
|
|
function GetisGray: Boolean;
|
|
|
|
public
|
|
|
|
constructor Create(const AImage: pIplImage);
|
2014-05-11 02:42:34 +02:00
|
|
|
constructor CreateClone(const AImage: pIplImage);
|
2014-05-08 22:52:42 +02:00
|
|
|
destructor Destroy; override;
|
|
|
|
function GrayImage: IocvImage;
|
|
|
|
function Clone: IocvImage;
|
|
|
|
property IplImage: pIplImage Read GetIplImage;
|
|
|
|
property isGray: Boolean read GetisGray;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TOnOcvNotify = procedure(Sender: TObject; const IplImage: IocvImage) of object;
|
|
|
|
TOnOcvContour = procedure(Sender: TObject; const IplImage: IocvImage; const ContourCount: Integer; const Contours: pCvSeq)
|
|
|
|
of object;
|
2014-05-07 13:14:32 +02:00
|
|
|
|
2013-09-12 12:50:55 +02:00
|
|
|
IocvDataReceiver = interface
|
|
|
|
['{F67DEC9E-CCE0-49D2-AB9B-AD7E1020C5DC}']
|
2014-05-08 22:52:42 +02:00
|
|
|
procedure TakeImage(const IplImage: IocvImage);
|
2013-09-12 12:50:55 +02:00
|
|
|
procedure SetVideoSource(const Value: TObject);
|
|
|
|
end;
|
|
|
|
|
|
|
|
IocvDataSource = interface
|
|
|
|
['{80640C0A-6828-42F8-83E7-DA5FD9036DFF}']
|
2014-05-06 21:13:57 +02:00
|
|
|
procedure AddReceiver(const OpenCVVideoReceiver: IocvDataReceiver);
|
|
|
|
procedure RemoveReceiver(const OpenCVVideoReceiver: IocvDataReceiver);
|
2014-02-24 20:18:30 +01:00
|
|
|
function GetName: string;
|
2014-05-08 22:52:42 +02:00
|
|
|
function GetImage: IocvImage;
|
2013-09-12 12:50:55 +02:00
|
|
|
end;
|
|
|
|
|
2014-05-06 21:13:57 +02:00
|
|
|
TocvReceiverList = TThreadList<IocvDataReceiver>;
|
2013-09-12 12:50:55 +02:00
|
|
|
|
2013-12-02 19:39:13 +01:00
|
|
|
TocvDataSource = class(TComponent, IocvDataSource)
|
2013-09-12 12:50:55 +02:00
|
|
|
protected
|
2014-05-06 21:13:57 +02:00
|
|
|
FOpenCVVideoReceivers: TocvReceiverList;
|
2014-05-08 22:52:42 +02:00
|
|
|
FImage: IocvImage;
|
2014-02-24 20:18:30 +01:00
|
|
|
function GetName: string; virtual;
|
2014-05-08 22:52:42 +02:00
|
|
|
procedure NotifyReceiver(const IplImage: IocvImage); virtual;
|
|
|
|
function GetImage: IocvImage;
|
2013-09-12 12:50:55 +02:00
|
|
|
public
|
2013-12-02 19:39:13 +01:00
|
|
|
constructor Create(AOwner: TComponent); override;
|
2013-09-12 12:50:55 +02:00
|
|
|
destructor Destroy; override;
|
2014-05-06 21:13:57 +02:00
|
|
|
procedure AddReceiver(const OpenCVVideoReceiver: IocvDataReceiver); virtual;
|
|
|
|
procedure RemoveReceiver(const OpenCVVideoReceiver: IocvDataReceiver); virtual;
|
2014-05-08 22:52:42 +02:00
|
|
|
property Image: IocvImage read GetImage;
|
2013-09-12 12:50:55 +02:00
|
|
|
end;
|
|
|
|
|
2013-12-02 19:39:13 +01:00
|
|
|
TocvDataReceiver = class(TComponent, IocvDataReceiver)
|
2013-09-12 12:50:55 +02:00
|
|
|
private
|
2013-12-02 19:39:13 +01:00
|
|
|
FocvVideoSource: IocvDataSource;
|
2013-09-16 13:49:10 +02:00
|
|
|
protected
|
2014-05-08 22:52:42 +02:00
|
|
|
procedure TakeImage(const IplImage: IocvImage); virtual;
|
2013-09-16 13:49:10 +02:00
|
|
|
procedure SetVideoSource(const Value: TObject); virtual;
|
2013-12-02 19:39:13 +01:00
|
|
|
procedure SetOpenCVVideoSource(const Value: IocvDataSource); virtual;
|
2013-09-16 13:49:10 +02:00
|
|
|
public
|
|
|
|
destructor Destroy; override;
|
|
|
|
published
|
2013-12-02 19:39:13 +01:00
|
|
|
property VideoSource: IocvDataSource Read FocvVideoSource write SetOpenCVVideoSource;
|
2013-09-16 13:49:10 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TocvDataSourceAndReceiver = class(TocvDataSource, IocvDataReceiver)
|
|
|
|
private
|
2013-12-02 19:39:13 +01:00
|
|
|
FocvVideoSource: IocvDataSource;
|
2013-09-12 12:50:55 +02:00
|
|
|
protected
|
2014-05-08 22:52:42 +02:00
|
|
|
procedure TakeImage(const IplImage: IocvImage); virtual;
|
2013-09-12 12:50:55 +02:00
|
|
|
procedure SetVideoSource(const Value: TObject); virtual;
|
2013-12-02 19:39:13 +01:00
|
|
|
procedure SetOpenCVVideoSource(const Value: IocvDataSource); virtual;
|
2013-09-16 13:49:10 +02:00
|
|
|
public
|
2013-09-12 12:50:55 +02:00
|
|
|
destructor Destroy; override;
|
|
|
|
published
|
2013-12-02 19:39:13 +01:00
|
|
|
property VideoSource: IocvDataSource Read FocvVideoSource write SetOpenCVVideoSource;
|
2013-09-12 12:50:55 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2014-05-07 13:14:32 +02:00
|
|
|
uses
|
2014-05-08 22:52:42 +02:00
|
|
|
core_c, imgproc_c, imgproc.types_c;
|
2014-05-07 13:14:32 +02:00
|
|
|
|
2014-05-06 21:13:57 +02:00
|
|
|
{TOpenCVDataSource}
|
2013-09-12 12:50:55 +02:00
|
|
|
|
2014-05-06 21:13:57 +02:00
|
|
|
procedure TocvDataSource.AddReceiver(const OpenCVVideoReceiver: IocvDataReceiver);
|
2013-09-12 12:50:55 +02:00
|
|
|
begin
|
2014-05-06 21:13:57 +02:00
|
|
|
FOpenCVVideoReceivers.Add(OpenCVVideoReceiver);
|
2013-09-12 12:50:55 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TocvDataSource.Create(AOwner: TComponent);
|
|
|
|
begin
|
2013-12-02 19:39:13 +01:00
|
|
|
inherited;
|
2014-05-06 21:13:57 +02:00
|
|
|
FOpenCVVideoReceivers := TocvReceiverList.Create;
|
2013-09-12 12:50:55 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TocvDataSource.Destroy;
|
|
|
|
begin
|
2014-05-06 21:13:57 +02:00
|
|
|
FOpenCVVideoReceivers.Free;
|
2014-05-08 22:52:42 +02:00
|
|
|
FImage := nil;
|
2013-09-12 12:50:55 +02:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2014-05-08 22:52:42 +02:00
|
|
|
function TocvDataSource.GetImage: IocvImage;
|
2014-05-07 13:14:32 +02:00
|
|
|
begin
|
|
|
|
Result := FImage;
|
|
|
|
end;
|
|
|
|
|
2014-02-24 20:18:30 +01:00
|
|
|
function TocvDataSource.GetName: string;
|
|
|
|
begin
|
|
|
|
Result := Name;
|
|
|
|
end;
|
|
|
|
|
2014-05-08 22:52:42 +02:00
|
|
|
procedure TocvDataSource.NotifyReceiver(const IplImage: IocvImage);
|
2014-05-06 21:13:57 +02:00
|
|
|
Var
|
|
|
|
R: IocvDataReceiver;
|
|
|
|
LockList: TList<IocvDataReceiver>;
|
|
|
|
begin
|
|
|
|
LockList := FOpenCVVideoReceivers.LockList;
|
|
|
|
try
|
2014-05-08 22:52:42 +02:00
|
|
|
FImage := IplImage;
|
2014-05-06 21:13:57 +02:00
|
|
|
for R in LockList do
|
|
|
|
R.TakeImage(IplImage);
|
|
|
|
finally
|
|
|
|
FOpenCVVideoReceivers.UnlockList;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TocvDataSource.RemoveReceiver(const OpenCVVideoReceiver: IocvDataReceiver);
|
2013-09-12 12:50:55 +02:00
|
|
|
begin
|
2014-05-06 21:13:57 +02:00
|
|
|
FOpenCVVideoReceivers.Remove(OpenCVVideoReceiver);
|
2013-09-12 12:50:55 +02:00
|
|
|
end;
|
|
|
|
|
2014-05-06 21:13:57 +02:00
|
|
|
{TOpenCVDataSourceAndReceiver}
|
2013-09-12 12:50:55 +02:00
|
|
|
|
|
|
|
destructor TocvDataSourceAndReceiver.Destroy;
|
|
|
|
begin
|
2013-09-16 13:49:10 +02:00
|
|
|
if Assigned(FocvVideoSource) then
|
2014-05-06 21:13:57 +02:00
|
|
|
FocvVideoSource.RemoveReceiver(Self);
|
2013-09-12 12:50:55 +02:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2013-12-02 19:39:13 +01:00
|
|
|
procedure TocvDataSourceAndReceiver.SetOpenCVVideoSource(const Value: IocvDataSource);
|
2013-09-12 12:50:55 +02:00
|
|
|
begin
|
2013-12-02 19:39:13 +01:00
|
|
|
if (FocvVideoSource <> Value) then
|
2013-09-12 12:50:55 +02:00
|
|
|
begin
|
|
|
|
if Assigned(FocvVideoSource) then
|
2014-05-06 21:13:57 +02:00
|
|
|
FocvVideoSource.RemoveReceiver(Self);
|
2013-09-12 12:50:55 +02:00
|
|
|
FocvVideoSource := Value;
|
|
|
|
if Assigned(FocvVideoSource) then
|
2014-05-06 21:13:57 +02:00
|
|
|
FocvVideoSource.AddReceiver(Self);
|
2013-09-12 12:50:55 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TocvDataSourceAndReceiver.SetVideoSource(const Value: TObject);
|
|
|
|
begin
|
|
|
|
VideoSource := Value as TocvDataSource;
|
|
|
|
end;
|
|
|
|
|
2014-05-08 22:52:42 +02:00
|
|
|
procedure TocvDataSourceAndReceiver.TakeImage(const IplImage: IocvImage);
|
2013-09-12 12:50:55 +02:00
|
|
|
begin
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2014-05-06 21:13:57 +02:00
|
|
|
{TocvDataReceiver}
|
2013-09-16 13:49:10 +02:00
|
|
|
|
|
|
|
destructor TocvDataReceiver.Destroy;
|
|
|
|
begin
|
|
|
|
if Assigned(FocvVideoSource) then
|
2014-05-06 21:13:57 +02:00
|
|
|
FocvVideoSource.RemoveReceiver(Self);
|
2013-09-16 13:49:10 +02:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2013-12-02 19:39:13 +01:00
|
|
|
procedure TocvDataReceiver.SetOpenCVVideoSource(const Value: IocvDataSource);
|
2013-09-16 13:49:10 +02:00
|
|
|
begin
|
|
|
|
if (FocvVideoSource <> Value) then
|
|
|
|
begin
|
|
|
|
if Assigned(FocvVideoSource) then
|
2014-05-06 21:13:57 +02:00
|
|
|
FocvVideoSource.RemoveReceiver(Self);
|
2013-09-16 13:49:10 +02:00
|
|
|
FocvVideoSource := Value;
|
|
|
|
if Assigned(FocvVideoSource) then
|
2014-05-06 21:13:57 +02:00
|
|
|
FocvVideoSource.AddReceiver(Self);
|
2013-09-16 13:49:10 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TocvDataReceiver.SetVideoSource(const Value: TObject);
|
|
|
|
begin
|
2014-05-06 21:13:57 +02:00
|
|
|
if (Value <> Self) then
|
|
|
|
VideoSource := Value as TocvDataSource;
|
2013-09-16 13:49:10 +02:00
|
|
|
end;
|
|
|
|
|
2014-05-08 22:52:42 +02:00
|
|
|
procedure TocvDataReceiver.TakeImage(const IplImage: IocvImage);
|
|
|
|
begin
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
{TocvImage}
|
|
|
|
|
|
|
|
function TocvImage.Clone: IocvImage;
|
|
|
|
begin
|
2014-05-11 02:42:34 +02:00
|
|
|
Result := TocvImage.CreateClone(FImage);
|
2014-05-08 22:52:42 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TocvImage.Create(const AImage: pIplImage);
|
|
|
|
begin
|
|
|
|
FImage := AImage;
|
|
|
|
end;
|
|
|
|
|
2014-05-11 02:42:34 +02:00
|
|
|
constructor TocvImage.CreateClone(const AImage: pIplImage);
|
2014-05-08 22:52:42 +02:00
|
|
|
begin
|
|
|
|
FImage := cvCloneImage(AImage);
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TocvImage.Destroy;
|
|
|
|
begin
|
|
|
|
cvReleaseImage(FImage);
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TocvImage.GetIplImage: pIplImage;
|
2013-09-16 13:49:10 +02:00
|
|
|
begin
|
2014-05-08 22:52:42 +02:00
|
|
|
Result := FImage;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TocvImage.GetisGray: Boolean;
|
|
|
|
begin
|
|
|
|
Result := FImage^.nChannels = 1;
|
|
|
|
end;
|
2013-09-16 13:49:10 +02:00
|
|
|
|
2014-05-08 22:52:42 +02:00
|
|
|
function TocvImage.GrayImage: IocvImage;
|
|
|
|
Var
|
|
|
|
iImage: pIplImage;
|
|
|
|
begin
|
|
|
|
if isGray then
|
|
|
|
Result := Self
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
iImage := cvCreateImage(cvGetSize(FImage), IPL_DEPTH_8U, 1);
|
|
|
|
cvCvtColor(FImage, iImage, CV_RGB2GRAY);
|
|
|
|
Result := TocvImage.Create(iImage);
|
|
|
|
end;
|
2013-09-16 13:49:10 +02:00
|
|
|
end;
|
|
|
|
|
2013-09-12 12:50:55 +02:00
|
|
|
end.
|