{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "\n", "\n", "| - | - |\n", "|-----------------------------------------------------------------|-----------------------------------------------------------------|\n", "| [Exercise 11 (to grayscale)](<#Exercise-11-(to-grayscale)>) | [Exercise 12 (radial fade)](<#Exercise-12-(radial-fade)>) |\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Image processing\n", "\n", "An image is a collection of *pixels*, which is abbreviation for picture elements.\n", "A grayscale image can be represented as as two dimensional array, whose first axis corresponds to the x coordinate of the image and the second axis corresponds to the y coordinate. The array contains at each coordinate pair (x,y) a value, which is typically a float between 0.0 and 1.0, or an integer between 0 and 255. This specifies the level of grayness. For example, if the array contains value 255 at coordinates (0,0), then in the image the pixel at top left is white.\n", "\n", "In color images there is third axis is for the red, green, and blue components of each pixel. For each of these color components there is a value between 0.1 and 1.0 (or between 0 and 255). The combinations of different values for the three components red, green, and blue can result in at least 16.7 million colors.\n", "\n", "Since images can be represented as multidimensional arrays, we can easily process images using NumPy functions. Let's see examples of these." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "painting=plt.imread(\"painting.png\")\n", "print(painting.shape)\n", "print(f\"The image consists of {painting.shape[0] * painting.shape[1]} pixels\")\n", "plt.imshow(painting);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because the image is now a NumPy array, we can easily perform some operations:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(painting[:,::-1]); # mirror the image in x direction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the following we set the pixels on the first 30 rows white:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "painting2 = painting.copy() # don't mess the original painting!\n", "painting2[0:30, :, :] = 1.0 # max value for all three components produces white\n", "plt.imshow(painting2);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a bit more complicated operation we can create a function that returns a copy of the image so that it fades to black as we move to left." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def fadex(image):\n", " height, width = image.shape[:2]\n", " m=np.linspace(0,1, width).reshape(1,width,1)\n", " result = image*m # note that we rely on broadcasting here\n", " return result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "modified=fadex(painting)\n", "print(modified.shape)\n", "plt.imshow(modified);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####