Ask Your Question
0

How to display a single frame from a webcam stream with the press of a key? It's showing assertion failed when I tried to compile the code.

asked 2015-03-02 06:28:10 -0500

Biswesh gravatar image

updated 2015-03-02 06:34:59 -0500

       #include "opencv2/highgui/highgui.hpp" 
       #include <iostream> 
       #include "opencv2/opencv.hpp" 
       using namespace cv; 
       using namespace std; 
        int main(int, char**) 
      { VideoCapture capturo(0); 
      Mat img1, img2; 
      int input; 
      namedWindow("Image 1",CV_WINDOW_AUTOSIZE); 
      namedWindow("Image 2",CV_WINDOW_AUTOSIZE); 
     for(;;){ 
     char input = cvWaitKey(33);
     if(input == 27) break;
     if (input == 49)
    { img1= capturo.grab();
        cv::imshow("Image 1",img1); }
      if (input == 50){ 
      img2= capturo.grab(); 
      cv::imshow("Image 2",img2); } } 
       return 0; }

Please tell what am I doing wrong here. I guess the first few frames is not read so the error is coming up. Can you please give a solution for this?

edit retag flag offensive close merge delete

Comments

And which assertion error does it give you? And where?

FooBar gravatar imageFooBar ( 2015-03-02 06:49:55 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-03-02 07:13:05 -0500

updated 2015-03-02 07:13:46 -0500

You are mixing up C and C++ code interfaces. Could you try this piece of code? Also many systems require you to and the output of the waitKey command with 0xFF.

   #include "opencv2/highgui/highgui.hpp" 
   #include <iostream> 
   #include "opencv2/opencv.hpp" 

   using namespace cv; 
   using namespace std; 

   int main(int, char**) 
   { 
        VideoCapture capturo(0); 
        Mat img1, img2; 
        int input; 
        namedWindow("Image 1",CV_WINDOW_AUTOSIZE); 
        namedWindow("Image 2",CV_WINDOW_AUTOSIZE); 
        for(;;){ 
             int input = 0xFF & waitKey(33);
             if(input == 27) break;
             if (input == 49){ 
                  capturo >> img1;
                  imshow("Image 1",img1.clone()); 
             }
             if (input == 50){ 
                   capturo >> img2;
                   imshow("Image 2",img2.clone()); 
             }
        } 
  return 0; 
  }
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-03-02 06:28:10 -0500

Seen: 125 times

Last updated: Mar 02 '15