demo - 副本.cpp
9.93 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "MSRegionSurveilance.h"
#include "highgui.h"
#include "cv.h"
#include "RegionAssist.h"
#include "time_debug.h"
#include <opencv2/opencv.hpp>
using namespace std;
//using namespace cv;
void imag_ana_1channels();
void imag_ana_3channels();
int main()
{
imag_ana_1channels();
//imag_ana_3channels();
system("pause");
}
void imag_ana_3channels()
{
//AlarmInfo 1进入禁区 2离开禁区 3单向越界 4双向越界 5徘徊 6丢包
int numROI = 1;
debug_time_t stime, etime;
RegionInfo* pRegionInfo = new RegionInfo[numROI];
int LeftPointX = 1050;
int LeftPointY = 450;
int RightPointX = 1400;
int RightPointY = 600;
// 进入禁区
pRegionInfo[0].AlarmInfo = 1;
pRegionInfo[0].nPointNum = 4;
pRegionInfo[0].pROI[0].x = LeftPointX;
pRegionInfo[0].pROI[0].y = LeftPointY;
pRegionInfo[0].pROI[1].x = RightPointX;
pRegionInfo[0].pROI[1].y = LeftPointY;
pRegionInfo[0].pROI[2].x = RightPointX;
pRegionInfo[0].pROI[2].y = RightPointY;
pRegionInfo[0].pROI[3].x = LeftPointX;
pRegionInfo[0].pROI[3].y = RightPointY;
//pRegionInfo[0].AlarmInfo = 1;
//pRegionInfo[0].nPointNum = 4;
//pRegionInfo[0].pROI[0].x = LeftPointX/*226*/;
//pRegionInfo[0].pROI[0].y = LeftPointY;
//pRegionInfo[0].pROI[1].x = RightPointX/*223*/;
//pRegionInfo[0].pROI[1].y = LeftPointY;
//pRegionInfo[0].pROI[2].x = LeftPointX/*223*/;
//pRegionInfo[0].pROI[2].y = RightPointY;
//pRegionInfo[0].pROI[3].x = RightPointX/*223*/;
//pRegionInfo[0].pROI[3].y = RightPointY;
//pRegionInfo[0].dirPoint.x = 200;//用于定义方向的点 位于合法方向指向的一侧
//pRegionInfo[0].dirPoint.y = 109;
// 定义
MS_RegionSurveillance MS1;
IRegionSurveillance* test = MS1.GetMS_RS();
if (!test)
{
printf("model initial failed");
exit(0);
}
CvCapture* video1 = cvCaptureFromFile("../../data/test1.mp4");
// 用opencv函数读取视频的一帧
IplImage * src = cvQueryFrame(video1);
IplImage * srcscale = cvCreateImage(cvSize(src->width, src->height), 8, 3);
cvResize(src, srcscale);
// 获得图像信息
int width = srcscale->width;
int height = srcscale->height;
int widthstep = srcscale->widthStep;
int nChannel = srcscale->nChannels;
int frameCount = 0; // 处理的图像序号
MS_ObjectInfo *ObjInfo = NULL; // 结果结构体
int ObjCount;
int error_flag;
long total_t = 0;
// 循环处理视频的每一帧
while (src)
{
src = cvQueryFrame(video1);
if (!src)
break;
cvResize(src, srcscale);
// 如果当前处理的是第一帧,则要进行初始化
if (frameCount == 0)
{
error_flag = test->RSinit(width, height, widthstep, (unsigned char*)(srcscale->imageData), nChannel, 3, 0, 10, 1000000);
if (error_flag == 0)
{
cout << "设置有问题!!!" << endl;
return;
}
error_flag = test->RSRegion(numROI, pRegionInfo, false);
if (error_flag == 0)
{
cout << "区域设置有问题!!!" << endl;
return;
}
}
GetTime(&stime);
// 智能分析
test->RSDetect((unsigned char*)(srcscale->imageData), pRegionInfo);
GetTime(&etime);
total_t += CalcTimeDiff(&stime, &etime);
//printf("RSDetect %d:%ld fps\n", frameCount, cost_t);
// 获取智能分析结果
ObjCount = test->getObjectNum();
ObjInfo = new MS_ObjectInfo[ObjCount];
test->getObjectInfo(ObjCount, ObjInfo);
printf("alram:\n");
for (int i = 0; i < ObjCount; i++)
{
if (ObjInfo[i].pbAlarmType[0])
{
switch (ObjInfo[i].pbAlarmType[0])
{
case 1:
printf("obj %d: 进入禁区\n", ObjInfo[i].UniqueID);
break;
case 2:
printf("obj %d: 离开禁区\n", ObjInfo[i].UniqueID);
break;
case 3:
printf("obj %d: 单向越界\n", ObjInfo[i].UniqueID);
break;
case 4:
printf("obj %d: 双向越界\n", ObjInfo[i].UniqueID);
break;
case 5:
printf("obj %d: 徘徊\n", ObjInfo[i].UniqueID);
break;
case 6:
printf("obj %d: 丢包\n", ObjInfo[i].UniqueID);
break;
}
}
}
cvRectangle(srcscale, cvPoint(LeftPointX, LeftPointY), cvPoint(RightPointX, RightPointY), cvScalar(0, 0, 0));
// 轨迹描绘
Trajectory((unsigned char*)(srcscale->imageData), ObjInfo, ObjCount, width, height, widthstep);
// 目标框描绘
ExternalRectangle((unsigned char*)(srcscale->imageData), ObjInfo, ObjCount, widthstep, numROI);
//画出警报区域
//disp area 1
/*for (int i = 0; i < numROI; i++)
{
cvLine(srcscale, cvPoint(pRegionInfo[i].pROI[0].x, pRegionInfo[i].pROI[0].y),
cvPoint(pRegionInfo[i].pROI[1].x, pRegionInfo[i].pROI[1].y), cvScalar(255, 0, 255), 1);
//获取箭头端点坐标
if (pRegionInfo[i].AlarmInfo == 3)
{
cv::Point pStart, pEnd;//by zl
pStart.x = pRegionInfo[i].ArrowStartPoint.x;
pStart.y = pRegionInfo[i].ArrowStartPoint.y;
pEnd.x = pRegionInfo[i].ArrowEndPoint.x;
pEnd.y = pRegionInfo[i].ArrowEndPoint.y;
DrawArrow(srcscale, pStart, pEnd, 15, 30);//调用绘制箭头函数
//显示dirPoint
cv::Point pt1;
pt1.x = pRegionInfo[i].dirPoint.x;
pt1.y = pRegionInfo[i].dirPoint.y;
cvLine(srcscale, pt1, pt1, cvScalar(255, 0, 255), 3);
}
//
}*/
++frameCount;
// 图像显示
cvShowImage("3channels", srcscale);
cvWaitKey(1);
delete[] ObjInfo;
} //循环处理结束
long aver_t = total_t / frameCount;
printf("aver time:%ld us", aver_t);
delete[] pRegionInfo;
test->RSrelease();
MS1.Destroy();
cvReleaseCapture(&video1);
cvReleaseImage(&srcscale);
}
void imag_ana_1channels()
{
//AlarmInfo 1进入禁区 2离开禁区 3单向越界 4双向越界 5徘徊 6丢包
int numROI = 1;
debug_time_t stime, etime;
RegionInfo* pRegionInfo = new RegionInfo[numROI];
int LeftPointX = 1050;
int LeftPointY = 450;
int RightPointX = 1400;
int RightPointY = 600;
// 进入禁区
int index = 0;
pRegionInfo[index].AlarmInfo = 1;
pRegionInfo[index].nPointNum = 4;
pRegionInfo[index].pROI[0].x = LeftPointX;
pRegionInfo[index].pROI[0].y = LeftPointY;
pRegionInfo[index].pROI[1].x = RightPointX;
pRegionInfo[index].pROI[1].y = LeftPointY;
pRegionInfo[index].pROI[2].x = RightPointX;
pRegionInfo[index].pROI[2].y = RightPointY;
pRegionInfo[index].pROI[3].x = LeftPointX;
pRegionInfo[index].pROI[3].y = RightPointY;
index = 0;
//离开禁区
pRegionInfo[index].AlarmInfo = 2;
pRegionInfo[index].nPointNum = 4;
pRegionInfo[index].pROI[0].x = LeftPointX;
pRegionInfo[index].pROI[0].y = LeftPointY;
pRegionInfo[index].pROI[1].x = RightPointX;
pRegionInfo[index].pROI[1].y = LeftPointY;
pRegionInfo[index].pROI[2].x = RightPointX;
pRegionInfo[index].pROI[2].y = RightPointY;
pRegionInfo[index].pROI[3].x = LeftPointX;
pRegionInfo[index].pROI[3].y = RightPointY;
// 定义
MS_RegionSurveillance MS1;
IRegionSurveillance* test = MS1.GetMS_RS();
if (!test)
{
printf("model initial failed");
exit(0);
}
CvCapture* video1 = cvCaptureFromFile("../../data/test1.mp4");
// 用opencv函数读取视频的一帧
IplImage * src = cvQueryFrame(video1);
IplImage * srcscale = cvCreateImage(cvSize(src->width, src->height), 8, 3); //用于缩放 当前先不缩放
IplImage * gray = cvCreateImage(cvSize(srcscale->width, srcscale->height), 8, 1);
cvResize(src, srcscale);
cvCvtColor(srcscale, gray, CV_RGB2GRAY);
//cv::Mat saveImg = cv::cvarrToMat(src, true);
//cv::rectangle(saveImg, cv::Rect(1000, 450, 1450-1000, 600-450), cv::Scalar(255, 255, 0), 2);
//cv::imwrite("img.jpg", saveImg);
//cv::imshow("image", saveImg);
//cv::waitKey(0);
CvVideoWriter *writer = cvCreateVideoWriter("result_2019_9_23.avi", CV_FOURCC('M', 'J', 'P', 'G'), 30, cvSize(srcscale->width, srcscale->height));
int frameCount = 0; // 处理的图像序号
MS_ObjectInfo *ObjInfo = NULL; // 结果结构体
int ObjCount;
int error_flag;
long total_t = 0;
printf("width = %d height = %d\n", gray->width, gray->height);
// 循环处理视频的每一帧
while (src)
{
src = cvQueryFrame(video1);
if (!src)
break;
cvResize(src, srcscale);
cvCvtColor(srcscale, gray, CV_RGB2GRAY);
// 如果当前处理的是第一帧,则要进行初始化
if (frameCount == 0)
{
error_flag = test->RSinit(gray->width, gray->height, gray->widthStep,
(unsigned char*)(gray->imageData), gray->nChannels, 1, 0, 10, 1000000);
if (error_flag == 0)
{
cout << "设置有问题!!!" << endl;
return;
}
error_flag = test->RSRegion(numROI, pRegionInfo, false);
if (error_flag == 0)
{
cout << "区域设置有问题!!!" << endl;
return;
}
}
GetTime(&stime);
// 智能分析
test->RSDetect((unsigned char*)(gray->imageData), pRegionInfo);
GetTime(&etime);
total_t += CalcTimeDiff(&stime, &etime);
printf("detect time:%ld us\n", CalcTimeDiff(&stime, &etime));
// 获取智能分析结果
ObjCount = test->getObjectNum();
ObjInfo = new MS_ObjectInfo[ObjCount];
test->getObjectInfo(ObjCount, ObjInfo);
//报警区域
cvRectangle(srcscale, cvPoint(LeftPointX, LeftPointY), cvPoint(RightPointX, RightPointY), cvScalar(0, 0, 0));
// 轨迹描绘
Trajectory((unsigned char*)(srcscale->imageData), ObjInfo, ObjCount, srcscale->width, srcscale->height, srcscale->widthStep);
// 目标框描绘
ExternalRectangle((unsigned char*)(srcscale->imageData), ObjInfo, ObjCount, srcscale->widthStep, numROI);
for (int i = 0; i < ObjCount; i++)
{
for (int j = 0; j < numROI; j++)
{
if (ObjInfo[i].pbAlarmType[j])
{
switch (ObjInfo[i].pbAlarmType[j])
{
case 1:
printf("obj %d: 进入禁区\n", ObjInfo[i].UniqueID);
break;
case 2:
printf("obj %d: 离开禁区\n", ObjInfo[i].UniqueID);
break;
case 3:
printf("obj %d: 单向越界\n", ObjInfo[i].UniqueID);
break;
case 4:
printf("obj %d: 双向越界\n", ObjInfo[i].UniqueID);
break;
case 5:
printf("obj %d: 徘徊\n", ObjInfo[i].UniqueID);
break;
case 6:
printf("obj %d: 丢包\n", ObjInfo[i].UniqueID);
break;
}
}
}
}
++frameCount;
cvWriteFrame(writer, srcscale);
// 图像显示
cvShowImage("1channels", srcscale);
cvWaitKey(1);
delete[] ObjInfo;
} //循环处理结束
long aver_t = total_t / frameCount;
printf("aver time:%ld us", aver_t);
cvReleaseVideoWriter(&writer);
delete[] pRegionInfo;
test->RSrelease();
MS1.Destroy();
cvReleaseCapture(&video1);
cvReleaseImage(&srcscale);
cvReleaseImage(&gray);
}