如何将托管位图从 C# 传递到包装的非托管 OpenCV 的 Mat
How to pass managed Bitmap from C# to wrapped unmanaged OpenCV's Mat
我有一个由三部分组成的项目:
-
托管的C#项目有一个回调,它给了我一个位图(它应该有PixelFormat=Format24bppRgb)。我已经尝试了几种方法来将位图转换为我可以传递到第2部分的东西,这是我尝试的最后一件事:
public int BufferCB(IntPtr pBuffer, int BufferLen) { byte[] aux = new byte[BufferLen]; Marshal.Copy(pBuffer, aux, 0, BufferLen); String s_aux = System.Text.Encoding.Default.GetString(aux); wrappedClassInstance.GetBitmap(s_aux); }
-
托管C++/CLI包装项目3:
int WrappedClass::GetBitmap(array<System::Byte>^ s_in) { pin_ptr<unsigned char> pin = &s_in[0]; unsigned char* p = pin; return privateImplementation->GetBitmap(p); }
-
使用OpenCV的非托管C++应用程序。我想用将这些数据加载到Mat中
Mat myMat; int NativeClass::GetBitmap(unsigned char *s_in) { // If there's no input: if (!s_in) { return -1; } /* h and w are defined elsewhere */ myMat = Mat(h, w, CV_8UC3, (void *)s_in, Mat::AUTO_STEP)); imwrite("test.bmp", myMat); return 0; }
当到达imwrite函数时,将引发异常:"System.Runtime.InteropServices.SEHException(0x80004005)"。它没有说太多,但我猜我传递到Mat的数据在整理时已经损坏了。
以前,我试图在没有包装器的情况下传递数据:
[DllImport("mydll.dll", ...)]
static extern void GetBitmap(IntPtr pBuffer, int h, int w);
void TestMethod(IntPtr pBuffer, int BufferLen)
{
// h and w defined elsewhere
// GetBitmap is essentially the same as in item 3.
GetBitmap(pBuffer, BufferLen, h, w);
}
这起到了作用(它将位图保存到文件中),但由于DLL在我终止进程之前一直处于附加状态,因此该解决方案对我来说不够好。我也不想将Mat类"镜像"到我的项目中,因为我知道Mat应该接受来自某些字符*的数据。
请帮忙,我该怎么做?我是否进行了错误的类型转换?
谢谢。
我已经将第2部分从memcpy非托管内存更改为本机内存,只是为了确保这一点。但我的代码真正的问题是,每次我想得到一个新的Mat时,我都会调用Mat构造函数:
Mat myMat;
int MyClass::GetBitmap(unsigned char* s_in) {
myMat = Mat(...)
}
相反,我做了:
Mat myMat;
int MyClass::GetBitmap(unsigned char* s_in) {
Mat aux;
aux = Mat(...)
aux.copyTo(myMat);
aux.release();
}
现在我的代码运行得很好。
编辑:删除了一些使用new Mat(...)
的部分,这是一个拼写错误,它无法编译,因为我使用的是Mat,而不是Mat*。
相关文章:
- Difference in displaying cv2 Mat
- 如何在c++17中制作一个模板包装器/装饰器
- std::vector的包装器,使数组的结构看起来像结构的数组
- 如何在c++迭代器类型中包装std::chrono
- 是否可以用"iostream"包装现有的TCP/OOpenSSL会话
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 用pybind11包装C++抽象类时出错
- 为左值和右值的包装器实现C++范围
- C结构,其指针将被包装在unique_ptr中
- 如何包装第三方DLL在R中使用
- 将CHW格式的浮点向量转换为cv::Mat
- C++中的openCV Mat访问冲突
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 将 N-arg 函数包装到另一个函数中
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 包装一个对象并假装它是一个 int
- 为什么包装 setMouseCallback 会导致 Mat 对象为空
- 如何将托管位图从 C# 传递到包装的非托管 OpenCV 的 Mat
- 如何使用c++ Mat类ptr对象的基本C包装器打印Mat元素?
- Mat::at - error:类型的非const引用的无效初始化的包装器