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

c++ opencv read(frame) freezes

vega
1#
vega Published in 2017-12-07 17:46:20Z

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

stream1.read(frame);

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;
    stream1.read(cameraFrame);
    imshow("cam", cameraFrame);
    if (waitKey(30) >= 0)
        break;
    }
    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);
 stream1.set(CV_CAP_PROP_FRAME_HEIGHT,img_height);
 if(!stream1.isOpened())
 {
     std::cout << "[cvLoop] ERROR: The camera could not be found!" <<std::endl;
     return;
 }
 //end

 // 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;
     return;
  }*/
#endif

// 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( BACKPROJ_HIST_IMAGE_WINDOW_NAME, 1 );
cv::namedWindow( VS_WINDOW_NAME, 1 );

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

cv_running=true;
// LOOP
//Mat frame;
//waitKey(300);
//stream1.read(frame);
//imshow("cam", frame);

while(cv_running)
{
    //waitKey(300);
    Sleep(startTimeOfLastSlice, dt);
    // Make a local copy of the robot state (protected r/w operation using the mutex)
    pthread_mutex_lock(&mutexGVIn);
    GV_COPY(gv,gvIn);
    pthread_mutex_unlock(&mutexGVIn);
#ifdef USE_SIMULATED_CAMERA
    // 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
#else
     // get the camera image
     // new
     // Mat frame;
    cout << "1" << endl;
    //stream1.retrieve(frame);
    stream1.read(frame);
    //imshow("cam", frame);
    cout << "2" << endl;
    //end
    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

stream1.read(frame);

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.263951 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO