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 <opencv2\opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <cv.h>
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;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ImageCS2
{
public partial class Form1 : Form
{
[DllImport("ImageCPP.dll")]
public static extern IntPtr run1(string a, out int width, out int height, out int step);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string filename;
OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
{
filename = op.FileName;
int width, height, step;
IntPtr dst = run1(filename, out width, out height, out step);
Bitmap img = new Bitmap(width, height, step, System.Drawing.Imaging.PixelFormat. Format24bppRgb, dst);
pictureBox1.Image = img;
}
}
}
}
(6) C++传递灰度图,在C#中的代码略有差别,代码如下:
[DllImport("raildetection.dll")]
public static extern IntPtr run1(string a , out int width, out int height, out int step);
private void button1_Click(object sender, EventArgs e)
{
string filename;
OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
{
filename = op.FileName;
int width, height, step;
IntPtr dst = run1(filename,out width,out height, out step);
Bitmap img = new Bitmap(width, height, step, <strong>System.Drawing.Imaging.PixelFormat.Format8bppIndexed</strong>,dst);
<strong>img.Palette = CvToolbox.GrayscalePalette;</strong>
pictureBox1.Image = img;
}
}
public static class CvToolbox
{
// #region Color Pallette
/// <summary>
/// The ColorPalette of Grayscale for Bitmap Format8bppIndexed
/// </summary>
public static readonly ColorPalette GrayscalePalette = GenerateGrayscalePalette();
private static ColorPalette GenerateGrayscalePalette()
{
using (Bitmap image = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
{
ColorPalette palette = image.Palette;
for (int i = 0; i < 256; i++)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
return palette;
}
}
}
三、托管形式调用C++中的代码:
(1)C++工程下建“win32项目”应用程序的dll
(2)建头文件:factory.h
#ifndef FACTORY_H
#define FACTORY_H
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
class factory
{
public:
void show();
};
#endif
(3)建CPP factory.cpp
#include"factory.h"
#include<opencv2/opencv.hpp>
using namespace cv;
void factory::show()
{
Mat img=imread("E://Image//1.jpg");
imshow("src",img);
waitKey(0);
}
(4)建头文件clrClass.h
#pragma once
#include"factory.h"
public ref class clrClass
{
public:
clrClass(void);
~clrClass(void);
int member;
void showImage();
private:
factory *clrFac;
};
(5)建CPP clrClass.cpp
#include"clrClass.h"
#include"factory.h"
clrClass::clrClass(void)
{
clrFac=new factory();
}
clrClass::~clrClass(void)
{
}
void clrClass::showImage()
{
clrFac->show();
}
(6)使用C++托管类进行封装。新增clrClass类。并且右击C++项目->属性->配置属性->公共语言运行时支持->公共语言运行时支持(、\clr),然后进行编译生成DLL。托管形式不是将生成的dll放在C#项目工程的bin->debug目录下,使用方法后面介绍
(7)C#创建控制台应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace gg2
{
class Program
{
[STAThread]
static void Main(string[] args)
{
clrClass clr = new clrClass();
clr.showImage();
}
}
}
(8)右击C#项目中的引用,将生成的dll添加在引用中,就可以使用C++的类方法了