/usr/include/opencv2/core/core.hpp is in libopencv-core-dev 2.4.9.1+dfsg-1+deb8u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 | /*! \file core.hpp
\brief The Core Functionality
*/
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __OPENCV_CORE_HPP__
#define __OPENCV_CORE_HPP__
#include "opencv2/core/types_c.h"
#include "opencv2/core/version.hpp"
#ifdef __cplusplus
#ifndef SKIP_INCLUDES
#include <limits.h>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <complex>
#include <map>
#include <new>
#include <string>
#include <vector>
#include <sstream>
#endif // SKIP_INCLUDES
/*! \namespace cv
Namespace where all the C++ OpenCV functionality resides
*/
namespace cv {
#undef abs
#undef min
#undef max
#undef Complex
using std::vector;
using std::string;
using std::ptrdiff_t;
template<typename _Tp> class Size_;
template<typename _Tp> class Point_;
template<typename _Tp> class Rect_;
template<typename _Tp, int cn> class Vec;
template<typename _Tp, int m, int n> class Matx;
typedef std::string String;
class Mat;
class SparseMat;
typedef Mat MatND;
namespace ogl {
class Buffer;
class Texture2D;
class Arrays;
}
// < Deprecated
class GlBuffer;
class GlTexture;
class GlArrays;
class GlCamera;
// >
namespace gpu {
class GpuMat;
}
class CV_EXPORTS MatExpr;
class CV_EXPORTS MatOp_Base;
class CV_EXPORTS MatArg;
class CV_EXPORTS MatConstIterator;
template<typename _Tp> class Mat_;
template<typename _Tp> class MatIterator_;
template<typename _Tp> class MatConstIterator_;
template<typename _Tp> class MatCommaInitializer_;
#if !defined(ANDROID) || (defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_WCHAR_T)
typedef std::basic_string<wchar_t> WString;
CV_EXPORTS string fromUtf16(const WString& str);
CV_EXPORTS WString toUtf16(const string& str);
#endif
CV_EXPORTS string format( const char* fmt, ... );
CV_EXPORTS string tempfile( const char* suffix CV_DEFAULT(0));
// matrix decomposition types
enum { DECOMP_LU=0, DECOMP_SVD=1, DECOMP_EIG=2, DECOMP_CHOLESKY=3, DECOMP_QR=4, DECOMP_NORMAL=16 };
enum { NORM_INF=1, NORM_L1=2, NORM_L2=4, NORM_L2SQR=5, NORM_HAMMING=6, NORM_HAMMING2=7, NORM_TYPE_MASK=7, NORM_RELATIVE=8, NORM_MINMAX=32 };
enum { CMP_EQ=0, CMP_GT=1, CMP_GE=2, CMP_LT=3, CMP_LE=4, CMP_NE=5 };
enum { GEMM_1_T=1, GEMM_2_T=2, GEMM_3_T=4 };
enum { DFT_INVERSE=1, DFT_SCALE=2, DFT_ROWS=4, DFT_COMPLEX_OUTPUT=16, DFT_REAL_OUTPUT=32,
DCT_INVERSE = DFT_INVERSE, DCT_ROWS=DFT_ROWS };
/*!
The standard OpenCV exception class.
Instances of the class are thrown by various functions and methods in the case of critical errors.
*/
class CV_EXPORTS Exception : public std::exception
{
public:
/*!
Default constructor
*/
Exception();
/*!
Full constructor. Normally the constuctor is not called explicitly.
Instead, the macros CV_Error(), CV_Error_() and CV_Assert() are used.
*/
Exception(int _code, const string& _err, const string& _func, const string& _file, int _line);
virtual ~Exception() throw();
/*!
\return the error description and the context as a text string.
*/
virtual const char *what() const throw();
void formatMessage();
string msg; ///< the formatted error message
int code; ///< error code @see CVStatus
string err; ///< error description
string func; ///< function name. Available only when the compiler supports getting it
string file; ///< source file name where the error has occured
int line; ///< line number in the source file where the error has occured
};
//! Signals an error and raises the exception.
/*!
By default the function prints information about the error to stderr,
then it either stops if setBreakOnError() had been called before or raises the exception.
It is possible to alternate error processing by using redirectError().
\param exc the exception raisen.
*/
CV_EXPORTS void error( const Exception& exc );
//! Sets/resets the break-on-error mode.
/*!
When the break-on-error mode is set, the default error handler
issues a hardware exception, which can make debugging more convenient.
\return the previous state
*/
CV_EXPORTS bool setBreakOnError(bool flag);
typedef int (CV_CDECL *ErrorCallback)( int status, const char* func_name,
const char* err_msg, const char* file_name,
int line, void* userdata );
//! Sets the new error handler and the optional user data.
/*!
The function sets the new error handler, called from cv::error().
\param errCallback the new error handler. If NULL, the default error handler is used.
\param userdata the optional user data pointer, passed to the callback.
\param prevUserdata the optional output parameter where the previous user data pointer is stored
\return the previous error handler
*/
CV_EXPORTS ErrorCallback redirectError( ErrorCallback errCallback,
void* userdata=0, void** prevUserdata=0);
#if defined __GNUC__
#define CV_Func __func__
#elif defined _MSC_VER
#define CV_Func __FUNCTION__
#else
#define CV_Func ""
#endif
#define CV_Error( code, msg ) cv::error( cv::Exception(code, msg, CV_Func, __FILE__, __LINE__) )
#define CV_Error_( code, args ) cv::error( cv::Exception(code, cv::format args, CV_Func, __FILE__, __LINE__) )
#define CV_Assert( expr ) if(!!(expr)) ; else cv::error( cv::Exception(CV_StsAssert, #expr, CV_Func, __FILE__, __LINE__) )
#ifdef _DEBUG
#define CV_DbgAssert(expr) CV_Assert(expr)
#else
#define CV_DbgAssert(expr)
#endif
CV_EXPORTS void glob(String pattern, std::vector<String>& result, bool recursive = false);
CV_EXPORTS void setNumThreads(int nthreads);
CV_EXPORTS int getNumThreads();
CV_EXPORTS int getThreadNum();
CV_EXPORTS_W const string& getBuildInformation();
//! Returns the number of ticks.
/*!
The function returns the number of ticks since the certain event (e.g. when the machine was turned on).
It can be used to initialize cv::RNG or to measure a function execution time by reading the tick count
before and after the function call. The granularity of ticks depends on the hardware and OS used. Use
cv::getTickFrequency() to convert ticks to seconds.
*/
CV_EXPORTS_W int64 getTickCount();
/*!
Returns the number of ticks per seconds.
The function returns the number of ticks (as returned by cv::getTickCount()) per second.
The following code computes the execution time in milliseconds:
\code
double exec_time = (double)getTickCount();
// do something ...
exec_time = ((double)getTickCount() - exec_time)*1000./getTickFrequency();
\endcode
*/
CV_EXPORTS_W double getTickFrequency();
/*!
Returns the number of CPU ticks.
On platforms where the feature is available, the function returns the number of CPU ticks
since the certain event (normally, the system power-on moment). Using this function
one can accurately measure the execution time of very small code fragments,
for which cv::getTickCount() granularity is not enough.
*/
CV_EXPORTS_W int64 getCPUTickCount();
/*!
Returns SSE etc. support status
The function returns true if certain hardware features are available.
Currently, the following features are recognized:
- CV_CPU_MMX - MMX
- CV_CPU_SSE - SSE
- CV_CPU_SSE2 - SSE 2
- CV_CPU_SSE3 - SSE 3
- CV_CPU_SSSE3 - SSSE 3
- CV_CPU_SSE4_1 - SSE 4.1
- CV_CPU_SSE4_2 - SSE 4.2
- CV_CPU_POPCNT - POPCOUNT
- CV_CPU_AVX - AVX
\note {Note that the function output is not static. Once you called cv::useOptimized(false),
most of the hardware acceleration is disabled and thus the function will returns false,
until you call cv::useOptimized(true)}
*/
CV_EXPORTS_W bool checkHardwareSupport(int feature);
//! returns the number of CPUs (including hyper-threading)
CV_EXPORTS_W int getNumberOfCPUs();
/*!
Allocates memory buffer
This is specialized OpenCV memory allocation function that returns properly aligned memory buffers.
The usage is identical to malloc(). The allocated buffers must be freed with cv::fastFree().
If there is not enough memory, the function calls cv::error(), which raises an exception.
\param bufSize buffer size in bytes
\return the allocated memory buffer.
*/
CV_EXPORTS void* fastMalloc(size_t bufSize);
/*!
Frees the memory allocated with cv::fastMalloc
This is the corresponding deallocation function for cv::fastMalloc().
When ptr==NULL, the function has no effect.
*/
CV_EXPORTS void fastFree(void* ptr);
template<typename _Tp> static inline _Tp* allocate(size_t n)
{
return new _Tp[n];
}
template<typename _Tp> static inline void deallocate(_Tp* ptr, size_t)
{
delete[] ptr;
}
/*!
Aligns pointer by the certain number of bytes
This small inline function aligns the pointer by the certian number of bytes by shifting
it forward by 0 or a positive offset.
*/
template<typename _Tp> static inline _Tp* alignPtr(_Tp* ptr, int n=(int)sizeof(_Tp))
{
return (_Tp*)(((size_t)ptr + n-1) & -n);
}
/*!
Aligns buffer size by the certain number of bytes
This small inline function aligns a buffer size by the certian number of bytes by enlarging it.
*/
static inline size_t alignSize(size_t sz, int n)
{
assert((n & (n - 1)) == 0); // n is a power of 2
return (sz + n-1) & -n;
}
/*!
Turns on/off available optimization
The function turns on or off the optimized code in OpenCV. Some optimization can not be enabled
or disabled, but, for example, most of SSE code in OpenCV can be temporarily turned on or off this way.
\note{Since optimization may imply using special data structures, it may be unsafe
to call this function anywhere in the code. Instead, call it somewhere at the top level.}
*/
CV_EXPORTS_W void setUseOptimized(bool onoff);
/*!
Returns the current optimization status
The function returns the current optimization status, which is controlled by cv::setUseOptimized().
*/
CV_EXPORTS_W bool useOptimized();
/*!
The STL-compilant memory Allocator based on cv::fastMalloc() and cv::fastFree()
*/
template<typename _Tp> class Allocator
{
public:
typedef _Tp value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template<typename U> class rebind { typedef Allocator<U> other; };
explicit Allocator() {}
~Allocator() {}
explicit Allocator(Allocator const&) {}
template<typename U>
explicit Allocator(Allocator<U> const&) {}
// address
pointer address(reference r) { return &r; }
const_pointer address(const_reference r) { return &r; }
pointer allocate(size_type count, const void* =0)
{ return reinterpret_cast<pointer>(fastMalloc(count * sizeof (_Tp))); }
void deallocate(pointer p, size_type) {fastFree(p); }
size_type max_size() const
{ return max(static_cast<_Tp>(-1)/sizeof(_Tp), 1); }
void construct(pointer p, const _Tp& v) { new(static_cast<void*>(p)) _Tp(v); }
void destroy(pointer p) { p->~_Tp(); }
};
/////////////////////// Vec (used as element of multi-channel images /////////////////////
/*!
A helper class for cv::DataType
The class is specialized for each fundamental numerical data type supported by OpenCV.
It provides DataDepth<T>::value constant.
*/
template<typename _Tp> class DataDepth {};
template<> class DataDepth<bool> { public: enum { value = CV_8U, fmt=(int)'u' }; };
template<> class DataDepth<uchar> { public: enum { value = CV_8U, fmt=(int)'u' }; };
template<> class DataDepth<schar> { public: enum { value = CV_8S, fmt=(int)'c' }; };
template<> class DataDepth<char> { public: enum { value = CV_8S, fmt=(int)'c' }; };
template<> class DataDepth<ushort> { public: enum { value = CV_16U, fmt=(int)'w' }; };
template<> class DataDepth<short> { public: enum { value = CV_16S, fmt=(int)'s' }; };
template<> class DataDepth<int> { public: enum { value = CV_32S, fmt=(int)'i' }; };
// this is temporary solution to support 32-bit unsigned integers
template<> class DataDepth<unsigned> { public: enum { value = CV_32S, fmt=(int)'i' }; };
template<> class DataDepth<float> { public: enum { value = CV_32F, fmt=(int)'f' }; };
template<> class DataDepth<double> { public: enum { value = CV_64F, fmt=(int)'d' }; };
template<typename _Tp> class DataDepth<_Tp*> { public: enum { value = CV_USRTYPE1, fmt=(int)'r' }; };
////////////////////////////// Small Matrix ///////////////////////////
/*!
A short numerical vector.
This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements)
on which you can perform basic arithmetical operations, access individual elements using [] operator etc.
The vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc.,
which elements are dynamically allocated in the heap.
The template takes 2 parameters:
-# _Tp element type
-# cn the number of elements
In addition to the universal notation like Vec<float, 3>, you can use shorter aliases
for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec<float, 3>.
*/
struct CV_EXPORTS Matx_AddOp {};
struct CV_EXPORTS Matx_SubOp {};
struct CV_EXPORTS Matx_ScaleOp {};
struct CV_EXPORTS Matx_MulOp {};
struct CV_EXPORTS Matx_MatMulOp {};
struct CV_EXPORTS Matx_TOp {};
template<typename _Tp, int m, int n> class Matx
{
public:
typedef _Tp value_type;
typedef Matx<_Tp, (m < n ? m : n), 1> diag_type;
typedef Matx<_Tp, m, n> mat_type;
enum { depth = DataDepth<_Tp>::value, rows = m, cols = n, channels = rows*cols,
type = CV_MAKETYPE(depth, channels) };
//! default constructor
Matx();
Matx(_Tp v0); //!< 1x1 matrix
Matx(_Tp v0, _Tp v1); //!< 1x2 or 2x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2); //!< 1x3 or 3x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 1x4, 2x2 or 4x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 1x5 or 5x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 1x6, 2x3, 3x2 or 6x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 1x7 or 7x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 1x8, 2x4, 4x2 or 8x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 1x9, 3x3 or 9x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9); //!< 1x10, 2x5 or 5x2 or 10x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
_Tp v4, _Tp v5, _Tp v6, _Tp v7,
_Tp v8, _Tp v9, _Tp v10, _Tp v11); //!< 1x12, 2x6, 3x4, 4x3, 6x2 or 12x1 matrix
Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
_Tp v4, _Tp v5, _Tp v6, _Tp v7,
_Tp v8, _Tp v9, _Tp v10, _Tp v11,
_Tp v12, _Tp v13, _Tp v14, _Tp v15); //!< 1x16, 4x4 or 16x1 matrix
explicit Matx(const _Tp* vals); //!< initialize from a plain array
static Matx all(_Tp alpha);
static Matx zeros();
static Matx ones();
static Matx eye();
static Matx diag(const diag_type& d);
static Matx randu(_Tp a, _Tp b);
static Matx randn(_Tp a, _Tp b);
//! dot product computed with the default precision
_Tp dot(const Matx<_Tp, m, n>& v) const;
//! dot product computed in double-precision arithmetics
double ddot(const Matx<_Tp, m, n>& v) const;
//! convertion to another data type
template<typename T2> operator Matx<T2, m, n>() const;
//! change the matrix shape
template<int m1, int n1> Matx<_Tp, m1, n1> reshape() const;
//! extract part of the matrix
template<int m1, int n1> Matx<_Tp, m1, n1> get_minor(int i, int j) const;
//! extract the matrix row
Matx<_Tp, 1, n> row(int i) const;
//! extract the matrix column
Matx<_Tp, m, 1> col(int i) const;
//! extract the matrix diagonal
diag_type diag() const;
//! transpose the matrix
Matx<_Tp, n, m> t() const;
//! invert matrix the matrix
Matx<_Tp, n, m> inv(int method=DECOMP_LU) const;
//! solve linear system
template<int l> Matx<_Tp, n, l> solve(const Matx<_Tp, m, l>& rhs, int flags=DECOMP_LU) const;
Vec<_Tp, n> solve(const Vec<_Tp, m>& rhs, int method) const;
//! multiply two matrices element-wise
Matx<_Tp, m, n> mul(const Matx<_Tp, m, n>& a) const;
//! element access
const _Tp& operator ()(int i, int j) const;
_Tp& operator ()(int i, int j);
//! 1D element access
const _Tp& operator ()(int i) const;
_Tp& operator ()(int i);
Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp);
Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp);
template<typename _T2> Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp);
Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp);
template<int l> Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp);
Matx(const Matx<_Tp, n, m>& a, Matx_TOp);
_Tp val[m*n]; //< matrix elements
};
typedef Matx<float, 1, 2> Matx12f;
typedef Matx<double, 1, 2> Matx12d;
typedef Matx<float, 1, 3> Matx13f;
typedef Matx<double, 1, 3> Matx13d;
typedef Matx<float, 1, 4> Matx14f;
typedef Matx<double, 1, 4> Matx14d;
typedef Matx<float, 1, 6> Matx16f;
typedef Matx<double, 1, 6> Matx16d;
typedef Matx<float, 2, 1> Matx21f;
typedef Matx<double, 2, 1> Matx21d;
typedef Matx<float, 3, 1> Matx31f;
typedef Matx<double, 3, 1> Matx31d;
typedef Matx<float, 4, 1> Matx41f;
typedef Matx<double, 4, 1> Matx41d;
typedef Matx<float, 6, 1> Matx61f;
typedef Matx<double, 6, 1> Matx61d;
typedef Matx<float, 2, 2> Matx22f;
typedef Matx<double, 2, 2> Matx22d;
typedef Matx<float, 2, 3> Matx23f;
typedef Matx<double, 2, 3> Matx23d;
typedef Matx<float, 3, 2> Matx32f;
typedef Matx<double, 3, 2> Matx32d;
typedef Matx<float, 3, 3> Matx33f;
typedef Matx<double, 3, 3> Matx33d;
typedef Matx<float, 3, 4> Matx34f;
typedef Matx<double, 3, 4> Matx34d;
typedef Matx<float, 4, 3> Matx43f;
typedef Matx<double, 4, 3> Matx43d;
typedef Matx<float, 4, 4> Matx44f;
typedef Matx<double, 4, 4> Matx44d;
typedef Matx<float, 6, 6> Matx66f;
typedef Matx<double, 6, 6> Matx66d;
/*!
A short numerical vector.
This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements)
on which you can perform basic arithmetical operations, access individual elements using [] operator etc.
The vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc.,
which elements are dynamically allocated in the heap.
The template takes 2 parameters:
-# _Tp element type
-# cn the number of elements
In addition to the universal notation like Vec<float, 3>, you can use shorter aliases
for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec<float, 3>.
*/
template<typename _Tp, int cn> class Vec : public Matx<_Tp, cn, 1>
{
public:
typedef _Tp value_type;
enum { depth = DataDepth<_Tp>::value, channels = cn, type = CV_MAKETYPE(depth, channels) };
//! default constructor
Vec();
Vec(_Tp v0); //!< 1-element vector constructor
Vec(_Tp v0, _Tp v1); //!< 2-element vector constructor
Vec(_Tp v0, _Tp v1, _Tp v2); //!< 3-element vector constructor
Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 4-element vector constructor
Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 5-element vector constructor
Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 6-element vector constructor
Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 7-element vector constructor
Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 8-element vector constructor
Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 9-element vector constructor
Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9); //!< 10-element vector constructor
explicit Vec(const _Tp* values);
Vec(const Vec<_Tp, cn>& v);
static Vec all(_Tp alpha);
//! per-element multiplication
Vec mul(const Vec<_Tp, cn>& v) const;
//! conjugation (makes sense for complex numbers and quaternions)
Vec conj() const;
/*!
cross product of the two 3D vectors.
For other dimensionalities the exception is raised
*/
Vec cross(const Vec& v) const;
//! convertion to another data type
template<typename T2> operator Vec<T2, cn>() const;
//! conversion to 4-element CvScalar.
operator CvScalar() const;
/*! element access */
const _Tp& operator [](int i) const;
_Tp& operator[](int i);
const _Tp& operator ()(int i) const;
_Tp& operator ()(int i);
Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp);
Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp);
template<typename _T2> Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp);
};
/* \typedef
Shorter aliases for the most popular specializations of Vec<T,n>
*/
typedef Vec<uchar, 2> Vec2b;
typedef Vec<uchar, 3> Vec3b;
typedef Vec<uchar, 4> Vec4b;
typedef Vec<short, 2> Vec2s;
typedef Vec<short, 3> Vec3s;
typedef Vec<short, 4> Vec4s;
typedef Vec<ushort, 2> Vec2w;
typedef Vec<ushort, 3> Vec3w;
typedef Vec<ushort, 4> Vec4w;
typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<int, 6> Vec6i;
typedef Vec<int, 8> Vec8i;
typedef Vec<float, 2> Vec2f;
typedef Vec<float, 3> Vec3f;
typedef Vec<float, 4> Vec4f;
typedef Vec<float, 6> Vec6f;
typedef Vec<double, 2> Vec2d;
typedef Vec<double, 3> Vec3d;
typedef Vec<double, 4> Vec4d;
typedef Vec<double, 6> Vec6d;
//////////////////////////////// Complex //////////////////////////////
/*!
A complex number class.
The template class is similar and compatible with std::complex, however it provides slightly
more convenient access to the real and imaginary parts using through the simple field access, as opposite
to std::complex::real() and std::complex::imag().
*/
template<typename _Tp> class Complex
{
public:
//! constructors
Complex();
Complex( _Tp _re, _Tp _im=0 );
Complex( const std::complex<_Tp>& c );
//! conversion to another data type
template<typename T2> operator Complex<T2>() const;
//! conjugation
Complex conj() const;
//! conversion to std::complex
operator std::complex<_Tp>() const;
_Tp re, im; //< the real and the imaginary parts
};
/*!
\typedef
*/
typedef Complex<float> Complexf;
typedef Complex<double> Complexd;
//////////////////////////////// Point_ ////////////////////////////////
/*!
template 2D point class.
The class defines a point in 2D space. Data type of the point coordinates is specified
as a template parameter. There are a few shorter aliases available for user convenience.
See cv::Point, cv::Point2i, cv::Point2f and cv::Point2d.
*/
template<typename _Tp> class Point_
{
public:
typedef _Tp value_type;
// various constructors
Point_();
Point_(_Tp _x, _Tp _y);
Point_(const Point_& pt);
Point_(const CvPoint& pt);
Point_(const CvPoint2D32f& pt);
Point_(const Size_<_Tp>& sz);
Point_(const Vec<_Tp, 2>& v);
Point_& operator = (const Point_& pt);
//! conversion to another data type
template<typename _Tp2> operator Point_<_Tp2>() const;
//! conversion to the old-style C structures
operator CvPoint() const;
operator CvPoint2D32f() const;
operator Vec<_Tp, 2>() const;
//! dot product
_Tp dot(const Point_& pt) const;
//! dot product computed in double-precision arithmetics
double ddot(const Point_& pt) const;
//! cross-product
double cross(const Point_& pt) const;
//! checks whether the point is inside the specified rectangle
bool inside(const Rect_<_Tp>& r) const;
_Tp x, y; //< the point coordinates
};
/*!
template 3D point class.
The class defines a point in 3D space. Data type of the point coordinates is specified
as a template parameter.
\see cv::Point3i, cv::Point3f and cv::Point3d
*/
template<typename _Tp> class Point3_
{
public:
typedef _Tp value_type;
// various constructors
Point3_();
Point3_(_Tp _x, _Tp _y, _Tp _z);
Point3_(const Point3_& pt);
explicit Point3_(const Point_<_Tp>& pt);
Point3_(const CvPoint3D32f& pt);
Point3_(const Vec<_Tp, 3>& v);
Point3_& operator = (const Point3_& pt);
//! conversion to another data type
template<typename _Tp2> operator Point3_<_Tp2>() const;
//! conversion to the old-style CvPoint...
operator CvPoint3D32f() const;
//! conversion to cv::Vec<>
operator Vec<_Tp, 3>() const;
//! dot product
_Tp dot(const Point3_& pt) const;
//! dot product computed in double-precision arithmetics
double ddot(const Point3_& pt) const;
//! cross product of the 2 3D points
Point3_ cross(const Point3_& pt) const;
_Tp x, y, z; //< the point coordinates
};
//////////////////////////////// Size_ ////////////////////////////////
/*!
The 2D size class
The class represents the size of a 2D rectangle, image size, matrix size etc.
Normally, cv::Size ~ cv::Size_<int> is used.
*/
template<typename _Tp> class Size_
{
public:
typedef _Tp value_type;
//! various constructors
Size_();
Size_(_Tp _width, _Tp _height);
Size_(const Size_& sz);
Size_(const CvSize& sz);
Size_(const CvSize2D32f& sz);
Size_(const Point_<_Tp>& pt);
Size_& operator = (const Size_& sz);
//! the area (width*height)
_Tp area() const;
//! conversion of another data type.
template<typename _Tp2> operator Size_<_Tp2>() const;
//! conversion to the old-style OpenCV types
operator CvSize() const;
operator CvSize2D32f() const;
_Tp width, height; // the width and the height
};
//////////////////////////////// Rect_ ////////////////////////////////
/*!
The 2D up-right rectangle class
The class represents a 2D rectangle with coordinates of the specified data type.
Normally, cv::Rect ~ cv::Rect_<int> is used.
*/
template<typename _Tp> class Rect_
{
public:
typedef _Tp value_type;
//! various constructors
Rect_();
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
Rect_(const Rect_& r);
Rect_(const CvRect& r);
Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);
Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);
Rect_& operator = ( const Rect_& r );
//! the top-left corner
Point_<_Tp> tl() const;
//! the bottom-right corner
Point_<_Tp> br() const;
//! size (width, height) of the rectangle
Size_<_Tp> size() const;
//! area (width*height) of the rectangle
_Tp area() const;
//! conversion to another data type
template<typename _Tp2> operator Rect_<_Tp2>() const;
//! conversion to the old-style CvRect
operator CvRect() const;
//! checks whether the rectangle contains the point
bool contains(const Point_<_Tp>& pt) const;
_Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle
};
/*!
\typedef
shorter aliases for the most popular cv::Point_<>, cv::Size_<> and cv::Rect_<> specializations
*/
typedef Point_<int> Point2i;
typedef Point2i Point;
typedef Size_<int> Size2i;
typedef Size_<double> Size2d;
typedef Size2i Size;
typedef Rect_<int> Rect;
typedef Point_<float> Point2f;
typedef Point_<double> Point2d;
typedef Size_<float> Size2f;
typedef Point3_<int> Point3i;
typedef Point3_<float> Point3f;
typedef Point3_<double> Point3d;
/*!
The rotated 2D rectangle.
The class represents rotated (i.e. not up-right) rectangles on a plane.
Each rectangle is described by the center point (mass center), length of each side
(represented by cv::Size2f structure) and the rotation angle in degrees.
*/
class CV_EXPORTS RotatedRect
{
public:
//! various constructors
RotatedRect();
RotatedRect(const Point2f& center, const Size2f& size, float angle);
RotatedRect(const CvBox2D& box);
//! returns 4 vertices of the rectangle
void points(Point2f pts[]) const;
//! returns the minimal up-right rectangle containing the rotated rectangle
Rect boundingRect() const;
//! conversion to the old-style CvBox2D structure
operator CvBox2D() const;
Point2f center; //< the rectangle mass center
Size2f size; //< width and height of the rectangle
float angle; //< the rotation angle. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.
};
//////////////////////////////// Scalar_ ///////////////////////////////
/*!
The template scalar class.
This is partially specialized cv::Vec class with the number of elements = 4, i.e. a short vector of four elements.
Normally, cv::Scalar ~ cv::Scalar_<double> is used.
*/
template<typename _Tp> class Scalar_ : public Vec<_Tp, 4>
{
public:
//! various constructors
Scalar_();
Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);
Scalar_(const CvScalar& s);
Scalar_(_Tp v0);
//! returns a scalar with all elements set to v0
static Scalar_<_Tp> all(_Tp v0);
//! conversion to the old-style CvScalar
operator CvScalar() const;
//! conversion to another data type
template<typename T2> operator Scalar_<T2>() const;
//! per-element product
Scalar_<_Tp> mul(const Scalar_<_Tp>& t, double scale=1 ) const;
// returns (v0, -v1, -v2, -v3)
Scalar_<_Tp> conj() const;
// returns true iff v1 == v2 == v3 == 0
bool isReal() const;
};
typedef Scalar_<double> Scalar;
CV_EXPORTS void scalarToRawData(const Scalar& s, void* buf, int type, int unroll_to=0);
//////////////////////////////// Range /////////////////////////////////
/*!
The 2D range class
This is the class used to specify a continuous subsequence, i.e. part of a contour, or a column span in a matrix.
*/
class CV_EXPORTS Range
{
public:
Range();
Range(int _start, int _end);
Range(const CvSlice& slice);
int size() const;
bool empty() const;
static Range all();
operator CvSlice() const;
int start, end;
};
/////////////////////////////// DataType ////////////////////////////////
/*!
Informative template class for OpenCV "scalars".
The class is specialized for each primitive numerical type supported by OpenCV (such as unsigned char or float),
as well as for more complex types, like cv::Complex<>, std::complex<>, cv::Vec<> etc.
The common property of all such types (called "scalars", do not confuse it with cv::Scalar_)
is that each of them is basically a tuple of numbers of the same type. Each "scalar" can be represented
by the depth id (CV_8U ... CV_64F) and the number of channels.
OpenCV matrices, 2D or nD, dense or sparse, can store "scalars",
as long as the number of channels does not exceed CV_CN_MAX.
*/
template<typename _Tp> class DataType
{
public:
typedef _Tp value_type;
typedef value_type work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 1, depth = -1, channels = 1, fmt=0,
type = CV_MAKETYPE(depth, channels) };
};
template<> class DataType<bool>
{
public:
typedef bool value_type;
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<> class DataType<uchar>
{
public:
typedef uchar value_type;
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<> class DataType<schar>
{
public:
typedef schar value_type;
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<> class DataType<char>
{
public:
typedef schar value_type;
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<> class DataType<ushort>
{
public:
typedef ushort value_type;
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<> class DataType<short>
{
public:
typedef short value_type;
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<> class DataType<int>
{
public:
typedef int value_type;
typedef value_type work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<> class DataType<float>
{
public:
typedef float value_type;
typedef value_type work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<> class DataType<double>
{
public:
typedef double value_type;
typedef value_type work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<typename _Tp, int m, int n> class DataType<Matx<_Tp, m, n> >
{
public:
typedef Matx<_Tp, m, n> value_type;
typedef Matx<typename DataType<_Tp>::work_type, m, n> work_type;
typedef _Tp channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = m*n,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<typename _Tp, int cn> class DataType<Vec<_Tp, cn> >
{
public:
typedef Vec<_Tp, cn> value_type;
typedef Vec<typename DataType<_Tp>::work_type, cn> work_type;
typedef _Tp channel_type;
typedef value_type vec_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = cn,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
template<typename _Tp> class DataType<std::complex<_Tp> >
{
public:
typedef std::complex<_Tp> value_type;
typedef value_type work_type;
typedef _Tp channel_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
};
template<typename _Tp> class DataType<Complex<_Tp> >
{
public:
typedef Complex<_Tp> value_type;
typedef value_type work_type;
typedef _Tp channel_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
};
template<typename _Tp> class DataType<Point_<_Tp> >
{
public:
typedef Point_<_Tp> value_type;
typedef Point_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
};
template<typename _Tp> class DataType<Point3_<_Tp> >
{
public:
typedef Point3_<_Tp> value_type;
typedef Point3_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 3,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
};
template<typename _Tp> class DataType<Size_<_Tp> >
{
public:
typedef Size_<_Tp> value_type;
typedef Size_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
};
template<typename _Tp> class DataType<Rect_<_Tp> >
{
public:
typedef Rect_<_Tp> value_type;
typedef Rect_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 4,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
};
template<typename _Tp> class DataType<Scalar_<_Tp> >
{
public:
typedef Scalar_<_Tp> value_type;
typedef Scalar_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 4,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
};
template<> class DataType<Range>
{
public:
typedef Range value_type;
typedef value_type work_type;
typedef int channel_type;
enum { generic_type = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
};
//////////////////// generic_type ref-counting pointer class for C/C++ objects ////////////////////////
/*!
Smart pointer to dynamically allocated objects.
This is template pointer-wrapping class that stores the associated reference counter along with the
object pointer. The class is similar to std::smart_ptr<> from the recent addons to the C++ standard,
but is shorter to write :) and self-contained (i.e. does add any dependency on the compiler or an external library).
Basically, you can use "Ptr<MyObjectType> ptr" (or faster "const Ptr<MyObjectType>& ptr" for read-only access)
everywhere instead of "MyObjectType* ptr", where MyObjectType is some C structure or a C++ class.
To make it all work, you need to specialize Ptr<>::delete_obj(), like:
\code
template<> void Ptr<MyObjectType>::delete_obj() { call_destructor_func(obj); }
\endcode
\note{if MyObjectType is a C++ class with a destructor, you do not need to specialize delete_obj(),
since the default implementation calls "delete obj;"}
\note{Another good property of the class is that the operations on the reference counter are atomic,
i.e. it is safe to use the class in multi-threaded applications}
*/
template<typename _Tp> class Ptr
{
public:
//! empty constructor
Ptr();
//! take ownership of the pointer. The associated reference counter is allocated and set to 1
Ptr(_Tp* _obj);
//! calls release()
~Ptr();
//! copy constructor. Copies the members and calls addref()
Ptr(const Ptr& ptr);
template<typename _Tp2> Ptr(const Ptr<_Tp2>& ptr);
//! copy operator. Calls ptr.addref() and release() before copying the members
Ptr& operator = (const Ptr& ptr);
//! increments the reference counter
void addref();
//! decrements the reference counter. If it reaches 0, delete_obj() is called
void release();
//! deletes the object. Override if needed
void delete_obj();
//! returns true iff obj==NULL
bool empty() const;
//! cast pointer to another type
template<typename _Tp2> Ptr<_Tp2> ptr();
template<typename _Tp2> const Ptr<_Tp2> ptr() const;
//! helper operators making "Ptr<T> ptr" use very similar to "T* ptr".
_Tp* operator -> ();
const _Tp* operator -> () const;
operator _Tp* ();
operator const _Tp*() const;
_Tp* obj; //< the object pointer.
_Atomic_word* refcount; //< the associated reference counter
};
//////////////////////// Input/Output Array Arguments /////////////////////////////////
/*!
Proxy datatype for passing Mat's and vector<>'s as input parameters
*/
class CV_EXPORTS _InputArray
{
public:
enum {
KIND_SHIFT = 16,
FIXED_TYPE = 0x8000 << KIND_SHIFT,
FIXED_SIZE = 0x4000 << KIND_SHIFT,
KIND_MASK = ~(FIXED_TYPE|FIXED_SIZE) - (1 << KIND_SHIFT) + 1,
NONE = 0 << KIND_SHIFT,
MAT = 1 << KIND_SHIFT,
MATX = 2 << KIND_SHIFT,
STD_VECTOR = 3 << KIND_SHIFT,
STD_VECTOR_VECTOR = 4 << KIND_SHIFT,
STD_VECTOR_MAT = 5 << KIND_SHIFT,
EXPR = 6 << KIND_SHIFT,
OPENGL_BUFFER = 7 << KIND_SHIFT,
OPENGL_TEXTURE = 8 << KIND_SHIFT,
GPU_MAT = 9 << KIND_SHIFT,
OCL_MAT =10 << KIND_SHIFT
};
_InputArray();
_InputArray(const Mat& m);
_InputArray(const MatExpr& expr);
template<typename _Tp> _InputArray(const _Tp* vec, int n);
template<typename _Tp> _InputArray(const vector<_Tp>& vec);
template<typename _Tp> _InputArray(const vector<vector<_Tp> >& vec);
_InputArray(const vector<Mat>& vec);
template<typename _Tp> _InputArray(const vector<Mat_<_Tp> >& vec);
template<typename _Tp> _InputArray(const Mat_<_Tp>& m);
template<typename _Tp, int m, int n> _InputArray(const Matx<_Tp, m, n>& matx);
_InputArray(const Scalar& s);
_InputArray(const double& val);
// < Deprecated
_InputArray(const GlBuffer& buf);
_InputArray(const GlTexture& tex);
// >
_InputArray(const gpu::GpuMat& d_mat);
_InputArray(const ogl::Buffer& buf);
_InputArray(const ogl::Texture2D& tex);
virtual Mat getMat(int i=-1) const;
virtual void getMatVector(vector<Mat>& mv) const;
// < Deprecated
virtual GlBuffer getGlBuffer() const;
virtual GlTexture getGlTexture() const;
// >
virtual gpu::GpuMat getGpuMat() const;
/*virtual*/ ogl::Buffer getOGlBuffer() const;
/*virtual*/ ogl::Texture2D getOGlTexture2D() const;
virtual int kind() const;
virtual Size size(int i=-1) const;
virtual size_t total(int i=-1) const;
virtual int type(int i=-1) const;
virtual int depth(int i=-1) const;
virtual int channels(int i=-1) const;
virtual bool empty() const;
#ifdef OPENCV_CAN_BREAK_BINARY_COMPATIBILITY
virtual ~_InputArray();
#endif
int flags;
void* obj;
Size sz;
};
enum
{
DEPTH_MASK_8U = 1 << CV_8U,
DEPTH_MASK_8S = 1 << CV_8S,
DEPTH_MASK_16U = 1 << CV_16U,
DEPTH_MASK_16S = 1 << CV_16S,
DEPTH_MASK_32S = 1 << CV_32S,
DEPTH_MASK_32F = 1 << CV_32F,
DEPTH_MASK_64F = 1 << CV_64F,
DEPTH_MASK_ALL = (DEPTH_MASK_64F<<1)-1,
DEPTH_MASK_ALL_BUT_8S = DEPTH_MASK_ALL & ~DEPTH_MASK_8S,
DEPTH_MASK_FLT = DEPTH_MASK_32F + DEPTH_MASK_64F
};
/*!
Proxy datatype for passing Mat's and vector<>'s as input parameters
*/
class CV_EXPORTS _OutputArray : public _InputArray
{
public:
_OutputArray();
_OutputArray(Mat& m);
template<typename _Tp> _OutputArray(vector<_Tp>& vec);
template<typename _Tp> _OutputArray(vector<vector<_Tp> >& vec);
_OutputArray(vector<Mat>& vec);
template<typename _Tp> _OutputArray(vector<Mat_<_Tp> >& vec);
template<typename _Tp> _OutputArray(Mat_<_Tp>& m);
template<typename _Tp, int m, int n> _OutputArray(Matx<_Tp, m, n>& matx);
template<typename _Tp> _OutputArray(_Tp* vec, int n);
_OutputArray(gpu::GpuMat& d_mat);
_OutputArray(ogl::Buffer& buf);
_OutputArray(ogl::Texture2D& tex);
_OutputArray(const Mat& m);
template<typename _Tp> _OutputArray(const vector<_Tp>& vec);
template<typename _Tp> _OutputArray(const vector<vector<_Tp> >& vec);
_OutputArray(const vector<Mat>& vec);
template<typename _Tp> _OutputArray(const vector<Mat_<_Tp> >& vec);
template<typename _Tp> _OutputArray(const Mat_<_Tp>& m);
template<typename _Tp, int m, int n> _OutputArray(const Matx<_Tp, m, n>& matx);
template<typename _Tp> _OutputArray(const _Tp* vec, int n);
_OutputArray(const gpu::GpuMat& d_mat);
_OutputArray(const ogl::Buffer& buf);
_OutputArray(const ogl::Texture2D& tex);
virtual bool fixedSize() const;
virtual bool fixedType() const;
virtual bool needed() const;
virtual Mat& getMatRef(int i=-1) const;
/*virtual*/ gpu::GpuMat& getGpuMatRef() const;
/*virtual*/ ogl::Buffer& getOGlBufferRef() const;
/*virtual*/ ogl::Texture2D& getOGlTexture2DRef() const;
virtual void create(Size sz, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const;
virtual void create(int rows, int cols, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const;
virtual void create(int dims, const int* size, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const;
virtual void release() const;
virtual void clear() const;
#ifdef OPENCV_CAN_BREAK_BINARY_COMPATIBILITY
virtual ~_OutputArray();
#endif
};
typedef const _InputArray& InputArray;
typedef InputArray InputArrayOfArrays;
typedef const _OutputArray& OutputArray;
typedef OutputArray OutputArrayOfArrays;
typedef OutputArray InputOutputArray;
typedef OutputArray InputOutputArrayOfArrays;
CV_EXPORTS OutputArray noArray();
/////////////////////////////////////// Mat ///////////////////////////////////////////
enum { MAGIC_MASK=0xFFFF0000, TYPE_MASK=0x00000FFF, DEPTH_MASK=7 };
static inline size_t getElemSize(int type) { return CV_ELEM_SIZE(type); }
/*!
Custom array allocator
*/
class CV_EXPORTS MatAllocator
{
public:
MatAllocator() {}
virtual ~MatAllocator() {}
virtual void allocate(int dims, const int* sizes, int type, _Atomic_word*& refcount,
uchar*& datastart, uchar*& data, size_t* step) = 0;
virtual void deallocate(_Atomic_word* refcount, uchar* datastart, uchar* data) = 0;
};
/*!
The n-dimensional matrix class.
The class represents an n-dimensional dense numerical array that can act as
a matrix, image, optical flow map, 3-focal tensor etc.
It is very similar to CvMat and CvMatND types from earlier versions of OpenCV,
and similarly to those types, the matrix can be multi-channel. It also fully supports ROI mechanism.
There are many different ways to create cv::Mat object. Here are the some popular ones:
<ul>
<li> using cv::Mat::create(nrows, ncols, type) method or
the similar constructor cv::Mat::Mat(nrows, ncols, type[, fill_value]) constructor.
A new matrix of the specified size and specifed type will be allocated.
"type" has the same meaning as in cvCreateMat function,
e.g. CV_8UC1 means 8-bit single-channel matrix, CV_32FC2 means 2-channel (i.e. complex)
floating-point matrix etc:
\code
// make 7x7 complex matrix filled with 1+3j.
cv::Mat M(7,7,CV_32FC2,Scalar(1,3));
// and now turn M to 100x60 15-channel 8-bit matrix.
// The old content will be deallocated
M.create(100,60,CV_8UC(15));
\endcode
As noted in the introduction of this chapter, Mat::create()
will only allocate a new matrix when the current matrix dimensionality
or type are different from the specified.
<li> by using a copy constructor or assignment operator, where on the right side it can
be a matrix or expression, see below. Again, as noted in the introduction,
matrix assignment is O(1) operation because it only copies the header
and increases the reference counter. cv::Mat::clone() method can be used to get a full
(a.k.a. deep) copy of the matrix when you need it.
<li> by constructing a header for a part of another matrix. It can be a single row, single column,
several rows, several columns, rectangular region in the matrix (called a minor in algebra) or
a diagonal. Such operations are also O(1), because the new header will reference the same data.
You can actually modify a part of the matrix using this feature, e.g.
\code
// add 5-th row, multiplied by 3 to the 3rd row
M.row(3) = M.row(3) + M.row(5)*3;
// now copy 7-th column to the 1-st column
// M.col(1) = M.col(7); // this will not work
Mat M1 = M.col(1);
M.col(7).copyTo(M1);
// create new 320x240 image
cv::Mat img(Size(320,240),CV_8UC3);
// select a roi
cv::Mat roi(img, Rect(10,10,100,100));
// fill the ROI with (0,255,0) (which is green in RGB space);
// the original 320x240 image will be modified
roi = Scalar(0,255,0);
\endcode
Thanks to the additional cv::Mat::datastart and cv::Mat::dataend members, it is possible to
compute the relative sub-matrix position in the main "container" matrix using cv::Mat::locateROI():
\code
Mat A = Mat::eye(10, 10, CV_32S);
// extracts A columns, 1 (inclusive) to 3 (exclusive).
Mat B = A(Range::all(), Range(1, 3));
// extracts B rows, 5 (inclusive) to 9 (exclusive).
// that is, C ~ A(Range(5, 9), Range(1, 3))
Mat C = B(Range(5, 9), Range::all());
Size size; Point ofs;
C.locateROI(size, ofs);
// size will be (width=10,height=10) and the ofs will be (x=1, y=5)
\endcode
As in the case of whole matrices, if you need a deep copy, use cv::Mat::clone() method
of the extracted sub-matrices.
<li> by making a header for user-allocated-data. It can be useful for
<ol>
<li> processing "foreign" data using OpenCV (e.g. when you implement
a DirectShow filter or a processing module for gstreamer etc.), e.g.
\code
void process_video_frame(const unsigned char* pixels,
int width, int height, int step)
{
cv::Mat img(height, width, CV_8UC3, pixels, step);
cv::GaussianBlur(img, img, cv::Size(7,7), 1.5, 1.5);
}
\endcode
<li> for quick initialization of small matrices and/or super-fast element access
\code
double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};
cv::Mat M = cv::Mat(3, 3, CV_64F, m).inv();
\endcode
</ol>
partial yet very common cases of this "user-allocated data" case are conversions
from CvMat and IplImage to cv::Mat. For this purpose there are special constructors
taking pointers to CvMat or IplImage and the optional
flag indicating whether to copy the data or not.
Backward conversion from cv::Mat to CvMat or IplImage is provided via cast operators
cv::Mat::operator CvMat() an cv::Mat::operator IplImage().
The operators do not copy the data.
\code
IplImage* img = cvLoadImage("greatwave.jpg", 1);
Mat mtx(img); // convert IplImage* -> cv::Mat
CvMat oldmat = mtx; // convert cv::Mat -> CvMat
CV_Assert(oldmat.cols == img->width && oldmat.rows == img->height &&
oldmat.data.ptr == (uchar*)img->imageData && oldmat.step == img->widthStep);
\endcode
<li> by using MATLAB-style matrix initializers, cv::Mat::zeros(), cv::Mat::ones(), cv::Mat::eye(), e.g.:
\code
// create a double-precision identity martix and add it to M.
M += Mat::eye(M.rows, M.cols, CV_64F);
\endcode
<li> by using comma-separated initializer:
\code
// create 3x3 double-precision identity matrix
Mat M = (Mat_<double>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);
\endcode
here we first call constructor of cv::Mat_ class (that we describe further) with the proper matrix,
and then we just put "<<" operator followed by comma-separated values that can be constants,
variables, expressions etc. Also, note the extra parentheses that are needed to avoid compiler errors.
</ul>
Once matrix is created, it will be automatically managed by using reference-counting mechanism
(unless the matrix header is built on top of user-allocated data,
in which case you should handle the data by yourself).
The matrix data will be deallocated when no one points to it;
if you want to release the data pointed by a matrix header before the matrix destructor is called,
use cv::Mat::release().
The next important thing to learn about the matrix class is element access. Here is how the matrix is stored.
The elements are stored in row-major order (row by row). The cv::Mat::data member points to the first element of the first row,
cv::Mat::rows contains the number of matrix rows and cv::Mat::cols - the number of matrix columns. There is yet another member,
cv::Mat::step that is used to actually compute address of a matrix element. cv::Mat::step is needed because the matrix can be
a part of another matrix or because there can some padding space in the end of each row for a proper alignment.
\image html roi.png
Given these parameters, address of the matrix element M_{ij} is computed as following:
addr(M_{ij})=M.data + M.step*i + j*M.elemSize()
if you know the matrix element type, e.g. it is float, then you can use cv::Mat::at() method:
addr(M_{ij})=&M.at<float>(i,j)
(where & is used to convert the reference returned by cv::Mat::at() to a pointer).
if you need to process a whole row of matrix, the most efficient way is to get
the pointer to the row first, and then just use plain C operator []:
\code
// compute sum of positive matrix elements
// (assuming that M is double-precision matrix)
double sum=0;
for(int i = 0; i < M.rows; i++)
{
const double* Mi = M.ptr<double>(i);
for(int j = 0; j < M.cols; j++)
sum += std::max(Mi[j], 0.);
}
\endcode
Some operations, like the above one, do not actually depend on the matrix shape,
they just process elements of a matrix one by one (or elements from multiple matrices
that are sitting in the same place, e.g. matrix addition). Such operations are called
element-wise and it makes sense to check whether all the input/output matrices are continuous,
i.e. have no gaps in the end of each row, and if yes, process them as a single long row:
\code
// compute sum of positive matrix elements, optimized variant
double sum=0;
int cols = M.cols, rows = M.rows;
if(M.isContinuous())
{
cols *= rows;
rows = 1;
}
for(int i = 0; i < rows; i++)
{
const double* Mi = M.ptr<double>(i);
for(int j = 0; j < cols; j++)
sum += std::max(Mi[j], 0.);
}
\endcode
in the case of continuous matrix the outer loop body will be executed just once,
so the overhead will be smaller, which will be especially noticeable in the case of small matrices.
Finally, there are STL-style iterators that are smart enough to skip gaps between successive rows:
\code
// compute sum of positive matrix elements, iterator-based variant
double sum=0;
MatConstIterator_<double> it = M.begin<double>(), it_end = M.end<double>();
for(; it != it_end; ++it)
sum += std::max(*it, 0.);
\endcode
The matrix iterators are random-access iterators, so they can be passed
to any STL algorithm, including std::sort().
*/
class CV_EXPORTS Mat
{
public:
//! default constructor
Mat();
//! constructs 2D matrix of the specified size and type
// (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
Mat(int rows, int cols, int type);
Mat(Size size, int type);
//! constucts 2D matrix and fills it with the specified value _s.
Mat(int rows, int cols, int type, const Scalar& s);
Mat(Size size, int type, const Scalar& s);
//! constructs n-dimensional matrix
Mat(int ndims, const int* sizes, int type);
Mat(int ndims, const int* sizes, int type, const Scalar& s);
//! copy constructor
Mat(const Mat& m);
//! constructor for matrix headers pointing to user-allocated data
Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);
Mat(Size size, int type, void* data, size_t step=AUTO_STEP);
Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0);
//! creates a matrix header for a part of the bigger matrix
Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());
Mat(const Mat& m, const Rect& roi);
Mat(const Mat& m, const Range* ranges);
//! converts old-style CvMat to the new matrix; the data is not copied by default
Mat(const CvMat* m, bool copyData=false);
//! converts old-style CvMatND to the new matrix; the data is not copied by default
Mat(const CvMatND* m, bool copyData=false);
//! converts old-style IplImage to the new matrix; the data is not copied by default
Mat(const IplImage* img, bool copyData=false);
//! builds matrix from std::vector with or without copying the data
template<typename _Tp> explicit Mat(const vector<_Tp>& vec, bool copyData=false);
//! builds matrix from cv::Vec; the data is copied by default
template<typename _Tp, int n> explicit Mat(const Vec<_Tp, n>& vec, bool copyData=true);
//! builds matrix from cv::Matx; the data is copied by default
template<typename _Tp, int m, int n> explicit Mat(const Matx<_Tp, m, n>& mtx, bool copyData=true);
//! builds matrix from a 2D point
template<typename _Tp> explicit Mat(const Point_<_Tp>& pt, bool copyData=true);
//! builds matrix from a 3D point
template<typename _Tp> explicit Mat(const Point3_<_Tp>& pt, bool copyData=true);
//! builds matrix from comma initializer
template<typename _Tp> explicit Mat(const MatCommaInitializer_<_Tp>& commaInitializer);
//! download data from GpuMat
explicit Mat(const gpu::GpuMat& m);
//! destructor - calls release()
~Mat();
//! assignment operators
Mat& operator = (const Mat& m);
Mat& operator = (const MatExpr& expr);
//! returns a new matrix header for the specified row
Mat row(int y) const;
//! returns a new matrix header for the specified column
Mat col(int x) const;
//! ... for the specified row span
Mat rowRange(int startrow, int endrow) const;
Mat rowRange(const Range& r) const;
//! ... for the specified column span
Mat colRange(int startcol, int endcol) const;
Mat colRange(const Range& r) const;
//! ... for the specified diagonal
// (d=0 - the main diagonal,
// >0 - a diagonal from the lower half,
// <0 - a diagonal from the upper half)
Mat diag(int d=0) const;
//! constructs a square diagonal matrix which main diagonal is vector "d"
static Mat diag(const Mat& d);
//! returns deep copy of the matrix, i.e. the data is copied
Mat clone() const;
//! copies the matrix content to "m".
// It calls m.create(this->size(), this->type()).
void copyTo( OutputArray m ) const;
//! copies those matrix elements to "m" that are marked with non-zero mask elements.
void copyTo( OutputArray m, InputArray mask ) const;
//! converts matrix to another datatype with optional scalng. See cvConvertScale.
void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;
void assignTo( Mat& m, int type=-1 ) const;
//! sets every matrix element to s
Mat& operator = (const Scalar& s);
//! sets some of the matrix elements to s, according to the mask
Mat& setTo(InputArray value, InputArray mask=noArray());
//! creates alternative matrix header for the same data, with different
// number of channels and/or different number of rows. see cvReshape.
Mat reshape(int cn, int rows=0) const;
Mat reshape(int cn, int newndims, const int* newsz) const;
//! matrix transposition by means of matrix expressions
MatExpr t() const;
//! matrix inversion by means of matrix expressions
MatExpr inv(int method=DECOMP_LU) const;
//! per-element matrix multiplication by means of matrix expressions
MatExpr mul(InputArray m, double scale=1) const;
//! computes cross-product of 2 3D vectors
Mat cross(InputArray m) const;
//! computes dot-product
double dot(InputArray m) const;
//! Matlab-style matrix initialization
static MatExpr zeros(int rows, int cols, int type);
static MatExpr zeros(Size size, int type);
static MatExpr zeros(int ndims, const int* sz, int type);
static MatExpr ones(int rows, int cols, int type);
static MatExpr ones(Size size, int type);
static MatExpr ones(int ndims, const int* sz, int type);
static MatExpr eye(int rows, int cols, int type);
static MatExpr eye(Size size, int type);
//! allocates new matrix data unless the matrix already has specified size and type.
// previous data is unreferenced if needed.
void create(int rows, int cols, int type);
void create(Size size, int type);
void create(int ndims, const int* sizes, int type);
//! increases the reference counter; use with care to avoid memleaks
void addref();
//! decreases reference counter;
// deallocates the data when reference counter reaches 0.
void release();
//! deallocates the matrix data
void deallocate();
//! internal use function; properly re-allocates _size, _step arrays
void copySize(const Mat& m);
//! reserves enough space to fit sz hyper-planes
void reserve(size_t sz);
//! resizes matrix to the specified number of hyper-planes
void resize(size_t sz);
//! resizes matrix to the specified number of hyper-planes; initializes the newly added elements
void resize(size_t sz, const Scalar& s);
//! internal function
void push_back_(const void* elem);
//! adds element to the end of 1d matrix (or possibly multiple elements when _Tp=Mat)
template<typename _Tp> void push_back(const _Tp& elem);
template<typename _Tp> void push_back(const Mat_<_Tp>& elem);
void push_back(const Mat& m);
//! removes several hyper-planes from bottom of the matrix
void pop_back(size_t nelems=1);
//! locates matrix header within a parent matrix. See below
void locateROI( Size& wholeSize, Point& ofs ) const;
//! moves/resizes the current matrix ROI inside the parent matrix.
Mat& adjustROI( int dtop, int dbottom, int dleft, int dright );
//! extracts a rectangular sub-matrix
// (this is a generalized form of row, rowRange etc.)
Mat operator()( Range rowRange, Range colRange ) const;
Mat operator()( const Rect& roi ) const;
Mat operator()( const Range* ranges ) const;
//! converts header to CvMat; no data is copied
operator CvMat() const;
//! converts header to CvMatND; no data is copied
operator CvMatND() const;
//! converts header to IplImage; no data is copied
operator IplImage() const;
template<typename _Tp> operator vector<_Tp>() const;
template<typename _Tp, int n> operator Vec<_Tp, n>() const;
template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const;
//! returns true iff the matrix data is continuous
// (i.e. when there are no gaps between successive rows).
// similar to CV_IS_MAT_CONT(cvmat->type)
bool isContinuous() const;
//! returns true if the matrix is a submatrix of another matrix
bool isSubmatrix() const;
//! returns element size in bytes,
// similar to CV_ELEM_SIZE(cvmat->type)
size_t elemSize() const;
//! returns the size of element channel in bytes.
size_t elemSize1() const;
//! returns element type, similar to CV_MAT_TYPE(cvmat->type)
int type() const;
//! returns element type, similar to CV_MAT_DEPTH(cvmat->type)
int depth() const;
//! returns element type, similar to CV_MAT_CN(cvmat->type)
int channels() const;
//! returns step/elemSize1()
size_t step1(int i=0) const;
//! returns true if matrix data is NULL
bool empty() const;
//! returns the total number of matrix elements
size_t total() const;
//! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise
int checkVector(int elemChannels, int depth=-1, bool requireContinuous=true) const;
//! returns pointer to i0-th submatrix along the dimension #0
uchar* ptr(int i0=0);
const uchar* ptr(int i0=0) const;
//! returns pointer to (i0,i1) submatrix along the dimensions #0 and #1
uchar* ptr(int i0, int i1);
const uchar* ptr(int i0, int i1) const;
//! returns pointer to (i0,i1,i3) submatrix along the dimensions #0, #1, #2
uchar* ptr(int i0, int i1, int i2);
const uchar* ptr(int i0, int i1, int i2) const;
//! returns pointer to the matrix element
uchar* ptr(const int* idx);
//! returns read-only pointer to the matrix element
const uchar* ptr(const int* idx) const;
template<int n> uchar* ptr(const Vec<int, n>& idx);
template<int n> const uchar* ptr(const Vec<int, n>& idx) const;
//! template version of the above method
template<typename _Tp> _Tp* ptr(int i0=0);
template<typename _Tp> const _Tp* ptr(int i0=0) const;
template<typename _Tp> _Tp* ptr(int i0, int i1);
template<typename _Tp> const _Tp* ptr(int i0, int i1) const;
template<typename _Tp> _Tp* ptr(int i0, int i1, int i2);
template<typename _Tp> const _Tp* ptr(int i0, int i1, int i2) const;
template<typename _Tp> _Tp* ptr(const int* idx);
template<typename _Tp> const _Tp* ptr(const int* idx) const;
template<typename _Tp, int n> _Tp* ptr(const Vec<int, n>& idx);
template<typename _Tp, int n> const _Tp* ptr(const Vec<int, n>& idx) const;
//! the same as above, with the pointer dereferencing
template<typename _Tp> _Tp& at(int i0=0);
template<typename _Tp> const _Tp& at(int i0=0) const;
template<typename _Tp> _Tp& at(int i0, int i1);
template<typename _Tp> const _Tp& at(int i0, int i1) const;
template<typename _Tp> _Tp& at(int i0, int i1, int i2);
template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;
template<typename _Tp> _Tp& at(const int* idx);
template<typename _Tp> const _Tp& at(const int* idx) const;
template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx);
template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const;
//! special versions for 2D arrays (especially convenient for referencing image pixels)
template<typename _Tp> _Tp& at(Point pt);
template<typename _Tp> const _Tp& at(Point pt) const;
//! template methods for iteration over matrix elements.
// the iterators take care of skipping gaps in the end of rows (if any)
template<typename _Tp> MatIterator_<_Tp> begin();
template<typename _Tp> MatIterator_<_Tp> end();
template<typename _Tp> MatConstIterator_<_Tp> begin() const;
template<typename _Tp> MatConstIterator_<_Tp> end() const;
enum { MAGIC_VAL=0x42FF0000, AUTO_STEP=0, CONTINUOUS_FLAG=CV_MAT_CONT_FLAG, SUBMATRIX_FLAG=CV_SUBMAT_FLAG };
/*! includes several bit-fields:
- the magic signature
- continuity flag
- depth
- number of channels
*/
int flags;
//! the matrix dimensionality, >= 2
int dims;
//! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
int rows, cols;
//! pointer to the data
uchar* data;
//! pointer to the reference counter;
// when matrix points to user-allocated data, the pointer is NULL
_Atomic_word* refcount;
//! helper fields used in locateROI and adjustROI
uchar* datastart;
uchar* dataend;
uchar* datalimit;
//! custom allocator
MatAllocator* allocator;
struct CV_EXPORTS MSize
{
MSize(int* _p);
Size operator()() const;
const int& operator[](int i) const;
int& operator[](int i);
operator const int*() const;
bool operator == (const MSize& sz) const;
bool operator != (const MSize& sz) const;
int* p;
};
struct CV_EXPORTS MStep
{
MStep();
MStep(size_t s);
const size_t& operator[](int i) const;
size_t& operator[](int i);
operator size_t() const;
MStep& operator = (size_t s);
size_t* p;
size_t buf[2];
protected:
MStep& operator = (const MStep&);
};
MSize size;
MStep step;
protected:
void initEmpty();
};
/*!
Random Number Generator
The class implements RNG using Multiply-with-Carry algorithm
*/
class CV_EXPORTS RNG
{
public:
enum { UNIFORM=0, NORMAL=1 };
RNG();
RNG(uint64 state);
//! updates the state and returns the next 32-bit unsigned integer random number
unsigned next();
operator uchar();
operator schar();
operator ushort();
operator short();
operator unsigned();
//! returns a random integer sampled uniformly from [0, N).
unsigned operator ()(unsigned N);
unsigned operator ()();
operator int();
operator float();
operator double();
//! returns uniformly distributed integer random number from [a,b) range
int uniform(int a, int b);
//! returns uniformly distributed floating-point random number from [a,b) range
float uniform(float a, float b);
//! returns uniformly distributed double-precision floating-point random number from [a,b) range
double uniform(double a, double b);
void fill( InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange=false );
//! returns Gaussian random variate with mean zero.
double gaussian(double sigma);
uint64 state;
};
/*!
Random Number Generator - MT
The class implements RNG using the Mersenne Twister algorithm
*/
class CV_EXPORTS RNG_MT19937
{
public:
RNG_MT19937();
RNG_MT19937(unsigned s);
void seed(unsigned s);
unsigned next();
operator int();
operator unsigned();
operator float();
operator double();
unsigned operator ()(unsigned N);
unsigned operator ()();
//! returns uniformly distributed integer random number from [a,b) range
int uniform(int a, int b);
//! returns uniformly distributed floating-point random number from [a,b) range
float uniform(float a, float b);
//! returns uniformly distributed double-precision floating-point random number from [a,b) range
double uniform(double a, double b);
private:
enum PeriodParameters {N = 624, M = 397};
unsigned state[N];
int mti;
};
/*!
Termination criteria in iterative algorithms
*/
class CV_EXPORTS TermCriteria
{
public:
enum
{
COUNT=1, //!< the maximum number of iterations or elements to compute
MAX_ITER=COUNT, //!< ditto
EPS=2 //!< the desired accuracy or change in parameters at which the iterative algorithm stops
};
//! default constructor
TermCriteria();
//! full constructor
TermCriteria(int type, int maxCount, double epsilon);
//! conversion from CvTermCriteria
TermCriteria(const CvTermCriteria& criteria);
//! conversion to CvTermCriteria
operator CvTermCriteria() const;
int type; //!< the type of termination criteria: COUNT, EPS or COUNT + EPS
int maxCount; // the maximum number of iterations/elements
double epsilon; // the desired accuracy
};
typedef void (*BinaryFunc)(const uchar* src1, size_t step1,
const uchar* src2, size_t step2,
uchar* dst, size_t step, Size sz,
void*);
CV_EXPORTS BinaryFunc getConvertFunc(int sdepth, int ddepth);
CV_EXPORTS BinaryFunc getConvertScaleFunc(int sdepth, int ddepth);
CV_EXPORTS BinaryFunc getCopyMaskFunc(size_t esz);
//! swaps two matrices
CV_EXPORTS void swap(Mat& a, Mat& b);
//! converts array (CvMat or IplImage) to cv::Mat
CV_EXPORTS Mat cvarrToMat(const CvArr* arr, bool copyData=false,
bool allowND=true, int coiMode=0);
//! extracts Channel of Interest from CvMat or IplImage and makes cv::Mat out of it.
CV_EXPORTS void extractImageCOI(const CvArr* arr, OutputArray coiimg, int coi=-1);
//! inserts single-channel cv::Mat into a multi-channel CvMat or IplImage
CV_EXPORTS void insertImageCOI(InputArray coiimg, CvArr* arr, int coi=-1);
//! adds one matrix to another (dst = src1 + src2)
CV_EXPORTS_W void add(InputArray src1, InputArray src2, OutputArray dst,
InputArray mask=noArray(), int dtype=-1);
//! subtracts one matrix from another (dst = src1 - src2)
CV_EXPORTS_W void subtract(InputArray src1, InputArray src2, OutputArray dst,
InputArray mask=noArray(), int dtype=-1);
//! computes element-wise weighted product of the two arrays (dst = scale*src1*src2)
CV_EXPORTS_W void multiply(InputArray src1, InputArray src2,
OutputArray dst, double scale=1, int dtype=-1);
//! computes element-wise weighted quotient of the two arrays (dst = scale*src1/src2)
CV_EXPORTS_W void divide(InputArray src1, InputArray src2, OutputArray dst,
double scale=1, int dtype=-1);
//! computes element-wise weighted reciprocal of an array (dst = scale/src2)
CV_EXPORTS_W void divide(double scale, InputArray src2,
OutputArray dst, int dtype=-1);
//! adds scaled array to another one (dst = alpha*src1 + src2)
CV_EXPORTS_W void scaleAdd(InputArray src1, double alpha, InputArray src2, OutputArray dst);
//! computes weighted sum of two arrays (dst = alpha*src1 + beta*src2 + gamma)
CV_EXPORTS_W void addWeighted(InputArray src1, double alpha, InputArray src2,
double beta, double gamma, OutputArray dst, int dtype=-1);
//! scales array elements, computes absolute values and converts the results to 8-bit unsigned integers: dst(i)=saturate_cast<uchar>abs(src(i)*alpha+beta)
CV_EXPORTS_W void convertScaleAbs(InputArray src, OutputArray dst,
double alpha=1, double beta=0);
//! transforms array of numbers using a lookup table: dst(i)=lut(src(i))
CV_EXPORTS_W void LUT(InputArray src, InputArray lut, OutputArray dst,
int interpolation=0);
//! computes sum of array elements
CV_EXPORTS_AS(sumElems) Scalar sum(InputArray src);
//! computes the number of nonzero array elements
CV_EXPORTS_W int countNonZero( InputArray src );
//! returns the list of locations of non-zero pixels
CV_EXPORTS_W void findNonZero( InputArray src, OutputArray idx );
//! computes mean value of selected array elements
CV_EXPORTS_W Scalar mean(InputArray src, InputArray mask=noArray());
//! computes mean value and standard deviation of all or selected array elements
CV_EXPORTS_W void meanStdDev(InputArray src, OutputArray mean, OutputArray stddev,
InputArray mask=noArray());
//! computes norm of the selected array part
CV_EXPORTS_W double norm(InputArray src1, int normType=NORM_L2, InputArray mask=noArray());
//! computes norm of selected part of the difference between two arrays
CV_EXPORTS_W double norm(InputArray src1, InputArray src2,
int normType=NORM_L2, InputArray mask=noArray());
//! naive nearest neighbor finder
CV_EXPORTS_W void batchDistance(InputArray src1, InputArray src2,
OutputArray dist, int dtype, OutputArray nidx,
int normType=NORM_L2, int K=0,
InputArray mask=noArray(), int update=0,
bool crosscheck=false);
//! scales and shifts array elements so that either the specified norm (alpha) or the minimum (alpha) and maximum (beta) array values get the specified values
CV_EXPORTS_W void normalize( InputArray src, OutputArray dst, double alpha=1, double beta=0,
int norm_type=NORM_L2, int dtype=-1, InputArray mask=noArray());
//! finds global minimum and maximum array elements and returns their values and their locations
CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT double* minVal,
CV_OUT double* maxVal=0, CV_OUT Point* minLoc=0,
CV_OUT Point* maxLoc=0, InputArray mask=noArray());
CV_EXPORTS void minMaxIdx(InputArray src, double* minVal, double* maxVal,
int* minIdx=0, int* maxIdx=0, InputArray mask=noArray());
//! transforms 2D matrix to 1D row or column vector by taking sum, minimum, maximum or mean value over all the rows
CV_EXPORTS_W void reduce(InputArray src, OutputArray dst, int dim, int rtype, int dtype=-1);
//! makes multi-channel array out of several single-channel arrays
CV_EXPORTS void merge(const Mat* mv, size_t count, OutputArray dst);
CV_EXPORTS void merge(const vector<Mat>& mv, OutputArray dst );
//! makes multi-channel array out of several single-channel arrays
CV_EXPORTS_W void merge(InputArrayOfArrays mv, OutputArray dst);
//! copies each plane of a multi-channel array to a dedicated array
CV_EXPORTS void split(const Mat& src, Mat* mvbegin);
CV_EXPORTS void split(const Mat& m, vector<Mat>& mv );
//! copies each plane of a multi-channel array to a dedicated array
CV_EXPORTS_W void split(InputArray m, OutputArrayOfArrays mv);
//! copies selected channels from the input arrays to the selected channels of the output arrays
CV_EXPORTS void mixChannels(const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts,
const int* fromTo, size_t npairs);
CV_EXPORTS void mixChannels(const vector<Mat>& src, vector<Mat>& dst,
const int* fromTo, size_t npairs);
CV_EXPORTS_W void mixChannels(InputArrayOfArrays src, InputArrayOfArrays dst,
const vector<int>& fromTo);
//! extracts a single channel from src (coi is 0-based index)
CV_EXPORTS_W void extractChannel(InputArray src, OutputArray dst, int coi);
//! inserts a single channel to dst (coi is 0-based index)
CV_EXPORTS_W void insertChannel(InputArray src, InputOutputArray dst, int coi);
//! reverses the order of the rows, columns or both in a matrix
CV_EXPORTS_W void flip(InputArray src, OutputArray dst, int flipCode);
//! replicates the input matrix the specified number of times in the horizontal and/or vertical direction
CV_EXPORTS_W void repeat(InputArray src, int ny, int nx, OutputArray dst);
CV_EXPORTS Mat repeat(const Mat& src, int ny, int nx);
CV_EXPORTS void hconcat(const Mat* src, size_t nsrc, OutputArray dst);
CV_EXPORTS void hconcat(InputArray src1, InputArray src2, OutputArray dst);
CV_EXPORTS_W void hconcat(InputArrayOfArrays src, OutputArray dst);
CV_EXPORTS void vconcat(const Mat* src, size_t nsrc, OutputArray dst);
CV_EXPORTS void vconcat(InputArray src1, InputArray src2, OutputArray dst);
CV_EXPORTS_W void vconcat(InputArrayOfArrays src, OutputArray dst);
//! computes bitwise conjunction of the two arrays (dst = src1 & src2)
CV_EXPORTS_W void bitwise_and(InputArray src1, InputArray src2,
OutputArray dst, InputArray mask=noArray());
//! computes bitwise disjunction of the two arrays (dst = src1 | src2)
CV_EXPORTS_W void bitwise_or(InputArray src1, InputArray src2,
OutputArray dst, InputArray mask=noArray());
//! computes bitwise exclusive-or of the two arrays (dst = src1 ^ src2)
CV_EXPORTS_W void bitwise_xor(InputArray src1, InputArray src2,
OutputArray dst, InputArray mask=noArray());
//! inverts each bit of array (dst = ~src)
CV_EXPORTS_W void bitwise_not(InputArray src, OutputArray dst,
InputArray mask=noArray());
//! computes element-wise absolute difference of two arrays (dst = abs(src1 - src2))
CV_EXPORTS_W void absdiff(InputArray src1, InputArray src2, OutputArray dst);
//! set mask elements for those array elements which are within the element-specific bounding box (dst = lowerb <= src && src < upperb)
CV_EXPORTS_W void inRange(InputArray src, InputArray lowerb,
InputArray upperb, OutputArray dst);
//! compares elements of two arrays (dst = src1 <cmpop> src2)
CV_EXPORTS_W void compare(InputArray src1, InputArray src2, OutputArray dst, int cmpop);
//! computes per-element minimum of two arrays (dst = min(src1, src2))
CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst);
//! computes per-element maximum of two arrays (dst = max(src1, src2))
CV_EXPORTS_W void max(InputArray src1, InputArray src2, OutputArray dst);
//! computes per-element minimum of two arrays (dst = min(src1, src2))
CV_EXPORTS void min(const Mat& src1, const Mat& src2, Mat& dst);
//! computes per-element minimum of array and scalar (dst = min(src1, src2))
CV_EXPORTS void min(const Mat& src1, double src2, Mat& dst);
//! computes per-element maximum of two arrays (dst = max(src1, src2))
CV_EXPORTS void max(const Mat& src1, const Mat& src2, Mat& dst);
//! computes per-element maximum of array and scalar (dst = max(src1, src2))
CV_EXPORTS void max(const Mat& src1, double src2, Mat& dst);
//! computes square root of each matrix element (dst = src**0.5)
CV_EXPORTS_W void sqrt(InputArray src, OutputArray dst);
//! raises the input matrix elements to the specified power (b = a**power)
CV_EXPORTS_W void pow(InputArray src, double power, OutputArray dst);
//! computes exponent of each matrix element (dst = e**src)
CV_EXPORTS_W void exp(InputArray src, OutputArray dst);
//! computes natural logarithm of absolute value of each matrix element: dst = log(abs(src))
CV_EXPORTS_W void log(InputArray src, OutputArray dst);
//! computes cube root of the argument
CV_EXPORTS_W float cubeRoot(float val);
//! computes the angle in degrees (0..360) of the vector (x,y)
CV_EXPORTS_W float fastAtan2(float y, float x);
CV_EXPORTS void exp(const float* src, float* dst, int n);
CV_EXPORTS void log(const float* src, float* dst, int n);
CV_EXPORTS void fastAtan2(const float* y, const float* x, float* dst, int n, bool angleInDegrees);
CV_EXPORTS void magnitude(const float* x, const float* y, float* dst, int n);
//! converts polar coordinates to Cartesian
CV_EXPORTS_W void polarToCart(InputArray magnitude, InputArray angle,
OutputArray x, OutputArray y, bool angleInDegrees=false);
//! converts Cartesian coordinates to polar
CV_EXPORTS_W void cartToPolar(InputArray x, InputArray y,
OutputArray magnitude, OutputArray angle,
bool angleInDegrees=false);
//! computes angle (angle(i)) of each (x(i), y(i)) vector
CV_EXPORTS_W void phase(InputArray x, InputArray y, OutputArray angle,
bool angleInDegrees=false);
//! computes magnitude (magnitude(i)) of each (x(i), y(i)) vector
CV_EXPORTS_W void magnitude(InputArray x, InputArray y, OutputArray magnitude);
//! checks that each matrix element is within the specified range.
CV_EXPORTS_W bool checkRange(InputArray a, bool quiet=true, CV_OUT Point* pos=0,
double minVal=-DBL_MAX, double maxVal=DBL_MAX);
//! converts NaN's to the given number
CV_EXPORTS_W void patchNaNs(InputOutputArray a, double val=0);
//! implements generalized matrix product algorithm GEMM from BLAS
CV_EXPORTS_W void gemm(InputArray src1, InputArray src2, double alpha,
InputArray src3, double gamma, OutputArray dst, int flags=0);
//! multiplies matrix by its transposition from the left or from the right
CV_EXPORTS_W void mulTransposed( InputArray src, OutputArray dst, bool aTa,
InputArray delta=noArray(),
double scale=1, int dtype=-1 );
//! transposes the matrix
CV_EXPORTS_W void transpose(InputArray src, OutputArray dst);
//! performs affine transformation of each element of multi-channel input matrix
CV_EXPORTS_W void transform(InputArray src, OutputArray dst, InputArray m );
//! performs perspective transformation of each element of multi-channel input matrix
CV_EXPORTS_W void perspectiveTransform(InputArray src, OutputArray dst, InputArray m );
//! extends the symmetrical matrix from the lower half or from the upper half
CV_EXPORTS_W void completeSymm(InputOutputArray mtx, bool lowerToUpper=false);
//! initializes scaled identity matrix
CV_EXPORTS_W void setIdentity(InputOutputArray mtx, const Scalar& s=Scalar(1));
//! computes determinant of a square matrix
CV_EXPORTS_W double determinant(InputArray mtx);
//! computes trace of a matrix
CV_EXPORTS_W Scalar trace(InputArray mtx);
//! computes inverse or pseudo-inverse matrix
CV_EXPORTS_W double invert(InputArray src, OutputArray dst, int flags=DECOMP_LU);
//! solves linear system or a least-square problem
CV_EXPORTS_W bool solve(InputArray src1, InputArray src2,
OutputArray dst, int flags=DECOMP_LU);
enum
{
SORT_EVERY_ROW=0,
SORT_EVERY_COLUMN=1,
SORT_ASCENDING=0,
SORT_DESCENDING=16
};
//! sorts independently each matrix row or each matrix column
CV_EXPORTS_W void sort(InputArray src, OutputArray dst, int flags);
//! sorts independently each matrix row or each matrix column
CV_EXPORTS_W void sortIdx(InputArray src, OutputArray dst, int flags);
//! finds real roots of a cubic polynomial
CV_EXPORTS_W int solveCubic(InputArray coeffs, OutputArray roots);
//! finds real and complex roots of a polynomial
CV_EXPORTS_W double solvePoly(InputArray coeffs, OutputArray roots, int maxIters=300);
//! finds eigenvalues of a symmetric matrix
CV_EXPORTS bool eigen(InputArray src, OutputArray eigenvalues, int lowindex=-1,
int highindex=-1);
//! finds eigenvalues and eigenvectors of a symmetric matrix
CV_EXPORTS bool eigen(InputArray src, OutputArray eigenvalues,
OutputArray eigenvectors,
int lowindex=-1, int highindex=-1);
CV_EXPORTS_W bool eigen(InputArray src, bool computeEigenvectors,
OutputArray eigenvalues, OutputArray eigenvectors);
enum
{
COVAR_SCRAMBLED=0,
COVAR_NORMAL=1,
COVAR_USE_AVG=2,
COVAR_SCALE=4,
COVAR_ROWS=8,
COVAR_COLS=16
};
//! computes covariation matrix of a set of samples
CV_EXPORTS void calcCovarMatrix( const Mat* samples, int nsamples, Mat& covar, Mat& mean,
int flags, int ctype=CV_64F);
//! computes covariation matrix of a set of samples
CV_EXPORTS_W void calcCovarMatrix( InputArray samples, OutputArray covar,
OutputArray mean, int flags, int ctype=CV_64F);
/*!
Principal Component Analysis
The class PCA is used to compute the special basis for a set of vectors.
The basis will consist of eigenvectors of the covariance matrix computed
from the input set of vectors. After PCA is performed, vectors can be transformed from
the original high-dimensional space to the subspace formed by a few most
prominent eigenvectors (called the principal components),
corresponding to the largest eigenvalues of the covariation matrix.
Thus the dimensionality of the vector and the correlation between the coordinates is reduced.
The following sample is the function that takes two matrices. The first one stores the set
of vectors (a row per vector) that is used to compute PCA, the second one stores another
"test" set of vectors (a row per vector) that are first compressed with PCA,
then reconstructed back and then the reconstruction error norm is computed and printed for each vector.
\code
using namespace cv;
PCA compressPCA(const Mat& pcaset, int maxComponents,
const Mat& testset, Mat& compressed)
{
PCA pca(pcaset, // pass the data
Mat(), // we do not have a pre-computed mean vector,
// so let the PCA engine to compute it
CV_PCA_DATA_AS_ROW, // indicate that the vectors
// are stored as matrix rows
// (use CV_PCA_DATA_AS_COL if the vectors are
// the matrix columns)
maxComponents // specify, how many principal components to retain
);
// if there is no test data, just return the computed basis, ready-to-use
if( !testset.data )
return pca;
CV_Assert( testset.cols == pcaset.cols );
compressed.create(testset.rows, maxComponents, testset.type());
Mat reconstructed;
for( int i = 0; i < testset.rows; i++ )
{
Mat vec = testset.row(i), coeffs = compressed.row(i), reconstructed;
// compress the vector, the result will be stored
// in the i-th row of the output matrix
pca.project(vec, coeffs);
// and then reconstruct it
pca.backProject(coeffs, reconstructed);
// and measure the error
printf("%d. diff = %g\n", i, norm(vec, reconstructed, NORM_L2));
}
return pca;
}
\endcode
*/
class CV_EXPORTS PCA
{
public:
//! default constructor
PCA();
//! the constructor that performs PCA
PCA(InputArray data, InputArray mean, int flags, int maxComponents=0);
PCA(InputArray data, InputArray mean, int flags, double retainedVariance);
//! operator that performs PCA. The previously stored data, if any, is released
PCA& operator()(InputArray data, InputArray mean, int flags, int maxComponents=0);
PCA& computeVar(InputArray data, InputArray mean, int flags, double retainedVariance);
//! projects vector from the original space to the principal components subspace
Mat project(InputArray vec) const;
//! projects vector from the original space to the principal components subspace
void project(InputArray vec, OutputArray result) const;
//! reconstructs the original vector from the projection
Mat backProject(InputArray vec) const;
//! reconstructs the original vector from the projection
void backProject(InputArray vec, OutputArray result) const;
Mat eigenvectors; //!< eigenvectors of the covariation matrix
Mat eigenvalues; //!< eigenvalues of the covariation matrix
Mat mean; //!< mean value subtracted before the projection and added after the back projection
};
CV_EXPORTS_W void PCACompute(InputArray data, CV_OUT InputOutputArray mean,
OutputArray eigenvectors, int maxComponents=0);
CV_EXPORTS_W void PCAComputeVar(InputArray data, CV_OUT InputOutputArray mean,
OutputArray eigenvectors, double retainedVariance);
CV_EXPORTS_W void PCAProject(InputArray data, InputArray mean,
InputArray eigenvectors, OutputArray result);
CV_EXPORTS_W void PCABackProject(InputArray data, InputArray mean,
InputArray eigenvectors, OutputArray result);
/*!
Singular Value Decomposition class
The class is used to compute Singular Value Decomposition of a floating-point matrix and then
use it to solve least-square problems, under-determined linear systems, invert matrices,
compute condition numbers etc.
For a bit faster operation you can pass flags=SVD::MODIFY_A|... to modify the decomposed matrix
when it is not necessarily to preserve it. If you want to compute condition number of a matrix
or absolute value of its determinant - you do not need SVD::u or SVD::vt,
so you can pass flags=SVD::NO_UV|... . Another flag SVD::FULL_UV indicates that the full-size SVD::u and SVD::vt
must be computed, which is not necessary most of the time.
*/
class CV_EXPORTS SVD
{
public:
enum { MODIFY_A=1, NO_UV=2, FULL_UV=4 };
//! the default constructor
SVD();
//! the constructor that performs SVD
SVD( InputArray src, int flags=0 );
//! the operator that performs SVD. The previously allocated SVD::u, SVD::w are SVD::vt are released.
SVD& operator ()( InputArray src, int flags=0 );
//! decomposes matrix and stores the results to user-provided matrices
static void compute( InputArray src, OutputArray w,
OutputArray u, OutputArray vt, int flags=0 );
//! computes singular values of a matrix
static void compute( InputArray src, OutputArray w, int flags=0 );
//! performs back substitution
static void backSubst( InputArray w, InputArray u,
InputArray vt, InputArray rhs,
OutputArray dst );
template<typename _Tp, int m, int n, int nm> static void compute( const Matx<_Tp, m, n>& a,
Matx<_Tp, nm, 1>& w, Matx<_Tp, m, nm>& u, Matx<_Tp, n, nm>& vt );
template<typename _Tp, int m, int n, int nm> static void compute( const Matx<_Tp, m, n>& a,
Matx<_Tp, nm, 1>& w );
template<typename _Tp, int m, int n, int nm, int nb> static void backSubst( const Matx<_Tp, nm, 1>& w,
const Matx<_Tp, m, nm>& u, const Matx<_Tp, n, nm>& vt, const Matx<_Tp, m, nb>& rhs, Matx<_Tp, n, nb>& dst );
//! finds dst = arg min_{|dst|=1} |m*dst|
static void solveZ( InputArray src, OutputArray dst );
//! performs back substitution, so that dst is the solution or pseudo-solution of m*dst = rhs, where m is the decomposed matrix
void backSubst( InputArray rhs, OutputArray dst ) const;
Mat u, w, vt;
};
//! computes SVD of src
CV_EXPORTS_W void SVDecomp( InputArray src, CV_OUT OutputArray w,
CV_OUT OutputArray u, CV_OUT OutputArray vt, int flags=0 );
//! performs back substitution for the previously computed SVD
CV_EXPORTS_W void SVBackSubst( InputArray w, InputArray u, InputArray vt,
InputArray rhs, CV_OUT OutputArray dst );
//! computes Mahalanobis distance between two vectors: sqrt((v1-v2)'*icovar*(v1-v2)), where icovar is the inverse covariation matrix
CV_EXPORTS_W double Mahalanobis(InputArray v1, InputArray v2, InputArray icovar);
//! a synonym for Mahalanobis
CV_EXPORTS double Mahalonobis(InputArray v1, InputArray v2, InputArray icovar);
//! performs forward or inverse 1D or 2D Discrete Fourier Transformation
CV_EXPORTS_W void dft(InputArray src, OutputArray dst, int flags=0, int nonzeroRows=0);
//! performs inverse 1D or 2D Discrete Fourier Transformation
CV_EXPORTS_W void idft(InputArray src, OutputArray dst, int flags=0, int nonzeroRows=0);
//! performs forward or inverse 1D or 2D Discrete Cosine Transformation
CV_EXPORTS_W void dct(InputArray src, OutputArray dst, int flags=0);
//! performs inverse 1D or 2D Discrete Cosine Transformation
CV_EXPORTS_W void idct(InputArray src, OutputArray dst, int flags=0);
//! computes element-wise product of the two Fourier spectrums. The second spectrum can optionally be conjugated before the multiplication
CV_EXPORTS_W void mulSpectrums(InputArray a, InputArray b, OutputArray c,
int flags, bool conjB=false);
//! computes the minimal vector size vecsize1 >= vecsize so that the dft() of the vector of length vecsize1 can be computed efficiently
CV_EXPORTS_W int getOptimalDFTSize(int vecsize);
/*!
Various k-Means flags
*/
enum
{
KMEANS_RANDOM_CENTERS=0, // Chooses random centers for k-Means initialization
KMEANS_PP_CENTERS=2, // Uses k-Means++ algorithm for initialization
KMEANS_USE_INITIAL_LABELS=1 // Uses the user-provided labels for K-Means initialization
};
//! clusters the input data using k-Means algorithm
CV_EXPORTS_W double kmeans( InputArray data, int K, CV_OUT InputOutputArray bestLabels,
TermCriteria criteria, int attempts,
int flags, OutputArray centers=noArray() );
//! returns the thread-local Random number generator
CV_EXPORTS RNG& theRNG();
//! returns the next unifomly-distributed random number of the specified type
template<typename _Tp> static inline _Tp randu() { return (_Tp)theRNG(); }
//! fills array with uniformly-distributed random numbers from the range [low, high)
CV_EXPORTS_W void randu(InputOutputArray dst, InputArray low, InputArray high);
//! fills array with normally-distributed random numbers with the specified mean and the standard deviation
CV_EXPORTS_W void randn(InputOutputArray dst, InputArray mean, InputArray stddev);
//! shuffles the input array elements
CV_EXPORTS void randShuffle(InputOutputArray dst, double iterFactor=1., RNG* rng=0);
CV_EXPORTS_AS(randShuffle) void randShuffle_(InputOutputArray dst, double iterFactor=1.);
//! draws the line segment (pt1, pt2) in the image
CV_EXPORTS_W void line(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color,
int thickness=1, int lineType=8, int shift=0);
//! draws the rectangle outline or a solid rectangle with the opposite corners pt1 and pt2 in the image
CV_EXPORTS_W void rectangle(CV_IN_OUT Mat& img, Point pt1, Point pt2,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
//! draws the rectangle outline or a solid rectangle covering rec in the image
CV_EXPORTS void rectangle(CV_IN_OUT Mat& img, Rect rec,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
//! draws the circle outline or a solid circle in the image
CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
//! draws an elliptic arc, ellipse sector or a rotated ellipse in the image
CV_EXPORTS_W void ellipse(CV_IN_OUT Mat& img, Point center, Size axes,
double angle, double startAngle, double endAngle,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
//! draws a rotated ellipse in the image
CV_EXPORTS_W void ellipse(CV_IN_OUT Mat& img, const RotatedRect& box, const Scalar& color,
int thickness=1, int lineType=8);
//! draws a filled convex polygon in the image
CV_EXPORTS void fillConvexPoly(Mat& img, const Point* pts, int npts,
const Scalar& color, int lineType=8,
int shift=0);
CV_EXPORTS_W void fillConvexPoly(InputOutputArray img, InputArray points,
const Scalar& color, int lineType=8,
int shift=0);
//! fills an area bounded by one or more polygons
CV_EXPORTS void fillPoly(Mat& img, const Point** pts,
const int* npts, int ncontours,
const Scalar& color, int lineType=8, int shift=0,
Point offset=Point() );
CV_EXPORTS_W void fillPoly(InputOutputArray img, InputArrayOfArrays pts,
const Scalar& color, int lineType=8, int shift=0,
Point offset=Point() );
//! draws one or more polygonal curves
CV_EXPORTS void polylines(Mat& img, const Point** pts, const int* npts,
int ncontours, bool isClosed, const Scalar& color,
int thickness=1, int lineType=8, int shift=0 );
CV_EXPORTS_W void polylines(InputOutputArray img, InputArrayOfArrays pts,
bool isClosed, const Scalar& color,
int thickness=1, int lineType=8, int shift=0 );
//! clips the line segment by the rectangle Rect(0, 0, imgSize.width, imgSize.height)
CV_EXPORTS bool clipLine(Size imgSize, CV_IN_OUT Point& pt1, CV_IN_OUT Point& pt2);
//! clips the line segment by the rectangle imgRect
CV_EXPORTS_W bool clipLine(Rect imgRect, CV_OUT CV_IN_OUT Point& pt1, CV_OUT CV_IN_OUT Point& pt2);
/*!
Line iterator class
The class is used to iterate over all the pixels on the raster line
segment connecting two specified points.
*/
class CV_EXPORTS LineIterator
{
public:
//! intializes the iterator
LineIterator( const Mat& img, Point pt1, Point pt2,
int connectivity=8, bool leftToRight=false );
//! returns pointer to the current pixel
uchar* operator *();
//! prefix increment operator (++it). shifts iterator to the next pixel
LineIterator& operator ++();
//! postfix increment operator (it++). shifts iterator to the next pixel
LineIterator operator ++(int);
//! returns coordinates of the current pixel
Point pos() const;
uchar* ptr;
const uchar* ptr0;
int step, elemSize;
int err, count;
int minusDelta, plusDelta;
int minusStep, plusStep;
};
//! converts elliptic arc to a polygonal curve
CV_EXPORTS_W void ellipse2Poly( Point center, Size axes, int angle,
int arcStart, int arcEnd, int delta,
CV_OUT vector<Point>& pts );
enum
{
FONT_HERSHEY_SIMPLEX = 0,
FONT_HERSHEY_PLAIN = 1,
FONT_HERSHEY_DUPLEX = 2,
FONT_HERSHEY_COMPLEX = 3,
FONT_HERSHEY_TRIPLEX = 4,
FONT_HERSHEY_COMPLEX_SMALL = 5,
FONT_HERSHEY_SCRIPT_SIMPLEX = 6,
FONT_HERSHEY_SCRIPT_COMPLEX = 7,
FONT_ITALIC = 16
};
//! renders text string in the image
CV_EXPORTS_W void putText( Mat& img, const string& text, Point org,
int fontFace, double fontScale, Scalar color,
int thickness=1, int lineType=8,
bool bottomLeftOrigin=false );
//! returns bounding box of the text string
CV_EXPORTS_W Size getTextSize(const string& text, int fontFace,
double fontScale, int thickness,
CV_OUT int* baseLine);
///////////////////////////////// Mat_<_Tp> ////////////////////////////////////
/*!
Template matrix class derived from Mat
The class Mat_ is a "thin" template wrapper on top of cv::Mat. It does not have any extra data fields,
nor it or cv::Mat have any virtual methods and thus references or pointers to these two classes
can be safely converted one to another. But do it with care, for example:
\code
// create 100x100 8-bit matrix
Mat M(100,100,CV_8U);
// this will compile fine. no any data conversion will be done.
Mat_<float>& M1 = (Mat_<float>&)M;
// the program will likely crash at the statement below
M1(99,99) = 1.f;
\endcode
While cv::Mat is sufficient in most cases, cv::Mat_ can be more convenient if you use a lot of element
access operations and if you know matrix type at compile time.
Note that cv::Mat::at<_Tp>(int y, int x) and cv::Mat_<_Tp>::operator ()(int y, int x) do absolutely the
same thing and run at the same speed, but the latter is certainly shorter:
\code
Mat_<double> M(20,20);
for(int i = 0; i < M.rows; i++)
for(int j = 0; j < M.cols; j++)
M(i,j) = 1./(i+j+1);
Mat E, V;
eigen(M,E,V);
cout << E.at<double>(0,0)/E.at<double>(M.rows-1,0);
\endcode
It is easy to use Mat_ for multi-channel images/matrices - just pass cv::Vec as cv::Mat_ template parameter:
\code
// allocate 320x240 color image and fill it with green (in RGB space)
Mat_<Vec3b> img(240, 320, Vec3b(0,255,0));
// now draw a diagonal white line
for(int i = 0; i < 100; i++)
img(i,i)=Vec3b(255,255,255);
// and now modify the 2nd (red) channel of each pixel
for(int i = 0; i < img.rows; i++)
for(int j = 0; j < img.cols; j++)
img(i,j)[2] ^= (uchar)(i ^ j); // img(y,x)[c] accesses c-th channel of the pixel (x,y)
\endcode
*/
template<typename _Tp> class Mat_ : public Mat
{
public:
typedef _Tp value_type;
typedef typename DataType<_Tp>::channel_type channel_type;
typedef MatIterator_<_Tp> iterator;
typedef MatConstIterator_<_Tp> const_iterator;
//! default constructor
Mat_();
//! equivalent to Mat(_rows, _cols, DataType<_Tp>::type)
Mat_(int _rows, int _cols);
//! constructor that sets each matrix element to specified value
Mat_(int _rows, int _cols, const _Tp& value);
//! equivalent to Mat(_size, DataType<_Tp>::type)
explicit Mat_(Size _size);
//! constructor that sets each matrix element to specified value
Mat_(Size _size, const _Tp& value);
//! n-dim array constructor
Mat_(int _ndims, const int* _sizes);
//! n-dim array constructor that sets each matrix element to specified value
Mat_(int _ndims, const int* _sizes, const _Tp& value);
//! copy/conversion contructor. If m is of different type, it's converted
Mat_(const Mat& m);
//! copy constructor
Mat_(const Mat_& m);
//! constructs a matrix on top of user-allocated data. step is in bytes(!!!), regardless of the type
Mat_(int _rows, int _cols, _Tp* _data, size_t _step=AUTO_STEP);
//! constructs n-dim matrix on top of user-allocated data. steps are in bytes(!!!), regardless of the type
Mat_(int _ndims, const int* _sizes, _Tp* _data, const size_t* _steps=0);
//! selects a submatrix
Mat_(const Mat_& m, const Range& rowRange, const Range& colRange=Range::all());
//! selects a submatrix
Mat_(const Mat_& m, const Rect& roi);
//! selects a submatrix, n-dim version
Mat_(const Mat_& m, const Range* ranges);
//! from a matrix expression
explicit Mat_(const MatExpr& e);
//! makes a matrix out of Vec, std::vector, Point_ or Point3_. The matrix will have a single column
explicit Mat_(const vector<_Tp>& vec, bool copyData=false);
template<int n> explicit Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData=true);
template<int m, int n> explicit Mat_(const Matx<typename DataType<_Tp>::channel_type, m, n>& mtx, bool copyData=true);
explicit Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData=true);
explicit Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData=true);
explicit Mat_(const MatCommaInitializer_<_Tp>& commaInitializer);
Mat_& operator = (const Mat& m);
Mat_& operator = (const Mat_& m);
//! set all the elements to s.
Mat_& operator = (const _Tp& s);
//! assign a matrix expression
Mat_& operator = (const MatExpr& e);
//! iterators; they are smart enough to skip gaps in the end of rows
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
//! equivalent to Mat::create(_rows, _cols, DataType<_Tp>::type)
void create(int _rows, int _cols);
//! equivalent to Mat::create(_size, DataType<_Tp>::type)
void create(Size _size);
//! equivalent to Mat::create(_ndims, _sizes, DatType<_Tp>::type)
void create(int _ndims, const int* _sizes);
//! cross-product
Mat_ cross(const Mat_& m) const;
//! data type conversion
template<typename T2> operator Mat_<T2>() const;
//! overridden forms of Mat::row() etc.
Mat_ row(int y) const;
Mat_ col(int x) const;
Mat_ diag(int d=0) const;
Mat_ clone() const;
//! overridden forms of Mat::elemSize() etc.
size_t elemSize() const;
size_t elemSize1() const;
int type() const;
int depth() const;
int channels() const;
size_t step1(int i=0) const;
//! returns step()/sizeof(_Tp)
size_t stepT(int i=0) const;
//! overridden forms of Mat::zeros() etc. Data type is omitted, of course
static MatExpr zeros(int rows, int cols);
static MatExpr zeros(Size size);
static MatExpr zeros(int _ndims, const int* _sizes);
static MatExpr ones(int rows, int cols);
static MatExpr ones(Size size);
static MatExpr ones(int _ndims, const int* _sizes);
static MatExpr eye(int rows, int cols);
static MatExpr eye(Size size);
//! some more overriden methods
Mat_& adjustROI( int dtop, int dbottom, int dleft, int dright );
Mat_ operator()( const Range& rowRange, const Range& colRange ) const;
Mat_ operator()( const Rect& roi ) const;
Mat_ operator()( const Range* ranges ) const;
//! more convenient forms of row and element access operators
_Tp* operator [](int y);
const _Tp* operator [](int y) const;
//! returns reference to the specified element
_Tp& operator ()(const int* idx);
//! returns read-only reference to the specified element
const _Tp& operator ()(const int* idx) const;
//! returns reference to the specified element
template<int n> _Tp& operator ()(const Vec<int, n>& idx);
//! returns read-only reference to the specified element
template<int n> const _Tp& operator ()(const Vec<int, n>& idx) const;
//! returns reference to the specified element (1D case)
_Tp& operator ()(int idx0);
//! returns read-only reference to the specified element (1D case)
const _Tp& operator ()(int idx0) const;
//! returns reference to the specified element (2D case)
_Tp& operator ()(int idx0, int idx1);
//! returns read-only reference to the specified element (2D case)
const _Tp& operator ()(int idx0, int idx1) const;
//! returns reference to the specified element (3D case)
_Tp& operator ()(int idx0, int idx1, int idx2);
//! returns read-only reference to the specified element (3D case)
const _Tp& operator ()(int idx0, int idx1, int idx2) const;
_Tp& operator ()(Point pt);
const _Tp& operator ()(Point pt) const;
//! conversion to vector.
operator vector<_Tp>() const;
//! conversion to Vec
template<int n> operator Vec<typename DataType<_Tp>::channel_type, n>() const;
//! conversion to Matx
template<int m, int n> operator Matx<typename DataType<_Tp>::channel_type, m, n>() const;
};
typedef Mat_<uchar> Mat1b;
typedef Mat_<Vec2b> Mat2b;
typedef Mat_<Vec3b> Mat3b;
typedef Mat_<Vec4b> Mat4b;
typedef Mat_<short> Mat1s;
typedef Mat_<Vec2s> Mat2s;
typedef Mat_<Vec3s> Mat3s;
typedef Mat_<Vec4s> Mat4s;
typedef Mat_<ushort> Mat1w;
typedef Mat_<Vec2w> Mat2w;
typedef Mat_<Vec3w> Mat3w;
typedef Mat_<Vec4w> Mat4w;
typedef Mat_<int> Mat1i;
typedef Mat_<Vec2i> Mat2i;
typedef Mat_<Vec3i> Mat3i;
typedef Mat_<Vec4i> Mat4i;
typedef Mat_<float> Mat1f;
typedef Mat_<Vec2f> Mat2f;
typedef Mat_<Vec3f> Mat3f;
typedef Mat_<Vec4f> Mat4f;
typedef Mat_<double> Mat1d;
typedef Mat_<Vec2d> Mat2d;
typedef Mat_<Vec3d> Mat3d;
typedef Mat_<Vec4d> Mat4d;
//////////// Iterators & Comma initializers //////////////////
class CV_EXPORTS MatConstIterator
{
public:
typedef uchar* value_type;
typedef ptrdiff_t difference_type;
typedef const uchar** pointer;
typedef uchar* reference;
typedef std::random_access_iterator_tag iterator_category;
//! default constructor
MatConstIterator();
//! constructor that sets the iterator to the beginning of the matrix
MatConstIterator(const Mat* _m);
//! constructor that sets the iterator to the specified element of the matrix
MatConstIterator(const Mat* _m, int _row, int _col=0);
//! constructor that sets the iterator to the specified element of the matrix
MatConstIterator(const Mat* _m, Point _pt);
//! constructor that sets the iterator to the specified element of the matrix
MatConstIterator(const Mat* _m, const int* _idx);
//! copy constructor
MatConstIterator(const MatConstIterator& it);
//! copy operator
MatConstIterator& operator = (const MatConstIterator& it);
//! returns the current matrix element
uchar* operator *() const;
//! returns the i-th matrix element, relative to the current
uchar* operator [](ptrdiff_t i) const;
//! shifts the iterator forward by the specified number of elements
MatConstIterator& operator += (ptrdiff_t ofs);
//! shifts the iterator backward by the specified number of elements
MatConstIterator& operator -= (ptrdiff_t ofs);
//! decrements the iterator
MatConstIterator& operator --();
//! decrements the iterator
MatConstIterator operator --(int);
//! increments the iterator
MatConstIterator& operator ++();
//! increments the iterator
MatConstIterator operator ++(int);
//! returns the current iterator position
Point pos() const;
//! returns the current iterator position
void pos(int* _idx) const;
ptrdiff_t lpos() const;
void seek(ptrdiff_t ofs, bool relative=false);
void seek(const int* _idx, bool relative=false);
const Mat* m;
size_t elemSize;
uchar* ptr;
uchar* sliceStart;
uchar* sliceEnd;
};
/*!
Matrix read-only iterator
*/
template<typename _Tp>
class MatConstIterator_ : public MatConstIterator
{
public:
typedef _Tp value_type;
typedef ptrdiff_t difference_type;
typedef const _Tp* pointer;
typedef const _Tp& reference;
typedef std::random_access_iterator_tag iterator_category;
//! default constructor
MatConstIterator_();
//! constructor that sets the iterator to the beginning of the matrix
MatConstIterator_(const Mat_<_Tp>* _m);
//! constructor that sets the iterator to the specified element of the matrix
MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col=0);
//! constructor that sets the iterator to the specified element of the matrix
MatConstIterator_(const Mat_<_Tp>* _m, Point _pt);
//! constructor that sets the iterator to the specified element of the matrix
MatConstIterator_(const Mat_<_Tp>* _m, const int* _idx);
//! copy constructor
MatConstIterator_(const MatConstIterator_& it);
//! copy operator
MatConstIterator_& operator = (const MatConstIterator_& it);
//! returns the current matrix element
_Tp operator *() const;
//! returns the i-th matrix element, relative to the current
_Tp operator [](ptrdiff_t i) const;
//! shifts the iterator forward by the specified number of elements
MatConstIterator_& operator += (ptrdiff_t ofs);
//! shifts the iterator backward by the specified number of elements
MatConstIterator_& operator -= (ptrdiff_t ofs);
//! decrements the iterator
MatConstIterator_& operator --();
//! decrements the iterator
MatConstIterator_ operator --(int);
//! increments the iterator
MatConstIterator_& operator ++();
//! increments the iterator
MatConstIterator_ operator ++(int);
//! returns the current iterator position
Point pos() const;
};
/*!
Matrix read-write iterator
*/
template<typename _Tp>
class MatIterator_ : public MatConstIterator_<_Tp>
{
public:
typedef _Tp* pointer;
typedef _Tp& reference;
typedef std::random_access_iterator_tag iterator_category;
//! the default constructor
MatIterator_();
//! constructor that sets the iterator to the beginning of the matrix
MatIterator_(Mat_<_Tp>* _m);
//! constructor that sets the iterator to the specified element of the matrix
MatIterator_(Mat_<_Tp>* _m, int _row, int _col=0);
//! constructor that sets the iterator to the specified element of the matrix
MatIterator_(const Mat_<_Tp>* _m, Point _pt);
//! constructor that sets the iterator to the specified element of the matrix
MatIterator_(const Mat_<_Tp>* _m, const int* _idx);
//! copy constructor
MatIterator_(const MatIterator_& it);
//! copy operator
MatIterator_& operator = (const MatIterator_<_Tp>& it );
//! returns the current matrix element
_Tp& operator *() const;
//! returns the i-th matrix element, relative to the current
_Tp& operator [](ptrdiff_t i) const;
//! shifts the iterator forward by the specified number of elements
MatIterator_& operator += (ptrdiff_t ofs);
//! shifts the iterator backward by the specified number of elements
MatIterator_& operator -= (ptrdiff_t ofs);
//! decrements the iterator
MatIterator_& operator --();
//! decrements the iterator
MatIterator_ operator --(int);
//! increments the iterator
MatIterator_& operator ++();
//! increments the iterator
MatIterator_ operator ++(int);
};
template<typename _Tp> class MatOp_Iter_;
/*!
Comma-separated Matrix Initializer
The class instances are usually not created explicitly.
Instead, they are created on "matrix << firstValue" operator.
The sample below initializes 2x2 rotation matrix:
\code
double angle = 30, a = cos(angle*CV_PI/180), b = sin(angle*CV_PI/180);
Mat R = (Mat_<double>(2,2) << a, -b, b, a);
\endcode
*/
template<typename _Tp> class MatCommaInitializer_
{
public:
//! the constructor, created by "matrix << firstValue" operator, where matrix is cv::Mat
MatCommaInitializer_(Mat_<_Tp>* _m);
//! the operator that takes the next value and put it to the matrix
template<typename T2> MatCommaInitializer_<_Tp>& operator , (T2 v);
//! another form of conversion operator
Mat_<_Tp> operator *() const;
operator Mat_<_Tp>() const;
protected:
MatIterator_<_Tp> it;
};
template<typename _Tp, int m, int n> class MatxCommaInitializer
{
public:
MatxCommaInitializer(Matx<_Tp, m, n>* _mtx);
template<typename T2> MatxCommaInitializer<_Tp, m, n>& operator , (T2 val);
Matx<_Tp, m, n> operator *() const;
Matx<_Tp, m, n>* dst;
int idx;
};
template<typename _Tp, int m> class VecCommaInitializer : public MatxCommaInitializer<_Tp, m, 1>
{
public:
VecCommaInitializer(Vec<_Tp, m>* _vec);
template<typename T2> VecCommaInitializer<_Tp, m>& operator , (T2 val);
Vec<_Tp, m> operator *() const;
};
/*!
Automatically Allocated Buffer Class
The class is used for temporary buffers in functions and methods.
If a temporary buffer is usually small (a few K's of memory),
but its size depends on the parameters, it makes sense to create a small
fixed-size array on stack and use it if it's large enough. If the required buffer size
is larger than the fixed size, another buffer of sufficient size is allocated dynamically
and released after the processing. Therefore, in typical cases, when the buffer size is small,
there is no overhead associated with malloc()/free().
At the same time, there is no limit on the size of processed data.
This is what AutoBuffer does. The template takes 2 parameters - type of the buffer elements and
the number of stack-allocated elements. Here is how the class is used:
\code
void my_func(const cv::Mat& m)
{
cv::AutoBuffer<float, 1000> buf; // create automatic buffer containing 1000 floats
buf.allocate(m.rows); // if m.rows <= 1000, the pre-allocated buffer is used,
// otherwise the buffer of "m.rows" floats will be allocated
// dynamically and deallocated in cv::AutoBuffer destructor
...
}
\endcode
*/
template<typename _Tp, size_t fixed_size=4096/sizeof(_Tp)+8> class AutoBuffer
{
public:
typedef _Tp value_type;
enum { buffer_padding = (int)((16 + sizeof(_Tp) - 1)/sizeof(_Tp)) };
//! the default contructor
AutoBuffer();
//! constructor taking the real buffer size
AutoBuffer(size_t _size);
//! destructor. calls deallocate()
~AutoBuffer();
//! allocates the new buffer of size _size. if the _size is small enough, stack-allocated buffer is used
void allocate(size_t _size);
//! deallocates the buffer if it was dynamically allocated
void deallocate();
//! returns pointer to the real buffer, stack-allocated or head-allocated
operator _Tp* ();
//! returns read-only pointer to the real buffer, stack-allocated or head-allocated
operator const _Tp* () const;
protected:
//! pointer to the real buffer, can point to buf if the buffer is small enough
_Tp* ptr;
//! size of the real buffer
size_t size;
//! pre-allocated buffer
_Tp buf[fixed_size+buffer_padding];
};
/////////////////////////// multi-dimensional dense matrix //////////////////////////
/*!
n-Dimensional Dense Matrix Iterator Class.
The class cv::NAryMatIterator is used for iterating over one or more n-dimensional dense arrays (cv::Mat's).
The iterator is completely different from cv::Mat_ and cv::SparseMat_ iterators.
It iterates through the slices (or planes), not the elements, where "slice" is a continuous part of the arrays.
Here is the example on how the iterator can be used to normalize 3D histogram:
\code
void normalizeColorHist(Mat& hist)
{
#if 1
// intialize iterator (the style is different from STL).
// after initialization the iterator will contain
// the number of slices or planes
// the iterator will go through
Mat* arrays[] = { &hist, 0 };
Mat planes[1];
NAryMatIterator it(arrays, planes);
double s = 0;
// iterate through the matrix. on each iteration
// it.planes[i] (of type Mat) will be set to the current plane of
// i-th n-dim matrix passed to the iterator constructor.
for(int p = 0; p < it.nplanes; p++, ++it)
s += sum(it.planes[0])[0];
it = NAryMatIterator(hist);
s = 1./s;
for(int p = 0; p < it.nplanes; p++, ++it)
it.planes[0] *= s;
#elif 1
// this is a shorter implementation of the above
// using built-in operations on Mat
double s = sum(hist)[0];
hist.convertTo(hist, hist.type(), 1./s, 0);
#else
// and this is even shorter one
// (assuming that the histogram elements are non-negative)
normalize(hist, hist, 1, 0, NORM_L1);
#endif
}
\endcode
You can iterate through several matrices simultaneously as long as they have the same geometry
(dimensionality and all the dimension sizes are the same), which is useful for binary
and n-ary operations on such matrices. Just pass those matrices to cv::MatNDIterator.
Then, during the iteration it.planes[0], it.planes[1], ... will
be the slices of the corresponding matrices
*/
class CV_EXPORTS NAryMatIterator
{
public:
//! the default constructor
NAryMatIterator();
//! the full constructor taking arbitrary number of n-dim matrices
NAryMatIterator(const Mat** arrays, uchar** ptrs, int narrays=-1);
//! the full constructor taking arbitrary number of n-dim matrices
NAryMatIterator(const Mat** arrays, Mat* planes, int narrays=-1);
//! the separate iterator initialization method
void init(const Mat** arrays, Mat* planes, uchar** ptrs, int narrays=-1);
//! proceeds to the next plane of every iterated matrix
NAryMatIterator& operator ++();
//! proceeds to the next plane of every iterated matrix (postfix increment operator)
NAryMatIterator operator ++(int);
//! the iterated arrays
const Mat** arrays;
//! the current planes
Mat* planes;
//! data pointers
uchar** ptrs;
//! the number of arrays
int narrays;
//! the number of hyper-planes that the iterator steps through
size_t nplanes;
//! the size of each segment (in elements)
size_t size;
protected:
int iterdepth;
size_t idx;
};
//typedef NAryMatIterator NAryMatNDIterator;
typedef void (*ConvertData)(const void* from, void* to, int cn);
typedef void (*ConvertScaleData)(const void* from, void* to, int cn, double alpha, double beta);
//! returns the function for converting pixels from one data type to another
CV_EXPORTS ConvertData getConvertElem(int fromType, int toType);
//! returns the function for converting pixels from one data type to another with the optional scaling
CV_EXPORTS ConvertScaleData getConvertScaleElem(int fromType, int toType);
/////////////////////////// multi-dimensional sparse matrix //////////////////////////
class SparseMatIterator;
class SparseMatConstIterator;
template<typename _Tp> class SparseMatIterator_;
template<typename _Tp> class SparseMatConstIterator_;
/*!
Sparse matrix class.
The class represents multi-dimensional sparse numerical arrays. Such a sparse array can store elements
of any type that cv::Mat is able to store. "Sparse" means that only non-zero elements
are stored (though, as a result of some operations on a sparse matrix, some of its stored elements
can actually become 0. It's user responsibility to detect such elements and delete them using cv::SparseMat::erase().
The non-zero elements are stored in a hash table that grows when it's filled enough,
so that the search time remains O(1) in average. Elements can be accessed using the following methods:
<ol>
<li>Query operations: cv::SparseMat::ptr() and the higher-level cv::SparseMat::ref(),
cv::SparseMat::value() and cv::SparseMat::find, for example:
\code
const int dims = 5;
int size[] = {10, 10, 10, 10, 10};
SparseMat sparse_mat(dims, size, CV_32F);
for(int i = 0; i < 1000; i++)
{
int idx[dims];
for(int k = 0; k < dims; k++)
idx[k] = rand()%sparse_mat.size(k);
sparse_mat.ref<float>(idx) += 1.f;
}
\endcode
<li>Sparse matrix iterators. Like cv::Mat iterators and unlike cv::Mat iterators, the sparse matrix iterators are STL-style,
that is, the iteration is done as following:
\code
// prints elements of a sparse floating-point matrix and the sum of elements.
SparseMatConstIterator_<float>
it = sparse_mat.begin<float>(),
it_end = sparse_mat.end<float>();
double s = 0;
int dims = sparse_mat.dims();
for(; it != it_end; ++it)
{
// print element indices and the element value
const Node* n = it.node();
printf("(")
for(int i = 0; i < dims; i++)
printf("%3d%c", n->idx[i], i < dims-1 ? ',' : ')');
printf(": %f\n", *it);
s += *it;
}
printf("Element sum is %g\n", s);
\endcode
If you run this loop, you will notice that elements are enumerated
in no any logical order (lexicographical etc.),
they come in the same order as they stored in the hash table, i.e. semi-randomly.
You may collect pointers to the nodes and sort them to get the proper ordering.
Note, however, that pointers to the nodes may become invalid when you add more
elements to the matrix; this is because of possible buffer reallocation.
<li>A combination of the above 2 methods when you need to process 2 or more sparse
matrices simultaneously, e.g. this is how you can compute unnormalized
cross-correlation of the 2 floating-point sparse matrices:
\code
double crossCorr(const SparseMat& a, const SparseMat& b)
{
const SparseMat *_a = &a, *_b = &b;
// if b contains less elements than a,
// it's faster to iterate through b
if(_a->nzcount() > _b->nzcount())
std::swap(_a, _b);
SparseMatConstIterator_<float> it = _a->begin<float>(),
it_end = _a->end<float>();
double ccorr = 0;
for(; it != it_end; ++it)
{
// take the next element from the first matrix
float avalue = *it;
const Node* anode = it.node();
// and try to find element with the same index in the second matrix.
// since the hash value depends only on the element index,
// we reuse hashvalue stored in the node
float bvalue = _b->value<float>(anode->idx,&anode->hashval);
ccorr += avalue*bvalue;
}
return ccorr;
}
\endcode
</ol>
*/
class CV_EXPORTS SparseMat
{
public:
typedef SparseMatIterator iterator;
typedef SparseMatConstIterator const_iterator;
//! the sparse matrix header
struct CV_EXPORTS Hdr
{
Hdr(int _dims, const int* _sizes, int _type);
void clear();
_Atomic_word refcount;
int dims;
int valueOffset;
size_t nodeSize;
size_t nodeCount;
size_t freeList;
vector<uchar> pool;
vector<size_t> hashtab;
int size[CV_MAX_DIM];
};
//! sparse matrix node - element of a hash table
struct CV_EXPORTS Node
{
//! hash value
size_t hashval;
//! index of the next node in the same hash table entry
size_t next;
//! index of the matrix element
int idx[CV_MAX_DIM];
};
//! default constructor
SparseMat();
//! creates matrix of the specified size and type
SparseMat(int dims, const int* _sizes, int _type);
//! copy constructor
SparseMat(const SparseMat& m);
//! converts dense 2d matrix to the sparse form
/*!
\param m the input matrix
*/
explicit SparseMat(const Mat& m);
//! converts old-style sparse matrix to the new-style. All the data is copied
SparseMat(const CvSparseMat* m);
//! the destructor
~SparseMat();
//! assignment operator. This is O(1) operation, i.e. no data is copied
SparseMat& operator = (const SparseMat& m);
//! equivalent to the corresponding constructor
SparseMat& operator = (const Mat& m);
//! creates full copy of the matrix
SparseMat clone() const;
//! copies all the data to the destination matrix. All the previous content of m is erased
void copyTo( SparseMat& m ) const;
//! converts sparse matrix to dense matrix.
void copyTo( Mat& m ) const;
//! multiplies all the matrix elements by the specified scale factor alpha and converts the results to the specified data type
void convertTo( SparseMat& m, int rtype, double alpha=1 ) const;
//! converts sparse matrix to dense n-dim matrix with optional type conversion and scaling.
/*!
\param rtype The output matrix data type. When it is =-1, the output array will have the same data type as (*this)
\param alpha The scale factor
\param beta The optional delta added to the scaled values before the conversion
*/
void convertTo( Mat& m, int rtype, double alpha=1, double beta=0 ) const;
// not used now
void assignTo( SparseMat& m, int type=-1 ) const;
//! reallocates sparse matrix.
/*!
If the matrix already had the proper size and type,
it is simply cleared with clear(), otherwise,
the old matrix is released (using release()) and the new one is allocated.
*/
void create(int dims, const int* _sizes, int _type);
//! sets all the sparse matrix elements to 0, which means clearing the hash table.
void clear();
//! manually increments the reference counter to the header.
void addref();
// decrements the header reference counter. When the counter reaches 0, the header and all the underlying data are deallocated.
void release();
//! converts sparse matrix to the old-style representation; all the elements are copied.
operator CvSparseMat*() const;
//! returns the size of each element in bytes (not including the overhead - the space occupied by SparseMat::Node elements)
size_t elemSize() const;
//! returns elemSize()/channels()
size_t elemSize1() const;
//! returns type of sparse matrix elements
int type() const;
//! returns the depth of sparse matrix elements
int depth() const;
//! returns the number of channels
int channels() const;
//! returns the array of sizes, or NULL if the matrix is not allocated
const int* size() const;
//! returns the size of i-th matrix dimension (or 0)
int size(int i) const;
//! returns the matrix dimensionality
int dims() const;
//! returns the number of non-zero elements (=the number of hash table nodes)
size_t nzcount() const;
//! computes the element hash value (1D case)
size_t hash(int i0) const;
//! computes the element hash value (2D case)
size_t hash(int i0, int i1) const;
//! computes the element hash value (3D case)
size_t hash(int i0, int i1, int i2) const;
//! computes the element hash value (nD case)
size_t hash(const int* idx) const;
//@{
/*!
specialized variants for 1D, 2D, 3D cases and the generic_type one for n-D case.
return pointer to the matrix element.
<ul>
<li>if the element is there (it's non-zero), the pointer to it is returned
<li>if it's not there and createMissing=false, NULL pointer is returned
<li>if it's not there and createMissing=true, then the new element
is created and initialized with 0. Pointer to it is returned
<li>if the optional hashval pointer is not NULL, the element hash value is
not computed, but *hashval is taken instead.
</ul>
*/
//! returns pointer to the specified element (1D case)
uchar* ptr(int i0, bool createMissing, size_t* hashval=0);
//! returns pointer to the specified element (2D case)
uchar* ptr(int i0, int i1, bool createMissing, size_t* hashval=0);
//! returns pointer to the specified element (3D case)
uchar* ptr(int i0, int i1, int i2, bool createMissing, size_t* hashval=0);
//! returns pointer to the specified element (nD case)
uchar* ptr(const int* idx, bool createMissing, size_t* hashval=0);
//@}
//@{
/*!
return read-write reference to the specified sparse matrix element.
ref<_Tp>(i0,...[,hashval]) is equivalent to *(_Tp*)ptr(i0,...,true[,hashval]).
The methods always return a valid reference.
If the element did not exist, it is created and initialiazed with 0.
*/
//! returns reference to the specified element (1D case)
template<typename _Tp> _Tp& ref(int i0, size_t* hashval=0);
//! returns reference to the specified element (2D case)
template<typename _Tp> _Tp& ref(int i0, int i1, size_t* hashval=0);
//! returns reference to the specified element (3D case)
template<typename _Tp> _Tp& ref(int i0, int i1, int i2, size_t* hashval=0);
//! returns reference to the specified element (nD case)
template<typename _Tp> _Tp& ref(const int* idx, size_t* hashval=0);
//@}
//@{
/*!
return value of the specified sparse matrix element.
value<_Tp>(i0,...[,hashval]) is equivalent
\code
{ const _Tp* p = find<_Tp>(i0,...[,hashval]); return p ? *p : _Tp(); }
\endcode
That is, if the element did not exist, the methods return 0.
*/
//! returns value of the specified element (1D case)
template<typename _Tp> _Tp value(int i0, size_t* hashval=0) const;
//! returns value of the specified element (2D case)
template<typename _Tp> _Tp value(int i0, int i1, size_t* hashval=0) const;
//! returns value of the specified element (3D case)
template<typename _Tp> _Tp value(int i0, int i1, int i2, size_t* hashval=0) const;
//! returns value of the specified element (nD case)
template<typename _Tp> _Tp value(const int* idx, size_t* hashval=0) const;
//@}
//@{
/*!
Return pointer to the specified sparse matrix element if it exists
find<_Tp>(i0,...[,hashval]) is equivalent to (_const Tp*)ptr(i0,...false[,hashval]).
If the specified element does not exist, the methods return NULL.
*/
//! returns pointer to the specified element (1D case)
template<typename _Tp> const _Tp* find(int i0, size_t* hashval=0) const;
//! returns pointer to the specified element (2D case)
template<typename _Tp> const _Tp* find(int i0, int i1, size_t* hashval=0) const;
//! returns pointer to the specified element (3D case)
template<typename _Tp> const _Tp* find(int i0, int i1, int i2, size_t* hashval=0) const;
//! returns pointer to the specified element (nD case)
template<typename _Tp> const _Tp* find(const int* idx, size_t* hashval=0) const;
//! erases the specified element (2D case)
void erase(int i0, int i1, size_t* hashval=0);
//! erases the specified element (3D case)
void erase(int i0, int i1, int i2, size_t* hashval=0);
//! erases the specified element (nD case)
void erase(const int* idx, size_t* hashval=0);
//@{
/*!
return the sparse matrix iterator pointing to the first sparse matrix element
*/
//! returns the sparse matrix iterator at the matrix beginning
SparseMatIterator begin();
//! returns the sparse matrix iterator at the matrix beginning
template<typename _Tp> SparseMatIterator_<_Tp> begin();
//! returns the read-only sparse matrix iterator at the matrix beginning
SparseMatConstIterator begin() const;
//! returns the read-only sparse matrix iterator at the matrix beginning
template<typename _Tp> SparseMatConstIterator_<_Tp> begin() const;
//@}
/*!
return the sparse matrix iterator pointing to the element following the last sparse matrix element
*/
//! returns the sparse matrix iterator at the matrix end
SparseMatIterator end();
//! returns the read-only sparse matrix iterator at the matrix end
SparseMatConstIterator end() const;
//! returns the typed sparse matrix iterator at the matrix end
template<typename _Tp> SparseMatIterator_<_Tp> end();
//! returns the typed read-only sparse matrix iterator at the matrix end
template<typename _Tp> SparseMatConstIterator_<_Tp> end() const;
//! returns the value stored in the sparse martix node
template<typename _Tp> _Tp& value(Node* n);
//! returns the value stored in the sparse martix node
template<typename _Tp> const _Tp& value(const Node* n) const;
////////////// some internal-use methods ///////////////
Node* node(size_t nidx);
const Node* node(size_t nidx) const;
uchar* newNode(const int* idx, size_t hashval);
void removeNode(size_t hidx, size_t nidx, size_t previdx);
void resizeHashTab(size_t newsize);
enum { MAGIC_VAL=0x42FD0000, MAX_DIM=CV_MAX_DIM, HASH_SCALE=0x5bd1e995, HASH_BIT=0x80000000 };
int flags;
Hdr* hdr;
};
//! finds global minimum and maximum sparse array elements and returns their values and their locations
CV_EXPORTS void minMaxLoc(const SparseMat& a, double* minVal,
double* maxVal, int* minIdx=0, int* maxIdx=0);
//! computes norm of a sparse matrix
CV_EXPORTS double norm( const SparseMat& src, int normType );
//! scales and shifts array elements so that either the specified norm (alpha) or the minimum (alpha) and maximum (beta) array values get the specified values
CV_EXPORTS void normalize( const SparseMat& src, SparseMat& dst, double alpha, int normType );
/*!
Read-Only Sparse Matrix Iterator.
Here is how to use the iterator to compute the sum of floating-point sparse matrix elements:
\code
SparseMatConstIterator it = m.begin(), it_end = m.end();
double s = 0;
CV_Assert( m.type() == CV_32F );
for( ; it != it_end; ++it )
s += it.value<float>();
\endcode
*/
class CV_EXPORTS SparseMatConstIterator
{
public:
//! the default constructor
SparseMatConstIterator();
//! the full constructor setting the iterator to the first sparse matrix element
SparseMatConstIterator(const SparseMat* _m);
//! the copy constructor
SparseMatConstIterator(const SparseMatConstIterator& it);
//! the assignment operator
SparseMatConstIterator& operator = (const SparseMatConstIterator& it);
//! template method returning the current matrix element
template<typename _Tp> const _Tp& value() const;
//! returns the current node of the sparse matrix. it.node->idx is the current element index
const SparseMat::Node* node() const;
//! moves iterator to the previous element
SparseMatConstIterator& operator --();
//! moves iterator to the previous element
SparseMatConstIterator operator --(int);
//! moves iterator to the next element
SparseMatConstIterator& operator ++();
//! moves iterator to the next element
SparseMatConstIterator operator ++(int);
//! moves iterator to the element after the last element
void seekEnd();
const SparseMat* m;
size_t hashidx;
uchar* ptr;
};
/*!
Read-write Sparse Matrix Iterator
The class is similar to cv::SparseMatConstIterator,
but can be used for in-place modification of the matrix elements.
*/
class CV_EXPORTS SparseMatIterator : public SparseMatConstIterator
{
public:
//! the default constructor
SparseMatIterator();
//! the full constructor setting the iterator to the first sparse matrix element
SparseMatIterator(SparseMat* _m);
//! the full constructor setting the iterator to the specified sparse matrix element
SparseMatIterator(SparseMat* _m, const int* idx);
//! the copy constructor
SparseMatIterator(const SparseMatIterator& it);
//! the assignment operator
SparseMatIterator& operator = (const SparseMatIterator& it);
//! returns read-write reference to the current sparse matrix element
template<typename _Tp> _Tp& value() const;
//! returns pointer to the current sparse matrix node. it.node->idx is the index of the current element (do not modify it!)
SparseMat::Node* node() const;
//! moves iterator to the next element
SparseMatIterator& operator ++();
//! moves iterator to the next element
SparseMatIterator operator ++(int);
};
/*!
The Template Sparse Matrix class derived from cv::SparseMat
The class provides slightly more convenient operations for accessing elements.
\code
SparseMat m;
...
SparseMat_<int> m_ = (SparseMat_<int>&)m;
m_.ref(1)++; // equivalent to m.ref<int>(1)++;
m_.ref(2) += m_(3); // equivalent to m.ref<int>(2) += m.value<int>(3);
\endcode
*/
template<typename _Tp> class SparseMat_ : public SparseMat
{
public:
typedef SparseMatIterator_<_Tp> iterator;
typedef SparseMatConstIterator_<_Tp> const_iterator;
//! the default constructor
SparseMat_();
//! the full constructor equivelent to SparseMat(dims, _sizes, DataType<_Tp>::type)
SparseMat_(int dims, const int* _sizes);
//! the copy constructor. If DataType<_Tp>.type != m.type(), the m elements are converted
SparseMat_(const SparseMat& m);
//! the copy constructor. This is O(1) operation - no data is copied
SparseMat_(const SparseMat_& m);
//! converts dense matrix to the sparse form
SparseMat_(const Mat& m);
//! converts the old-style sparse matrix to the C++ class. All the elements are copied
SparseMat_(const CvSparseMat* m);
//! the assignment operator. If DataType<_Tp>.type != m.type(), the m elements are converted
SparseMat_& operator = (const SparseMat& m);
//! the assignment operator. This is O(1) operation - no data is copied
SparseMat_& operator = (const SparseMat_& m);
//! converts dense matrix to the sparse form
SparseMat_& operator = (const Mat& m);
//! makes full copy of the matrix. All the elements are duplicated
SparseMat_ clone() const;
//! equivalent to cv::SparseMat::create(dims, _sizes, DataType<_Tp>::type)
void create(int dims, const int* _sizes);
//! converts sparse matrix to the old-style CvSparseMat. All the elements are copied
operator CvSparseMat*() const;
//! returns type of the matrix elements
int type() const;
//! returns depth of the matrix elements
int depth() const;
//! returns the number of channels in each matrix element
int channels() const;
//! equivalent to SparseMat::ref<_Tp>(i0, hashval)
_Tp& ref(int i0, size_t* hashval=0);
//! equivalent to SparseMat::ref<_Tp>(i0, i1, hashval)
_Tp& ref(int i0, int i1, size_t* hashval=0);
//! equivalent to SparseMat::ref<_Tp>(i0, i1, i2, hashval)
_Tp& ref(int i0, int i1, int i2, size_t* hashval=0);
//! equivalent to SparseMat::ref<_Tp>(idx, hashval)
_Tp& ref(const int* idx, size_t* hashval=0);
//! equivalent to SparseMat::value<_Tp>(i0, hashval)
_Tp operator()(int i0, size_t* hashval=0) const;
//! equivalent to SparseMat::value<_Tp>(i0, i1, hashval)
_Tp operator()(int i0, int i1, size_t* hashval=0) const;
//! equivalent to SparseMat::value<_Tp>(i0, i1, i2, hashval)
_Tp operator()(int i0, int i1, int i2, size_t* hashval=0) const;
//! equivalent to SparseMat::value<_Tp>(idx, hashval)
_Tp operator()(const int* idx, size_t* hashval=0) const;
//! returns sparse matrix iterator pointing to the first sparse matrix element
SparseMatIterator_<_Tp> begin();
//! returns read-only sparse matrix iterator pointing to the first sparse matrix element
SparseMatConstIterator_<_Tp> begin() const;
//! returns sparse matrix iterator pointing to the element following the last sparse matrix element
SparseMatIterator_<_Tp> end();
//! returns read-only sparse matrix iterator pointing to the element following the last sparse matrix element
SparseMatConstIterator_<_Tp> end() const;
};
/*!
Template Read-Only Sparse Matrix Iterator Class.
This is the derived from SparseMatConstIterator class that
introduces more convenient operator *() for accessing the current element.
*/
template<typename _Tp> class SparseMatConstIterator_ : public SparseMatConstIterator
{
public:
typedef std::forward_iterator_tag iterator_category;
//! the default constructor
SparseMatConstIterator_();
//! the full constructor setting the iterator to the first sparse matrix element
SparseMatConstIterator_(const SparseMat_<_Tp>* _m);
SparseMatConstIterator_(const SparseMat* _m);
//! the copy constructor
SparseMatConstIterator_(const SparseMatConstIterator_& it);
//! the assignment operator
SparseMatConstIterator_& operator = (const SparseMatConstIterator_& it);
//! the element access operator
const _Tp& operator *() const;
//! moves iterator to the next element
SparseMatConstIterator_& operator ++();
//! moves iterator to the next element
SparseMatConstIterator_ operator ++(int);
};
/*!
Template Read-Write Sparse Matrix Iterator Class.
This is the derived from cv::SparseMatConstIterator_ class that
introduces more convenient operator *() for accessing the current element.
*/
template<typename _Tp> class SparseMatIterator_ : public SparseMatConstIterator_<_Tp>
{
public:
typedef std::forward_iterator_tag iterator_category;
//! the default constructor
SparseMatIterator_();
//! the full constructor setting the iterator to the first sparse matrix element
SparseMatIterator_(SparseMat_<_Tp>* _m);
SparseMatIterator_(SparseMat* _m);
//! the copy constructor
SparseMatIterator_(const SparseMatIterator_& it);
//! the assignment operator
SparseMatIterator_& operator = (const SparseMatIterator_& it);
//! returns the reference to the current element
_Tp& operator *() const;
//! moves the iterator to the next element
SparseMatIterator_& operator ++();
//! moves the iterator to the next element
SparseMatIterator_ operator ++(int);
};
//////////////////// Fast Nearest-Neighbor Search Structure ////////////////////
/*!
Fast Nearest Neighbor Search Class.
The class implements D. Lowe BBF (Best-Bin-First) algorithm for the last
approximate (or accurate) nearest neighbor search in multi-dimensional spaces.
First, a set of vectors is passed to KDTree::KDTree() constructor
or KDTree::build() method, where it is reordered.
Then arbitrary vectors can be passed to KDTree::findNearest() methods, which
find the K nearest neighbors among the vectors from the initial set.
The user can balance between the speed and accuracy of the search by varying Emax
parameter, which is the number of leaves that the algorithm checks.
The larger parameter values yield more accurate results at the expense of lower processing speed.
\code
KDTree T(points, false);
const int K = 3, Emax = INT_MAX;
int idx[K];
float dist[K];
T.findNearest(query_vec, K, Emax, idx, 0, dist);
CV_Assert(dist[0] <= dist[1] && dist[1] <= dist[2]);
\endcode
*/
class CV_EXPORTS_W KDTree
{
public:
/*!
The node of the search tree.
*/
struct Node
{
Node() : idx(-1), left(-1), right(-1), boundary(0.f) {}
Node(int _idx, int _left, int _right, float _boundary)
: idx(_idx), left(_left), right(_right), boundary(_boundary) {}
//! split dimension; >=0 for nodes (dim), < 0 for leaves (index of the point)
int idx;
//! node indices of the left and the right branches
int left, right;
//! go to the left if query_vec[node.idx]<=node.boundary, otherwise go to the right
float boundary;
};
//! the default constructor
CV_WRAP KDTree();
//! the full constructor that builds the search tree
CV_WRAP KDTree(InputArray points, bool copyAndReorderPoints=false);
//! the full constructor that builds the search tree
CV_WRAP KDTree(InputArray points, InputArray _labels,
bool copyAndReorderPoints=false);
//! builds the search tree
CV_WRAP void build(InputArray points, bool copyAndReorderPoints=false);
//! builds the search tree
CV_WRAP void build(InputArray points, InputArray labels,
bool copyAndReorderPoints=false);
//! finds the K nearest neighbors of "vec" while looking at Emax (at most) leaves
CV_WRAP int findNearest(InputArray vec, int K, int Emax,
OutputArray neighborsIdx,
OutputArray neighbors=noArray(),
OutputArray dist=noArray(),
OutputArray labels=noArray()) const;
//! finds all the points from the initial set that belong to the specified box
CV_WRAP void findOrthoRange(InputArray minBounds,
InputArray maxBounds,
OutputArray neighborsIdx,
OutputArray neighbors=noArray(),
OutputArray labels=noArray()) const;
//! returns vectors with the specified indices
CV_WRAP void getPoints(InputArray idx, OutputArray pts,
OutputArray labels=noArray()) const;
//! return a vector with the specified index
const float* getPoint(int ptidx, int* label=0) const;
//! returns the search space dimensionality
CV_WRAP int dims() const;
vector<Node> nodes; //!< all the tree nodes
CV_PROP Mat points; //!< all the points. It can be a reordered copy of the input vector set or the original vector set.
CV_PROP vector<int> labels; //!< the parallel array of labels.
CV_PROP int maxDepth; //!< maximum depth of the search tree. Do not modify it
CV_PROP_RW int normType; //!< type of the distance (cv::NORM_L1 or cv::NORM_L2) used for search. Initially set to cv::NORM_L2, but you can modify it
};
//////////////////////////////////////// XML & YAML I/O ////////////////////////////////////
class CV_EXPORTS FileNode;
/*!
XML/YAML File Storage Class.
The class describes an object associated with XML or YAML file.
It can be used to store data to such a file or read and decode the data.
The storage is organized as a tree of nested sequences (or lists) and mappings.
Sequence is a heterogenious array, which elements are accessed by indices or sequentially using an iterator.
Mapping is analogue of std::map or C structure, which elements are accessed by names.
The most top level structure is a mapping.
Leaves of the file storage tree are integers, floating-point numbers and text strings.
For example, the following code:
\code
// open file storage for writing. Type of the file is determined from the extension
FileStorage fs("test.yml", FileStorage::WRITE);
fs << "test_int" << 5 << "test_real" << 3.1 << "test_string" << "ABCDEFGH";
fs << "test_mat" << Mat::eye(3,3,CV_32F);
fs << "test_list" << "[" << 0.0000000000001 << 2 << CV_PI << -3435345 << "2-502 2-029 3egegeg" <<
"{:" << "month" << 12 << "day" << 31 << "year" << 1969 << "}" << "]";
fs << "test_map" << "{" << "x" << 1 << "y" << 2 << "width" << 100 << "height" << 200 << "lbp" << "[:";
const uchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1};
fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0])));
fs << "]" << "}";
\endcode
will produce the following file:
\verbatim
%YAML:1.0
test_int: 5
test_real: 3.1000000000000001e+00
test_string: ABCDEFGH
test_mat: !!opencv-matrix
rows: 3
cols: 3
dt: f
data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1. ]
test_list:
- 1.0000000000000000e-13
- 2
- 3.1415926535897931e+00
- -3435345
- "2-502 2-029 3egegeg"
- { month:12, day:31, year:1969 }
test_map:
x: 1
y: 2
width: 100
height: 200
lbp: [ 0, 1, 1, 0, 1, 1, 0, 1 ]
\endverbatim
and to read the file above, the following code can be used:
\code
// open file storage for reading.
// Type of the file is determined from the content, not the extension
FileStorage fs("test.yml", FileStorage::READ);
int test_int = (int)fs["test_int"];
double test_real = (double)fs["test_real"];
string test_string = (string)fs["test_string"];
Mat M;
fs["test_mat"] >> M;
FileNode tl = fs["test_list"];
CV_Assert(tl.type() == FileNode::SEQ && tl.size() == 6);
double tl0 = (double)tl[0];
int tl1 = (int)tl[1];
double tl2 = (double)tl[2];
int tl3 = (int)tl[3];
string tl4 = (string)tl[4];
CV_Assert(tl[5].type() == FileNode::MAP && tl[5].size() == 3);
int month = (int)tl[5]["month"];
int day = (int)tl[5]["day"];
int year = (int)tl[5]["year"];
FileNode tm = fs["test_map"];
int x = (int)tm["x"];
int y = (int)tm["y"];
int width = (int)tm["width"];
int height = (int)tm["height"];
int lbp_val = 0;
FileNodeIterator it = tm["lbp"].begin();
for(int k = 0; k < 8; k++, ++it)
lbp_val |= ((int)*it) << k;
\endcode
*/
class CV_EXPORTS_W FileStorage
{
public:
//! file storage mode
enum
{
READ=0, //! read mode
WRITE=1, //! write mode
APPEND=2, //! append mode
MEMORY=4,
FORMAT_MASK=(7<<3),
FORMAT_AUTO=0,
FORMAT_XML=(1<<3),
FORMAT_YAML=(2<<3)
};
enum
{
UNDEFINED=0,
VALUE_EXPECTED=1,
NAME_EXPECTED=2,
INSIDE_MAP=4
};
//! the default constructor
CV_WRAP FileStorage();
//! the full constructor that opens file storage for reading or writing
CV_WRAP FileStorage(const string& source, int flags, const string& encoding=string());
//! the constructor that takes pointer to the C FileStorage structure
FileStorage(CvFileStorage* fs);
//! the destructor. calls release()
virtual ~FileStorage();
//! opens file storage for reading or writing. The previous storage is closed with release()
CV_WRAP virtual bool open(const string& filename, int flags, const string& encoding=string());
//! returns true if the object is associated with currently opened file.
CV_WRAP virtual bool isOpened() const;
//! closes the file and releases all the memory buffers
CV_WRAP virtual void release();
//! closes the file, releases all the memory buffers and returns the text string
CV_WRAP string releaseAndGetString();
//! returns the first element of the top-level mapping
CV_WRAP FileNode getFirstTopLevelNode() const;
//! returns the top-level mapping. YAML supports multiple streams
CV_WRAP FileNode root(int streamidx=0) const;
//! returns the specified element of the top-level mapping
FileNode operator[](const string& nodename) const;
//! returns the specified element of the top-level mapping
CV_WRAP FileNode operator[](const char* nodename) const;
//! returns pointer to the underlying C FileStorage structure
CvFileStorage* operator *() { return fs; }
//! returns pointer to the underlying C FileStorage structure
const CvFileStorage* operator *() const { return fs; }
//! writes one or more numbers of the specified format to the currently written structure
void writeRaw( const string& fmt, const uchar* vec, size_t len );
//! writes the registered C structure (CvMat, CvMatND, CvSeq). See cvWrite()
void writeObj( const string& name, const void* obj );
//! returns the normalized object name for the specified file name
static string getDefaultObjectName(const string& filename);
Ptr<CvFileStorage> fs; //!< the underlying C FileStorage structure
string elname; //!< the currently written element
vector<char> structs; //!< the stack of written structures
int state; //!< the writer state
};
class CV_EXPORTS FileNodeIterator;
/*!
File Storage Node class
The node is used to store each and every element of the file storage opened for reading -
from the primitive objects, such as numbers and text strings, to the complex nodes:
sequences, mappings and the registered objects.
Note that file nodes are only used for navigating file storages opened for reading.
When a file storage is opened for writing, no data is stored in memory after it is written.
*/
class CV_EXPORTS_W_SIMPLE FileNode
{
public:
//! type of the file storage node
enum
{
NONE=0, //!< empty node
INT=1, //!< an integer
REAL=2, //!< floating-point number
FLOAT=REAL, //!< synonym or REAL
STR=3, //!< text string in UTF-8 encoding
STRING=STR, //!< synonym for STR
REF=4, //!< integer of size size_t. Typically used for storing complex dynamic structures where some elements reference the others
SEQ=5, //!< sequence
MAP=6, //!< mapping
TYPE_MASK=7,
FLOW=8, //!< compact representation of a sequence or mapping. Used only by YAML writer
USER=16, //!< a registered object (e.g. a matrix)
EMPTY=32, //!< empty structure (sequence or mapping)
NAMED=64 //!< the node has a name (i.e. it is element of a mapping)
};
//! the default constructor
CV_WRAP FileNode();
//! the full constructor wrapping CvFileNode structure.
FileNode(const CvFileStorage* fs, const CvFileNode* node);
//! the copy constructor
FileNode(const FileNode& node);
//! returns element of a mapping node
FileNode operator[](const string& nodename) const;
//! returns element of a mapping node
CV_WRAP FileNode operator[](const char* nodename) const;
//! returns element of a sequence node
CV_WRAP FileNode operator[](int i) const;
//! returns type of the node
CV_WRAP int type() const;
//! returns true if the node is empty
CV_WRAP bool empty() const;
//! returns true if the node is a "none" object
CV_WRAP bool isNone() const;
//! returns true if the node is a sequence
CV_WRAP bool isSeq() const;
//! returns true if the node is a mapping
CV_WRAP bool isMap() const;
//! returns true if the node is an integer
CV_WRAP bool isInt() const;
//! returns true if the node is a floating-point number
CV_WRAP bool isReal() const;
//! returns true if the node is a text string
CV_WRAP bool isString() const;
//! returns true if the node has a name
CV_WRAP bool isNamed() const;
//! returns the node name or an empty string if the node is nameless
CV_WRAP string name() const;
//! returns the number of elements in the node, if it is a sequence or mapping, or 1 otherwise.
CV_WRAP size_t size() const;
//! returns the node content as an integer. If the node stores floating-point number, it is rounded.
operator int() const;
//! returns the node content as float
operator float() const;
//! returns the node content as double
operator double() const;
//! returns the node content as text string
operator string() const;
//! returns pointer to the underlying file node
CvFileNode* operator *();
//! returns pointer to the underlying file node
const CvFileNode* operator* () const;
//! returns iterator pointing to the first node element
FileNodeIterator begin() const;
//! returns iterator pointing to the element following the last node element
FileNodeIterator end() const;
//! reads node elements to the buffer with the specified format
void readRaw( const string& fmt, uchar* vec, size_t len ) const;
//! reads the registered object and returns pointer to it
void* readObj() const;
// do not use wrapper pointer classes for better efficiency
const CvFileStorage* fs;
const CvFileNode* node;
};
/*!
File Node Iterator
The class is used for iterating sequences (usually) and mappings.
*/
class CV_EXPORTS FileNodeIterator
{
public:
//! the default constructor
FileNodeIterator();
//! the full constructor set to the ofs-th element of the node
FileNodeIterator(const CvFileStorage* fs, const CvFileNode* node, size_t ofs=0);
//! the copy constructor
FileNodeIterator(const FileNodeIterator& it);
//! returns the currently observed element
FileNode operator *() const;
//! accesses the currently observed element methods
FileNode operator ->() const;
//! moves iterator to the next node
FileNodeIterator& operator ++ ();
//! moves iterator to the next node
FileNodeIterator operator ++ (int);
//! moves iterator to the previous node
FileNodeIterator& operator -- ();
//! moves iterator to the previous node
FileNodeIterator operator -- (int);
//! moves iterator forward by the specified offset (possibly negative)
FileNodeIterator& operator += (int ofs);
//! moves iterator backward by the specified offset (possibly negative)
FileNodeIterator& operator -= (int ofs);
//! reads the next maxCount elements (or less, if the sequence/mapping last element occurs earlier) to the buffer with the specified format
FileNodeIterator& readRaw( const string& fmt, uchar* vec,
size_t maxCount=(size_t)INT_MAX );
const CvFileStorage* fs;
const CvFileNode* container;
CvSeqReader reader;
size_t remaining;
};
////////////// convenient wrappers for operating old-style dynamic structures //////////////
template<typename _Tp> class SeqIterator;
typedef Ptr<CvMemStorage> MemStorage;
/*!
Template Sequence Class derived from CvSeq
The class provides more convenient access to sequence elements,
STL-style operations and iterators.
\note The class is targeted for simple data types,
i.e. no constructors or destructors
are called for the sequence elements.
*/
template<typename _Tp> class Seq
{
public:
typedef SeqIterator<_Tp> iterator;
typedef SeqIterator<_Tp> const_iterator;
//! the default constructor
Seq();
//! the constructor for wrapping CvSeq structure. The real element type in CvSeq should match _Tp.
Seq(const CvSeq* seq);
//! creates the empty sequence that resides in the specified storage
Seq(MemStorage& storage, int headerSize = sizeof(CvSeq));
//! returns read-write reference to the specified element
_Tp& operator [](int idx);
//! returns read-only reference to the specified element
const _Tp& operator[](int idx) const;
//! returns iterator pointing to the beginning of the sequence
SeqIterator<_Tp> begin() const;
//! returns iterator pointing to the element following the last sequence element
SeqIterator<_Tp> end() const;
//! returns the number of elements in the sequence
size_t size() const;
//! returns the type of sequence elements (CV_8UC1 ... CV_64FC(CV_CN_MAX) ...)
int type() const;
//! returns the depth of sequence elements (CV_8U ... CV_64F)
int depth() const;
//! returns the number of channels in each sequence element
int channels() const;
//! returns the size of each sequence element
size_t elemSize() const;
//! returns index of the specified sequence element
size_t index(const _Tp& elem) const;
//! appends the specified element to the end of the sequence
void push_back(const _Tp& elem);
//! appends the specified element to the front of the sequence
void push_front(const _Tp& elem);
//! appends zero or more elements to the end of the sequence
void push_back(const _Tp* elems, size_t count);
//! appends zero or more elements to the front of the sequence
void push_front(const _Tp* elems, size_t count);
//! inserts the specified element to the specified position
void insert(int idx, const _Tp& elem);
//! inserts zero or more elements to the specified position
void insert(int idx, const _Tp* elems, size_t count);
//! removes element at the specified position
void remove(int idx);
//! removes the specified subsequence
void remove(const Range& r);
//! returns reference to the first sequence element
_Tp& front();
//! returns read-only reference to the first sequence element
const _Tp& front() const;
//! returns reference to the last sequence element
_Tp& back();
//! returns read-only reference to the last sequence element
const _Tp& back() const;
//! returns true iff the sequence contains no elements
bool empty() const;
//! removes all the elements from the sequence
void clear();
//! removes the first element from the sequence
void pop_front();
//! removes the last element from the sequence
void pop_back();
//! removes zero or more elements from the beginning of the sequence
void pop_front(_Tp* elems, size_t count);
//! removes zero or more elements from the end of the sequence
void pop_back(_Tp* elems, size_t count);
//! copies the whole sequence or the sequence slice to the specified vector
void copyTo(vector<_Tp>& vec, const Range& range=Range::all()) const;
//! returns the vector containing all the sequence elements
operator vector<_Tp>() const;
CvSeq* seq;
};
/*!
STL-style Sequence Iterator inherited from the CvSeqReader structure
*/
template<typename _Tp> class SeqIterator : public CvSeqReader
{
public:
//! the default constructor
SeqIterator();
//! the constructor setting the iterator to the beginning or to the end of the sequence
SeqIterator(const Seq<_Tp>& seq, bool seekEnd=false);
//! positions the iterator within the sequence
void seek(size_t pos);
//! reports the current iterator position
size_t tell() const;
//! returns reference to the current sequence element
_Tp& operator *();
//! returns read-only reference to the current sequence element
const _Tp& operator *() const;
//! moves iterator to the next sequence element
SeqIterator& operator ++();
//! moves iterator to the next sequence element
SeqIterator operator ++(int) const;
//! moves iterator to the previous sequence element
SeqIterator& operator --();
//! moves iterator to the previous sequence element
SeqIterator operator --(int) const;
//! moves iterator forward by the specified offset (possibly negative)
SeqIterator& operator +=(int);
//! moves iterator backward by the specified offset (possibly negative)
SeqIterator& operator -=(int);
// this is index of the current element module seq->total*2
// (to distinguish between 0 and seq->total)
int index;
};
class CV_EXPORTS Algorithm;
class CV_EXPORTS AlgorithmInfo;
struct CV_EXPORTS AlgorithmInfoData;
template<typename _Tp> struct ParamType {};
/*!
Base class for high-level OpenCV algorithms
*/
class CV_EXPORTS_W Algorithm
{
public:
Algorithm();
virtual ~Algorithm();
string name() const;
template<typename _Tp> typename ParamType<_Tp>::member_type get(const string& name) const;
template<typename _Tp> typename ParamType<_Tp>::member_type get(const char* name) const;
CV_WRAP int getInt(const string& name) const;
CV_WRAP double getDouble(const string& name) const;
CV_WRAP bool getBool(const string& name) const;
CV_WRAP string getString(const string& name) const;
CV_WRAP Mat getMat(const string& name) const;
CV_WRAP vector<Mat> getMatVector(const string& name) const;
CV_WRAP Ptr<Algorithm> getAlgorithm(const string& name) const;
void set(const string& name, int value);
void set(const string& name, double value);
void set(const string& name, bool value);
void set(const string& name, const string& value);
void set(const string& name, const Mat& value);
void set(const string& name, const vector<Mat>& value);
void set(const string& name, const Ptr<Algorithm>& value);
template<typename _Tp> void set(const string& name, const Ptr<_Tp>& value);
CV_WRAP void setInt(const string& name, int value);
CV_WRAP void setDouble(const string& name, double value);
CV_WRAP void setBool(const string& name, bool value);
CV_WRAP void setString(const string& name, const string& value);
CV_WRAP void setMat(const string& name, const Mat& value);
CV_WRAP void setMatVector(const string& name, const vector<Mat>& value);
CV_WRAP void setAlgorithm(const string& name, const Ptr<Algorithm>& value);
template<typename _Tp> void setAlgorithm(const string& name, const Ptr<_Tp>& value);
void set(const char* name, int value);
void set(const char* name, double value);
void set(const char* name, bool value);
void set(const char* name, const string& value);
void set(const char* name, const Mat& value);
void set(const char* name, const vector<Mat>& value);
void set(const char* name, const Ptr<Algorithm>& value);
template<typename _Tp> void set(const char* name, const Ptr<_Tp>& value);
void setInt(const char* name, int value);
void setDouble(const char* name, double value);
void setBool(const char* name, bool value);
void setString(const char* name, const string& value);
void setMat(const char* name, const Mat& value);
void setMatVector(const char* name, const vector<Mat>& value);
void setAlgorithm(const char* name, const Ptr<Algorithm>& value);
template<typename _Tp> void setAlgorithm(const char* name, const Ptr<_Tp>& value);
CV_WRAP string paramHelp(const string& name) const;
int paramType(const char* name) const;
CV_WRAP int paramType(const string& name) const;
CV_WRAP void getParams(CV_OUT vector<string>& names) const;
virtual void write(FileStorage& fs) const;
virtual void read(const FileNode& fn);
typedef Algorithm* (*Constructor)(void);
typedef int (Algorithm::*Getter)() const;
typedef void (Algorithm::*Setter)(int);
CV_WRAP static void getList(CV_OUT vector<string>& algorithms);
CV_WRAP static Ptr<Algorithm> _create(const string& name);
template<typename _Tp> static Ptr<_Tp> create(const string& name);
virtual AlgorithmInfo* info() const /* TODO: make it = 0;*/ { return 0; }
};
class CV_EXPORTS AlgorithmInfo
{
public:
friend class Algorithm;
AlgorithmInfo(const string& name, Algorithm::Constructor create);
~AlgorithmInfo();
void get(const Algorithm* algo, const char* name, int argType, void* value) const;
void addParam_(Algorithm& algo, const char* name, int argType,
void* value, bool readOnly,
Algorithm::Getter getter, Algorithm::Setter setter,
const string& help=string());
string paramHelp(const char* name) const;
int paramType(const char* name) const;
void getParams(vector<string>& names) const;
void write(const Algorithm* algo, FileStorage& fs) const;
void read(Algorithm* algo, const FileNode& fn) const;
string name() const;
void addParam(Algorithm& algo, const char* name,
int& value, bool readOnly=false,
int (Algorithm::*getter)()=0,
void (Algorithm::*setter)(int)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
short& value, bool readOnly=false,
int (Algorithm::*getter)()=0,
void (Algorithm::*setter)(int)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
bool& value, bool readOnly=false,
int (Algorithm::*getter)()=0,
void (Algorithm::*setter)(int)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
double& value, bool readOnly=false,
double (Algorithm::*getter)()=0,
void (Algorithm::*setter)(double)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
string& value, bool readOnly=false,
string (Algorithm::*getter)()=0,
void (Algorithm::*setter)(const string&)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
Mat& value, bool readOnly=false,
Mat (Algorithm::*getter)()=0,
void (Algorithm::*setter)(const Mat&)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
vector<Mat>& value, bool readOnly=false,
vector<Mat> (Algorithm::*getter)()=0,
void (Algorithm::*setter)(const vector<Mat>&)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
Ptr<Algorithm>& value, bool readOnly=false,
Ptr<Algorithm> (Algorithm::*getter)()=0,
void (Algorithm::*setter)(const Ptr<Algorithm>&)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
float& value, bool readOnly=false,
float (Algorithm::*getter)()=0,
void (Algorithm::*setter)(float)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
unsigned int& value, bool readOnly=false,
unsigned int (Algorithm::*getter)()=0,
void (Algorithm::*setter)(unsigned int)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
uint64& value, bool readOnly=false,
uint64 (Algorithm::*getter)()=0,
void (Algorithm::*setter)(uint64)=0,
const string& help=string());
void addParam(Algorithm& algo, const char* name,
uchar& value, bool readOnly=false,
uchar (Algorithm::*getter)()=0,
void (Algorithm::*setter)(uchar)=0,
const string& help=string());
template<typename _Tp, typename _Base> void addParam(Algorithm& algo, const char* name,
Ptr<_Tp>& value, bool readOnly=false,
Ptr<_Tp> (Algorithm::*getter)()=0,
void (Algorithm::*setter)(const Ptr<_Tp>&)=0,
const string& help=string());
template<typename _Tp> void addParam(Algorithm& algo, const char* name,
Ptr<_Tp>& value, bool readOnly=false,
Ptr<_Tp> (Algorithm::*getter)()=0,
void (Algorithm::*setter)(const Ptr<_Tp>&)=0,
const string& help=string());
protected:
AlgorithmInfoData* data;
void set(Algorithm* algo, const char* name, int argType,
const void* value, bool force=false) const;
};
struct CV_EXPORTS Param
{
enum { INT=0, BOOLEAN=1, REAL=2, STRING=3, MAT=4, MAT_VECTOR=5, ALGORITHM=6, FLOAT=7, UNSIGNED_INT=8, UINT64=9, SHORT=10, UCHAR=11 };
Param();
Param(int _type, bool _readonly, int _offset,
Algorithm::Getter _getter=0,
Algorithm::Setter _setter=0,
const string& _help=string());
int type;
int offset;
bool readonly;
Algorithm::Getter getter;
Algorithm::Setter setter;
string help;
};
template<> struct ParamType<bool>
{
typedef bool const_param_type;
typedef bool member_type;
enum { type = Param::BOOLEAN };
};
template<> struct ParamType<int>
{
typedef int const_param_type;
typedef int member_type;
enum { type = Param::INT };
};
template<> struct ParamType<short>
{
typedef int const_param_type;
typedef int member_type;
enum { type = Param::SHORT };
};
template<> struct ParamType<double>
{
typedef double const_param_type;
typedef double member_type;
enum { type = Param::REAL };
};
template<> struct ParamType<string>
{
typedef const string& const_param_type;
typedef string member_type;
enum { type = Param::STRING };
};
template<> struct ParamType<Mat>
{
typedef const Mat& const_param_type;
typedef Mat member_type;
enum { type = Param::MAT };
};
template<> struct ParamType<vector<Mat> >
{
typedef const vector<Mat>& const_param_type;
typedef vector<Mat> member_type;
enum { type = Param::MAT_VECTOR };
};
template<> struct ParamType<Algorithm>
{
typedef const Ptr<Algorithm>& const_param_type;
typedef Ptr<Algorithm> member_type;
enum { type = Param::ALGORITHM };
};
template<> struct ParamType<float>
{
typedef float const_param_type;
typedef float member_type;
enum { type = Param::FLOAT };
};
template<> struct ParamType<unsigned>
{
typedef unsigned const_param_type;
typedef unsigned member_type;
enum { type = Param::UNSIGNED_INT };
};
template<> struct ParamType<uint64>
{
typedef uint64 const_param_type;
typedef uint64 member_type;
enum { type = Param::UINT64 };
};
template<> struct ParamType<uchar>
{
typedef uchar const_param_type;
typedef uchar member_type;
enum { type = Param::UCHAR };
};
/*!
"\nThe CommandLineParser class is designed for command line arguments parsing\n"
"Keys map: \n"
"Before you start to work with CommandLineParser you have to create a map for keys.\n"
" It will look like this\n"
" const char* keys =\n"
" {\n"
" { s| string| 123asd |string parameter}\n"
" { d| digit | 100 |digit parameter }\n"
" { c|noCamera|false |without camera }\n"
" { 1| |some text|help }\n"
" { 2| |333 |another help }\n"
" };\n"
"Usage syntax: \n"
" \"{\" - start of parameter string.\n"
" \"}\" - end of parameter string\n"
" \"|\" - separator between short name, full name, default value and help\n"
"Supported syntax: \n"
" --key1=arg1 <If a key with '--' must has an argument\n"
" you have to assign it through '=' sign.> \n"
"<If the key with '--' doesn't have any argument, it means that it is a bool key>\n"
" -key2=arg2 <If a key with '-' must has an argument \n"
" you have to assign it through '=' sign.> \n"
"If the key with '-' doesn't have any argument, it means that it is a bool key\n"
" key3 <This key can't has any parameter> \n"
"Usage: \n"
" Imagine that the input parameters are next:\n"
" -s=string_value --digit=250 --noCamera lena.jpg 10000\n"
" CommandLineParser parser(argc, argv, keys) - create a parser object\n"
" parser.get<string>(\"s\" or \"string\") will return you first parameter value\n"
" parser.get<string>(\"s\", false or \"string\", false) will return you first parameter value\n"
" without spaces in end and begin\n"
" parser.get<int>(\"d\" or \"digit\") will return you second parameter value.\n"
" It also works with 'unsigned int', 'double', and 'float' types>\n"
" parser.get<bool>(\"c\" or \"noCamera\") will return you true .\n"
" If you enter this key in commandline>\n"
" It return you false otherwise.\n"
" parser.get<string>(\"1\") will return you the first argument without parameter (lena.jpg) \n"
" parser.get<int>(\"2\") will return you the second argument without parameter (10000)\n"
" It also works with 'unsigned int', 'double', and 'float' types \n"
*/
class CV_EXPORTS CommandLineParser
{
public:
//! the default constructor
CommandLineParser(int argc, const char* const argv[], const char* key_map);
//! get parameter, you can choose: delete spaces in end and begin or not
template<typename _Tp>
_Tp get(const std::string& name, bool space_delete=true)
{
if (!has(name))
{
return _Tp();
}
std::string str = getString(name);
return analyzeValue<_Tp>(str, space_delete);
}
//! print short name, full name, current value and help for all params
void printParams();
protected:
std::map<std::string, std::vector<std::string> > data;
std::string getString(const std::string& name);
bool has(const std::string& keys);
template<typename _Tp>
_Tp analyzeValue(const std::string& str, bool space_delete=false);
template<typename _Tp>
static _Tp getData(const std::string& str)
{
_Tp res = _Tp();
std::stringstream s1(str);
s1 >> res;
return res;
}
template<typename _Tp>
_Tp fromStringNumber(const std::string& str);//the default conversion function for numbers
};
template<> CV_EXPORTS
bool CommandLineParser::get<bool>(const std::string& name, bool space_delete);
template<> CV_EXPORTS
std::string CommandLineParser::analyzeValue<std::string>(const std::string& str, bool space_delete);
template<> CV_EXPORTS
int CommandLineParser::analyzeValue<int>(const std::string& str, bool space_delete);
template<> CV_EXPORTS
unsigned int CommandLineParser::analyzeValue<unsigned int>(const std::string& str, bool space_delete);
template<> CV_EXPORTS
uint64 CommandLineParser::analyzeValue<uint64>(const std::string& str, bool space_delete);
template<> CV_EXPORTS
float CommandLineParser::analyzeValue<float>(const std::string& str, bool space_delete);
template<> CV_EXPORTS
double CommandLineParser::analyzeValue<double>(const std::string& str, bool space_delete);
/////////////////////////////// Parallel Primitives //////////////////////////////////
// a base body class
class CV_EXPORTS ParallelLoopBody
{
public:
virtual ~ParallelLoopBody();
virtual void operator() (const Range& range) const = 0;
};
CV_EXPORTS void parallel_for_(const Range& range, const ParallelLoopBody& body, double nstripes=-1.);
/////////////////////////// Synchronization Primitives ///////////////////////////////
class CV_EXPORTS Mutex
{
public:
Mutex();
~Mutex();
Mutex(const Mutex& m);
Mutex& operator = (const Mutex& m);
void lock();
bool trylock();
void unlock();
struct Impl;
protected:
Impl* impl;
};
class CV_EXPORTS AutoLock
{
public:
AutoLock(Mutex& m) : mutex(&m) { mutex->lock(); }
~AutoLock() { mutex->unlock(); }
protected:
Mutex* mutex;
private:
AutoLock(const AutoLock&);
AutoLock& operator = (const AutoLock&);
};
class TLSDataContainer
{
private:
int key_;
protected:
CV_EXPORTS TLSDataContainer();
CV_EXPORTS ~TLSDataContainer(); // virtual is not required
public:
virtual void* createDataInstance() const = 0;
virtual void deleteDataInstance(void* data) const = 0;
CV_EXPORTS void* getData() const;
};
template <typename T>
class TLSData : protected TLSDataContainer
{
public:
inline TLSData() {}
inline ~TLSData() {}
inline T* get() const { return (T*)getData(); }
private:
virtual void* createDataInstance() const { return new T; }
virtual void deleteDataInstance(void* data) const { delete (T*)data; }
};
}
#endif // __cplusplus
#include "opencv2/core/operations.hpp"
#include "opencv2/core/mat.hpp"
#endif /*__OPENCV_CORE_HPP__*/
|