-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnalyzerUnit.cpp
More file actions
216 lines (140 loc) · 5.46 KB
/
AnalyzerUnit.cpp
File metadata and controls
216 lines (140 loc) · 5.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
#include <vector>
#include <string>
#include <dirent.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include "AnalyzerUnit.hpp"
#include "common/UtilityFunctions.hpp"
#include "LoadTemplates.hpp"
#define SubPixelAveragingGrid 1
AnalyzerUnit::AnalyzerUnit(std::string EventID, std::string ImageDir, int CameraNumber, std::vector<FiducialMark>& Templates)
{
/*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->Templates = Templates;
}
AnalyzerUnit::~AnalyzerUnit(void ){
}
/*! \brief Load a frame for fiducial tracking
*
* \param void
* \return loads the frame to the class variable
*
*/
void AnalyzerUnit::LoadFrameForFiducialTracking(void){
LoadFrameName = this->ImageDir+"cam"+std::to_string(this->CameraNumber)+"_image"+std::to_string(this->AnalyzeFrame)+".png";
if (getFilesize(LoadFrameName) < 1000000) {
this->okToProceed=false;
std::cout<<"Malformed image: "<<LoadFrameName<<"\n";
std::cout<<"Skipping on this event and continue...\n";
throw -10;
} else {
this->AnalysisFrame=cv::imread(LoadFrameName,0);
this->okToProceed = true;
}
}
/*! \brief Track Fiducial marks
*
* \param void
* \return loads fiducial marks in cv::point vector
*
*/
void AnalyzerUnit::TrackAllFiducialMarks(void){
/*How many Markers to track*/
int nMarkersToTrack = this->Templates.size();
/*Track Templates*/
cv::Point2f _storeEachTemplateBestMatchLoc;
cv::Point2f _thisReferencePoint;
for (int i=0; i<nMarkersToTrack; i++){
this->TrackAFeature(this->Templates[i], _storeEachTemplateBestMatchLoc);
/*Position matches*/
this->TemplatePos.push_back(_storeEachTemplateBestMatchLoc);
/*Reference points*/
_thisReferencePoint = cv::Point2f(this->Templates[i].TemplateX, this->Templates[i].TemplateY);
this->ReferencePoints.push_back(_thisReferencePoint);
//std::cout<<"Template X: "<<this->TemplatePos[i].x<<" Y: "<<this->TemplatePos[i].y<<"\n";
/*GC*/
_thisReferencePoint = cv::Point2f(0.0,0.0);
_storeEachTemplateBestMatchLoc = cv::Point2f(0.0,0.0);
}
this->okToProceed = true;
}
/*! \brief Track a keypoint
*
* \param cv::Mat template to track, TBI:
* \return cv::Point x,y of the template
*
*/
void AnalyzerUnit::TrackAFeature(FiducialMark& Template, cv::Point2f& BestMatchLoc){
cv::Mat result;
/*Restrict the ROT of the analysisFrame*/
cv::Rect TemplateSearchZone = cv::Rect(Template.TemplateX-45, Template.TemplateY-45, 90, 90);
cv::Mat _AnaFrameSmallROI = cv::Mat(this->AnalysisFrame, TemplateSearchZone);
// Create the result matrix
int result_cols = _AnaFrameSmallROI.cols - Template.TemplateImage.cols + 1;
int result_rows = _AnaFrameSmallROI.rows - Template.TemplateImage.rows + 1;
result.create( result_rows, result_cols, CV_32F );
// Do the Matching and Normalize
int match_method=CV_TM_SQDIFF;
cv::matchTemplate( _AnaFrameSmallROI, Template.TemplateImage, result, match_method );
cv::normalize( result, result, 0, 1, cv::NORM_MINMAX, -1, cv::Mat() );
// Localizing the best match with minMaxLoc
double minVal; double maxVal; cv::Point minLoc; cv::Point maxLoc;
cv::Point matchLoc;
cv::minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat() );
// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if( match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )
{ matchLoc = minLoc; }
else
{ matchLoc = maxLoc; }
/*Append result to return var*/
cv::Point2f BestMatchSubPixel;
BestMatchSubPixel = cv::Point2f(0.0,0.0);
float total_mass=0.0;
float inv_mass=0.0;
float pixel_value=0.0;
for (int i=-SubPixelAveragingGrid; i<=SubPixelAveragingGrid; i++){
for (int j=-SubPixelAveragingGrid; j<=SubPixelAveragingGrid; j++){
pixel_value = result.at<float>(matchLoc.y+j, matchLoc.x+i);
BestMatchSubPixel+=cv::Point2f(matchLoc.x+i,matchLoc.y+j)*pixel_value;
total_mass +=pixel_value;
}
}
inv_mass = 1.0/total_mass;
BestMatchSubPixel *= inv_mass;
/*Offset correction*/
BestMatchLoc = cv::Point2f(BestMatchSubPixel.x+TemplateSearchZone.x+Template.correctionsX, BestMatchSubPixel.y+Template.correctionsY+TemplateSearchZone.y);
//std::cout<<BestMatchLoc<<"\n";
result.release();
}
/*! \brief Calculate perspective transform homotopy
*
* \param void
* \return perspectivetransform
*
*/
void AnalyzerUnit::CalculatePerspectiveShift(void){
this->HomographyMatrix = cv::findHomography(this->TemplatePos, this->ReferencePoints, CV_RANSAC, 3);
}
/*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;
}