平滑着色算法

Smooth coloring algorithm

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

的实现有什么问题

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

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

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

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

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 集这样的分形有一个 属性 使自适应超级采样成为一个不错的选择:有很大的区域,值不会发生太大变化(并且会浪费额外的计算),以及其他值变化很大的区域。他们需要在图片变化很大的地方(即图像中的嘈杂位)采集大量样本。您可以使用几种方法来确定需要大量采样的内容。但是两个容易思考的方法是:

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