c - OpenCV 2.1 : Runtime error

标签 c opencv runtime-error

我有一个使用 OpenCV 的程序。我有一个网络摄像头,它可以捕捉彩色帧,我想将彩色帧转换为灰度帧。因此,我使用了 cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY); 将彩色帧转换为黑白帧。

在使用这个颜色->灰度转换函数时,我得到一个运行时错误:

OpenCV Error: Null pointer (NULL array pointer is passed) in unknown function, file ..\..\..\..\ocv\opencv\src\cxcore\cxarray.cpp, line 2376

以前有人遇到过这个问题吗?任何解决方案如何解决这个问题?

谢谢

我的程序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>

#include"cv.h"
#include"highgui.h"
#include"cxtypes.h"

    #define ERROR -1
    #define NO_ERROR 1

int main()
{

    int EXIT_STATUS = NO_ERROR;

    int camera_index = 0;
    CvCapture   *capture = 0;
    IplImage *color_frame = NULL;
    IplImage *gray_frame = NULL;
    int exit_key_press = 0;

    capture = cvCaptureFromCAM(camera_index);
    cvNamedWindow("SURF", CV_WINDOW_AUTOSIZE);

            while(exit_key_press != 's')
            {

                /* Capture a frame */
                color_frame = cvQueryFrame(capture);
                if(color_frame == NULL)
                {
                    break;
                }
                else
                {
                   // When this line is enabled the runtime error occurs.
                    //cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
                    cvShowImage("SURF", color_frame );



                }
                exit_key_press = cvWaitKey(1);
            }

    cvDestroyWindow("Output");
    cvReleaseCapture(&capture);

    printf("\n\n~*~The END~*~");
    exit(EXIT_STATUS);

}

最佳答案

我看到很多人试图完成这个简单的任务,但遇到了麻烦。

所以我冒昧地将您的源代码更改为一个程序,该程序可以在屏幕上显示来自网络摄像头的灰度转换视频。

请使用此代码作为引用。

我在我的 Macbook Pro 上编译:

g++  -I/usr/include/opencv  -c gray.cpp -o gray.o -m32 -arch i386
g++  gray.o -o gray -L/usr/lib -lcxcore -lcv -lhighgui -lcvaux -lml -ldl -m32 -arch i386

文件:gray.cpp

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>

#include"cv.h"
#include"highgui.h"
#include"cxtypes.h"


#define ERROR -1
#define NO_ERROR 1


int main()
{
    int camera_index = 0;
    IplImage *color_frame = NULL;
    int exit_key_press = 0;

    CvCapture *capture = NULL;
    capture = cvCaptureFromCAM(camera_index);
    if (!capture)
    {
        printf("!!! ERROR: cvCaptureFromCAM\n");
        return -1;
    }

    cvNamedWindow("Grayscale video", CV_WINDOW_AUTOSIZE);

    while (exit_key_press != 'q')
    {
        /* Capture a frame */
        color_frame = cvQueryFrame(capture);
        if (color_frame == NULL)
        {
            printf("!!! ERROR: cvQueryFrame\n");
            break;
        }
        else
        {
            IplImage* gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);  
            if (gray_frame == NULL)
            {
                printf("!!! ERROR: cvCreateImage\n");
                continue;
            }

            cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
            cvShowImage("Grayscale video", gray_frame);
            cvReleaseImage(&gray_frame);
        }
            exit_key_press = cvWaitKey(1);
    }

    cvDestroyWindow("Grayscale video");
    cvReleaseCapture(&capture);

    return 0;
}

关于c - OpenCV 2.1 : Runtime error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3438500/

相关文章:

c++ - Opencv c++Template/Pattern 匹配缩放和旋转不变量

c++ - 未处理的异常,来源未知

c - 什么是在 C 中打开文件?

c - 从文本文件填充 C 中的二维数组

c - 无法链接 opencv 库

vba - 运行时错误 1004 : MS Excel is refreshing some data. 请稍后使用查询重试

java - UVA Online Judge 正在返回我找不到的运行时错误

c - Makefile FFLAGS 描述

c - Recv 调用获取空数据

opencv - 仅去除垂直 HoughLines/检测 Horizo​​ntal HoughLines