Ask Your Question

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

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

Adrianos01 gravatar image

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

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:

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;

        cout << "[ERROR]: Could not load classifier cascade Face" <<endl;
        return -1;

        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
        // capture a frame from the camera
        frame = cvQueryFrame(capture);

        // display it
        if(!frame) break;


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

    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++)

                     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


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 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

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

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


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


 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 -0500 )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 -0500 )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 -0500 )edit

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

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

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

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


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 -0500 )edit

Question Tools


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

Seen: 10,424 times

Last updated: Sep 24 '13