python perlin noise
python 利用 noise 生成纹理。
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 23 20:04:41 2018
@author: shiyi
"""
import random, math
import cv2
import numpy as np
"""
Texture generation using Perlin noise
"""
class NoiseUtils:
def __init__(self, imageSize):
self.imageSize = imageSize
self.gradientNumber = 256
self.grid = [[]]
self.gradients = []
self.permutations = []
self.img = {}
self.__generateGradientVectors()
self.__normalizeGradientVectors()
self.__generatePermutationsTable()
def __generateGradientVectors(self):
for i in range(self.gradientNumber):
while True:
x, y = random.uniform(-1, 1), random.uniform(-1, 1)
if x * x + y * y < 1:
self.gradients.append([x, y])
break
def __normalizeGradientVectors(self):
for i in range(self.gradientNumber):
x, y = self.gradients[i][0], self.gradients[i][1]
length = math.sqrt(x * x + y * y)
self.gradients[i] = [x / length, y / length]
# The modern version of the Fisher-Yates shuffle
def __generatePermutationsTable(self):
self.permutations = [i for i in range(self.gradientNumber)]
for i in reversed(range(self.gradientNumber)):
j = random.randint(0, i)
self.permutations[i], self.permutations[j] = \
self.permutations[j], self.permutations[i]
def getGradientIndex(self, x, y):
return self.permutations[(x + self.permutations[y % self.gradientNumber]) % self.gradientNumber]
def perlinNoise(self, x, y):
qx0 = int(math.floor(x))
qx1 = qx0 + 1
qy0 = int(math.floor(y))
qy1 = qy0 + 1
q00 = self.getGradientIndex(qx0, qy0)
q01 = self.getGradientIndex(qx1, qy0)
q10 = self.getGradientIndex(qx0, qy1)
q11 = self.getGradientIndex(qx1, qy1)
tx0 = x - math.floor(x)
tx1 = tx0 - 1
ty0 = y - math.floor(y)
ty1 = ty0 - 1
v00 = self.gradients[q00][0] * tx0 + self.gradients[q00][1] * ty0
v01 = self.gradients[q01][0] * tx1 + self.gradients[q01][1] * ty0
v10 = self.gradients[q10][0] * tx0 + self.gradients[q10][1] * ty1
v11 = self.gradients[q11][0] * tx1 + self.gradients[q11][1] * ty1
wx = tx0 * tx0 * (3 - 2 * tx0)
v0 = v00 + wx * (v01 - v00)
v1 = v10 + wx * (v11 - v10)
wy = ty0 * ty0 * (3 - 2 * ty0)
return (v0 + wy * (v1 - v0)) * 0.5 + 1
def makeTexture(self, texture = None):
if texture is None:
texture = self.cloud
noise = {}
max = min = None
for i in range(self.imageSize):
for j in range(self.imageSize):
value = texture(i, j)
noise[i, j] = value
if max is None or max < value:
max = value
if min is None or min > value:
min = value
for i in range(self.imageSize):
for j in range(self.imageSize):
self.img[i, j] = (int) ((noise[i, j] - min) / (max - min) * 255 )
def fractalBrownianMotion(self, x, y, func):
octaves = 12
amplitude = 1.0
frequency = 1.0 / self.imageSize
persistence = 0.5
value = 0.0
for k in range(octaves):
value += func(x * frequency, y * frequency) * amplitude
frequency *= 2
amplitude *= persistence
return value
def cloud(self, x, y, func = None):
if func is None:
func = self.perlinNoise
return self.fractalBrownianMotion(8 * x, 8 * y, func)
def wood(self, x, y, noise = None):
if noise is None:
noise = self.perlinNoise
frequency = 1.0 / self.imageSize
n = noise(4 * x * frequency, 4 * y * frequency) * 10
return n - int(n)
def marble(self, x, y, noise = None):
if noise is None:
noise = self.perlinNoise
frequency = 1.0 / self.imageSize
n = self.fractalBrownianMotion(8 * x, 8 * y, self.perlinNoise)
return (math.sin(16 * x * frequency + 4 * (n - 0.5)) + 1) * 0.5
if __name__ == "__main__":
imageSize = 512
noise = NoiseUtils(imageSize)
noise.makeTexture(texture = noise.cloud)
img = np.zeros((512, 512))
pixels = img.copy()
for i in range(0, imageSize):
for j in range(0, imageSize):
c = noise.img[i, j]
pixels[i, j] = c
pixels = pixels.astype('uint8')
cv2.imwrite('Noise.png', pixels)
# cv2.imshow("Noise", pixels)
python perlin noise的更多相关文章
- Perlin Noise 及其应用
Perlin Noise 可以用来表现自然界中无法用简单形状来表达的物体的形态,比如火焰.烟雾.表面纹路等.要生成 Perlin Noise 可以使用工具离线生成,也可以使用代码运行时生成.最简单常用 ...
- 【Ray Tracing The Next Week 超详解】 光线追踪2-4 Perlin noise
Preface 为了得到更好的纹理,很多人采用各种形式的柏林噪声(该命名来自于发明人 Ken Perlin) 柏林噪声是一种比较模糊的白噪声的东西:(引用书中一张图) 柏林噪声是用来生成一些看似杂乱 ...
- 利用perlin noise 生成 wood texture
%%% Perlin Noise %%% Wood_texture clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image P ...
- OpenCV——Perlin Noise
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- python 不同版本下载资源
Unofficial Windows Binaries for Python Extension Packages by Christoph Gohlke, Laboratory for Fluore ...
- 利用Perlin nosie 完毕(PS 滤镜—— 分成云彩)
%%%% Cloud %%%% 利用perlin noise生成云彩 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image ...
- 利用Perlin nosie 完成(PS 滤镜—— 分成云彩)
%%%% Cloud %%%% 利用perlin noise生成云彩 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image ...
- GraphicsLab Project 之 Curl Noise
作者:i_dovelemon 日期:2020-04-25 主题:Perlin Noise, Curl Noise, Finite Difference Method 引言 最近在研究流体效果相关的模拟 ...
- 翻译: 星球生成 II
翻译: 星球生成 II 本文翻译自Planet Generation - Part II 译者: FreeBlues 以下为译文: 概述 在前一章 我解释了如何为星球创建一个几何球体. 在本文中, 我 ...
随机推荐
- shell 布尔运算
布尔运算 Bash 里的逻辑运算,与.或.非. 在 Shell 下如何进行逻辑运算 范例:true or false 单独测试 true 和 false,可以看出 true 是真值,false 为假 ...
- re.sub
1.re.sub主要功能实现正则的替换. re.sub定义: sub(pattern, repl, string, count=0, flags=0) 意思为:对字符串string按照正则表达式pat ...
- (转)C#制作一个消息拦截器
首先,我们先要制作一个自定义Attribute,让他可以具有上下文读取功能,所以我们这个Attribute类要同时继承Attribute和IContextAttribute. 接口IContextAt ...
- PHP消息队列之Beanstalk
Beanstalk,一个高性能.轻量级的分布式内存队列
- SpringBoot中的数据库连接池
内置的连接池 目前Spring Boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池. 数据库连接可以使用DataSource池进行自动配置. 由于Tomcat数 ...
- LeetCode--122--卖卖股票的最佳时机II
问题描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易( ...
- [INS-20802] Oracle Net Configuration Assistant failed,Caught UnknownHostException
在64位Centos上安装64的oracle 11g R2,出现错误: [INS-20802] Oracle Net Configuration Assistant failed 根据提示查看日志文件 ...
- seekg()/seekp()与tellg()/tellp()的用法详解
本文转载于:http://blog.csdn.net/mafuli007/article/details/7314917 (在tcp的文件发送部分有应用) 对输入流操作:seekg()与tellg() ...
- LeetCode 318. Maximum Product of Word Lengths (状态压缩)
题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...
- failed to load response data
当需要根据后台传回地址跳转页面时 即使使用preserve log 可以查看上一个页面获取地址请求,但是此时请求返回值为failed to load response data 当关闭页面跳转可以查看 ...