目前Lazarus只能支持到opencv2.4.13的直接调用。要调用高版本的Opencv只能用其它语言生成dll等间接调用。经过对Opencv和C++数据的研究。可以使用Lazarus直接调用高版本的Opencv了。
以下是调用Opencv4.70的简单例子。
一、定义类型和函数
{$IfDef WINDOWS}
const
opencv_core='opencv_core470.dll';
opencv_imgcodecs ='opencv_imgcodecs470.dll';
opencv_highgui='opencv_highgui470.dll';
opencv_dnn ='opencv_dnn470.dll';
{$Else}
const
opencv_core='opencv_core470.so';
opencv_imgcodecs ='opencv_imgcodecs470.so';
opencv_highgui='opencv_highgui470.so';
opencv_dnn ='opencv_dnn470.so';
{$EndIf}
{c string数据结构}
TVCStr= record
st:PChar;
R1,R2:DWORD;
Len: UInt64;
capac:UInt64;
end;
pVCStr=^TVCStr;
TVCmat=record
a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12:UInt64;
end;
TVC_inputarray=record
b1,b2,b3,b4:UInt64;
end;
.........等其它数据结构
procedure InputArray(dct:TVC_inputarray;scr:TVCmat);cdecl;external opencv_core name
'??0_InputArray@cv@@QEAA@AEBVMat@1@@Z';
function imread(const filename: TVCStr;iscolor:Integer): TVCmat;stdcall; external opencv_imgcodecs name
'?imread@cv@@YA?AVMat@1@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z';
procedure imshow(const Title: TVCStr;matt: TVC_inputarray); cdecl; external opencv_highgui name
'?imshow@cv@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBV_InputArray@1@@Z' ;
二、调用dll
var
filenameVC:TVCStr;
inputMat:TVCmat;
outputarrayMat:TVC_inputarray;
begin
filenameVC.st:='E:/opencv4.81/ConsoleApplication1/x64/Debug/test2.jpg';//注意一是要使用/,不要用win中的\。二是字符串大小要大于16个字符,小于会出错。
filenameVC.capac:=Length(filenameVC.st)+1;
filenameVC.Len:=Length(filenameVC.st);
inputMat:=imread(filenameVC,1);
inputarray(outputarrayMat,inputMat);
imshow(filenameVC,outputarrayMat);
end;