基于mfc 通过openCV播放avi视频及多显示窗口实现

OpenCV支持的AVI如下: Container FourCC Name Description AVI 'DIB ' RGB(A) Uncompressed RGB, 24 or 32 bit AVI 'I420' RAW I420 Uncompressed YUV, 4:2:0 chroma subsampled AVI 'IYUV' RAW I420 identical to I420

对于非上述格式的AVI需下载安装K-Lite_Codec_Pack_610_Full.exe。

 

在Picture控件显示视频或图片,图片和视频大小会自动适应该Picture大小。

主要代码如下:

void COpencvtestDlg::DrawPicToHDC(IplImage *img, UINT ID)
{
 CDC *pDC = GetDlgItem(ID)->GetDC();
 HDC hDC= pDC->GetSafeHdc();
 CRect rect;
 GetDlgItem(ID)->GetClientRect(&rect);
 CvvImage cimg;
 
 cimg.CopyOf(img);
 cimg.DrawToHDC(hDC,&rect);
 
 ReleaseDC(pDC);
}

void COpencvtestDlg::OnOpen()
{

// TODO: Add your command handler code here
// 文件打开对话框
 CString strAviFilePath;
 CFileDialog dlg(true,"*.avi",NULL,NULL,"*.avi|*.avi||");
 if (dlg.DoModal()==IDOK)
 {
  strAviFilePath = dlg.GetPathName();
 }else
 {
  return;
 }
 
 
    /*//显示图片代码
    IplImage *image=NULL; //原始图像
 if(image) cvReleaseImage(&image);
   
 CString filename;
 filename = "fruits.jpg";
 image = cvLoadImage(filename,1); //显示图片
 DrawPicToHDC(image, IDC_STATIC_PIC);
 */
     //视频播放
    pFrame = NULL;  
    if(pFrame)
    cvReleaseImage(&pFrame);
    pCapture = NULL;
 pCapture = cvCaptureFromFile(strAviFilePath);
    if(pCapture = cvCaptureFromFile(strAviFilePath))//加载视频文件,这里文件名为“3.avi”
 {
  while(pFrame = cvQueryFrame( pCapture ))    
  {
   DrawPicToHDC(pFrame, IDC_STATIC_PIC);
  }
 
 }
 else
 {
     MessageBox("打开AVI视频文件失败!"); 
 } 
}

问题:

1、该方法在播放时因程序执行在循环中会停止响应任何消息。

2、视频播放帧率明显快。

 

//视频播放方法二
    cvNamedWindow("Video", 1);
 cvMoveWindow("Video", 30, 0);
 if(pCapture = cvCaptureFromFile(strAviFilePath))//加载视频文件,这里文件名为“3.avi”
 {
  while(pFrame = cvQueryFrame( pCapture ))
  {
   //显示图像
   cvShowImage("Video", pFrame);
   //如果有按键事件,则跳出循环
   //此等待也为cvShowImage函数提供时间完成显示
   //等待时间可以根据CPU速度调整
   if( cvWaitKey(200) >= 0 )
   break;
  }
  //销毁窗口
  cvDestroyWindow("Video");

  // cvReleaseImage(&pFrame); //??运行错误

     cvReleaseCapture(&pCapture);
 }
 else
 {
  MessageBox("打开AVI视频文件失败!");
 }

该方法帧率正常

 

问题解决:

1.采用多线程技术解决,在创建线程中显示视频。可以实现多窗口显示。关键代码如下:

 pFrame = NULL;  
    if(pFrame)
    cvReleaseImage(&pFrame);
    pCapture = NULL;
 //pCapture = cvCaptureFromFile(strAviFilePath);
    if(pCapture = cvCaptureFromFile(strAviFilePath))//加载视频文件,这里文件名为“3.avi”
 {
        //创建线程
     CWinThread* pThread = AfxBeginThread(VideoProcess,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
     b_flagProcess = 1;

 
 }
 else
 {
     MessageBox("打开AVI视频文件失败!"); 
 }

 

UINT VideoProcess(LPVOID lpParameter)//必须声明为UINT类型
{
 COpencvtestDlg *TestDlg = (COpencvtestDlg*) lpParameter;
 while(pFrame = cvQueryFrame( pCapture ))    
 {
  TestDlg -> DrawPicToHDC(pFrame, IDC_RAW_PIC); 
  TestDlg -> DrawPicToHDC(pFrame, IDC_RESULT_PIC);
     Sleep(25);
 }   
 return 0;
}

 

2.cvWaitkey()函数在MFC中不能使用,改用Sleep();

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值