EMGU (openCV) 在 C# 和 C++ 之间来回传递图像
EMGU (openCV) Passing a image back and forth between C# & C++
我试图做的事情在概念上似乎很简单。但是,我很难处理它(底部的错误消息)。我使用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());
}
相关文章:
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 使用 LoadToolbarEx 解决异常以及正常图像与大图像计数之间的差异
- OPENCV:如何使用5点算法从来自不同相机的两个图像之间的特征匹配来计算必需矩阵
- 从opencv的视频关注和标量拍摄的图像之间的整数划分
- 视差图和校正图像之间的叠加
- 如何在OpENCV中的图像中找到像素之间的欧几里得距离
- 如何在Python中实现ITK映像和SimpleItk图像之间的转换
- OpenCV Mahalanobis函数,用于计算两个图像之间的距离
- EMGU (openCV) 在 C# 和 C++ 之间来回传递图像
- 如何使用fftw包编写计算模板(T)和二维图像(亚胺)之间二维卷积的函数
- 图像操作2个图像之间的差异
- OpenCV:快速量化两个图像之间的差异
- 使用 lerp 在两个图像之间执行线性插值
- 计算图像对之间的几何变换
- 在 OpenCV / C++ 中对应两个图像之间的值
- Xcode 中多个图像之间的交集
- 如何找出两个图像之间的对象差异
- 图像深度和通道之间的差异
- 原始图像和调整大小的图像之间的关系(C++/OpenCV)
- 检测两幅图像之间的差异