mirror of
https://github.com/lkwq007/stablediffusion-infinity.git
synced 2025-01-08 11:57:27 +08:00
Move code for perlin noise to a separate file
This commit is contained in:
parent
f706623219
commit
738433fbdf
45
perlin2d.py
Normal file
45
perlin2d.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
##########
|
||||||
|
# https://stackoverflow.com/questions/42147776/producing-2d-perlin-noise-with-numpy/42154921#42154921
|
||||||
|
def perlin(x, y, seed=0):
|
||||||
|
# permutation table
|
||||||
|
np.random.seed(seed)
|
||||||
|
p = np.arange(256, dtype=int)
|
||||||
|
np.random.shuffle(p)
|
||||||
|
p = np.stack([p, p]).flatten()
|
||||||
|
# coordinates of the top-left
|
||||||
|
xi, yi = x.astype(int), y.astype(int)
|
||||||
|
# internal coordinates
|
||||||
|
xf, yf = x - xi, y - yi
|
||||||
|
# fade factors
|
||||||
|
u, v = fade(xf), fade(yf)
|
||||||
|
# noise components
|
||||||
|
n00 = gradient(p[p[xi] + yi], xf, yf)
|
||||||
|
n01 = gradient(p[p[xi] + yi + 1], xf, yf - 1)
|
||||||
|
n11 = gradient(p[p[xi + 1] + yi + 1], xf - 1, yf - 1)
|
||||||
|
n10 = gradient(p[p[xi + 1] + yi], xf - 1, yf)
|
||||||
|
# combine noises
|
||||||
|
x1 = lerp(n00, n10, u)
|
||||||
|
x2 = lerp(n01, n11, u) # FIX1: I was using n10 instead of n01
|
||||||
|
return lerp(x1, x2, v) # FIX2: I also had to reverse x1 and x2 here
|
||||||
|
|
||||||
|
|
||||||
|
def lerp(a, b, x):
|
||||||
|
"linear interpolation"
|
||||||
|
return a + x * (b - a)
|
||||||
|
|
||||||
|
|
||||||
|
def fade(t):
|
||||||
|
"6t^5 - 15t^4 + 10t^3"
|
||||||
|
return 6 * t ** 5 - 15 * t ** 4 + 10 * t ** 3
|
||||||
|
|
||||||
|
|
||||||
|
def gradient(h, x, y):
|
||||||
|
"grad converts h to the right gradient vector and return the dot product with (x,y)"
|
||||||
|
vectors = np.array([[0, 1], [0, -1], [1, 0], [-1, 0]])
|
||||||
|
g = vectors[h % 4]
|
||||||
|
return g[:, :, 0] * x + g[:, :, 1] * y
|
||||||
|
|
||||||
|
|
||||||
|
##########
|
43
utils.py
43
utils.py
@ -7,6 +7,7 @@ import scipy.signal
|
|||||||
from scipy.spatial import cKDTree
|
from scipy.spatial import cKDTree
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from perlin2d import *
|
||||||
|
|
||||||
patch_match_compiled = True
|
patch_match_compiled = True
|
||||||
if os.name != "nt":
|
if os.name != "nt":
|
||||||
@ -24,50 +25,8 @@ except NameError:
|
|||||||
print("patch_match compiling failed")
|
print("patch_match compiling failed")
|
||||||
patch_match_compiled = False
|
patch_match_compiled = False
|
||||||
|
|
||||||
##########
|
|
||||||
# https://stackoverflow.com/questions/42147776/producing-2d-perlin-noise-with-numpy
|
|
||||||
def perlin(x, y, seed=0):
|
|
||||||
# permutation table
|
|
||||||
np.random.seed(seed)
|
|
||||||
p = np.arange(256, dtype=int)
|
|
||||||
np.random.shuffle(p)
|
|
||||||
p = np.stack([p, p]).flatten()
|
|
||||||
# coordinates of the top-left
|
|
||||||
xi, yi = x.astype(int), y.astype(int)
|
|
||||||
# internal coordinates
|
|
||||||
xf, yf = x - xi, y - yi
|
|
||||||
# fade factors
|
|
||||||
u, v = fade(xf), fade(yf)
|
|
||||||
# noise components
|
|
||||||
n00 = gradient(p[p[xi] + yi], xf, yf)
|
|
||||||
n01 = gradient(p[p[xi] + yi + 1], xf, yf - 1)
|
|
||||||
n11 = gradient(p[p[xi + 1] + yi + 1], xf - 1, yf - 1)
|
|
||||||
n10 = gradient(p[p[xi + 1] + yi], xf - 1, yf)
|
|
||||||
# combine noises
|
|
||||||
x1 = lerp(n00, n10, u)
|
|
||||||
x2 = lerp(n01, n11, u) # FIX1: I was using n10 instead of n01
|
|
||||||
return lerp(x1, x2, v) # FIX2: I also had to reverse x1 and x2 here
|
|
||||||
|
|
||||||
|
|
||||||
def lerp(a, b, x):
|
|
||||||
"linear interpolation"
|
|
||||||
return a + x * (b - a)
|
|
||||||
|
|
||||||
|
|
||||||
def fade(t):
|
|
||||||
"6t^5 - 15t^4 + 10t^3"
|
|
||||||
return 6 * t ** 5 - 15 * t ** 4 + 10 * t ** 3
|
|
||||||
|
|
||||||
|
|
||||||
def gradient(h, x, y):
|
|
||||||
"grad converts h to the right gradient vector and return the dot product with (x,y)"
|
|
||||||
vectors = np.array([[0, 1], [0, -1], [1, 0], [-1, 0]])
|
|
||||||
g = vectors[h % 4]
|
|
||||||
return g[:, :, 0] * x + g[:, :, 1] * y
|
|
||||||
|
|
||||||
|
|
||||||
##########
|
|
||||||
|
|
||||||
|
|
||||||
def edge_pad(img, mask, mode=1):
|
def edge_pad(img, mask, mode=1):
|
||||||
if mode == 0:
|
if mode == 0:
|
||||||
|
Loading…
Reference in New Issue
Block a user