Last active
January 16, 2024 06:36
-
-
Save dglaude/fcc99fd1146bb1a6783c3c06f1b51313 to your computer and use it in GitHub Desktop.
ulab 2x, 3x image upscaling in CircuitPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Double size upscaling of an array by @v923z | |
import ulab | |
n = 4 # input size, the thermal sensor is 8x8 | |
b = ulab.array(range(n * n)).reshape((n, n)) # dummy data for testing | |
a = ulab.zeros((2*n-1, 2*n-1)) # the upscaled image | |
# Some magic by @v923z | |
a[::2,::2] = b | |
a[1::2,::2] = 0.5 * (b[:-1,:] + b[1:, :]) | |
if False: # Solution by @v923z potentially more memory effective as it always take slice of b as source | |
a[::2,1::2] = 0.5 * (b[:,1:] + b[:,:-1]) | |
a[1::2,1::2] = 0.25 * (b[:-1,1:] + b[1:,:-1] + b[1:,1:] + b[:-1,:-1]) | |
else: # Alternative by @David.Glaude that is using two view of a to compute piece of a | |
a[::,1::2]=0.5 * (a[::,:-1:2] + a[::,2::2]) | |
print(b) | |
#array([[0.0, 1.0, 2.0, 3.0], | |
# [4.0, 5.0, 6.0, 7.0], | |
# [8.0, 9.0, 10.0, 11.0], | |
# [12.0, 13.0, 14.0, 15.0]], dtype=float32) | |
print(a) | |
#array([[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0], | |
# [2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0], | |
# [4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0], | |
# [6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0], | |
# [8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0], | |
# [10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0], | |
# [12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0]], dtype=float32) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Triple size upscaling of an array based on magic by @v923z | |
import ulab | |
n = 2 # input size, the thermal sensor is 8x8 | |
b = ulab.array(range(0, n * n * 3, 3)).reshape((n, n)) # dummy data for testing | |
a = ulab.zeros((3*n-2, 3*n-2)) # the upscaled image | |
a[::3,::3] = b | |
a[1::3,::3] = b[:-1,:] * (2/3) + b[1:, :] /3 | |
a[2::3,::3] = b[:-1,:] / 3 + b[1:, :] * (2/3) | |
a[::,1::3] = a[::,:-1:3] * (2/3) + a[::,3::3] * (1/3) | |
a[::,2::3] = a[::,:-1:3] * (1/3) + a[::,3::3] * (2/3) | |
print(b) | |
#array([[0.0, 3.0], | |
# [6.0, 9.0]], dtype=float32) | |
print(a) | |
#array([[0.0, 1.0, 2.0, 3.0], | |
# [2.0, 3.0, 4.0, 5.0], | |
# [4.0, 5.0, 6.0, 7.0], | |
# [6.0, 7.0, 8.0, 9.0]], dtype=float32) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Quadruple size upscaling of an array based on magic by @v923z | |
import ulab | |
n = 2 # input size, the thermal sensor is 8x8 | |
b = ulab.array(range(0, n * n * 4, 4)).reshape((n, n)) # dummy data for testing | |
a = ulab.zeros((4*n-3, 4*n-3)) # the upscaled image | |
a[::4,::4] = b | |
a[1::4,::4] = b[:-1,:] * (3/4) + b[1:, :] * (1/4) | |
a[2::4,::4] = b[:-1,:] * (2/4) + b[1:, :] * (2/4) | |
a[3::4,::4] = b[:-1,:] * (1/4) + b[1:, :] * (3/4) | |
a[::,1::4] = a[::,:-1:4] * (3/4) + a[::,4::4] * (1/4) | |
a[::,2::4] = a[::,:-1:4] * (2/4) + a[::,4::4] * (2/4) | |
a[::,3::4] = a[::,:-1:4] * (1/4) + a[::,4::4] * (3/4) | |
print(b) | |
#array([[0.0, 4.0], | |
# [8.0, 12.0]], dtype=float32) | |
print(a) | |
#array([[0.0, 1.0, 2.0, 3.0, 4.0], | |
# [2.0, 3.0, 4.0, 5.0, 6.0], | |
# [4.0, 5.0, 6.0, 7.0, 8.0], | |
# [6.0, 7.0, 8.0, 9.0, 10.0], | |
# [8.0, 9.0, 10.0, 11.0, 12.0]], dtype=float32) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment