【问题标题】:Create dynamic library with opencv inside C++在 C++ 中使用 opencv 创建动态库
【发布时间】:2014-07-09 23:32:03
【问题描述】:


我在里面用 Opencv 构建了一个项目,然后在 C# 中使用它。
但是我不能将我的项目包含 opencv 库打包到动态 DLL 中,然后在 C# 中调用它。
有人可以给我一些提示吗? :(
我的代码:

#include "opencv\cv.h"
#include "opencv\highgui.h"
using namespace cv;
extern "C" _declspec(dllexport) string OpenCVTest(string fileName)
{   

     Mat img = imread(fileName);    
     IplImage image = img;  
     cvShowImage("Hello world", &image);
     cvWaitKey(0);
     cvDestroyAllWindows();

     return "";
}

【问题讨论】:

    标签: c# c++ opencv


    【解决方案1】:

    你必须使用 Pinvoke 技术,它可以让你从 c# 代码调用 c++ 函数。

    如果你有这样的用 c++ 编写的函数

    extern "C" {
    
    MYAPI void print_line(const char* str);
    
    }
    

    你可以在c#中为它定义一个原型

    [DllImport("NativeLib.dll")]
    private static extern void print_line(string str);
    

    然后就可以在c#中作为普通函数使用了

    如果你想从 c# 传递一个 std::string 那么你必须使用一个编组器,请参考这个

    Custom Marshaler for PInvoke with std::string

    【讨论】:

    • 但我无法将 OpenCV Lib 导入 C#。如何将 Opencv lib 打包到我的项目 C++ 动态 DLL 中?
    • 你必须将你的 c++ 库与 opencv 链接起来,并且这些 dll 必须在执行时对 c# exe 可用
    猜你喜欢
    • 1970-01-01
    • 2013-09-26
    • 2022-01-10
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2017-01-14
    • 1970-01-01
    相关资源
    最近更新 更多