【问题标题】:Perlin noise in Python's noise libraryPython 噪声库中的 Perlin 噪声
【发布时间】:2020-06-06 14:07:38
【问题描述】:

我在为我的项目生成 Perlin 噪音时遇到问题。由于我想了解如何正确使用库,因此我尝试逐步遵循此页面:https://medium.com/@yvanscher/playing-with-perlin-noise-generating-realistic-archipelagos-b59f004d8401 第一部分有代码:

import noise
import numpy as np
from scipy.misc import toimage

shape = (1024,1024)
scale = 100.0
octaves = 6
persistence = 0.5
lacunarity = 2.0

world = np.zeros(shape)
for i in range(shape[0]):
    for j in range(shape[1]):
        world[i][j] = noise.pnoise2(i/scale, 
                                    j/scale, 
                                    octaves=octaves, 
                                    persistence=persistence, 
                                    lacunarity=lacunarity, 
                                    repeatx=1024, 
                                    repeaty=1024, 
                                    base=0)

toimage(world).show()

我在最后复制并粘贴了一些小改动(toimage 已过时),所以我有:

import noise
import numpy as np
from PIL import Image

shape = (1024,1024)
scale = 100
octaves = 6
persistence = 0.5
lacunarity = 2.0
seed = np.random.randint(0,100)

world = np.zeros(shape)
for i in range(shape[0]):
    for j in range(shape[1]):
        world[i][j] = noise.pnoise2(i/scale,
                                    j/scale,
                                    octaves=octaves,
                                    persistence=persistence,
                                    lacunarity=lacunarity,
                                    repeatx=1024,
                                    repeaty=1024,
                                    base=seed)

Image.fromarray(world, mode='L').show()

我尝试了很多不同的模式,但这种噪声甚至不接近相干噪声。我的结果类似于this (mode='L')。有人可以解释一下,我做错了什么吗?

【问题讨论】:

    标签: python noise perlin-noise


    【解决方案1】:

    这是工作代码。我冒昧地清理了一下。有关详细信息,请参阅 cmets。作为最后的建议:在测试代码时,使用 matplotlib 进行可视化。它的imshow() 功能比PIL 更强大。

    import noise
    import numpy as np
    from PIL import Image
    
    shape = (1024,1024)
    scale = .5
    octaves = 6
    persistence = 0.5
    lacunarity = 2.0
    seed = np.random.randint(0,100)
    
    world = np.zeros(shape)
    
    # make coordinate grid on [0,1]^2
    x_idx = np.linspace(0, 1, shape[0])
    y_idx = np.linspace(0, 1, shape[1])
    world_x, world_y = np.meshgrid(x_idx, y_idx)
    
    # apply perlin noise, instead of np.vectorize, consider using itertools.starmap()
    world = np.vectorize(noise.pnoise2)(world_x/scale,
                            world_y/scale,
                            octaves=octaves,
                            persistence=persistence,
                            lacunarity=lacunarity,
                            repeatx=1024,
                            repeaty=1024,
                            base=seed)
    
    # here was the error: one needs to normalize the image first. Could be done without copying the array, though
    img = np.floor((world + .5) * 255).astype(np.uint8) # <- Normalize world first
    Image.fromarray(img, mode='L').show()
    

    【讨论】:

      【解决方案2】:

      如果有人追我,你应该使用噪音库进行标准化

      img = np.floor((world + 1) * 127).astype(np.uint8)
      

      这样就不会出现任何与应有的颜色相反的异常点。

      【讨论】:

        猜你喜欢
        • 2011-09-20
        • 2014-02-15
        • 2021-09-04
        • 2011-07-28
        • 2021-06-30
        • 2013-07-23
        • 2020-06-22
        • 2011-03-19
        • 1970-01-01
        相关资源
        最近更新 更多