|
| 1 | +import numpy as np |
| 2 | +from PIL import Image |
| 3 | + |
| 4 | +def make_sphere_depthmap(xlim, ylim, *, rad=1., size=200): |
| 5 | + return np.array([ |
| 6 | + [ |
| 7 | + (rad**2 - x**2 - y**2)**(1./2) if x**2 + y**2 < rad**2 else 0 |
| 8 | + for x in np.linspace(xlim[0], xlim[1], size) |
| 9 | + ] |
| 10 | + for y in np.linspace(ylim[0], ylim[1], size) |
| 11 | + ]) |
| 12 | + |
| 13 | +def make_random_dot_stereogram(depthmap, pattern_size, *, shift_coef=0.3): |
| 14 | + repeated = np.random.randint(0, 256, size=pattern_size).astype(np.uint8) |
| 15 | + rds = np.zeros(depthmap.shape, dtype=np.uint8) |
| 16 | + for x in range(rds.shape[1]): |
| 17 | + for y in range(rds.shape[0]): |
| 18 | + if x < repeated.shape[1]: |
| 19 | + rds[y, x] = repeated[y % repeated.shape[0], x] |
| 20 | + else: |
| 21 | + shift = int(depthmap[y, x] * shift_coef * repeated.shape[1]) |
| 22 | + rds[y, x] = rds[y, x - repeated.shape[1] + shift] |
| 23 | + return rds |
| 24 | + |
| 25 | +if __name__ == '__main__': |
| 26 | + depthmap = make_sphere_depthmap((-2, 2), (-1.5, 1.5)) |
| 27 | + Image.fromarray((depthmap*256).astype(np.uint8)).save('depthmap.png') |
| 28 | + Image.fromarray(make_random_dot_stereogram(depthmap, (50, 50))).save('rds.png') |
| 29 | + |
0 commit comments