Skip to content

Commit

Permalink
Update IO tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
sitic committed May 10, 2024
1 parent 1859a67 commit f2fe773
Showing 1 changed file with 118 additions and 12 deletions.
130 changes: 118 additions & 12 deletions docs/tutorials/io.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -380,33 +380,72 @@
"execution_count": null,
"metadata": {
"tags": [
"skip-execution"
"remove-input",
"remove-output"
]
},
"outputs": [],
"source": [
"import optimap as om\n",
"from IPython.display import Video\n",
"video = om.load_video(om.download_example_data(\"Sinus_Rabbit_1.npy\", silent=True))\n",
"video = om.video.rotate_left(video)\n",
"om.video.set_ffmpeg_defaults('libx264', {'-c:v': 'libx264', '-crf': '20', '-preset': 'fast', \"-pix_fmt\": \"yuv420p\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"om.export_video('video.mp4', video)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"Video(filename='video.mp4', embed=True, html_attributes=\"controls autoplay loop\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This will generate a .mp4 video file containing the entire video data at the original resolution with a display framerate of 60fps (default). See {func}`video.export_video` for more options, e.g.:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"om.export_video(\"video.mp4\", video, fps=15, cmap=\"magma\", vmin=0.5, vmax=0.9)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"skip-execution"
"remove-input"
]
},
"outputs": [],
"source": [
"om.export_video(\"video.mp4\", video[100:500], fps=30, skip_frames=2,\n",
" cmap=\"viridis\", vmin=0.1, vmax=0.9)"
"Video(filename='video.mp4', html_attributes=\"controls autoplay loop\", embed=True)"
]
},
{
Expand All @@ -428,8 +467,80 @@
},
"outputs": [],
"source": [
"video_waves = om.video.normalize_pixelwise(video)\n",
"om.video.export_video_with_overlay(\"video.mp4\", video, video_waves)"
"overlay = om.video.normalize_pixelwise(video)\n",
"om.video.export_video_with_overlay(\"video.mp4\", video, overlay=overlay, fps=15)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# video_warped = om.motion_compensate(video, contrast_kernel=5, ref_frame=40)\n",
"# video_waves = om.video.temporal_difference(video_warped, 3)\n",
"# video_waves[video_waves > 0] = 0\n",
"# video_waves = om.video.normalize_pixelwise(video_waves)\n",
"# overlay = 1-video_waves.copy()\n",
"# overlay[overlay < 0.5] = 0\n",
"# overlay[:, om.background_mask(video_warped[0], show=False)] = 0\n",
"# om.video.export_video_with_overlay(\"video.mp4\", video_warped, overlay, vmin_overlay=0, vmax_overlay=0.9, fps=15)\n",
"# Video(filename=\"video.mp4\", html_attributes=\"controls autoplay loop\", embed=True)\n",
"\n",
"video_warped = om.motion_compensate(video, contrast_kernel=5, ref_frame=40)\n",
"overlay = om.video.temporal_difference(video_warped, 3)\n",
"# normalize to range [1, -1], note that we invert we are interested in negative values\n",
"overlay = om.video.normalize_pixelwise(overlay, ymin=1, ymax=-1)\n",
"# set background to zero\n",
"background = om.background_mask(video_warped[0], show=False)\n",
"overlay[:, background] = 0\n",
"alpha = overlay.copy()\n",
"alpha[alpha < 0.5] = 0\n",
"\n",
"overlay_motion = om.video.temporal_difference(video, 3)\n",
"overlay_motion = om.video.normalize_pixelwise(overlay_motion, ymin=1, ymax=-1)\n",
"\n",
"om.video.export_video_with_overlay(\"video.mp4\", video_warped, overlay=overlay, alpha=alpha, fps=15)\n",
"Video(filename=\"video.mp4\", html_attributes=\"controls autoplay loop\", embed=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To export videos side-by-side, use {func}`om.video.export_videos`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"om.video.export_videos(\"video.mp4\", [video, overlay_motion, video_warped, overlay], ncols=2, fps=15,\n",
" cmaps=['gray', 'Purples', 'gray', 'Purples'],\n",
" vmins=0,\n",
" vmaxs=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
" # , padding=26, padding_color=\"white\",\n",
"Video(filename='video.mp4', embed=True, html_attributes=\"controls autoplay loop\")"
]
},
{
Expand Down Expand Up @@ -479,11 +590,6 @@
"om.save_mask(\"mask.png\", mask)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand All @@ -502,7 +608,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.9"
"version": "3.12.2"
},
"test_name": "notebook1"
},
Expand Down

0 comments on commit f2fe773

Please sign in to comment.