python 利用 noise 生成纹理。

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Apr 23 20:04:41 2018
  4. @author: shiyi
  5. """
  6. import random, math
  7. import cv2
  8. import numpy as np
  9. """
  10. Texture generation using Perlin noise
  11. """
  12. class NoiseUtils:
  13. def __init__(self, imageSize):
  14. self.imageSize = imageSize
  15. self.gradientNumber = 256
  16. self.grid = [[]]
  17. self.gradients = []
  18. self.permutations = []
  19. self.img = {}
  20. self.__generateGradientVectors()
  21. self.__normalizeGradientVectors()
  22. self.__generatePermutationsTable()
  23. def __generateGradientVectors(self):
  24. for i in range(self.gradientNumber):
  25. while True:
  26. x, y = random.uniform(-1, 1), random.uniform(-1, 1)
  27. if x * x + y * y < 1:
  28. self.gradients.append([x, y])
  29. break
  30. def __normalizeGradientVectors(self):
  31. for i in range(self.gradientNumber):
  32. x, y = self.gradients[i][0], self.gradients[i][1]
  33. length = math.sqrt(x * x + y * y)
  34. self.gradients[i] = [x / length, y / length]
  35. # The modern version of the Fisher-Yates shuffle
  36. def __generatePermutationsTable(self):
  37. self.permutations = [i for i in range(self.gradientNumber)]
  38. for i in reversed(range(self.gradientNumber)):
  39. j = random.randint(0, i)
  40. self.permutations[i], self.permutations[j] = \
  41. self.permutations[j], self.permutations[i]
  42. def getGradientIndex(self, x, y):
  43. return self.permutations[(x + self.permutations[y % self.gradientNumber]) % self.gradientNumber]
  44. def perlinNoise(self, x, y):
  45. qx0 = int(math.floor(x))
  46. qx1 = qx0 + 1
  47. qy0 = int(math.floor(y))
  48. qy1 = qy0 + 1
  49. q00 = self.getGradientIndex(qx0, qy0)
  50. q01 = self.getGradientIndex(qx1, qy0)
  51. q10 = self.getGradientIndex(qx0, qy1)
  52. q11 = self.getGradientIndex(qx1, qy1)
  53. tx0 = x - math.floor(x)
  54. tx1 = tx0 - 1
  55. ty0 = y - math.floor(y)
  56. ty1 = ty0 - 1
  57. v00 = self.gradients[q00][0] * tx0 + self.gradients[q00][1] * ty0
  58. v01 = self.gradients[q01][0] * tx1 + self.gradients[q01][1] * ty0
  59. v10 = self.gradients[q10][0] * tx0 + self.gradients[q10][1] * ty1
  60. v11 = self.gradients[q11][0] * tx1 + self.gradients[q11][1] * ty1
  61. wx = tx0 * tx0 * (3 - 2 * tx0)
  62. v0 = v00 + wx * (v01 - v00)
  63. v1 = v10 + wx * (v11 - v10)
  64. wy = ty0 * ty0 * (3 - 2 * ty0)
  65. return (v0 + wy * (v1 - v0)) * 0.5 + 1
  66. def makeTexture(self, texture = None):
  67. if texture is None:
  68. texture = self.cloud
  69. noise = {}
  70. max = min = None
  71. for i in range(self.imageSize):
  72. for j in range(self.imageSize):
  73. value = texture(i, j)
  74. noise[i, j] = value
  75. if max is None or max < value:
  76. max = value
  77. if min is None or min > value:
  78. min = value
  79. for i in range(self.imageSize):
  80. for j in range(self.imageSize):
  81. self.img[i, j] = (int) ((noise[i, j] - min) / (max - min) * 255 )
  82. def fractalBrownianMotion(self, x, y, func):
  83. octaves = 12
  84. amplitude = 1.0
  85. frequency = 1.0 / self.imageSize
  86. persistence = 0.5
  87. value = 0.0
  88. for k in range(octaves):
  89. value += func(x * frequency, y * frequency) * amplitude
  90. frequency *= 2
  91. amplitude *= persistence
  92. return value
  93. def cloud(self, x, y, func = None):
  94. if func is None:
  95. func = self.perlinNoise
  96. return self.fractalBrownianMotion(8 * x, 8 * y, func)
  97. def wood(self, x, y, noise = None):
  98. if noise is None:
  99. noise = self.perlinNoise
  100. frequency = 1.0 / self.imageSize
  101. n = noise(4 * x * frequency, 4 * y * frequency) * 10
  102. return n - int(n)
  103. def marble(self, x, y, noise = None):
  104. if noise is None:
  105. noise = self.perlinNoise
  106. frequency = 1.0 / self.imageSize
  107. n = self.fractalBrownianMotion(8 * x, 8 * y, self.perlinNoise)
  108. return (math.sin(16 * x * frequency + 4 * (n - 0.5)) + 1) * 0.5
  109. if __name__ == "__main__":
  110. imageSize = 512
  111. noise = NoiseUtils(imageSize)
  112. noise.makeTexture(texture = noise.cloud)
  113. img = np.zeros((512, 512))
  114. pixels = img.copy()
  115. for i in range(0, imageSize):
  116. for j in range(0, imageSize):
  117. c = noise.img[i, j]
  118. pixels[i, j] = c
  119. pixels = pixels.astype('uint8')
  120. cv2.imwrite('Noise.png', pixels)
  121. # cv2.imshow("Noise", pixels)

python perlin noise的更多相关文章

  1. Perlin Noise 及其应用

    Perlin Noise 可以用来表现自然界中无法用简单形状来表达的物体的形态,比如火焰.烟雾.表面纹路等.要生成 Perlin Noise 可以使用工具离线生成,也可以使用代码运行时生成.最简单常用 ...

  2. 【Ray Tracing The Next Week 超详解】 光线追踪2-4 Perlin noise

     Preface 为了得到更好的纹理,很多人采用各种形式的柏林噪声(该命名来自于发明人 Ken Perlin) 柏林噪声是一种比较模糊的白噪声的东西:(引用书中一张图) 柏林噪声是用来生成一些看似杂乱 ...

  3. 利用perlin noise 生成 wood texture

    %%% Perlin Noise %%% Wood_texture clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image P ...

  4. OpenCV——Perlin Noise

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  5. python 不同版本下载资源

    Unofficial Windows Binaries for Python Extension Packages by Christoph Gohlke, Laboratory for Fluore ...

  6. 利用Perlin nosie 完毕(PS 滤镜—— 分成云彩)

    %%%% Cloud %%%% 利用perlin noise生成云彩 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image ...

  7. 利用Perlin nosie 完成(PS 滤镜—— 分成云彩)

    %%%% Cloud %%%% 利用perlin noise生成云彩 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image ...

  8. GraphicsLab Project 之 Curl Noise

    作者:i_dovelemon 日期:2020-04-25 主题:Perlin Noise, Curl Noise, Finite Difference Method 引言 最近在研究流体效果相关的模拟 ...

  9. 翻译: 星球生成 II

    翻译: 星球生成 II 本文翻译自Planet Generation - Part II 译者: FreeBlues 以下为译文: 概述 在前一章 我解释了如何为星球创建一个几何球体. 在本文中, 我 ...

随机推荐

  1. WPF基础学习笔记整理 (五) DependencyObject & DependencyProperty

    参考资料: 1.http://www.cnblogs.com/Zhouyongh/archive/2009/10/20/1586278.html 基础知识: DependencyObject & ...

  2. rostopic 命令

    rostopic bw display bandwidth used by topic// rostopic delay display delay for topic which has heade ...

  3. Java 面向对象之接口、多态

    01接口的概念 A:接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来完成 ...

  4. VC_可再发行组件包

    1. 中文 : 可再发行组件包 英文 : Redistributable Package 例子 : Download Microsoft Visual C++ 2010 Redistributable ...

  5. byte[]数组与十六进制字符串与字符串的互相转换 ——转载

    https://www.cnblogs.com/lelehellow/p/6369631.html

  6. C# 导出HTML为Excel

    最近在项目中需要Excel导出,有多个Sheet页,每个Sheet页的内容比较多,且不是规整的表格,绑定值是个比较麻烦的事,便考虑直接将HTML转换为Excel文件进行导出. 一.使用JS方法将HTM ...

  7. Jzzhu and Apples CodeForces - 449C (构造,数学)

    大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1 考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可 有奇数个的话, 余下一个2*p不划 ...

  8. 『PyTorch』第七弹_nn.Module扩展层

    有下面代码可以看出torch层函数(nn.Module)用法,使用超参数实例化层函数类(常位于网络class的__init__中),而网络class实际上就是一个高级的递归的nn.Module的cla ...

  9. 『Scrapy』终端调用&选择器方法

    Scrapy终端 示例,输入如下命令后shell会进入Python(或IPython)交互式界面: scrapy shell "http://www.itcast.cn/channel/te ...

  10. 78. Subsets 90. Subsets II

    1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...