从 dll 调用 opencv 垫到 Windows 表单,图像出现故障
Calling an opencv Mat from a dll to windows forms, image is glitchy
我有一个基于openCv的dll,它连接到相机。然后,我将cv::mat
对象调用到 C# 应用程序中,并将图像显示为图片框对象中的位图。 这有效,但图像偶尔会出现"故障",每隔几秒钟就会显示线条、静态和爆裂的闪烁。
有没有办法在显示位图之前检查位图是否有效? 当我使用cv::imshow
在 dll 中显示图像时,它看起来不错。
我拥有的代码是:
在 C++ DLL 中:
__declspec(dllexport) uchar* getArucoFrame(void)
{
cv::Mat OriginalImg = returnLeftFrame(); // calls the frame from where the camera thread stores it.
cv::Mat tmp;
cv::cvtColor(OriginalImg, tmp, CV_BGRA2BGR);
//if I cv::imshow the Mat here, it looks good.
return tmp.data;
}
在 C# 端:
//on a button
threadImageShow = new Thread(imageShow);
threadImageShow.Start();
//show image frame in box
private void imageShow()
{
while(true)
{
IntPtr ptr = getArucoFrame();
if (pictureBoxFrame.Image != null)
{
pictureBoxFrame.Image.Dispose();
}
Bitmap a = new Bitmap(640, 360, 3 * 640, PixelFormat.Format24bppRgb, ptr);
pictureBoxFrame.Image = a;
Thread.Sleep(20);
}
}
DLL 调用
[DllImport("Vector.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr getArucoFrame();
由于图像在 dll 中看起来不错,并且在图片框中出现故障,因此我在调试时遇到问题。任何帮助非常感谢。谢谢。
您在这里遇到的问题是您将指向临时图像数据的指针cv::Mat tmp;
传递到 C# 中,但它在退出时被释放getArucoFrame(void)
,因此它是悬空指针。它可能有效,但似乎有时会被新数据覆盖。一个最简单但不是最佳解决方法是将其声明为静态static cv::Mat tmp;
以便在 DLL 卸载时释放它。
相关文章:
- 分段故障(堆芯转储)矢量
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- C++中的动态铸造故障
- 数组的指针从不分段故障
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- vscode g++链路故障:体系结构x86_64的未定义符号
- 访问被拒绝后,c++中的故障保护代码
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 将图像添加到资源文件夹UWP C++
- 从 dll 调用 opencv 垫到 Windows 表单,图像出现故障
- FloatType图像ITK上的分割故障
- IMG_Load故障(不支持图像格式)