【问题标题】:segmentation fault (core dumped) in opencvopencv中的分段错误(核心转储)
【发布时间】:2014-01-23 18:58:37
【问题描述】:

我正在尝试执行一个程序以使用 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;
        }

【问题讨论】:

  • 我们需要知道核心转储的确切位置(在哪一行)!

标签: segmentation-fault coredump


【解决方案1】:

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

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

【讨论】:

  • 现在我创建了 for 循环来迭代视频中的确切帧数,我没有收到任何错误,但视频窗口没有出现。我尝试显示帧的值,我得到了一个值矩阵,但没有出现窗口
  • 现在看来这都是不同的问题。打开提及它的新问题是个好主意。如果我们在这里混合了几个问题,可能会令人困惑..
【解决方案2】:

您能否更具体地说明您的错误意味着您的程序在哪一行崩溃?

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

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

【讨论】:

  • 感谢您的回复。但我仍然没有得到输出。仅显示 printf 行..
【解决方案3】:

在你的程序中为什么要遍历 for 循环 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;
}

【讨论】:

  • 嗨,我在代码中添加了这两行:capture.set(cv::CAP_PROP_FRAME_WIDTH, 640); capture.set(cv::CAP_PROP_FRAME_HEIGHT, 480);我没有收到任何错误,但没有显示视频..
【解决方案4】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-25
    • 2021-06-03
    相关资源
    最近更新 更多