C# 调用 opencv 写的C++函数接口实现方法

  • C# 调用 C++ 函数实现方法

C# 调用 C++ 函数比较复杂的是结构体的封装,一般数据类型可以简单地转换为C# 的数据类型即可,对于结构体类型的传递或者是结构体中包含指针,结构体嵌套传递都会比较复杂、

结构体传递的方法例子:

C# 函数封装C++ 结构体 参考

https://blog.csdn.net/sdl2005lyx/article/details/6801113

图像二值化函数的C++接口封装例子:

C++ 函数接口

struct ImageRoi
{
    int x=0; //!< x coordinate of the top-left corner
    int y=0; //!< y coordinate of the top-left corner
    int width=0; //!< width of the rectangle
    int height=0; //!< height of the rectangle
    bool isSetRoi = 0;
};

struct ImageInfo
{
    int width=0;
    int height = 0;
    int pixelFormat= Gray8;
};
 

extern "C" __declspec(dllexport)  int  thresholdImage(Byte* srcData, ImageRoi roi, ImageInfo imginfo, int lowT, int highT, Byte* binData, Byte* resultData,bool isrpy);

 

C# 部分的接口代码:

 public struct ImageRoi
        {
            public ImageRoi(int x, int y, int w, int h,bool isSetRoi)
            {
                _x = x;
                _y = y;
                _width = w;
                _height = h;
                _isSetRoi = isSetRoi;
            }
            public int _x;
            public int _y;
            public int _width;
            public int _height;
            public bool _isSetRoi;
        };

        public struct ImageInfo
        {
            public ImageInfo(int w, int h, MyImageFormat px)
            {
                _width = w;
                _height = h;
                _pixelFormat = (int)px;
            }
            public int _width;
            public int _height;
            public int _pixelFormat;
        }

 

[DllImport(dllpath, EntryPoint = "thresholdImage", CallingConvention = CallingConvention.Cdecl)]
public static extern int thresholdImage(IntPtr srcData, ImageRoi roi, ImageInfo imginfo, int lowT, int highT, IntPtr binData, IntPtr resultData, bool isrpy);

其中图像数据指针即可以是IntPtr类型 ,也可以是byte[] (在C#代码中为字节数组指针);

结构体的定义和C++类似,传递的时候属于值传递,

图像数据返回也采用指针,在C#上层对数据进行转化显示结果图像。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 C#调用 OpenCV,你需要使用 OpenCV 的 C/C++ 库,并且使用 C# 的 P/Invoke(Platform Invocation Services)技术来将 C/C++ 函数导入到 C# 中。 以下是在 C#调用 OpenCV 的基本步骤: 1. 下载和安装 OpenCV。你可以从 OpenCV 官方网站下载最新版本。 2. 在你的 C# 项目中添加对 OpenCV C/C++ 库的引用。你可以将 OpenCV 的头文件和库文件直接复制到你的项目中,或者使用 NuGet 包管理器安装 OpenCV 库。 3. 在你的 C# 代码中使用 [DllImport] 属性声明需要调用的 C/C++ 函数。例如,如果你想调用 OpenCV 中的 cv::imread() 函数,则需要声明一个类似于下面这样的函数: ``` [DllImport("opencv_core240", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr cv_imread([MarshalAs(UnmanagedType.LPStr)]string filename, int flags); ``` 其中,`opencv_core240` 是 OpenCV 的库文件名,`cv_imread()` 是要调用函数名。 4. 在你的 C# 代码中调用你声明的函数。例如,如果你想使用 cv::imread() 函数读取一张图片并显示它,你可以编类似于下面这样的代码: ``` IntPtr imgPtr = cv_imread("test.jpg", 1); Mat img = new Mat(imgPtr); CvInvoke.Imshow("image", img); CvInvoke.WaitKey(0); ``` 其中,`test.jpg` 是要读取的图片文件名,`1` 是要传递给 `cv_imread()` 函数的标志参数。 以上就是在 C#调用 OpenCV 的基本步骤。请注意,由于 P/Invoke 是一个相对较低级的技术,因此你需要对 C/C++C# 之间的数据类型转换和内存管理有一定的了解,以确保你的代码正确且高效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值