视频无法持续播放

时间:2013-08-21 09:22:59

标签: qt opencv

我正在使用opencv在Qt中播放视频。我有6个平铺的摄像机,我正在播放视频。问题是如果其中一个视频没有播放,即完成,那么GUI就会冻结并退出。我得到的错误是你必须重新实现QApplication :: notify()并在那里捕获异常。这该怎么做? 我使用的代码如下。

功能中的某个地方

 void MainWindow::ActivateWindow()
{
  //Some part of code to set Index for stacked widget

   if(stackWidget->currentIndex()==9)
   {
      const int imagePeriod == 1000/25;
      imageTimer->setInterval(imagePeriod);
      connect(imageTimer,SIGNAL(timeout()),this,SLOT(demoSlot());
      imageTimer->start();
   }
}

在插槽demoSlot

 void MainWindow::demoSlot()
{
   captureCamera1 cvCaptureFromFile("/root/mp.mp4");
   captureCamera2 cvCaptureFromFile("/root/mp.mp4");
   captureCamera3 cvCaptureFromFile("/root/mp.mp4");

   while(imageTimer->isActive())
   {
      frameCamera1 = cvQueryFrame(captureCamera1);
      frameCamera2 = cvQueryFrame(captureCamera2);
      frameCamera3 = cvQueryFrame(captureCamera2);

      sourceImageCam1 = frameCamera1;
      sourceImageCam2 = frameCamera2;
      sourceImageCam3 = frameCamera3;

      cv::resize(sourceImageCam1,sourceImageCam1,cv::size(400,100),0,0);
      cv::resize(sourceImageCam1,sourceImageCam1,cv::size(400,100),0,0);
      cv::resize(sourceImageCam1,sourceImageCam1,cv::size(400,100),0,0);

      cv::cvtColor(sourceImageCam1,sourceImageCam2,CV_BGR2RGB);
      cv::cvtColor(sourceImageCam2,sourceImageCam2,CV_BGR2RGB);
      cv::cvtColor(sourceImageCam2,sourceImageCam2,CV_BGR2RGB);

      QImage tempImage1 = QImage((const unsigned char* sourceImageCam1.data,sourceImageCam1.cols,sourceImageCam2.rows,QImage::Format_RG888);
      QImage tempImage2 = QImage((const unsigned char* sourceImageCam2.data,sourceImageCam2.cols,sourceImageCam2.rows,QImage::Format_RG888);
      QImage tempImage3 = QImage((const unsigned char* sourceImageCam3.data,sourceImageCam3.cols,sourceImageCam3.rows,QImage::Format_RG888);

      labelCameraCapture1->setPixmap(QPixmap::fromImage(tempImage1));     //label to display video
      labelCameraCapture2->setPixmap(QPixmap::fromImage(tempImage2));
      labelCameraCapture3->setPixmap(QPixmap::fromImage(tempImage3));

      lblCameraCapture1->resize(lblCameraCapture1->Pixmap->size());
      lblCameraCapture1->resize(lblCameraCapture1->Pixmap->size());
      lblCameraCapture1->resize(lblCameraCapture1->Pixmap->size());

      cvWaitkey(20);
      qApp->processEvents();
    }
 if(imageTimer->isActive())
 {
   imageTimer->stop();
 }
 else
 {
   imageTimer->start();
 }
}

在头文件

   cvCapture *captureCamera1;
   cvCapture *captureCamera1;
   cvCapture *captureCamera1;

   IplImage frameCamera1;
   IplImage frameCamera2;
   IplImage frameCamera3;

   cv::Mat sourceImageCam1;
   cv::Mat sourceImageCam2;
   cv::Mat sourceImageCam3;

2 个答案:

答案 0 :(得分:1)

这样做可以改变3部电影很简单。

class MainWindow : public QMainWindow {
Q_OBJECT

   explicit QMainWindow(QWidget *parent) ....
   // prepare timer and so on

public slots:
   void startVideo() {
      vid1.close();
      vid1.open("/root/mp.mp4");
      imageTimer->start();
   }

   void demoSlot() {
      cv::Mat frame;
      vid1 >> frame;
      cv::cvtColor(frame,frame,CV_BGR2RGB);
      QImage img((uchar*) frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
      label1->setPixmap(QPixmap::fromImage(img));
   }

private:
   ...
   QTimer *imageTimer;
   cv::VideoCapture vid1;
};

答案 1 :(得分:0)

检查从相机捕获的帧是否为NULL。然后只需跳过此相机的处理步骤。

最好不要混用C ++和C接口(我的意思是cv :: Mat和IplImage)。