【问题标题】:Perlin noise looks too griddyPerlin 噪音看起来太粗糙了
【发布时间】:2018-01-23 00:38:39
【问题描述】:

我已经编写了自己的 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')

它生成的图片是:

无论频率或八度如何,它总是看起来很粗糙。我的结论是我用错了,但我不确定为什么我的解决方案是错误的。我通过频率使用小数单位并遍历我的二维数组。我已经尝试过切换索引等等,但似乎仍然没有连续性。如何获得平滑的柏林噪声?

【问题讨论】:

  • 这是 Python2.x 吗?如果是这样,x / freq 使用整数除法并将循环中的所有值向下舍入为零
  • 这是python 3

标签: python graphics noise perlin-noise


【解决方案1】:

我认为存在一些潜在问题

  • 在规范化范围之前不要转换为int,除非你想失去精度
  • 为了规范化,从maxperlin 中减去min 而不是加上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')

【讨论】:

  • 我觉得将 min 转换为 abs 绝对是我出错的地方。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2017-09-04
  • 2014-08-17
  • 2016-08-17
  • 1970-01-01
  • 2014-03-27
  • 2015-03-20
  • 2012-11-21
  • 2012-12-22
相关资源
最近更新 更多