views:

375

answers:

2

Hi Guys, I have a program which uses OpenCV. I have a webcam and it captures color frames and I want to convert the color frames to gray-scale frames. So, I used the cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY); to convert the color frames to BW frames.

Upon using this color->Grayscale conversion function, I get a runtime error as:

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

Anyone experienced this problem before? Any solutions how to fix this?

Thanks

My Program

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

}
+1  A: 

Don't you have to allocate your IplImage? It is not specified by the conversion function but I believe you have to allocate a dst the same size/type as the source.

code-gijoe
IplImage* gray_frame = cvCreateImage(cvSize(color_frame->width,color_frame->height),IPL_DEPTH_8U, 1);
code-gijoe
Hi... thanks for replying. The dst in my case is the gray_frame which is of the type IplImage, So I think that should not be a problem. I'm editing the program. Can you once try this new program on your system. It still doesn't work.
vikramtheone
IplImage *gray_frame = NULL;
code-gijoe
You HAVE to allocate the IplImage before handing it to the converter. You only need to allocate it once though.
code-gijoe
You are using the "C" api which is pointer based, so you have to make you allocations via allocating functions such as cvCreateImage.
code-gijoe
Code-gijoe, I tried, but once again my application is hanging, this time at the point where I have the line, IplImage* gray_frame = cvCreateImage(cvSize(color_frame->width,color_frame->height),IPL_DEPTH_8U, 1); Were you able to run your program after the change you have suggested? Can you please paste your program in your reply?
vikramtheone
Sorry my friend, I had made a minor mistake. But yes you were indeed right I did what you suggested correctly and it worked!!! Thank you so so so so much for your help. I was really lost.
vikramtheone
+1  A: 

I see a lot of people trying to do this simple task and having trouble with it.

So I took the liberty of changing your source code into a program that would display on the screen the grayscale converted video from the webcam.

Please use this code for reference.

I compiled on my Macbook Pro with:

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

File: 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;
}
karlphillip