简体   繁体   English

创建C ++ / CLI OpenCV包装器以在C#中使用

[英]Creating an C++/CLI OpenCV wrapper to use in C#

I want to create an OpenCV wrapper to use it in C#. 我想创建一个OpenCV包装器以在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. 我使用此链接作为参考http://drthitirat.wordpress.com/2013/06/06/use-opencv-c-codes-in-a-vc-project-solution-of-creating-a-managed- clr-wrapper /到目前为止,我已经创建了一个包含我的图像处理代码的C ++控制台应用程序。 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? 我还创建了一个C ++ / CLI类库,其中包含了OpenCV代码,但是当我尝试构建它时,我得到了许多关于C ++代码中使用的OpenCV函数的未解决的外部错误,我不知道如何解决它...知道如何解决这个问题吗? Is there a simpler, easier way of using C++ OpenCV code in C#? 在C#中使用C ++ OpenCV代码有一种更简单,更简单的方法吗? I don't want to use Emgu or any other wrapper, my image processing code has to be in C++. 我不想使用Emgu或任何其他包装器,我的图像处理代码必须使用C ++。

How I solved the problem with opencv using visual studio 2012: 我如何使用visual studio 2012解决opencv的问题:

  1. I created a c++ image processing library with opencv and compiled it to a static library (.lib) as mentioned here . 我创建的OpenCV一个C ++图像处理库和它编译成如所提到的静态库(.LIB) 这里 Basically, create an console application project and change in Project configuration -> General -> Configuration type to a "static library (.lib)". 基本上,创建一个控制台应用程序项目并将项目配置 - >常规 - >配置类型更改为“静态库(.lib)”。 That will compile your project to a .lib file which later you should use in c++/cli. 这会将您的项目编译为.lib文件,稍后您应该在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 . 然后我做了一个C ++ / CLI包装器 - 对于c ++中的每个类,我即将发布到c#我在这个链接中创建了一个包装器。 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. 我只是通过添加项目添加到项目 - >现有项目并从项目1中选择标题来使用来自c ++的现有标题。这也有一个优点,即如果你在.lib文件中更改了某些内容,那么你有相同的.h文件,所以当你重新编译.lib,你不必在c ++ / cli中更改标题。 I included .lib from 1. going to Project properties -> Linker -> Input -> Additional dependencies and put the path to the .lib file. 我将1.lib包含在1.项目属性 - >链接器 - >输入 - >附加依赖项中,并将路径放到.lib文件中。 This project was compiled to .dll file. 该项目被编译为.dll文件。 (Project configuration -> General -> Configuration type -> "dynamic library (.dll)"). (项目配置 - >常规 - >配置类型 - >“动态库(.dll)”)。
  3. In c# project I've just add to references the .dll and used classes from c++/cli , which are managed classes. 在c#项目中,我只是添加引用.dll和c ++ / cli中使用的类,它们是托管类。 And that is how magic works. 这就是魔法的作用。

Remark A: I can assure that this solution worked. 备注A:我可以保证这个解决方案有效。 I used features like pattern detection and camshift with opencv 2.4.2. 我使用了opencv 2.4.2等模式检测和camshift等功能。

Remark B: Another topic is how exactly marshalling should be made. 备注B:另一个主题是如何进行准确的编组。 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. 在简单数据类型的情况下,毫无疑问使用像UInt32等c ++ / cli数据类型。但问题是,如果你想传递更复杂的对象,如cv :: Mat,它们在c ++ / cli中没有直接等价的类型。 In that case, I made simplified version of such classes on c++/cli side. 在那种情况下,我在c ++ / cli方面制作了这类的简化版本。

Remark C: Don't mess architecture in different projects. 备注C:不要在不同的项目中混淆架构。 If you are using eg x86, be consequent in all projects. 如果您使用的是x86,则可以在所有项目中使用。

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. 备注D:实际上,在c#中使用c ++代码的问题有两个解决方案:我描述的那个和使用动态动态编组从c#托管代码直接调用c ++非托管代码。 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). 有两个主要的缺点:即时编组需要时间,你不知道复杂数据类型之间的确切做法(即,与int或字符串不同的一切)。 So c++/cli it is really a good option because you are able to mix managed and unmanaged code. 所以c ++ / cli它真的是一个很好的选择,因为你可以混合托管和非托管代码。

Remark E: This solution is general, it does not count only in opencv. 备注E:此解决方案是通用的,它不仅仅在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 ++ / cli包装器用于旋转阶段(电机设备),它只有c ++驱动程序并在c#代码中使用了这些驱动程序。

Regarding Remark C: Use a dependency walker like Depends ( http://www.dependencywalker.com/ ) to see which dependencies are not consistent. 关于备注C:使用Depends( http://www.dependencywalker.com/ )之类的依赖性walker来查看哪些依赖项不一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM