Saving frames from stereo cameras using OpenCV

asked 2015-05-14 07:45:00 -0600

I am trying to capture frames/images from stereo web camera. imshow work well but When I press capture key 99 to save frames form both cameras it give exception

Unhandled exception at 0x005a13af (msvcr100d.dll) in OpencvConfig.exe: 0xC0000005: Access violation reading location 0x7466656c

and stop working. Kindly help me in this regards. I am using OpenCV_2.4.11 and Visual Studio 2010.

Here is my source code:

#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;


int main(int, char**)
{
    //To capture images pre variables

    int key = 0;
    string nameL = "left";
    string nameR = "right";
    char bufferL[20];
    char bufferR[20];
    int counter = 0;

    ////////////////////////////////

    cv::VideoCapture capLeft(0); // open the Left camera
    cv::VideoCapture capRight(1); // open the Right camera

    if(!capLeft.isOpened() || !capRight.isOpened())  // check if we succeeded
    {
        std::cerr << "ERROR: Could not open cameras." << std::endl;
        return -1;
    }


    cv::namedWindow("Left",1);
    cv::namedWindow("Right",1);

    for(;;)
    {
        bool isValid = true;


        cv::Mat frameLeft;
        cv::Mat frameRight;

        try
        {
          capLeft >> frameLeft; // get a new frame from left camera
          capRight >> frameRight; //get a new frame from right camera
        }
        catch( cv::Exception& e )
        {
          std::cout << "An exception occurred. Ignoring frame. " << e.err << std::endl;
          isValid = false;
        }

        if (isValid)
        {
          try
          {
            //Capture Frames and save them

            while(key!=27)
            {
                capLeft >> frameLeft; // get a new frame from left camera
                capRight >> frameRight; //get a new frame from right camera

                cv::imshow("Left", frameLeft);
                cv::imshow("Right", frameRight);

                if(key==99)
                {
                    sprintf(bufferL,"%s%s.ppm",nameL,counter);
                    imwrite(bufferL,frameLeft);

                    sprintf(bufferR,"%s%s.ppm",nameR,counter);
                    imwrite(bufferR,frameRight);
                }
                key = waitKey(700);
                counter++;
            } 

            /////////////// Cool stuff ends

          }
          catch( cv::Exception& e )
          {
            std::cout << "An exception occurred. Ignoring frame. " << e.err << std::endl;
          }
        }
        if(cv::waitKey(30) >= 0) break;
    }
     //the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
edit retag flag offensive close merge delete