-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnalyzerUnit.cpp
More file actions
228 lines (166 loc) · 6.46 KB
/
Copy pathAnalyzerUnit.cpp
File metadata and controls
228 lines (166 loc) · 6.46 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <vector>
#include <string>
#include <dirent.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include "AnalyzerUnit.hpp"
#include "ImageEntropyMethods/ImageEntropyMethods.hpp"
#include "LBP/LBPUser.hpp"
#include "AlgorithmTraining/Trainer.hpp"
#include "common/UtilityFunctions.hpp"
AnalyzerUnit::AnalyzerUnit(std::string EventID, std::string ImageDir, int CameraNumber, Trainer** TrainedData)
{
/*Give the properties required to make the object - the identifiers i.e. the camera number, and location*/
this->ImageDir=ImageDir;
this->CameraNumber=CameraNumber;
this->EventID=EventID;
this->TrainedData = *TrainedData;
}
AnalyzerUnit::~AnalyzerUnit(void ){
/*Clear the bubble memory pointers*/
for (int i=0; i<this->BubbleList.size(); i++){
delete this->BubbleList[i];
}
}
/*! \brief Parse and sort a list of triggers from an event for a specific camera
*
* \param searchPattern
* \return this->CameraFrames
*
* This function makes a list of all the file names matching the trigger
* from a certain camera in a file and then makes aa sorted
* list of all the images that can then be processed one by one .
*/
void AnalyzerUnit::ParseAndSortFramesInFolder( void )
{
std::string searchPattern = "cam"+std::to_string(this->CameraNumber)+"_image";
/*Function to Generate File Lists*/
{
DIR *dir = opendir (this->ImageDir.c_str()) ;
if (dir)
{
/* print all the files and directories within directory */
struct dirent* hFile;
while (( hFile = readdir( dir )) != NULL )
{
if ( !strcmp( hFile->d_name, "." )) continue;
if ( !strcmp( hFile->d_name, ".." )) continue;
// in linux hidden files all start with '.'
if ( hFile->d_name[0] == '.' ) continue;
// dirFile.name is the name of the file. Do whatever string comparison
// you want here. Something like:
if ( strstr( hFile->d_name, searchPattern.c_str() ))
//printf( "found an .bmp file: %s\n", hFile->d_name );
this->CameraFrames.push_back(std::string(hFile->d_name));
}
closedir (dir);
std::sort(this->CameraFrames.begin(), this->CameraFrames.end(), frameSortFunc);
}
else
{
/* could not open directory */
//perror ("");
this->StatusCode = 1;
}
}
}
/*! \brief Find the trigger frame
*
* \return int this->TriggerFrame
*
* Function uses the Image Entropy algorithm to search for
* and decide the frame where the bubble can be first seen. (Trigger frame)
*/
void AnalyzerUnit::FindTriggerFrame(void ){
/*First, check if the sequence of events is malformed*/
if (this->CameraFrames.size()<20){
this->okToProceed=false;
this->TriggerFrameIdentificationStatus = -9;
return;
}
cv::Mat workingFrame, img_mask0, img_mask1;
cv::Mat comparisonFrame;
/*Static variable to store the threshold entropy WHERE USED??*/
float entropyThreshold;
//if(this->CameraNumber==2) entropyThreshold = 0.0009;
//else entropyThreshold = 0.0003;
entropyThreshold = 0.0003;
/*Variable to store the current entropy in*/
float singleEntropy;
/*The reference image does not change, it is the first frame*/
std::string refImg = this->ImageDir + this->CameraFrames[0];
//std::cout<<"Ref Image: "<<refImg<<"\n";
if(getFilesize(refImg)<1000000){
this->okToProceed=false;
this->TriggerFrameIdentificationStatus = -9;
return;
}
comparisonFrame = cv::imread(refImg.c_str());
/*Start by flagging that a bubble wasnt found, flag gets changed to 0 if all goes well*/
this->TriggerFrameIdentificationStatus=-3;
//for (int i = 1; i < this->CameraFrames.size(); i++) {
for (int i = 1; i < 30; i++) {
/*The name and load to memory evalImage*/
std::string evalImg = this->ImageDir + this->CameraFrames[i];
/*Check if image is malformed. If yes, then stop*/
if(getFilesize(evalImg)<1000000){
this->okToProceed=false;
this->TriggerFrameIdentificationStatus = -9;
return;
}
workingFrame = cv::imread(evalImg.c_str());
/*BackgroundSubtract*/
cv::absdiff(workingFrame, comparisonFrame, img_mask0);
/*Find LBP and then calculate Entropy*/
//img_mask1 = lbpImage(img_mask0);
singleEntropy = calculateEntropyFrame(img_mask0);
/* ****************
* Debug Point here
* **************** */
//debugShow(img_mask1);
//std::cout<<"Entropy of BkgSub "<<i+30<<" image: "<<singleEntropy<<"\n";
/*Calculate entropy of ROI*/
if (i==1 and singleEntropy>0.0005){
//printf("************ ---> WARNING <--******************\n");
//printf("Entropy is massive - something has triggered at the very first frame | ");
//printf("Autobub Image analysis is meaningless on this data set. Manual check recommended\n");
//printf(" *** Autobub skip --> *** \n");
this->TriggerFrameIdentificationStatus = -2;
this->okToProceed=false;
break;
//exit (EXIT_FAILURE);
}
//std::cout<<"Frame Entropy: "<<singleEntropy<<std::endl;
/*Nothing works better than manual entropy settings. :-(*/
if (singleEntropy > entropyThreshold and i > this->minEvalFrameNumber) {
this->TriggerFrameIdentificationStatus = 0;
this->MatTrigFrame = i;
break;
}
}
if (this->TriggerFrameIdentificationStatus==-3) {
//printf("\n**No bubbles were found. Manually check this folder**\n");
this->okToProceed=false;
this->MatTrigFrame;
}
}
/*Misc functions*/
/*! \brief Sorting function for camera frames
*
* To be used by std::sort for sorting files associated with
* the camera frames. Not to be used otherwise.
*/
bool frameSortFunc(std::string i, std::string j)
{
unsigned int sequence_i, camera_i;
int got_i = sscanf(i.c_str(), "cam%d_image%u.png",
&camera_i, &sequence_i
);
assert(got_i == 2);
unsigned int sequence_j, camera_j;
int got_j = sscanf(j.c_str(), "cam%d_image%u.png",
&camera_j, &sequence_j
);
assert(got_j == 2);
return sequence_i < sequence_j;
}