gpt4 book ai didi

python - 如何实现红噪声?

转载 作者:太空狗 更新时间:2023-10-29 21:09:41 25 4
gpt4 key购买 nike

我刚刚阅读了 red noise一位教授在一次讲座中提到它后的文章。

我的想法是从 {0,..., 255} 中的随机数开始。然后我通过在 {0, ..., 255} 中添加一个随机偏移来完成从左到右的第一行。第一行完成后,我将取上部和左侧元素的平均值,并为下一个像素添加一个随机偏移量。

这样,我从左到右、从上到下创建图像。

我是这样实现的:

#!/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)

给出

enter image description here

看起来很酷。但是,我认为它应该更像这样:https://commons.wikimedia.org/wiki/File:Red.noise.col.png

我做错了什么/我该如何解决?

最佳答案

我认为问题可能出在当您在左侧和上方有一个有效位置时计算相关值时。你有:

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)))

在您的版本中,当您实际需要上方像素和左侧像素时,您取上方和对角线上方和左侧像素的平均值。

关于python - 如何实现红噪声?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34888126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com