使用OpenCV从立体相机保存帧

时间:2015-05-14 12:36:19

标签: c++ opencv camera-calibration

我正在尝试从立体网络摄像头捕获帧/图像。 imshow工作得很好但是当我按下捕获键99来保存两个相机的帧时它会给出异常

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

然后停止工作。请在这方面帮助我。我正在使用OpenCV_2.4.11和Visual Studio 2010.

这是我的源代码

//This works for me on OpenCV 2.0 with 2 Logicool webcams.

#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;
}

0 个答案:

没有答案
相关问题