首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python噪声库中的Perlin噪声

Python噪声库中的Perlin噪声
EN

Stack Overflow用户
提问于 2020-02-22 09:04:24
回答 2查看 5.5K关注 0票数 4

我在为我的项目产生Perlin噪音方面有问题。当我想了解如何正确地使用库时,我试着一步一步地遵循这个页面:https://medium.com/@yvanscher/playing-with-perlin-noise-generating-realistic-archipelagos-b59f004d8401,在第一部分中,有代码:

代码语言:javascript
复制
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()

我复制-粘贴它与小的变化在结尾(到图像是过时的),所以我有:

代码语言:javascript
复制
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()

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-22 10:21:10

这是工作代码。我冒昧地把它清理了一下。详见评论。最后的建议是:在测试代码时,使用matplotlib进行可视化。它的imshow()函数比PIL强大得多。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-10-13 19:40:49

如果有人跟在我后面,带着噪音库,你应该跟得上正常点。

代码语言:javascript
复制
img = np.floor((world + 1) * 127).astype(np.uint8)

这样,就不会有任何与其应有的颜色相反的异常颜色的斑点。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60350598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档