2

我有一个简单的程序可以拍摄视频并播放(尽管它对视频进行了一些图像处理)。可以从对话框结果中检索视频,也可以直接通过提供文件路径来检索视频。当我使用cv::CvCapture capture1时,我得到了像 capture1.isOpen()、capture1.get(CV_CAP_PROP_FRAME_COUNT)等属性,但是当我使用CvCapture* capture2时,我得到了奇怪的错误。

我想使用cv::CvCapture capture1因为我的功能符合capture1。或者有什么方法可以使用这两种类型并在它们之间进行某种转换,比如类型转换或其他东西。

实际上我有两个程序,program1 的功能是针对cv::CvCapture而 program2 的功能是针对CvCapture*。我的意思是这两个程序以不同的方式读取视频文件。

然后我合并这两个程序以使用程序 1 中的一些函数和程序 2 中的一些函数。但我无法将cv::CvCapture转换为CvCapture*

我将 OpenCv 与 Qt Creator 一起使用。

我的代码在这里发布很长,但我已经简化了我的代码以使其更小且易于理解。我的代码可能无法正确编译,因为我对其进行了修改以使其更简单。

任何帮助,将不胜感激。提前致谢 :)

void MainWindow::on_pushButton_clicked()
{

 std::string fileName = QFileDialog::getOpenFileName(this,tr("Open Video"), ".",tr("Video Files (*.mp4 *.avi)")).toStdString();

cv::VideoCapture  capture1(fileName);       // when I use the cv::VideoCapture capture it gives  an error

//error: cannot convert 'cv::VideoCapture' to 'CvCapture*' for argument '1' to 'IplImage* cvQueryFrame(CvCapture*)
    //CvCapture* capture2 = cvCaptureFromCAM(-1);
    // but when i use the CvCapture* capture2, it does not recognize capture2.isOpend() and capture2.get(CV_CAP_PROP_FRAME_COUNT) etc. don't work.
    // Is there any way to convert VideoCapture to CvCapture*?
    if (!capture.isOpened())
        {
            QMessageBox msgBox;
            msgBox.exec(); // some messagebox message. not important actually
        }
 cvNamedWindow( name );
 IplImage* Ximage = cvQueryFrame(capture);
 if (!Ximage)
   {
     QMessageBox msgBox;
     msgBox.exec();
    }

 double rate= capture.get(CV_CAP_PROP_FPS);
 int frames=(int)capture.get(CV_CAP_PROP_FRAME_COUNT);
int frameno=(int)capture.get(CV_CAP_PROP_POS_FRAMES);
 bool stop(false);

 capture.read(imgsize);

 cv::Mat out(imgsize.rows,imgsize.cols,CV_8SC1);
 cv::Mat out2(imgsize.rows,imgsize.cols,CV_8SC1);
        //I print the frame numbers and the total frames on  a label.
            ui->label_3->setText(QString::number(frameno/1000)+" / "+QString::number(frames/1000)); 
            ui->label->setScaledContents(true); 
            ui->label->setPixmap(QPixmap::fromImage(img1)); // here I show the frames on a label.
  }
4

1 回答 1

8

cv::VideoCapture来自 OpenCV 的C++ 接口,可用于从相机设备和磁盘上的文件中捕获

cv::VideoCapture  capture1(fileName); 
if (!capture.isOpened())
{
    // failed, print error message
}

并且cvCaptureFromCAM()是来自 OpenCV 的C 接口的函数,仅用于从摄像头设备捕获:

CvCapture* capture2 = cvCaptureFromCAM(-1);
if (!capture2)
{
    // failed, print error message
}

不要将这些接口混合/合并在一起,选择一个并坚持下去。

如果要使用 C 接口从视频文件中捕获,请cvCaptureFromFile() 改用:

CvCapture* capture  = cvCaptureFromFile(fileName);
if (!capture)
{
    // print error, quit application
}

检查这些示例:

于 2012-06-06T01:03:28.030 回答