/usr/include/vigra/separableconvolution.hxx is in libvigraimpex-dev 1.7.1+dfsg1-2ubuntu4.
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 | /************************************************************************/
/* */
/* Copyright 1998-2002 by Ullrich Koethe */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_SEPARABLECONVOLUTION_HXX
#define VIGRA_SEPARABLECONVOLUTION_HXX
#include <cmath>
#include "utilities.hxx"
#include "numerictraits.hxx"
#include "imageiteratoradapter.hxx"
#include "bordertreatment.hxx"
#include "gaussians.hxx"
#include "array_vector.hxx"
namespace vigra {
/********************************************************/
/* */
/* internalConvolveLineWrap */
/* */
/********************************************************/
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void internalConvolveLineWrap(SrcIterator is, SrcIterator iend, SrcAccessor sa,
DestIterator id, DestAccessor da,
KernelIterator kernel, KernelAccessor ka,
int kleft, int kright)
{
// int w = iend - is;
int w = std::distance( is, iend );
typedef typename PromoteTraits<
typename SrcAccessor::value_type,
typename KernelAccessor::value_type>::Promote SumType;
SrcIterator ibegin = is;
for(int x=0; x<w; ++x, ++is, ++id)
{
KernelIterator ik = kernel + kright;
SumType sum = NumericTraits<SumType>::zero();
if(x < kright)
{
int x0 = x - kright;
SrcIterator iss = iend + x0;
for(; x0; ++x0, --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
iss = ibegin;
SrcIterator isend = is + (1 - kleft);
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
}
else if(w-x <= -kleft)
{
SrcIterator iss = is + (-kright);
SrcIterator isend = iend;
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
int x0 = -kleft - w + x + 1;
iss = ibegin;
for(; x0; --x0, --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
}
else
{
SrcIterator iss = is - kright;
SrcIterator isend = is + (1 - kleft);
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
}
da.set(detail::RequiresExplicitCast<typename
DestAccessor::value_type>::cast(sum), id);
}
}
/********************************************************/
/* */
/* internalConvolveLineClip */
/* */
/********************************************************/
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor,
class Norm>
void internalConvolveLineClip(SrcIterator is, SrcIterator iend, SrcAccessor sa,
DestIterator id, DestAccessor da,
KernelIterator kernel, KernelAccessor ka,
int kleft, int kright, Norm norm)
{
// int w = iend - is;
int w = std::distance( is, iend );
typedef typename PromoteTraits<
typename SrcAccessor::value_type,
typename KernelAccessor::value_type>::Promote SumType;
SrcIterator ibegin = is;
for(int x=0; x<w; ++x, ++is, ++id)
{
KernelIterator ik = kernel + kright;
SumType sum = NumericTraits<SumType>::zero();
if(x < kright)
{
int x0 = x - kright;
Norm clipped = NumericTraits<Norm>::zero();
for(; x0; ++x0, --ik)
{
clipped += ka(ik);
}
SrcIterator iss = ibegin;
SrcIterator isend = is + (1 - kleft);
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
sum = norm / (norm - clipped) * sum;
}
else if(w-x <= -kleft)
{
SrcIterator iss = is + (-kright);
SrcIterator isend = iend;
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
Norm clipped = NumericTraits<Norm>::zero();
int x0 = -kleft - w + x + 1;
for(; x0; --x0, --ik)
{
clipped += ka(ik);
}
sum = norm / (norm - clipped) * sum;
}
else
{
SrcIterator iss = is + (-kright);
SrcIterator isend = is + (1 - kleft);
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
}
da.set(detail::RequiresExplicitCast<typename
DestAccessor::value_type>::cast(sum), id);
}
}
/********************************************************/
/* */
/* internalConvolveLineReflect */
/* */
/********************************************************/
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void internalConvolveLineReflect(SrcIterator is, SrcIterator iend, SrcAccessor sa,
DestIterator id, DestAccessor da,
KernelIterator kernel, KernelAccessor ka,
int kleft, int kright)
{
// int w = iend - is;
int w = std::distance( is, iend );
typedef typename PromoteTraits<
typename SrcAccessor::value_type,
typename KernelAccessor::value_type>::Promote SumType;
SrcIterator ibegin = is;
for(int x=0; x<w; ++x, ++is, ++id)
{
KernelIterator ik = kernel + kright;
SumType sum = NumericTraits<SumType>::zero();
if(x < kright)
{
int x0 = x - kright;
SrcIterator iss = ibegin - x0;
for(; x0; ++x0, --ik, --iss)
{
sum += ka(ik) * sa(iss);
}
SrcIterator isend = is + (1 - kleft);
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
}
else if(w-x <= -kleft)
{
SrcIterator iss = is + (-kright);
SrcIterator isend = iend;
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
int x0 = -kleft - w + x + 1;
iss = iend - 2;
for(; x0; --x0, --ik, --iss)
{
sum += ka(ik) * sa(iss);
}
}
else
{
SrcIterator iss = is + (-kright);
SrcIterator isend = is + (1 - kleft);
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
}
da.set(detail::RequiresExplicitCast<typename
DestAccessor::value_type>::cast(sum), id);
}
}
/********************************************************/
/* */
/* internalConvolveLineRepeat */
/* */
/********************************************************/
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void internalConvolveLineRepeat(SrcIterator is, SrcIterator iend, SrcAccessor sa,
DestIterator id, DestAccessor da,
KernelIterator kernel, KernelAccessor ka,
int kleft, int kright)
{
// int w = iend - is;
int w = std::distance( is, iend );
typedef typename PromoteTraits<
typename SrcAccessor::value_type,
typename KernelAccessor::value_type>::Promote SumType;
SrcIterator ibegin = is;
for(int x=0; x<w; ++x, ++is, ++id)
{
KernelIterator ik = kernel + kright;
SumType sum = NumericTraits<SumType>::zero();
if(x < kright)
{
int x0 = x - kright;
SrcIterator iss = ibegin;
for(; x0; ++x0, --ik)
{
sum += ka(ik) * sa(iss);
}
SrcIterator isend = is + (1 - kleft);
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
}
else if(w-x <= -kleft)
{
SrcIterator iss = is + (-kright);
SrcIterator isend = iend;
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
int x0 = -kleft - w + x + 1;
iss = iend - 1;
for(; x0; --x0, --ik)
{
sum += ka(ik) * sa(iss);
}
}
else
{
SrcIterator iss = is + (-kright);
SrcIterator isend = is + (1 - kleft);
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
}
da.set(detail::RequiresExplicitCast<typename
DestAccessor::value_type>::cast(sum), id);
}
}
/********************************************************/
/* */
/* internalConvolveLineAvoid */
/* */
/********************************************************/
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void internalConvolveLineAvoid(SrcIterator is, SrcIterator iend, SrcAccessor sa,
DestIterator id, DestAccessor da,
KernelIterator kernel, KernelAccessor ka,
int kleft, int kright)
{
// int w = iend - is;
int w = std::distance( is, iend );
typedef typename PromoteTraits<
typename SrcAccessor::value_type,
typename KernelAccessor::value_type>::Promote SumType;
is += kright;
id += kright;
for(int x=kright; x<w+kleft; ++x, ++is, ++id)
{
KernelIterator ik = kernel + kright;
SumType sum = NumericTraits<SumType>::zero();
SrcIterator iss = is + (-kright);
SrcIterator isend = is + (1 - kleft);
for(; iss != isend ; --ik, ++iss)
{
sum += ka(ik) * sa(iss);
}
da.set(detail::RequiresExplicitCast<typename
DestAccessor::value_type>::cast(sum), id);
}
}
/********************************************************/
/* */
/* Separable convolution functions */
/* */
/********************************************************/
/** \addtogroup SeparableConvolution One-dimensional and separable convolution functions
Perform 1D convolution and separable filtering in 2 dimensions.
These generic convolution functions implement
the standard convolution operation for a wide range of images and
signals that fit into the required interface. They need a suitable
kernel to operate.
*/
//@{
/** \brief Performs a 1-dimensional convolution of the source signal using the given
kernel.
The KernelIterator must point to the center iterator, and
the kernel's size is given by its left (kleft <= 0) and right
(kright >= 0) borders. The signal must always be larger than the kernel.
At those positions where the kernel does not completely fit
into the signal's range, the specified \ref BorderTreatmentMode is
applied.
The signal's value_type (SrcAccessor::value_type) must be a
linear space over the kernel's value_type (KernelAccessor::value_type),
i.e. addition of source values, multiplication with kernel values,
and NumericTraits must be defined.
The kernel's value_type must be an algebraic field,
i.e. the arithmetic operations (+, -, *, /) and NumericTraits must
be defined.
<b> Declarations:</b>
pass arguments explicitly:
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void convolveLine(SrcIterator is, SrcIterator isend, SrcAccessor sa,
DestIterator id, DestAccessor da,
KernelIterator ik, KernelAccessor ka,
int kleft, int kright, BorderTreatmentMode border)
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void convolveLine(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
tuple5<KernelIterator, KernelAccessor,
int, int, BorderTreatmentMode> kernel)
}
\endcode
<b> Usage:</b>
<b>\#include</b> \<<a href="separableconvolution_8hxx-source.html">vigra/separableconvolution.hxx</a>\>
\code
std::vector<float> src, dest;
...
// define binomial filter of size 5
static float kernel[] =
{ 1.0/16.0, 4.0/16.0, 6.0/16.0, 4.0/16.0, 1.0/16.0};
typedef vigra::StandardAccessor<float> FAccessor;
typedef vigra::StandardAccessor<float> KernelAccessor;
vigra::convolveLine(src.begin(), src.end(), FAccessor(), dest.begin(), FAccessor(),
kernel+2, KernelAccessor(), -2, 2, BORDER_TREATMENT_REFLECT);
// ^^^^^^^^ this is the center of the kernel
\endcode
<b> Required Interface:</b>
\code
RandomAccessIterator is, isend;
RandomAccessIterator id;
RandomAccessIterator ik;
SrcAccessor src_accessor;
DestAccessor dest_accessor;
KernelAccessor kernel_accessor;
NumericTraits<SrcAccessor::value_type>::RealPromote s = src_accessor(is);
s = s + s;
s = kernel_accessor(ik) * s;
dest_accessor.set(
NumericTraits<DestAccessor::value_type>::fromRealPromote(s), id);
\endcode
If border == BORDER_TREATMENT_CLIP:
\code
NumericTraits<KernelAccessor::value_type>::RealPromote k = kernel_accessor(ik);
k = k + k;
k = k - k;
k = k * k;
k = k / k;
\endcode
<b> Preconditions:</b>
\code
kleft <= 0
kright >= 0
iend - is >= kright + kleft + 1
\endcode
If border == BORDER_TREATMENT_CLIP: Sum of kernel elements must be
!= 0.
*/
doxygen_overloaded_function(template <...> void convolveLine)
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void convolveLine(SrcIterator is, SrcIterator iend, SrcAccessor sa,
DestIterator id, DestAccessor da,
KernelIterator ik, KernelAccessor ka,
int kleft, int kright, BorderTreatmentMode border)
{
typedef typename KernelAccessor::value_type KernelValue;
vigra_precondition(kleft <= 0,
"convolveLine(): kleft must be <= 0.\n");
vigra_precondition(kright >= 0,
"convolveLine(): kright must be >= 0.\n");
// int w = iend - is;
int w = std::distance( is, iend );
vigra_precondition(w >= kright - kleft + 1,
"convolveLine(): kernel longer than line\n");
switch(border)
{
case BORDER_TREATMENT_WRAP:
{
internalConvolveLineWrap(is, iend, sa, id, da, ik, ka, kleft, kright);
break;
}
case BORDER_TREATMENT_AVOID:
{
internalConvolveLineAvoid(is, iend, sa, id, da, ik, ka, kleft, kright);
break;
}
case BORDER_TREATMENT_REFLECT:
{
internalConvolveLineReflect(is, iend, sa, id, da, ik, ka, kleft, kright);
break;
}
case BORDER_TREATMENT_REPEAT:
{
internalConvolveLineRepeat(is, iend, sa, id, da, ik, ka, kleft, kright);
break;
}
case BORDER_TREATMENT_CLIP:
{
// find norm of kernel
typedef typename KernelAccessor::value_type KT;
KT norm = NumericTraits<KT>::zero();
KernelIterator iik = ik + kleft;
for(int i=kleft; i<=kright; ++i, ++iik) norm += ka(iik);
vigra_precondition(norm != NumericTraits<KT>::zero(),
"convolveLine(): Norm of kernel must be != 0"
" in mode BORDER_TREATMENT_CLIP.\n");
internalConvolveLineClip(is, iend, sa, id, da, ik, ka, kleft, kright, norm);
break;
}
default:
{
vigra_precondition(0,
"convolveLine(): Unknown border treatment mode.\n");
}
}
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
inline
void convolveLine(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
tuple5<KernelIterator, KernelAccessor,
int, int, BorderTreatmentMode> kernel)
{
convolveLine(src.first, src.second, src.third,
dest.first, dest.second,
kernel.first, kernel.second,
kernel.third, kernel.fourth, kernel.fifth);
}
/********************************************************/
/* */
/* separableConvolveX */
/* */
/********************************************************/
/** \brief Performs a 1 dimensional convolution in x direction.
It calls \ref convolveLine() for every row of the image. See \ref convolveLine()
for more information about required interfaces and vigra_preconditions.
<b> Declarations:</b>
pass arguments explicitly:
\code
namespace vigra {
template <class SrcImageIterator, class SrcAccessor,
class DestImageIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void separableConvolveX(SrcImageIterator supperleft,
SrcImageIterator slowerright, SrcAccessor sa,
DestImageIterator dupperleft, DestAccessor da,
KernelIterator ik, KernelAccessor ka,
int kleft, int kright, BorderTreatmentMode border)
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcImageIterator, class SrcAccessor,
class DestImageIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void separableConvolveX(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
pair<DestImageIterator, DestAccessor> dest,
tuple5<KernelIterator, KernelAccessor,
int, int, BorderTreatmentMode> kernel)
}
\endcode
<b> Usage:</b>
<b>\#include</b> \<<a href="separableconvolution_8hxx-source.html">vigra/separableconvolution.hxx</a>\>
\code
vigra::FImage src(w,h), dest(w,h);
...
// define Gaussian kernel with std. deviation 3.0
vigra::Kernel1D<double> kernel;
kernel.initGaussian(3.0);
vigra::separableConvolveX(srcImageRange(src), destImage(dest), kernel1d(kernel));
\endcode
*/
doxygen_overloaded_function(template <...> void separableConvolveX)
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void separableConvolveX(SrcIterator supperleft,
SrcIterator slowerright, SrcAccessor sa,
DestIterator dupperleft, DestAccessor da,
KernelIterator ik, KernelAccessor ka,
int kleft, int kright, BorderTreatmentMode border)
{
typedef typename KernelAccessor::value_type KernelValue;
vigra_precondition(kleft <= 0,
"separableConvolveX(): kleft must be <= 0.\n");
vigra_precondition(kright >= 0,
"separableConvolveX(): kright must be >= 0.\n");
int w = slowerright.x - supperleft.x;
int h = slowerright.y - supperleft.y;
vigra_precondition(w >= kright - kleft + 1,
"separableConvolveX(): kernel longer than line\n");
int y;
for(y=0; y<h; ++y, ++supperleft.y, ++dupperleft.y)
{
typename SrcIterator::row_iterator rs = supperleft.rowIterator();
typename DestIterator::row_iterator rd = dupperleft.rowIterator();
convolveLine(rs, rs+w, sa, rd, da,
ik, ka, kleft, kright, border);
}
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
inline void
separableConvolveX(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
tuple5<KernelIterator, KernelAccessor,
int, int, BorderTreatmentMode> kernel)
{
separableConvolveX(src.first, src.second, src.third,
dest.first, dest.second,
kernel.first, kernel.second,
kernel.third, kernel.fourth, kernel.fifth);
}
/********************************************************/
/* */
/* separableConvolveY */
/* */
/********************************************************/
/** \brief Performs a 1 dimensional convolution in y direction.
It calls \ref convolveLine() for every column of the image. See \ref convolveLine()
for more information about required interfaces and vigra_preconditions.
<b> Declarations:</b>
pass arguments explicitly:
\code
namespace vigra {
template <class SrcImageIterator, class SrcAccessor,
class DestImageIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void separableConvolveY(SrcImageIterator supperleft,
SrcImageIterator slowerright, SrcAccessor sa,
DestImageIterator dupperleft, DestAccessor da,
KernelIterator ik, KernelAccessor ka,
int kleft, int kright, BorderTreatmentMode border)
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcImageIterator, class SrcAccessor,
class DestImageIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void separableConvolveY(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
pair<DestImageIterator, DestAccessor> dest,
tuple5<KernelIterator, KernelAccessor,
int, int, BorderTreatmentMode> kernel)
}
\endcode
<b> Usage:</b>
<b>\#include</b> \<<a href="separableconvolution_8hxx-source.html">vigra/separableconvolution.hxx</a>\>
\code
vigra::FImage src(w,h), dest(w,h);
...
// define Gaussian kernel with std. deviation 3.0
vigra::Kernel1D kernel;
kernel.initGaussian(3.0);
vigra::separableConvolveY(srcImageRange(src), destImage(dest), kernel1d(kernel));
\endcode
*/
doxygen_overloaded_function(template <...> void separableConvolveY)
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
void separableConvolveY(SrcIterator supperleft,
SrcIterator slowerright, SrcAccessor sa,
DestIterator dupperleft, DestAccessor da,
KernelIterator ik, KernelAccessor ka,
int kleft, int kright, BorderTreatmentMode border)
{
typedef typename KernelAccessor::value_type KernelValue;
vigra_precondition(kleft <= 0,
"separableConvolveY(): kleft must be <= 0.\n");
vigra_precondition(kright >= 0,
"separableConvolveY(): kright must be >= 0.\n");
int w = slowerright.x - supperleft.x;
int h = slowerright.y - supperleft.y;
vigra_precondition(h >= kright - kleft + 1,
"separableConvolveY(): kernel longer than line\n");
int x;
for(x=0; x<w; ++x, ++supperleft.x, ++dupperleft.x)
{
typename SrcIterator::column_iterator cs = supperleft.columnIterator();
typename DestIterator::column_iterator cd = dupperleft.columnIterator();
convolveLine(cs, cs+h, sa, cd, da,
ik, ka, kleft, kright, border);
}
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class KernelIterator, class KernelAccessor>
inline void
separableConvolveY(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
tuple5<KernelIterator, KernelAccessor,
int, int, BorderTreatmentMode> kernel)
{
separableConvolveY(src.first, src.second, src.third,
dest.first, dest.second,
kernel.first, kernel.second,
kernel.third, kernel.fourth, kernel.fifth);
}
//@}
/********************************************************/
/* */
/* Kernel1D */
/* */
/********************************************************/
/** \brief Generic 1 dimensional convolution kernel.
This kernel may be used for convolution of 1 dimensional signals or for
separable convolution of multidimensional signals.
Convlution functions access the kernel via a 1 dimensional random access
iterator which they get by calling \ref center(). This iterator
points to the center of the kernel. The kernel's size is given by its left() (<=0)
and right() (>= 0) methods. The desired border treatment mode is
returned by borderTreatment().
The different init functions create a kernel with the specified
properties. The kernel's value_type must be a linear space, i.e. it
must define multiplication with doubles and NumericTraits.
The kernel defines a factory function kernel1d() to create an argument object
(see \ref KernelArgumentObjectFactories).
<b> Usage:</b>
<b>\#include</b> \<<a href="stdconvolution_8hxx-source.html">vigra/stdconvolution.hxx</a>\>
\code
vigra::FImage src(w,h), dest(w,h);
...
// define Gaussian kernel with std. deviation 3.0
vigra::Kernel1D kernel;
kernel.initGaussian(3.0);
vigra::separableConvolveX(srcImageRange(src), destImage(dest), kernel1d(kernel));
\endcode
<b> Required Interface:</b>
\code
value_type v = vigra::NumericTraits<value_type>::one(); // if norm is not
// given explicitly
double d;
v = d * v;
\endcode
*/
template <class ARITHTYPE>
class Kernel1D
{
public:
typedef ArrayVector<ARITHTYPE> InternalVector;
/** the kernel's value type
*/
typedef typename InternalVector::value_type value_type;
/** the kernel's reference type
*/
typedef typename InternalVector::reference reference;
/** the kernel's const reference type
*/
typedef typename InternalVector::const_reference const_reference;
/** deprecated -- use Kernel1D::iterator
*/
typedef typename InternalVector::iterator Iterator;
/** 1D random access iterator over the kernel's values
*/
typedef typename InternalVector::iterator iterator;
/** const 1D random access iterator over the kernel's values
*/
typedef typename InternalVector::const_iterator const_iterator;
/** the kernel's accessor
*/
typedef StandardAccessor<ARITHTYPE> Accessor;
/** the kernel's const accessor
*/
typedef StandardConstAccessor<ARITHTYPE> ConstAccessor;
struct InitProxy
{
InitProxy(Iterator i, int count, value_type & norm)
: iter_(i), base_(i),
count_(count), sum_(count),
norm_(norm)
{}
~InitProxy()
{
vigra_precondition(count_ == 1 || count_ == sum_,
"Kernel1D::initExplicitly(): "
"Wrong number of init values.");
}
InitProxy & operator,(value_type const & v)
{
if(sum_ == count_)
norm_ = *iter_;
norm_ += v;
--count_;
if(count_ > 0)
{
++iter_;
*iter_ = v;
}
return *this;
}
Iterator iter_, base_;
int count_, sum_;
value_type & norm_;
};
static value_type one() { return NumericTraits<value_type>::one(); }
/** Default constructor.
Creates a kernel of size 1 which would copy the signal
unchanged.
*/
Kernel1D()
: kernel_(),
left_(0),
right_(0),
border_treatment_(BORDER_TREATMENT_REFLECT),
norm_(one())
{
kernel_.push_back(norm_);
}
/** Copy constructor.
*/
Kernel1D(Kernel1D const & k)
: kernel_(k.kernel_),
left_(k.left_),
right_(k.right_),
border_treatment_(k.border_treatment_),
norm_(k.norm_)
{}
/** Construct from kernel with different element type, e.g. double => FixedPoint16.
*/
template <class U>
Kernel1D(Kernel1D<U> const & k)
: kernel_(k.center()+k.left(), k.center()+k.right()+1),
left_(k.left()),
right_(k.right()),
border_treatment_(k.borderTreatment()),
norm_(k.norm())
{}
/** Copy assignment.
*/
Kernel1D & operator=(Kernel1D const & k)
{
if(this != &k)
{
left_ = k.left_;
right_ = k.right_;
border_treatment_ = k.border_treatment_;
norm_ = k.norm_;
kernel_ = k.kernel_;
}
return *this;
}
/** Initialization.
This initializes the kernel with the given constant. The norm becomes
v*size().
Instead of a single value an initializer list of length size()
can be used like this:
\code
vigra::Kernel1D<float> roberts_gradient_x;
roberts_gradient_x.initExplicitly(0, 1) = 1.0, -1.0;
\endcode
In this case, the norm will be set to the sum of the init values.
An initializer list of wrong length will result in a run-time error.
*/
InitProxy operator=(value_type const & v)
{
int size = right_ - left_ + 1;
for(unsigned int i=0; i<kernel_.size(); ++i) kernel_[i] = v;
norm_ = (double)size*v;
return InitProxy(kernel_.begin(), size, norm_);
}
/** Destructor.
*/
~Kernel1D()
{}
/**
Init as a sampled Gaussian function. The radius of the kernel is
always 3*std_dev. '<tt>norm</tt>' denotes the sum of all bins of the kernel
(i.e. the kernel is corrected for the normalization error introduced
by windowing the Gaussian to a finite interval). However,
if <tt>norm</tt> is 0.0, the kernel is normalized to 1 by the analytic
expression for the Gaussian, and <b>no</b> correction for the windowing
error is performed.
Precondition:
\code
std_dev >= 0.0
\endcode
Postconditions:
\code
1. left() == -(int)(3.0*std_dev + 0.5)
2. right() == (int)(3.0*std_dev + 0.5)
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == norm
\endcode
*/
void initGaussian(double std_dev, value_type norm);
/** Init as a Gaussian function with norm 1.
*/
void initGaussian(double std_dev)
{
initGaussian(std_dev, one());
}
/**
Init as Lindeberg's discrete analog of the Gaussian function. The radius of the kernel is
always 3*std_dev. 'norm' denotes the sum of all bins of the kernel.
Precondition:
\code
std_dev >= 0.0
\endcode
Postconditions:
\code
1. left() == -(int)(3.0*std_dev + 0.5)
2. right() == (int)(3.0*std_dev + 0.5)
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == norm
\endcode
*/
void initDiscreteGaussian(double std_dev, value_type norm);
/** Init as a Lindeberg's discrete analog of the Gaussian function
with norm 1.
*/
void initDiscreteGaussian(double std_dev)
{
initDiscreteGaussian(std_dev, one());
}
/**
Init as a Gaussian derivative of order '<tt>order</tt>'.
The radius of the kernel is always <tt>3*std_dev + 0.5*order</tt>.
'<tt>norm</tt>' denotes the norm of the kernel so that the
following condition is fulfilled:
\f[ \sum_{i=left()}^{right()}
\frac{(-i)^{order}kernel[i]}{order!} = norm
\f]
Thus, the kernel will be corrected for the error introduced
by windowing the Gaussian to a finite interval. However,
if <tt>norm</tt> is 0.0, the kernel is normalized to 1 by the analytic
expression for the Gaussian derivative, and <b>no</b> correction for the
windowing error is performed.
Preconditions:
\code
1. std_dev >= 0.0
2. order >= 1
\endcode
Postconditions:
\code
1. left() == -(int)(3.0*std_dev + 0.5*order + 0.5)
2. right() == (int)(3.0*std_dev + 0.5*order + 0.5)
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == norm
\endcode
*/
void initGaussianDerivative(double std_dev, int order, value_type norm);
/** Init as a Gaussian derivative with norm 1.
*/
void initGaussianDerivative(double std_dev, int order)
{
initGaussianDerivative(std_dev, order, one());
}
/**
Init an optimal 3-tap smoothing filter.
The filter values are
\code
[0.216, 0.568, 0.216]
\endcode
These values are optimal in the sense that the 3x3 filter obtained by separable application
of this filter is the best possible 3x3 approximation to a Gaussian filter.
The equivalent Gaussian has sigma = 0.680.
Postconditions:
\code
1. left() == -1
2. right() == 1
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initOptimalSmoothing3()
{
this->initExplicitly(-1, 1) = 0.216, 0.568, 0.216;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/**
Init an optimal 3-tap smoothing filter to be used in the context of first derivative computation.
This filter must be used in conjunction with the symmetric difference filter (see initSymmetricDifference()),
such that the difference filter is applied along one dimension, and the smoothing filter along the other.
The filter values are
\code
[0.224365, 0.55127, 0.224365]
\endcode
These values are optimal in the sense that the 3x3 filter obtained by combining
this filter with the symmetric difference is the best possible 3x3 approximation to a
Gaussian first derivative filter. The equivalent Gaussian has sigma = 0.675.
Postconditions:
\code
1. left() == -1
2. right() == 1
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initOptimalFirstDerivativeSmoothing3()
{
this->initExplicitly(-1, 1) = 0.224365, 0.55127, 0.224365;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/**
Init an optimal 3-tap smoothing filter to be used in the context of second derivative computation.
This filter must be used in conjunction with the 3-tap second difference filter (see initSecondDifference3()),
such that the difference filter is applied along one dimension, and the smoothing filter along the other.
The filter values are
\code
[0.13, 0.74, 0.13]
\endcode
These values are optimal in the sense that the 3x3 filter obtained by combining
this filter with the 3-tap second difference is the best possible 3x3 approximation to a
Gaussian second derivative filter. The equivalent Gaussian has sigma = 0.433.
Postconditions:
\code
1. left() == -1
2. right() == 1
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initOptimalSecondDerivativeSmoothing3()
{
this->initExplicitly(-1, 1) = 0.13, 0.74, 0.13;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/**
Init an optimal 5-tap smoothing filter.
The filter values are
\code
[0.03134, 0.24, 0.45732, 0.24, 0.03134]
\endcode
These values are optimal in the sense that the 5x5 filter obtained by separable application
of this filter is the best possible 5x5 approximation to a Gaussian filter.
The equivalent Gaussian has sigma = 0.867.
Postconditions:
\code
1. left() == -2
2. right() == 2
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initOptimalSmoothing5()
{
this->initExplicitly(-2, 2) = 0.03134, 0.24, 0.45732, 0.24, 0.03134;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/**
Init an optimal 5-tap smoothing filter to be used in the context of first derivative computation.
This filter must be used in conjunction with the optimal 5-tap first derivative filter
(see initOptimalFirstDerivative5()), such that the derivative filter is applied along one dimension,
and the smoothing filter along the other. The filter values are
\code
[0.04255, 0.241, 0.4329, 0.241, 0.04255]
\endcode
These values are optimal in the sense that the 5x5 filter obtained by combining
this filter with the optimal 5-tap first derivative is the best possible 5x5 approximation to a
Gaussian first derivative filter. The equivalent Gaussian has sigma = 0.906.
Postconditions:
\code
1. left() == -2
2. right() == 2
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initOptimalFirstDerivativeSmoothing5()
{
this->initExplicitly(-2, 2) = 0.04255, 0.241, 0.4329, 0.241, 0.04255;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/**
Init an optimal 5-tap smoothing filter to be used in the context of second derivative computation.
This filter must be used in conjunction with the optimal 5-tap second derivative filter
(see initOptimalSecondDerivative5()), such that the derivative filter is applied along one dimension,
and the smoothing filter along the other. The filter values are
\code
[0.0243, 0.23556, 0.48028, 0.23556, 0.0243]
\endcode
These values are optimal in the sense that the 5x5 filter obtained by combining
this filter with the optimal 5-tap second derivative is the best possible 5x5 approximation to a
Gaussian second derivative filter. The equivalent Gaussian has sigma = 0.817.
Postconditions:
\code
1. left() == -2
2. right() == 2
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initOptimalSecondDerivativeSmoothing5()
{
this->initExplicitly(-2, 2) = 0.0243, 0.23556, 0.48028, 0.23556, 0.0243;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/**
Init a 5-tap filter as defined by Peter Burt in the context of pyramid creation.
The filter values are
\code
[a, 0.25, 0.5-2*a, 0.25, a]
\endcode
The default <tt>a = 0.04785</tt> is optimal in the sense that it minimizes the difference
to a true Gaussian filter (which would have sigma = 0.975). For other values of <tt>a</tt>, the scale
of the most similar Gaussian can be approximated by
\code
sigma = 5.1 * a + 0.731
\endcode
Preconditions:
\code
0 <= a <= 0.125
\endcode
Postconditions:
\code
1. left() == -2
2. right() == 2
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initBurtFilter(double a = 0.04785)
{
vigra_precondition(a >= 0.0 && a <= 0.125,
"Kernel1D::initBurtFilter(): 0 <= a <= 0.125 required.");
this->initExplicitly(-2, 2) = a, 0.25, 0.5 - 2.0*a, 0.25, a;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/**
Init as a Binomial filter. 'norm' denotes the sum of all bins
of the kernel.
Precondition:
\code
radius >= 0
\endcode
Postconditions:
\code
1. left() == -radius
2. right() == radius
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == norm
\endcode
*/
void initBinomial(int radius, value_type norm);
/** Init as a Binomial filter with norm 1.
*/
void initBinomial(int radius)
{
initBinomial(radius, one());
}
/**
Init as an Averaging filter. 'norm' denotes the sum of all bins
of the kernel. The window size is (2*radius+1) * (2*radius+1)
Precondition:
\code
radius >= 0
\endcode
Postconditions:
\code
1. left() == -radius
2. right() == radius
3. borderTreatment() == BORDER_TREATMENT_CLIP
4. norm() == norm
\endcode
*/
void initAveraging(int radius, value_type norm);
/** Init as an Averaging filter with norm 1.
*/
void initAveraging(int radius)
{
initAveraging(radius, one());
}
/**
Init as a symmetric gradient filter of the form
<TT>[ 0.5 * norm, 0.0 * norm, -0.5 * norm]</TT>
<b>Deprecated</b>. Use initSymmetricDifference() instead.
Postconditions:
\code
1. left() == -1
2. right() == 1
3. borderTreatment() == BORDER_TREATMENT_REPEAT
4. norm() == norm
\endcode
*/
void
initSymmetricGradient(value_type norm )
{
initSymmetricDifference(norm);
setBorderTreatment(BORDER_TREATMENT_REPEAT);
}
/** Init as a symmetric gradient filter with norm 1.
<b>Deprecated</b>. Use initSymmetricDifference() instead.
*/
void initSymmetricGradient()
{
initSymmetricGradient(one());
}
void
initSymmetricDifference(value_type norm );
/** Init as the 3-tap symmetric difference filter
The filter values are
\code
[0.5, 0, -0.5]
\endcode
Postconditions:
\code
1. left() == -1
2. right() == 1
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initSymmetricDifference()
{
initSymmetricDifference(one());
}
/**
Init the 3-tap second difference filter.
The filter values are
\code
[1, -2, 1]
\endcode
Postconditions:
\code
1. left() == -1
2. right() == 1
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1
\endcode
*/
void
initSecondDifference3()
{
this->initExplicitly(-1, 1) = 1.0, -2.0, 1.0;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/**
Init an optimal 5-tap first derivative filter.
This filter must be used in conjunction with the corresponding 5-tap smoothing filter
(see initOptimalFirstDerivativeSmoothing5()), such that the derivative filter is applied along one dimension,
and the smoothing filter along the other.
The filter values are
\code
[0.1, 0.3, 0.0, -0.3, -0.1]
\endcode
These values are optimal in the sense that the 5x5 filter obtained by combining
this filter with the corresponding 5-tap smoothing filter is the best possible 5x5 approximation to a
Gaussian first derivative filter. The equivalent Gaussian has sigma = 0.906.
If the filter is instead separably combined with itself, an almost optimal approximation of the
mixed second Gaussian derivative at scale sigma = 0.899 results.
Postconditions:
\code
1. left() == -2
2. right() == 2
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initOptimalFirstDerivative5()
{
this->initExplicitly(-2, 2) = 0.1, 0.3, 0.0, -0.3, -0.1;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/**
Init an optimal 5-tap second derivative filter.
This filter must be used in conjunction with the corresponding 5-tap smoothing filter
(see initOptimalSecondDerivativeSmoothing5()), such that the derivative filter is applied along one dimension,
and the smoothing filter along the other.
The filter values are
\code
[0.22075, 0.117, -0.6755, 0.117, 0.22075]
\endcode
These values are optimal in the sense that the 5x5 filter obtained by combining
this filter with the corresponding 5-tap smoothing filter is the best possible 5x5 approximation to a
Gaussian second derivative filter. The equivalent Gaussian has sigma = 0.817.
Postconditions:
\code
1. left() == -2
2. right() == 2
3. borderTreatment() == BORDER_TREATMENT_REFLECT
4. norm() == 1.0
\endcode
*/
void initOptimalSecondDerivative5()
{
this->initExplicitly(-2, 2) = 0.22075, 0.117, -0.6755, 0.117, 0.22075;
this->setBorderTreatment(BORDER_TREATMENT_REFLECT);
}
/** Init the kernel by an explicit initializer list.
The left and right boundaries of the kernel must be passed.
A comma-separated initializer list is given after the assignment
operator. This function is used like this:
\code
// define horizontal Roberts filter
vigra::Kernel1D<float> roberts_gradient_x;
roberts_gradient_x.initExplicitly(0, 1) = 1.0, -1.0;
\endcode
The norm is set to the sum of the initialzer values. If the wrong number of
values is given, a run-time error results. It is, however, possible to give
just one initializer. This creates an averaging filter with the given constant:
\code
vigra::Kernel1D<float> average5x1;
average5x1.initExplicitly(-2, 2) = 1.0/5.0;
\endcode
Here, the norm is set to value*size().
<b> Preconditions:</b>
\code
1. left <= 0
2. right >= 0
3. the number of values in the initializer list
is 1 or equals the size of the kernel.
\endcode
*/
Kernel1D & initExplicitly(int left, int right)
{
vigra_precondition(left <= 0,
"Kernel1D::initExplicitly(): left border must be <= 0.");
vigra_precondition(right >= 0,
"Kernel1D::initExplicitly(): right border must be >= 0.");
right_ = right;
left_ = left;
kernel_.resize(right - left + 1);
return *this;
}
/** Get iterator to center of kernel
Postconditions:
\code
center()[left()] ... center()[right()] are valid kernel positions
\endcode
*/
iterator center()
{
return kernel_.begin() - left();
}
const_iterator center() const
{
return kernel_.begin() - left();
}
/** Access kernel value at specified location.
Preconditions:
\code
left() <= location <= right()
\endcode
*/
reference operator[](int location)
{
return kernel_[location - left()];
}
const_reference operator[](int location) const
{
return kernel_[location - left()];
}
/** left border of kernel (inclusive), always <= 0
*/
int left() const { return left_; }
/** right border of kernel (inclusive), always >= 0
*/
int right() const { return right_; }
/** size of kernel (right() - left() + 1)
*/
int size() const { return right_ - left_ + 1; }
/** current border treatment mode
*/
BorderTreatmentMode borderTreatment() const
{ return border_treatment_; }
/** Set border treatment mode.
*/
void setBorderTreatment( BorderTreatmentMode new_mode)
{ border_treatment_ = new_mode; }
/** norm of kernel
*/
value_type norm() const { return norm_; }
/** set a new norm and normalize kernel, use the normalization formula
for the given <tt>derivativeOrder</tt>.
*/
void
normalize(value_type norm, unsigned int derivativeOrder = 0, double offset = 0.0);
/** normalize kernel to norm 1.
*/
void
normalize()
{
normalize(one());
}
/** get a const accessor
*/
ConstAccessor accessor() const { return ConstAccessor(); }
/** get an accessor
*/
Accessor accessor() { return Accessor(); }
private:
InternalVector kernel_;
int left_, right_;
BorderTreatmentMode border_treatment_;
value_type norm_;
};
template <class ARITHTYPE>
void Kernel1D<ARITHTYPE>::normalize(value_type norm,
unsigned int derivativeOrder,
double offset)
{
typedef typename NumericTraits<value_type>::RealPromote TmpType;
// find kernel sum
Iterator k = kernel_.begin();
TmpType sum = NumericTraits<TmpType>::zero();
if(derivativeOrder == 0)
{
for(; k < kernel_.end(); ++k)
{
sum += *k;
}
}
else
{
unsigned int faculty = 1;
for(unsigned int i = 2; i <= derivativeOrder; ++i)
faculty *= i;
for(double x = left() + offset; k < kernel_.end(); ++x, ++k)
{
sum = TmpType(sum + *k * VIGRA_CSTD::pow(-x, int(derivativeOrder)) / faculty);
}
}
vigra_precondition(sum != NumericTraits<value_type>::zero(),
"Kernel1D<ARITHTYPE>::normalize(): "
"Cannot normalize a kernel with sum = 0");
// normalize
sum = norm / sum;
k = kernel_.begin();
for(; k != kernel_.end(); ++k)
{
*k = *k * sum;
}
norm_ = norm;
}
/***********************************************************************/
template <class ARITHTYPE>
void Kernel1D<ARITHTYPE>::initGaussian(double std_dev,
value_type norm)
{
vigra_precondition(std_dev >= 0.0,
"Kernel1D::initGaussian(): Standard deviation must be >= 0.");
if(std_dev > 0.0)
{
Gaussian<ARITHTYPE> gauss((ARITHTYPE)std_dev);
// first calculate required kernel sizes
int radius = (int)(3.0 * std_dev + 0.5);
if(radius == 0)
radius = 1;
// allocate the kernel
kernel_.erase(kernel_.begin(), kernel_.end());
kernel_.reserve(radius*2+1);
for(ARITHTYPE x = -(ARITHTYPE)radius; x <= (ARITHTYPE)radius; ++x)
{
kernel_.push_back(gauss(x));
}
left_ = -radius;
right_ = radius;
}
else
{
kernel_.erase(kernel_.begin(), kernel_.end());
kernel_.push_back(1.0);
left_ = 0;
right_ = 0;
}
if(norm != 0.0)
normalize(norm);
else
norm_ = 1.0;
// best border treatment for Gaussians is BORDER_TREATMENT_REFLECT
border_treatment_ = BORDER_TREATMENT_REFLECT;
}
/***********************************************************************/
template <class ARITHTYPE>
void Kernel1D<ARITHTYPE>::initDiscreteGaussian(double std_dev,
value_type norm)
{
vigra_precondition(std_dev >= 0.0,
"Kernel1D::initDiscreteGaussian(): Standard deviation must be >= 0.");
if(std_dev > 0.0)
{
// first calculate required kernel sizes
int radius = (int)(3.0*std_dev + 0.5);
if(radius == 0)
radius = 1;
double f = 2.0 / std_dev / std_dev;
// allocate the working array
int maxIndex = (int)(2.0 * (radius + 5.0 * VIGRA_CSTD::sqrt((double)radius)) + 0.5);
InternalVector warray(maxIndex+1);
warray[maxIndex] = 0.0;
warray[maxIndex-1] = 1.0;
for(int i = maxIndex-2; i >= radius; --i)
{
warray[i] = warray[i+2] + f * (i+1) * warray[i+1];
if(warray[i] > 1.0e40)
{
warray[i+1] /= warray[i];
warray[i] = 1.0;
}
}
// the following rescaling ensures that the numbers stay in a sensible range
// during the rest of the iteration, so no other rescaling is needed
double er = VIGRA_CSTD::exp(-radius*radius / (2.0*std_dev*std_dev));
warray[radius+1] = er * warray[radius+1] / warray[radius];
warray[radius] = er;
for(int i = radius-1; i >= 0; --i)
{
warray[i] = warray[i+2] + f * (i+1) * warray[i+1];
er += warray[i];
}
double scale = norm / (2*er - warray[0]);
initExplicitly(-radius, radius);
iterator c = center();
for(int i=0; i<=radius; ++i)
{
c[i] = c[-i] = warray[i] * scale;
}
}
else
{
kernel_.erase(kernel_.begin(), kernel_.end());
kernel_.push_back(norm);
left_ = 0;
right_ = 0;
}
norm_ = norm;
// best border treatment for Gaussians is BORDER_TREATMENT_REFLECT
border_treatment_ = BORDER_TREATMENT_REFLECT;
}
/***********************************************************************/
template <class ARITHTYPE>
void
Kernel1D<ARITHTYPE>::initGaussianDerivative(double std_dev,
int order,
value_type norm)
{
vigra_precondition(order >= 0,
"Kernel1D::initGaussianDerivative(): Order must be >= 0.");
if(order == 0)
{
initGaussian(std_dev, norm);
return;
}
vigra_precondition(std_dev > 0.0,
"Kernel1D::initGaussianDerivative(): "
"Standard deviation must be > 0.");
Gaussian<ARITHTYPE> gauss((ARITHTYPE)std_dev, order);
// first calculate required kernel sizes
int radius = (int)(3.0 * std_dev + 0.5 * order + 0.5);
if(radius == 0)
radius = 1;
// allocate the kernels
kernel_.clear();
kernel_.reserve(radius*2+1);
// fill the kernel and calculate the DC component
// introduced by truncation of the Gaussian
ARITHTYPE dc = 0.0;
for(ARITHTYPE x = -(ARITHTYPE)radius; x <= (ARITHTYPE)radius; ++x)
{
kernel_.push_back(gauss(x));
dc += kernel_[kernel_.size()-1];
}
dc = ARITHTYPE(dc / (2.0*radius + 1.0));
// remove DC, but only if kernel correction is permitted by a non-zero
// value for norm
if(norm != 0.0)
{
for(unsigned int i=0; i < kernel_.size(); ++i)
{
kernel_[i] -= dc;
}
}
left_ = -radius;
right_ = radius;
if(norm != 0.0)
normalize(norm, order);
else
norm_ = 1.0;
// best border treatment for Gaussian derivatives is
// BORDER_TREATMENT_REFLECT
border_treatment_ = BORDER_TREATMENT_REFLECT;
}
/***********************************************************************/
template <class ARITHTYPE>
void
Kernel1D<ARITHTYPE>::initBinomial(int radius,
value_type norm)
{
vigra_precondition(radius > 0,
"Kernel1D::initBinomial(): Radius must be > 0.");
// allocate the kernel
InternalVector(radius*2+1).swap(kernel_);
typename InternalVector::iterator x = kernel_.begin() + radius;
// fill kernel
x[radius] = norm;
for(int j=radius-1; j>=-radius; --j)
{
x[j] = 0.5 * x[j+1];
for(int i=j+1; i<radius; ++i)
{
x[i] = 0.5 * (x[i] + x[i+1]);
}
x[radius] *= 0.5;
}
left_ = -radius;
right_ = radius;
norm_ = norm;
// best border treatment for Binomial is BORDER_TREATMENT_REFLECT
border_treatment_ = BORDER_TREATMENT_REFLECT;
}
/***********************************************************************/
template <class ARITHTYPE>
void Kernel1D<ARITHTYPE>::initAveraging(int radius,
value_type norm)
{
vigra_precondition(radius > 0,
"Kernel1D::initAveraging(): Radius must be > 0.");
// calculate scaling
double scale = 1.0 / (radius * 2 + 1);
// normalize
kernel_.erase(kernel_.begin(), kernel_.end());
kernel_.reserve(radius*2+1);
for(int i=0; i<=radius*2+1; ++i)
{
kernel_.push_back(scale * norm);
}
left_ = -radius;
right_ = radius;
norm_ = norm;
// best border treatment for Averaging is BORDER_TREATMENT_CLIP
border_treatment_ = BORDER_TREATMENT_CLIP;
}
/***********************************************************************/
template <class ARITHTYPE>
void
Kernel1D<ARITHTYPE>::initSymmetricDifference(value_type norm)
{
kernel_.erase(kernel_.begin(), kernel_.end());
kernel_.reserve(3);
kernel_.push_back(ARITHTYPE(0.5 * norm));
kernel_.push_back(ARITHTYPE(0.0 * norm));
kernel_.push_back(ARITHTYPE(-0.5 * norm));
left_ = -1;
right_ = 1;
norm_ = norm;
// best border treatment for symmetric difference is
// BORDER_TREATMENT_REFLECT
border_treatment_ = BORDER_TREATMENT_REFLECT;
}
/**************************************************************/
/* */
/* Argument object factories for Kernel1D */
/* */
/* (documentation: see vigra/convolution.hxx) */
/* */
/**************************************************************/
template <class KernelIterator, class KernelAccessor>
inline
tuple5<KernelIterator, KernelAccessor, int, int, BorderTreatmentMode>
kernel1d(KernelIterator ik, KernelAccessor ka,
int kleft, int kright, BorderTreatmentMode border)
{
return
tuple5<KernelIterator, KernelAccessor, int, int, BorderTreatmentMode>(
ik, ka, kleft, kright, border);
}
template <class T>
inline
tuple5<typename Kernel1D<T>::const_iterator, typename Kernel1D<T>::ConstAccessor,
int, int, BorderTreatmentMode>
kernel1d(Kernel1D<T> const & k)
{
return
tuple5<typename Kernel1D<T>::const_iterator, typename Kernel1D<T>::ConstAccessor,
int, int, BorderTreatmentMode>(
k.center(),
k.accessor(),
k.left(), k.right(),
k.borderTreatment());
}
template <class T>
inline
tuple5<typename Kernel1D<T>::const_iterator, typename Kernel1D<T>::ConstAccessor,
int, int, BorderTreatmentMode>
kernel1d(Kernel1D<T> const & k, BorderTreatmentMode border)
{
return
tuple5<typename Kernel1D<T>::const_iterator, typename Kernel1D<T>::ConstAccessor,
int, int, BorderTreatmentMode>(
k.center(),
k.accessor(),
k.left(), k.right(),
border);
}
} // namespace vigra
#endif // VIGRA_SEPARABLECONVOLUTION_HXX
|