Ask Your Question
0

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

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

Shaban gravatar image

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

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.

#include"stdafx.h"
#include<vector>
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>

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("Frame");   
    cv::namedWindow("Background Model"/*,CV_WINDOW_NORMAL*/);
    //cv::resizeWindow("Background Model",400,300);
    cv::namedWindow("Blob"/*,CV_WINDOW_NORMAL*/);
    //cv::resizeWindow("Blob",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;
    assert(human.load("hogcascade_pedestrians.xml"));

    for(;;){

        cap >> frame;                           
        cv::GaussianBlur(frame,blurred,cv::Size(3,3),0,0,cv::BORDER_DEFAULT);

        bgs.operator()(blurred,fg);                         
        bgs.getBackgroundImage(bgmodel);                                

        cv::erode(fg,fg,cv::Mat(),cv::Point(-1,-1),1);                         
        cv::dilate(fg,fg,cv::Mat(),cv::Point(-1,-1),3);       

        cv::threshold(fg,thresholded,70.0f,255,CV_THRESH_BINARY);

        cv::findContours(thresholded,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
        cv::cvtColor(thresholded,blob,CV_GRAY2RGB);
        cv::drawContours(blob,contours,-1,cv::Scalar(255,255,255),CV_FILLED,8);

        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 ...
(more)
edit retag flag offensive close merge delete

Comments

1

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

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-10-03 03:48:10 -0600

Moster gravatar image

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

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

Question Tools

Stats

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

Seen: 4,011 times

Last updated: Oct 03 '13