Ask Your Question

OpenCV Error: Assertion failed BLA BLA BLA in unknown function...

asked 2013-10-02 23:13:16 -0500

Shaban gravatar image

updated 2013-10-03 00:34:45 -0500

Hi guys, I have a problem:

I wanna count the average color for each GBR (Green Blue Red) from a human. Firstly I wanna count the average color from the blob (I'm using "contour method"), and then I wanna count the average color from the rect where the human exist. But there's an error code:

OpenCV Error: Assertion failed (dims) = 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)size.p[1] in unknown function, file C:\OpenCV\include\opencv2\core/mat.hpp, line 459

I think the problems when I count the average color on the blob:

                        int totalx, totaly, totalz;
                        float avex, avey, avez;
                        totalx = 0;
                        totaly = 0;
                        totalz = 0;

                        for(int cpos = 0; cpos < contours[cnum].size(); cpos++){

                            //printf("[%d, %d]", contours[cnum][cpos].x, contours[cnum][cpos].y);
                            cv::Point3_ <uchar>* p = frame.ptr<cv::Point3_ <uchar> >(contours[cnum][cpos].x, contours[cnum][cpos].y);

                            int a = p -> x;
                            int b = p -> y;
                            int c = p -> z;

                            totalx += a;
                            totaly += b;
                            totalz += c;


                        avex = (float)totalx / contours[cnum].size();
                        avey = (float)totaly / contours[cnum].size();
                        avez = (float)totalz / contours[cnum].size();

                        std::cout << avex << std::endl;
                        std::cout << avey << std::endl;
                        std::cout << avez << std::endl;

Can you tell me why? I'll appreciate any help here.. Thanks! :)

And here we go whole my code:

// Shaban.cpp : Defines the entry point for the console application.


int main(int argc, char *argv[])
    cv::Mat frame;                                              
    cv::Mat fg;     
    cv::Mat blurred;
    cv::Mat thresholded;
    cv::Mat gray;
    cv::Mat blob;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Background Model"/*,CV_WINDOW_NORMAL*/);
    //cv::resizeWindow("Background Model",400,300);
    //cv::VideoCapture cap(0);  
    cv::VideoCapture cap("campus3.avi");    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 3;
        bgs.history = 1000;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

    std::vector<std::vector<cv::Point>> contours;               

    cv::CascadeClassifier human;


        cap >> frame;                           





        int cmin = 20; 
        int cmax = 1000;
        bool FOD1 = true;
        bool FOD2 = true;
        std::vector<cv::Rect> rects;

        for(int cnum = 0; cnum < contours.size(); cnum++){

            if(contours[cnum].size() > cmin && contours[cnum].size() < cmax){       

                human.detectMultiScale(frame, rects);   
                //printf("\nThe contour NO = %d size = %d \n", cnum, contours[cnum].size());

                if(rects.size() > 0){
                    for(unsigned int r = 0; r < rects.size(); r++) {

                        int totalx, totaly, totalz;
                        float avex, avey, avez;
                        totalx = 0;
                        totaly = 0 ...
edit retag flag offensive close merge delete



try to switch the x and y where you access the frame (frame.ptr...). In opencv points are usually x(col) and y(row), but for Mat access you need Mat(row, col).

Moster gravatar imageMoster ( 2013-10-03 01:28:20 -0500 )edit

Wow, I didn't notice that and you're right. Now my code is work! Thanks Moster! :D

Shaban gravatar imageShaban ( 2013-10-03 01:46:03 -0500 )edit

Can I make it an answer and you accep that? So the question shows up as answered.

Moster gravatar imageMoster ( 2013-10-03 01:54:48 -0500 )edit

Of course, you can make your comment as an answer and I'll accept that as a right answer.. ;)

Shaban gravatar imageShaban ( 2013-10-03 03:20:39 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-10-03 03:48:10 -0500

Moster gravatar image

updated 2013-10-03 03:49:09 -0500

ok, you simply need to swap the x and y while accessing your image. Points have x(col) and y(row), but in the Mat container you have (Rows, cols).

cv::Point3_ <uchar>* p = frame.ptr<cv::Point3_ <uchar> >(contours[cnum][cpos].y, contours[cnum][cpos].x);
edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2013-10-02 23:13:16 -0500

Seen: 3,550 times

Last updated: Oct 03 '13