-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_usage.py
184 lines (151 loc) · 6.17 KB
/
example_usage.py
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
# %% [markdown]
# # resampler: Example usage
#
# [**[Open in Colab]**](https://colab.research.google.com/github/hhoppe/resampler/blob/main/example_usage.ipynb)
#
# [**[Kaggle]**](https://www.kaggle.com/notebooks/welcome?src=https://github.com/hhoppe/resampler/blob/main/example_usage.ipynb)
#
# [**[MyBinder]**](https://mybinder.org/v2/gh/hhoppe/resampler/main?filepath=example_usage.ipynb)
#
# [**[DeepNote]**](https://deepnote.com/launch?url=https%3A%2F%2Fgithub.com%2Fhhoppe%2Fresampler%2Fblob%2Fmain%2Fexample_usage.ipynb)
#
# [**[GitHub source]**](https://github.com/hhoppe/resampler)
#
# [**[API docs]**](https://hhoppe.github.io/resampler/)
#
# [**[PyPI package]**](https://pypi.org/project/resampler/)
# %%
# !pip install -q mediapy resampler
# %%
"""Simple examples of `resampler` usage."""
import collections
import math
import typing
from typing import Any
import mediapy as media
import numpy as np
import matplotlib.pyplot as plt
import resampler
# %% [markdown]
# ### Upsample (magnify) an image
# %%
array = np.random.default_rng(1).random((4, 6, 3)) # 4x6 RGB image.
upsampled = resampler.resize(array, (128, 192)) # To 128x192 resolution.
media.show_images({'original 4x6': array, 'upsampled 128x192': upsampled}, height=128)
# %% [markdown]
# ### Downsample (minify) an image
# %%
image = media.read_image('https://github.com/hhoppe/data/raw/main/image.png')
downsampled = resampler.resize(image, (32, 32))
media.show_images({'original 128x128': image, 'downsampled 32x32': downsampled}, height=128)
# %%
yx = (np.moveaxis(np.indices((96, 192)), 0, -1) + (0.5, 0.5)) / 96
radius = np.linalg.norm(yx - (0.75, 0.5), axis=-1)
array = np.cos((radius + 0.1) ** 0.5 * 70.0) * 0.5 + 0.5
downsampled = resampler.resize(array, (24, 48))
media.show_images(
{'original 96x192': array, 'downsampled 24x48': downsampled}, height=96, vmin=0, vmax=1
)
# %% [markdown]
# ### Upsample a 1D array
# %%
a = [3.0, 5.0, 8.0, 7.0] # 4 source samples in 1D.
new_dual = resampler.resize(a, (32,)) # (default gridtype='dual') 8x resolution.
new_primal = resampler.resize(a, (25,), gridtype='primal') # 8x resolution.
# %%
_, ax_init = plt.subplots(1, 2, figsize=(7, 1.5))
axs = typing.cast(np.ndarray[Any, Any], ax_init)
axs[0].set(title="gridtype='dual'")
axs[0].plot((np.arange(len(a)) + 0.5) / len(a), a, 'o')
axs[0].plot((np.arange(len(new_dual)) + 0.5) / len(new_dual), new_dual, '.')
axs[1].set(title="gridtype='primal'")
axs[1].plot(np.arange(len(a)) / (len(a) - 1), a, 'o')
axs[1].plot(np.arange(len(new_primal)) / (len(new_primal) - 1), new_primal, '.')
# %% [markdown]
# ### Upsample a video
# %%
batch_size = 4
batch_of_images = media.moving_circle((16, 16), batch_size)
upsampled = resampler.resize(batch_of_images, (batch_size, 64, 64))
media.show_videos({'original': batch_of_images, 'upsampled': upsampled}, fps=1)
# %% [markdown]
# ### Advanced resizing options
# %% [markdown]
# Map an image to a wider grid using custom `scale` and `translate` vectors,
# with horizontal `'reflect'` and vertical `'natural'` boundary rules,
# providing a constant value for the exterior,
# using different filters (Lanczos and O-MOMS) in the two dimensions,
# disabling gamma correction, performing computations in double-precision,
# and returning an output array in single-precision:
# %%
new = resampler.resize(
image,
(128, 512),
boundary=('natural', 'reflect'),
cval=(0.2, 0.7, 0.3),
filter=('lanczos3', 'omoms5'),
gamma='identity',
scale=(0.8, 0.25),
translate=(0.1, 0.35),
precision='float64',
dtype='float32',
)
media.show_images({'original image': image, 'result of resize()': new})
# %% [markdown]
# ### Translate an image
# %%
videos = collections.defaultdict(list)
for filter in ['impulse', 'triangle', 'lanczos3']:
for angle in np.linspace(0.0, math.tau, 20, endpoint=False):
translate = np.array([np.cos(angle), np.sin(angle)]) * (0.6 / image.shape[0])
videos[f"filter='{filter}'"].append(
resampler.resize(image, image.shape[:2], filter=filter, translate=translate)
)
media.show_videos(videos, fps=20, height=image.shape[0] * 2)
# %% [markdown]
# ### Translate with upscaling
# %%
videos = collections.defaultdict(list)
for filter in ['impulse', 'triangle', 'trapezoid', 'lanczos3']:
for angle in np.linspace(0.0, math.tau, 20, endpoint=False):
translate = np.array([np.cos(angle), np.sin(angle)]) * (0.6 / image.shape[0]) + [-0.4, 0.0]
scale = 2.0
videos[f"filter='{filter}'"].append(
resampler.resize(image, image.shape[:2], filter=filter, translate=translate, scale=scale)
)
media.show_videos(videos, fps=20, height=image.shape[0] * 2)
# %% [markdown]
# ### Translate with downscaling
# %%
videos = collections.defaultdict(list)
for filter in ['impulse', 'triangle', 'trapezoid', 'lanczos3']:
for angle in np.linspace(0.0, math.tau, 20, endpoint=False):
translate = np.array([np.cos(angle), np.sin(angle)]) * (0.6 / image.shape[0]) + [0.0, 0.0]
new_shape = np.array(image.shape[:2]) // 2
videos[f"filter='{filter}'"].append(
resampler.resize(image, new_shape, filter=filter, translate=translate)
)
media.show_videos(videos, fps=20, height=image.shape[0] * 2)
# %% [markdown]
# ### Rotate an image
# %%
videos = collections.defaultdict(list)
for filter in ['impulse', 'triangle', 'lanczos3']:
for angle in np.sin(np.linspace(0.0, math.tau, 60, endpoint=False)) * math.radians(6):
videos[f"filter='{filter}'"].append(
resampler.rotate_image_about_center(image, angle, filter=filter, boundary='constant')
)
media.show_videos(videos, fps=20, height=image.shape[0] * 2)
# %% [markdown]
# ### Warp with general resampling
# %% [markdown]
# Warp an image by transforming it using
# [polar coordinates](https://en.wikipedia.org/wiki/Polar_coordinate_system):
# %%
shape = image.shape[:2]
yx = ((np.indices(shape).T + 0.5) / shape - 0.5).T # [-0.5, 0.5]^2
radius, angle = np.linalg.norm(yx, axis=0), np.arctan2(*yx)
angle += (0.8 - radius).clip(0, 1) * 2.0 - 0.6
coords = np.dstack((np.sin(angle) * radius, np.cos(angle) * radius)) + 0.5
resampled = resampler.resample(image, coords, boundary='constant')
media.show_images({'original image': image, 'resampled': resampled}, height=image.shape[0] * 2)