微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用归一化迭代计数的 Mandelbrot 集

如何解决使用归一化迭代计数的 Mandelbrot 集

我有以下 Python 程序试图使用 normalized iteration count algorithm 为 Mandelbrot 集着色:

from PIL import Image
import numpy as np
from matplotlib.colors import hsv_to_rgb

steps = 256 # maximum iterations
bailout_radius = 64 # bailout radius

def normalized_iteration(n,abs_z):
    return n + 1 - np.log2(np.log(abs_z))/np.log(2)

def make_set(real_start,real_end,imag_start,imag_end,height):

    width = \
        int(abs(height * (real_end - real_start) / (imag_end - imag_start)))

    real_axis = \
        np.linspace(real_start,num = width)
    imag_axis = \
        np.linspace(imag_start,num = height)
    complex_plane = \
        np.zeros((height,width),dtype = np.complex_)

    real,imag = np.meshgrid(real_axis,imag_axis)

    complex_plane.real = real
    complex_plane.imag = imag

    pixels = \
        np.zeros((height,width,3),dtype = np.float_)

    new = np.zeros_like(complex_plane)
    is_not_done = np.ones((height,dtype = bool)

    # cosine_interpolation = lambda x: (np.cos(x * np.pi + np.pi) + 1) / 2
    
    for i in range(steps):
        new[is_not_done] = \
            new[is_not_done] ** 2 + complex_plane[is_not_done]
        
        mask = np.logical_and(np.absolute(new) > bailout_radius,is_not_done)
        pixels[mask,:] = (i,0.6,1)
        is_not_done = np.logical_and(is_not_done,np.logical_not(mask))

    new_after_mask = np.zeros_like(complex_plane)
    new_after_mask[np.logical_not(is_not_done)] = \
        new[np.logical_not(is_not_done)]
    new_after_mask[is_not_done] = bailout_radius

    pixels[:,:,0] = \
        normalized_iteration(pixels[:,0],np.absolute(new_after_mask)) / steps

    image = Image.fromarray((hsv_to_rgb(np.flipud(pixels)) * 255).astype(np.uint8))

    image.show()

make_set(-2,1,-1,2000) 

它产生了相当漂亮的图像。但是,当我将它与采用此算法的其他集合进行比较时,我的集合中的颜色几乎没有变化。如果我减少 steps,我会得到更多样化的梯度,但这会降低分形的质量。这段代码的重要部分是我的 normalized_iteration 定义,它与 this Wikipedia article's version,

略有不同
def normalized_iteration(n,abs_z):
    return n + 1 - np.log2(np.log(abs_z))/np.log(2)

我使用该定义的地方(将函数映射到像素数组),

pixels[:,np.absolute(new_after_mask)) / steps

和最后一个数组,我将 HSV 格式转换为 RGB 并将 [0,1) 上的像素值转换为 [0,255) 上的值

image = Image.fromarray((hsv_to_rgb(np.flipud(pixels)) * 255).astype(np.uint8))

我已经与这个问题斗争了一段时间了,我不确定出了什么问题。感谢您帮助我确定如何使渐变的颜色更加多样化,并支持我可能难以阅读的代码。另外,我意识到那里有优化的空间。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。