-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeeker.cpp
More file actions
161 lines (126 loc) · 5.13 KB
/
Seeker.cpp
File metadata and controls
161 lines (126 loc) · 5.13 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
#include "Seeker.hpp"
const int Seeker::MIN_OBJECT_AREA = 20 * 20;
vector<Point> Seeker::circle_contour = {Point(100, 100), Point(200,100), Point(200, 200), Point(100, 200)};
Mat Seeker::freezedTrajectory(720, 1280, CV_8UC3);
Mat Seeker::freezedTrajectoryGrey(720, 1280, CV_8UC1);
/// В конструкторе инициализируем массивы для графиков
Seeker::Seeker() {
};
/// Поиск объекта, определение его границ и координат центра
bool Seeker::findObject(cv::Mat* Output_frame, cv::Mat* BGR_frame, int FRAME_WIDTH, int FRAME_HEIGHT) {
// Поиск контура
Output_frame->copyTo(frame);
contourPoints = 0;
contourPoints = Mat::zeros(BGR_frame->size().height, BGR_frame->size().width, CV_8UC3);
contourPointsColored = Mat::zeros(BGR_frame->size().height, BGR_frame->size().width, CV_8UC3);
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(frame, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_NONE);
if ((hierarchy.size() == 0) || (hierarchy.size() == -1)) {
cout << "Контуры не найдены" << endl;
return false;
};
bool objectFound = false;
vector<int> areas(contours.size());
vector<int> xPos(contours.size());
vector<int> yPos(contours.size());
if (hierarchy.size() < 100)
{
///
for (int index = 0; index < contours.size(); index++) {
Moments moment = moments((cv::Mat)contours[index]);
double area = moment.m00;
xPos[index] = moment.m10 / area;
yPos[index] = moment.m01 / area;
if (area > 200000)
{ // Отсев слишком большого объекта
areas[index] = 0;
continue;
};
areas[index] = area;
};
// Поиск максимального элемента и его индекса в векторе areas
int Max_area = *std::max_element(begin(areas), end(areas));
int Max_area_index = distance(areas.begin(), find(areas.begin(), areas.end(), Max_area));
/// Если площадь объекта больше минимальной, то запоминаем контур
if (Max_area > MIN_OBJECT_AREA)
{
// СonvexHull контура
vector< vector<Point> > hull(contours.size());
convexHull(contours[Max_area_index], hull[Max_area_index]);
this->contour = hull[Max_area_index];
vector<vector<Point>> cont = {contour};
drawContours(contourPoints, cont, 0, Scalar(255, 255, 255), -1, 8);
BGR_frame->copyTo(contourPointsColored, contourPoints);
contourPointsColored.copyTo(freezedTrajectory, contourPoints);
// bitwise_or(freezedTrajectory, freezedTrajectory1, freezedTrajectory);
// Проверка на близость с границей
bool notAroundBorder = checkContourBorder(FRAME_WIDTH, FRAME_HEIGHT);
if (notAroundBorder) {
// Поиск и отрисовка прямоугольника, получение его меньшей стороны
RotatedRect box = minAreaRect(hull[Max_area_index]);
box.points(this->vtx);
float RotRectWidth = box.size.width;
float RotRectHeight = box.size.height;
float smallest_side = min(RotRectHeight, RotRectWidth);
this->pxX = xPos[Max_area_index];
this->pxY = yPos[Max_area_index];
this->pxDiameter = smallest_side;
return true;
} else {
return false;
};
}
else
{
return false;
};
}
else
{
cout << "Слишком много шума" << endl;
return false;
};
};
/// Отрисовка контура объекта, центра, вставка текста
void Seeker::drawObject(Mat *BGR_frame, float xPos, float yPos, float zPos, int xPosPx, int yPosPx) {
vector<vector<Point>> cont = {contour};
drawContours(*BGR_frame, cont, 0, Scalar(0, 0, 255), 2, 8);
circle(*BGR_frame, Point(xPosPx, yPosPx), 3, cv::Scalar(0, 0, 255));
putText(*BGR_frame, to_string(int(xPos*1000)) + " , " + to_string(int(yPos*1000)) + " , " + to_string(int(zPos*1000)), Point(xPosPx, yPosPx + 20), 1, 1, Scalar(255, 255, 0));
for (int i = 0; i < 4; i++) {
line(*BGR_frame, vtx[i], vtx[(i + 1) % 4], Scalar(0, 255, 0), 1, LINE_AA);
};
};
/// Выделение в кадре только мяча (без фона)
void Seeker::disableBackground(Mat* BGR_frame) {
BGR_frame->copyTo(contourPointsColored, contourPoints);
contourPointsColored.copyTo(*BGR_frame);
};
void Seeker::enableFreezedTrajectory(Mat* BGR_frame) {
// findNonZero(freezedTrajectory, contourFreezedPoints);
cvtColor(freezedTrajectory, freezedTrajectoryGrey, 6);
freezedTrajectory.copyTo(*BGR_frame, freezedTrajectoryGrey);
};
/// Возврат FALSE если контур мячика возле границы кадра
bool Seeker::checkContourBorder(int FRAME_WIDTH, int FRAME_HEIGHT) {
for (Point point : this->contour) {
if (abs((point.x) - FRAME_WIDTH) < 3 || abs((point.y) - FRAME_HEIGHT) < 3) {
return false;
} else if (abs((point.x) - FRAME_WIDTH) > (FRAME_WIDTH-3) || abs((point.y) - FRAME_HEIGHT) > (FRAME_HEIGHT-3)) {
return false;
} else {
continue;
}
};
return true;
};
int Seeker::get_pxX() {
return this->pxX;
};
int Seeker::get_pxY() {
return this->pxY;
};
int Seeker::get_pxDiameter() {
return this->pxDiameter;
};