EMGU (openCV) 在 C# 和 C++ 之间来回传递图像

EMGU (openCV) Passing a image back and forth between C# & C++

本文关键字:之间 图像 C++ openCV EMGU      更新时间:2023-10-16

我试图做的事情在概念上似乎很简单。但是,我很难处理它(底部的错误消息)。我使用EMGU文档和之前的问题解决了大部分问题。

我有一个带有Imagebox控件和按钮的windows窗体。点击按钮,我想加载一个图像(从文件),并将其传递到C++dll,该dll可以获取图像文件,并将相同的文件传递回C#程序,该程序可以显示在Imagebox控件上

C#

    private void rbtnLoadImage_Click(object sender, EventArgs e)
    {
        Image<Bgr, Int32> img1 = new Image<Bgr, Int32>("abc_color.jpg");
        IntPtr pnt = CvInvoke.cvCreateImageHeader(new Size(img1.Width, img1.Height), IPL_DEPTH.IPL_DEPTH_32S , img1.NumberOfChannels);
        Marshal.StructureToPtr(img1.MIplImage, pnt, false);
        MIplImage mptr = (MIplImage)Marshal.PtrToStructure(testIplImagePass(pnt), typeof(MIplImage));
        Image<Bgr, Int32> img2 = Image<Bgr, Int32>.FromIplImagePtr(mptr.imageData);
        imgbox.Image = img2;
    }
    [DllImport("xyz.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr testIplImagePass(IntPtr imagevar);

C++

    extern "C" { __declspec(dllexport) IplImage* testIplImagePass(IplImage* imagevar); }
    IplImage* testIplImagePass(IplImage* imagevar)
    {
        return imagevar;
    }

我得到的错误消息是"在Emgu.CV.dll中发生类型为"System.NotImplementedException"的未处理异常。其他信息:未实现239249通道映像的加载。"

它发生在线上

    Image<Bgr, Int32> img2 = Image<Bgr, Int32>.FromIplImagePtr(mptr.imageData);

我在这里做错了什么?欢迎提出任何建议。

此方法很简单,最少只需要一个映像数据副本(在C#中,从托管内存到非托管内存)。

此外,它是安全的代码(没有C#指针或修复)。

该代码是为<Bgr, Byte>量身定制的。对于普通版本,您还需要传递元素大小和编号C#和C++之间的通道数。

C#部件:

Image<Bgr, Byte> img = ImageSource();
int colWidth = img.Cols * 3;
int img_size = img.Rows * colWidth;
IntPtr unmanaged = Marshal.AllocHGlobal(img_size);
//Copy img data into unmanaged memory
for (int i = 0; i < img.Rows; i++)
    Marshal.Copy(img.Bytes, i * img.MIplImage.WidthStep, img_unmanaged + i * colWidth, 
                 colWidth);
//Call c++ function with
//void FooCpp(IntPtr unmanaged, int rows, int cols);
FooCpp(unmanaged, rows, cols);
//If the values for cols and rows have changed you need to pass them back to c# 
int newColWidth = newCols*3;
//Get back the data: outImg constructor DOES NOT COPY the data
//You can directly use outImg as long as unmanaged lives.
Image<Bgr, Byte> outImg = new Image<Bgr, Byte>(newCols, newRows, newColWidth, unmanaged);
img = outImg.Clone();
Marshal.FreeHGlobal(unmanaged);
unmanaged = IntPtr.Zero;
//continue to use img here

C++部分:

void FooCpp(unsigned char* imgPtr, int rows, int cols){
    //Optionally: create a cv::Mat image or just use the image data         
    cv::Mat img(rows, cols, CV_8UC3, imgPtr);
    //use and modify img here. 
    //Can also use imgPtr directly as long as you know what you're doing
    //Passback: you can relax row and cols limits as long as the size is smaller
    int newRows = std::min(rows, img.rows);
    int newCols = std::min(cols, img.cols);
    if (img.elemSize() == 3)
        for (int i = 0; i < newRows; i++)
            memcpy(imgPtr + i*newCols*img.elemSize(), img.data + i*img.step, 
                   newCols*img.elemSize());
}