python - 平滑着色算法

标签 python algorithm python-3.x colors mandelbrot

这个问题已经发布了很多,但我已经通读了这里发布的问题并在不久前构建了我自己的实现。在挖掘了一些旧项目并再次遇到它之后,我放弃了这个项目并决定现在再试一次。但是,我仍然无法弄清楚 smooth coloring algorithm 的这个实现有什么问题。 .

但是,对于“图像”(tkinter Canvas )的平滑 strip 和着色,我通常有一个错误(可能?)。我想说这只是我定义的 RGB 调色板的结果,以及我要达到的深度,但我对此不是 100% 肯定的。在大多数情况下,它似乎是平滑的 strip ,稍后会详细介绍。我会尽量减少它。

我定义了以下 RGB 调色板(137 种颜色)here这是我在相当不错的缩放级别和计算集合时达到 5,000 深度时得到的结果。

at a fairly decent zoom level

现在,这就是它看起来很奇怪的地方。这是在一两个缩放级别之后,我想说这只是因为有这么多不同颜色的像素以保持乐观的前景。它看起来在更远的缩放级别上也是如此,尽管自然不那么突出。

zoomed out

这是我递归计算集合并为其着色的相关函数

def mandel(self, x, y, z, iteration):

    mod_z = math.sqrt((z.real * z.real) + (z.imag * z.imag))

    #If its not in the set or we have reached the maximum depth
    if  mod_z >= 100.0 or iteration == DEPTH:
        if iteration < DEPTH:
            if iteration > MAX_COLOR:
                iteration = iteration % MAX_COLOR
            nu = math.log2(math.log2(abs(mod_z)) / math.log2(2)) / math.log2(2)
            value = iteration + 5 - nu
            print(iteration)
            color_1 = RGB_PALETTE[math.floor(value)]
            color_2 = RGB_PALETTE[math.floor(value) + 1]
            fractional_iteration = value % 1

            color = self.linear_interp(color_1, color_2, fractional_iteration)

            self.canvas.create_line(x, y, x + 1, y + 1,
                                    fill = self.rgb_to_hex(color))

    else:

        z = (z * z) + self.c
        self.mandel(x, y, z, iteration + 1)

    return z

def create_image(self):
    begin = time.time() #For computing how long it took (start time)
    diam_a = self.max_a - self.min_a
    diam_b = self.max_b - self.min_b
    for y in range(HEIGHT):
        for x in range(WIDTH):
            self.c =  complex(x * (diam_a / WIDTH) + self.min_a, 
                              y * (diam_b / HEIGHT) + self.min_b)

            constant = 1.0
            z = self.c

            bound = 1 / 4
            q = (self.c.real - bound)**2 + (self.c.imag * self.c.imag)
            x1 = self.c.real
            y2 = self.c.imag * self.c.imag
            sixteenth = 1 / 16

            #See if its in p2-bulb for different coloring schema between the main bulb and other bulbs of the set
            if not (q*(q + (x1 - bound)) < y2 / (constant * 4) or 
                    (x1 + constant)**2 + y2 < sixteenth): 
                #value of the recursive call while it is not in the set
                z = self.mandel(x, y, z, iteration = 0)

            if self.progress_bar != None:
                self.progress_bar["value"] = y

    self.canvas.update() #Update the progress bar
    print("Took %s Minutes to Render" % ((time.time() - begin) / MINUTE))

如果以上内容还不够,请发布与计算和绘制集合相关的完整代码 here

最佳答案

您要消除的是混叠。所以解决方案(显然)是抗锯齿。分形的一个有效解决方案是自适应超采样。看这个adaptively super sampled mandelbrot sequence . Supersampling只是意味着输出中的每个像素都是像素区域中几个样本的平均值(记住你的像素不是无限小的点 - 它们有面积)。可以使用不同的模式,在美学、运行时和实现复杂性之间进行不同的权衡。

诸如 mandelbrot 或 julia 集之类的分形具有使自适应 super 采样成为不错选择的属性:有很大的区域,其中的值变化不大(并且会浪费额外的计算),而其他区域的值改变很多。他们需要在图片变化很大的地方(即图像中的嘈杂位)采集大量样本。您可以使用几种方法来确定需要大量采样的内容。但是有两个容易想到的方法是:

  • 多次遍历图像
    • 在每次连续传递中,如果像素的颜色相对于它的邻居的变化超过阈值,则在该像素的区域中执行进一步的采样并平均出这些值
  • 总是为每个像素做一些样本(比如 4 个)
    • 如果这些样本差异很大,就做更多
    • 这将在值相对相似的区域浪费计算

关于python - 平滑着色算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30271132/

相关文章:

python - 缺少依赖项在 Windows 7 上为 Python 3.4.1 32 位安装 NumPy 1.9

java - 短代码的 Python 到 Java 翻译

algorithm - 分配玩家到 table

c++ - C++ 中线段树的 STL

python - 使用python将十六进制字符串转换为整数

python - 删除字符串中的每隔一个字符(必须是字母或数字)而不影响Python中的空格

c++ - 可以使用一个指针来匹配 Python 中 float 变量的 int 值吗?

java - 在java中解析和比较字符串键的最有效算法

python 3 : How can I align the the format of this data when it prints

python - 在 python 中对 "FileNotFoundError"进行单元测试