opencv中的分段错误(核心转储)

时间:2014-01-06 11:15:55

标签: segmentation-fault coredump

我正在尝试使用opencv执行程序来显示视频文件。该程序编译正确。但我执行它我得到错误:分段错误(核心转储)。你可以找到错误..

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <stdlib.h>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/opencv.hpp>
    #include <iostream>
    #include <fstream>
    #include <stdio.h>

    using namespace cv;
    using namespace std;

    int main(int argc, char *argv[])
    {
        if (argc != 2)
        {
              printf("Usage:video\n");
              return -1;
        }
        VideoCapture capture("Home//cuda-workspace//DisplayVideo//video_1.avi");
        namedWindow("display", cv::WINDOW_AUTOSIZE);


        if(!capture.isOpened())
        {
              printf("Failed to open the video\n");
              return -1;
        }

        for(;;)
        {
              Mat frame;
              capture >> frame; // get a new frame from camera
              imshow("display",frame);
        }

    return 0;
        }

4 个答案:

答案 0 :(得分:0)

无限循环?!!! 我不认为这是正确的!你没有退出。

for(;;)
{
    Mat frame;
    capture >> frame; // get a new frame from camera
    imshow("display",frame);
}

答案 1 :(得分:0)

请您详细说明您的错误更具体意味着程序崩溃的哪一行?

无论如何,我已经重新编写了你的​​程序,请检查它。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdlib.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
      printf("Usage:video\n");
      return -1;
    }

    VideoCapture capture("Home//cuda-workspace//DisplayVideo//video_1.avi");


    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

    cout << "Frame per seconds : " << fps << endl;

    namedWindow("display", cv::WINDOW_AUTOSIZE);


    if(!capture.isOpened())
    {
      printf("Failed to open the video\n");
      return -1;
    }

    while(1)
    {
        Mat frame;

        bool bSuccess = capture.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "can't read the frame from video file" << endl;
            break;
        }

        imshow("display",frame);
    }

    return 0;
}

答案 2 :(得分:0)

在你的程序中为什么要遍历1000次循环?直到视频的最后一帧才会遍历并不是真的。所以请在您的代码中进行以下更改并再次检查。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdlib.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    printf("Usage: %s video\n", argv[0]);

    if(argc != 2)
    {
        printf("\nPlease provide video path \n");
        return -1;
    }
    else
    {
        printf("\nyour video file path :%s \n",argv[1]);
    }

    VideoCapture capture(argv[1]);

    namedWindow("display",cv::WINDOW_AUTOSIZE);

    printf("2\n");

    if(!capture.isOpened())
    {
        printf("Failed to open the video\n");

        return -1
    }

    while(1)
    {
        Mat frame;

        bool bSuccess = capture.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "can't read the frame from video file" << endl;
            break;
        }

        imshow("display",frame);
    }

    return 0;
}

答案 3 :(得分:0)

我不知道你是否解决了问题。 但我处理视频的方法如下:

VideoCapture capture("//Home//cuda-workspace//DisplayVideo//video_1.avi");

if(!capture.isOpened())
{
  return -1;
}

namedWindow("display"); 
Mat frame;
while(1)
{
    capture >> frame; // get a new frame from camera
    if (frame.empty()) //When it finish the video it breaks out of the cycle
    {
        break;
    }
    imshow("display",frame);
    waitKey(30);    // This is for you can visualize the video, 
        //otherwise the reading process is very fast 
}

我希望它适合你:) 有关waitkey使用的更多信息,您可以查看文档waitKey documentation