使用Xcode的OpenCV中的空指针错误

时间:2015-02-12 01:34:44

标签: c xcode opencv

我是学生,用Xcode学习OpenCV。

在Googling的帮助下,我在搜索路径中包含了OpenCV文件目录,并添加了OpenCV框架。我在项目文件夹中包含了带有jpeg或png扩展名的测试图像,并将其加载到Xcode中。

我写下了基本的OpenCV代码,当我编译它时,"空指针错误"一直跟着。

#include <opencv/cv.h>
#include <opencv/highgui.h>

int main()
{
    IplImage *img = cvLoadImage("./ex2-4/background.jpeg", CV_LOAD_IMAGE_COLOR);

    if ( img == NULL ) {
        printf("Error : null pointer is entered\n");
    }

    cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
    cvShowImage("Example1", img);
    cvWaitKey(0);

    cvReleaseImage(&img);
    cvDestroyWindow("Example1");

    return 0;
}

错误信息如下所示:

错误:输入空指针 OpenCV错误:cvGetMat中的空指针(传递NULL数组指针),文件/opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.10/modules/core/src/array.cpp,line 2382 libc ++ abi.dylib:以cv类型的未捕获异常终止::异常:/opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.10/modules/core/src/array.cpp :2382:错误:( - 27)NULL数组指针在函数cvGetMat

中传递

程序以退出代码结束:9

PS:但是,它在Visual Studio中运行得很好,但我想在Xcode上运行。

1 个答案:

答案 0 :(得分:0)

即使您的问题已得到解答,但以下代码段仍是危险源:

if ( img == NULL ) {
        printf("Error : null pointer is entered\n");
    }

您知道imgNULL并继续执行。在这种情况下,应该终止执行,

if ( img == NULL ) {
        printf("Error : null pointer is entered\n");
        return -1;
    }