-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
238 lines (193 loc) · 7.1 KB
/
main.cpp
File metadata and controls
238 lines (193 loc) · 7.1 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
229
230
231
232
233
234
235
236
237
238
#include "anomalies-detecting.h"
//#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <time.h>
using namespace cv;
void drawProximity(const cv::Mat& coeffs, Mat& imageGray,
int offsetX, int offsetY, int squeezeMultiplyFactor, float threshold)
{
cv::Rect rct(offsetX, offsetY, coeffs.cols * squeezeMultiplyFactor, coeffs.rows * squeezeMultiplyFactor);
cv::Scalar clr{ 255, 0, 255 };
cv::rectangle(imageGray, rct, clr);
const float minValue = threshold;
const float maxValue = 1;
const float medium = (minValue + maxValue) / 2;
for (int y = 0; y < coeffs.rows; ++y)
for (int x = 0; x < coeffs.cols; ++x)
{
auto value = coeffs.at<float>({ x, y });
if (value < minValue)
continue;
for (int i = 0; i < squeezeMultiplyFactor; ++i)
for (int j = 0; j < squeezeMultiplyFactor; ++j)
{
Point pt(x * squeezeMultiplyFactor + i + offsetX, y * squeezeMultiplyFactor + j + offsetY);
if (pt.x < 0 || pt.x >= imageGray.cols || pt.y < 0 || pt.y >= imageGray.rows)
continue;
// get pixel
auto color = imageGray.at<Vec3b>(pt);
// ... do something to the color ....
if (value < medium)
{
const auto coeff = (value - minValue) / (medium - minValue);
color[1] = 255 * coeff;
color[0] = 255 * (1.f - coeff);
color[2] = 0;
}
else
{
const auto coeff = (value - medium) / (maxValue - medium);
color[2] = 255 * coeff;
color[1] = 255 * (1.f - coeff);
color[0] = 0;
}
// set pixel
imageGray.at<Vec3b>(pt) = color;
}
}
}
void ConnectedComponentsStats(cv::Mat& img, cv::Mat& output,
int offsetX, int offsetY, int squeezeMultiplyFactor)
{
using namespace std;
// Use connected components with stats
Mat labels, stats, centroids;
auto num_objects = connectedComponentsWithStats(img, labels, stats, centroids);
// Check the number of objects detected
if (num_objects < 2) {
cout << "No objects detected" << endl;
return;
}
else {
cout << "Number of objects detected: " << num_objects - 1 << endl;
}
// Create output image coloring the objects and show area
for (auto i = 1; i < num_objects; i++) {
cv::Rect rct(
stats.at<int>(i, CC_STAT_LEFT) * squeezeMultiplyFactor + offsetX,
stats.at<int>(i, CC_STAT_TOP) * squeezeMultiplyFactor + offsetY,
stats.at<int>(i, CC_STAT_WIDTH) * squeezeMultiplyFactor,
stats.at<int>(i, CC_STAT_HEIGHT) * squeezeMultiplyFactor
);
cv::Scalar clr{ 255, 0, 255 };
cv::rectangle(output, rct, clr);
}
}
inline int GetSqueezeExtra(int size)
{
enum { MaxFragmentSize = 512 };
return (size + MaxFragmentSize - 1) / MaxFragmentSize;
}
int main(int argc, char** argv)
{
try
{
clock_t start = clock();
// set default values for tracking algorithm and video
const char* videoPath = (argc >= 2) ? argv[1] : "videos/run.mp4";
// create a video capture object to read videos
cv::VideoCapture cap(videoPath);
Mat frame;
if (!cap.isOpened())
{
frame = cv::imread(videoPath);
if (frame.empty())
{
std::cerr << "Unable to open the file. Exiting!\n";
return -1;
}
}
else
{
// Capture the current frame
cap >> frame;
}
//char ch;
Mat flowImageGray;
const char windowName[] = "Anomalies";
namedWindow(windowName, WINDOW_NORMAL);
if (frame.empty())
{
std::cerr << "No image in the file. Exiting!\n";
return -1;
}
const int StartSqueezeMultiplyFactor = 4;
const int FragmentSize = 256;
std::vector<cv::Rect> positions;
if (argc > 2)
{
try {
std::istringstream ss(argv[2]);
std::string buffer;
while (std::getline(ss, buffer, ','))
{
int x = std::stoi(buffer);
if (!std::getline(ss, buffer, ','))
break;
int y = std::stoi(buffer);
positions.push_back({ x, y, FragmentSize, FragmentSize });
}
}
catch (...) {
positions.clear();
}
}
if (positions.empty())
{
positions.push_back({0, 0, frame.cols, frame.rows});
}
const float threshold = 0.5f;
// Iterate until the user presses the Esc key
while (true)
{
// Convert to grayscale
//cvtColor(frame, flowImageGray, COLOR_BGR2GRAY);
flowImageGray = frame;
for (auto& rect : positions)
{
// must be a multiple of 4
const int SqueezeMultiplyFactor = StartSqueezeMultiplyFactor
* std::max(GetSqueezeExtra(rect.width), GetSqueezeExtra(rect.height));
const auto proximity = getAnomalies(frame, rect, SqueezeMultiplyFactor);
// Draw the optical flow map
drawProximity(proximity, flowImageGray, rect.x, rect.y, SqueezeMultiplyFactor, threshold);
cv::Mat img_thr(proximity.rows, proximity.cols, CV_8UC1);
for (int y = 0; y < proximity.rows; ++y)
for (int x = 0; x < proximity.cols; ++x)
{
auto value = proximity.at<float>({ x, y });
img_thr.at<uchar>({ x, y }) = (value >= threshold) ? 255 : 0;
}
ConnectedComponentsStats(img_thr, flowImageGray, rect.x, rect.y, SqueezeMultiplyFactor);
}
// Display the output image
imshow(windowName, flowImageGray);
// Break out of the loop if the user presses the Esc key
char ch = waitKey(10);
if (ch == 27)
break;
// Capture the current frame
Mat newFrame;
cap >> newFrame;
if (newFrame.empty())
{
std::cout << "Handling mapping in " << (double)(clock() - start) / CLOCKS_PER_SEC << " seconds" << std::endl;
waitKey(0);
break;
}
std::swap(frame, newFrame);
}
if (argc > 3)
{
imwrite(argv[3], flowImageGray);
}
return 0;
}
catch (const std::exception& ex)
{
std::cerr << "Fatal: " << ex.what() << '\n';
return 1;
}
}