|
2 | 2 | import os |
3 | 3 |
|
4 | 4 | import tike.zernike |
| 5 | +import tike.linalg |
5 | 6 | import tike.view |
6 | 7 | import matplotlib.pyplot as plt |
7 | 8 | import numpy as np |
@@ -50,3 +51,47 @@ def test_radial_1(self): |
50 | 51 |
|
51 | 52 | def test_radial_2(self): |
52 | 53 | self._radial_template(2) |
| 54 | + |
| 55 | + def test_transform(self): |
| 56 | + |
| 57 | + fname = os.path.join(testdir, 'result', 'zernike') |
| 58 | + os.makedirs(fname, exist_ok=True) |
| 59 | + |
| 60 | + # import libimage |
| 61 | + |
| 62 | + # f0 = libimage.load('cryptomeria', 256) |
| 63 | + f0 = plt.imread(os.path.join(testdir, 'data', 'probe.png')) |
| 64 | + size = f0.shape[-1] |
| 65 | + plt.imsave(os.path.join(fname, 'basis-0.png'), f0) |
| 66 | + |
| 67 | + f0 = f0.reshape(size * size, 1) |
| 68 | + |
| 69 | + _basis = [] |
| 70 | + |
| 71 | + for d in range(0, 100): |
| 72 | + _basis.append( |
| 73 | + tike.zernike.basis( |
| 74 | + size, |
| 75 | + degree_min=d, |
| 76 | + degree_max=d + 1, |
| 77 | + )) |
| 78 | + |
| 79 | + basis = np.concatenate(_basis, axis=0) |
| 80 | + print(f'degree {d} - {len(basis)}') |
| 81 | + basis = np.moveaxis(basis, 0, -1) |
| 82 | + basis = basis.reshape(size * size, -1) |
| 83 | + # weight only pixels inside basis |
| 84 | + w = (basis[..., 0] > 0).astype('float32') |
| 85 | + |
| 86 | + # x = tike.linalg.lstsq(basis, f0, weights=w) |
| 87 | + x, _, _, _ = np.linalg.lstsq(basis, f0, rcond=1e-4) |
| 88 | + |
| 89 | + f1 = basis @ x |
| 90 | + f1 = f1.reshape(size, size) |
| 91 | + plt.imsave(os.path.join(fname, f'basis-{d:02d}.png'), f1) |
| 92 | + |
| 93 | + plt.figure() |
| 94 | + plt.title(f"basis weights for {d} degree polynomials") |
| 95 | + plt.bar(list(range(x.size)), x.flatten()) |
| 96 | + plt.savefig(os.path.join(fname, f'basis-w-{d:02d}.png')) |
| 97 | + plt.close() |
0 commit comments