Commit e1126306 authored by wanghailong's avatar wanghailong

解决初始化失败的问题

parent 26243a88
......@@ -57,9 +57,9 @@ void CBM_model::Initialize()
// _writer10.open("DPM.avi",cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),1);
mog_fg = cv::Mat::zeros(cv::Size(new_width, new_height), CV_8UC1);
mog_fg2 = cv::Mat::zeros(cv::Size(new_width, new_height), CV_8UC1);
imgStatic = cv::Mat::zeros(cv::Size(new_width, new_height), CV_8UC3);
// mog_fg = cv::Mat::zeros(cv::Size(new_width, new_height), CV_8UC1);
// mog_fg2 = cv::Mat::zeros(cv::Size(new_width, new_height), CV_8UC1);
// imgStatic = cv::Mat::zeros(cv::Size(new_width, new_height), CV_8UC3);
my_mog_fg = myCreateImage( new_width, new_height, 1);
my_mog_fg2 = myCreateImage( new_width, new_height, 1);
......@@ -196,9 +196,9 @@ bool CBM_model::Motion_Detection(myImage *img)
staticFG_pixel_num_pre = staticFG_pixel_num_now;
staticFG_pixel_num_now = check_foreground2(my_imgStatic);
myImage_2_opencv(my_imgStatic,imgStatic);
myImage_2_opencv(my_mog_fg,mog_fg);
myImage_2_opencv(my_mog_fg2,mog_fg2);
// myImage_2_opencv(my_imgStatic,imgStatic);
// myImage_2_opencv(my_mog_fg,mog_fg);
// myImage_2_opencv(my_mog_fg2,mog_fg2);
// cv::imwrite("static_obj.jpg", imgStatic);
// cv::imwrite("Long-term.jpg", mog_fg);
// cv::imwrite("Short-term.jpg", mog_fg2);
......
......@@ -91,7 +91,7 @@ public:
int TEMPORAL_RULE;
//VideoDetails *_video;//input video
cv::VideoWriter _writer6, _writer7, _writer8, _writer9, _writer10;
//cv::VideoWriter _writer6, _writer7, _writer8, _writer9, _writer10;
CBM_model(myImage * input, int set_MOG_LearnFrame, int set_min_area, int set_buffer_len, float set_resize, myImage * mask);
~CBM_model();
void Initialize();
......@@ -143,9 +143,9 @@ public:
myImage * maskROI;
myImage * input_temp;
private:
cv::Mat mog_fg;//long term
cv::Mat mog_fg2;//short term
cv::Mat imgStatic;
// cv::Mat mog_fg;//long term
// cv::Mat mog_fg2;//short term
// cv::Mat imgStatic;
myImage * my_mog_fg;//long term
......
cmake_minimum_required(VERSION 2.8)
project(ObjLeft)
find_package(OpenCV 4.1.1 REQUIRED)
find_package(OpenCV REQUIRED)
link_directories(${MYPROJECT_DIR})
......
......@@ -28,13 +28,13 @@ double INPUT_RESIZE;
bool DetectAnomaly::detectAnomaly(cv::Mat img, vector<vector<int>>* resultLocation)
{
yolo_test = img.clone();
cv::Rect rect(0,610,2400,910);
img = img(rect);
cv::resize(img, img,cv::Size(2400*0.25, 910*0.25), cv::INTER_CUBIC);
img.copyTo(myimg);
medianBlur(mat_myimg, mat_myimg, 3);
opencv_2_myImage(myimg, myimg1); //transfer opencv data to myimage data
//img.copyTo(*myimg);
myimg = &img;
medianBlur(*mat_myimg, *mat_myimg, 3);
opencv_2_myImage(*myimg, myimg1); //transfer opencv data to myimage data
/************************************************************************/
/* abandoned object detection algorithm */
......@@ -51,9 +51,9 @@ bool DetectAnomaly::detectAnomaly(config cfg, cv::Mat img, vector<vector<int>>*
img = img(rect);
//cv::resize(img, img,cv::Size(2400*0.25, 910*0.25), cv::INTER_CUBIC);
cv::resize(img, img,cv::Size(cfg.image_width * cfg.input_resize, cfg.image_height * cfg.input_resize), cv::INTER_CUBIC);
img.copyTo(myimg);
medianBlur(mat_myimg, mat_myimg, 3);
opencv_2_myImage(myimg, myimg1); //transfer opencv data to myimage data
myimg = &img;
medianBlur(*mat_myimg, *mat_myimg, 3);
opencv_2_myImage(*myimg, myimg1); //transfer opencv data to myimage data
/************************************************************************/
/* abandoned object detection algorithm */
......@@ -142,8 +142,9 @@ DetectAnomaly::DetectAnomaly()
mymask = myCreateImage(cfgAnomaly.image_width, cfgAnomaly.image_height, 3);
myInverse(mymask, mymask);
myimg1 = myCreateImage(cfgAnomaly.image_width, cfgAnomaly.image_height, 3);
myimg = cv::Mat(cv::Size(cfgAnomaly.image_width, cfgAnomaly.image_height), CV_8UC3);
myimg.copyTo(mat_myimg);
myimg = new cv::Mat(cv::Size(cfgAnomaly.image_width, cfgAnomaly.image_height), CV_8UC3);
//*myimg.copyTo(*mat_myimg);
mat_myimg = myimg;
// mat_myimg(myimg, 0);
// set roi
......@@ -163,15 +164,15 @@ DetectAnomaly::DetectAnomaly()
cv::polylines(Image1, PointArray, arr, 1,true, cv::Scalar(255, 255, 255),5,8,0);
cv::fillPoly(Image1, ppt, arr, 1, cv::Scalar(255, 255, 255),0);
cv::threshold(Image1, Image1, 254, 255,THRESH_BINARY);
mask = Image1.clone();
mask = &Image1;
opencv_2_myImage(mask, mymask);
opencv_2_myImage(*mask, mymask);
#ifdef DEBUG
//cv::imwrite("mask.jpg", Image1);
cv::waitKey(1);
//cv::imwrite("mask.jpg", *mask);
//cv::waitKey(1);
//cvReleaseImage(&Image1);
#endif
_objleft.ObjLeftSet(myimg1, cfgAnomaly.gmm_learn_frame, cfgAnomaly.min_fg, cfgAnomaly.buffer_length, mymask);
_objleft.ObjLeftSet(myimg1, cfgAnomaly.gmm_learn_frame, cfgAnomaly.min_fg, cfgAnomaly.buffer_length, mymask, mask);
#ifdef DEBUG
printf("Finished initialization.\n");
#endif
......@@ -220,8 +221,9 @@ DetectAnomaly::DetectAnomaly(config cfg) : cfgAnomaly(cfg)
mymask = myCreateImage(cfgAnomaly.image_width, cfgAnomaly.image_height, 3);
myInverse(mymask, mymask);
myimg1 = myCreateImage(cfgAnomaly.image_width, cfgAnomaly.image_height, 3);
myimg = cv::Mat(cv::Size(cfgAnomaly.image_width, cfgAnomaly.image_height), CV_8UC3);
myimg.copyTo(mat_myimg);
myimg = new cv::Mat(cv::Size(cfgAnomaly.image_width, cfgAnomaly.image_height), CV_8UC3);
//myimg.copyTo(mat_myimg);
mat_myimg = myimg;
// mat_myimg(myimg, 0);
// set roi
......@@ -251,15 +253,15 @@ DetectAnomaly::DetectAnomaly(config cfg) : cfgAnomaly(cfg)
cv::polylines(Image1, PointArray, arr, 1,true, cv::Scalar(255, 255, 255),5,8,0);
cv::fillPoly(Image1, ppt, arr, 1, cv::Scalar(255, 255, 255),0);
cv::threshold(Image1, Image1, 254, 255, THRESH_BINARY);
mask = Image1.clone();
mask = &Image1;
opencv_2_myImage(mask, mymask);
opencv_2_myImage(*mask, mymask);
#ifdef DEBUG
//cv::imwrite("mask.jpg", Image1);
cv::waitKey(1);
cv::imwrite("mask.jpg", Image1);
//cv::waitKey(1);
//cvReleaseImage(&Image1);
#endif
_objleft.ObjLeftSet(myimg1, cfgAnomaly.gmm_learn_frame, cfgAnomaly.min_fg, cfgAnomaly.buffer_length, mymask);
_objleft.ObjLeftSet(myimg1, cfgAnomaly.gmm_learn_frame, cfgAnomaly.min_fg, cfgAnomaly.buffer_length, mymask,mask);
#ifdef DEBUG
printf("Finished initialization.\n");
#endif
......
......@@ -44,13 +44,13 @@ private:
cv::Point PointArray1[4];
cv::Point *PointArray[2] = {&PointArray1[0], &PointArray1[2]};
cv::Mat mask;
cv::Mat* mask;
myImage *mymask;
cv::Mat myimg;
cv::Mat* myimg;
myImage *myimg1;
cv::Mat mat_myimg;
cv::Mat* mat_myimg;
ObjLeftDetect _objleft;
bool obj_left;
......@@ -63,7 +63,6 @@ public:
DetectAnomaly();
DetectAnomaly(config cfg);
cv::Mat yolo_test;
~DetectAnomaly();
};
......
......@@ -22,34 +22,36 @@ ObjLeftDetect::ObjLeftDetect()
{
}
void ObjLeftDetect::ObjLeftSet(myImage *input, int set_MOG_LearnFrame, int set_min_area, int set_buffer_len, myImage *mask)
void ObjLeftDetect::ObjLeftSet(myImage *input, int set_MOG_LearnFrame, int set_min_area, int set_buffer_len, myImage *mask, cv::Mat* cvmask)
{
_CBM_model = new CBM_model(input, set_MOG_LearnFrame, set_min_area, set_buffer_len, INPUT_RESIZE, mask);
new_width = (int)(input->width * INPUT_RESIZE);
new_height = (int)(input->height * INPUT_RESIZE);
objmask = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC1);
myImage_2_opencv(mask, objmask);
cv::findContours(objmask,contours,cv::noArray(),cv::RETR_EXTERNAL,cv::CHAIN_APPROX_SIMPLE);
//objmask = new cv::Mat(cv::Size(new_width,new_height),CV_8UC1);
//myImage_2_opencv(mask, *objmask);
//cv::imwrite("mask2.jpg",*cvmask);
cv::findContours(*cvmask,contours,cv::noArray(),cv::RETR_EXTERNAL,cv::CHAIN_APPROX_SIMPLE);
if (contours.size() > 0)
{
for(size_t i = 0; i < contours.size(); i++)
{
if (cv::contourArea(contours[i]) < 10000)
{
contours.erase(contours.begin() + i);
i--;
}
if (cv::contourArea(contours[i]) < 10000)
{
contours.erase(contours.begin() + i);
i--;
}
}
}
cv::Mat showpic = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC1);
drawContours(showpic, contours, 0, Scalar::all(255));
// cv::Mat showpic = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC1);
// drawContours(showpic, contours, 0, Scalar::all(255));
//cv::imwrite("show.jpg",showpic);
// new_width = (int)(input->width);
// new_height = (int)(input->height);
// std::cout << "contours size : " << contours.size() << " "<< contours[0].size() <<std::endl;
//std::cout << "contours size : " << contours.size() << " "<< contours[0].size() <<std::endl;
// new_width = (int)(input->width);
// new_height = (int)(input->height);
initialize();
......@@ -77,11 +79,11 @@ void ObjLeftDetect::initialize()
ObjLocation.clear();
LeftLocation.clear();
myimg2 = myCreateImage(new_width,new_height,3);
_ImgSynopsis = myCreateImage(new_width,new_height,3);
//myimg2 = myCreateImage(new_width,new_height,3);
//_ImgSynopsis = myCreateImage(new_width,new_height,3);
A = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC3);
B = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC3);
//A = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC3);
//B = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC3);
......@@ -167,25 +169,25 @@ bool ObjLeftDetect::process(myImage * input, vector<vector<int>>* resultLocation
}
myImageZero(_ImgSynopsis);
for (int i = 0; i < _ImgSynopsis->width; i++)
{
for (int j = 0; j < _ImgSynopsis->height; j++)
{
myColor aaa = myGet2D(_CBM_model->my_imgStatic,i,j);
myColor bbb; bbb.B = 200; bbb.G = 250; bbb.R = 10;
if ((aaa.B==0)&&(aaa.G==200)&&(aaa.R==255))
{
mySet2D(_ImgSynopsis,bbb,i,j);
}
}
}
myImage_2_opencv(_ImgSynopsis,test);
// cvShowImage("summary",test);
cv::waitKey(1);
test.copyTo(A);
myImageZero(_ImgSynopsis);
//myImageZero(_ImgSynopsis);
// for (int i = 0; i < _ImgSynopsis->width; i++)
// {
// for (int j = 0; j < _ImgSynopsis->height; j++)
// {
// myColor aaa = myGet2D(_CBM_model->my_imgStatic,i,j);
// myColor bbb; bbb.B = 200; bbb.G = 250; bbb.R = 10;
// if ((aaa.B==0)&&(aaa.G==200)&&(aaa.R==255))
// {
// mySet2D(_ImgSynopsis,bbb,i,j);
// }
// }
// }
// myImage_2_opencv(_ImgSynopsis,test);
// // cvShowImage("summary",test);
// cv::waitKey(1);
// test.copyTo(A);
// myImageZero(_ImgSynopsis);
// set_alarm = soft_validation3( _ImgSynopsis, LeftLocation);
if(resultLocation != nullptr){
if(resultLocation->size() > 0){
......@@ -212,367 +214,367 @@ bool ObjLeftDetect::process(myImage * input, vector<vector<int>>* resultLocation
}
bool ObjLeftDetect::soft_validation3( myImage * ImgSynopsis, vector<Obj_info*> obj_left)
{
bool _set_alarm = false;
bool ** foreground;
int temporal_rule = BUFFER_LENGTH;
int retreval_time = temporal_rule/2 + temporal_rule/6;
bool ** ForeSynopsis;
ForeSynopsis = (bool **)malloc(new_width*sizeof(bool *));
for (int i = 0; i < new_width; i++){
ForeSynopsis[i] = (bool *)malloc(new_height*sizeof(bool));
}
for (int i = 0; i < new_width; i++){
for (int j = 0; j < new_height; j++){
ForeSynopsis[i][j] = false;
}
}
int ** floodfillMap;
floodfillMap = (int **)malloc(new_width*sizeof(int *));
for (int i = 0; i < new_width; i++){
floodfillMap[i] = (int *)malloc(new_height*sizeof(int));
}
for (int i = 0; i < new_width; i++){
for (int j = 0; j < new_height; j++){
floodfillMap[i][j] = 0;
}
}
/************************************************************************/
/* capture the color information of the suspected owner */
/************************************************************************/
foreground = _CBM_model->GetPrevious_nForeground(retreval_time-1);//the moment the bag dropped (need to check!!)
bool foreground_found = false;
#pragma omp parallel for num_threads(12)
for (int j = 0; j < new_width; j++)
{
for (int k = 0; k < new_height; k++)
{
if (foreground[j][k] == true)
{
for (int n = 0; n < obj_left.size(); n++)
{
float owner_dist = point_dist( (float)j, (float)k, (float)obj_left.at(n)->x, (float)obj_left.at(n)->y);
if (owner_dist<OWNER_SEARCH_ROI)//distance threshold
{
for (int w = -30; w < 30; w++)
{
//rgb histogram accumulated
myColor color;
color = myGet2D( _CBM_model->_GetPrevious_nFrame(retreval_time-1), j, k);
//printf("get color: r = %lf, g = %lf, b = %lf\n",(float)color.R,(float)color.G, (float)color.B);
obj_left.at(n)->Owner_B[(int)((float)color.B/255.0*10.0)] += 1.0;
obj_left.at(n)->Owner_G[(int)((float)color.G/255.0*10.0)] += 1.0;
obj_left.at(n)->Owner_R[(int)((float)color.R/255.0*10.0)] += 1.0;
}
foreground_found = true;
}
}
}
}
}
//if there is no foreground found near to the suspected left object, that is, we have a false alarm of object left detection
//than return nothing!
if (foreground_found==false)
return false;
// bool ObjLeftDetect::soft_validation3( myImage * ImgSynopsis, vector<Obj_info*> obj_left)
// {
// bool _set_alarm = false;
// bool ** foreground;
// int temporal_rule = BUFFER_LENGTH;
// int retreval_time = temporal_rule/2 + temporal_rule/6;
// bool ** ForeSynopsis;
// ForeSynopsis = (bool **)malloc(new_width*sizeof(bool *));
// for (int i = 0; i < new_width; i++){
// ForeSynopsis[i] = (bool *)malloc(new_height*sizeof(bool));
// }
// for (int i = 0; i < new_width; i++){
// for (int j = 0; j < new_height; j++){
// ForeSynopsis[i][j] = false;
// }
// }
//else if we found foregournd object, it must be the owner, than we extract the color information of further processing
// int ** floodfillMap;
// floodfillMap = (int **)malloc(new_width*sizeof(int *));
// for (int i = 0; i < new_width; i++){
// floodfillMap[i] = (int *)malloc(new_height*sizeof(int));
// }
// for (int i = 0; i < new_width; i++){
// for (int j = 0; j < new_height; j++){
// floodfillMap[i][j] = 0;
// }
// }
myFloatColor ** obj_colors;
obj_colors = (myFloatColor **)malloc(obj_left.size()*sizeof(myFloatColor *));
for (int i = 0; i < obj_left.size(); i++)
obj_colors[i] = (myFloatColor *)malloc(10*sizeof(myFloatColor));
for (int i = 0; i < obj_left.size(); i++)
{
//normalizing the histogram accumulated information
double total_r = 0.0, total_g = 0.0, total_b = 0.0;
for (int j = 0; j < 10; j++)
{
total_r = total_r + obj_left.at(i)->Owner_R[j];
total_g = total_g + obj_left.at(i)->Owner_G[j];
total_b = total_b + obj_left.at(i)->Owner_B[j];
}
for (int j = 0; j < 10; j++)
{
obj_left.at(i)->Owner_R[j] = (obj_left.at(i)->Owner_R[j]/(total_r+0.001));
obj_left.at(i)->Owner_G[j] = (obj_left.at(i)->Owner_G[j]/(total_g+0.001));
obj_left.at(i)->Owner_B[j] = (obj_left.at(i)->Owner_B[j]/(total_b+0.001));
// /************************************************************************/
// /* capture the color information of the suspected owner */
// /************************************************************************/
// foreground = _CBM_model->GetPrevious_nForeground(retreval_time-1);//the moment the bag dropped (need to check!!)
// bool foreground_found = false;
// #pragma omp parallel for num_threads(12)
// for (int j = 0; j < new_width; j++)
// {
// for (int k = 0; k < new_height; k++)
// {
// if (foreground[j][k] == true)
// {
// for (int n = 0; n < obj_left.size(); n++)
// {
// float owner_dist = point_dist( (float)j, (float)k, (float)obj_left.at(n)->x, (float)obj_left.at(n)->y);
// if (owner_dist<OWNER_SEARCH_ROI)//distance threshold
// {
// for (int w = -30; w < 30; w++)
// {
// //rgb histogram accumulated
// myColor color;
// color = myGet2D( _CBM_model->_GetPrevious_nFrame(retreval_time-1), j, k);
// //printf("get color: r = %lf, g = %lf, b = %lf\n",(float)color.R,(float)color.G, (float)color.B);
// obj_left.at(n)->Owner_B[(int)((float)color.B/255.0*10.0)] += 1.0;
// obj_left.at(n)->Owner_G[(int)((float)color.G/255.0*10.0)] += 1.0;
// obj_left.at(n)->Owner_R[(int)((float)color.R/255.0*10.0)] += 1.0;
// }
// foreground_found = true;
// }
// }
// }
// }
// }
obj_colors[i][j].R = obj_left.at(i)->Owner_R[j];
obj_colors[i][j].G = obj_left.at(i)->Owner_G[j];
obj_colors[i][j].B = obj_left.at(i)->Owner_B[j];
}
}
// //if there is no foreground found near to the suspected left object, that is, we have a false alarm of object left detection
// //than return nothing!
// if (foreground_found==false)
// return false;
// //else if we found foregournd object, it must be the owner, than we extract the color information of further processing
// myFloatColor ** obj_colors;
// obj_colors = (myFloatColor **)malloc(obj_left.size()*sizeof(myFloatColor *));
// for (int i = 0; i < obj_left.size(); i++)
// obj_colors[i] = (myFloatColor *)malloc(10*sizeof(myFloatColor));
// for (int i = 0; i < obj_left.size(); i++)
// {
// //normalizing the histogram accumulated information
// double total_r = 0.0, total_g = 0.0, total_b = 0.0;
// for (int j = 0; j < 10; j++)
// {
// total_r = total_r + obj_left.at(i)->Owner_R[j];
// total_g = total_g + obj_left.at(i)->Owner_G[j];
// total_b = total_b + obj_left.at(i)->Owner_B[j];
// }
// for (int j = 0; j < 10; j++)
// {
// obj_left.at(i)->Owner_R[j] = (obj_left.at(i)->Owner_R[j]/(total_r+0.001));
// obj_left.at(i)->Owner_G[j] = (obj_left.at(i)->Owner_G[j]/(total_g+0.001));
// obj_left.at(i)->Owner_B[j] = (obj_left.at(i)->Owner_B[j]/(total_b+0.001));
// obj_colors[i][j].R = obj_left.at(i)->Owner_R[j];
// obj_colors[i][j].G = obj_left.at(i)->Owner_G[j];
// obj_colors[i][j].B = obj_left.at(i)->Owner_B[j];
// }
// }
/************************************************************************/
/* generate the synopsis image */
/************************************************************************/
//SYSTEMTIME s;
//GetSystemTime(&s);
for (int i = 1; i < retreval_time; i=i+1)//���e���Oi=i+3
{
cv::Mat img = cv::Mat(cv::Size(new_width,new_height),CV_8UC3);
cv::Mat fg = cv::Mat(cv::Size(new_width,new_height),CV_8UC1);
foreground = _CBM_model->GetPrevious_nForeground(i);
#pragma omp parallel for num_threads(12)
for (int j = 0; j < new_width; j++)
{
for (int k = 0; k < new_height; k++)
{
if (foreground[j][k] == true)
{
myColor color;
color = myGet2D( _CBM_model->_GetPrevious_nFrame(i), j, k);
mySet2D(ImgSynopsis,color,j,k);
ForeSynopsis[j][k] = true;
cv::Scalar ppp;
ppp.val[0] = color.B; ppp.val[1] = color.G; ppp.val[2] = color.R;
Set2D(img,k,j,ppp);
cv::Scalar aaa;
aaa.val[0] = aaa.val[1] = aaa.val[2] = 255;
Set2D(fg,k,j,aaa);
}
else
{
cv::Scalar ppp;
ppp.val[0] = ppp.val[1] = ppp.val[2] = 0;
Set2D(img,k,j,ppp);
cv::Scalar aaa;
aaa.val[0] = aaa.val[1] = aaa.val[2] = 0;
Set2D(fg,k,j,aaa);
}
}
}
//char a[50], b[50];
//sprintf(a,"temp\\mask%d%d%d%d%d%d_%d.bmp",s.wYear,s.wMonth,s.wDay,s.wHour,s.wMinute,s.wMilliseconds,i);
//sprintf(b,"temp\\image%d%d%d%d%d%d_%d.bmp",s.wYear,s.wMonth,s.wDay,s.wHour,s.wMinute,s.wMilliseconds,i);
//cvSaveImage(a,fg);
//cvSaveImage(b,img);
//cvReleaseImage(&img);
//cvReleaseImage(&fg);
}
// /************************************************************************/
// /* generate the synopsis image */
// /************************************************************************/
// //SYSTEMTIME s;
// //GetSystemTime(&s);
// for (int i = 1; i < retreval_time; i=i+1)//���e���Oi=i+3
// {
// cv::Mat img = cv::Mat(cv::Size(new_width,new_height),CV_8UC3);
// cv::Mat fg = cv::Mat(cv::Size(new_width,new_height),CV_8UC1);
// foreground = _CBM_model->GetPrevious_nForeground(i);
// #pragma omp parallel for num_threads(12)
// for (int j = 0; j < new_width; j++)
// {
// for (int k = 0; k < new_height; k++)
// {
// if (foreground[j][k] == true)
// {
// myColor color;
// color = myGet2D( _CBM_model->_GetPrevious_nFrame(i), j, k);
// mySet2D(ImgSynopsis,color,j,k);
// ForeSynopsis[j][k] = true;
// cv::Scalar ppp;
// ppp.val[0] = color.B; ppp.val[1] = color.G; ppp.val[2] = color.R;
// Set2D(img,k,j,ppp);
// cv::Scalar aaa;
// aaa.val[0] = aaa.val[1] = aaa.val[2] = 255;
// Set2D(fg,k,j,aaa);
// }
// else
// {
// cv::Scalar ppp;
// ppp.val[0] = ppp.val[1] = ppp.val[2] = 0;
// Set2D(img,k,j,ppp);
// cv::Scalar aaa;
// aaa.val[0] = aaa.val[1] = aaa.val[2] = 0;
// Set2D(fg,k,j,aaa);
// }
// }
// }
// //char a[50], b[50];
// //sprintf(a,"temp\\mask%d%d%d%d%d%d_%d.bmp",s.wYear,s.wMonth,s.wDay,s.wHour,s.wMinute,s.wMilliseconds,i);
// //sprintf(b,"temp\\image%d%d%d%d%d%d_%d.bmp",s.wYear,s.wMonth,s.wDay,s.wHour,s.wMinute,s.wMilliseconds,i);
// //cvSaveImage(a,fg);
// //cvSaveImage(b,img);
// //cvReleaseImage(&img);
// //cvReleaseImage(&fg);
// }
cv::Mat temp = cv::Mat(cv::Size(ImgSynopsis->width,ImgSynopsis->height),CV_8UC3);
myImage_2_opencv(ImgSynopsis,temp);
// cvShowImage("synopsis",temp);
//cvReleaseImage(&temp);
// cv::Mat temp = cv::Mat(cv::Size(ImgSynopsis->width,ImgSynopsis->height),CV_8UC3);
// myImage_2_opencv(ImgSynopsis,temp);
// // cvShowImage("synopsis",temp);
// //cvReleaseImage(&temp);
//cvZero(B);
// //cvZero(B);
/************************************************************************/
/* Pedestrian detector */
/************************************************************************/
for (int t = (retreval_time-1); t>=2; t--)
{
//_CBM_model->DetectPrevious_nForeground_HOG(t);
//_CBM_model->DetectPrevious_nForeground_DPM(t);
}
// /************************************************************************/
// /* Pedestrian detector */
// /************************************************************************/
// for (int t = (retreval_time-1); t>=2; t--)
// {
// //_CBM_model->DetectPrevious_nForeground_HOG(t);
// //_CBM_model->DetectPrevious_nForeground_DPM(t);
// }
/************************************************************************/
/* use mounatin climbing algorithm to extract the candidate trajectory */
/************************************************************************/
for (int n = 0; n < obj_left.size(); n++)
{
int _t_time = retreval_time-1;
obj_left.at(n)->traj_label = n+1;
// /************************************************************************/
// /* use mounatin climbing algorithm to extract the candidate trajectory */
// /************************************************************************/
// for (int n = 0; n < obj_left.size(); n++)
// {
// int _t_time = retreval_time-1;
// obj_left.at(n)->traj_label = n+1;
for (int i = -OWNER_SEARCH_ROI/2; i < OWNER_SEARCH_ROI/2+1; i++)
{
for (int j = -OWNER_SEARCH_ROI/2; j < OWNER_SEARCH_ROI/2+1; j++)
{
Spatial_Temporal_Search(floodfillMap,obj_left.at(n)->x + i, obj_left.at(n)->y + j, obj_colors[n], _t_time, n+1);
}
}
}
// for (int i = -OWNER_SEARCH_ROI/2; i < OWNER_SEARCH_ROI/2+1; i++)
// {
// for (int j = -OWNER_SEARCH_ROI/2; j < OWNER_SEARCH_ROI/2+1; j++)
// {
// Spatial_Temporal_Search(floodfillMap,obj_left.at(n)->x + i, obj_left.at(n)->y + j, obj_colors[n], _t_time, n+1);
// }
// }
// }
/************************************************************************/
/* Verifing the object left event by distance information */
/************************************************************************/
cv::Mat temp2 = cv::Mat(cv::Size(ImgSynopsis->width,ImgSynopsis->height),CV_8UC3);
//myImage_2_opencv(ImgSynopsis,temp2);
B.copyTo(temp2);
for (int n = 0; n < obj_left.size(); n++){
bool inside = false, outside = false;
int inside_count = 0, outside_count = 0;
for (int i = 0; i < new_width; i++){
for (int j = 0; j < new_height; j++){
//if (floodfillMap[i][j] == obj_left.at(n)->traj_label){
if (floodfillMap[i][j] == (n+1)){
float dist = point_dist( (float)i, (float)j, (float)obj_left.at(n)->x, (float)obj_left.at(n)->y);
// /************************************************************************/
// /* Verifing the object left event by distance information */
// /************************************************************************/
// cv::Mat temp2 = cv::Mat(cv::Size(ImgSynopsis->width,ImgSynopsis->height),CV_8UC3);
// //myImage_2_opencv(ImgSynopsis,temp2);
// B.copyTo(temp2);
// for (int n = 0; n < obj_left.size(); n++){
// bool inside = false, outside = false;
// int inside_count = 0, outside_count = 0;
// for (int i = 0; i < new_width; i++){
// for (int j = 0; j < new_height; j++){
// //if (floodfillMap[i][j] == obj_left.at(n)->traj_label){
// if (floodfillMap[i][j] == (n+1)){
// float dist = point_dist( (float)i, (float)j, (float)obj_left.at(n)->x, (float)obj_left.at(n)->y);
if (dist>OWNER_SEARCH_ROI/2){
inside_count++;
//outside = true;
//printf("outside dist = %lf, i = %d, j = %d, objx = %d, objy = %d\n",dist,i,j,obj_left.at(n)->x,obj_left.at(n)->y);
}
else if (dist<OWNER_SEARCH_ROI/2){
outside_count++;
//inside = true;
//printf("inside dist = %lf, i = %d, j = %d, objx = %d, objy = %d\n",dist,i,j,obj_left.at(n)->x,obj_left.at(n)->y);
}
}
}
}
if (inside_count>OWNER_SEARCH_ROI) inside = true;
if (outside_count>OWNER_SEARCH_ROI) outside = true;
if ((inside==true)&&(outside==true)){
printf("\a");
printf("\a");
_set_alarm = true;
//cvCircle( temp2, cvPoint(obj_left.at(n)->x,obj_left.at(n)->y), OWNER_SEARCH_ROI/3, cv::Scalar(0,255,255),2,8,0);
}
}
// cvShowImage("synopsis",temp2);
temp2.copyTo(B);
cv::waitKey(1);
//cvReleaseImage(&temp2);
free(*ForeSynopsis);
free(ForeSynopsis);
free(*floodfillMap);
free(floodfillMap);
free(*obj_colors);
free(obj_colors);
return _set_alarm;
}
int ObjLeftDetect::Spatial_Temporal_Search( int ** Image, int i, int j, myFloatColor * colors, int time_stamp,int my_label)
{
if ((j < 0) || (j >= new_height-1)) return 0;
if ((i < 0) || (i >= new_width-1)) return 0;
if(Image[i][j] == my_label) return 0;
int object_x = i;
int object_y = j;
bool ** foreground;
for (int t = time_stamp; t>=2; t--)
{
foreground = _CBM_model->GetPrevious_nForeground(t);
//foreground = _CBM_model->GetPrevious_nForeground_DPM(t);
//foreground = _CBM_model->GetPrevious_nForeground_HOG(t);
if (foreground[object_x][object_y] == true)
{
for (int k = 0; k < new_width; k++)
for (int l = 0; l < new_height; l++)
image[k][l] = 0;
spatial_flood( foreground, object_x, object_y);
for (int k = 0; k < 10; k++)
{
connect_colors[k].B = 0.0;
connect_colors[k].G = 0.0;
connect_colors[k].R = 0.0;
}
int min_x = new_width, max_x = 0;
int min_y = new_height, max_y = 0;
for (int k = 0; k < new_width; k++)
{
for (int l = 0; l < new_height; l++)
{
if (image[k][l] == 1)
{
myColor color_temp;
color_temp = myGet2D( _CBM_model->_GetPrevious_nFrame(t), k, l);
connect_colors[(int)((double)color_temp.R/255.0*10.0)].R+=1.0;
connect_colors[(int)((double)color_temp.G/255.0*10.0)].G+=1.0;
connect_colors[(int)((double)color_temp.B/255.0*10.0)].B+=1.0;
// if (dist>OWNER_SEARCH_ROI/2){
// inside_count++;
// //outside = true;
// //printf("outside dist = %lf, i = %d, j = %d, objx = %d, objy = %d\n",dist,i,j,obj_left.at(n)->x,obj_left.at(n)->y);
// }
// else if (dist<OWNER_SEARCH_ROI/2){
// outside_count++;
// //inside = true;
// //printf("inside dist = %lf, i = %d, j = %d, objx = %d, objy = %d\n",dist,i,j,obj_left.at(n)->x,obj_left.at(n)->y);
// }
// }
// }
// }
// if (inside_count>OWNER_SEARCH_ROI) inside = true;
// if (outside_count>OWNER_SEARCH_ROI) outside = true;
// if ((inside==true)&&(outside==true)){
// printf("\a");
// printf("\a");
// _set_alarm = true;
// //cvCircle( temp2, cvPoint(obj_left.at(n)->x,obj_left.at(n)->y), OWNER_SEARCH_ROI/3, cv::Scalar(0,255,255),2,8,0);
// }
// }
// // cvShowImage("synopsis",temp2);
// temp2.copyTo(B);
// cv::waitKey(1);
// //cvReleaseImage(&temp2);
if (min_x > k) min_x = k;
if (max_x < k) max_x = k;
// free(*ForeSynopsis);
// free(ForeSynopsis);
// free(*floodfillMap);
// free(floodfillMap);
if (min_y > l) min_y = l;
if (max_y < l) max_y = l;
// free(*obj_colors);
// free(obj_colors);
}
}
}
// return _set_alarm;
// }
object_x = min_x + (max_x-min_x)/2;
object_y = min_y + (max_y-min_y)/2;
// int ObjLeftDetect::Spatial_Temporal_Search( int ** Image, int i, int j, myFloatColor * colors, int time_stamp,int my_label)
// {
// if ((j < 0) || (j >= new_height-1)) return 0;
// if ((i < 0) || (i >= new_width-1)) return 0;
// if(Image[i][j] == my_label) return 0;
// int object_x = i;
// int object_y = j;
// bool ** foreground;
// for (int t = time_stamp; t>=2; t--)
// {
// foreground = _CBM_model->GetPrevious_nForeground(t);
// //foreground = _CBM_model->GetPrevious_nForeground_DPM(t);
// //foreground = _CBM_model->GetPrevious_nForeground_HOG(t);
// if (foreground[object_x][object_y] == true)
// {
// for (int k = 0; k < new_width; k++)
// for (int l = 0; l < new_height; l++)
// image[k][l] = 0;
// spatial_flood( foreground, object_x, object_y);
// for (int k = 0; k < 10; k++)
// {
// connect_colors[k].B = 0.0;
// connect_colors[k].G = 0.0;
// connect_colors[k].R = 0.0;
// }
// int min_x = new_width, max_x = 0;
// int min_y = new_height, max_y = 0;
// for (int k = 0; k < new_width; k++)
// {
// for (int l = 0; l < new_height; l++)
// {
// if (image[k][l] == 1)
// {
// myColor color_temp;
// color_temp = myGet2D( _CBM_model->_GetPrevious_nFrame(t), k, l);
// connect_colors[(int)((double)color_temp.R/255.0*10.0)].R+=1.0;
// connect_colors[(int)((double)color_temp.G/255.0*10.0)].G+=1.0;
// connect_colors[(int)((double)color_temp.B/255.0*10.0)].B+=1.0;
// if (min_x > k) min_x = k;
// if (max_x < k) max_x = k;
// if (min_y > l) min_y = l;
// if (max_y < l) max_y = l;
// }
// }
// }
// object_x = min_x + (max_x-min_x)/2;
// object_y = min_y + (max_y-min_y)/2;
//normalizing the histogram accumulated information
double total_r = 0.0, total_g = 0.0, total_b = 0.0;
for (int w = 0; w < 10; w++)
{
total_r = total_r + connect_colors[w].R;
total_g = total_g + connect_colors[w].G;
total_b = total_b + connect_colors[w].B;
}
for (int w = 0; w < 10; w++)
{
connect_colors[w].R = connect_colors[w].R/(total_r+0.001);
connect_colors[w].G = connect_colors[w].G/(total_g+0.001);
connect_colors[w].B = connect_colors[w].B/(total_b+0.001);
}
double color_prob_r = 0.0, color_prob_g = 0.0, color_prob_b = 0.0;
for (int s = 0; s < 10; s++)
{
color_prob_r = color_prob_r + colors[s].R*connect_colors[s].R;
color_prob_g = color_prob_g + colors[s].G*connect_colors[s].G;
color_prob_b = color_prob_b + colors[s].B*connect_colors[s].B;
}
if ((color_prob_r>=0.0)&&(color_prob_g>=0.0)&&(color_prob_b>=0.0))
{
int foreground_num = 0, overlap_num = 0;
for (int k = 0; k < new_width; k++)
{
for (int l = 0; l < new_height; l++)
{
if ((Image[k][l] == my_label)&&(image[k][l] == 1))
overlap_num++;
}
}
myColor color_temp, color_add;
color_add = color_rainbow(time_stamp, t);
for (int k = 0; k < new_width; k++)
{
for (int l = 0; l < new_height; l++)
{
if (image[k][l] == 1)
{
Image[k][l] = my_label;
// //normalizing the histogram accumulated information
// double total_r = 0.0, total_g = 0.0, total_b = 0.0;
// for (int w = 0; w < 10; w++)
// {
// total_r = total_r + connect_colors[w].R;
// total_g = total_g + connect_colors[w].G;
// total_b = total_b + connect_colors[w].B;
// }
// for (int w = 0; w < 10; w++)
// {
// connect_colors[w].R = connect_colors[w].R/(total_r+0.001);
// connect_colors[w].G = connect_colors[w].G/(total_g+0.001);
// connect_colors[w].B = connect_colors[w].B/(total_b+0.001);
// }
// double color_prob_r = 0.0, color_prob_g = 0.0, color_prob_b = 0.0;
// for (int s = 0; s < 10; s++)
// {
// color_prob_r = color_prob_r + colors[s].R*connect_colors[s].R;
// color_prob_g = color_prob_g + colors[s].G*connect_colors[s].G;
// color_prob_b = color_prob_b + colors[s].B*connect_colors[s].B;
// }
// if ((color_prob_r>=0.0)&&(color_prob_g>=0.0)&&(color_prob_b>=0.0))
// {
// int foreground_num = 0, overlap_num = 0;
// for (int k = 0; k < new_width; k++)
// {
// for (int l = 0; l < new_height; l++)
// {
// if ((Image[k][l] == my_label)&&(image[k][l] == 1))
// overlap_num++;
// }
// }
// myColor color_temp, color_add;
// color_add = color_rainbow(time_stamp, t);
// for (int k = 0; k < new_width; k++)
// {
// for (int l = 0; l < new_height; l++)
// {
// if (image[k][l] == 1)
// {
// Image[k][l] = my_label;
if (overlap_num < 1000)
{
color_temp = myGet2D( _CBM_model->_GetPrevious_nFrame(t), k, l);
cv::Scalar setcolor;
setcolor.val[0] = color_temp.B + color_add.B;
setcolor.val[1] = color_temp.G + color_add.G;
setcolor.val[2] = color_temp.R + color_add.R;
if (setcolor.val[0] > 255) setcolor.val[0] = 255;
if (setcolor.val[1] > 255) setcolor.val[1] = 255;
if (setcolor.val[2] > 255) setcolor.val[2] = 255;
Set2D(B,l,k,setcolor);
}
}
}
}
}
}
}
// if (overlap_num < 1000)
// {
// color_temp = myGet2D( _CBM_model->_GetPrevious_nFrame(t), k, l);
// cv::Scalar setcolor;
// setcolor.val[0] = color_temp.B + color_add.B;
// setcolor.val[1] = color_temp.G + color_add.G;
// setcolor.val[2] = color_temp.R + color_add.R;
// if (setcolor.val[0] > 255) setcolor.val[0] = 255;
// if (setcolor.val[1] > 255) setcolor.val[1] = 255;
// if (setcolor.val[2] > 255) setcolor.val[2] = 255;
// Set2D(B,l,k,setcolor);
// }
// }
// }
// }
// }
// }
// }
return 0;
}
// return 0;
// }
int ObjLeftDetect::spatial_flood(bool ** foreground, int i,int j)
{
if ((j < 0) || (j >= new_height-1)) return 0;
......
......@@ -31,11 +31,11 @@ public:
bool process(myImage * input, vector<vector<int>>* resultLocation);
CBM_model *_CBM_model;
//cv::VideoWriter _writer1, _writer2;
cv::Mat A, B;
//cv::Mat A, B;
int ** image;
myFloatColor * connect_colors;
void ObjLeftSet(myImage *input, int set_MOG_LearnFrame, int set_min_area, int set_buffer_len, myImage *mask);
void ObjLeftSet(myImage *input, int set_MOG_LearnFrame, int set_min_area, int set_buffer_len, myImage *mask, cv::Mat* cvmask);
private:
//SYSTEMTIME st;
......@@ -55,9 +55,9 @@ private:
int new_width, new_height;
vector<Obj_info *> alarmList;
myImage * myimg1, * myimg2;
cv::Mat ImgSynopsis;
myImage * _ImgSynopsis;
//myImage * myimg1, * myimg2;
//cv::Mat ImgSynopsis;
//myImage * _ImgSynopsis;
int Spatial_Temporal_Search( int ** Image, int i, int j, myFloatColor * colors, int time_stamp,int my_label);
int spatial_flood( bool ** foreground, int i, int j);
......@@ -70,7 +70,7 @@ private:
int pic_num = 0;
//get mask points
cv::Mat objmask;
cv::Mat *objmask;
std::vector<std::vector<cv::Point>> contours;
};
......
......@@ -40,8 +40,8 @@ int main()
opt.bottom_left_y = 853;
opt.bottom_right_x = 2168;
opt.bottom_right_y = 842;
//DetectAnomaly* detect = new DetectAnomaly(opt);
DetectAnomaly* detect = new DetectAnomaly();
DetectAnomaly* detect = new DetectAnomaly(opt);
//DetectAnomaly* detect = new DetectAnomaly();
int pic_num = 0;
#ifdef DEBUG
//cv::VideoWriter _writer;
......@@ -65,8 +65,8 @@ int main()
// continue;
// }
//obj_left = detect->detectAnomaly(opt,qImg,&resultLocation);
obj_left = detect->detectAnomaly(qImg,&resultLocation);
obj_left = detect->detectAnomaly(opt,qImg,&resultLocation);
//obj_left = detect->detectAnomaly(qImg,&resultLocation);
......@@ -80,32 +80,32 @@ int main()
delete results_map[0];
delete input_mats;
#ifdef DEBUG
// if (obj_left == true)
// {
if (obj_left == true)
{
// printf("alram!!\n");
printf("alram!!\n");
// for(int i = 0; i < resultLocation.size();i++){
// int x0 = resultLocation[i][0] * 4;
// int y0 = resultLocation[i][1] * 4 + 610;
// int x1 = resultLocation[i][2] * 4;
// int y1 = resultLocation[i][3] * 4 + 610;
for(int i = 0; i < resultLocation.size();i++){
int x0 = resultLocation[i][0] * 4;
int y0 = resultLocation[i][1] * 4 + 610;
int x1 = resultLocation[i][2] * 4;
int y1 = resultLocation[i][3] * 4 + 610;
// cv::Point p1(x0, y0);
// cv::Point p2(x1, y1);
// cv::rectangle(qImg, p1, p2, cv::Scalar(0, 255, 0), 2);
// }
// stringstream ss1;
// string outFile;
// ss1 << "abanoned_"
// << "show"
// << count
// << ".jpg";
// ss1 >> outFile;
// cv::imwrite(outFile.c_str(), qImg);
cv::Point p1(x0, y0);
cv::Point p2(x1, y1);
cv::rectangle(qImg, p1, p2, cv::Scalar(0, 255, 0), 2);
}
stringstream ss1;
string outFile;
ss1 << "abanoned_"
<< "show"
<< count
<< ".jpg";
ss1 >> outFile;
cv::imwrite(outFile.c_str(), qImg);
// count++;
// }
count++;
}
#endif
}
gettimeofday(&end, NULL);
......@@ -116,7 +116,7 @@ int main()
printf("Finished detec, then release ...\n");
#endif
delete detect;
//delete detect;
//system("pause");
return 0;
}
......@@ -9,7 +9,7 @@ project(yolo-trt VERSION 1.0)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3 -Wno-write-strings")
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath -Wl,$ORIGIN")
find_package(OpenCV 4.1.1 REQUIRED)
find_package(OpenCV REQUIRED)
#cuda
#export PATH=/usr/local/cuda-10.2/bin:$PATH
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment