2

I have a simple program which takes a video and plays it (though it does some image processing on the video). The video can be retrieved from a Dialog Box result, or directly by giving the path of the file. When I use cv::CvCapture capture1, I get the properties like capture1.isOpen(), capture1.get(CV_CAP_PROP_FRAME_COUNT) etc. but when I use CvCapture* capture2 I get weird errors.

I want to use cv::CvCapture capture1 because of my functions are in accordance with capture1. Or is there any way to use both types with some kind of conversion between them like type casting or something else.

Actually I had two programs, the functions of the program1 was for cv::CvCapture and the functions of the program2 was for CvCapture*. I mean the two programs read the video file in different manners.

I then merged these two programs to use some functions from program1 and some functions from program2. But I can't convert cv::CvCapture to CvCapture*.

I am using OpenCv with Qt Creator.

My code is very long to post here but I have simplified my code to make it smaller and understandable. My code may not compile correctly because I modified it to make it simpler.

Any help would be appreciated. Thanks in advance :)

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.
  }
karlphillip
  • 92,053
  • 36
  • 243
  • 426
Ruhi Akaboy
  • 475
  • 3
  • 8
  • 17

1 Answers1

8

cv::VideoCapture is from the C++ interface of OpenCV, and can be used to capture from a camera device and from a file on the disk

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

and cvCaptureFromCAM() is a function from the C interface of OpenCV that is used only to capture from a camera device:

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

Don't mix/merge this interfaces together, pick one and stick with it.

If you want to use the C interface to capture from a video file, use cvCaptureFromFile() instead:

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

Check these examples:

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 2
    **karlphillip** thank you very much for your answer, now if I choose any of them then I will have to modify my functions according to that, because the functions related with **cvCapture*** do not work with **cv::videoCapture**, I think there are equivalent functions for both of them doing the same task. I must look for those. anyways, Thanks alot – Ruhi Akaboy Jun 06 '12 at 08:07