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
Can you tell me why? I'll appreciate any help here.. Thanks! :)
And here we go my code:
#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.varThresholdGen = 15;
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());
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;
cv::Mat imgroi;
if(rects.size() == 1){
imgroi = frame(rects[0]);
}
int extotalx, extotaly, extotalz;
float exavex, exavey, exavez;
extotalx = 0;
extotaly = 0;
extotalz = 0;
for(int i = 0; i < imgroi.rows; i++){
for(int j = 0; j < imgroi.cols; j++){
cv::Point3_ <uchar>* ex_p = frame.ptr<cv::Point3_ <uchar> >(i,j);
int ex_a = ex_p -> x;
int ex_b = ex_p -> y;
int ex_c = ex_p -> z;
extotalx += ex_a;
extotaly += ex_b;
extotalz += ex_c;
}
}
exavex = (float)extotalx / (imgroi.rows * imgroi.cols);
exavey = (float)extotaly / (imgroi.rows * imgroi.cols);
exavez = (float)extotalz / (imgroi.rows * imgroi.cols);
std::cout << exavex << std::endl;
std::cout << exavey << std::endl;
std::cout << exavez << std::endl;
}
}
cv::imshow("Frame",frame);
cv::imshow("Background Model",bgmodel);
cv::imshow("Blob",blob);
if(cv::waitKey(30) >= 0) break;
}
return 0;
}