{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Convert falseColor\n\nThis demo shows how colored images are converted into other color spaces..\n\n``img1`` contains an rgba-image with false color encoded color information and gray values\nthe gray values stay like they are, the false color values are decoded in ``HSV`` color space\nand gray values with respect to their hue are determined. The resulting image is called ``img2``.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import colorsys\nimport itom\nfrom itom import dataObject\nfrom itom import plot\n\n\nimg1 = dataObject.rand([200, 200], \"rgba32\")\n[height, width] = img1.shape\nimg2 = img1.copy()\n\nplot(img1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for m in range(height):\n for n in range(width):\n px = img1[m, n]\n [h, s, v] = colorsys.rgb_to_hsv(px.r / 255.0, px.g / 255.0, px.b / 255.0)\n if s > 0.3:\n h_ = int(v * h * 255)\n img2[m, n] = itom.rgba(h_, h_, h_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "img1 contains an rgba-image with false color encoded color information and gray values\nthe gray values stay like they are, the false color values are decoded in HSV color space\nand gray values with respect to their hue are determined. The resulting image is called img2\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "[height, width] = img1.shape\nimg2 = img1.copy()\n\nresult_map = \"hotIron\" # 'gray' or 'hotIron'\n\nif result_map == \"hotIron\":\n map = []\n for i in range(0, 256):\n map.append(itom.rgba(i, 0, 0))\n for i in range(0, 256):\n map.append(itom.rgba(255, i, 0))\n for i in range(0, 256):\n map.append(itom.rgba(255, 255, i))\n map_len = len(map)\n\nfor m in range(height):\n for n in range(width):\n px = img1[m, n]\n [h, s, v] = colorsys.rgb_to_hsv(px.r / 255.0, px.g / 255.0, px.b / 255.0)\n if s > 0.3:\n if result_map == \"gray\":\n h_ = int(v * h * 255)\n img2[m, n] = itom.rgba(h_, h_, h_)\n elif result_map == \"hotIron\":\n s_ = int(v * 255)\n img2[m, n] = map[int(h * map_len)] * itom.rgba(s_, s_, s_)\n else:\n raise RuntimeError(\"map must be gray or hotIron\")\n\nplot(img2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] } ], "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.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }