-
Notifications
You must be signed in to change notification settings - Fork 0
/
frameObject.cpp
162 lines (141 loc) · 5.75 KB
/
frameObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//#include "shadow.h"
#include "frameObject.h"
//#define TH 30
#define TL 10
LoggerPtr loggerFrameObject(Logger::getLogger( "FrameObject"));
FrameObject::FrameObject(){
frame = NULL;
background = NULL;
foregroundMask = NULL;
salientForegroundMask = NULL;
frameNumber = 0;
size.height=0;
size.width=0;
}
FrameObject::~FrameObject(){
try{
cvReleaseImage(&frame);
cvReleaseImage(&background);
cvReleaseImage(&foregroundMask);
cvReleaseImage(&salientForegroundMask);
cvReleaseBlobs(frameBlobs);
list<DetectedObject*>::iterator i;
for(i=detectedObject.begin();i!=detectedObject.end(); i++){
(*i)->~_DetectedObject();
}
}
catch(exception& e){
throw e.what();
LOG4CXX_ERROR(loggerFrameObject, "Error in distruction FrameObject: "<< e.what());
}
}
FrameObject::FrameObject(IplImage * currentFrame, IplImage * currentBackground,IplImage *salient, int nFrame = 1){
try{
size = cvGetSize(currentFrame);
frame = currentFrame;
background = currentBackground;
foregroundMask = cvCreateImage(size, currentFrame->depth,1);
salientForegroundMask = salient;
frameNumber = nFrame;
frameBlobs = CvBlobs();
}
catch(exception& e){
LOG4CXX_ERROR(loggerFrameObject, e.what());
}
}
IplImage* FrameObject::getFrame(){return cvCloneImage(frame);}
IplImage* FrameObject::getBackground(){return cvCloneImage(background);}
IplImage* FrameObject::getForegroundMask(){return foregroundMask;}
IplImage* FrameObject::getSalientMask(){return salientForegroundMask;}
int FrameObject::getFrameNumber(){return frameNumber;}
bool FrameObject::isToSave(IplImage *vMask,IplImage *mask,int threashold){
int _threashold=0;
uchar* data_vMask = (uchar *)vMask->imageData;
int step_vMask = vMask->widthStep/sizeof(uchar);
uchar* data_mask = (uchar *)mask->imageData;
int step_mask = mask->widthStep/sizeof(uchar);
for(int i=0; i<vMask->height;i++){
for(int j=0; j<vMask->width;j++){
if((float)data_mask[i*step_mask+j] == 255){
_threashold++;
}
}
}
if(_threashold<threashold) return FALSE;
return TRUE;
}
void FrameObject::detectAll(initializationParams initPar){
LOG4CXX_DEBUG(loggerFrameObject , "Detection All");
int fitting=initPar.fitting;
try{
IplImage * img = getFrame();
IplImage * background = getBackground();
DetectedObject *temp = new DetectedObject(size,img->depth);
//cameraCorrection(img,img,MEDIAN,1.1,5);
double TH = initPar.THRESHOLD;
backgroundSuppression(img,background,this->foregroundMask);
//cvThreshold(this->foregroundMask,this->salientForegroundMask,TL,255,CV_THRESH_BINARY);
cvThreshold(this->foregroundMask,this->foregroundMask,TH,255,CV_THRESH_BINARY);
LOG4CXX_TRACE(loggerFrameObject, "Background mask created");
//elemento strutturante
LOG4CXX_TRACE(loggerFrameObject, "Morphing correction");
IplConvKernel *element=cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_ELLIPSE, NULL);
//operazione morfolologica del gradiente, OPEN per migliorare la maschera
cvMorphologyEx (this->foregroundMask, this->foregroundMask, NULL, element, CV_MOP_OPEN, 1);
cvReleaseStructuringElement(&element);
LOG4CXX_TRACE(loggerFrameObject, "Morphing correction completed");
//estraggo le blob relative alla scena
IplImage *labelImg=cvCreateImage(size, IPL_DEPTH_LABEL, 1);
unsigned int labeled=cvLabel(this->foregroundMask, labelImg,this->frameBlobs);
LOG4CXX_TRACE(loggerFrameObject, "Area Filtering");
cvFilterByArea(this->frameBlobs,initPar.minArea,initPar.maxArea);
IplImage *src = cvCreateImage(size,8,3);
//salvo i valori relativi all'oggetto :
// creo la mashera di ogni blob e divido ombra da oggetto
LOG4CXX_DEBUG(loggerFrameObject, "salvo i valori relativi all'oggetto, creo la mashera di ogni blob e divido ombra da oggetto");
for (CvBlobs::const_iterator it=this->frameBlobs.begin(); it!=this->frameBlobs.end(); ++it)
{
cvZero(src);
cvRenderBlob(labelImg,it->second,img,src,CV_BLOB_RENDER_COLOR);
cvZero(temp->totalMask);
cvZero(temp->shadowMask);
cvCvtColor(src,temp->totalMask,CV_RGB2GRAY);
//maschera dell'ombra
shadowDetection(img,background,temp->totalMask,temp->shadowMask,initPar);
//maschera invertita dell'ombra
cvThreshold(temp->shadowMask,temp->invertedShadowMask,120,255,CV_THRESH_BINARY_INV);
//and tra la maschera del foreground e la maschera invertita dell'ombra
// ottengo la maschera del MVO
cvAnd(temp->totalMask,temp->invertedShadowMask,temp->mvoMask);
//creo l'ogetto blob dell'ogetto necessario per il traking
IplImage *label=cvCreateImage(size, IPL_DEPTH_LABEL, 1);
unsigned int labelResult=cvLabel(temp->mvoMask, label, temp->mvoBlobs);
cvReleaseImage(&label);
//Ritaglio dell'mvo
cvOr(frame,temp->mvo,temp->mvo,temp->mvoMask);
/*LA BLOB DEVE ESSERE UN SALIENT FOREGROUND*/
IplImage * verityMask=cvCloneImage(temp->mvoMask);
cvZero(verityMask);
cvAnd(temp->mvoMask,this->getSalientMask(),verityMask,temp->mvoMask);
if(isToSave(verityMask,temp->mvoMask,fitting)){
//salvo il risultato nella lista
detectedObject.push_front(temp);
temp = new DetectedObject(size,img->depth);
}
cvReleaseImage(&verityMask);
}
temp->~_DetectedObject();
cvReleaseImage(&labelImg);
cvReleaseImage(&img);
cvReleaseImage(&background);
cvReleaseImage(&src);
}
catch(exception& e)
{
LOG4CXX_ERROR(loggerFrameObject, "Frame number " << this->frameNumber << " losted: \n-\t" << e.what());
}
LOG4CXX_DEBUG(loggerFrameObject, "Detection completed: " << this->detectedObject.size() << " object detected.");
}
list<DetectedObject*> FrameObject:: getDetectedObject(){
return detectedObject;
}