{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "\n", "In the film industry, it is often necessary to impose actors on top of a rendered background. To do that, the actors are filmed on a \"green screen\". Here's an example shot (``images/greenscreen.jpg``):\n", "\n", "\n", "\n", "Say we'd like to help these folks travel into a forest (``skimage/forest.jpg``):\n", "\n", "\n", "\n", "Can you transplant the foreground of the greenscreen onto the backdrop of the forest?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from skimage import io\n", "\n", "forest = io.imread('skimage/forest.jpg')\n", "people = io.imread('skimage/greenscreen.jpg')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "forest.shape, people.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "forest = forest[:375, :500, :]\n", "\n", "from skimage import img_as_float\n", "forest = img_as_float(forest)\n", "people = img_as_float(people)\n", "\n", "print(forest.max(), people.max())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mask = people[..., 1] < 0.7" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(mask)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "people_selected = people.copy()\n", "#people_selected[mask] = 0\n", "\n", "# Should clean up mask a bit here to fill all the holes\n", "from skimage import morphology\n", "mask = morphology.binary_closing(mask, morphology.selem.disk(15))\n", "#mask = morphology.binary_erosion(mask, morphology.selem.disk(5))\n", "\n", "plt.imshow(mask)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "forest_with_ppl = forest.copy()\n", "forest_with_ppl[mask] = people[mask]\n", "\n", "plt.imshow(forest_with_ppl)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6rc1" } }, "nbformat": 4, "nbformat_minor": 1 }