pythonã§æ£å¸å³ã¢ãã¡ã¼ã·ã§ã³ã試ãã¦ã¿ã
æè¿pythonã触ãå§ããã®ã§ãããæ£å¸å³ãã¢ãã¡ã¼ã·ã§ã³ãããæ¹æ³ãåãããªãã£ãã®ã§èª¿ã¹ã¦ã¿ã¾ããã
æ£å¸å³ã¯matplotlib.plt.scatter(x,y)
ã§ä½æããäºãåºæ¥ã¾ãã
ã¾ããã¢ãã¡ã¼ã·ã§ã³ããããæ¹æ³ã¯äºéãã®ããæ¹ãããããã§ãã
- animation.ArtistAnimation
- äºåã«ç¨æãã¦ãããã¼ã¿ãæç»
- animation.FuncAnimation
- éæãã¼ã¿ãæ´æ°ãã
ããã§åå¨ä¸ã®ç¹ãä¸åº¦ãã¤ç§»åãããã¨ããã¢ãã¡ã¼ã·ã§ã³ãArtistAnimationã¨FuncAnimationã®2ã¤ã®æ¹æ³ã§è©¦ãã¦ã¿ã¾ããã å®è¡çµæã¯ã©ã¡ãã次ã®ãããªãã®ã«ãªãã¾ãã
animation.ArtistAnimationã®å ´å
äºåã«plt.scatterã®æ»ãå¤ãlistã«ä¿åãã¦ãããanimation.ArtistAnimationã®ç¬¬äºå¼æ°ã«æ¸¡ã㨠æç»ããäºãåºæ¥ã¾ããã
# -*- coding: UTF-8 -*- import math import matplotlib.pyplot as plt import matplotlib.animation as animation fig = plt.figure() # 360度åã®ãã¼ã¿ãä½æ ims = [] for i in range(360): rad = math.radians(i) im = plt.scatter(math.cos(rad), math.sin(rad)) ims.append([im]) # ã¢ãã¡ã¼ã·ã§ã³ä½æ ani = animation.ArtistAnimation(fig, ims, interval=1, repeat_delay=1000) # 表示 plt.show()
animation.FuncAnimationã®å ´å
æ´æ°ã®ããã®ã³ã¼ã«ããã¯é¢æ°ãanimation.FuncAnimationã®ç¬¬äºå¼æ°ã«å
¥ãã¾ãã
ã¾ããã³ã¼ã«ããã¯é¢æ°ã®å¼æ°ã¯fargs=(fig, im)ã®ããã«ãã¦æ¸¡ãäºãåºæ¥ã¾ãã
# -*- coding: UTF-8 -*- import math import matplotlib.pyplot as plt import matplotlib.animation as animation # æ´æ°ããå 容 def _update_plot(i, fig, im): rad = math.radians(i) # ååã®ãã¬ã¼ã å 容ãä¸æ¦åé¤ if len(im) > 0: im[0].remove() im.pop() im.append(plt.scatter(math.cos(rad), math.sin(rad))) fig = plt.figure() # ã°ã©ããä¸å¤®ã«è¡¨ç¤º ax = fig.add_subplot(1,1,1) # ã°ã©ãã®ç®çç¯å²è¨å® ax.set_xlim([-1.5, 1.5]) ax.set_ylim([-1.5, 1.5]) im = [] # ãã¬ã¼ã æ´æ°ã®éã«ååã®ãããããåé¤ããããã«ç¨æ # ã¢ãã¡ã¼ã·ã§ã³ä½æ ani = animation.FuncAnimation(fig, _update_plot, fargs = (fig, im), frames = 360, interval = 1) # 表示 plt.show()
åãã¬ã¼ã ã§æç»ãããç¹ãæ¶ããªããã¢ãã¡ã¼ã·ã§ã³ãããæ¹æ³ãåãããæ©ã¿ã¾ããããplt.scatterã®æ»ãå¤ãremoveããäºã§è§£æ±ºåºæ¥ã¾ãããï¼æ¬å½ã¯ãã£ã¨è¯ãããæ¹ãããã®ããããã¾ãããï¼
ãã¾ã
ãã£ãããªã®ã§ãæã²ã¼ã ã®ãã¬ã¤åç»ãããã¬ã¼ã ãåãåºãã¦ç¹å¾´éæ½åºãããã®ãanimation.ArtistAnimationã§è¡¨ç¤ºãã¦ã¿ã¾ããã
åç»ãããã¬ã¼ã ã®åãåºãã¯ffmpegã便å©ã§ãã
Macã®å ´åã¯
brew install ffmpeg --with-libvorbis --with-libvpx --with-schroedinger
ã§ffmpegãå°å
¥ããå¾ã
ffmpeg -i input.mpeg -f image2 frame%d.jpg
ã¨ããäºã§ãã¬ã¼ã ã®åãåºããåºæ¥ã¾ãã
ã¾ãç¹å¾´éæ½åºã«ã¯opencvã使ãã¾ããã詳ããã¯opencv, pythonã§æ¤ç´¢ãã¦ã¿ã¦ãã ããã
#coding: UTF-8 import cv2 from matplotlib import pyplot as plt import matplotlib.animation as animation fig = plt.figure() ims = [] for i in range(1, 100): name = "frame" + str(i) + ".jpg" img = cv2.imread(name,0) width, height = img.shape # æ¤åºå¨ã®åæå orb = cv2.FastFeatureDetector() # ç¹å¾´éã®æ¤åºã¨åºåç¨ã®è¨ç® kp = orb.detect(img, None) x = [] y = [] for p in kp: x.append(p.pt[0]) y.append(height - p.pt[1]) # opencvã®åº§æ¨ç³»ã¯å·¦ä¸åç¹ãªã®ã§èª¿æ´ im = plt.scatter(x, y) ims.append([im]) # åç»ä¿åã®æºå Writer = animation.writers['ffmpeg'] writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800) ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=1000) #åç»ã¨ãã¦ä¿å ani.save('im.mp4', writer=writer) plt.show()
å®è¡çµæã¯ãããªæãã«ãªãã¾ãã
ãã£ãããã ãã®è¡æ°ã§ããã¾ã§åºæ¥ãã®ã¯å¬ããã§ããã ãããpython!