柏林噪音看起来太吵了

2024-09-27 00:16:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了自己的perlin库,还使用了一个标准python库来生成噪声。下面是我的代码:

import sys
from noise import pnoise2, snoise2

perlin = np.empty((sizeOfImage,sizeOfImage),dtype=np.float32)
freq = 1024
for y in range(256):
    for x in range(256):
        perlin[y][x] = int(pnoise2(x / freq, y / freq, 4) * 32.0 + 128.0)
max = np.amax(perlin)
min = np.amin(perlin)
max += abs(min)
perlin += abs(min)
perlin /= max
perlin *= 255
img = Image.fromarray(perlin, 'L')
img.save('my.png')
dp(filename='my.png')

它生成的图像是:enter image description here

不管频率或倍频程如何,它总是看起来很粗糙。我的结论是我用错了,但我不知道为什么我的解决方案是错误的。我使用分数单位通过频率和迭代通过我的二维数组。我试过转换指标,但还是没有连续性。怎样才能得到平滑的柏林噪声?在


Tags: inimportimgformynprangeabs
1条回答
网友
1楼 · 发布于 2024-09-27 00:16:57

我认为有一些潜在的问题

  • 除非您想失去精度,否则在对范围进行规格化之前不要转换为int
  • 要使其正常化,请从max和{}中减去{},而不是添加abs(min)

例如:

import numpy as np
from PIL import Image
import sys
from noise import pnoise2, snoise2

sizeOfImage = 256

perlin = np.empty((sizeOfImage,sizeOfImage),dtype=np.float32)
freq = 1024
for y in range(256):
    for x in range(256):
        perlin[y][x] = pnoise2(x / freq, y / freq, 4) # don't need to scale or shift here as the code below undoes that anyway
max = np.amax(perlin)
min = np.amin(perlin)
max -= min
perlin -= min
perlin /= max
perlin *= 255
img = Image.fromarray(perlin.astype('uint8'), 'L') # convert to int here instead
img.save('my.png')

enter image description here

相关问题 更多 >

    热门问题