{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Heightmap Demo\n", "stough 202-\n", "\n", "In this quick demo I wanted to show how an image can be seen as a height-map, where the xy-plane is the domain of the image, while the z-axis represents the image intensity. This is easy enough with [matplotlib's 3D plotting](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html) capability. Specifically I would refer you to the examples [here](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#surface-plots)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib widget\n", "import matplotlib.pyplot as plt\n", "from matplotlib import cm\n", "from mpl_toolkits.mplot3d import Axes3D\n", "import numpy as np\n", "from skimage import color\n", "\n", "# For importing from alternative directory sources\n", "import sys \n", "sys.path.insert(0, '../dip_utils')\n", "\n", "from matrix_utils import arr_info\n", "from vis_utils import vis_image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "I = color.rgb2gray(plt.imread('../dip_pics/D_image.jpg'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr_info(I)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis_image(I, cmap='gray')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X, Y = np.meshgrid(range(0,I.shape[0]), range(0,I.shape[1]), indexing='ij')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(5,5))\n", "ax = fig.add_subplot(111, projection='3d')\n", "\n", "ax.plot_surface(X, Y, I, cmap=cm.coolwarm);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The argument `projection='3d'` means the `ax` returned is a [Axes3D](https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html) object. We then call that object's `plot_surface` method with a colormap so that the colors change as a function of the image intensity, or z coordinate. \n", "\n", "Note also that 3D plotting normalizes the units for a square output, even while the original image may not be. Lastly, code that does this surface plotting has been added to [vis_utils](../dip_utils/vis_utils.py) if we need it in the future." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 4 }