使用python证明细菌繁殖按指数增长
2017-11-19 19:25
281 查看
""" This script produces a video analysis proving for the first time in history that bacterial populations grow exponentially. See the result here: http://i.imgur.com/uoITKiA.gif We proceed as follows: - Download a video of bacteria growing under a microscope. - Cut the video to keep only the first 7 seconds. - Threshold each frame to find where the bacteria are, and compute the total number of pixels that they occupy - Make an animated plot of the number of pixels occupied versus time - Assemble the original video, the thresholded version, and the plot, and write everything to a file. I don't know the source of the original video so I can't give credits, sorry guys ! """ import numpy as np import matplotlib.pyplot as plt from moviepy.video.io.bindings import mplfig_to_npimage from moviepy.editor import VideoFileClip, VideoClip, clips_array # DOWLOAD THE VIDEO Requires Youtube-dl installed. import os os.system("youtube-dl gEwzDydciWc -o growth.mp4") # requires Youtube-dl # LOAD THE VIDEO, SELECT THE EXCERPT BETWEEN t=0-7 seconds video = VideoFileClip("Bacteria Growth.mp4", audio=False).subclip(0,7) # THRESHOLD THE FRAMES TO GET THE POPULATION AREA # We normalize each frame by the luminosity of its upper left corner # to correct for the changing luminosity of the video thresholder = lambda im: ( 1.0*im[:,:,1]/np.mean(im[:50,:50]) ) < 0.8 thresholded = video.fl_image(thresholder) thresholded_RGB = thresholded.set_ismask(True).to_RGB() # RGB Version for movie areas = [(t,frame.sum()) for (t,frame) in thresholded.iter_frames(with_times=True)] # ESTIMATE THE GROWTH RATE tt, aa = [np.array(e) for e in zip(*areas)] # times and areas gr_rate, a0 = np.polyfit(tt, np.log(aa),1) # PLOT THE FIGURE fig, ax = plt.subplots(1, figsize=(3,2.5), facecolor=(1,1,1)) ax.plot(tt, np.exp(a0+gr_rate*tt), lw=2, ls='--', c='k', label = r"$Ae^{rt}$") l, = ax.plot(tt, aa, lw=3, c='r') ax.set_ylabel("Pop. surface (pixels)") ax.set_xlabel("time (unit unknown)") ax.legend(loc=2) fig.tight_layout() # <- I love this function :D # ANIMATE THE FIGURE WITH MOVIEPY def plot_until_t(t): tt, aa = zip(*[(t_,v) for (t_,v) in areas if t_<=t]) l.set_xdata(tt) l.set_ydata(aa) return mplfig_to_npimage(fig) plotclip = VideoClip(plot_until_t, duration=video.duration) # ASSEMBLE ALL THE CLIPS, WRITE TO A FILE final_clip = clips_array([[clip.margin(2, color=[255,255,255]) for clip in [video.resize(.6), thresholded_RGB.resize(.6), plotclip]]], bg_color=[255,255,255]) final_clip.write_videofile('growth2.mp4', fps=15) #final_clip.write_gif('growth.gif', fps=15, opt="OptimizeTransparency", fuzz=10)
由于不能传mp4格式文件:源文件,处理后文件我都放在我的网盘里。http://pan.baidu.com/s/1miGaR3a
相关文章推荐
- 使用python实现细菌繁殖的算法代码,欢迎拍砖讨论!
- Python使用量继续增长本年最受欢迎
- 数据挖掘 关联规则的FP-growth-tree(FP增长树)的python实现 使用方法
- Python使用Com组件及Access查询分析类实现
- 在 Python中使用 cout << 'Hello,world!' << endl
- 使用python发送简单的邮件
- 在python 中使用 windows dll
- Python模块之chardir: 使用chartdir生成各种图表
- unix下面使用python无法广播
- 在python 中使用正则表达式
- 使用SWIG实现C++扩展Python
- 关于在组件GIS开发中使用Python的一点补充说明
- 使用minidom来处理XML的示例(Python 学习)(转载)
- C Java PHP Perl Python 的程序代码美化工具(Pretty Print Program/Source Code Beautifier)使用
- 在IE中使用Python作为开发脚本
- 第一次使用python
- python中使用状态机的简单例子---截取'/x00'之前的字符串
- 使用Python进行AES加密和解密的示例代码
- ctypes: 使用python调用C编写的动态链接库
- python 进行多线程编程,使用Semaphore控制线程数