Ask Your Question
0

Error when running a simple program

asked 2012-09-30 11:56:17 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

Hey there, After writing this simple program that displays a video.


 include <opencv/highgui.h>

 int main( int argc, char** argv ) {
 cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE); 
 CvCapture *capture  = cvCreateFileCapture(argv[1]); 
 IplImage *frame;

 while(1){
         frame = cvQueryFrame(capture);
         if (!frame) break;
         cvShowImage("Example2" , frame);
         cvReleaseCapture(&capture);
         cvDestroyWindow("Example2");
         } 
}

I compiled it and run it without any errors. But at the second time I tried to run it again (Also compiled it with no errors), I got this error message:


linking pad video/x-raw-yuv

sink is already linked


Thanks in advance :)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2012-09-30 19:01:27 -0500

karlphillip gravatar image

There are several problems with your code:

  • Don't release the capture interface inside the loop!!! Do it only at the end of the application;
  • Code defensively: Always initialize your variables and check the return of the functions;
  • Add a call to cvWaitKey() after displaying the image from the camera;

You code would look something like this:

#include <highgui.h>
#include <stdio.h>

int main( int argc, char** argv ) 
{ 
    cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE); 
    CvCapture *capture = cvCreateFileCapture(argv[1]); 
    if (!capture)
    {
         printf("!!! Failed cvCreateFileCapture\n");
         return -1;
    }

    IplImage* frame = NULL;

    while(1) 
    { 
        frame = cvQueryFrame(capture); 
        if (!frame) break; 

        cvShowImage("Example2" , frame); 
        cvWaitKey(33);
    }

    cvReleaseCapture(&capture); 
    cvDestroyWindow("Example2"); 
    return 0;
}
edit flag offensive delete link more

Comments

Thank you very much, I really appreciate it. Can I have your mail also now I follow you on twitter. My twitter is SalehAbbasSaber. THanks again :)

salehali gravatar imagesalehali ( 2012-09-30 23:05:30 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2012-09-30 11:56:17 -0500

Seen: 374 times

Last updated: Oct 01 '12