Files
BM-A0/Tutorial/1-images.ipynb
2024-12-02 20:08:23 +01:00

340 lines
7.9 KiB
Plaintext
Executable File

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working with images"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import imageio.v2 as imageio"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Image loading and display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image = imageio.imread('taj_mahal_gray.jpg')\n",
"\n",
"start, step = 300, 30\n",
"print(f'The image \\n{image}\\nhas {image.shape[0]} rows and {image.shape[1]} columns.')\n",
"print(f'The {step} pixels in the zero-th row, starting at column {start} contain the values \\n{image[0, start:start + step]}.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"_, ax = plt.subplots(1, 2, figsize=(9, 3))\n",
"ax[0].imshow(image)\n",
"ax[1].imshow(image, cmap='gray')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Deep and shallow copy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image = imageio.imread('taj_mahal_gray.jpg')\n",
"\n",
"# Assign another name to the same underlying storage (\"Reference\")\n",
"# copy = image\n",
"\n",
"# Copy the underlying data to a new storage location (\"Copy\")\n",
"copy = image.copy()\n",
"\n",
"copy[200:350, 300:450] = 0\n",
"\n",
"fig, ax = plt.subplots(1, 2, figsize=(9, 3))\n",
"ax[0].imshow(image)\n",
"ax[1].imshow(copy, cmap='gray')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Image statistics & manipulations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"im_integral = imageio.imread('taj_mahal_gray.jpg')\n",
"im_float = im_integral / 255. # normalize grayvalues to [0,1]\n",
"\n",
"fig, axs = plt.subplots(1, 2, figsize=(9, 3))\n",
"for ax, im in zip(axs, [im_integral, im_float]):\n",
" artist = ax.imshow(im, cmap='gray')\n",
" ax.set_title(f'min={im.min()}, max={im.max()}')\n",
" fig.colorbar(artist, ax=ax)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"im = imageio.imread('taj_mahal_small.jpg')\n",
"print(im.shape)\n",
"_, ax = plt.subplots()\n",
"ax.imshow(im)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conversion to grayscale image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"im = im / 255.0\n",
"\n",
"coeffs = np.array([0.299, 0.587, 0.114])\n",
"gray_explicit = im[:,:,0] * 0.299 + im[:,:,1] * 0.587 + im[:,:,2] * 0.114\n",
"gray_broadcasting = (im * coeffs).sum(axis=2)\n",
"assert np.allclose(gray_broadcasting, gray_explicit)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gray_mean = im.mean(-1)\n",
"\n",
"_, ax = plt.subplots(1, 2, figsize=(9, 3))\n",
"ax[0].imshow(gray_broadcasting, cmap='gray')\n",
"ax[1].imshow(gray_mean, cmap='gray')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Image filtering"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import scipy.ndimage as sn\n",
"from skimage.transform import rescale"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load image\n",
"im = imageio.imread('taj_mahal_gray.jpg') / 255.0\n",
"im_aa = rescale(im, 0.2, anti_aliasing=True)\n",
"im_noaa = rescale(im, 0.2, anti_aliasing=False)\n",
"\n",
"_, ax = plt.subplots(1, 2, figsize=(9,3))\n",
"ax[0].imshow(im_aa, cmap='gray')\n",
"ax[0].set_title('AA')\n",
"ax[1].imshow(im_noaa, cmap='gray')\n",
"ax[1].set_title('No AA')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Gaussian filter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sigma = 2.3\n",
"kernel_size = 15\n",
"\n",
"space = np.arange(-kernel_size // 2 + 1, kernel_size // 2 + 1)\n",
"x, y = np.meshgrid(space, space)\n",
"\n",
"kernel = np.exp(-(x ** 2 + y ** 2) / (2 * sigma ** 2))\n",
"kernel = kernel / np.sum(kernel)\n",
"_, ax = plt.subplots(subplot_kw={'projection': '3d'})\n",
"ax.plot_surface(x, y, kernel, cmap='viridis')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Apply gaussian filter to image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# convolution with the gaussian filter\n",
"filtered = sn.convolve(im, kernel)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_, ax = plt.subplots(1, 2, figsize=(9,3));\n",
"ax[0].imshow(im, cmap='gray')\n",
"ax[0].set_title('Original')\n",
"ax[1].imshow(filtered, cmap='gray')\n",
"ax[1].set_title('2D filter')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Edge filter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dx = np.array([\n",
" [-1, 0, 1],\n",
" [-1, 0, 1],\n",
" [-1, 0, 1]\n",
"])\n",
"dy = dx.T\n",
"\n",
"_, ax = plt.subplots(1, 2, figsize=(5,2))\n",
"ax[0].imshow(dx, cmap='gray')\n",
"ax[1].imshow(dy, cmap='gray')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"nabla_x = sn.convolve(im, dx)\n",
"nabla_y = sn.convolve(im, dy)\n",
"grad_magnitude = np.sqrt(nabla_x ** 2 + nabla_y ** 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fi, ax = plt.subplots(1, 3, figsize=(9,3))\n",
"ax[0].imshow(nabla_x, cmap='gray')\n",
"ax[1].imshow(nabla_y, cmap='gray')\n",
"ax[2].imshow(grad_magnitude, cmap='gray')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"nabla_x = sn.convolve(filtered, dx)\n",
"nabla_y = sn.convolve(filtered, dy)\n",
"grad_magnitude = np.sqrt(nabla_x ** 2 + nabla_x ** 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fi, ax = plt.subplots(1, 3, figsize=(9,3))\n",
"ax[0].imshow(nabla_x, cmap='gray')\n",
"ax[1].imshow(nabla_y, cmap='gray')\n",
"ax[2].imshow(grad_magnitude, cmap='gray')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}