demo.cpp 11.2 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include "highgui.h"
#include "cv.h"
#include "RegionAssist.h"

#include "time_debug.h"
#include <deque>
#include <opencv2/opencv.hpp>
#include "ms_region_surveilance.h"
#include <cv.h>
#include <highgui.h>

using namespace std;
using namespace cv;

void imag_ana_3channels();

#define threshold_diff1 200 //设置简单帧差法阈值
#define threshold_diff2 200 //设置简单帧差法阈值

int first_update = 0;//首次更新标志  
#define Th 80  
#define Ta 80  

void calc_fore(IplImage *current, IplImage *back, IplImage *fore)
{
	int i, j;

	for (i = 0; i<current->height; i++)
	{
		for (j = 0; j<current->width; j++)
		{
			if (abs((u_char)current->imageData[i*current->widthStep + j] - (u_char)back->imageData[i*current->widthStep + j]) <= Ta)
			{
				fore->imageData[i*current->widthStep + j] = 0;//background  
			}
			else
			{
				fore->imageData[i*current->widthStep + j] = 255;//foreground  
			}
		}
	}
}

void update_currentback(IplImage *current, IplImage *curr_back)
{
	int i, j;
	int p, q;

	for (i = 0; i<current->height; i++)
	{
		for (j = 0; j<current->width; j++)
		{
			p = (u_char)current->imageData[i * current->widthStep + j];
			q = (u_char)curr_back->imageData[i * current->widthStep + j];
			//printf("%d,%d\n",p,q);  

			if (p >= q)
			{
				if (q == 255)
				{
					q = 254;
				}
				curr_back->imageData[i * current->widthStep + j] = q + 1;
			}
			else
			{
				if (q == 0)
				{
					q = 1;
				}
				curr_back->imageData[i * current->widthStep + j] = q - 1;
			}
		}
	}
}

void update_bufferedback(IplImage *curr_back, IplImage *buf_back, IplImage *abandon)
{
	int i, j, height, width;
	int leave_flag = 0;

	height = curr_back->height;
	width = curr_back->widthStep;

	if (first_update == 0)
	{
		for (i = 0; i < height; i++)
		{
			for (j = 0; j < width; j++)
			{
				if (abs((u_char)curr_back->imageData[i*width + j] - (u_char)buf_back->imageData[i*width + j]) <= Th)
				{
					abandon->imageData[i*width + j] = 0;//background  
				}
				else
				{
					abandon->imageData[i*width + j] = curr_back->imageData[i*width + j];//foreground  
					first_update = 1;
				}
			}
		}
		return;
	}

	//物体离开判断  
	for (i = 0; i < height; i++)
	{
		for (j = 0; j < width; j++)
		{
			if (abandon->imageData[i*width + j] != 0)
			{
				if (abandon->imageData[i*width + j] != curr_back->imageData[i*width + j])
				{
					leave_flag = 1;                 //物体掩膜处之前背景与当前的不一致,1:物体未离开,0:物体离开  
				}
			}
		}
	}

	if (leave_flag == 0)     //物体离开  
	{
		cvCopy(curr_back, buf_back);
		for (i = 0; i < height; i++)
		{
			for (j = 0; j < width; j++)
			{
				if (abandon->imageData[i*width + j] != 0)
				{
					abandon->imageData[i*width + j] = curr_back->imageData[i*width + j];
				}
			}
		}
	}
	else
	{
		for (i = 0; i < height; i++)
		{
			for (j = 0; j < width; j++)
			{
				if (abandon->imageData[i*width + j] != 0)
				{
					buf_back->imageData[i*width + j] = abandon->imageData[i*width + j];
				}
			}
		}
	}
}

int main()
{
	imag_ana_3channels();
	system("pause");
}



void init_param_video1(region_info* pRegionInfo);
void init_param_video2(region_info* pRegionInfo);

void imag_ana_3channels()
{
	int numROI = 2;

	debug_time_t stime, etime;

	region_info* pRegionInfo = new region_info[numROI];
	init_param_video2(pRegionInfo);

	void * test = nullptr;

	//CvCapture* video1 = cvCaptureFromFile("convert.mp4");
	//CvCapture* video1 = cvCaptureFromFile("test/20201211_152752.dav");
	CvCapture* video1 = cvCaptureFromFile("convert.mp4");

	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);

	cvSetCaptureProperty(video1, CV_CAP_PROP_POS_FRAMES, 0); 

	CvVideoWriter *writer = cvCreateVideoWriter("result_20201211_152752.avi", CV_FOURCC('D', 'I', 'V', 'X'), 25, cvSize(srcscale->width, srcscale->height));

	int frameCount = 0; // 处理的图像序号
						//MS_ObjectInfo *ObjInfo = NULL;  // 结果结构体
	int ObjCount;
	int error_flag;
	long total_t = 0;


	//初始化参数
	rs_param param;
	param.gpuid = 0;				//指定显卡id
									//其余算法参数 如长短边等
	param.image.set_data(srcscale->width, srcscale->height, srcscale->nChannels, (unsigned char *)srcscale->imageData);
	param.channel_deal = 3;
	param.filt_flag = 0;
	param.min_area = 200;
	param.max_area = 1000000;
	int ret = rs_init(&test, param);
	if (!test)
	{
		printf(" initial failed");
		exit(0);
	}

	//初始化有效区域
	error_flag = rs_init_region(test, numROI, pRegionInfo, false);
	//error_flag = test->RSRegion(numROI, pRegionInfo, false);
	if (error_flag != 0)
	{
		cout << "区域设置有问题!!!" << endl;
		return;
	}
	
	//初始化背景图片
	int init_bk_framecount = 0;
	while (src && init_bk_framecount < 1)
	{
		//src = cvQueryFrame(video1);    //正常使用 可初始化诺干帧 直到取到没有人的干净的背景画面
		cv::Mat back_image = cv::imread("base_rgb.jpg");
		
		sy_img img;
		img.set_data(back_image.cols, back_image.rows, back_image.channels(), (unsigned char *)back_image.data);
		rs_init_background(test, img);
		init_bk_framecount++;
	}

	printf("width = %d height = %d\n", srcscale->width, srcscale->height);
	// 循环处理视频的每一帧 
	std::deque<std::string> dqe;
	unsigned int frames = 0;
	while (src)
	{	
		src = cvQueryFrame(video1);

		if (!src)
			break;
		cvResize(src, srcscale);
		
		
		GetTime(&stime);
		rs_result result;
		sy_img img;
		img.set_data(srcscale->width, srcscale->height, srcscale->nChannels, (unsigned char *)srcscale->imageData);
		
		error_flag = rs_detect(test, img, numROI, pRegionInfo, &result);

		GetTime(&etime);
		total_t += CalcTimeDiff(&stime, &etime);
		printf("count=%d detect time:%ld ms\n", result.obj_count, CalcTimeDiff(&stime, &etime) / 1000);
		//DrawRegion((unsigned char*)(srcscale->imageData), pRegionInfo, numROI, srcscale->widthStep);
		//Trajectory((unsigned char*)(srcscale->imageData), result.obj_infos, result.obj_count, srcscale->width, srcscale->height, srcscale->widthStep, numROI);


		// 目标框描绘
		ExternalRectangle((unsigned char*)(srcscale->imageData), srcscale->width, srcscale->height, result.obj_infos, result.obj_count, srcscale->widthStep, numROI);
		std::string str;
		for (int i = 0; i < result.obj_count; i++)
		{
			for (int j = 0; j < numROI; j++)
			{
				for (int k = 0; k < ALARMTYPENUM; ++k)
				{
					if (result.obj_infos[i].pb_alarm_type[j][k])
					{
						switch (result.obj_infos[i].pb_alarm_type[j][k])
						{
						case 1:
							printf("obj %d: 进入禁区\n", result.obj_infos[i].unique_id);
							str = " 有目标闯入禁区 ";
							break;
						case 2:
							printf("obj %d: 离开禁区\n", result.obj_infos[i].unique_id);
							str = " 有目标离开禁区 ";
							break;
						case 3:
							printf("obj %d: 单向越界\n", result.obj_infos[i].unique_id);
							str = " 有目标单向越界 ";
							break;
						case 4:
							printf("obj %d: 双向越界\n", result.obj_infos[i].unique_id);
							str = " 有目标越界 ";
							break;
						case 5:
							printf("obj %d: 徘徊\n", result.obj_infos[i].unique_id);
							str = " 有目标在禁区内徘徊 ";
							break;
						case 6:
							printf("obj %d: 丢包\n", result.obj_infos[i].unique_id);
							str = " 丢包 ";
							break;
						default:
							break;
						}
					}
					else
					{
						;
					}
				}
			}
		}

		++frameCount;
		cvWriteFrame(writer, srcscale);
		// 图像显示
		cvShowImage("1channels", srcscale);
		//cvSaveImage("base_new.jpg", src); //保存图像
		cvWaitKey(1);

		++frames;
	}

	long aver_t = total_t / frameCount;
	printf("aver time:%ld us", aver_t);

	cvReleaseVideoWriter(&writer);

	cvReleaseCapture(&video1);
	cvReleaseImage(&srcscale);

	system("pause");
}

void init_param_video2(region_info* pRegionInfo)
{
	int index = 0;
	pRegionInfo[index].frame_num = 0;
	pRegionInfo[index].alarm_human = false;    //行人
	pRegionInfo[index].alarm_stay = true;     //丢包

	pRegionInfo[index].point_num = 5;

	pRegionInfo[index].p_roi[0].x_ = 560;
	pRegionInfo[index].p_roi[0].y_ = 0;

	pRegionInfo[index].p_roi[1].x_ = 1425;
	pRegionInfo[index].p_roi[1].y_ = 0;

	pRegionInfo[index].p_roi[2].x_ = 1400;
	pRegionInfo[index].p_roi[2].y_ = 260;

	pRegionInfo[index].p_roi[3].x_ = 560;
	pRegionInfo[index].p_roi[3].y_ = 290;

	pRegionInfo[index].p_roi[4].x_ = 560;
	pRegionInfo[index].p_roi[4].y_ = 0;

	index = 1;
	pRegionInfo[index].frame_num = 100;
	pRegionInfo[index].alarm_human = false;    //行人
	pRegionInfo[index].alarm_stay = true;     //丢包

	pRegionInfo[index].point_num = 5;

	pRegionInfo[index].p_roi[0].x_ = 550;
	pRegionInfo[index].p_roi[0].y_ = 300;

	pRegionInfo[index].p_roi[1].x_ = 1390;
	pRegionInfo[index].p_roi[1].y_ = 260;

	pRegionInfo[index].p_roi[2].x_ = 1390;
	pRegionInfo[index].p_roi[2].y_ = 620;

	pRegionInfo[index].p_roi[3].x_ = 580;
	pRegionInfo[index].p_roi[3].y_ = 650;

	pRegionInfo[index].p_roi[4].x_ = 550;
	pRegionInfo[index].p_roi[4].y_ = 300;
}

void init_param_video1(region_info* pRegionInfo)
{
	int index = 0;
	pRegionInfo[index].frame_num = 0;
	pRegionInfo[index].alarm_human = false;    //行人
	pRegionInfo[index].alarm_stay = true;     //丢包

	pRegionInfo[index].point_num = 5;

	pRegionInfo[index].p_roi[0].x_ = 280;
	pRegionInfo[index].p_roi[0].y_ = 0;

	pRegionInfo[index].p_roi[1].x_ = 600;
	pRegionInfo[index].p_roi[1].y_ = 0;

	pRegionInfo[index].p_roi[2].x_ = 600;
	pRegionInfo[index].p_roi[2].y_ = 150;

	pRegionInfo[index].p_roi[3].x_ = 315;
	pRegionInfo[index].p_roi[3].y_ = 100;

	pRegionInfo[index].p_roi[4].x_ = 280;
	pRegionInfo[index].p_roi[4].y_ = 0;

	index = 1;
	pRegionInfo[index].frame_num = 100;
	pRegionInfo[index].alarm_human = false;    //行人
	pRegionInfo[index].alarm_stay = true;     //丢包

	pRegionInfo[index].point_num = 5;

	pRegionInfo[index].p_roi[0].x_ = 335;
	pRegionInfo[index].p_roi[0].y_ = 330;

	pRegionInfo[index].p_roi[1].x_ = 425;
	pRegionInfo[index].p_roi[1].y_ = 340;

	pRegionInfo[index].p_roi[2].x_ = 415;
	pRegionInfo[index].p_roi[2].y_ = 445;

	pRegionInfo[index].p_roi[3].x_ = 335;
	pRegionInfo[index].p_roi[3].y_ = 435;

	pRegionInfo[index].p_roi[4].x_ = 335;
	pRegionInfo[index].p_roi[4].y_ = 330;

	index = 2;
	pRegionInfo[index].frame_num = 100;
	pRegionInfo[index].alarm_human = false;    //行人
	pRegionInfo[index].alarm_stay = true;     //丢包

	pRegionInfo[index].point_num = 5;

	pRegionInfo[index].p_roi[0].x_ = 560;
	pRegionInfo[index].p_roi[0].y_ = 355;

	pRegionInfo[index].p_roi[1].x_ = 680;
	pRegionInfo[index].p_roi[1].y_ = 365;

	pRegionInfo[index].p_roi[2].x_ = 660;
	pRegionInfo[index].p_roi[2].y_ = 470;

	pRegionInfo[index].p_roi[3].x_ = 560;
	pRegionInfo[index].p_roi[3].y_ = 470;

	pRegionInfo[index].p_roi[4].x_ = 560;
	pRegionInfo[index].p_roi[4].y_ = 355;

	index = 3;
	pRegionInfo[index].frame_num = 100;
	pRegionInfo[index].alarm_human = false;    //行人
	pRegionInfo[index].alarm_stay = true;     //丢包

	pRegionInfo[index].point_num = 5;

	pRegionInfo[index].p_roi[0].x_ = 320;
	pRegionInfo[index].p_roi[0].y_ = 115;

	pRegionInfo[index].p_roi[1].x_ = 650;
	pRegionInfo[index].p_roi[1].y_ = 180;

	pRegionInfo[index].p_roi[2].x_ = 640;
	pRegionInfo[index].p_roi[2].y_ = 325;

	pRegionInfo[index].p_roi[3].x_ = 310;
	pRegionInfo[index].p_roi[3].y_ = 280;

	pRegionInfo[index].p_roi[4].x_ = 320;
	pRegionInfo[index].p_roi[4].y_ = 115;
}