多线程,借助Opencv在MFC的picture控件上不断刷新显示的问题
MFC显示图片是主线程,另开了一个线程采集图片放到一个全局变量(Mat类型)当中,MFC显示图片这个主线程将全局变量中的图片数据强制转化为IplImage 类,通过函数DrawPicToHDC显示到控件上。
图片数据转换及显示的代码如下:
IplImage depth_temp=IplImage(mDepth);
DrawPicToHDC(&depth_temp,IDC_PIC_DEPTH);
然后调用Invalidate(false);
不断在Onpaint函数中重绘达到连续显示图片成为连续画面的效果。
DrawPicToHDC函数如下:
void CPlatform_GameDlg::DrawPicToHDC(IplImage *img, UINT ID)
{
CDC *pDC=GetDlgItem(ID)->GetDC();
HDC hDC=pDC->GetSafeHdc();
CRect rect;
GetDlgItem(ID)->GetClientRect(&rect);
if(img->nChannels != 1)
{
cimg.CopyOf(img);
}
else
{
CvvImage img1;
img1.Create(img->width,img->height,8*3,img->origin);
cvConvertImage(img, img1.GetImage(), CV_GRAY2RGB);
cimg.CopyOf(img1, 0);
}
cimg.DrawToHDC(hDC,&rect);
ReleaseDC(pDC);
}
然后当开始运行的时候没问题,当程序运行时间稍长,不知道什么时候就出错误了,错误如下:
First-chance exception at 0x7538b760 in Platform_Game.exe: Microsoft C++ exception: cv::Exception at memory location 0x001ce188..
Unhandled exception at 0x7538b760 in Platform_Game.exe: Microsoft C++ exception: cv::Exception at memory location 0x001ce188..
求高人指点……?onpaint中直接调用invalidate(false);刷新界面貌似不太恰当,运行久了会出这个问题吗?