1
votes

I am editing a open source project called Opencv for Delphi which compiles fine with Delphi 6 , Delphi 2009 and Delphi xe2.

I just removed incompatible (eg :Application.MainFormOnTaskbar := True;) for the Delphi 6 compilation.

On the run time d6 app works fine without errors, but the rest are compiling well but with run time error when calling cvopencv.dll.

The original project is a Spanish project compiled with Delphi 2007 which works fine.

In the d6 exe i can't read any thing except ??????? but others showing in spanish which means it is a UNICODE problem, when calling cvopencv.dllthe error happens.

What can i do to make this project work with Delphi 2009 or higher (still d2009 compiling well)

========================= After David Heffemans Answer =====================

procedure TForm1.Button2Click(Sender: TObject);
var

   file1 : PAnsiChar; // "haarcascade_frontalface_alt.xml";
   file2 : PAnsiChar; //"haarcascade_eye.xml";
   file3 : PAnsiChar; //"haarcascade_upperbody.xml";
   SourceFileName : AnsiString;
   StorageType : Integer;
   ImSize : CvSize;

begin
   memo1.Lines.Clear;
   GetMem(Storage, SizeOf(CvMemStorage));
   SourceFileName:=Edit1.Text;
   StorageType:=0;
   storage:=nil;
   storage :=cvCreateMemStorage(storageType);
   file1 := PAnsiChar(ExtractFilePath(Application.ExeName)+'haarcascade_frontalface_alt.xml');
   file2 := PAnsiChar(ExtractFilePath(Application.ExeName)+'haarcascade_eye.xml');
   file3 := PAnsiChar(ExtractFilePath(Application.ExeName)+'haarcascade_upperbody.xml');

   cascade_f := cvLoad(file1, nil, nil, nil);
   cascade_e := cvLoad(file2, nil, nil, nil);
   cascade_ub := cvLoad(file3, nil, nil, nil);

   if cascade_f=nil then ShowMessage('Íå ìîãó çàãðóçèòü êàñêàä Face');
   if cascade_e=nil then ShowMessage('Íå ìîãó çàãðóçèòü êàñêàä Eye');
   if cascade_ub=nil then ShowMessage('Íå ìîãó çàãðóçèòü êàñêàä UpperBody');

   img := cvLoadImage(PAnsiChar(AnsiString(SourceFileName)), 1);

   cvNamedWindow((PAnsiChar(AnsiString(Edit1.Text))), 1);

   detectEyes(img);
   cvShowImage(PAnsiChar(AnsiString(SourceFileName)), img);

end;

end.
2

2 Answers

7
votes

The OpenCV library you refer to says this at the top of the header:

Borland Delphi 4,5,6,7 API for Intel Open Source Computer Vision Library

This code presents a Delphi wrapper around the OpenCV DLLs.

For these older Delphi versions, Char, PChar and string map to AnsiChar, PAnsiChar and AnsiString respectively. This matches the OpenCV DLLs which are pure ANSI. There are no Unicode APIs in OpenCV (at least so far as I can discern).

If you wish to use this unit from Delphi 2009 and up you need to recognise that in Delphi 2009, Unicode support was added. This changed the mapping of Char, PChar and string map to WideChar, PWideChar and UnicodeString respectively. This is the cause of the incompatibility you have encountered because the DLL behind this unit is still processing single byte ANSI text.

Probably the most sensible approach with this unit is to change it to use AnsiChar, PAnsiChar and AnsiString. You can do this with a global search and replace in the unit.

This will make the OpenCV.pas unit match the DLLs again. You now need to be careful how you interface with this OpenCV.pas unit. The functions that receive PAnsiChar parameters need special treatment.

For example consider the following example:

Function  cvLoadImage( const filename : PChar; 
    iscolor : integer=1) : PIplImage; cdecl; 

This is the original declaration. After your search and replace it will read like this:

Function  cvLoadImage( const filename : PAnsiChar; 
    iscolor : integer=1) : PIplImage; cdecl; 

When you come to call it you need to ensure you pass a proper PAnsiChar. Like this:

var
  FileName: string;
...
image := cvLoadImage(PAnsiChar(AnsiString(FileName)));

You must convert from string (which maps to UnicodeString) to AnsiString and only then can you cast to PAnsiChar. Do not write PAnsiChar(FileName). This will not convert the Unicode string to ANSI and will fail.

Fortunately for you the OpenCV library has a relatively small number of APIs that receive text.

1
votes

See the project - Delphi-OpenCV on github (OpenCV version - 2.4.6, environment - Delphi XE2-XE4). Translation of OpenCV library header files in Delphi: https://github.com/Laex/Delphi-OpenCV