demo.cpp_test 28.4 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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
#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"

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];
				}
			}
		}
	}
}

void test_opencv2()
{
	CvCapture *capture = cvCreateFileCapture("convert.mp4");
	IplImage *current_back, *buff_back, *abandon, *frame, *current_img, *fore;
	int count, intern;

	frame = cvQueryFrame(capture);
	fore = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1);
	current_back = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1);
	current_img = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1);
	buff_back = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1);
	abandon = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1);

	count = 0;
	intern = count + 20;

	while (1)
	{
		cvCvtColor(frame, current_img, CV_RGB2GRAY);

		if (count == 0)
		{
			//初始化背景模版  
			cvCopy(current_img, current_back);
			cvCopy(current_img, buff_back);
		}

		if (count > 0)
		{
			//计算前景掩膜  
			calc_fore(current_back, buff_back, fore);

			//更新跟踪背景  
			update_currentback(current_img, current_back);

			if (count == intern)
			{
				update_bufferedback(current_back, buff_back, abandon);
				intern = count + 20;
			}
			cvShowImage("current", current_img);
			cvShowImage("current_back", current_back);
			cvShowImage("buff_back", buff_back);
			cvShowImage("abandon detection", fore);
		}

		count++;
		frame = cvQueryFrame(capture);

		if (cvWaitKey(23) >= 0)
		{
			break;
		}
	}
	cvNamedWindow("current", 0);
	cvNamedWindow("buff_back", 0);
	cvNamedWindow("current_back", 0);
	cvNamedWindow("abandon detection", 0);
	cvReleaseCapture(&capture);
}




void test_opencv()
{

	Mat img_src1, img_src2, img_src3;//3帧法需要3帧图片
	Mat img_dst, gray1, gray2, gray3;
	Mat gray_diff1, gray_diff2;//存储2次相减的图片
	Mat gray_diff11, gray_diff12;
	Mat gray_diff21, gray_diff22;
	Mat gray;//用来显示前景的
	bool pause = false;

	gray1 = imread("gray_base.jpg", CV_8U);
	gray2 = imread("gray2.jpg", CV_8U);
	subtract(gray1, gray2, gray_diff1);

	cv::imshow("gray1", gray1);
	cv::waitKey(0);

	cv::imshow("gray2", gray2);
	cv::waitKey(0);

	cv::imshow("diff", gray_diff1);
	cv::waitKey(0);


	VideoCapture vido_file("test/20201211_152752.dav");//在这里改相应的文件名
	//namedWindow("foreground", 0);

	Mat img_base, gray_base;

	
	{
		vido_file >> img_base;
		cvtColor(img_base, gray_base, CV_BGR2GRAY);
		cv::imshow("base", gray_base);
		cv::imwrite("gray_base.jpg", gray_base);
		cv::waitKey(0);
	}

	int frame_count = 0;
	for (;;)
	{
		if (!false)
		{
			vido_file >> img_src1;
			cvtColor(img_src1, gray1, CV_BGR2GRAY);
	
			waitKey(33);
			vido_file >> img_src2;
			cvtColor(img_src2, gray2, CV_BGR2GRAY);

			cv::imshow("gray2", gray2);
			cv::imwrite("gray2.jpg", gray2);
			cv::waitKey(0);

			Mat img_src1_small(img_src2.rows / 2, img_src2.cols / 2, img_src2.type());

			resize(img_src2, img_src1_small, img_src1_small.size(), 0, 0, INTER_LINEAR);

			imshow("video_src", img_src1_small);//

		/*	waitKey(33);
			vido_file >> img_src3;
			cvtColor(img_src3, gray3, CV_BGR2GRAY);*/

			Sobel(gray_base, gray_base, CV_8U, 1, 0, 3, 0.4, 128);

			Sobel(gray1, gray1, CV_8U, 1, 0, 3, 0.4, 128);
			Sobel(gray2, gray2, CV_8U, 1, 0, 3, 0.4, 128);
			Sobel(gray3, gray3, CV_8U, 1, 0, 3, 0.4, 128);

			subtract(gray_base, gray1, gray_diff11);//第二帧减第一帧
			subtract(gray1, gray_base, gray_diff12);
			add(gray_diff11, gray_diff12, gray_diff1);
			subtract(gray_base, gray2, gray_diff21);//第三帧减第二帧
			subtract(gray2, gray_base, gray_diff22);
			add(gray_diff21, gray_diff22, gray_diff2);

			cv::imshow("gray_diff1", gray_diff1);
			//cv::waitKey(0);

			cv::imshow("gray_diff2", gray_diff2);
			//cv::waitKey(0);


			for (int i = 0; i<gray_diff1.rows; i++)
				for (int j = 0; j<gray_diff1.cols; j++)
				{
					if (abs(gray_diff1.at<unsigned char>(i, j)) >= threshold_diff1)//这里模板参数一定要用unsigned char,否则就一直报错
						gray_diff1.at<unsigned char>(i, j) = 255;            //第一次相减阈值处理
					else gray_diff1.at<unsigned char>(i, j) = 0;

					if (abs(gray_diff2.at<unsigned char>(i, j)) >= threshold_diff2)//第二次相减阈值处理
						gray_diff2.at<unsigned char>(i, j) = 255;
					else gray_diff2.at<unsigned char>(i, j) = 0;
				}

			bitwise_and(gray_diff1, gray_diff2, gray);
			
			dilate(gray, gray, Mat()); erode(gray, gray, Mat());
		
			Mat gray_small(gray.rows/2, gray.cols/2, gray.type());

			resize(gray, gray_small, gray_small.size(), 0, 0, INTER_LINEAR);

			imshow("foreground", gray_small);
		}

		cvWaitKey(0);
		/*if (cvWaitKey(0) >= 0)
			break;*/
	}
}

void imageSubtract(Mat &image1, Mat &image2);


void test_video()
{
	VideoCapture vido_file("convert.mp4");//在这里改相应的文件名
	
	Mat img_base, current_img;
	//vido_file >> img_base;
	img_base = imread("base_rgb.png");
	for (;;)
	{
		vido_file >> current_img;

		imageSubtract(img_base, current_img);
	}
	
}

const unsigned char FORE_GROUD = 255;
int thresh = 10;

#include "ConExtraction.h"
void test_canny()
{
	CvCapture* video1 = cvCaptureFromFile("test/20201211_152752.dav");

	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);
	// 用opencv函数读取视频的一


	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;

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

	cv::Mat bk_image = cv::imread("images2/0.jpg");

	while (src)
	{
		src = cvQueryFrame(video1);

		Mat matimg;
		matimg = cvarrToMat(src);

		Mat DstPic, edge, grayImage;
		DstPic.create(matimg.size(), matimg.type());
		cvtColor(matimg, grayImage, COLOR_BGR2GRAY);
		blur(grayImage, edge, Size(3, 3));
		Canny(edge, edge, 35, 105, 3);

		cv::Mat small_edge;
		cv::resize(edge, small_edge, cv::Size(edge.cols / 2, edge.rows / 2));
		/*imshow("边缘提取效果", edge);
		cv::waitKey(1);*/

		Mat bk_DstPic, bk_edge, bk_grayImage;
		bk_DstPic.create(matimg.size(), matimg.type());
		cvtColor(bk_image, bk_grayImage, COLOR_BGR2GRAY);
		blur(bk_grayImage, bk_edge, Size(3, 3));
		Canny(bk_edge, bk_edge, 35, 105, 3);
	
		/*imshow("bk_edge", bk_edge);
		cv::waitKey(1);*/

		Mat difframe2, tempframe;

		absdiff(bk_edge, edge, difframe2);//做差求绝对值            2-3
		threshold(difframe2, tempframe, 20, 255.0, CV_THRESH_BINARY);
		
		dilate(tempframe, tempframe, Mat());//膨胀  
		Mat erode_element = getStructuringElement(MORPH_RECT, Size(2, 2));
		erode(tempframe, tempframe, erode_element);//腐蚀
		

		imshow("tempframe", tempframe);
		cv::waitKey(1);

		/*CConExtraction *pConExtraction = new CConExtraction();
		vector<CContour> m_Contours = pConExtraction->ExtractContours(tempframe.data, tempframe.cols, tempframe.rows, tempframe.step);

		{
			for (int i = 0; i < m_Contours.size(); i++)
			{
				rectangle(matimg,
					CvPoint(m_Contours[i].left, m_Contours[i].top),
					CvPoint(m_Contours[i].right, m_Contours[i].bottom), CV_RGB(0, 255, 0), 2);
			}

			cv::Mat small_mat;
			cv::resize(matimg, small_mat, cv::Size(matimg.cols / 2, matimg.rows / 2));
			cv::imshow("res", matimg);
			cv::waitKey(1);
		}
*/


		continue;

		/*CConExtraction *pConExtraction = new CConExtraction();
		vector<CContour> m_Contours = pConExtraction->ExtractContours(edge.data, edge.cols, edge.rows, edge.step);

		{
			for (int i = 0; i < m_Contours.size(); i++)
			{
				rectangle(matimg,
					CvPoint(m_Contours[i].left, m_Contours[i].top),
					CvPoint(m_Contours[i].right, m_Contours[i].bottom), CV_RGB(0, 255, 0), 2);
			}

			cv::Mat small_mat;
			cv::resize(matimg, small_mat, cv::Size(matimg.cols / 2, matimg.rows / 2));
			cv::imshow("res", small_mat);
			cv::waitKey(1);
		}*/

		continue;
	}
}


void test_sub1228()
{
	int nFrmNum = 0;
	cv::Mat img_raw, img_gray, img_gray_bk, img_diff, img_diffLast, img_foreg, image_show;
	VideoCapture m_cam;
	string videoStr = "20201209_20201209112553_20201209112713_032528.mp4";
	if (m_cam.open(videoStr))
		cout << "Video open success." << endl;

	cv::Mat img_mask = imread("bk_mask1.bmp", -1);
	img_gray_bk = imread("img_backg.bmp", -1);
	
	//cvtColor(img_backg, img_gray_bk, COLOR_BGR2GRAY);
	
	CConExtraction *pConExtraction = new CConExtraction();
	cv::Mat img_disp(img_mask.rows, img_mask.cols * 2, CV_8UC1, CvScalar{ 0 });

//	VideoWriter m_video("result.mp4", VideoWriter::fourcc('H', '2', '6', '4'), 25, Size(img_mask.cols * 2, img_mask.rows), false);
	while (1)
	{
		nFrmNum++;
		cout << "nFrmNum: " << nFrmNum << endl;
		m_cam >> img_raw;
		if (img_raw.data == NULL)
			break;
		cvtColor(img_raw, img_gray, COLOR_BGR2GRAY);

		if (nFrmNum <= 2)
		{
			absdiff(img_gray, img_gray_bk, img_diff);
			//if (nFrmNum == 1)
			//	img_backg = img_gray.clone();
			//else
			//{
			//	absdiff(img_gray, img_backg, img_diff);
			//	//img_backg = img_gray.clone();
			//}
		}
		else
		{
			img_diffLast = img_diff.clone();
			absdiff(img_gray, img_gray_bk, img_diff);
			bitwise_and(img_diffLast, img_diff, img_foreg);
			threshold(img_foreg, img_foreg, 45, 255, 0);
			//img_backg = img_gray.clone();

			dilate(img_foreg, img_foreg, getStructuringElement(MORPH_RECT, Size(7, 7)));
			/*erode(img_foreg, img_foreg, getStructuringElement(MORPH_RECT, Size(3, 3)));
			dilate(img_foreg, img_foreg, getStructuringElement(MORPH_RECT, Size(3, 3)));*/

			printf("size: %d %d %d %d\n", img_foreg.cols, img_foreg.rows, img_mask.cols, img_mask.rows);
			img_foreg = img_foreg.mul(img_mask);

			//ExtractContours
			vector<CContour> forecontours_all;
			vector<CContour> forecontours_max;
			forecontours_all = pConExtraction->ExtractContours(img_foreg.data, img_foreg.cols, img_foreg.rows, img_foreg.cols);
			forecontours_max = pConExtraction->AeraMaxX(forecontours_all, 10);

			int ex_width = 3;
			for (int i = 0; i < forecontours_max.size(); i++)
			{
				int rLeft = forecontours_max[i].left - ex_width;
				if (rLeft < 0)
					rLeft = 0;
				int rTop = forecontours_max[i].top - ex_width;
				if (rTop < 0)
					rTop = 0;
				int rWidth = forecontours_max[i].width + 2 * ex_width;
				if (rLeft + rWidth > img_foreg.cols)
					rWidth = img_foreg.cols - rLeft;
				int rHeight = forecontours_max[i].height + 2 * ex_width;
				if (rTop + rHeight > img_foreg.rows)
					rHeight = img_foreg.rows - rTop;
				rectangle(img_gray, Rect(rLeft, rTop, rWidth, rHeight), Scalar(0, 255, 255), 2);
				rectangle(img_foreg, Rect(rLeft, rTop, rWidth, rHeight), Scalar(255), 1);
			}
			img_gray.copyTo(img_disp(Rect(0, 0, img_gray.cols, img_gray.rows)));
			img_foreg.copyTo(img_disp(Rect(img_gray.cols, 0, img_gray.cols, img_gray.rows)));
			//m_video.write(img_disp);
			
			cv::resize(img_disp, image_show, cv::Size(img_disp.cols/2, img_disp.rows/2));
			cv::imshow("img_raw", image_show);
			//imshow("img_foreg", img_foreg);
			
			if (cvWaitKey(1) == 'q')
				break;
		}

	}
	m_cam.release();
	//m_video.release();
}

void cal_mask()
{
	Mat img_raw(1080, 1920, CV_8UC1, Scalar(0));
	Mat img_mask(1080, 1920, CV_8UC1, Scalar(0));

	/*Point rp[3][5];
	rp[0][0] = Point(335, 330);
	rp[0][1] = Point(425, 340);
	rp[0][2] = Point(415, 445);
	rp[0][3] = Point(335, 435);
	rp[0][4] = Point(335, 330);

	rp[1][0] = Point(560, 355);
	rp[1][1] = Point(680, 365);
	rp[1][2] = Point(660, 470);
	rp[1][3] = Point(560, 470);
	rp[1][4] = Point(560, 355);

	rp[2][0] = Point(320, 115);
	rp[2][1] = Point(650, 180);
	rp[2][2] = Point(640, 325);
	rp[2][3] = Point(310, 280);
	rp[2][4] = Point(320, 115);*/

	Point rp[1][5];
	rp[0][0] = Point(550, 300);
	rp[0][1] = Point(1390, 260);
	rp[0][2] = Point(1390, 620);
	rp[0][3] = Point(580, 650);
	rp[0][4] = Point(550, 300);

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

	/*rp[1][0] = Point(560, 355);
	rp[1][1] = Point(680, 365);
	rp[1][2] = Point(660, 470);
	rp[1][3] = Point(560, 470);
	rp[1][4] = Point(560, 355);

	rp[2][0] = Point(320, 115);
	rp[2][1] = Point(650, 180);
	rp[2][2] = Point(640, 325);
	rp[2][3] = Point(310, 280);
	rp[2][4] = Point(320, 115);*/


	const Point* ppt[1] = { rp[0] };
	int npt[] = { 5 };

	fillPoly(img_raw, ppt, npt, 1, Scalar(255));
	fillPoly(img_mask, ppt, npt, 1, Scalar(1));

	imshow("img_raw", img_raw);
	cv::waitKey(0);
	imwrite("bk_mask.bmp", img_mask);
}


void cal_mask1()
{
	Mat img_raw(576, 704, CV_8UC1, Scalar(0));
	Mat img_mask(576, 704, CV_8UC1, Scalar(0));

	Point rp[3][5];
	rp[0][0] = Point(335, 330);
	rp[0][1] = Point(425, 340);
	rp[0][2] = Point(415, 445);
	rp[0][3] = Point(335, 435);
	rp[0][4] = Point(335, 330);

	rp[1][0] = Point(560, 355);
	rp[1][1] = Point(680, 365);
	rp[1][2] = Point(660, 470);
	rp[1][3] = Point(560, 470);
	rp[1][4] = Point(560, 355);

	rp[2][0] = Point(320, 115);
	rp[2][1] = Point(650, 180);
	rp[2][2] = Point(640, 325);
	rp[2][3] = Point(310, 280);
	rp[2][4] = Point(320, 115);

	const Point* ppt[3] = { rp[0],  rp[1],  rp[2]};
	int npt[] = { 5, 5, 5};

	fillPoly(img_raw, ppt, npt, 3, Scalar(255));
	fillPoly(img_mask, ppt, npt, 3, Scalar(1));

	imshow("img_raw", img_raw);
	cv::waitKey(0);
	imwrite("bk_mask1.bmp", img_mask);
}


int main()
{
	//cal_mask1();
	////test_video();

	///*cv::Mat img1 = cv::imread("base_rgb.png");
	//cv::Mat img2 = cv::imread("test3.png");
	//imageSubtract(img1, img2);*/
	//
	//test_sub1228();

	imag_ana_3channels();
	//test_opencv2();

	//test_canny();

	system("pause");
}


#include <cv.h>



#include <highgui.h>


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


void imageSubtract(Mat &image1, Mat &image2)
{
	if ((image1.rows != image2.rows) || (image1.cols != image2.cols))
	{
		if (image1.rows > image2.rows)
		{
			resize(image1, image1, image2.size(), 0, 0, INTER_LINEAR);
		}
		else if (image1.rows < image2.rows)
		{
			resize(image2, image2, image1.size(), 0, 0, INTER_LINEAR);
		}
	}

	Mat image1_gary, image2_gary;
	if (image1.channels() != 1)
	{
		cvtColor(image1, image1_gary, COLOR_BGR2GRAY);
	}
	if (image2.channels() != 1)
	{
		cvtColor(image2, image2_gary, COLOR_BGR2GRAY);
	}

	Mat frameDifference, absFrameDifferece;
	Mat previousGrayFrame = image2_gary.clone();
	//图1减图2
	subtract(image1_gary, image2_gary, frameDifference, Mat(), CV_16SC1);

	//取绝对值
	absFrameDifferece = abs(frameDifference);

	//位深的改变
	absFrameDifferece.convertTo(absFrameDifferece, CV_8UC1, 1, 0);
	imshow("absFrameDifferece", absFrameDifferece);
	Mat segmentation;

	//阈值处理(这一步很关键,要调好二值化的值)
	threshold(absFrameDifferece, segmentation, 75, 255, THRESH_BINARY);

	//中值滤波
	medianBlur(segmentation, segmentation, 3);

	//形态学处理(开闭运算)
	//形态学处理用到的算子
	Mat morphologyKernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	morphologyEx(segmentation, segmentation, MORPH_CLOSE, morphologyKernel, Point(-1, -1), 2, BORDER_REPLICATE);

	//显示二值化图片
	imshow("segmentation", segmentation);

	//找边界
	
	CvMemStorage* m_storage = cvCreateMemStorage(0);
	CvSeq *pContour = NULL;
	CvSeq *pConInner = NULL;
	IplImage *ip_segmentation = (IplImage *)&IplImage(segmentation);
	cvFindContours(ip_segmentation, m_storage, &pContour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
	//释放内存
	IplImage *show_image = (IplImage *)&IplImage(image2);
	// 外轮廓循环   
	int wai = 0;
	int nei = 0;
	for (; pContour != NULL; pContour = pContour->h_next)
	{
		wai++;
		//// 内轮廓循环   
		//for (pConInner = pContour->v_next; pConInner != NULL; pConInner = pConInner->h_next)
		//{
		//	nei++;
		//	// 内轮廓面积   
		//	dConArea = fabs(cvContourArea(pConInner, CV_WHOLE_SEQ));
		//	printf("%f\n", dConArea);
		//}
		CvRect rect = cvBoundingRect(pContour, 0);
		cvRectangle(show_image, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height), CV_RGB(255, 255, 255), 1, 8, 0);
	}


	cvReleaseMemStorage(&m_storage);

	
	cvShowImage("img", show_image);
	//imshow("效果图", image2);
	cv::waitKey(1);
}





//vector< vector<Point> > contours;
//vector<Vec4i> hierarchy;
//
///*const int CON_NUM = 100;
//const int CON_MAX_NUM = 1000;
//contours.resize(CON_MAX_NUM);
//for (int ii = 0; ii < CON_MAX_NUM; ii++)
//contours[ii].resize(CON_MAX_NUM);*/
//
//printf("before findContours\n");
//findContours(segmentation, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));//CV_RETR_TREE
//printf("after findContours\n");
//
//printf("contours size : %d\n", contours.size());
//
//for (int i = 0; i < contours.size(); i++)
//{
//	printf("contours i=%d, size=%d\n", i, contours[i].size());
//
//	vector<Point>().swap(contours[i]);
//	printf("finish release: %d\n", i);
//	contours[i].clear();
//	printf("finish release: %d\n", i);
//}
//
//printf("begin release\n");
//vector< vector<Point> >().swap(contours);
//contours.clear();
//
//printf("release\n");
///*vector< vector<Point> > contours_poly(contours.size());
//vector<Rect> boundRect;
//boundRect.clear();*/
//
////for (int index = 0; index < contours.size(); index++)
////{
////	approxPolyDP(Mat(contours[index]), contours_poly[index], 3, true);
////	Rect rect = boundingRect(Mat(contours_poly[index]));
////	//rectangle(image2, rect, Scalar(0, 255, 0), 2);
////}