Skip to content

Instantly share code, notes, and snippets.

@PetteriAimonen
Last active December 31, 2020 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PetteriAimonen/28bc22bc5caacb4fe31489f93f71e9d8 to your computer and use it in GitHub Desktop.
Save PetteriAimonen/28bc22bc5caacb4fe31489f93f71e9d8 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# Interpolate depth maps
from PIL import Image
import math
import random
contours = Image.open("contours.png")
contourspx = contours.load()
w, h = contours.size
def nearest_contour(x0, y0, angle):
dx = math.cos(angle)
dy = math.sin(angle)
for i in range(300):
x = x0 + dx * i
y = y0 + dy * i
if x < 0 or y < 0 or x >= w or y >= h: return None, None
px = contourspx[x, y]
if px != 255:
return px, i
return None, None
result = contours.copy()
resultpx = result.load()
for y in range(0, h):
print("%d/%d" % (y, h))
if y % 10 == 0: result.save("result.png")
for x in range(0, w):
if contourspx[x, y] == 255:
sum_vals = 0.0
sum_weights = 0.0
for angle in range(0, 360, 5):
rad = angle * math.pi / 180
c, d = nearest_contour(x, y, rad)
if c is not None:
weight = 1.0 / d**0.9
sum_vals += c * weight
sum_weights += weight
if sum_weights == 0:
v = (resultpx[x - 1, y] + resultpx[x, y - 1]) / 2
else:
v = sum_vals / sum_weights
resultpx[x, y] = int(round(v + random.normalvariate(0.0, 1.0)))
result.save("result.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment