-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.cpp
110 lines (67 loc) · 3.56 KB
/
blob.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
// Blob.cpp
#include "Blob.h"
Blob::Blob(std::vector<cv::Point> _contour) {
currentContour = _contour;
currentBoundingRect = cv::boundingRect(currentContour);
cv::Point currentCenter;
currentCenter.x = (currentBoundingRect.x + currentBoundingRect.x + currentBoundingRect.width) / 2;
currentCenter.y = (currentBoundingRect.y + currentBoundingRect.y + currentBoundingRect.height) / 2;
centerPositions.push_back(currentCenter);
dblCurrentDiagonalSize = sqrt(pow(currentBoundingRect.width, 2) + pow(currentBoundingRect.height, 2));
dblCurrentAspectRatio = (float)currentBoundingRect.width / (float)currentBoundingRect.height;
blnStillBeingTracked = true;
blnCurrentMatchFoundOrNewBlob = true;
intNumOfConsecutiveFramesWithoutAMatch = 0;
}
void Blob::predictNextPosition(void) {
int numPositions = (int)centerPositions.size();
if (numPositions == 1) {
predictedNextPosition.x = centerPositions.back().x;
predictedNextPosition.y = centerPositions.back().y;
}
else if (numPositions == 2) {
int deltaX = centerPositions[1].x - centerPositions[0].x;
int deltaY = centerPositions[1].y - centerPositions[0].y;
predictedNextPosition.x = centerPositions.back().x + deltaX;
predictedNextPosition.y = centerPositions.back().y + deltaY;
}
else if (numPositions == 3) {
int sumOfXChanges = ((centerPositions[2].x - centerPositions[1].x) * 2) +
((centerPositions[1].x - centerPositions[0].x) * 1);
int deltaX = (int)std::round((float)sumOfXChanges / 3.0);
int sumOfYChanges = ((centerPositions[2].y - centerPositions[1].y) * 2) +
((centerPositions[1].y - centerPositions[0].y) * 1);
int deltaY = (int)std::round((float)sumOfYChanges / 3.0);
predictedNextPosition.x = centerPositions.back().x + deltaX;
predictedNextPosition.y = centerPositions.back().y + deltaY;
}
else if (numPositions == 4) {
int sumOfXChanges = ((centerPositions[3].x - centerPositions[2].x) * 3) +
((centerPositions[2].x - centerPositions[1].x) * 2) +
((centerPositions[1].x - centerPositions[0].x) * 1);
int deltaX = (int)std::round((float)sumOfXChanges / 6.0);
int sumOfYChanges = ((centerPositions[3].y - centerPositions[2].y) * 3) +
((centerPositions[2].y - centerPositions[1].y) * 2) +
((centerPositions[1].y - centerPositions[0].y) * 1);
int deltaY = (int)std::round((float)sumOfYChanges / 6.0);
predictedNextPosition.x = centerPositions.back().x + deltaX;
predictedNextPosition.y = centerPositions.back().y + deltaY;
}
else if (numPositions >= 5) {
int sumOfXChanges = ((centerPositions[numPositions - 1].x - centerPositions[numPositions - 2].x) * 4) +
((centerPositions[numPositions - 2].x - centerPositions[numPositions - 3].x) * 3) +
((centerPositions[numPositions - 3].x - centerPositions[numPositions - 4].x) * 2) +
((centerPositions[numPositions - 4].x - centerPositions[numPositions - 5].x) * 1);
int deltaX = (int)std::round((float)sumOfXChanges / 10.0);
int sumOfYChanges = ((centerPositions[numPositions - 1].y - centerPositions[numPositions - 2].y) * 4) +
((centerPositions[numPositions - 2].y - centerPositions[numPositions - 3].y) * 3) +
((centerPositions[numPositions - 3].y - centerPositions[numPositions - 4].y) * 2) +
((centerPositions[numPositions - 4].y - centerPositions[numPositions - 5].y) * 1);
int deltaY = (int)std::round((float)sumOfYChanges / 10.0);
predictedNextPosition.x = centerPositions.back().x + deltaX;
predictedNextPosition.y = centerPositions.back().y + deltaY;
}
else {
// DO nothing
}
}