-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenCV_API.m
76 lines (63 loc) · 2.81 KB
/
OpenCV_API.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
%================================================================
% This class abstracts the API to an external OpenCV library.
% It implements static methods for updating the build information
% at compile time and build time.
classdef OpenCV_API < coder.ExternalDependency
%#codegen
methods (Static)
function bName = getDescriptiveName(~)
bName = 'OpenCV_API';
end
function tf = isSupportedContext(buildContext)
myTarget = {'mex','rtw'};% 验证代码生成目标为mex或者dll,lib,exe
if buildContext.isCodeGenTarget(myTarget)
tf = true;
else
error('OpenCV_API only supported for mex, lib, exe, dll');
end
% hw = buildContext.getHardwareImplementation();
end
function updateBuildInfo(buildInfo, buildContext)
% 输入buildInfo请参考:RTW.BuildInfo
% 输入buildContext请参考:coder.BuildConfig class
%
% Get file extensions for the current platform
% [~, linkLibExt, execLibExt, ~] = buildContext.getStdLibInfo();
% Code to run on Linux platform
includeFilePath = fullfile("E:\opencv4_4_0\opencv\MinGW64_v8_OpenCV4_4_Contrib_install\include");
buildInfo.addIncludePaths(includeFilePath);
% Link files
libPath = fullfile("E:\opencv4_4_0\opencv\MinGW64_v8_OpenCV4_4_Contrib_install\x64\mingw\lib");
linkFiles = "libopencv_world440.dll.a";
linkPath = libPath;
linkPriority = "";
linkPrecompiled = true;
linkLinkOnly = true;
group = '';
buildInfo.addLinkObjects(linkFiles, linkPath, ...
linkPriority, linkPrecompiled, linkLinkOnly, group);
% include and source path
% buildInfo.addIncludePaths("./");
% buildInfo.addSourcePaths("./");
end
%API for library function 'imread'
function outImg = imread(imagePath)
arguments
imagePath (1,1) string
end
outImg = coder.nullcopy(zeros(480,640,3,"uint8"));
if coder.target('MATLAB')
% running in MATLAB, use built-in addition
outImg = imread_opencv_mex(imagePath);
else
% Add the required include statements to the generated function code
coder.cinclude('opencvAPI.h');
% include external C++ functions
coder.updateBuildInfo('addSourceFiles', "opencvAPI.cpp");
% 调用OpenCV C++代码包装器
imgPath = [char(imagePath),0];
coder.ceval('imreadOpenCV', coder.rref(imgPath),coder.wref(outImg));
end
end
end
end