Python 如何实现红色噪音?
在一位教授在一次讲座中提到这篇文章之后,我刚刚读了这篇文章 我的想法是从{0,…,255}中的一个随机数开始。然后通过在{0,…,255}中添加一个随机偏移量,从左到右完成第一行。 第一行完成后,我将取左上元素的平均值,并为下一个像素添加一个随机偏移量 这样,我从左到右,从上到下创建图像 我已经这样实现了它:Python 如何实现红色噪音?,python,computer-vision,Python,Computer Vision,在一位教授在一次讲座中提到这篇文章之后,我刚刚读了这篇文章 我的想法是从{0,…,255}中的一个随机数开始。然后通过在{0,…,255}中添加一个随机偏移量,从左到右完成第一行。 第一行完成后,我将取左上元素的平均值,并为下一个像素添加一个随机偏移量 这样,我从左到右,从上到下创建图像 我已经这样实现了它: #!/usr/bin/env python # -*- coding: utf-8 -*- """Create a red noise RGB image of the dimensio
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Create a red noise RGB image of the dimensions you want."""
import numpy
import Image
import random
def create_red_noise(outfile, width, height, r=10):
"""
Create red noise RGB image
Parameters
----------
outfile : str
width : int
height : int
r : int
Random maximum offset compared to the last pixel
"""
array = numpy.random.rand(height, width, 3) * 255
for x in range(width):
for y in range(height):
if y == 0:
if x == 0:
continue
else:
for i in range(3):
array[y][x][i] = (array[y][x-1][i] +
random.randint(-r, r))
else:
if x == 0:
for i in range(3):
array[y][x][i] = (array[y-1][x][i] +
random.randint(-r, r))
else:
for i in range(3):
array[y][x][i] = (((array[y-1][x][i] +
array[y-1][x-1][i]) / 2.0 +
random.randint(-r, r)))
im_out = Image.fromarray(array.astype('uint8')).convert('RGBA')
im_out.save(outfile)
def get_parser():
"""Get parser object for create_random_image.py."""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
parser = ArgumentParser(description=__doc__,
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-f", "--file",
dest="filename",
help="write red noise image to FILE",
default="red-noise.jpg",
metavar="FILE")
parser.add_argument("-x", "--width",
dest="width",
default=1280,
type=int,
help="width of the image")
parser.add_argument("-y", "--height",
dest="height",
default=960,
type=int,
help="height of the image")
parser.add_argument("-o", "--offset",
dest="offset",
default=10,
type=int,
help="maximum offset compared to the neighbors")
return parser
if __name__ == "__main__":
args = get_parser().parse_args()
create_red_noise(args.filename, args.width, args.height, args.offset)
给
看起来很酷。不过,我觉得应该是这样的:
我做错了什么/如何修复它?我认为问题可能是当您在左侧和上方有一个有效位置时计算相关值。你有:
array[y][x][i] = (((array[y-1][x][i] +
array[y-1][x-1][i]) / 2.0 +
random.randint(-r, r)))
我认为这应该是:
array[y][x][i] = (((array[y-1][x][i] +
array[y][x-1][i]) / 2.0 +
random.randint(-r, r)))
在你的版本中,当你真的想要上面的像素和左边的像素时,你是取上面和左边的像素的平均值。在维基百科图像上,你可以清楚地看到严重的过滤,它是平滑的,没有粗糙的边缘。彩色噪声通常通过衰减高频分量来实现。。至少在音频领域。我看不到一种“修复”代码的方法,你应该寻找正确的方法(我确信谷歌上有很多点击)。据我所知,噪音的颜色反映了给定噪音的(功率)频谱。您的条纹图像仍可能具有红色噪声频谱。空间变异与红色的噪声无关,是吗?@AndrasDeak:但它不会是真正的噪声(即随机噪声)。@KarolyHorvath好吧,至少不是布朗运动的噪声:)除了这个bug之外,我认为你的算法和维基提供的图像之间的唯一区别可能是色调。希望这有帮助!这解释了为什么线条不是45度对角线,但它不能解释为什么一般模式与提供的屏幕截图不匹配好吧,所以我的解决方案显然不会像@o_o所指出的那样产生棕色噪音。二维相关噪波比一维噪波稍微复杂一点,这被维基掩盖了。这里是关于粉红噪声生成的另一个SO交换:您可以按照提供的链接到MATLAB实现来生成粉红噪声或棕色/红色噪声。该解决方案涉及具有随机相移的傅里叶逆变换。