Mandelbrot Zoom-Video -- Code Optimierung
Verfasst: Montag 18. Mai 2020, 10:57
Hallo zusammen,
Ich habe einen Code geschrieben, der ein Zoom-Video in die Mandelbrotmenge erstellt.
Der Code funktioniert einwandfrei und nun geht es an das weitere Optimieren. Da nun also meine Frage:
Was kann man an folgendem Code noch verbessern? Um ihn effektiver (natürlich vor allem RAM und Zeit effektiver) zu machen.
Über Vorschläge wäre ich sehr dankbar, bitte postet dann einfach den Code mit den Veränderungen in eurem Kommentar.
Ich habe einen Code geschrieben, der ein Zoom-Video in die Mandelbrotmenge erstellt.
Der Code funktioniert einwandfrei und nun geht es an das weitere Optimieren. Da nun also meine Frage:
Was kann man an folgendem Code noch verbessern? Um ihn effektiver (natürlich vor allem RAM und Zeit effektiver) zu machen.
Über Vorschläge wäre ich sehr dankbar, bitte postet dann einfach den Code mit den Veränderungen in eurem Kommentar.
Code: Alles auswählen
import matplotlib.animation as manimation
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from itertools import cycle
import matplotlib.colors as clr
import matplotlib.animation as animation
import moviepy.editor as mp
import os
import cv2
import time
colorpoints = [(1-(1-q)**4, c) for q, c in zip(np.linspace(0, 1, 20),
cycle(['#ffff88', '#000000', #Standard: #ffff88, #000000, #ffaa00
'#ffaa00',]))]
#http://www.am.uni-duesseldorf.de/de/Links/Tools/farbtabelle.html for RGB-codes
cmap = clr.LinearSegmentedColormap.from_list('mycmap',colorpoints, N=2048)
rc('animation', html='html5')
fig_size = 8
fig = plt.figure(figsize=(fig_size, fig_size), dpi = 150, tight_layout=True)
max_frames = 10
max_zoom = 100000 #max_zoom for these coordinates (0.357535415497125, 0.070571561552046) 1.7592187E13
#can go higher but it is possible that it zooms into a non borderregion region
#but 1.7592187E13 is already a huge zoom-factor
rmin, rmax, imin, imax = -2.5, 1.5, -2, 2
Writer = animation.writers['ffmpeg']
writer = animation.FFMpegWriter(fps=2, metadata=dict(artist='Lukas Kretschamnn'), bitrate = -1, extra_args=['-pix_fmt', 'yuv420p'] )
def mandelbrot(rmin, rmax, rpoints, imin, imax, ipoints, max_iterations=1000, infinity_border=10):
image = np.zeros((rpoints, ipoints))
r, i = np.mgrid[rmin:rmax:(rpoints * 1j), imin:imax:(ipoints * 1j)]
c = r + 1j * i
z = np.zeros_like(c)
for k in range(max_iterations):
z = z ** 2 + c
mask = (np.abs(z) > infinity_border) & (image == 0)
image[mask] = k
z[mask] = np.nan
return -image.T
def init():
return plt.gca()
def animate(i):
r_center, i_center = 0.340037926617566, -0.051446751669 #Standard: 0.357535415497125, 0.070571561552046
zoom = (i / max_frames) ** 3 * max_zoom + 1
scalefactor = 1 / zoom
rmin_ = (rmin - r_center) * scalefactor + r_center
imin_ = (imin - i_center) * scalefactor + i_center
rmax_ = (rmax - r_center) * scalefactor + r_center
imax_ = (imax - i_center) * scalefactor + i_center
image = mandelbrot(rmin_, rmax_, 1000, imin_, imax_, 1000) #increase rpoints and ipoints for better resolution
plt.axis('off', bbox_inches='tight', pad_inches = 0, tight_layout=True)
plt.imshow(image, cmap=cmap, interpolation='none')
print("Frame number {} created;".format(i), "next frame: {}".format(i + 1), "|", ((i + 1)/max_frames) * 100, "%") #counter starts with zero; so last frame number is max_frames - 1
def cut_frames():
cam = cv2.VideoCapture("OUTPUT.mp4")
try:
# create a directory named data
if not os.path.exists('data'):
os.makedirs('data')
# if directory was not created then raise an error
except OSError:
print ('Error: Creating directory of data failed')
# frame
currentframe = 0
while(True):
# reading from frame
ret,frame = cam.read()
if ret:
# if video is still left continue creating images
name = './data/frame' + str(currentframe) + '.jpg'
print ('Creating...' + name)
# writing the extracted images
cv2.imwrite(name, frame)
# increasing counter so that it will
# show how many frames are created
currentframe += 1
else:
break
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
print("Video Processing")
t0a = time.time()
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=max_frames, interval=150)
anim.save('OUTPUT.mp4', writer=writer, dpi = 150)
t1a = time.time()
print("Took:", t1a - t0a, "Next Step: Frame slicing")
t0b = time.time()
cut_frames()
t1b = time.time()
print("Took:", t1b - t0b, " FINISHED")