Changeset 2190 for www/study/study.py
- Timestamp:
- 01/12/08 13:53:10 (5 years ago)
- File:
-
- 1 edited
-
www/study/study.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
www/study/study.py
r2187 r2190 1752 1752 ERROR_SUBFS33[y][x][y][1 + x] = -1 1753 1753 1754 # Output 3.7.1: direct binary search, iteration 0 1755 # Output 3.7.2: direct binary search, iteration 1 1756 # Output 3.7.3: direct binary search, iteration 2 1757 # Output 3.7.4: direct binary search, iteration 5 1758 def test37x(src): 1759 random.seed(0) 1760 (w, h) = src.size() 1761 dest = Image((w, h)) 1762 for y in range(h): 1763 for x in range(w): 1764 c = src.getGray(x, y) + random.random() - 0.5 1765 d = c > 0.5 1766 dest.setGray(x, y, d) 1767 return dest 1768 1769 def test37y(src, dest): 1770 threshold = 0.4 1771 kernel = Matrix(6, 6, 0.) # have a border of zeroes 1772 for j in range(4): 1773 for i in range(4): 1774 kernel[j][i] = math.pow(math.e, - math.sqrt(i * i + j * j)) 1775 (w, h) = src.size() 1776 # Build fast pixel lookup tables 1777 srcmat = Matrix(w, h, 0.) 1778 destmat = Matrix(w, h, 0.) 1779 for y in range(h): 1780 for x in range(w): 1781 srcmat[y][x] = src.getGray(x, y) 1782 destmat[y][x] = dest.getGray(x, y) 1783 # Build human perception model for both source and destination 1784 srchvs = Matrix(w, h, 0.) 1785 desthvs = Matrix(w, h, 0.) 1786 for y in range(h): 1787 for x in range(w): 1788 srcp = destp = 0. 1789 for j in range(-3, 4): 1790 if y + j < 0 or y + j >= h: 1791 continue 1792 for i in range(-3, 4): 1793 if x + i < 0 or x + i >= w: 1794 continue 1795 m = kernel[abs(j)][abs(i)] 1796 srcp += m * srcmat[y + j][x + i] 1797 destp += m * destmat[y + j][x + i] 1798 srchvs[y][x] = srcp 1799 desthvs[y][x] = destp 1800 swaps = toggles = 0 1801 for y in range(h): 1802 for x in range(w): 1803 d = destmat[y][x] 1804 best = 0. 1805 # Compute the effect of a toggle 1806 e = 0. 1807 for j in range(-3, 4): 1808 if y + j < 0 or y + j >= h: 1809 continue 1810 for i in range(-3, 4): 1811 if x + i < 0 or x + i >= w: 1812 continue 1813 m = kernel[abs(j)][abs(i)] 1814 p = srchvs[y + j][x + i] 1815 q1 = desthvs[y + j][x + i] 1816 q2 = q1 - m * d + m * (1. - d) 1817 e += abs(q1 - p) - abs(q2 - p) 1818 if e > best: 1819 best = e 1820 op = False 1821 # Compute the effect of swaps 1822 for dx, dy in [(0, 1), (0, -1), (-1, 0), (1, 0)]: 1823 if y + dy < 0 or y + dy >= h or x + dx < 0 or x + dx >= w: 1824 continue 1825 d2 = destmat[y + dy][x + dx] 1826 if d2 == d: 1827 continue 1828 e = 0. 1829 for j in range(-4, 5): 1830 for i in range(-4, 5): 1831 if y + j < 0 or y + j >= h or x + i < 0 or x + i >= w: 1832 continue 1833 ma = kernel[abs(j)][abs(i)] 1834 mb = kernel[abs(j - dy)][abs(i - dx)] 1835 p = srchvs[y + j][x + i] 1836 q1 = desthvs[y + j][x + i] 1837 q2 = q1 - ma * d + ma * d2 - mb * d2 + mb * d 1838 e += abs(q1 - p) - abs(q2 - p) 1839 if e > best: 1840 best = e 1841 op = (dx, dy) 1842 # Apply the change if interesting 1843 if best <= 0.: 1844 continue 1845 if op: 1846 dx, dy = op 1847 d2 = destmat[y + dy][x + dx] 1848 destmat[y + dy][x + dx] = d 1849 else: 1850 d2 = 1. - d 1851 destmat[y][x] = d2 1852 for j in range(-3, 4): 1853 for i in range(-3, 4): 1854 m = kernel[abs(j)][abs(i)] 1855 if y + j >= 0 and y + j < h and x + i >= 0 and x + i < w: 1856 desthvs[y + j][x + i] -= m * d 1857 desthvs[y + j][x + i] += m * d2 1858 if op and y + dy + j >= 0 and y + dy + j < h \ 1859 and x + dx + i >= 0 and x + dx + i < w: 1860 desthvs[y + dy + j][x + dx + i] -= m * d2 1861 desthvs[y + dy + j][x + dx + i] += m * d 1862 for y in range(h): 1863 for x in range(w): 1864 dest.setGray(x, y, destmat[y][x]) 1865 return dest 1866 1867 if chapter(3): 1868 tmp = test37x(grad256bw) 1869 tmp.save("grad3-7-1.png") 1870 tmp = test37y(grad256bw, tmp) 1871 tmp.save("grad3-7-2.png") 1872 tmp = test37y(grad256bw, tmp) 1873 tmp.save("grad3-7-3.png") 1874 tmp = test37y(grad256bw, tmp) 1875 tmp = test37y(grad256bw, tmp) 1876 tmp = test37y(grad256bw, tmp) 1877 tmp.save("grad3-7-4.png") 1878 1879 if chapter(3): 1880 tmp = test37x(lenna256bw) 1881 tmp.save("out3-7-1.png") 1882 tmp = test37y(lenna256bw, tmp) 1883 tmp.save("out3-7-2.png") 1884 tmp = test37y(lenna256bw, tmp) 1885 tmp.save("out3-7-3.png") 1886 tmp = test37y(lenna256bw, tmp) 1887 tmp = test37y(lenna256bw, tmp) 1888 tmp = test37y(lenna256bw, tmp) 1889 tmp.save("out3-7-4.png") 1890 1754 1891 ############################################################################## 1755 1892 if chapter(4):
Note: See TracChangeset
for help on using the changeset viewer.
