【发布时间】:2018-02-01 10:36:40
【问题描述】:
我有一个基于 openCv 的 dll,它连接到相机。然后我将cv::mat 对象调用到C# 应用程序中,并将图像显示为图片框对象中的位图。
这可行,但图像偶尔会出现“故障”,每隔几秒就会出现线条闪烁、静电和爆裂声。
有没有办法在显示位图之前检查它是否有效?
当我在 dll 中显示图像时,使用cv::imshow,它看起来很好。
我的代码是:
在 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 中看起来不错,而在图片框中出现故障,我在调试时遇到了问题。非常感谢任何帮助。谢谢。
【问题讨论】:
标签: c# c++ winforms opencv picturebox