Home c++ opencv read(frame) freezes
Reply: 0

c++ opencv read(frame) freezes

user4408 Published in March 19, 2018, 12:51 am

I got a problem with opencv2 reading a videoCapture stream in C++. I'm using Ubuntu in a VirtualBox on MacOS Sierra and


just freezes without any error in one of my programs. I checked it with a cout before and after that line.

In a different, much simpler program it works fine. First the simple and working code:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main() {
    VideoCapture stream1(0);   //0 is the id of video device.0 if you have only one camera.

if (!stream1.isOpened()) { //check if video device has been initialised
    cout << "cannot open camera";

//unconditional loop
while (true) {
    Mat cameraFrame;
    imshow("cam", cameraFrame);
    if (waitKey(30) >= 0)
    return 0;

Here I get a stream from my webcam.

And here a part of my code that is not working:

 VideoCapture stream1(0);
 stream1.set(CV_CAP_PROP_FRAME_WIDTH, img_width);
     std::cout << "[cvLoop] ERROR: The camera could not be found!" <<std::endl;

 // Set the resolution of the real camera to be 320x240 pixels
 //cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, img_width);
 //cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT,img_height);

 // The capture for the camera could not be initialized -> Is the camera connected?
 /*if( !capture )
     std::cout << "[cvLoop] ERROR: The camera could not be found!" <<std::endl;

// Set the desired feature values in opencv frame (origin in the upper left corner)
PrVector3 desired_s_opencvf;
desired_s_opencvf[0] = img_width*0.5;
desired_s_opencvf[1] = img_height*0.5;
desired_s_opencvf[2] = diameter_desired_px;

// Set the parameters for visual servoing
initVisualServoing(f, img_width, img_height, diameter_real, diameter_desired_px, dt, desired_s_opencvf);

// Create windows to display images
cv::namedWindow( VS_WINDOW_NAME, 1 );

// Set the mouse callback to select objects on the VS image
cv::setMouseCallback( VS_WINDOW_NAME, onMouse, 0 );

//Mat frame;
//imshow("cam", frame);

    Sleep(startTimeOfLastSlice, dt);
    // Make a local copy of the robot state (protected r/w operation using the mutex)
    // get the opengl image
    if (simulated_frame) {delete simulated_frame;} //throw away last frame
    cv::Mat* simulated_frame=getSimImg();
    iplimg = IplImage(*simulated_frame); //Backwards compatibility, TODO: remove
    //frame = &iplimg;                    //pointer does not actually change, just to make it clear here where it points to
     // get the camera image
     // new
     // Mat frame;
    cout << "1" << endl;
    //imshow("cam", frame);
    cout << "2" << endl;
    cout << "before" << endl;
    //frame = cvQueryFrame( capture );
    cout << "after" << endl;

As you may see, there are lots of comments because I tried some things I found googling the problem. Unfortunately, I'm not able to show the whole code, but this is part of a loop function in a bigger program and it works till there and everything afterwards is not even executed because the program freezes in the first loop at


You might see the commented line with

//frame = cvQueryFrame( capture );

That is from a previous version where it did not work either, so I changed it to the C++ version but with no success.

And the missing

Mat frame;

is done at the beginning, so it's not in the code I posted here.

I hope I did not forget any important information.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312524 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO