{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## geom_imshow()\n", "\n", "`geom_imshow()` displays an image specified by 2D or 3D Numpy array.\n", "\n", "Whether the image is grayscale or color depends on the shape of the image array:\n", "- (M, N) - grey-scale image\n", "- (M, N, 3) - color RGB image\n", "- (M, N, 4) - color RGB image with alpha channel\n", "\n", "The array's `dtype` can be int, uint or float.\n", "\n", "By default, all values in the image array will be transformed to the range [0-255] using a linear scaler." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:32:19.572978Z", "iopub.status.busy": "2024-04-17T07:32:19.572889Z", "iopub.status.idle": "2024-04-17T07:32:19.889348Z", "shell.execute_reply": "2024-04-17T07:32:19.889060Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Grayscale image\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:32:19.903534Z", "iopub.status.busy": "2024-04-17T07:32:19.903399Z", "iopub.status.idle": "2024-04-17T07:32:19.935614Z", "shell.execute_reply": "2024-04-17T07:32:19.935412Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A2x3 = np.array([\n", " [50, 150 ,200],\n", " [200,100,50]\n", " ])\n", "\n", "ggplot() + geom_imshow(A2x3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Grayscale image without normalization" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:32:19.936889Z", "iopub.status.busy": "2024-04-17T07:32:19.936784Z", "iopub.status.idle": "2024-04-17T07:32:19.939294Z", "shell.execute_reply": "2024-04-17T07:32:19.939120Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_imshow(A2x3, norm=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Grayscale image in \"Viridis\" colors" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:32:19.940532Z", "iopub.status.busy": "2024-04-17T07:32:19.940367Z", "iopub.status.idle": "2024-04-17T07:32:19.944817Z", "shell.execute_reply": "2024-04-17T07:32:19.944635Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_imshow(A2x3, cmap=\"viridis\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RGB image\n", "\n", "M x N x 3 array" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:32:19.945895Z", "iopub.status.busy": "2024-04-17T07:32:19.945752Z", "iopub.status.idle": "2024-04-17T07:32:19.948138Z", "shell.execute_reply": "2024-04-17T07:32:19.947961Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A2x3x3 = np.array([\n", " [[255, 0, 0], [0, 255, 0], [0, 0, 255]],\n", " [[0, 255, 0], [0, 0, 255], [255, 0, 0]]\n", " ])\n", "ggplot() + geom_imshow(A2x3x3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RGB image with alpha channel\n", "\n", "M x N x 4 array" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:32:19.949176Z", "iopub.status.busy": "2024-04-17T07:32:19.949063Z", "iopub.status.idle": "2024-04-17T07:32:19.951659Z", "shell.execute_reply": "2024-04-17T07:32:19.951488Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A2x3x4 = np.array([\n", " [[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]],\n", " [[0, 1, 0, 0.3], [0, 0, 1, 0.3], [1, 0, 0, 0.3]]\n", " ])\n", "\n", "ggplot() + geom_imshow(A2x3x4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Just a random image" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:32:19.952617Z", "iopub.status.busy": "2024-04-17T07:32:19.952504Z", "iopub.status.idle": "2024-04-17T07:32:19.955230Z", "shell.execute_reply": "2024-04-17T07:32:19.955060Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(42)\n", "image = np.random.choice([0.0, 1.0], [10, 100, 3])\n", "ggplot() + geom_imshow(image) + coord_cartesian()\n" ] } ], "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": 1 }