创建C ++ / CLI OpenCV的包装在C#中使用 [英] Creating an C++/CLI OpenCV wrapper to use in C#

查看:492
本文介绍了创建C ++ / CLI OpenCV的包装在C#中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个OpenCV的包装在C#中使用它。我使用这个链接作为参考的 http://drthitirat.wordpress.com/2013/06/06/use-opencv-c-codes-in-a-vc-project-solution-的创造 - 一个管理-CLR-包装/
到目前为止,我已经创建了一个包含我的图像处理代码,一个C ++控制台应用程序。此外,我创建了一个C ++ / CLI类库中,我裹OpenCV的代码,但是当我尝试构建它,我得到了很多关于在C ++代码中使用OpenCV函数解析的外部错误的,我不知道如何解决它...任何想法如何解决这一问题?有没有在C#中使用C ++ OpenCV的代码更简单的,更简单的方法?我不想用Emgu或任何其他的包装,我的图像处理代码必须在C ++中。

I want to create an OpenCV wrapper to use it in C#. I am using this link as a reference http://drthitirat.wordpress.com/2013/06/06/use-opencv-c-codes-in-a-vc-project-solution-of-creating-a-managed-clr-wrapper/ So far I have created a C++ console application that contains my image processing code. Also I created a C++/CLI class library in which I wrapped the OpenCV code, but when i try to build it I get a lot of unresolved externals errors about OpenCV functions used in the C++ code and I don't know how to fix it... Any idea how to fix the problem? Is there a simpler, easier way of using C++ OpenCV code in C#? I don't want to use Emgu or any other wrapper, my image processing code has to be in C++.

推荐答案

使用我如何解决了OpenCV的问题的Visual Studio 2012:

How I solved the problem with opencv using visual studio 2012:


  1. 我创建了OpenCV的C ++的图像处理库,并将其编译为一个静态库提(.LIB)的此处。基本上,创建一个控制台应用程序项目,并更改项目配置 - >常规 - >配置类型为静态库(.LIB)。这将编译你的项目,后来你应该在C ++ / CLI使用的.lib文件。 //www.codeproject:C语言为每个类++我正要发布到C#我做了一个包装像的
  2. 然后我做了一个C ++ / CLI包装.COM /用品/ 19354 /快速-C-CLI-了解-C-CLI-在高于10分钟少的相对=nofollow>此链接。 >现有项目,并选择从标题项目1.这也有一个好处,如果你的.lib文件改变的东西,你有相同的.h文件,所以当 - 我用刚刚加入到项目一样添加到项目中存在从C ++头重新编译的.lib,您不必更改页眉在C ++ / CLI。我从1包含的.lib将项目属性 - >链接器 - >输入 - >附加依赖,并把路径的.lib文件。该项目编译为.dll文件。 (项目配置 - >常规 - >配置类型 - >动态链接库文件(.dll))

  3. 在C#项目我刚刚加入引用.dll和使用的类的。 C ++ / CLI,这是管理类。这是多么神奇的作品。

  1. I created a c++ image processing library with opencv and compiled it to a static library (.lib) as mentioned here. Basically, create an console application project and change in Project configuration -> General -> Configuration type to a "static library (.lib)". That will compile your project to a .lib file which later you should use in c++/cli.
  2. Then i made a C++/CLI wrapper - for each class in c++ I was about to publish to c# I made a wrapper like in this link. I used existed headers from c++ just by adding to project like Add to project -> Existing Items and choose headers from project 1. This also have an advantage that if you change something in .lib file, you have the same .h files so when you recompile .lib, you don't have to change headers in c++/cli. I included .lib from 1. going to Project properties -> Linker -> Input -> Additional dependencies and put the path to the .lib file. This project was compiled to .dll file. (Project configuration -> General -> Configuration type -> "dynamic library (.dll)").
  3. In c# project I've just add to references the .dll and used classes from c++/cli , which are managed classes. And that is how magic works.

备注答:我可以保证这个解决方案工作。我喜欢用模式检测和CAMSHIFT与OpenCV的2.4.2功能。

Remark A: I can assure that this solution worked. I used features like pattern detection and camshift with opencv 2.4.2.

备注乙:的另一个话题是究竟如何编组应。在简单数据类型的情况下,没有使用C ++ / CLI数据类型如UInt32的等等。毫无疑问,但问题是,如果你想通过更复杂的对象,如简历::垫不具有类型C ++ / CLI直接等同。在这种情况下,我对C ++ / CLI方提出这样的类的简化版本。

Remark B: Another topic is how exactly marshalling should be made. In case of simple data types, there is no doubt in using c++/cli data types like UInt32 etc. But the question is if you want to pass more sophisticated objects like cv::Mat which do not have types direct equivalent in c++/cli. In that case, I made simplified version of such classes on c++/cli side.

备注C:不要乱架构在不同的项目。如果您在使用例如。86,在所有项目后续

Remark C: Don't mess architecture in different projects. If you are using e.g. x86, be consequent in all projects.

备注D:实际上,在C#中使用C ++代码的问题有两种解决方案:我所描述的和直接呼叫到C ++托管代码在C#中使用动态的即时编组托管代码。主要有两大缺点:对即时编组需要时间和你真的不知道究竟是如何做到这一点的复杂数据类型(即,一切还有什么比int或字符串不同)之间。所以C ++ / CLI它确实是一个很好的选择,因为你可以混合托管和非托管代码。

Remark D: Practically, the problem using c++ code in c# has two solutions: the one I described and the direct calling to c++ unmanaged code from c# managed code using dynamic on-the-fly marshalling. There are two major disadvantages: on-the-fly marshalling takes time and you do not really know how exactly do this between complex data types (namely, everything what is different than int or string). So c++/cli it is really a good option because you are able to mix managed and unmanaged code.

备注E:这个解决方案是通用的,它不仅在OpenCV的计数。我成功地使用本作C ++ / CLI包装旋转阶段(机动设备),它只有C ++驱动程序和使用,在C#代码驱动程序。

Remark E: This solution is general, it does not count only in opencv. I successfully used this making c++/cli wrappers to rotation stages (motor devices) which only have c++ drivers and used that drivers in c# code.

关于备注C:使用的Dependency Walker像安装depends( HTTP: //www.dependencywalker.com/ ),看看哪些依赖是不相符的。

Regarding Remark C: Use a dependency walker like Depends (http://www.dependencywalker.com/) to see which dependencies are not consistent.

这篇关于创建C ++ / CLI OpenCV的包装在C#中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆