Ask Your Question
0

How to start camera in OpenCV and draw a rectangle on a detection? Nullpointer exception.

asked 2013-04-23 11:00:38 -0600

Adrianos01 gravatar image

updated 2013-04-23 11:07:27 -0600

I want to start my Webcam and draw a Rectangle based on the regions returned by face detection. The compiler of Code::Blocks says no error while compiling. If i want to "Run" the Code the webcam started and in a few second after stoped it and i get this Error in Terminal:

http://imageshack.us/photo/my-images/835/20130423173054486x346sc.png/

So i have the following code:

#include "/usr/local/include/opencv/cv.h"
#include "/usr/local/include/opencv/highgui.h"

#include <iostream>
#include <cstdio>

using namespace std;
using namespace cv;

CvRect *r;

const string haarcascade_face = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml";
const string haarcascade_eye = "/usr/local/share/OpenCV/haarcascades/haarcascade_eye_tree_eyeglasses.xml";

////String window_name = "Capture - Face detection";

CvHaarClassifierCascade* cascade;
CvMemStorage* storage;

void detectAndDisplay( IplImage* img);


int main()
{
    CascadeClassifier cascade_face, cascade_eye;
    char c;

    cascade = (CvHaarClassifierCascade*)cvLoad("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml");
    CvCapture *capture = 0;



    IplImage *frame;


    if(!cascade_face.load(haarcascade_face))
    {
        cout << "[ERROR]: Could not load classifier cascade Face" <<endl;
        return -1;
    }

    if(!cascade_eye.load(haarcascade_eye))
    {
        cout << "[ERROR]: Could not load classifier cascade Eye" <<endl;
        return -1;
    }

    capture = cvCaptureFromCAM(0); // select camera
    if(capture == NULL)
    {
        cout << "[ERROR] No camera found!" << endl;
        return -1;
    }


    // loop forever till ESC
    for(;;)
    {
        // capture a frame from the camera
        frame = cvQueryFrame(capture);

        // display it
        if(!frame) break;


            detectAndDisplay(frame);


        // test for ESC
        c = cvWaitKey(33);
        if(c == 27 /* ESC */)
           break;
    }

    return 0;
}


void detectAndDisplay(IplImage* img)
{

    int i;

    CvSeq* faces = cvHaarDetectObjects(img, cascade, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize (100, 100));


    for(i = 0; i<(faces ? faces->total:0); i++)
    {

         r=(CvRect*)cvGetSeqElem(faces,i);
         cvRectangle(img,
                     cvPoint(r->x, r->y),
                     cvPoint(r->x + r->width, r->y + r->height),
                     CV_RGB(255,0,0), 1, 8, 0);
                     }


   cvShowImage("Capture - Face Detection", img);
}

I dont know what the problem is, can someone help me solve this problem?

Thank you for helping me!!!

edit retag flag offensive close merge delete

Comments

I tried your program and the null pointer exception was resolved just giving memory to the storage in the loop: storage = cvCreateMemStorage(0);

Hope will help you!

Taake gravatar imageTaake ( 2013-09-19 17:00:38 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
3

answered 2013-04-23 11:11:06 -0600

First of all, I am seeing that you are using the old C-style API. If you haven't go tons of experience with programming in C, it is the ideal place to get nullpointer exceptions. Basically this is caused by dangling pointers (pointers that refer to nothing or empty memory).

If you however, do not mind to go to C++, take a look at this guide. It is way more clear then your code and will get you setup pretty fast.

As for your problem, could you indicate the exact location in your code of where the debugging points you too?

edit flag offensive delete link more

Comments

I know this guide and i can compile it. My code is the same code in the link but i want to draw a rectangle not a circle.

Adrianos01 gravatar imageAdrianos01 ( 2013-04-23 11:48:52 -0600 )edit

Then in that code change the following line

 ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

into

 rectangle( frame, face[i].x, face[i].y, face[i].x + face[i].width, face[i].y + face[i].heigth, Scalar(0,0,255));
StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-23 11:53:20 -0600 )edit

hmm thank u it will works :)

   rectangle( frame, faces[i].x, faces[i].y, faces[i].x + faces[i].width, faces[i].y + faces[i].height, Scalar(0,0,255));

I am sitting her and to solve this compiler error:

Error: no matching function for call to 'rectangle (cv :: Mat &, int &, int &, int, int, cv :: Scalar) Note: void cv :: rectangle (cv :: Mat &, cv :: Point cv :: Point, Scalar const &, int, int, int) Note: no known conversion for argument 2 of 'int' to 'cv :: Point Note: no known conversion for argument 2 of 'int' to 'cv :: Rect

Adrianos01 gravatar imageAdrianos01 ( 2013-04-23 12:52:34 -0600 )edit

So her is the solution:

   rectangle( frame, cvPoint(faces[i].x, faces[i].y), cvPoint(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0,0,255),1,8,0);

Thank you Steven :)

Adrianos01 gravatar imageAdrianos01 ( 2013-04-23 12:56:55 -0600 )edit

Ok excuse me, forgot to put them into points :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-23 13:40:58 -0600 )edit
-1

answered 2013-09-24 16:31:56 -0600

Taake gravatar image

I tried your program and the null pointer exception was resolved just giving memory to the storage in the loop:

storage = cvCreateMemStorage(0);

Hope will help you!

edit flag offensive delete link more

Comments

I do not stimulate this solution, because it is actually suggesting people to stick to a C-style API, which will soon loose support ...

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-30 07:50:12 -0600 )edit

Question Tools

Stats

Asked: 2013-04-23 11:00:38 -0600

Seen: 11,168 times

Last updated: Sep 24 '13