svibe.cpp 96.7 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 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253
// svibe.cpp : 定义控制台应用程序的入口点。
#include <stdio.h>
#include <stddef.h>
#include "svibe.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
#include "MSRegionSurveilance.h" 

#include "gaussFilte.h"
#include <fstream>
#include <algorithm>
#include "help.h"
//#include "vpt.h"
/************************************************************************/
/* 函数: get_ArrowDir()		by zl										*/
/* 作用: 获取方向和用于绘制箭头的两个端点;								*/
/* 参数:  pROI0&pROI1:拌线两个端点;									*/
/*		 dir:确定单向警报方向的坐标点;								*/
/* 返回: 1:正方向;0:负方向												*/
/************************************************************************/
int RegionSurveillance::get_ArrowDir(CMPoint pROI0, CMPoint pROI1, CMPoint dir)
{

	float distance = (float)abs(pROI0.y - pROI1.y);
	//获取方向
	float k1, k2;  //k1为原线段的斜率

	if (pROI0.y > pROI1.y)	//在拌线靠近图像下方的端点处画箭头
	{
		pStart.x = pROI0.x;					
		pStart.y = pROI0.y;
	}
	else
	{
		pStart.x = pROI1.x;					
		pStart.y = pROI1.y;
	}

	if (pROI0.x != pROI1.x)	//若不为一根垂直线
	{
		k1 = (float)(pROI0.y - pROI1.y) / (float)(pROI0.x - pROI1.x);
		if (k1 != 0)		//若不为一根平行线
		{
			k2 = -1 / k1;
			
			
			float b = pROI1.y - k1 * pROI1.x;//y = kx + b 坐标系一图像左上顶点为原点,向右为x正方形 向下为y正方向
			if ((k1 < 0 && (float)((float)dir.y - k1 * (float)dir.x - b) < 0)||(k1 > 0 && (float)((float)dir.y - k1 * (float)dir.x - b) > 0))
			{
				if (distance < 30) //当两点y值差异较小时,垂线比较陡 若减去20 可能导致箭头太长
				{
					pEnd.x = pStart.x - 2; 
				}
				else
					pEnd.x = pStart.x - 20; 
			}
			else
			{
				if (distance < 30)
				{
					pEnd.x = pStart.x + 2;
				}
				else
					pEnd.x = pStart.x + 20; 
			}
			pEnd.y = (int)((pEnd.x - pStart.x) * k2 + pStart.y);
		}
		else				//若为水平线
		{
			pEnd.x = pStart.x;
			if (dir.y > pStart.y)
				pEnd.y = pStart.y + 20;
			else
				pEnd.y = pStart.y - 20;

		}

	}
	else					//若为垂直线
	{
		pEnd.y = pStart.y;
		if (dir.x > pStart.x)
			pEnd.x = pStart.x + 20;
		else
			pEnd.x = pStart.x - 20;
	}
	//防止越界
	pEnd.x = pEnd.x >= 0 ? pEnd.x : 0;
	pEnd.x = pEnd.x <= GlobelWidth ? pEnd.x : GlobelWidth;
	pEnd.y = pEnd.y >= 0 ? pEnd.y : 0;
	pEnd.y = pEnd.y <= GlobelHeight ? pEnd.y : GlobelHeight;

	if (pROI0.x == pROI1.x)	//若为垂直线
	{
		if (pEnd.x > pStart.x)
			return 1;
		else
			return 0;
	}
	else if (k1 <= 0)	//若不为垂直线且斜率小于等于0(含水平线情况)
	{
		if ((pStart.x != pEnd.x && pEnd.x > pStart.x) || (pStart.x == pEnd.x && pEnd.y > pStart.y))
			return 0;
		else
			return 1;
	}
	else
	{
		if ((pStart.x != pEnd.x && pEnd.x > pStart.x) || (pStart.x == pEnd.x && pEnd.y > pStart.y))
			return 1;
		else
			return 0;
	}
}

/******************************************************************************
* Function:        RSinit
* Description:     MaskImgData内存非配及VIBE 模型初始化
* Calls:           VibeModelInitC3
* Called By:       main
* Input:           nWidth          图像宽度
                   nHeight         图像高度
				   frameImgData    图像RGB
* Output:          vbM             VIBE 模型
* Return:          0 输入通道参数不合理       1 正常
*******************************************************************************/

int RegionSurveillance::RSinit(int nWidth, int nHeight, int widthstep, unsigned char* frameImgData, 
	int nChannel, int nChannel_deal, bool nfiltFlag, int minArea, int maxArea)
{

	if(nWidth < 600 || nHeight < 300)		//如果尺寸过小 修改vibe参数
		VBPARAM = new VIBEPARAM(35, 18);
	else
		VBPARAM = new VIBEPARAM(20, 35);
	
	//checkMID:软件许可检测 by zl
    if (/*checkMIDkeySet() == -1 */ checkTime() == -1 || (nChannel != 1 && nChannel != 3) || (nChannel_deal != 1 && nChannel_deal != 3) || (nChannel_deal > nChannel))
    {
		return 0;
    }

	channel_num = nChannel;
	dwGlobalID = 100;
	GlobelWidth = nWidth;
	GlobelHeight = nHeight;
	GlobelWidthstep = widthstep;
	gfiltFlag = nfiltFlag;
	OBJECT_AREA_MIN = minArea;		//默认最大过滤面积为1,000,000	by zl 20160304
	OBJECT_AREA_MAX = maxArea;		//默认最小过滤面积为300	by zl 20160304
	/*
	int widthstep = nWidth * 3;
	if (widthstep % 4)
	{
		widthstep = (widthstep / 4 + 1) * 4;
	}
	*/

	MaskImgData  = (unsigned char*) malloc(sizeof(unsigned char*) * GlobelWidth * GlobelHeight);
	memset(MaskImgData, 0, sizeof(unsigned char*));

	if (!vbM)
	{
		// 初始化模型
		if (nChannel == 3)
		{
			if (nChannel_deal == 3)
			{
				// 高斯模糊
				if (nfiltFlag == 1)
				{
					unsigned char* inputData = NULL;
					inputData = new unsigned char[GlobelWidth * GlobelHeight];
					
					int i = 0;
					while (i < nChannel)
					{
						for (int nheight = 0; nheight < GlobelHeight; nheight++)
						{
							for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
							{
								inputData[nheight * GlobelWidth + nwidth] = frameImgData[nheight * widthstep + nwidth * nChannel + i];
							}
						}

						gaussianFilter(inputData, GlobelWidth, GlobelHeight, gWinSize);

						for (int nheight = 0; nheight < GlobelHeight; nheight++)
						{
							for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
							{
								frameImgData[nheight * widthstep + nwidth * nChannel + i] = inputData[nheight * GlobelWidth + nwidth];
							}
						}
						i++;
					}

					delete[] inputData;
				}

				vbM = VibeModelInitC3(frameImgData);
			} 
			else
			{
				greyImgData  = (unsigned char*) malloc(sizeof(unsigned char*) * GlobelWidth * nHeight);
				memset(greyImgData, 0, sizeof(unsigned char*));
				// 将RGB图像转换为灰度图像
				VibeRGBToGray(frameImgData, greyImgData);

				// 高斯模糊
				if (nfiltFlag == 1)
				{
					unsigned char* inputData = NULL;
					inputData = new unsigned char[GlobelWidth * GlobelHeight];
					
					for (int nheight = 0; nheight < GlobelHeight; nheight++)
					{
						for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
						{
							inputData[nheight * GlobelWidth + nwidth] = greyImgData[nheight * GlobelWidth + nwidth];
						}
					}
					gaussianFilter(inputData, GlobelWidth, GlobelHeight, gWinSize);

					for (int nheight = 0; nheight < GlobelHeight; nheight++)
					{
						for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
						{
							greyImgData[nheight * GlobelWidth + nwidth] = inputData[nheight * GlobelWidth + nwidth];
						}
					}

					delete[] inputData;
				}
				
				vbM = VibeModelInitC1(greyImgData);
			}
		} 
		else
		{
			// 高斯模糊
			if (nfiltFlag == 1)
			{
				unsigned char* inputData = NULL;
				inputData = new unsigned char[GlobelWidth * GlobelHeight];
				
				for (int nheight = 0; nheight < GlobelHeight; nheight++)
				{
					for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
					{
						inputData[nheight * GlobelWidth + nwidth] = frameImgData[nheight * widthstep + nwidth];
					}
				}

				gaussianFilter(inputData, GlobelWidth, GlobelHeight, gWinSize);

				for (int nheight = 0; nheight < GlobelHeight; nheight++)
				{
					for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
					{
						frameImgData[nheight * widthstep + nwidth] = inputData[nheight * GlobelWidth + nwidth];
					}
				}

				delete[] inputData;
			}

			vbM = VibeModelInitC1(frameImgData);
		}	
	}
	
	pConExtraction = new CConExtraction();

	 left_flag = 1; // 逗留标志 0 遗留 1移除

	 maskImg = new unsigned char[nWidth*nHeight];		//膨胀和腐蚀 加速用 
	 memset(maskImg, 0, sizeof(maskImg[0])*nWidth*nHeight);		//膨胀和腐蚀 加速用 

	 return 1;
}


VibeModel_t * RegionSurveillance::VibeModelInitC1(uint8_t *image_data)
{
    int32_t x, y, xNG, yNG, index;

	RandInit();
	RandNeighborInit(GlobelWidth, GlobelHeight);

    vbM = (VibeModel_t *) malloc(sizeof(VibeModel_t)); 
    vbM->width		 = GlobelWidth;
    vbM->height		 = GlobelHeight;
    vbM->nChannel	 = 1;
    vbM->nSample	 = VBPARAM->P_SAMPLES;
    vbM->threshold	 = VBPARAM->P_THRESHOLD;
    vbM->nMatch		 = VBPARAM->P_MATCH;
    vbM->upSpeed	 = VBPARAM->P_UPSPEED;
	vbM->widthstep   = GlobelWidthstep;
	vbM->bg_samples  = (uint8_t *)malloc(vbM->width * vbM->height * vbM->nSample);

	for (y = 0; y < GlobelHeight; y++) 
	{
		for (x = 0; x < GlobelWidth; x++) 
		{
			for (index = 0; index < vbM->nSample; index++) 
			{		
				GetRandNeighborXY(x, y, &xNG, &yNG);
				vbM->bg_samples[y * vbM->width * vbM->nSample + x * vbM->nSample + index] = image_data[yNG * vbM->width + xNG];
			}
		}
	}

	g_frameCount++;
    
    return vbM;
}


/******************************************************************************
* Function:        VibeModelInitC3
* Description:     VIBE模型初始化
* Calls:           RandInit
                   RandNeighborInit
				   GetRandNeighborXY
* Called By:       RSinit
* Input:           image_data     图像RGB
* Output:          g_XNeighborTable             22100012
                   g_YNeighborTable             10001222
				   g_randTable	                65536个随机数
				   g_XTable                     0 0 1 2 3 ... 宽度-1 宽度-1
				   g_YTable						0 0 1 2 3 ... 高度-1 高度-1
* Return:          vbM
*******************************************************************************/
VibeModel_t* RegionSurveillance::VibeModelInitC3(uint8_t *image_data)
{
    int32_t x, y, xNG, yNG, index;
    int32_t image_offset = 0, sample_offset = 0;

	RandInit();
	RandNeighborInit(GlobelWidth, GlobelHeight);

    vbM = (VibeModel_t *) malloc(sizeof(VibeModel_t)); 
    vbM->width		= GlobelWidth;
    vbM->height	    = GlobelHeight;
    vbM->nChannel	= 3;
	vbM->widthstep  = GlobelWidthstep;
    vbM->nSample	= VBPARAM->P_SAMPLES;
    vbM->threshold	= VBPARAM->P_THRESHOLD;
    vbM->nMatch		= VBPARAM->P_MATCH;
    vbM->upSpeed	= VBPARAM->P_UPSPEED;
	vbM->bg_samples = (uint8_t *) malloc(vbM->width * vbM->height * 3 * vbM->nSample);

	for (y = 0; y < GlobelHeight; y++) 
	{
		for (x = 0; x < GlobelWidth; x++) 
		{
			for (index = 0; index < vbM->nSample; index++) 
			{
				GetRandNeighborXY(x, y, &xNG, &yNG);
				sample_offset = ((y * vbM->width * vbM->nSample + x * vbM->nSample + index) << 1) + (y * vbM->width * vbM->nSample + x * vbM->nSample + index);
				image_offset  = ((yNG * vbM->width + xNG) << 1) + (yNG * vbM->width + xNG);

				vbM->bg_samples[sample_offset]     = image_data[image_offset];
				vbM->bg_samples[sample_offset + 1] = image_data[image_offset + 1];
				vbM->bg_samples[sample_offset + 2] = image_data[image_offset + 2];
			}
		}
	}
    
	g_frameCount++;

    return vbM;
}

void RegionSurveillance::VibeModelUpdateC1(VibeModel_t *model, uint8_t *image, uint8_t *output)
{
	int32_t x, y, xNG, yNG, count, index, dist, sampleOffset, imageOffset;
	uint32_t rand;

	for (y = 0; y < model->height; y++) 
	{
		for (x = 0; x < model->width; x++) 
		{
			sampleOffset = y * model->width * model->nSample + x * model->nSample;
			imageOffset  = y * model->width + x;

			// compare pixel to background model
			count = 0; 
			index = 0;
			while ((count < model->nMatch) && (index < model->nSample)) 
			{
				//Euclidean distance computation
				dist = abs(image[imageOffset] - model->bg_samples[sampleOffset + index]);
				if (dist < model->threshold) 
				{
					count++;
				}
				index++;
			}

			// classify pixel and update model
			// foreground
			if (count < model->nMatch)
			{
				output[imageOffset] = VBPARAM->DEFAULT_COLOR_FOREGROUND;

				if (g_frameCount < model->nSample)
				{
					rand = GetRand() % (model->nSample);
					model->bg_samples[sampleOffset + rand] = image[imageOffset];  
					GetRandNeighborXY(x, y, &xNG, &yNG);
					rand = GetRand() % (model->nSample);
					model->bg_samples[yNG * model->width * model->nSample + xNG * model->nSample + rand] = image[imageOffset];
				}
			}
			else
			{
				output[imageOffset] = VBPARAM->DEFAULT_COLOR_BACKGROUND;

				if (g_frameCount < model->nSample)
				{
					rand = GetRand() % (model->nSample);
					model->bg_samples[sampleOffset + rand] = image[imageOffset];  
					GetRandNeighborXY(x, y, &xNG, &yNG);
					rand = GetRand() % (model->nSample);
					model->bg_samples[yNG * model->width * model->nSample + xNG * model->nSample + rand] = image[imageOffset];
				}
				else
				{
					//update current pixel model
					rand = GetRand() % (model->upSpeed);
					if (rand == 0 && left_flag == 1) 
					{
						rand = GetRand() % (model->nSample);
						model->bg_samples[sampleOffset + rand] = image[imageOffset];  
					}
					//update neighboring pixel model
					rand = GetRand() % (model->upSpeed);
					if (rand == 0 && left_flag == 1) 
					{
						GetRandNeighborXY(x, y, &xNG, &yNG);
						rand = GetRand() % (model->nSample);
						model->bg_samples[yNG * model->width * model->nSample + xNG * model->nSample + rand] = image[imageOffset];
					}
				}
			}
		}// end of for x loop
	}// end of for y loop
	
	
	VibeModelDilation2(output, maskImg, model->width, model->height);// 膨胀
	VibeModelErosion2(maskImg, output, model->width, model->height);// 腐蚀
	//未加速版本
	//VibeModelDilation(output, model->width, model->height);// 膨胀
	//VibeModelErosion(output, model->width, model->height);// 腐蚀
	

	g_frameCount++;
}


/******************************************************************************
* Function:        VibeModelUpdateC3
* Description:     VIBE模型更新
* Calls:           GetRand
                   GetRandNeighborXY
				   VibeModelDilation
				   VibeModelErosion
* Called By:       RSDetect
* Input:           model         VBM 模型
                   image         图像RGB
* Output:          output        前景是255 背景是0
                   model         VBM 模型
*******************************************************************************/
void RegionSurveillance::VibeModelUpdateC3(VibeModel_t *model, uint8_t *image, uint8_t *output)
{
	int32_t x, y, xNG, yNG;
	int32_t count, index;
	double dist;
	int32_t sampleOffset, imageOffset;
	uint32_t rand;
	int tmpValue = 0;
	int countArry = 0;

	for (y = 0; y < model->height; y++) 
	{
		for (x = 0; x < model->width; x++) 
		{
			// 当前点对应的模板中的索引
			sampleOffset = ((y * model-> width * model->nSample + x * model->nSample) << 1) + (y * model-> width * model->nSample + x * model->nSample);

			// 当前点对应的RGB中的索引
			imageOffset  = ((y * model->width + x) << 1) + (y * model->width + x);

			// 			if (imageOffset>=(model->width * model->height))
			// 				int sDEBUG=1;

			//compare pixel to background model
			count = 0; 
			index = 0;

			// 如果当前满足阈值的点数小于规定匹配点数 且 当前评估的点数小于20个点
			while ((count < model->nMatch) && (index < model->nSample)) 
			{
				//Euclidean distance computation
				tmpValue = sampleOffset + ((index << 1) + index);
				dist = (double)((image[imageOffset] - model->bg_samples[tmpValue ])*(image[imageOffset] - model->bg_samples[tmpValue])) +
					   (double)((image[imageOffset + 1] - model->bg_samples[tmpValue + 1])*(image[imageOffset + 1] - model->bg_samples[tmpValue+ 1])) +
					   (double)((image[imageOffset + 2] - model->bg_samples[tmpValue + 2])*(image[imageOffset + 2] - model->bg_samples[tmpValue + 2]));
				// 三通道计算距离之和
				//dist = abs(image[imageOffset] - model->bg_samples[tmpValue]) + abs(image[imageOffset + 1] - model->bg_samples[tmpValue + 1])
					 //+ abs(image[imageOffset + 2] - model->bg_samples[tmpValue + 2]);
				// 满足阈值条件的点
				if (dist < model->threshold * model->threshold * 3)
				{
					count++;
				}
		
				index++;
			}

			// 如果当前点为前景
			if (count < model->nMatch)
			{
				output[y * model->width + x] = VBPARAM->DEFAULT_COLOR_FOREGROUND;

				// 前20帧增加模板信息
				if (g_frameCount < model->nSample)
				{
					// 随机更新当前点对应的模板中的某一背景
					rand = GetRand() % (model->nSample);
					tmpValue = sampleOffset + ((rand << 1) +rand );
					model->bg_samples[tmpValue] = image[imageOffset];  
					model->bg_samples[tmpValue + 1] = image[imageOffset + 1];  
					model->bg_samples[tmpValue + 2] = image[imageOffset + 2]; 

					// 同时随机对当前点的某一个相邻点对应的模板中的随机一背景进行更新
					GetRandNeighborXY(x, y, &xNG, &yNG);
					rand = GetRand() % (model->nSample);
					tmpValue = ((yNG * model->width * model->nSample + xNG * model->nSample + rand) << 1)+ (yNG * model->width * model->nSample + xNG * model->nSample + rand);
					model->bg_samples[tmpValue] = image[imageOffset];
					model->bg_samples[tmpValue + 1] = image[imageOffset + 1];
					model->bg_samples[tmpValue + 2] = image[imageOffset + 2];
				}
			}

			// 如果当前点为背景
			else
			{
				output[y * model->width + x] = VBPARAM->DEFAULT_COLOR_BACKGROUND;

				// 对前20帧进行VIBE模板更新(同前景)
				if (g_frameCount < model->nSample)
				{
					//curr
					rand = GetRand() % (model->nSample);
					tmpValue = sampleOffset + ((rand << 1) + rand);
					model->bg_samples[tmpValue] = image[imageOffset];  
					model->bg_samples[tmpValue + 1] = image[imageOffset + 1];  
					model->bg_samples[tmpValue + 2] = image[imageOffset + 2]; 

					//neighbor
					GetRandNeighborXY(x, y, &xNG, &yNG);
					rand = GetRand() % (model->nSample);
					tmpValue = ((yNG * model->width * model->nSample + xNG * model->nSample + rand) << 1) + (yNG * model->width * model->nSample + xNG * model->nSample + rand) ;
					model->bg_samples[tmpValue] = image[imageOffset];
					model->bg_samples[tmpValue + 1] = image[imageOffset + 1];
					model->bg_samples[tmpValue + 2] = image[imageOffset + 2];
				}
				else
				{
					//update current pixel model
					rand = GetRand() % (model->upSpeed);
					if (rand == 0 && left_flag == 1) 
					{
						rand = GetRand() % (model->nSample);
						tmpValue = sampleOffset + ((rand << 1) + rand);
						model->bg_samples[tmpValue] = image[imageOffset];  
						model->bg_samples[tmpValue + 1] = image[imageOffset + 1];  
						model->bg_samples[tmpValue + 2] = image[imageOffset + 2];  
					}

					//update neighboring pixel model
					// 满足条件才进行背景更新(为了提速)
					rand = GetRand() % (model->upSpeed);
					if (rand == 0 && left_flag == 1) 
					{
						GetRandNeighborXY(x, y, &xNG, &yNG);
						rand = GetRand() % (model->nSample);
						tmpValue = ((yNG * model->width * model->nSample + xNG * model->nSample + rand) << 1) + (yNG * model->width * model->nSample + xNG * model->nSample + rand);
						model->bg_samples[tmpValue] = image[imageOffset];
						model->bg_samples[tmpValue + 1] = image[imageOffset + 1];
						model->bg_samples[tmpValue + 2] = image[imageOffset + 2];
					}
				}

				///////////////////////////////////////////////////////////////////////////////////////////////
				//bkImageData[imageOffset] =(bkImageData[imageOffset] + image[imageOffset]) >> 1;
				//bkImageData[imageOffset + 1] =(bkImageData[imageOffset + 1] + image[imageOffset + 1]) >> 1;
				//bkImageData[imageOffset + 2] =(bkImageData[imageOffset + 2] + image[imageOffset + 2]) >> 1;
				///////////////////////////////////////////////////////////////////////////////////////////////
			}

		}// end of for x loop
	}// end of for y loop


	//VibeModelDilation(output, model->width, model->height);	// 膨胀
	//VibeModelErosion(output, model->width, model->height);// 腐蚀

	VibeModelDilation2(output, maskImg, model->width, model->height);	// 加速 膨胀
	VibeModelErosion2(maskImg, output, model->width, model->height);		//  加速 膨胀

	//VibeModelErosion(output,model->width,model->height);

	g_frameCount++;
	//VibeModelGetTrace(output,image,model->width,model->height,model->widthstep);
}

#include <opencv2/opencv.hpp>


void RegionSurveillance::RSinit_backgroud(int nWidth, int nHeight, int channels, unsigned char* frameImgData)
{
	pConExtraction->InitBackgroud(frameImgData, nWidth, nHeight, channels);
}

void RegionSurveillance::RSDetect(unsigned char* frameImgData, RegionInfo* pRegionInfo)
{
	/*     
	int widthstep = nWidth * 3;
	if (widthstep % 4)
	{
		widthstep = (widthstep / 4 + 1) * 4;
	}
	*/

	// 获得当前帧的前景和背景(前景255 背景0)同时对VIBE模板进行更新
	//VibeModelUpdateC3(vbM, frameImgData, greyImgData, MaskImgData);
	
	//if (vbM->nChannel == 3)
	//{
	//	// 高斯模糊
	//	if (gfiltFlag == 1)
	//	{
	//		unsigned char* inputData = NULL;
	//		inputData = new unsigned char[GlobelWidth * GlobelHeight];

	//		int i = 0;
	//		while (i < 3)
	//		{
	//			for (int nheight = 0; nheight < GlobelHeight; nheight++)
	//			{
	//				for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
	//				{
	//					inputData[nheight * GlobelWidth + nwidth] = frameImgData[nheight * GlobelWidthstep + nwidth * 3 + i];
	//				}
	//			}

	//			gaussianFilter(inputData, GlobelWidth, GlobelHeight, gWinSize);

	//			for (int nheight = 0; nheight < GlobelHeight; nheight++)
	//			{
	//				for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
	//				{
	//					frameImgData[nheight * GlobelWidthstep + nwidth * 3 + i] = inputData[nheight * GlobelWidth + nwidth];
	//				}
	//			}
	//			i++;
	//		}

	//		delete[] inputData;
	//	}

	//	VibeModelUpdateC3(vbM, frameImgData, MaskImgData);
	//} 
	//else
	//{
	//	if (channel_num == 3)
	//	{
	//		// 将RGB图像转换为灰度图像
	//		VibeRGBToGray(frameImgData, greyImgData);

	//		// 高斯模糊
	//		if (gfiltFlag == 1)
	//		{
	//			unsigned char* inputData = NULL;
	//			inputData = new unsigned char[GlobelWidth * GlobelHeight];
	//			
	//			for (int nheight = 0; nheight < GlobelHeight; nheight++)
	//			{
	//				for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
	//				{
	//					inputData[nheight * GlobelWidth + nwidth] = greyImgData[nheight * GlobelWidth + nwidth];
	//				}
	//			}

	//			gaussianFilter(inputData, GlobelWidth, GlobelHeight, gWinSize);

	//			for (int nheight = 0; nheight < GlobelHeight; nheight++)
	//			{
	//				for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
	//				{
	//					greyImgData[nheight * GlobelWidth + nwidth] = inputData[nheight * GlobelWidth + nwidth];
	//				}
	//			}
	//		
	//			delete[] inputData;
	//		}
	//		
	//		VibeModelUpdateC1(vbM, greyImgData, MaskImgData);
	//	} 
	//	else
	//	{
	//		// 高斯模糊
	//		if (gfiltFlag == 1)
	//		{
	//			unsigned char* inputData = NULL;
	//			inputData = new unsigned char[GlobelWidth * GlobelHeight];

	//			for (int nheight = 0; nheight < GlobelHeight; nheight++)
	//			{
	//				for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
	//				{
	//					inputData[nheight * GlobelWidth + nwidth] = frameImgData[nheight * GlobelWidthstep + nwidth];
	//				}
	//			}

	//			gaussianFilter(inputData, GlobelWidth, GlobelHeight, gWinSize);

	//			for (int nheight = 0; nheight < GlobelHeight; nheight++)
	//			{
	//				for (int nwidth = 0; nwidth < GlobelWidth; nwidth++)
	//				{
	//					frameImgData[nheight * GlobelWidthstep + nwidth] = inputData[nheight * GlobelWidth + nwidth];
	//				}
	//			}
	//			delete[] inputData;
	//		}

	//		VibeModelUpdateC1(vbM, frameImgData, MaskImgData);
	//	}

	//	//VibeModelUpdateC1(vbM, greyImgData, MaskImgData);
	//}
	

	
	//  显示二值图像
	/*CvSize a;
	a.width = GlobelWidth;
	a.height = GlobelHeight;
	IplImage *im = cvCreateImage(a, 8, 1);
	im->imageData = (char*)MaskImgData;
	cvShowImage("L2", im);
	cvWaitKey(0);*/
	


		/*{
		cv::Mat tmp_image(GlobelHeight, GlobelWidth, cv::CV_8UC3, pRGBIn);
		cv::imshow("res", tmp_image);
		cv::waitKey(1);

		}*/

	//get trace
	VibeModelGetTrace(MaskImgData, frameImgData, pRegionInfo);
}


/******************************************************************************
* Function:        VibeModelDilation
* Description:     VIBE模型膨胀(如果当前点为前景或者当前点的邻域中有一个为前景,则当前点位前景)
* Called By:       VibeModelUpdateC3
* Input:           inputdata     当前图像前景背景(前景为255 背景为0)
                   width         图像宽度
				   height        图像高度
* Output:          inputdata     膨胀后当前图像前景背景
*******************************************************************************/
void RegionSurveillance::VibeModelDilation(unsigned char* inputdata, int width, int height)
{
	int i, j;

	//sum = height * width * sizeof(unsigned char);
	//unsigned char *tmpdata = (unsigned char*) malloc(sum);
	//memcpy((unsigned char*)tmpdata, (unsigned char*)inputdata, sum);

	for (i = 1; i < height - 1; i++)
	{
		for (j = 1; j < width - 1; j++)
		{
			if (inputdata[i * width + j] == 255)
			{
			} 
			else
			{
				if (inputdata[(i - 1) * width + j - 1] == 255 || inputdata[(i - 1) * width + j] == 255 || inputdata[(i - 1) * width + j + 1] == 255 || 
					inputdata[i * width + j - 1] == 255 || inputdata[i * width + j + 1] == 255 ||
					inputdata[(i + 1) * width + j - 1] == 255 || inputdata[(i + 1) * width + j] == 255 || inputdata[(i + 1) * width + j + 1] == 255)
				{
					inputdata[i * width + j] = 250;
				}
			}
		}
	}


	for (i = 1; i < height - 1; i++)
	{
		for (j = 1; j < width - 1; j++)
		{
			if (inputdata[i * width + j] == 250)
			{
				inputdata[i * width + j] = 255;
			}
		}
	}
}

/******************************************************************************
* Function:        VibeModelErosion
* Description:     VIBE模型腐蚀(如果当前点或当前点的某一个邻域中有一个点为背景,则当前为背景)
* Called By:       VibeModelUpdateC3
* Input:           inputdata     当前图像前景背景(前景为255 背景为0)
                   width         图像宽度
				   height        图像高度
* Output:          inputdata     腐蚀后当前图像前景背景
*******************************************************************************/
void RegionSurveillance::VibeModelErosion(unsigned char* inputdata, int width, int height)
{
	int i, j;

	//sum = height * width * sizeof(unsigned char);
	//unsigned char *tmpdata = (unsigned char*) malloc(sum);
	//memcpy((unsigned char*) tmpdata, (unsigned char*) inputdata, sum);

	for (i = 1; i < height - 1; i++)
	{
		for (j = 1; j < width - 1; j++)
		{
			if (inputdata[i * width + j] == 0)
			{
			} 
			else
			{
				if (inputdata[(i - 1) * width + j - 1] == 0 || inputdata[(i - 1) * width + j] == 0 || inputdata[(i - 1) * width + j + 1] == 0 || 
					inputdata[i * width + j - 1] == 0 || inputdata[i * width + j + 1] == 0 ||
					inputdata[(i + 1) * width + j - 1] == 0 || inputdata[(i + 1) * width + j] == 0 || inputdata[(i + 1) * width + j + 1] == 0)
				{
					inputdata[i * width + j] = 5;
				}
			}
		}
	}


	for (i = 1; i < height - 1; i++)
	{
		for (j = 1; j < width - 1; j++)
		{
			if (inputdata[i * width + j] == 5)
			{
				inputdata[i * width + j] = 0;
			}
		}
	}
}


void RegionSurveillance::VibeModelDilation2(unsigned char* inputdata,unsigned char *output, int width,int height)
{
	memset(output, 0, sizeof(output[0])*width*height);
	if (width<32 || height<32 || inputdata==NULL || output==NULL)
		return;
	unsigned char *src = inputdata;
	unsigned char *dst = output;

	//memcpy(output, inputdata, width*height);
	for (int i=0; i<height; i++) {
		output[i*width+0] = 0;
		output[i*width+width-1] = 0;
	}
	for (int j=0; j<width; j++) {
		output[j] = 0;
		output[(height-1)*width+j]=0;
	}
	for (int i=1; i<height-1; i++)
	{
		src = inputdata + i*width;
		dst = output + i*width;
		for (int j = 1; j < width - 16; j += 16)
		{
			// 2 unaligned loads.                                                 
			__m128i m = _mm_loadu_si128((const __m128i*)(src + j ));
			m = _mm_max_epu8(m, _mm_loadu_si128((const __m128i*)(src + j - 1 - width)));
			m = _mm_max_epu8(m, _mm_loadu_si128((const __m128i*)(src + j + 1)));
			m = _mm_max_epu8(m, _mm_loadu_si128((const __m128i*)(src + j - width)));
			m = _mm_max_epu8(m, _mm_loadu_si128((const __m128i*)(src + j + 1 - width)));

			// 3 aligned loads: more efficient.                                       
			m = _mm_max_epu8(m, _mm_loadu_si128((const __m128i*)(src + j -1)));

			m = _mm_max_epu8(m, _mm_loadu_si128((const __m128i*)(src + j + width)));		
			m = _mm_max_epu8(m, _mm_loadu_si128((const __m128i*)(src + j -1 + width)));		
			m = _mm_max_epu8(m, _mm_loadu_si128((const __m128i*)(src + j +1 + width)));		
			// Store the result in the destination image.
			_mm_storeu_si128((__m128i*)(dst + j), m);
		}
	}

}
void RegionSurveillance::VibeModelErosion2(unsigned char* inputdata,unsigned char *output, int width,int height)
{
	memset(output, 0, sizeof(output[0])*width*height);
	if (width<32 || height<32 || inputdata==NULL || output==NULL)
		return;
	unsigned char *src = inputdata;
	unsigned char *dst = output;

	//memcpy(output, inputdata, width*height);
	for (int i=0; i<height; i++) {
		output[i*width+0] = 0;
		output[i*width+width-1] = 0;
	}
	for (int j=0; j<width; j++) {
		output[j] = 0;
		output[(height-1)*width+j]=0;
	}
	for (int i=1; i<height-1; i++)
	{
		src = inputdata + i*width;
		dst = output + i*width;
		for (int j = 1; j < width - 16; j += 16)
		{
			// 2 unaligned loads.                                                 
			__m128i m = _mm_loadu_si128((const __m128i*)(src + j ));
			m = _mm_min_epu8(m, _mm_loadu_si128((const __m128i*)(src + j - 1 - width)));
			m = _mm_min_epu8(m, _mm_loadu_si128((const __m128i*)(src + j + 1)));
			m = _mm_min_epu8(m, _mm_loadu_si128((const __m128i*)(src + j - width)));
			m = _mm_min_epu8(m, _mm_loadu_si128((const __m128i*)(src + j + 1 - width)));

			// 3 aligned loads: more efficient.                                       
			m = _mm_min_epu8(m, _mm_loadu_si128((const __m128i*)(src + j -1)));

			m = _mm_min_epu8(m, _mm_loadu_si128((const __m128i*)(src + j + width)));		
			m = _mm_min_epu8(m, _mm_loadu_si128((const __m128i*)(src + j -1 + width)));		
			m = _mm_min_epu8(m, _mm_loadu_si128((const __m128i*)(src + j +1 + width)));		
			// Store the result in the destination image.
			_mm_storeu_si128((__m128i*)(dst + j), m);
		}
	}

}


//按照中心点的位置进行排序 by zl
void RegionSurveillance::SortForeground(vector<CForeground> &ForegroundArray)
{
	std::sort(ForegroundArray.begin(),ForegroundArray.end(), SortByPoint);  
}

/******************************************************************************
* Function:        VibeModelGetTrace
* Description:     
* Calls:          ExtractContours
* Called By:       RSDetect
* Input:           pSourceImg    当前图像前景背景(前景为255 背景为0)
                   pRGBIn        当前图像RGB
                   width         图像宽度
				   height        图像高度
				   widthStep     图像RGB step
* Output:          pRGBIn        将前景描绘过的RGB
                   m_TargetArray 
							m_ptArray 与当前目标匹配成功的所有前景的中心点
							m_VectorDirection  当前目标与之前目标的向量
							//m_VectorDirectionNormal dx/sqrt(dx^2+dy^2)  dy/sqrt(dx^2+dy^2)
							m_CurRefPoint  目标的中心点位置(不断更新,取5个的平均值)
							m_LastRefPoint 目标之前的中心点位置(不断更新,取5个的平均值)
							m_PredictionBasePoint 最后一个匹配点中心位置
							m_PredictionPoint 下一帧的预测位置
							m_bHaveDirection  当前目标是否已经计算过方向 true是 flase否
						    m_dwLostFrame   距当前帧未匹配到的数目(用来判断是否目标丢失)
							m_dwID  ID 用来对不同的前景绘图时进行颜色上的区分
							m_dwBestMatch  匹配到当前帧的前景序号
							m_bCalc  标志目标序列在当前帧中是否匹配到 true匹配到  false未匹配到
							m_AlarmState 报警状态 false否  true报警
							m_AlarmNum 报警编号 
							//m_TarBox 目标的外接矩形
*******************************************************************************/
#include <opencv2/opencv.hpp>

void RegionSurveillance::VibeModelGetTrace(unsigned char* pSourceImg, unsigned char* pRGBIn, RegionInfo* pRegionInfo)
{
	//cv::Mat image2(GlobelHeight, GlobelWidth, CV_8UC1, pRGBIn);

	/*
	memset(regionAlarm, 0, sizeof(bool) * MAXROINUM);
	*/
	vector<CForeground> ForegroundArray;

	int m_dwFrameDist = 11;
	int m_dwFrameWnd = 5;
	int m_MaxDistance = 20;
	int m_InitMaxDistance =20;
	int m_dwMaxLost = 100;
	int m_dwMinFrame = 6;
	int dwTargetSize = 0;
	CForeground testTarget; // testForgroundArray 中的testTarget

	//前景图集合-zhm
	vector<CContour> foreContours; 
	
	
	
	// 满足条件的(像素点 面积等条件100)前景外接矩形
	if (channel_num == 3)
	{
		foreContours = pConExtraction->ExtractContours(pSourceImg, GlobelWidth, GlobelHeight, GlobelWidthstep / 3);
	} 
	else
	{
		//foreContours = pConExtraction->ExtractContours_Canny(pRGBIn, GlobelWidth, GlobelHeight, GlobelWidthstep);
		//foreContours = pConExtraction->ExtractContours(pSourceImg, GlobelWidth, GlobelHeight, GlobelWidthstep);
		foreContours = pConExtraction->ExtractContours_PixelSub(pRGBIn, GlobelWidth, GlobelHeight, GlobelWidthstep);
	}

	//轨迹匹配-zhm
	// 进一步在foreContours的基础上对前景外接矩形的面积进行筛选 将满足面积大于300的外接矩形添入ForegroundArray中
 	int forNum = foreContours.size();
	
 	for (int i = 0; i < forNum; i++)
 	{
 		//tmp_Contour = foreContours[i];
  		testTarget.m_rtConnect.left = foreContours[i].left;
  		testTarget.m_rtConnect.right = foreContours[i].right;
  		testTarget.m_rtConnect.top = foreContours[i].top;
  		testTarget.m_rtConnect.bottom = foreContours[i].bottom;
  		testTarget.m_ptCenter.x = (testTarget.m_rtConnect.left + testTarget.m_rtConnect.right) / 2;
  		testTarget.m_ptCenter.y = (testTarget.m_rtConnect.top + testTarget.m_rtConnect.bottom) / 2;

		//大于最小过滤面积且小于最大过滤面积	by zl 20160304
		int targetArea = abs((testTarget.m_rtConnect.left - testTarget.m_rtConnect.right) * (testTarget.m_rtConnect.top - testTarget.m_rtConnect.bottom));
		if ( targetArea > OBJECT_AREA_MIN && targetArea < OBJECT_AREA_MAX)
		{
			ForegroundArray.push_back(testTarget); 
		}
 	}
	



	if (ForegroundArray.size() > 0)	//前景库不空则排序处理 by zl 20160307
	{
		SortForeground(ForegroundArray);

		/*for (int i = 0; i < ForegroundArray.size(); i++)
		{
			printf("i=%d l=%d t=%d r=%d b=%d\n", i, ForegroundArray[i].m_rtConnect.left, ForegroundArray[i].m_rtConnect.top,
				ForegroundArray[i].m_rtConnect.right, ForegroundArray[i].m_rtConnect.bottom);
		}*/

		MergeNeighborBox(ForegroundArray);

	/*	for (int i = 0; i < ForegroundArray.size(); i++)
		{
			printf("i=%d l=%d t=%d r=%d b=%d\n", i, ForegroundArray[i].m_rtConnect.left, ForegroundArray[i].m_rtConnect.top,
				ForegroundArray[i].m_rtConnect.right, ForegroundArray[i].m_rtConnect.bottom);
		}*/

		DislodgeOverlapBox(ForegroundArray);
	}


	//20140305
	foreContours.clear();
	vector<CContour>().swap(foreContours);
	{
		std::vector<CContour>tmp = foreContours;
		foreContours.swap(tmp);
	}

	
	//  memory leak for 1 block

	//判断是否帧内目标丢失,若丢失dwTargetSize自减 
	vector<CTarget>::size_type countn = 0;
	int dwStep = GlobelWidthstep;
	/////////////////////////////////////////////////////////////////////////////////////////
	dwTargetSize = m_TargetArray.size();
	for (int i = 0; i < dwTargetSize; i++, countn++)
	{
		// 若已经超过100帧未出现该前景 则将该前景从m_TargetArray中删除
		if (m_TargetArray[i].m_dwLostFrame++ > m_dwMaxLost)
		{
			m_TargetArray.erase(m_TargetArray.begin() + countn);
			dwTargetSize--;
			i--;
			countn--;
		}
		else
		{
			m_TargetArray[i].m_bCalc = false;
			m_TargetArray[i].m_dwBestMatch = -1;
		}
	}   

	// Calculate distance matrix and determine every target's best match

	int dwForegroundCount = ForegroundArray.size(); 


	//m_TargetArray.clear();
	dwTargetSize = m_TargetArray.size();
	double * pDistanceMatrix = new double[dwForegroundCount * dwTargetSize];

	// 将每一个目标和该帧中的前景进行匹配
	for (int i = 0; i < dwTargetSize; i++)
	{
		//CMPointDouble directionPre;
		double Distance = m_TargetArray[i].m_bHaveDirection ? m_MaxDistance : m_InitMaxDistance;
		for (int j = 0; j < dwForegroundCount; j++)
		{
			//directionPre.x = ForegroundArray[j].m_ptCenter.x - m_TargetArray[i].m_CurRefPoint.x;
			//directionPre.y = ForegroundArray[j].m_ptCenter.y - m_TargetArray[i].m_CurRefPoint.y;
			pDistanceMatrix[i * dwForegroundCount + j] = 
				sqrt((ForegroundArray[j].m_ptCenter.x - m_TargetArray[i].m_PredictionPoint.x) * 
				       (ForegroundArray[j].m_ptCenter.x - m_TargetArray[i].m_PredictionPoint.x) + 
				       (ForegroundArray[j].m_ptCenter.y - m_TargetArray[i].m_PredictionPoint.y) * 
 				       (ForegroundArray[j].m_ptCenter.y - m_TargetArray[i].m_PredictionPoint.y));
			/*
			double directionResult = m_TargetArray[i].m_VectorDirectionNormal.cx * directionPre.x
				+ m_TargetArray[i].m_VectorDirectionNormal.cy * directionPre.y;
			*/
			if (pDistanceMatrix[i * dwForegroundCount + j] <= Distance )	//&& directionResult >= 0
			{
				Distance = pDistanceMatrix[i * dwForegroundCount + j];
				m_TargetArray[i].m_dwBestMatch = j;
				m_TargetArray[i].m_TarBox = ForegroundArray[j].m_rtConnect;
			}
		}
	}
	// no leak after --> //delete []pDistanceMatrix;
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////
	m_FinalArray.clear();

	// Tracking and draw
	// 对当前帧的每一个前景与目标进行匹配
	bool left_flag_c = 1;
	for (int i = 0; i < dwForegroundCount; i++)
	{
		int dwBestMatch = -1;
		double Distance = m_MaxDistance > m_InitMaxDistance ? m_MaxDistance : m_InitMaxDistance;
		for (int j = 0; j < dwTargetSize; j++)
		{
			if (m_TargetArray[j].m_bHaveDirection)
			{
				if (pDistanceMatrix[j * dwForegroundCount + i] <= Distance && 
					pDistanceMatrix[j * dwForegroundCount + i] <= m_MaxDistance)
				{
					Distance = pDistanceMatrix[j * dwForegroundCount + i];
					dwBestMatch = j;
				}
			}
			else
			{
				if (pDistanceMatrix[j * dwForegroundCount + i] <= Distance && 
					pDistanceMatrix[j * dwForegroundCount + i] <= m_InitMaxDistance)
				{
					Distance = pDistanceMatrix[j * dwForegroundCount + i];
					dwBestMatch = j;
				}
			}
		}
		//m_TargetArray.clear();
		// 如果匹配成功  m_TargetArray[dwBestMatch].m_ptArray.size() !=500??????????????????????不理解为什么不能等于500
		if (dwBestMatch >= 0 && m_TargetArray[dwBestMatch].m_dwBestMatch == i && m_TargetArray[dwBestMatch].m_ptArray.size() < 500) 
		{
			// Find it
			m_TargetArray[dwBestMatch].m_bCalc = true;
			//m_TargetArray[dwBestMatch].m_AlarmState = false;
			//m_TargetArray[dwBestMatch].m_AlarmState1 = false;
			//m_TargetArray[dwBestMatch].m_AlarmState2 = false;
			//m_TargetArray[dwBestMatch].m_AlarmState3 = false;
			//m_TargetArray[dwBestMatch].m_AlarmState4 = false;
			//m_TargetArray[dwBestMatch].m_AlarmState5 = false;
			m_TargetArray[dwBestMatch].m_dwLostFrame = 0;
			m_TargetArray[dwBestMatch].m_ptArray.push_back(ForegroundArray[i].m_ptCenter);

			// Update current reference point
			int dwPointCount = m_TargetArray[dwBestMatch].m_ptArray.size();

			m_TargetArray[dwBestMatch].m_CurRefPoint.x += (double)ForegroundArray[i].m_ptCenter.x / m_dwFrameWnd;
			m_TargetArray[dwBestMatch].m_CurRefPoint.y += (double)ForegroundArray[i].m_ptCenter.y / m_dwFrameWnd;
			if (dwPointCount > m_dwFrameWnd)
			{
				m_TargetArray[dwBestMatch].m_CurRefPoint.x -= 
					(double) m_TargetArray[dwBestMatch].m_ptArray[dwPointCount - m_dwFrameWnd - 1].x / m_dwFrameWnd;
				m_TargetArray[dwBestMatch].m_CurRefPoint.y -= 
					(double) m_TargetArray[dwBestMatch].m_ptArray[dwPointCount - m_dwFrameWnd - 1].y / m_dwFrameWnd;
			}

			// Update last reference point
			// 如果与目标匹配成功的前景达到一定数量
			if (dwPointCount > m_dwFrameDist)
			{
				// 目标之前的中心点位置(5帧帧平均)
				m_TargetArray[dwBestMatch].m_LastRefPoint.x += 
					(double) m_TargetArray[dwBestMatch].m_ptArray[dwPointCount - m_dwFrameDist - 1].x / m_dwFrameWnd;
				m_TargetArray[dwBestMatch].m_LastRefPoint.y += 
					(double) m_TargetArray[dwBestMatch].m_ptArray[dwPointCount - m_dwFrameDist - 1].y / m_dwFrameWnd;
				if (dwPointCount >= m_dwFrameDist + m_dwFrameWnd)
				{
					if (dwPointCount > m_dwFrameDist + m_dwFrameWnd)
					{
						m_TargetArray[dwBestMatch].m_LastRefPoint.x -= 
							(double) m_TargetArray[dwBestMatch].m_ptArray[dwPointCount - m_dwFrameDist - m_dwFrameWnd - 1].x / m_dwFrameWnd;
						m_TargetArray[dwBestMatch].m_LastRefPoint.y -= 
							(double) m_TargetArray[dwBestMatch].m_ptArray[dwPointCount - m_dwFrameDist - m_dwFrameWnd - 1].y / m_dwFrameWnd;
					}

					// Calculate direction
					m_TargetArray[dwBestMatch].m_bHaveDirection = true;
					m_TargetArray[dwBestMatch].m_PredictionBasePoint = m_TargetArray[dwBestMatch].m_CurRefPoint;
					m_TargetArray[dwBestMatch].m_VectorDirection.cx = 
						(m_TargetArray[dwBestMatch].m_CurRefPoint.x - m_TargetArray[dwBestMatch].m_LastRefPoint.x) / m_dwFrameDist;
					m_TargetArray[dwBestMatch].m_VectorDirection.cy = 
						(m_TargetArray[dwBestMatch].m_CurRefPoint.y - m_TargetArray[dwBestMatch].m_LastRefPoint.y) / m_dwFrameDist;
					/*
					m_TargetArray[dwBestMatch].m_VectorDirectionNormal.cx = m_TargetArray[dwBestMatch].m_VectorDirection.cx /(sqrt(m_TargetArray[dwBestMatch].m_VectorDirection.cx * m_TargetArray[dwBestMatch].m_VectorDirection.cx 
						+ m_TargetArray[dwBestMatch].m_VectorDirection.cy * m_TargetArray[dwBestMatch].m_VectorDirection.cy));
					m_TargetArray[dwBestMatch].m_VectorDirectionNormal.cy = m_TargetArray[dwBestMatch].m_VectorDirection.cy /(sqrt(m_TargetArray[dwBestMatch].m_VectorDirection.cx * m_TargetArray[dwBestMatch].m_VectorDirection.cx 
						+ m_TargetArray[dwBestMatch].m_VectorDirection.cy * m_TargetArray[dwBestMatch].m_VectorDirection.cy));
					*/
				}
			}
		}
		else
		{
			// 检测到新的目标
			// New object found
			CTarget NewTarget;
			//NewTarget.GetID();
			NewTarget.m_dwID= ++dwGlobalID;	// GlobalID assigned for target

			// 新的前景的中心位置
			NewTarget.m_ptArray.push_back(ForegroundArray[i].m_ptCenter);

			m_TargetArray.push_back(NewTarget);

			NewTarget.m_ptArray.clear();
			vector<CMPoint>().swap(NewTarget.m_ptArray);
			{
				std::vector<CMPoint>tmp = NewTarget.m_ptArray;
				NewTarget.m_ptArray.swap(tmp);
			}

			dwBestMatch = m_TargetArray.size() - 1;

			// Update current reference point
			//m_TargetArray-m_CurRefPoint当前前景的中心点位置(取五帧的平均数)
			m_TargetArray[dwBestMatch].m_CurRefPoint.x += (double)ForegroundArray[i].m_ptCenter.x / m_dwFrameWnd;
			m_TargetArray[dwBestMatch].m_CurRefPoint.y += (double)ForegroundArray[i].m_ptCenter.y / m_dwFrameWnd;

			m_TargetArray[dwBestMatch].m_TarBox = ForegroundArray[i].m_rtConnect;
		}

	    MonitoringAlarm(pRGBIn, ForegroundArray[i], dwBestMatch, pRegionInfo);
		
		if (left_flag_c == 0)
		{
		}
		else
		{
			if (left_flag_now_s ==0 || left_flag_now_w == 0)
			{
				left_flag_c = 0;
			}
		}

		//保存最终输出目标
		m_FinalArray.push_back(m_TargetArray[dwBestMatch]);
		
	}

	left_flag = left_flag_c;

	// Release resources
	delete []pDistanceMatrix;

	ForegroundArray.clear();

	// suxiaoyun
	//ForegroundArray.swap(vector<CForeground>(0));
	//std::vector<CForeground>(ForegroundArray).swap(ForegroundArray);
	vector<CForeground>().swap(ForegroundArray);
	{
		std::vector<CForeground>tmp = ForegroundArray;
		ForegroundArray.swap(tmp);
	}

	///////////////////////////////////////////////////////////////////////////////////////
	// Prediction
	int dwObjectCount = m_TargetArray.size();
	// 遍历所有的目标
	for (int i=0; i < dwObjectCount; i++)
	{
		// 如果该目标在当前帧中没有匹配成功但是当前目标已经计算过方向
		if (!m_TargetArray[i].m_bCalc && m_TargetArray[i].m_bHaveDirection)
		{
			// Record prediction as current point
			m_TargetArray[i].m_ptArray.push_back(CMPoint((long) (m_TargetArray[i].m_PredictionPoint.x + 0.5), 
				(long) (m_TargetArray[i].m_PredictionPoint.y + 0.5)));
			// Update current reference point
			int dwPointCount = m_TargetArray[i].m_ptArray.size();
			m_TargetArray[i].m_CurRefPoint.x += (double) m_TargetArray[i].m_ptArray[dwPointCount - 1].x / m_dwFrameWnd;
			m_TargetArray[i].m_CurRefPoint.y += (double) m_TargetArray[i].m_ptArray[dwPointCount - 1].y / m_dwFrameWnd;
			if (dwPointCount > m_dwFrameWnd)
			{
				m_TargetArray[i].m_CurRefPoint.x -= 
					(double) m_TargetArray[i].m_ptArray[dwPointCount - m_dwFrameWnd - 1].x / m_dwFrameWnd;
				m_TargetArray[i].m_CurRefPoint.y -= 
					(double) m_TargetArray[i].m_ptArray[dwPointCount - m_dwFrameWnd - 1].y / m_dwFrameWnd;
			}

			// Update last reference point
			if (dwPointCount > m_dwFrameDist)
			{
				m_TargetArray[i].m_LastRefPoint.x += 
					(double) m_TargetArray[i].m_ptArray[dwPointCount - m_dwFrameDist - 1].x / m_dwFrameWnd;
				m_TargetArray[i].m_LastRefPoint.y += 
					(double) m_TargetArray[i].m_ptArray[dwPointCount - m_dwFrameDist - 1].y / m_dwFrameWnd;
				if (dwPointCount >= m_dwFrameDist + m_dwFrameWnd)
				{
					if (dwPointCount > m_dwFrameDist + m_dwFrameWnd)
					{
						m_TargetArray[i].m_LastRefPoint.x -= 
							(double) m_TargetArray[i].m_ptArray[dwPointCount - m_dwFrameDist - m_dwFrameWnd - 1].x / m_dwFrameWnd;
						m_TargetArray[i].m_LastRefPoint.y -= 
							(double) m_TargetArray[i].m_ptArray[dwPointCount - m_dwFrameDist - m_dwFrameWnd - 1].y / m_dwFrameWnd;
					}
				}
			}
		}
		if (m_TargetArray[i].m_bHaveDirection)
		{
			// Calculate prediction point
			m_TargetArray[i].m_PredictionPoint.x = 
				m_TargetArray[i].m_PredictionBasePoint.x + 
				m_TargetArray[i].m_VectorDirection.cx * 				(m_TargetArray[i].m_dwLostFrame + m_dwFrameWnd / 2 + 1);
			m_TargetArray[i].m_PredictionPoint.y = 
				m_TargetArray[i].m_PredictionBasePoint.y + 
				m_TargetArray[i].m_VectorDirection.cy * 
				(m_TargetArray[i].m_dwLostFrame + m_dwFrameWnd / 2 + 1);
		}
		else
		{
			m_TargetArray[i].m_PredictionPoint.x = 
				(double) m_TargetArray[i].m_ptArray[m_TargetArray[i].m_ptArray.size() - 1].x;
			m_TargetArray[i].m_PredictionPoint.y = 
				(double) m_TargetArray[i].m_ptArray[m_TargetArray[i].m_ptArray.size() - 1].y;
		}
	}
}


/******************************************************************************
* Function:        RSRegion
* Description:     感兴趣区域设置
* Calls:           vibeMask
* Called By:       main
* Input:           numROI              ROI个数
                   pRegionInfo         ROI区域
				   iflog			   true:输出日志 false:不输出日志
* Output:          pRegion             10个ROI指针(bool)每个大小为(图像宽*图像高)感兴趣区域值为1  其余为0
                   //regionAlarm         10个ROI 报警标志(bool)         
				   pAlarmInfo	       10个ROI 报警标志(int)   
* Return:          3 1
*******************************************************************************/
void printAlarmType(fstream *file, int style)
{
	switch (style)
	{
	case 1:
		*file<<"进入禁区, ";
		break;
	case 2:
		*file<<"离开禁区, ";
		break;
	case 3:
		*file<<"单向越界, ";
		break;
	case 4:
		*file<<"双向越界, ";
		break;
	case 5:
		*file<<"徘徊, ";
		break;
	case 6:
		*file<<"丢包, ";
		break;
	default :
		break;
	}
}


int RegionSurveillance::RSRegion(int numROI, RegionInfo* pRegionInfo, bool iflog)
{
	fstream file;//用于输出日志
	if (numROI > MAXROINUM)
	{
		return 0;
	}
	//自动设置用于确定方向的点
	for(int i = 0; i < numROI; i++)
	{
		if (iflog)
		{
			file.open("log.txt",ios::app);
			file<<i<<": ";
			printAlarmType(&file, pRegionInfo[i].AlarmInfo);
			file<<"P1("<<pRegionInfo[i].pROI[0].x<<","<<pRegionInfo[i].pROI[0].y<<"), P2("
				<<pRegionInfo[i].pROI[1].x<<","<<pRegionInfo[i].pROI[1].y<<")"; 
		}
		
		if (pRegionInfo[i].AlarmInfo == 3)
		{
			pRegionInfo[i].finaldir = get_ArrowDir(pRegionInfo[i].pROI[0], pRegionInfo[i].pROI[1], pRegionInfo[i].dirPoint);//确定方向计算,箭头端点坐标
			
			if (iflog)
				file<<", DirPoint("<<pRegionInfo[i].dirPoint.x<<","<<pRegionInfo[i].dirPoint.y<<")";
			
			pRegionInfo[i].ArrowStartPoint = pStart;	//箭头的起始端点 箭头指向正方形
			pRegionInfo[i].ArrowEndPoint = pEnd;		//箭头的终止端点 箭头指向正方形
		}

		if (iflog)
		{
			file<<endl;
			file.close();
		}
	}

	m_numROI = numROI;
	if (pRegion == NULL)
	{
		pRegion = new bool*[MAXROINUM];
		memset(pRegion, 0, sizeof(bool*) * MAXROINUM);
	}

	if (pAlarmInfo == NULL)
	{
		pAlarmInfo = new int[MAXROINUM];
		memset(pAlarmInfo, 0, sizeof(int) * MAXROINUM);
	}
	
	for (int i = 0; i < MAXROINUM; i++)
	{
		if (pRegion[i] == NULL)
		{
			pRegion[i] = new bool[GlobelHeight * GlobelWidth];
		}
		memset(pRegion[i], 0, sizeof(bool) * GlobelWidth * GlobelHeight);
		if (i <= m_numROI)
		{
			pAlarmInfo[i] = pRegionInfo[i].AlarmInfo;
		}
		else
		{
			pAlarmInfo[i] = 0;
		}
	}
	

	// 对每一个待观察的ROI进行监测
	for (int i = 0; i < numROI;  i++)
	{
		// 对每一个感兴趣区域进行标志 感兴趣区域pRegion值为1  其余为0
		vibeMask(pRegionInfo[i].nPointNum, pRegionInfo[i].pROI, pRegion[i]);
	}
	
	vector<vector<Point>> regions;

	regions.resize(numROI);

	for (int i = 0; i < numROI; i++)
	{
		for (int j = 0; j < pRegionInfo[i].nPointNum; j++)
		{
			Point cur_point{ pRegionInfo[i].pROI[j].x, pRegionInfo[i].pROI[j].y};
			regions[i].push_back(cur_point);
		}
	}

	pConExtraction->CalMask(regions, GlobelWidth, GlobelHeight);

	return 1;
}

void RegionSurveillance::vibeMask(int num, CMPoint *pPointList, bool *pRegion)
{
	
	if (num == 2)
	{
		double s;
		int i, x, y;
		int xs, ys;
		int dx, dy;

		dx = pPointList[1].x - pPointList[0].x;
		if (dx < 0)
		{
			dx = -dx;
		}
		dy = pPointList[1].y - pPointList[0].y;
		if (dy < 0)
		{
			dy = -dy;
		}
		if (dy < dx)
		{
			//draw according to x-y
			s = (double) (pPointList[1].y - pPointList[0].y) / (pPointList[1].x - pPointList[0].x);
			
			if (pPointList[0].x < pPointList[1].x)
			{
				xs = pPointList[0].x;
				ys = pPointList[0].y;
			}
			else
			{
				xs = pPointList[1].x;
				ys = pPointList[1].y;
			}

			for (i = 0; i <= dx; i++)
			{
				x = i + xs;
				y = (int) ((double) i * s) + ys;
				if (x > -1 && x < GlobelWidth && y > -1&& y < GlobelHeight)
				{
					pRegion[GlobelWidth * y + x] = 1;
				}
			}
		}
		else
		{
			//draw according to y-x
			s = (double) (pPointList[1].x - pPointList[0].x) / (pPointList[1].y - pPointList[0].y);
			
			if (pPointList[0].y < pPointList[1].y)
			{
				xs = pPointList[0].x;
				ys = pPointList[0].y;
			}
			else
			{
				xs = pPointList[1].x;
				ys = pPointList[1].y;
			}
			for (i = 0; i <= dy; i++)
			{
				y = i + ys;
				x = (int) ((double) i * s)+xs;
				if (y > -1 && y < GlobelHeight && x > -1 && x < GlobelWidth)
				{
					pRegion[GlobelWidth * y + x] = 1;
				}
			}
		}		
	}
	else if (num >= 3)
	{
		
		CMPoint LineMin;
		CMPoint LineMax;
	
		for (int k=0; k < num; k++)
		{
			int startp = k;
			int endp = k + 1;

			if (startp == num - 1)
			{
				endp = 0;
			}
		
			if (abs(pPointList[startp].x - pPointList[endp].x) >= abs(pPointList[startp].y - pPointList[endp].y))
			{
				if (pPointList[startp].x < pPointList[endp].x)
				{
					LineMin = pPointList[startp];
					LineMax = pPointList[endp];
					for (int x = LineMin.x; x <= LineMax.x; x++)
					{
						int y = (int) (((LineMin.y - LineMax.y) / (double) (LineMin.x - LineMax.x)) * (double) (x - LineMin.x) + 0.5 + (double)LineMin.y);
						//pRegion[GlobelWidth * y + x] = 1;
						
						//原程序存在越界 苏晓芸修改如下
						if (y > -1 && y < GlobelHeight && x > -1 && x < GlobelWidth)
						{
							pRegion[GlobelWidth * y + x] = 1;
						}
					}
				}
				else if (pPointList[startp].x > pPointList[endp].x)
				{
					LineMin = pPointList[endp];
					LineMax = pPointList[startp];
					for (int x = LineMin.x; x <= LineMax.x; x++)
					{
						int y = (int) (((LineMin.y - LineMax.y) / (double)(LineMin.x - LineMax.x)) * (double)(x - LineMin.x) + 0.5 + (double)LineMin.y);
						//pRegion[GlobelWidth * y + x] = 1;

						//原程序存在越界 苏晓芸修改如下
						if (y > -1 && y < GlobelHeight && x > -1 && x < GlobelWidth)
						{
							pRegion[GlobelWidth * y + x] = 1;
						}
					}
				}
				else
				{
					int YMin = (pPointList[startp].y < pPointList[endp].y)? pPointList[startp].y : pPointList[endp].y;
					int YMax = (pPointList[startp].y < pPointList[endp].y)? pPointList[endp].y : pPointList[startp].y;
					for (int i = YMin; i <= YMax; i++)
					{
						//pRegion[GlobelWidth * i + pPointList[startp].x] = 1;

						//原程序存在越界 苏晓芸修改如下
						if (i > -1 && i < GlobelHeight && pPointList[startp].x > -1 && pPointList[startp].x < GlobelWidth)
						{
							pRegion[GlobelWidth * i + pPointList[startp].x] = 1;
						}
					}
				}
			}
			else
			{
				if (pPointList[startp].y < pPointList[endp].y)
				{
					LineMin = pPointList[startp];
					LineMax = pPointList[endp];
					for (int y = LineMin.y; y <= LineMax.y; y++)
					{
						int x = (int) (((LineMin.x - LineMax.x) / (double) (LineMin.y - LineMax.y)) * (double) (y - LineMin.y) + 0.5 + (double)LineMin.x);
						//pRegion[GlobelWidth * y + x] = 1;

						//原程序存在越界 苏晓芸修改如下
						if (y > -1 && y < GlobelHeight && x > -1 && x < GlobelWidth)
						{
							pRegion[GlobelWidth * y + x] = 1;
						}
					}
				}
				else if (pPointList[startp].y > pPointList[endp].y)
				{
					LineMin = pPointList[endp];
					LineMax = pPointList[startp];
					for (int y = LineMin.y; y <= LineMax.y; y++)
					{
						int x = (int) (((LineMin.x - LineMax.x) / (double) (LineMin.y - LineMax.y)) * (double) (y - LineMin.y) + 0.5 + (double)LineMin.x);
						//pRegion[GlobelWidth * y + x] = 1;

						//原程序存在越界 苏晓芸修改如下
						if (y > -1 && y < GlobelHeight && x > -1 && x < GlobelWidth)
						{
							pRegion[GlobelWidth * y + x] = 1;
						}
					}
				}
				else
				{
					int XMin = (pPointList[startp].x < pPointList[endp].x)? pPointList[startp].x : pPointList[endp].x;
					int XMax = (pPointList[startp].x < pPointList[endp].x)? pPointList[endp].x : pPointList[startp].x;
					for (int i = XMin; i <= XMax; i++)
					{
						//pRegion[GlobelWidth * pPointList[startp].y + i] = 1;

						//原程序存在越界 苏晓芸修改如下
						if (pPointList[startp].y > -1 && pPointList[startp].y < GlobelHeight && i > -1 && i < GlobelWidth)
						{
							pRegion[GlobelWidth * pPointList[startp].y + i] = 1;
						}
					}
				}
			}
		}
	
		// 修改标记方式 将区域边缘标志置为0
		for (int i = 0; i < GlobelHeight; i++)
		{		
			for (int j = 0; j < GlobelWidth;  j++)
			{
				if (pRegion[GlobelWidth * i + j] == 0)
				{
					pRegion[GlobelWidth * i + j] = 1;
				}
				else
				{
					pRegion[GlobelWidth * i + j] = 0;
				}
			}
		}

		CMPoint StartPoint;
		int SumX = 0;
		int SumY = 0;
		for (int k = 0; k < num; k++)
		{
			SumX = SumX + pPointList[k].x;
			SumY = SumY + pPointList[k].y;
		}
		StartPoint.x = SumX / num;
		StartPoint.y = SumY / num;

		bool *pMaskMap  = new bool[GlobelWidth * GlobelHeight];
		bool *pMaskMapS = new bool[GlobelWidth * GlobelHeight];
		memset(pMaskMap,  0, sizeof(bool) * GlobelWidth * GlobelHeight);
		memset(pMaskMapS, 0, sizeof(bool) * GlobelWidth * GlobelHeight);
		pMaskMap[GlobelWidth * StartPoint.y + StartPoint.x] = 1;

		int msum = 1;
		while (msum != 0)
		{
			msum = 0;
			for (int i = 0; i < GlobelHeight; i++)
			{		
				for (int j = 0; j < GlobelWidth; j++)
				{
					msum = msum + pMaskMap[GlobelWidth * i + j] - pMaskMapS[GlobelWidth * i + j];
					pMaskMapS[GlobelWidth * i + j] = pMaskMap[GlobelWidth * i + j];
				}
			}
			for (int  i = 0; i < GlobelHeight; i++)
			{		
				for (int j = 0; j < GlobelWidth; j++)
				{
					if ((i <= 0) || (i >= GlobelHeight - 1) || (j <= 0) || (j >= GlobelWidth - 1))
					{
						continue;
					}

					if (pMaskMapS[GlobelWidth * i + j] == 1)
					{
						pMaskMap[GlobelWidth * i + j + 1] = 1;
						pMaskMap[GlobelWidth * i + j - 1] = 1;
						pMaskMap[GlobelWidth * (i + 1) + j] = 1;
						pMaskMap[GlobelWidth * (i - 1) + j] = 1;
					}
				}
			}
			for (int i = 0; i < GlobelHeight; i++)
			{		
				for (int j = 0; j < GlobelWidth; j++)
				{
					pMaskMap[GlobelWidth * i + j] = pMaskMap[GlobelWidth * i + j] && pRegion[GlobelWidth * i + j];
				}
			}
		}

		memcpy(pRegion, pMaskMap, sizeof(bool) * GlobelWidth * GlobelHeight);
    
		delete[] pMaskMap;
		delete[] pMaskMapS;
	}
}


int RegionSurveillance::getObjectNum()
{
	return m_FinalArray.size();
}



void RegionSurveillance::getObjectInfo(int ObjCount, MS_ObjectInfo* ObjInfo)
{
	int ii;
	//printf("getObjectInfo: %d\n", ObjCount);

	for (int i = 0; i < ObjCount; i++)
	{
		int dwPointCount = m_FinalArray[i].m_ptArray.size();
		ObjInfo[i].curPos = m_FinalArray[i].m_ptArray[dwPointCount - 1];
		int temp_dwPointCount = dwPointCount;
		if (dwPointCount > MAXTRACENUM)
		{
			dwPointCount = MAXTRACENUM;
		}
		ObjInfo[i].trace.nTraceNum = dwPointCount;
		for (ii = 0; ii < MAXROINUM; ii++)
		{
			ObjInfo[i].pbAlarmState[ii] = false;
		}

		for (int j = dwPointCount - 1, j1 = temp_dwPointCount - 1; j >= 0;  j--, j1--)
		{
			//ObjInfo[i].trace.pObjTrace[j] = Gauss->m_TargetArray[i].m_ptArray[j];
			ObjInfo[i].trace.pObjTrace[j] = m_FinalArray[i].m_ptArray[j1];
			if (ObjInfo[i].trace.pObjTrace[j].x < 0) 
			{
				ObjInfo[i].trace.pObjTrace[j].x = 0;
			}
			if (ObjInfo[i].trace.pObjTrace[j].x >= GlobelWidth)
			{
				ObjInfo[i].trace.pObjTrace[j].x = GlobelWidth - 1;
			}
			if (ObjInfo[i].trace.pObjTrace[j].y < 0)
			{
				ObjInfo[i].trace.pObjTrace[j].y = 0;
			}
			if (ObjInfo[i].trace.pObjTrace[j].y >= GlobelHeight)
			{
				ObjInfo[i].trace.pObjTrace[j].y = GlobelHeight - 1;
			}
		}	

        for (ii = 0; ii < MAXROINUM; ii++)
        {
			ObjInfo[i].pbAlarmState[ii] = m_FinalArray[i].pbAlarmState[ii];
			for (int k = 0; k < ALARMTYPENUM; ++k)
			{
				ObjInfo[i].pbAlarmType[ii][k] = m_FinalArray[i].pbAlarmType[ii][k];
			}

        }
		/*
		int AlarmNum = m_FinalArray[i].m_AlarmNum;
		if (m_FinalArray[i].m_AlarmState)
		{
			ObjInfo[i].pbAlarmState[AlarmNum] = true;
		}
		*/
		// unique ID for current object
		ObjInfo[i].TarBox = m_FinalArray[i].m_TarBox;    //目标外接矩形
		ObjInfo[i].UniqueID = m_FinalArray[i].m_dwID;	//当前目标的特点ID					
	}
}



// 将图像转换为灰度图像
void RegionSurveillance::VibeRGBToGray(unsigned char* RgbData, unsigned char* GrayData)
{
	int nHeight, nWidth;
	int index;

	for (nHeight = 0; nHeight != GlobelHeight; nHeight++)
		for (nWidth = 0; nWidth != GlobelWidth; nWidth++)
		{
			index = GlobelWidthstep * nHeight + 3 * nWidth;
			GrayData[GlobelWidthstep * nHeight / 3 + nWidth] = 
				    (RgbData[index + 2] * 19595 
			       + RgbData[index + 1] * 38469
			       + RgbData[index] * 7472) >> 16;
		}
}



//pAlarmInfo 1进入禁区 2离开禁区 3单向越界 4双向越界 5徘徊 6丢包
void RegionSurveillance::MonitoringAlarm(unsigned char* pRGBIn, CForeground ForegroundArray_i, int dwBestMatch, RegionInfo* pRegionInfo)
{

	// 目标锁定
 	//ExternalRectangle(pRGBIn, ForegroundArray_i, dwBestMatch, widthStep, 255, 0, 0);
	/*
	int dwStep = widthStep;

	unsigned char bRed   = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 3) * 85 + 85);
	unsigned char bGreen = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 7) * 36 + 36);
	unsigned char bBlue  = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 13) * 20 + 15);


	// 前景目标外接矩形描绘
	for (int t = ForegroundArray_i.m_rtConnect.left; t <= ForegroundArray_i.m_rtConnect.right; t++)
	{
		pRGBIn[ForegroundArray_i.m_rtConnect.top * dwStep + t * 3] = 0;
		pRGBIn[ForegroundArray_i.m_rtConnect.top * dwStep + t * 3 + 1] = 0;
		pRGBIn[ForegroundArray_i.m_rtConnect.top * dwStep + t * 3 + 2] = 255;
		pRGBIn[ForegroundArray_i.m_rtConnect.bottom * dwStep + t * 3] = 0;
		pRGBIn[ForegroundArray_i.m_rtConnect.bottom * dwStep + t * 3 + 1] = 0;
		pRGBIn[ForegroundArray_i.m_rtConnect.bottom * dwStep + t * 3 + 2] = 255;
	}
	for (int t = ForegroundArray_i.m_rtConnect.top; t < ForegroundArray_i.m_rtConnect.bottom; t++)
	{
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.left * 3] = 0;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.left * 3 + 1] = 0;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.left * 3 + 2] = 255;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.right * 3] = 0;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.right * 3 + 1] = 0;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.right * 3 + 2] = 255;
	}
	*/
		
	
	int a = ForegroundArray_i.m_rtConnect.bottom;
	int b = ForegroundArray_i.m_rtConnect.top;
	int c = ForegroundArray_i.m_rtConnect.left;
	int d = ForegroundArray_i.m_rtConnect.right;

	// 区域监控 (从1开始 因为适配中行物品遗留,0的位置留给存储人的位置,不是有效位置 -- update by mliu 20201230)
	for (int ii = 1; ii < m_numROI; ii++) 
	{					
		
		//if (pAlarmInfo[ii] == 1)	//进入禁区
		//{
		//	AccessToRestrictedAreas(pRegion[ii], dwBestMatch,a, b, c, d, ii);

		//	/*
		//	if (m_TargetArray[dwBestMatch].m_AlarmState1 == true && m_TargetArray[dwBestMatch].m_AlarmNum1 == ii)
		//	{
		//		//ExternalRectangle(pRGBIn, ForegroundArray_i, dwBestMatch, widthStep, 0, 0, 255);
		//	}
		//	*/
		//}
		//else if (pAlarmInfo[ii] == 2)	//离开禁区
		//{
		//	LeaveTheRestrictedAreas(pRegion[ii], dwBestMatch,a, b, c, d, ii);
		//	/*
		//	if (m_TargetArray[dwBestMatch].m_AlarmState2 == true && m_TargetArray[dwBestMatch].m_AlarmNum2 == ii)
		//	{
		//		//ExternalRectangle(pRGBIn, ForegroundArray_i, dwBestMatch, widthStep, 0, 255, 0);
		//	}
		//	*/
		//}
		//
		//else if (pAlarmInfo[ii] == 3) //单向越界
		//{
		//	OneWayCrossBorder(pRegion[ii], dwBestMatch, a, b, c, d, ii, pRegionInfo);
		//	/*
		//	if (m_TargetArray[dwBestMatch].m_AlarmState3 == true && m_TargetArray[dwBestMatch].m_AlarmNum3 == ii)
		//	{
		//		//ExternalRectangle(pRGBIn, ForegroundArray_i, dwBestMatch, widthStep, 0, 255, 0);
		//	}
		//	*/
		//	
		//}
		//else if (pAlarmInfo[ii] == 4) //双向越界
		//{
		//	TwoWayCrossBorder(pRegion[ii], dwBestMatch, a, b, c, d, ii);
		//	/*
		//	if (m_TargetArray[dwBestMatch].m_AlarmState4 == true && m_TargetArray[dwBestMatch].m_AlarmNum4 == ii)
		//	{
		//		//ExternalRectangle(pRGBIn, ForegroundArray_i, dwBestMatch, widthStep, 0, 0, 255);
		//	}
		//	*/

		//}
		//else if (pAlarmInfo[ii] == 5)	//徘徊
		//{
		//	WanderMonitoring(pRegion[ii], dwBestMatch, a, b, c, d, ii, pRegionInfo, pRGBIn);
		//	/*
		//	if (m_TargetArray[dwBestMatch].m_AlarmState5 == true && m_TargetArray[dwBestMatch].m_AlarmNum5 == ii)
		//	{
		//		ExternalRectangle(pRGBIn, ForegroundArray_i, dwBestMatch, widthStep, 0, 0, 255);
		//	}
		//	*/
		//}	

		if (pAlarmInfo[ii] == 1)	//丢包
		{
			StayMonitoring(pRegion[ii], dwBestMatch, a, b, c, d, ii, pRegionInfo, pRGBIn);
			/*
			if (m_TargetArray[dwBestMatch].m_AlarmState5 == true && m_TargetArray[dwBestMatch].m_AlarmNum5 == ii)
			{
				ExternalRectangle(pRGBIn, ForegroundArray_i, dwBestMatch, widthStep, 0, 0, 255);
			}
			*/
		}	
		else
		{

		}

	}
	


	// 轨迹描绘
	//Trajectory(pRGBIn, ForegroundArray_i, dwBestMatch, width, height, widthStep);
	/*
	int dwStep = widthStep;
	unsigned char bRed   = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 3) * 85 + 85);
	unsigned char bGreen = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 7) * 36 + 36);
	unsigned char bBlue  = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 13) * 20 + 15);
	
	int dwPointNum = m_TargetArray[dwBestMatch].m_ptArray.size();
	CMPoint ptLast = m_TargetArray[dwBestMatch].m_ptArray[0];
			
	// 对目标的每一个匹配项进行遍历
	for (int l = 1; l < dwPointNum; l++)
	{
		ForegroundArray_i.m_ptCenter = m_TargetArray[dwBestMatch].m_ptArray[l];										
		if (ForegroundArray_i.m_ptCenter == ptLast)
		{
			// Draw a point
			//描绘目标中心点
			pRGBIn[ForegroundArray_i.m_ptCenter.y * dwStep + ForegroundArray_i.m_ptCenter.x * 3] = bBlue;
			pRGBIn[ForegroundArray_i.m_ptCenter.y * dwStep + ForegroundArray_i.m_ptCenter.x * 3 + 1] = bGreen;
			pRGBIn[ForegroundArray_i.m_ptCenter.y * dwStep + ForegroundArray_i.m_ptCenter.x * 3 + 2] = bRed;
		}
		else
		{
			// Draw line
			if (abs(ForegroundArray_i.m_ptCenter.x - ptLast.x) > abs(ForegroundArray_i.m_ptCenter.y - ptLast.y))
			{
				double dk = ((double) ForegroundArray_i.m_ptCenter.y - ptLast.y) / (ForegroundArray_i.m_ptCenter.x - ptLast.x);
				double b = ForegroundArray_i.m_ptCenter.y - ForegroundArray_i.m_ptCenter.x * dk;
				int colMin = ForegroundArray_i.m_ptCenter.x;
				int colMax = ptLast.x;
				if (colMin > colMax)
				{
					int dwTemp = colMin;
					colMin = colMax;
					colMax = dwTemp;
				}
				for (int col = colMin; col <= colMax; col++)
				{
					int row = (int) (dk * col + b + 0.5);
					row = row < height ? row : height - 1;
					row = row >= 0 ? row : 0;
					pRGBIn[row * dwStep + col * 3] = bBlue;
					pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
					pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					if (++row < height)
					{
						pRGBIn[row * dwStep + col * 3] = bBlue;
						pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
						pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					}
					else if ((row -= 2) >= 0)
					{
						pRGBIn[row * dwStep + col * 3] = bBlue;
						pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
						pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					}
				}
			}
			else
			{
				double dk = ((double) ForegroundArray_i.m_ptCenter.x - ptLast.x) / (ForegroundArray_i.m_ptCenter.y - ptLast.y);
				double b = ForegroundArray_i.m_ptCenter.x - ForegroundArray_i.m_ptCenter.y * dk;
				int rowMin = ForegroundArray_i.m_ptCenter.y;
				int rowMax = ptLast.y;
				if (rowMin > rowMax)
				{
					int dwTemp = rowMin;
					rowMin = rowMax;
					rowMax = dwTemp;
				}
				for (int row = rowMin; row <= rowMax; row++)
				{
					int col = (int) (dk * row + b + 0.5);
					col = col < width? col : width - 1;
					col = col >= 0 ? col : 0;
					pRGBIn[row * dwStep + col * 3] = bBlue;
					pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
					pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					if (++col < width)
					{
						pRGBIn[row * dwStep + col * 3] = bBlue;
						pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
						pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					}
					else if ((col -= 2) >= 0)
					{
						pRGBIn[row * dwStep + col * 3] = bBlue;
						pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
						pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					}
				}
			}
		}
		ptLast = ForegroundArray_i.m_ptCenter;
	}
	*/
}

/*
//外接矩形描绘
void RegionSurveillance::ExternalRectangle(unsigned char* pRGBIn, CForeground ForegroundArray_i, int dwBestMatch, int widthStep, 
	int rgb_b, int rgb_g, int rgb_r)
{
	int dwStep = widthStep;

	// 前景目标外接矩形描绘
	for (int t = ForegroundArray_i.m_rtConnect.left; t <= ForegroundArray_i.m_rtConnect.right; t++)
	{
		pRGBIn[ForegroundArray_i.m_rtConnect.top * dwStep + t * 3] = rgb_b;
		pRGBIn[ForegroundArray_i.m_rtConnect.top * dwStep + t * 3 + 1] = rgb_g;
		pRGBIn[ForegroundArray_i.m_rtConnect.top * dwStep + t * 3 + 2] = rgb_r;
		pRGBIn[ForegroundArray_i.m_rtConnect.bottom * dwStep + t * 3] = rgb_b;
		pRGBIn[ForegroundArray_i.m_rtConnect.bottom * dwStep + t * 3 + 1] = rgb_g;
		pRGBIn[ForegroundArray_i.m_rtConnect.bottom * dwStep + t * 3 + 2] = rgb_r;
	}
	for (int t = ForegroundArray_i.m_rtConnect.top; t < ForegroundArray_i.m_rtConnect.bottom; t++)
	{
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.left * 3] = rgb_b;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.left * 3 + 1] = rgb_g;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.left * 3 + 2] = rgb_r;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.right * 3] = rgb_b;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.right * 3 + 1] = rgb_g;
		pRGBIn[t * dwStep + ForegroundArray_i.m_rtConnect.right * 3 + 2] = rgb_r;
	}
}

void RegionSurveillance::Trajectory(unsigned char* pRGBIn, CForeground ForegroundArray_i, int dwBestMatch, int width, int height, int widthStep)
{
	// 轨迹描绘
	int dwStep = widthStep;
	unsigned char bRed   = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 3) * 85 + 85);
	unsigned char bGreen = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 7) * 36 + 36);
	unsigned char bBlue  = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 13) * 20 + 15);

	int dwPointNum = m_TargetArray[dwBestMatch].m_ptArray.size();
	CMPoint ptLast = m_TargetArray[dwBestMatch].m_ptArray[0];

	// 对目标的每一个匹配项进行遍历
	for (int l = 1; l < dwPointNum; l++)
	{
		ForegroundArray_i.m_ptCenter = m_TargetArray[dwBestMatch].m_ptArray[l];										
		if (ForegroundArray_i.m_ptCenter == ptLast)
		{
			// Draw a point
			//描绘目标中心点
			pRGBIn[ForegroundArray_i.m_ptCenter.y * dwStep + ForegroundArray_i.m_ptCenter.x * 3] = bBlue;
			pRGBIn[ForegroundArray_i.m_ptCenter.y * dwStep + ForegroundArray_i.m_ptCenter.x * 3 + 1] = bGreen;
			pRGBIn[ForegroundArray_i.m_ptCenter.y * dwStep + ForegroundArray_i.m_ptCenter.x * 3 + 2] = bRed;
		}
		else
		{
			// Draw line
			if (abs(ForegroundArray_i.m_ptCenter.x - ptLast.x) > abs(ForegroundArray_i.m_ptCenter.y - ptLast.y))
			{
				double dk = ((double) ForegroundArray_i.m_ptCenter.y - ptLast.y) / (ForegroundArray_i.m_ptCenter.x - ptLast.x);
				double b = ForegroundArray_i.m_ptCenter.y - ForegroundArray_i.m_ptCenter.x * dk;
				int colMin = ForegroundArray_i.m_ptCenter.x;
				int colMax = ptLast.x;
				if (colMin > colMax)
				{
					int dwTemp = colMin;
					colMin = colMax;
					colMax = dwTemp;
				}
				for (int col = colMin; col <= colMax; col++)
				{
					int row = (int) (dk * col + b + 0.5);
					row = row < height ? row : height - 1;
					row = row >= 0 ? row : 0;
					pRGBIn[row * dwStep + col * 3] = bBlue;
					pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
					pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					if (++row < height)
					{
						pRGBIn[row * dwStep + col * 3] = bBlue;
						pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
						pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					}
					else if ((row -= 2) >= 0)
					{
						pRGBIn[row * dwStep + col * 3] = bBlue;
						pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
						pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					}
				}
			}
			else
			{
				double dk = ((double) ForegroundArray_i.m_ptCenter.x - ptLast.x) / (ForegroundArray_i.m_ptCenter.y - ptLast.y);
				double b = ForegroundArray_i.m_ptCenter.x - ForegroundArray_i.m_ptCenter.y * dk;
				int rowMin = ForegroundArray_i.m_ptCenter.y;
				int rowMax = ptLast.y;
				if (rowMin > rowMax)
				{
					int dwTemp = rowMin;
					rowMin = rowMax;
					rowMax = dwTemp;
				}
				for (int row = rowMin; row <= rowMax; row++)
				{
					int col = (int) (dk * row + b + 0.5);
					col = col < width? col : width - 1;
					col = col >= 0 ? col : 0;
					pRGBIn[row * dwStep + col * 3] = bBlue;
					pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
					pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					if (++col < width)
					{
						pRGBIn[row * dwStep + col * 3] = bBlue;
						pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
						pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					}
					else if ((col -= 2) >= 0)
					{
						pRGBIn[row * dwStep + col * 3] = bBlue;
						pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
						pRGBIn[row * dwStep + col * 3 + 2] = bRed;
					}
				}
			}
		}
		ptLast = ForegroundArray_i.m_ptCenter;
	}

}
*/


/******************************************************************************
* Function:        RAccessToRestrictedAreas
* Description:     进入禁区监测
* Input:           pRegion_i			感兴趣区域标记
                   dwBestMatch			目标索引
				   ForBottom			当前前景外接矩形纵坐标最小值
				   ForTop				当前前景外接矩形纵坐标最大值
				   ForLeft				当前前景外接矩形横坐标最小值
				   ForRight				当前前景外接矩形横坐标最大值
				   indexROI				ROI 索引
* Output:          m_TargetArray		目标信息        
*******************************************************************************/
void RegionSurveillance::AccessToRestrictedAreas(bool *pRegion_i, int dwBestMatch, int ForBottom, int ForTop, int ForLeft, int ForRight, int indexROI)
{

	// 目标外接矩形有一个边进入禁区且外接矩形中心点位于禁区内
	//if (pRegion_i[GlobelWidth * ForBottom + ForLeft] ||
	//	pRegion_i[GlobelWidth * ForTop + ForLeft] ||
	//	pRegion_i[GlobelWidth * ForTop + ForRight] ||
	//	pRegion_i[GlobelWidth * ForBottom + ForRight])
	{
		//将判断是否进入感兴趣区域改成了矩形中心点的上下左右和中心五个点都进入区域,判定为进入
		if (pRegion_i[GlobelWidth * (int) ((ForBottom + ForTop) / 2) + (ForLeft + ForRight) / 2] && \
			pRegion_i[GlobelWidth * (int)((ForBottom + ForTop) / 2) + (ForLeft + ForRight) / 2 + 1] && \
			pRegion_i[GlobelWidth * (int)((ForBottom + ForTop) / 2) + (ForLeft + ForRight) / 2 - 1] && \
			pRegion_i[GlobelWidth * (int)((ForBottom + ForTop) / 2+1) + (ForLeft + ForRight) / 2] &&\
			pRegion_i[GlobelWidth * (int)((ForBottom + ForTop) / 2-1) + (ForLeft + ForRight) / 2])
		{
			m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
			m_TargetArray[dwBestMatch].pbAlarmType[indexROI][0] = 1;
			return;
		}
	}	

	m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = false;
	m_TargetArray[dwBestMatch].pbAlarmType[indexROI][0] = 0;
	
}

//离开禁区
void RegionSurveillance::LeaveTheRestrictedAreas(bool *pRegion_i, int dwBestMatch, int ForBottom, int ForTop, int ForLeft, int ForRight, int indexROI)
{
	if (pRegion_i[GlobelWidth * ForBottom + ForLeft] ||
		pRegion_i[GlobelWidth * ForTop + ForLeft] ||
		pRegion_i[GlobelWidth * ForTop + ForRight] ||
		pRegion_i[GlobelWidth * ForBottom + ForRight])
	{
		if (pRegion_i[GlobelWidth * (int) ((ForBottom + ForTop) / 2) + (ForLeft + ForRight) / 2] == 0 && 
			pRegion_i[GlobelWidth * (int)m_TargetArray[dwBestMatch].m_PredictionPoint.y + (int)m_TargetArray[dwBestMatch].m_PredictionPoint.x] == 0)
		{
			bool flag = 0;
			int i;
			int tar_arr_size = m_TargetArray[dwBestMatch].m_ptArray.size();
			i = tar_arr_size - 1;
			while (i >= 0 && i >= tar_arr_size - (ForRight - ForLeft)/2)
			{
				if (pRegion_i[GlobelWidth * (int) (m_TargetArray[dwBestMatch].m_ptArray[i].y) + m_TargetArray[dwBestMatch].m_ptArray[i].x] == 1)
				{
					flag = 1;
					break;
				}

				i--;
			}
			if (flag == 1)
			{
				m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
				m_TargetArray[dwBestMatch].pbAlarmType[indexROI][1] = 2;
				return;
			}
		}
	}

	m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = false;
	m_TargetArray[dwBestMatch].pbAlarmType[indexROI][1] = 0;
}

//单向越界
//dir: 1 正方向(相对于通常坐标系)  0负方向
void RegionSurveillance::OneWayCrossBorder(bool *pRegion_i, int dwBestMatch, int ForBottom, int ForTop, int ForLeft, int ForRight, int indexROI, RegionInfo* pRegionInfo)
{
	// 只要目标外接矩形有一个点进入区域则报警
	int tar_size = m_TargetArray[dwBestMatch].m_ptArray.size();
 	int xmin = min(ForLeft, ForRight), xmax = max(ForLeft, ForRight), ymin = min(ForBottom, ForTop), ymax = max(ForBottom, ForTop);
	for (int iy = ymin; iy <= ymax; iy++)
	{
		for (int ix = xmin; ix <= xmax; ix++)
		{
			if (pRegion_i[GlobelWidth * iy + ix] && tar_size >= 4)
			{
				int point_x, point_y;
				point_x = m_TargetArray[dwBestMatch].m_ptArray[tar_size - 4].x;
				point_y = m_TargetArray[dwBestMatch].m_ptArray[tar_size - 4].y;

				// 判断方向
				if (indexROI >= 0 && indexROI < m_numROI && pRegionInfo[indexROI].nPointNum >= 2)
				{
					if (abs(pRegionInfo[indexROI].pROI[0].x - pRegionInfo[indexROI].pROI[1].x) >= 1)
					{
						double k, b;
						k = (double)(pRegionInfo[indexROI].pROI[0].y - pRegionInfo[indexROI].pROI[1].y) / (pRegionInfo[indexROI].pROI[0].x - pRegionInfo[indexROI].pROI[1].x);
						b = (double)(pRegionInfo[indexROI].pROI[0].x * pRegionInfo[indexROI].pROI[1].y - pRegionInfo[indexROI].pROI[1].x * pRegionInfo[indexROI].pROI[0].y) / 
							(pRegionInfo[indexROI].pROI[0].x - pRegionInfo[indexROI].pROI[1].x);
						
						double dist_c, dist_p;
						
						dist_c = fabs(k * m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].x - m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].y + b) / sqrt(k * k + 1.0);
						dist_p = fabs(k * point_x - point_y + b) / sqrt(k * k + 1.0);

						if (abs(dist_c - dist_p) < 10)
						{
							int pre_index = tar_size - 5;
							while (pre_index >= 0)
							{
								point_x = m_TargetArray[dwBestMatch].m_ptArray[pre_index].x;
								point_y = m_TargetArray[dwBestMatch].m_ptArray[pre_index].y;
								dist_p = fabs(k * point_x - point_y + b) / sqrt(k * k + 1.0);
								pre_index = pre_index - 1;
								if (abs(dist_c - dist_p) >= 10)
								{
									break;
								}
							}
						}


						if (abs(dist_c - dist_p) < 10)
						{
							m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = false;
							m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 0;
							return;
						}

						bool flag_c, flag_p;
						if (k * m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].x + b >= m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].y)
						{
							flag_c = 1;
						}
						else
						{
							flag_c = 0;
						}

						if (k *point_x + b >= point_y)
						{
							flag_p = 1;
						}
						else
						{
							flag_p = 0;
						}

		
						//if (pRegionInfo[indexROI].dir == 1)
						if (pRegionInfo[indexROI].finaldir == 1)	//by zl 20150914
						{
							if (flag_c * flag_p == 1)
							{
								if (dist_c <= dist_p)
								{
									m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
									m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
									return;
								}	
							}
							else if (flag_c == 0 && flag_p == 1)
							{
								m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
								m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
								return;
							}
							else if (flag_c ==0 && flag_p == 0)
							{
								if (dist_p <= dist_c)
								{
									m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
									m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
									return;
								}
							}
	
						} 
						else
						{
							if (flag_c == 0 && flag_p == 0)
							{
								if (dist_c <= dist_p)
								{
									m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
									m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
									return;
								}	
							}
							else if (flag_c == 1 && flag_p == 0)
							{
								m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
								m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
								return;
							}
							else if (flag_c == 1 && flag_p == 1)
							{
								if (dist_p <= dist_c)
								{
									m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
									m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
									return;
								}
							}
						}
					} 
					else
					{
						double k, b;
						k = 0;
						b = (double)(pRegionInfo[indexROI].pROI[0].x);

						double dist_c, dist_p;

						dist_c = fabs(m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].x - b);
						dist_p = fabs(point_x - b);

						if (abs(dist_c - dist_p) < 10)
						{
							int pre_index = tar_size - 5;
							while (pre_index >= 0)
							{
								point_x = m_TargetArray[dwBestMatch].m_ptArray[pre_index].x;
								dist_p = fabs(point_x - b);
								pre_index = pre_index - 1;
								if (abs(dist_c - dist_p) >= 10)
								{
									break;
								}
							}
						}
						
						bool flag_c, flag_p;
						if (m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].x >= b)
						{
							flag_c = 1;
						}
						else
						{
							flag_c = 0;
						}

						if (point_x >= b)
						{
							flag_p = 1;
						}
						else
						{
							flag_p = 0;
						}

						if (abs(dist_c - dist_p) < 10)
						{
							m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = false;
							m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 0;
							return;
						}
						//if (pRegionInfo[indexROI].dir == 1)
						if (pRegionInfo[indexROI].finaldir == 1)	//by zl 20150914
						{
							if (flag_c * flag_p == 1)
							{
								if (dist_c <= dist_p)
								{
									m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
									m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
									return;
								}	
							}
							else if (flag_c == 0 && flag_p == 1)
							{
								m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
								m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
								return;
							}
							else if (flag_c == 0 && flag_p == 0)
							{
								if (dist_p <= dist_c)
								{
									m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
									m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
									return;
								}
							}

						} 
						else
						{
							if (flag_c == 0 && flag_p == 0)
							{
								if (dist_c <= dist_p)
								{
									m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
									m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
									return;
								}	
							}
							else if (flag_c == 1 && flag_p == 0)
							{
								m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
								m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
								return;
							}
							else if (flag_c == 1 && flag_p == 1)
							{
								if (dist_p <= dist_c)
								{
									m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
									m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 3;
									return;
								}
							}
						}

						
					}
				}
			}
		}
	}

	m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = false;
	m_TargetArray[dwBestMatch].pbAlarmType[indexROI][2] = 0;
}

//双向越界
void RegionSurveillance::TwoWayCrossBorder(bool *pRegion_i, int dwBestMatch, int ForBottom, int ForTop, int ForLeft, int ForRight, int indexROI)
{
	// 只要目标外接矩形有一个点进入区域则报警
	int xmin = min(ForLeft, ForRight), xmax = max(ForLeft, ForRight), ymin = min(ForBottom, ForTop), ymax = max(ForBottom, ForTop);
	for (int iy = ymin; iy <= ymax; iy++)
	{
		for (int ix = xmin; ix <= xmax; ix++)
		{
			if (pRegion_i[GlobelWidth * iy + ix])
			{
				m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
				m_TargetArray[dwBestMatch].pbAlarmType[indexROI][3] = 4;
				return;
			}
		}
	}
	m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = false;
	m_TargetArray[dwBestMatch].pbAlarmType[indexROI][3] = 0;
}

////双向越界
//void RegionSurveillance::TwoWayCrossBorder(bool *pRegion_i, int dwBestMatch, int ForBottom, int ForTop, int ForLeft, int ForRight, int indexROI)
//{
//	// 只要目标外接矩形有一个点进入区域则报警
//	int xmin = min(ForLeft, ForRight), xmax = max(ForLeft, ForRight), ymin = min(ForBottom, ForTop),  ymax= max(ForBottom, ForTop);
//	for (int iy = ymin; iy <= ymax; iy++)
//	{
//		for (int ix = xmin; ix <= xmax; ix++)
//		{
//			if (pRegion_i[GlobelWidth * iy + ix])
//			{
//				m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
//				m_TargetArray[dwBestMatch].pbAlarmType[indexROI] = 4;
//				return;
//			}
//		} 
//	}
//    m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = false;
//	m_TargetArray[dwBestMatch].pbAlarmType[indexROI] = -1;
//}

// 监测遗留或移除 0 遗留 1移除
bool RegionSurveillance::detectLeftRemove(unsigned char* pRGBIn, int ForBottom, int ForTop, int ForLeft, int ForRight)
{
	bool flag;
	int surround_b, surround_t, surround_l, surround_r;
	int i, j;
	int temp;

	if (ForTop > ForBottom)
	{
		temp = ForBottom;
		ForBottom = ForTop;
		ForTop = temp;
	}

	if (ForLeft > ForRight)
	{
		temp = ForRight;
		ForRight = ForLeft;
		ForLeft = temp;
	}

	surround_l = max(0, ForLeft - 15);
	surround_t = max(0, ForTop - 15);
    surround_b = min(GlobelHeight - 1, ForBottom + 15);
	surround_r = min(GlobelHeight - 1, ForRight + 15);
	
	double s1[3], s2[3];
	int num;

	s1[0] = 0;
	s1[1] = 0;
	s1[2] = 0;
	
	num = 0;
	for (i = ForTop; i <= ForBottom; i++)
	{
		for (j = ForLeft; j <= ForRight; j++)
		{
			s1[0] += pRGBIn[GlobelWidthstep * i + 3 * j];
			s1[1] += pRGBIn[GlobelWidthstep * i + 3 * j + 1];
			s1[2] += pRGBIn[GlobelWidthstep * i + 3 * j + 2];
			num ++;
		}
	}
    if (num >= 1)
    {
		s1[0] /= num;
		s1[1] /= num;
		s1[2] /= num;
	}
	else
	{
		return 1;
	}

	double dist1;
	dist1 = sqrt(s1[0] * s1[0] + s1[1] * s1[1] + s1[2] * s1[2]);

	s2[0] = 0;
	s2[1] = 0;
	s2[2] = 0;

	num = 0;
	for (i = surround_t; i <= surround_b; i++)
	{
		for (j = surround_l; j <= surround_r; j++)
		{
			s2[0] += pRGBIn[GlobelWidthstep * i + 3 * j];
			s2[1] += pRGBIn[GlobelWidthstep * i + 3 * j + 1];
			s2[2] += pRGBIn[GlobelWidthstep * i + 3 * j + 2];
			num ++;
		}
	}
	if (num >= 1)
	{
		s2[0] /= num;
		s2[1] /= num;
		s2[2] /= num;
	}
	else
	{
		return 1;
	}

	double dist2;
	dist2 = sqrt(s2[0] * s2[0] + s2[1] * s2[1] + s2[2] * s2[2]);

	double ratio = 0;
	if (dist1 >= dist2)
	{
		if (fabs(dist1) < 0.0001)
		{
			ratio = 1.0;
		}
		else
		{
			ratio =  dist2 / dist1;
		}
		
	} 
	else
	{
		if (fabs(dist2) < 0.0001)
		{
			ratio = 1.0;
		} 
		else
		{
			ratio = dist1 / dist2;
		}
	}
	
	if (ratio >= 0.85 && ratio <= 1.0) //相近表示移除
	{
		flag = 1;
	} 
	else // 否则表示遗留
	{
		flag = 0;
	}

	return flag;
}

bool CTargetFunc (CTarget &a, CTarget &b)
{

	if (a.m_CurRefPoint.x == b.m_CurRefPoint.x && a.m_CurRefPoint.y == b.m_CurRefPoint.y)
		return true;
	else
		return false;
}
// 徘徊监测
void RegionSurveillance::WanderMonitoring(bool *pRegion_i, int dwBestMatch, int ForBottom, int ForTop, int ForLeft, int ForRight, int indexROI, RegionInfo* pRegionInfo, unsigned char* pRGBIn)
{
	// 目标外接矩形有一个边进入禁区且外接矩形中心点位于禁区内
	bool flag = 0;
	
	if ((pRegion_i[GlobelWidth * ForBottom + ForLeft] ||
		pRegion_i[GlobelWidth * ForTop + ForLeft] ||
		pRegion_i[GlobelWidth * ForTop + ForRight] ||
		pRegion_i[GlobelWidth * ForBottom + ForRight]) && (pRegion_i[GlobelWidth * (int)((ForBottom + ForTop) / 2) + (ForLeft + ForRight) / 2])&& (indexROI >= 0 && indexROI < m_numROI))
	{
		//cout << dwBestMatch << endl;
		if (m_TargetArray[dwBestMatch].nFrameCurr == 0)
		{
			m_TargetArray[dwBestMatch].nFrameCurr = 1;
			m_TargetArray[dwBestMatch].lastFrame = 0;
		}
		else
		{
			if (m_TargetArray[dwBestMatch].lastFrame <= 5)
			{
				m_TargetArray[dwBestMatch].nFrameCurr++;
				m_TargetArray[dwBestMatch].lastFrame = 0;
			} 
			else
			{
				m_TargetArray[dwBestMatch].nFrameCurr = 0;
				m_TargetArray[dwBestMatch].lastFrame = 0;
			}
		}
		if ((1 == m_TargetArray[dwBestMatch].m_ptArray.size() && m_TargetArray[dwBestMatch].nFrameCurr == 1) && m_TargetArray[dwBestMatch].m_bFirstInWander == true )     //因在徘徊区域内,检测目标对象判定为新对象,将该新对象的nFrameCurr,设置为该区域内最接近的对象的nFrameCurr
		{
			double bestDistance = 0;
			CTarget best;
			int index = 0;
			for (auto item : indexInWander)
			{
				auto distance = sqrt((m_TargetArray[dwBestMatch].m_CurRefPoint.x - item.m_PredictionPoint.x) *
					(m_TargetArray[dwBestMatch].m_CurRefPoint.x - item.m_PredictionPoint.x) +
					(m_TargetArray[dwBestMatch].m_CurRefPoint.y - item.m_PredictionPoint.y) *
					(m_TargetArray[dwBestMatch].m_CurRefPoint.y - item.m_PredictionPoint.y));
				if (0 == bestDistance)
				{
					bestDistance = distance;
					best = item;
				}
				else if (bestDistance > distance)
				{
					bestDistance = distance;
					best = item;
				}
				++index;
			}
			if (indexInWander.empty())
			{
				
			}
			else
			{
				auto CTargetFunc = [best](CTarget &a)->bool {
					if (best.m_dwID == a.m_dwID)
						return true;
					else
						return false;
				};
				m_TargetArray[dwBestMatch].nFrameCurr = best.nFrameCurr;
				m_TargetArray[dwBestMatch].m_dwID = best.m_dwID;
				auto iter = std::find_if(indexInWander.begin(), indexInWander.end(), CTargetFunc);
				if (iter != indexInWander.end())
					indexInWander.erase(iter);
			}
	
		}
		if (m_TargetArray[dwBestMatch].nFrameCurr >= pRegionInfo[indexROI].nFrameNum)
		{
			m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
			m_TargetArray[dwBestMatch].pbAlarmType[indexROI][4] = 5;
			flag = 1;

			CTarget best = m_TargetArray[dwBestMatch];
			auto CTargetFunc = [best](CTarget &a)->bool {
				if (best.m_dwID == a.m_dwID)
					return true;
				else
					return false;
			};
			auto iter = std::find_if(indexInWander.begin(), indexInWander.end(), CTargetFunc);
			if (iter != indexInWander.end())
				indexInWander.erase(iter);
			indexInWander.push_back(m_TargetArray[dwBestMatch]);
			//cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << dwBestMatch << endl;
		}
		else
		{
			CTarget best = m_TargetArray[dwBestMatch];
			auto CTargetFunc = [best](CTarget &a)->bool {
				if (best.m_dwID == a.m_dwID)
					return true;
				else
					return false;
			};
			auto iter = std::find_if(indexInWander.begin(), indexInWander.end(), CTargetFunc);
			if (iter != indexInWander.end())
				indexInWander.erase(iter);
		}
		m_TargetArray[dwBestMatch].m_bFirstInWander = false;


		// 判断模板是否进行更新  0 遗留 1移除
		left_flag_now_w = detectLeftRemove(pRGBIn, ForBottom, ForTop, ForLeft, ForRight);
	}
	else
	{
		left_flag_now_w = 1;
		//cout << dwBestMatch << endl;
		CTarget best = m_TargetArray[dwBestMatch];
		auto CTargetFunc = [best](CTarget &a)->bool {
			if (best.m_dwID == a.m_dwID)
				return true;
			else
				return false;
		};
		auto iter = std::find_if(indexInWander.begin(), indexInWander.end(), CTargetFunc);
		if(iter != indexInWander.end())
		indexInWander.erase(iter);
	}
	if (flag == 0)
	{
		m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = false;
		m_TargetArray[dwBestMatch].pbAlarmType[indexROI][4] = 0;
	}
	
	m_TargetArray[dwBestMatch].lastFrame++;
}


// 丢包监测
void RegionSurveillance::StayMonitoring(bool *pRegion_i, int dwBestMatch, int ForBottom, int ForTop, int ForLeft, int ForRight, int indexROI, RegionInfo* pRegionInfo, unsigned char* pRGBIn)
{
	// 目标外接矩形有一个边进入禁区且外接矩形中心点位于禁区内
	int tar_size = m_TargetArray[dwBestMatch].m_ptArray.size();
	int cur_dis;
	bool flag = 0;
	/*if (pRegion_i[GlobelWidth * ForBottom + ForLeft] ||
		pRegion_i[GlobelWidth * ForTop + ForLeft] ||
		pRegion_i[GlobelWidth * ForTop + ForRight] ||
		pRegion_i[GlobelWidth * ForBottom + ForRight])*/
	{
		//if (pRegion_i[GlobelWidth * (int) ((ForBottom + ForTop) / 2) + (ForLeft + ForRight) / 2])
		{
			if (indexROI >= 0 && indexROI < m_numROI)
			{
				if (m_TargetArray[dwBestMatch].nFrameCurr == 0)
				{
					m_TargetArray[dwBestMatch].nFrameCurr = 1;
					m_TargetArray[dwBestMatch].lastFrame = 0;
				}
				else
				{
					if (tar_size >= 2)
					{
						cur_dis = (m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].x - m_TargetArray[dwBestMatch].m_ptArray[tar_size - 2].x) * (m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].x - m_TargetArray[dwBestMatch].m_ptArray[tar_size - 2].x) + 
							          (m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].y - m_TargetArray[dwBestMatch].m_ptArray[tar_size - 2].y) * (m_TargetArray[dwBestMatch].m_ptArray[tar_size - 1].y - m_TargetArray[dwBestMatch].m_ptArray[tar_size - 2].y);
					}
					else
					{
						cur_dis = 0;
					}
					if (m_TargetArray[dwBestMatch].lastFrame <= 5 && cur_dis <= tar_size * tar_size)
					{
						m_TargetArray[dwBestMatch].nFrameCurr++;
						m_TargetArray[dwBestMatch].lastFrame = 0;
					} 
					else
					{
						m_TargetArray[dwBestMatch].nFrameCurr = 0;
						m_TargetArray[dwBestMatch].lastFrame = 0;
					}
				}
				
				if (m_TargetArray[dwBestMatch].nFrameCurr >= pRegionInfo[indexROI].nFrameNum)
				{
					
					m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = true;
					m_TargetArray[dwBestMatch].pbAlarmType[indexROI][5] = 6;
					flag = 1;
				}
			}
		}

		// 判断模板是否进行更新  0 遗留 1移除
		left_flag_now_s = detectLeftRemove(pRGBIn, ForBottom, ForTop, ForLeft, ForRight);
	}
	/*else
	{
		left_flag_now_s = 1;
	}*/

	if (flag == 0)
	{
		m_TargetArray[dwBestMatch].pbAlarmState[indexROI] = false;
		m_TargetArray[dwBestMatch].pbAlarmType[indexROI][5] = 0;
	}

	m_TargetArray[dwBestMatch].lastFrame++;
}

void RegionSurveillance::RSrelease()
{
	
	if (pConExtraction)
	{
		delete pConExtraction;
		pConExtraction = NULL;
	}
	

	for (int i = 0; i < MAXROINUM; i++)
	{
		if (pRegion[i] !=NULL)
		{
			delete[] pRegion[i];
		}
	}
	
	if (pRegion != NULL)
	{
		delete[] pRegion;
		pRegion = NULL;
	}

	if (pAlarmInfo != NULL)
	{
		delete[] pAlarmInfo;
		pAlarmInfo = NULL;
	}

	// 释放m_TargetArray
    int i;
	for (i = 0; i < (int)m_TargetArray.size(); i++)
	{
		m_TargetArray[i].m_ptArray.clear();
		vector<CMPoint>().swap(m_TargetArray[i].m_ptArray);
		{
			std::vector<CMPoint>tmp = m_TargetArray[i].m_ptArray;
			m_TargetArray[i].m_ptArray.swap(tmp);
		}
	}

	m_TargetArray.clear();
	m_FinalArray.clear();	

	//m_TargetArray.swap(vector<CTarget>(0));
	//m_FinalArray.swap(vector<CTarget>(0));

	if (MaskImgData != NULL)
	{
		free(MaskImgData);
		MaskImgData = NULL;
	}
	
	if (greyImgData != NULL)
	{
		free(greyImgData);
		greyImgData = NULL;
	}
	
	if (vbM->bg_samples != NULL)
	{
		free(vbM->bg_samples);
		vbM->bg_samples = NULL;
	}

	if (vbM != NULL)
	{
		free(vbM);
		vbM = NULL;
	}
	FreeRand();
	FreeRandNeighbor();

	if(maskImg)
	{
		delete [] maskImg;
		maskImg = NULL;
	}
	if(VBPARAM)
		delete VBPARAM;
	VBPARAM = NULL;
}

void RegionSurveillance::VibeModelFree(VibeModel_t *model)
{
	free(model->bg_samples);
	if (model != NULL)
	{
		free(model);
		model = NULL;
	}

	FreeRand();
	FreeRandNeighbor();
}




//{
//	CvSize a;
//	//a.width = GlobelWidth;
//	//a.height = GlobelHeight;
//	//IplImage *im = cvCreateImage(a, 8, 1);
//	//im->imageData = (char*)image2.data;

//	//IplImage *img_3 = cvCreateImage(cvGetSize(im), 8, 3); //创建3通道图像
//	//cvCvtColor(im, img_3, CV_GRAY2BGR);//单通道转3通道

//	IplImage *img_3;
//	img_3 = &IplImage(image2);

//	for (int i = 0; i < ForegroundArray.size(); i++)
//	{
//		//cvRectangle(img_3, CvPoint(foreContours[i].left, foreContours[i].top),
//		//	CvPoint(foreContours[i].right, foreContours[i].bottom), CV_RGB(0, 255, 0), 2);	//绿色画框
//		cvRectangle(img_3, CvPoint(ForegroundArray[i].m_rtConnect.left, ForegroundArray[i].m_rtConnect.top),
//			CvPoint(ForegroundArray[i].m_rtConnect.right, ForegroundArray[i].m_rtConnect.bottom), CV_RGB(0, 255, 0), 2);	//绿色画框
//	}

//	a.width = GlobelWidth / 2;
//	a.height = GlobelHeight / 2;
//	IplImage *desc = cvCreateImage(a, img_3->depth, img_3->nChannels);
//	cvResize(img_3, desc, CV_INTER_CUBIC);

//	cvShowImage("L2", img_3);
//	cvWaitKey(1);
//}

/*
// Draw it
int dwPointNum = m_TargetArray[dwBestMatch].m_ptArray.size();

if (dwPointNum >= m_dwMinFrame)
{
unsigned char bRed   = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 3) * 85 + 85);
unsigned char bGreen = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 7) * 36 + 36);
unsigned char bBlue  = (unsigned char) ((m_TargetArray[dwBestMatch].m_dwID % 13) * 20 + 15);

// Draw rectangle
//横线
// 目标外接矩形 红色
for (int t = ForegroundArray[i].m_rtConnect.left; t <= ForegroundArray[i].m_rtConnect.right; t++)
{
pRGBIn[ForegroundArray[i].m_rtConnect.top * dwStep + t * 3] = 0;
pRGBIn[ForegroundArray[i].m_rtConnect.top * dwStep + t * 3 + 1] = 0;
pRGBIn[ForegroundArray[i].m_rtConnect.top * dwStep + t * 3 + 2] = 255;
pRGBIn[ForegroundArray[i].m_rtConnect.bottom * dwStep + t * 3] = 0;
pRGBIn[ForegroundArray[i].m_rtConnect.bottom * dwStep + t * 3 + 1] = 0;
pRGBIn[ForegroundArray[i].m_rtConnect.bottom * dwStep + t * 3 + 2] = 255;
}
for (int t = ForegroundArray[i].m_rtConnect.top; t < ForegroundArray[i].m_rtConnect.bottom; t++)
{
pRGBIn[t * dwStep + ForegroundArray[i].m_rtConnect.left * 3] = 0;
pRGBIn[t * dwStep + ForegroundArray[i].m_rtConnect.left * 3 + 1] = 0;
pRGBIn[t * dwStep + ForegroundArray[i].m_rtConnect.left * 3 + 2] = 255;
pRGBIn[t * dwStep + ForegroundArray[i].m_rtConnect.right * 3] = 0;
pRGBIn[t * dwStep + ForegroundArray[i].m_rtConnect.right * 3 + 1] = 0;
pRGBIn[t * dwStep + ForegroundArray[i].m_rtConnect.right * 3 + 2] = 255;
}

//-----------------------------
int a = ForegroundArray[i].m_rtConnect.bottom;
int b = ForegroundArray[i].m_rtConnect.top;
int c = ForegroundArray[i].m_rtConnect.left;
int d = ForegroundArray[i].m_rtConnect.right;


// ROI 监控
for (int ii = 0; ii < m_numROI; ii++)
{
// 目标外接矩形进行区域且目标中心点在区域内,则报警
if (pAlarmInfo[ii] == 1)	//禁止区域
{
if (pRegion[ii][GlobelWidth * a + c] ||
pRegion[ii][GlobelWidth * b + c] ||
pRegion[ii][GlobelWidth * b + d] ||
pRegion[ii][GlobelWidth * a + d])
{
if (pRegion[ii][GlobelWidth * (int) ((a + b) / 2) + (c + d) / 2])
{
//printf("alarm 1!\n");
m_TargetArray[dwBestMatch].m_AlarmState = true;
m_TargetArray[dwBestMatch].m_AlarmNum = ii;
//printf("%d",m_TargetArray[dwBestMatch].m_AlarmNum );
}
}
}
// 只要目标外接矩形有一个点进入区域则报警
if (pAlarmInfo[ii] == 2) //拌线
{
int xmin = min(c, d), xmax = max(c, d), ymin = min(a, b), ymax = max(a, b);
for (int iy = ymin; iy <= ymax; iy++)
{
for (int ix = xmin; ix <= xmax; ix++)
{
if (pRegion[ii][GlobelWidth * iy + ix])
{
m_TargetArray[dwBestMatch].m_AlarmState = true;
m_TargetArray[dwBestMatch].m_AlarmNum = ii;
break;
}
}
}
}

// 只要目标外接矩形进入区域中,且面积大于ROI区域面积的一半同时外接矩形中心点在区域内,则报警
if (pAlarmInfo[ii] == 3)	//遗留检测
{
int TotalArea = 0;
if (TotalArea == 0)
{
for (int ai = 0; ai < GlobelHeight * GlobelWidth; ai++)
{
if (pRegion[ii][ai] == 1)
{
TotalArea++;
}
}
}
if (pRegion[ii][GlobelWidth * a + c] ||
pRegion[ii][GlobelWidth * b + c] ||
pRegion[ii][GlobelWidth * b + d] ||
pRegion[ii][GlobelWidth * a + d])
{
if (abs(a - b) * abs(c - d) >= (TotalArea / 2) && (pRegion[ii][GlobelWidth * (int) ((a + b) / 2) + (c + d) / 2]))
{
//printf("alarm 3!\n");
m_TargetArray[dwBestMatch].m_AlarmState = true;
m_TargetArray[dwBestMatch].m_AlarmNum = ii;
}
}
}
}

//保存最终输出目标
m_FinalArray.push_back(m_TargetArray[dwBestMatch]);

CMPoint ptLast = m_TargetArray[dwBestMatch].m_ptArray[0];

// 对目标的每一个匹配项进行遍历
for (int l = 1; l < dwPointNum; l++)
{
ForegroundArray[i].m_ptCenter = m_TargetArray[dwBestMatch].m_ptArray[l];
if (ForegroundArray[i].m_ptCenter == ptLast)
{
// Draw a point
//描绘目标中心点
pRGBIn[ForegroundArray[i].m_ptCenter.y * dwStep + ForegroundArray[i].m_ptCenter.x * 3] = bBlue;
pRGBIn[ForegroundArray[i].m_ptCenter.y * dwStep + ForegroundArray[i].m_ptCenter.x * 3 + 1] = bGreen;
pRGBIn[ForegroundArray[i].m_ptCenter.y * dwStep + ForegroundArray[i].m_ptCenter.x * 3 + 2] = bRed;
}
else
{
// Draw line
if (abs(ForegroundArray[i].m_ptCenter.x - ptLast.x) > abs(ForegroundArray[i].m_ptCenter.y - ptLast.y))
{
double dk = ((double) ForegroundArray[i].m_ptCenter.y - ptLast.y) / (ForegroundArray[i].m_ptCenter.x - ptLast.x);
double b = ForegroundArray[i].m_ptCenter.y - ForegroundArray[i].m_ptCenter.x * dk;
int colMin = ForegroundArray[i].m_ptCenter.x;
int colMax = ptLast.x;
if (colMin > colMax)
{
int dwTemp = colMin;
colMin = colMax;
colMax = dwTemp;
}
for (int col = colMin; col <= colMax; col++)
{
int row = (int) (dk * col + b + 0.5);
row = row < height ? row : height - 1;
row = row >= 0 ? row : 0;
pRGBIn[row * dwStep + col * 3] = bBlue;
pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
pRGBIn[row * dwStep + col * 3 + 2] = bRed;
if (++row < height)
{
pRGBIn[row * dwStep + col * 3] = bBlue;
pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
pRGBIn[row * dwStep + col * 3 + 2] = bRed;
}
else if ((row -= 2) >= 0)
{
pRGBIn[row * dwStep + col * 3] = bBlue;
pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
pRGBIn[row * dwStep + col * 3 + 2] = bRed;
}
}
}
else
{
double dk = ((double) ForegroundArray[i].m_ptCenter.x - ptLast.x) / (ForegroundArray[i].m_ptCenter.y - ptLast.y);
double b = ForegroundArray[i].m_ptCenter.x - ForegroundArray[i].m_ptCenter.y * dk;
int rowMin = ForegroundArray[i].m_ptCenter.y;
int rowMax = ptLast.y;
if (rowMin > rowMax)
{
int dwTemp = rowMin;
rowMin = rowMax;
rowMax = dwTemp;
}
for (int row = rowMin; row <= rowMax; row++)
{
int col = (int) (dk * row + b + 0.5);
col = col < width? col : width - 1;
col = col >= 0 ? col : 0;
pRGBIn[row * dwStep + col * 3] = bBlue;
pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
pRGBIn[row * dwStep + col * 3 + 2] = bRed;
if (++col < width)
{
pRGBIn[row * dwStep + col * 3] = bBlue;
pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
pRGBIn[row * dwStep + col * 3 + 2] = bRed;
}
else if ((col -= 2) >= 0)
{
pRGBIn[row * dwStep + col * 3] = bBlue;
pRGBIn[row * dwStep + col * 3 + 1] = bGreen;
pRGBIn[row * dwStep + col * 3 + 2] = bRed;
}
}
}
}
ptLast = ForegroundArray[i].m_ptCenter;
}
}	// draw it and judge area function
*/