c#与c++通过DLL互传图片数据
感谢网友Zhao-Michael参考示例1. C#Pass Image Data to C++ :[DllImport(DllName, EntryPoint = "DetectImage")]public static extern int DetectImage(IntPtr Image, int wid, int hei, int step);//Dll Invoke 函数签名使用方法一`
·
感谢网友Zhao-Michael代码贡献。
参考示例
1. C# Pass Image Data to C++ :
在C++定义DetectImage函数作为接收图片数据的处理
Mat img;
IMGAPI DetectImage(int* image, int wid, int hei, int step)
{
Mat img(Size(wid, hei), CV_8UC1, image, step);
imshow("", img);
return 0;
}
C#调用之前要引用C++项目生成的DLL
[DllImport(DllName, EntryPoint = "DetectImage")]
public static extern int DetectImage(IntPtr Image, int wid, int hei, int step); //Dll Invoke 函数签名
使用方法一
Image<Gray, byte> im = new Image<Gray, byte>("D:/taylorswift.jpg");
IntPtr im_ptr = new IntPtr();
int step = 0;
Size size = new Size();
CvInvoke.cvGetRawData(im, out im_ptr, out step, out size); //这边的 Step 是大于Width的最小的 4 的整数倍的值
DllHandler.DetectImage(im_ptr, size.Width, size.Height, step);
使用方法二
Image<Gray, byte> im = new Image<Gray, byte>("D:/taylorswift.jpg");
var b_src = im.Bytes;
var len = b_src.Length;
IntPtr im_ptr = Marshal.AllocHGlobal(len);
Marshal.Copy(b_src, 0, im_ptr, len);
DllHandler.DetectImage(im_ptr, im.Width, im.Height, (im.Width / 4 + 1) * 4);
2. C++ Pass Image Data to C# :
C++
Mat img;
IMGAPI DetectImage(int & ptr, int & wid, int & hei)
{
img = imread("D:/taylorswift.jpg", 0);
ptr = (int)img.data;
wid = img.cols;
hei = img.rows;
return 0;
}
C#
[DllImport(DllName, EntryPoint = "DetectImage")]
public static extern int DetectImage(out IntPtr Image, out int wid, out int hei);
//使用
int step = 0;
int wid = 0;
int hei = 0;
IntPtr im_ptr = new IntPtr();
DllHandler.DetectImage(out im_ptr, out wid, out hei);
var im = new Image<Gray, byte>(wid, hei);
CvInvoke.cvSetData(im, im_ptr, wid);
ShowCVImage(im);
参考
更多推荐
已为社区贡献1条内容
所有评论(0)