c++imread 函数_imreadC#调用C++、Opencv的代码可以通过托管和

C#调用C++、OpenCV的代码可以通过托管和非托管两种形式

非拖管的形式即是采用[DllImport]的形式,这种形式只能调用的C++的函数,

托管的形式用的是ref,可以调用C++类中的方法

推荐介绍下非托管的形式:

一、无参数传递下的非托管形式

(1).C++中建立“win32项目”为dll格式应用程序

(2).新建cpp程序add.cpp

代码如下:

extern "C" _declspec(dllexport) int add2(int x,int y)

{

return x+y;

}

(3).编译,将生成dll程序(debug目录下 )

(4).C#建立控制台应用程序

代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices; //一定得有

namespace CSDll

{

class Program

{

[DllImport("Dll2.dll",EntryPoint="add2",ExactSpelling=true,CallingConvention=CallingConvention.Cdecl)]

public static extern int add2(int a,int b);

static void Main(string[] args)

{

Console.WriteLine(add2(1, 2));

Console.Read();

}

}

}

(5).C++生成的dll一定要放在C#的bin目录下的debug中

二、传递opencv中的Mat图像

(1)C++ 工程下建立“win32项目”应用程序的dll

(2)创建cpp,代码如下:

#define DLL_API extern "C" _declspec(dllexport)

#include

#include "opencv2/imgproc/imgproc.hpp"

#include "opencv2/highgui/highgui.hpp"

#include

using namespace cv;

DLL_API uchar * _stdcall run1(char* filename, int & width, int & height, int & step )

{

IplImage* uu = cvLoadImage(filename);

IplImage* dst1 = cvCreateImage(cvSize(uu->width,uu->height),8,1);

cvCvtColor(uu,dst1,CV_RGB2GRAY);

Mat ss(dst1);

uchar * data = new uchar[dst1->height*dst1->width];

data= ss.data;

width = ss.size().width;

height = ss.size().height;

step = ss.step;

return data;

}

(3)编译,生成dll,并将dll放在C#所建工程的bin->debug目录下

(5)C#工程下创建窗体应用程序

首先传递C++中的彩,代码如下

using System;

本文来自电脑杂谈,转载请注明本文网址:

http://www.pc-fly.com/a/jisuanjixue/article-63301-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值