Search code examples
opencvimage-processingc++-cliemgucv

How do I convert Emgu::CV::Mat to OpenCV cv::Mat and vice versa?


I have to use OpenCV in both managed and unmanaged code in C++/CLI.

I'm trying Emgu CV in the managed code to wrap around the OpenCV objects but I'm having trouble doing the conversions.

How do I do something like this:

Emgu::CV::Mat convert = Function_That_Returns_OpenCV_CV_Mat();

or this:

Function_That_Takes_OpenCV_CV_Mat(Emgu_CV_Mat_variable);

?


Solution

  • From OpenCV Mat to Emgu Mat:

    cv::Mat openCvMat = Function_That_Returns_OpenCV_CV_Mat();
    System::IntPtr openCvMatSystemIntPtr = openCvMat.ptr;
    Emgu::CV::Mat^ result = 
       Emgu::CV::CvInvoke::CvArrToMat(
          openCvMatSystemIntPtr, false, false, 0);