def create_wrap_figures(): ground = snowy.load(qualify('ground.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.export(ground2x2, qualify('ground2x2.jpg')) ground = snowy.blur(ground, radius=14, filter=snowy.LANCZOS) snowy.export(ground, qualify('blurry_ground_bad.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.export(ground2x2, qualify('blurry_ground2x2_bad.jpg')) ground = snowy.load(qualify('ground.jpg')) ground = snowy.blur(ground, radius=14, wrapx=True, wrapy=True, filter=snowy.LANCZOS) snowy.export(ground, qualify('blurry_ground_good.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.export(ground2x2, qualify('blurry_ground2x2_good.jpg')) n = snowy.generate_noise(256, 512, frequency=4, seed=42, wrapx=False) n = 0.5 + 0.5 * np.sign(n) - n n = np.hstack([n, n]) n = snowy.add_border(n, width=4) snowy.export(n, qualify('tiled_noise_bad.png')) n = snowy.generate_noise(256, 512, frequency=4, seed=42, wrapx=True) n = 0.5 + 0.5 * np.sign(n) - n n = np.hstack([n, n]) n = snowy.add_border(n, width=4) snowy.export(n, qualify('tiled_noise_good.png')) c0 = create_circle(400, 200, 0.3) c1 = create_circle(400, 200, 0.08, 0.8, 0.8) circles = np.clip(c0 + c1, 0, 1) mask = circles != 0.0 sdf = snowy.unitize(snowy.generate_sdf(mask, wrapx=True, wrapy=True)) sdf = np.hstack([sdf, sdf, sdf, sdf]) sdf = snowy.resize(np.vstack([sdf, sdf]), width=512) sdf = snowy.add_border(sdf) snowy.export(sdf, qualify('tiled_sdf_good.png')) sdf = snowy.unitize(snowy.generate_sdf(mask, wrapx=False, wrapy=False)) sdf = np.hstack([sdf, sdf, sdf, sdf]) sdf = snowy.resize(np.vstack([sdf, sdf]), width=512) sdf = snowy.add_border(sdf) snowy.export(sdf, qualify('tiled_sdf_bad.png'))
def create_island(seed, gradient, freq=3.5): w, h = 750, 512 falloff = create_falloff(w, h) n1 = 1.000 * snowy.generate_noise(w, h, freq*1, seed+0) n2 = 0.500 * snowy.generate_noise(w, h, freq*2, seed+1) n3 = 0.250 * snowy.generate_noise(w, h, freq*4, seed+2) n4 = 0.125 * snowy.generate_noise(w, h, freq*8, seed+3) elevation = falloff * (falloff / 2 + n1 + n2 + n3 + n4) mask = elevation < 0.4 elevation = snowy.unitize(snowy.generate_sdf(mask)) if GRAY_ISLAND: return (1 - mask) * np.power(elevation, 3.0) elevation = snowy.generate_sdf(mask) - 100 * n4 mask = np.where(elevation < 0, 1, 0) el = 128 + 127 * elevation / np.amax(elevation) return applyColorGradient(el, gradient)
def Example_transfer(path, nbr_classes, idx): #print(path) # print(Image.open(path).copy()) img = np.asarray(Image.open(path).resize((128, 128))).copy() if (255 in img): img = img.copy() img.setflags(write=1) img[img == 255] = 0 value = 1000 #distance_helper=np.full((img.shape[0],img.shape[1]),value) distance_label = np.full((nbr_classes, img.shape[0], img.shape[1]), value) channel_index = np.unique(img) label = img != 0 edges = np.expand_dims(label, axis=-1) sdf = snowy.generate_sdf(edges) #snowy.show(snowy.unitize(sdf)) before = sdf[:, :, 0] cv2.imwrite( './' + str(idx) + '_before.png', 255 * (before - np.amin(before)) / (np.amax(before) - np.amin(before))) for index in channel_index: distance_label[index] = np.where(img == index, sdf[:, :, 0], value) test = np.full((img.shape[0], img.shape[1]), value) for index in channel_index: test = np.where(distance_label[index] != value, distance_label[index], test) #test= cv2.imwrite('./' + str(idx) + '_after.png', 255 * (test - np.amin(test)) / (np.amax(test) - np.amin(test)))
def test_draw_quad(): w, h = 100, 100 def show(im): snowy.show(snowy.resize(im, height=100, filter=None)) yellow = np.full((w, h, 4), (1, 1, 0, 1)) red = np.full((w, h, 4), (1, 0, 0, 1)) trans_border = np.full((w, h, 4), (0, 0, 1, 0.2)) t = 5 trans_border[t:h - t, t:w - t] *= 0 c0 = create_circle(w, h, 0.3) * yellow * 100000 c1 = create_circle(w, h, 0.07, 0.8, 0.8) * red * 10000 circles = np.clip(c0 + c1 + trans_border, 0, 1) r, g, b, a = circles.swapaxes(0, 2) luma = snowy.reshape(r + g + b) mask = luma != 0.0 sdf = snowy.unitize(np.abs(snowy.generate_sdf(mask))) cpcf = snowy.generate_cpcf(mask) voronoi = snowy.dereference_coords(circles, cpcf) show(voronoi) target = np.full((2000, 4000, 4), (0, 0, 0, 1), dtype=np.float32) seconds = timeit.timeit(lambda: snowy.draw_polygon( target, voronoi, np.array([(-1., -1, 1., 0., 1.), (-.5, +1, 1., 0., 0.), (+.5, +1, 1., 1., 0.), (+1., -1, 1., 1., 1.)])), number=1) show(target) print(seconds)
def test_cpcf(): w, h = 500, 500 def show(im): snowy.show(snowy.resize(im, height=100, filter=None)) yellow = np.full((w, h, 3), (1, 1, 0)) red = np.full((w, h, 3), (1, 0, 0)) blue_border = np.full((w, h, 3), (0, 0, 1)) t = 5 blue_border[t:h - t, t:w - t] *= 0 c0 = create_circle(w, h, 0.3) * yellow * 100000 c1 = create_circle(w, h, 0.07, 0.8, 0.8) * red * 10000 circles = np.clip(c0 + c1 + blue_border, 0, 1) r, g, b = circles.swapaxes(0, 2) luma = snowy.reshape(r + g + b) mask = luma != 0.0 sdf = snowy.unitize(np.abs(snowy.generate_sdf(mask))) cpcf = snowy.generate_cpcf(mask) voronoi = np.empty(circles.shape) np.copyto(voronoi, snowy.dereference_coords(circles, cpcf)) luma = np.dstack([luma, luma, luma]) sdf = np.dstack([sdf, sdf, sdf]) final = np.hstack([circles, luma, sdf, voronoi]) final = snowy.resize(final, height=400) show(final)
def test_sdf(): c0 = create_circle(200, 200, 0.3) c1 = create_circle(200, 200, 0.08, 0.8, 0.8) c0 = np.clip(c0 + c1, 0, 1) circles = snowy.add_border(c0, value=1) mask = circles != 0.0 sdf = snowy.unitize(snowy.generate_sdf(mask)) nx, ny = snowy.gradient(sdf) grad = snowy.unitize(nx + ny) snowy.show(snowy.hstack([circles, sdf, grad]))
def test_tweet(): import snowy as sn, numpy as np im = sn.generate_noise(2000, 500, 5, seed=2, wrapx=True) df = sn.generate_sdf(im < 0.0, wrapx=True) im = 0.5 + 0.5 * np.sign(im) - im cl = lambda L, U: np.where(np.logical_and(df > L, df < U), -im, 0) im += cl(20, 30) + cl(60, 70) + cl(100, 110) sn.show(sn.resize(im, height=100, wrapx=True)) sn.show(sn.resize(np.hstack([im, im]), height=200, wrapx=True))
def create_island(seed, freq=3.5): w, h = 750, 512 falloff = create_falloff(w, h) n1 = 1.000 * sn.generate_noise(w, h, freq * 1, seed + 0) n2 = 0.500 * sn.generate_noise(w, h, freq * 2, seed + 1) n3 = 0.250 * sn.generate_noise(w, h, freq * 4, seed + 2) n4 = 0.125 * sn.generate_noise(w, h, freq * 8, seed + 3) elevation = falloff * (falloff / 2 + n1 + n2 + n3 + n4) elevation = sn.generate_sdf(elevation < 0.4) elmax = max(abs(np.amin(elevation)), abs(np.amax(elevation))) return elevation / elmax
def Distance_transfer(path, nbr_classes, size): #print(path) img = np.asarray(Image.open(path).resize((size, size))).copy() if (255 in img): img = img.copy() img.setflags(write=1) img[img == 255] = 0 value = 1000 distance_label = np.full((nbr_classes, img.shape[0], img.shape[1]), value) channel_index = np.unique(img) label = img != 0 edges = np.expand_dims(label, axis=-1) sdf = snowy.generate_sdf(edges) before = sdf[:, :, 0] for index in channel_index: distance_label[index] = np.where(img == index, before, value) distance_label = np.transpose(distance_label, [1, 2, 0]) return distance_label
def test_tileable_distance(): c0 = create_circle(400, 200, 0.3) c1 = create_circle(400, 200, 0.08, 0.8, 0.8) circles = np.clip(c0 + c1, 0, 1) mask = circles != 0.0 sdf = snowy.unitize(snowy.generate_sdf(mask, wrapx=True, wrapy=True)) nx, ny = snowy.gradient(sdf) grad = snowy.unitize(nx + ny) stack2 = np.hstack([sdf, sdf, grad, grad]) snowy.show(snowy.resize(np.vstack([stack2, stack2]), 600, 200)) get_mask = lambda L, U: np.logical_and(sdf > L, sdf < U) get_contour = lambda L, U: np.where(get_mask(L, U), sdf, 0) sdf -= get_contour(.20, .25) sdf -= get_contour(.60, .65) sdf -= get_contour(.90, .95) snowy.show(snowy.resize(np.hstack([sdf, sdf, sdf, sdf]), height=300))
def Distance_extraction(self,prediction, target): #print(target.shape) #target = target[:,:,:,0].float().cuda() prediction_stack = [] prediction = F.softmax(prediction, dim=1) #print(prediction.shape) for idx,image in enumerate(prediction): tensor=image.argmax(0).cpu().numpy() tensor = np.uint8(tensor) edges = cv2.Canny(tensor, 0.5, 1) edges = edges != 0 edges = np.expand_dims(edges, axis=-1) sdf = snowy.unitize(snowy.generate_sdf(edges))[:,:,0] prediction_stack.append(torch.tensor(sdf,requires_grad=True).float().cuda()) prediction_stack =torch.stack(prediction_stack, dim=0) #print(target.shape) #print(prediction_stack.shape) loss = self.distance_loss(prediction_stack, target) #print(loss) return loss
apply_lut = interpolate.interp1d(xvals, yvals, axis=0) return apply_lut(snowy.unshape(np.clip(elevation_image, 0, 255))) def create_falloff(w, h, radius=0.4, cx=0.5, cy=0.5): hw, hh = 0.5 / w, 0.5 / h x = np.linspace(hw, 1 - hw, w) y = np.linspace(hh, 1 - hh, h) u, v = np.meshgrid(x, y, sparse=True) d2 = (u-cx)**2 + (v-cy)**2 return 1-snowy.unitize(snowy.reshape(d2)) c0 = create_circle(200, 200, 0.3) c1 = create_circle(200, 200, 0.08, 0.8, 0.8) c0 = np.clip(c0 + c1, 0, 1) circles = snowy.add_border(c0, value=1) sdf = snowy.unitize(snowy.generate_sdf(circles != 0.0)) stack = snowy.hstack([circles, sdf]) snowy.export(stack, qualify('sdf.png')) snowy.show(stack) # Islands def create_island(seed, gradient, freq=3.5): w, h = 750, 512 falloff = create_falloff(w, h) n1 = 1.000 * snowy.generate_noise(w, h, freq*1, seed+0) n2 = 0.500 * snowy.generate_noise(w, h, freq*2, seed+1) n3 = 0.250 * snowy.generate_noise(w, h, freq*4, seed+2) n4 = 0.125 * snowy.generate_noise(w, h, freq*8, seed+3) elevation = falloff * (falloff / 2 + n1 + n2 + n3 + n4) mask = elevation < 0.4 elevation = snowy.unitize(snowy.generate_sdf(mask))
print("Applying a warping operation to create the landmass mask.") i, j = np.arange(width, dtype="i2"), np.arange(height, dtype="i2") coords = np.dstack(np.meshgrid(i, j, sparse=False)) warpx = warpx * np.cos(noise * math.pi * 2) warpy = warpy * np.sin(noise * math.pi * 2) coords += np.int16(np.dstack([warpx, warpy])) warped = snowy.dereference_coords(island_noise, coords) mask = warped < 0.1 print("Computing the distance field.") elevation = snowy.generate_sdf(mask) elevation /= np.amax(elevation) print("Computing ambient occlusion.") occlusion = snowy.compute_skylight(elevation) occlusion = 0.25 + 0.75 * occlusion print("Generating normal map.") normals = snowy.resize(snowy.compute_normals(elevation), width, height) # Save the landmass portion of the elevation data. landmass = elevation * np.where(elevation < 0.0, 0.0, 1.0) snowy.save(trim(landmass), "landmass.png") # Flatten the normals according to landmass versus sea. normals += np.float64([0, 0, 1000]) * np.where(elevation < 0.0, 1.0, 0.01)
def get_dfm_image(sketch): dfm_image = snowy.unitize( snowy.generate_sdf(np.expand_dims(1 - sketch, 2) != 0)).squeeze() return dfm_image