Skip to content

Instantly share code, notes, and snippets.

@patakk
Created December 3, 2019 17:37
Show Gist options
  • Save patakk/40a816673ce8f91afb2a361b3a626b4f to your computer and use it in GitHub Desktop.
Save patakk/40a816673ce8f91afb2a361b3a626b4f to your computer and use it in GitHub Desktop.
import numpy as np
import noise
import cv2
WIDTH = 600
HEIGHT = 600
FREQUENCY = 0.001
def generate_noise_map(
shape = (WIDTH, HEIGHT),
frequency = FREQUENCY,
):
octaves = 6
persistence = 0.5
lacunarity = 2.0
seed = np.random.randint(0, 1000000)
world = np.zeros(shape)
for x in range(shape[0]):
for y in range(shape[1]):
world[y][x] = noise.pnoise3(x*frequency,
y*frequency,
seed,
octaves=octaves,
persistence=persistence,
lacunarity=lacunarity,
repeatx=WIDTH,
repeaty=HEIGHT,
base=0)
world = (world - world.min())/(world.max()-world.min())
return world
def get_dummy_annotations():
N = 30
a = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)
annotations = []
for j in range(N):
for i in range(N):
x = int((i+0.5)/N * WIDTH)
y = int((j+0.5)/N * HEIGHT)
annotations.append([x, y])
return annotations
def deform(annotations):
noise_map_x = generate_noise_map()
noise_map_y = generate_noise_map()
noise_strength = 40
for anno in annotations:
x = anno[0]
y = anno[1]
xd = noise_strength * (noise_map_x[y, x]*2 - 1)
yd = noise_strength * (noise_map_y[y, x]*2 - 1)
anno[0] = int(x + xd)
anno[1] = int(y + yd)
cv2.imwrite('noise_map_x.png', (255*noise_map_x).astype(np.uint8))
cv2.imwrite('noise_map_y.png', (255*noise_map_y).astype(np.uint8))
return annotations
def render(annotations):
img = np.zeros((HEIGHT, WIDTH, 3))
for anno in annotations:
x = anno[0]
y = anno[1]
cv2.rectangle(img, (x, y), (x+2, y+2), (0, 255, 0), 1)
return img
def demo():
annotations = get_dummy_annotations()
img = render(annotations)
cv2.imwrite('annotations_original.png', img)
annotations = deform(annotations)
img = render(annotations)
cv2.imwrite('annotations_deformed.png', img)
if __name__ == '__main__':
demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment