当我使用OpenMP(C / C ++)运行我的代码OpenCV代码时,我遇到了致命的错误

时间:2014-09-17 08:05:06

标签: c opencv openmp

我想并行化这个程序的执行,但是当我运行它时,我遇到了致命的错误。使用C.Your帮助与OpenMP进行OpenCV并行化将不胜感激。我是OpenMP和OpenCV的新手。

#include <cv.h>
#include <cvaux.h>
#include <iostream>
#include <cxcore.h>
#include <highgui.h>
#include <omp.h>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
    int no_of_frames = 0,i,j;
    int fps = 0;
    int mid_frame = 0;
    CvCapture* capture = cvCaptureFromFile("/home/nagaraj/ImageVideo/tunnel.avi");
    CvCapture* captureNew = cvCaptureFromFile("/home/nagaraj/ImageVideo/tunnel.avi");
    if (capture == NULL)
    {
        printf("Error: Can't open video.\n");
        return -1;
    }
    if (capture == NULL)
    {
        printf("Error: Can't open video.\n");
        return -1;
    }
    fps = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
    no_of_frames = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
    mid_frame = no_of_frames/2;
    omp_set_num_threads(2);


#pragma omp parallel sections
    {
#pragma omp section
    {
        //cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,0);
        IplImage* img = cvQueryFrame(capture);
        cvNamedWindow("Window1",CV_WINDOW_AUTOSIZE);
        cvShowImage("Window1",img);
        cvWaitKey(10000);
        cvReleaseImage(&img);
        cvDestroyWindow("Window1");
        cvReleaseCapture(&capture);
    }
#pragma omp section
    {
        cvSetCaptureProperty(captureNew,CV_CAP_PROP_POS_FRAMES,(double)mid_frame-1);
        IplImage* img = cvQueryFrame(captureNew);
        cvNamedWindow("Window2",CV_WINDOW_AUTOSIZE);
        cvShowImage("Window2",img);
        cvWaitKey(10000);
        cvReleaseImage(&img);
        cvDestroyWindow("Window2");
        cvReleaseCapture(&captureNew);
    }
}
}

1 个答案:

答案 0 :(得分:1)

代码中存在几个潜在问题:

  • 调用cvReleaseImage()的最可能原因是释放您未创建的资源。在这种情况下,IplImagecvQueryFrame()在内部创建。这样做会导致您的应用程序崩溃。

  • fpsno_of_framesmid_frame的值打印到屏幕上。调用以检索这些值的函数可能会失败并返回0,这会导致应用程序出现问题,因为您将其中一些数据传递给cvSetCaptureProperty()

    < / LI>
  • 有时cvQueryFrame()可以返回NULL,表示检索框架时出现问题。将NULL框架传递给cvShowImage()也可能导致您的应用程序崩溃:

    IplImage* img = cvQueryFrame(capture);
    if (img == NULL) {
          // handle error
    }