Reputation: 3125
I have an openCv based dll, that connects to a camera. I then call a cv::mat
object into a C# application, and display the image as a Bitmap in a picturebox object.
This works, but the image occasionally 'glitches', showing flashes of lines, static and pops, every few seconds.
Is there a way I can check if the bitmap is valid before displaying it?
When i show the image in the dll, using cv::imshow
, it looks fine.
The code I have is:
in the 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;
}
in the C# side:
//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);
}
}
//the dll call
[DllImport("Vector.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr getArucoFrame();
As the image looks good in the dll, and glitchy in the picturebox, i am having trouble debugging this. Any help much appreciated. thank you.
Upvotes: 0
Views: 993
Reputation: 2146
The problem you have here is that you pass pointer to data of temporary image cv::Mat tmp;
into C#, but it gets freed on exit of getArucoFrame(void)
, thus it is dangling pointer. It may work, but seems it sometimes gets overwritten by new data. An easiest, but not the most optimal fix would be declaring it static static cv::Mat tmp;
so it gets freed on DLL unload.
Upvotes: 2