3

I am runing web-cam, trying to detect object in real time and I have this code running. It gives me error at video.main(video.java:92) - CvRect sq = cvBoundingRect(ptr, 0); But I am checking if(ptr != null) . I don't understand why.

CvMemStorage storage = CvMemStorage.create();
CvSeq contours = new CvContour(null);          
noOfContors = cvFindContours(imgbin, storage, contours, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, new CvPoint(0,0));


 for (ptr = contours;  ptr != null; ptr = ptr.h_next()) {     
    if(ptr != null){
         CvRect sq = cvBoundingRect(ptr, 0);
         if(sq.height()*sq.width() > minAreaa && sq.height()* sq.width() < maxAreaa){
            p1.x(sq.x());
            p2.x(sq.x()+sq.width());
            p1.y(sq.y());
            p2.y(sq.y()+sq.height());
            cvRectangle(img1, p1, p2, CV_RGB(255, 0, 0), 2, 8, 0);
        } 
    }
}

In command window:

 OpenCV Error: Null pointer (NULL array pointer is passed) in unknown function, file ..\..\..\src\opencv\modules\core\src\array.cpp, line 2382
Exception in thread "main" java.lang.RuntimeException: ..\..\..\src\opencv\modules\core\src\array.cpp:2382: error: (-27) NULL array pointer is passed

    at com.googlecode.javacv.cpp.opencv_imgproc.cvBoundingRect(Native Method)
    at video.main(video.java:92)
1
  • 1
    Error mentions NULL array pointer. Commented Dec 29, 2013 at 23:41

2 Answers 2

2

Solved the problem by adding an additional condition in for loop, but still don't know if it's a proper way to handle it:

CvSeq contours1 = new CvContour(null);
for (ptr = contours;  ptr != null && cvFindContours(imgbin, storage, contours1, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, new CvPoint(0,0)) != 0; ptr = ptr.h_next()){ .....

}
Sign up to request clarification or add additional context in comments.

Comments

0

Use the following instead:

 for (ptr = contours; ptr != null && !ptr.isNull(); ptr = ptr.h_next()) {

the !ptr.isNull() fixes the null pointer error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.