使用递归插值计算平滑颜色图
computing smooth color map using interpolation recursively
我正在递归计算 mandelbrot 集并尝试使用平滑着色算法执行线性插值。但是,我无法将此 returns 浮点 RGB 值放入我正在使用的 ppm 图像中,因此我不得不使用 int()
进行舍入,从而创建更平滑但仍带状的图像。
有没有更简单的方法可以生成更好的非带状图像?
第二个函数是一个非常糟糕的 hack,只是在玩弄想法,因为平滑算法似乎产生了 256**3
范围内的 rgb 值
注释掉我正在做的线性插值。
这是我的三个函数:
def linear_interp(self, color_1, color_2, i):
r = (color_1[0] * (1 - i)) + (color_2[0] * i)
g = (color_1[1] * (1 - i)) + (color_2[1] * i)
b = (color_1[2] * (1 - i)) + (color_2[2] * i)
return (int(abs(r)), int(abs(g)), int(abs(b)))
def mandel(self, x, y, z, iteration = 0):
mod_z = sqrt((z.real * z.real) + (z.imag * z.imag))
#If its not in the set or we have reached the maximum depth
if abs(z) >= 2.00 or iteration == DEPTH:
if iteration == DEPTH:
mu = iteration
else:
mu = iteration + 1 - log(log(mod_z)) / log(2)
else:
mu = 0
z = (z * z) + self.c
self.mandel(x, y, z, iteration + 1)
return mu
def create_image(self):
begin = time.time() #For computing how long it took (start time)
self.rgb.palette = []
for y in range(HEIGHT):
self.rgb.palette.append([]) #Need to create the rows of our ppm
for x in range(WIDTH):
self.c = complex(x * ((self.max_a - self.min_a) / WIDTH) + self.min_a,
y * ((self.max_b - self.min_b) / HEIGHT) + self.min_b)
z = self.c
q = (self.c.real - 0.25)**2 + (self.c.imag * self.c.imag)
x = self.c.real
y2 = self.c.imag * self.c.imag
if not (q*(q + (x - 0.25)) < y2 / 4.0 or (x + 1.0)**2 + y2 <0.0625):
mu = self.mandel(x, y, z, iteration = 0)
rgb = self.linear_interp((255, 255, 0), (55, 55, 0), mu)
self.rgb.palette[y].append(rgb)
else:
self.rgb.palette[y].append((55, 55, 0))
if self.progress_bar != None:
self.progress_bar["value"] = y
self.canvas.update()
我得到的图像如下:
我认为这是罪魁祸首:
else:
mu = 0
self.mandel(x, y, z, iteration + 1)
return mu
这并没有正确地从递归调用中传递 mu
的值,因此对于 1 次调用后未触底的所有内容,您都会变黑。尝试
else:
...
mu = self.mandel(x, y, z, iteration + 1)
return mu
我正在递归计算 mandelbrot 集并尝试使用平滑着色算法执行线性插值。但是,我无法将此 returns 浮点 RGB 值放入我正在使用的 ppm 图像中,因此我不得不使用 int()
进行舍入,从而创建更平滑但仍带状的图像。
有没有更简单的方法可以生成更好的非带状图像?
第二个函数是一个非常糟糕的 hack,只是在玩弄想法,因为平滑算法似乎产生了 256**3
范围内的 rgb 值注释掉我正在做的线性插值。
这是我的三个函数:
def linear_interp(self, color_1, color_2, i):
r = (color_1[0] * (1 - i)) + (color_2[0] * i)
g = (color_1[1] * (1 - i)) + (color_2[1] * i)
b = (color_1[2] * (1 - i)) + (color_2[2] * i)
return (int(abs(r)), int(abs(g)), int(abs(b)))
def mandel(self, x, y, z, iteration = 0):
mod_z = sqrt((z.real * z.real) + (z.imag * z.imag))
#If its not in the set or we have reached the maximum depth
if abs(z) >= 2.00 or iteration == DEPTH:
if iteration == DEPTH:
mu = iteration
else:
mu = iteration + 1 - log(log(mod_z)) / log(2)
else:
mu = 0
z = (z * z) + self.c
self.mandel(x, y, z, iteration + 1)
return mu
def create_image(self):
begin = time.time() #For computing how long it took (start time)
self.rgb.palette = []
for y in range(HEIGHT):
self.rgb.palette.append([]) #Need to create the rows of our ppm
for x in range(WIDTH):
self.c = complex(x * ((self.max_a - self.min_a) / WIDTH) + self.min_a,
y * ((self.max_b - self.min_b) / HEIGHT) + self.min_b)
z = self.c
q = (self.c.real - 0.25)**2 + (self.c.imag * self.c.imag)
x = self.c.real
y2 = self.c.imag * self.c.imag
if not (q*(q + (x - 0.25)) < y2 / 4.0 or (x + 1.0)**2 + y2 <0.0625):
mu = self.mandel(x, y, z, iteration = 0)
rgb = self.linear_interp((255, 255, 0), (55, 55, 0), mu)
self.rgb.palette[y].append(rgb)
else:
self.rgb.palette[y].append((55, 55, 0))
if self.progress_bar != None:
self.progress_bar["value"] = y
self.canvas.update()
我得到的图像如下:
我认为这是罪魁祸首:
else:
mu = 0
self.mandel(x, y, z, iteration + 1)
return mu
这并没有正确地从递归调用中传递 mu
的值,因此对于 1 次调用后未触底的所有内容,您都会变黑。尝试
else:
...
mu = self.mandel(x, y, z, iteration + 1)
return mu