2014-06-16 23:38:35 +02:00
|
|
|
unit ocv.comp.Proc;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
Uses
|
|
|
|
ocv.comp.Types,
|
|
|
|
ocv.objdetect_c,
|
|
|
|
ocv.core.types_c;
|
|
|
|
|
|
|
|
function ocvHaarCascadeTransform(
|
2014-08-06 00:36:22 +02:00
|
|
|
{ } const Source: IocvImage;
|
|
|
|
{ } const Cascade: pCvHaarClassifierCascade;
|
|
|
|
{ } var HaarRects: TocvRects;
|
|
|
|
{ } const MinSize, MaxSize: TcvSize;
|
|
|
|
{ } const Equalize: Boolean = True;
|
|
|
|
{ } const Scale: Double = 1.3;
|
|
|
|
{ } const MinNeighbors: Integer = 3;
|
|
|
|
{ } const Flag: TocvHaarCascadeFlagSet = []): Boolean;
|
2014-06-16 23:38:35 +02:00
|
|
|
|
|
|
|
function ocvLoadHaarCascade(const HaarCascadeType: TocvHaarCascadeType): pCvHaarClassifierCascade;
|
|
|
|
|
2014-08-26 14:00:30 +02:00
|
|
|
function IsRectEmpty(const Rect: TocvRect): Boolean;
|
|
|
|
|
2014-06-16 23:38:35 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
Uses
|
2014-09-29 09:12:47 +02:00
|
|
|
{$IFDEF HAS_UNITSCOPE}
|
2015-03-30 23:02:32 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
2014-09-29 09:12:47 +02:00
|
|
|
Winapi.Windows,
|
2015-03-30 23:02:32 +02:00
|
|
|
{$ENDIF MSWINDOWS}
|
2014-06-16 23:38:35 +02:00
|
|
|
System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
System.ZLib,
|
2014-08-26 14:00:30 +02:00
|
|
|
{$ELSE}
|
2015-03-30 23:02:32 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
2014-08-26 14:00:30 +02:00
|
|
|
Windows,
|
2015-03-30 23:02:32 +02:00
|
|
|
{$ENDIF MSWINDOWS}
|
2014-08-26 14:00:30 +02:00
|
|
|
SysUtils,
|
|
|
|
Classes,
|
|
|
|
ZLib,
|
2014-09-29 09:12:47 +02:00
|
|
|
{$ENDIF}
|
2014-06-16 23:38:35 +02:00
|
|
|
ocv.core_c,
|
|
|
|
ocv.imgproc_c,
|
2015-09-19 21:49:45 +02:00
|
|
|
ocv.utils;
|
2014-06-16 23:38:35 +02:00
|
|
|
|
|
|
|
{$I Opencv.inc}
|
|
|
|
|
|
|
|
function ocvLoadHaarCascade(const HaarCascadeType: TocvHaarCascadeType): pCvHaarClassifierCascade;
|
|
|
|
|
|
|
|
function TempPath: string;
|
|
|
|
var
|
|
|
|
BufSize: Cardinal;
|
|
|
|
begin
|
|
|
|
BufSize := GetTempPath(0, nil);
|
|
|
|
SetLength(Result, BufSize);
|
|
|
|
GetTempPath(BufSize, PChar(Result));
|
|
|
|
Result := Trim(Result);
|
|
|
|
end;
|
|
|
|
|
2017-05-21 12:03:23 +02:00
|
|
|
function FileSize(const aFilename: String): Int64;
|
|
|
|
var
|
|
|
|
info: TWin32FileAttributeData;
|
|
|
|
begin
|
|
|
|
Result := -1;
|
|
|
|
|
|
|
|
if NOT GetFileAttributesEx(PWideChar(aFilename), GetFileExInfoStandard, @info) then
|
|
|
|
EXIT;
|
|
|
|
|
|
|
|
Result := Int64(info.nFileSizeLow) or Int64(info.nFileSizeHigh shl 32);
|
|
|
|
end;
|
|
|
|
|
2014-06-16 23:38:35 +02:00
|
|
|
Var
|
|
|
|
FullFileName: String;
|
|
|
|
RS: TResourceStream;
|
|
|
|
DC: TZDecompressionStream;
|
|
|
|
FS: TFileStream;
|
|
|
|
begin
|
|
|
|
Result := nil;
|
2016-11-21 15:51:34 +01:00
|
|
|
FullFileName := TempPath + CascadeRecourse[HaarCascadeType].FileName;
|
2017-05-21 12:03:23 +02:00
|
|
|
if (not FileExists(FullFileName)) or (FileSize(FullFileName) = 0) then
|
2014-06-16 23:38:35 +02:00
|
|
|
begin
|
2016-11-21 15:51:34 +01:00
|
|
|
RS := TResourceStream.Create(hInstance, CascadeRecourse[HaarCascadeType].Name, RT_RCDATA);
|
2014-06-16 23:38:35 +02:00
|
|
|
DC := TZDecompressionStream.Create(RS);
|
|
|
|
FS := TFileStream.Create(FullFileName, fmCreate);
|
|
|
|
try
|
|
|
|
FS.CopyFrom(DC, DC.Size);
|
|
|
|
finally
|
|
|
|
DC.Free;
|
|
|
|
FS.Free;
|
|
|
|
RS.Free;
|
|
|
|
end;
|
|
|
|
end;
|
2017-05-21 12:03:23 +02:00
|
|
|
if FileExists(FullFileName) and (FileSize(FullFileName) > 0) then
|
2014-06-16 23:38:35 +02:00
|
|
|
Result := cvLoad(c_str(FullFileName), nil, nil, nil);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function ocvHaarCascadeTransform;
|
|
|
|
Var
|
|
|
|
storage: pCvMemStorage;
|
|
|
|
gray: IocvImage;
|
|
|
|
detected_objects: pCvSeq;
|
|
|
|
i: Integer;
|
|
|
|
cvr: pCvRect;
|
2015-03-30 23:02:32 +02:00
|
|
|
// r, g, b: byte;
|
2014-06-16 23:38:35 +02:00
|
|
|
begin
|
|
|
|
SetLength(HaarRects, 0);
|
|
|
|
Result := False;
|
|
|
|
if Assigned(Cascade) then
|
|
|
|
begin
|
|
|
|
storage := cvCreateMemStorage(0);
|
|
|
|
try
|
|
|
|
gray := Source.GrayImage;
|
|
|
|
if Equalize then
|
|
|
|
cvEqualizeHist(gray.IpImage, gray.IpImage);
|
2014-08-06 00:36:22 +02:00
|
|
|
detected_objects := cvHaarDetectObjects(gray.IpImage, Cascade, storage, Scale, MinNeighbors, HaarSetToFlag(Flag), MinSize, MaxSize);
|
2014-06-16 23:38:35 +02:00
|
|
|
if Assigned(detected_objects) then
|
|
|
|
begin
|
|
|
|
SetLength(HaarRects, detected_objects^.total);
|
|
|
|
i := 0;
|
|
|
|
While i < detected_objects^.total do
|
|
|
|
begin
|
|
|
|
cvr := pCvRect(cvGetSeqElem(detected_objects, i));
|
|
|
|
HaarRects[i] := ocvRect(cvr^.X, cvr^.Y, (cvr^.X) + (cvr^.Width), (cvr^.Y) + (cvr^.Height));
|
|
|
|
Inc(i);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Result := True;
|
|
|
|
finally
|
|
|
|
cvReleaseMemStorage(storage);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-08-26 14:00:30 +02:00
|
|
|
function IsRectEmpty(const Rect: TocvRect): Boolean;
|
|
|
|
begin
|
|
|
|
Result := (Rect.Right <= Rect.Left) or (Rect.Bottom <= Rect.Top);
|
|
|
|
end;
|
|
|
|
|
2014-06-16 23:38:35 +02:00
|
|
|
end.
|