opencv与emgucv的参数传递

来源:互联网 发布:京东标题优化格式 编辑:程序博客网 时间:2024/05/02 13:41

因为需要使用c#调用c++编写的dll,且dll中涉及opencv函数调用,因此参数的传递可以采用emgucv提供的接口,而不用傻乎乎的将c#中的结构转为字节,再在c++中重新转为opencv的结构。

Dll中的C++函数如下,功能就是彩色图像yuv空间的直方图均衡:

inthisto_enhance(IplImage* src, IplImage* dst) {                  cv::Mat img = cv::cvarrToMat(src);         std::vector<cv::Mat> out;         cv::Mat kk;         cv::cvtColor(img, kk, CV_BGR2YUV);         cv::split(kk, out);          // 彩色直方图均衡过程         cv::Mat colorimt;         cv::Ptr<cv::CLAHE> clahe =cv::createCLAHE();         clahe->setClipLimit(3);         clahe->setTilesGridSize(cv::Size(8,8));         // 对YUV中亮度通道进行直方图均衡         kk = out[0];         // colorimt为CLAHE处理后的亮度通道         clahe->apply(kk, colorimt);         out.at(0) = colorimt;         cv::Mat cc;         cv::merge(out, cc); // 将Y,U,V三个通道合并         // cv::imshow("YUV",cc);  //YUV图         // 需要把YUV格式转化回RGB格式         cv::cvtColor(cc, colorimt, CV_YUV2BGR);         IplImage tmp = colorimt;         *dst = tmp;         return 0;}


这里讲dst作为参数传递,而不是返回值,目的就是在c#代码内申请对象空间,否则在c++函数内 new出来的对象即使采用Marshal.PtrToStructure转换为托管代码,依然会有莫名其妙的错误。

C#中的相关调用代码如下:

IntPtr ptr = CvInvoke.cvCreateImage(CvInvoke.cvGetSize(frame),Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);//申请空间后再作为参数传递API.histo_enhance(frame.Ptr,ptr);//dll调用MIplImage mlI =(MIplImage)Marshal.PtrToStructure(ptr, typeof(MIplImage));//托管与非托管的转换Image<Bgr, Byte> outframe = newImage<Bgr, Byte>(mlI.width, mlI.height, mlI.widthStep, mlI.imageData);


 

其中API定义如下:

public class API{[DllImport("dll名称",EntryPoint = "histo_enhance", CallingConvention =CallingConvention.Cdecl)]public static extern inthisto_enhance(IntPtr src,IntPtr dst);}


1 0