|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "01f63883", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Trichromatic Neural Étendue Expansion Experimental Code" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "ac28b847", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "### This notebook can be used to produce the trichromatic étendue expanded experimental holograms shown in the manuscript and in the supplementary information.\n", |
| 17 | + "\n", |
| 18 | + "### In the cells below please select one expander type and one target image. For example, to produce a 64x étendue expanded hologram with the neural étendue expander please select 'neural_tri_64x'. To produce a 16x étendue expanded hologram with a random expander [Kuo et al. 2020] please select 'random_16x'. To produce a conventional hologram [Shi et al. 2021] please select 'conventional_16x' or 'conventional_64x'. The target images provided are labeled as '000.png', '001.png', and so on." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "id": "8ef8193c", |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "import torch\n", |
| 29 | + "import matplotlib.pyplot as plt\n", |
| 30 | + "import numpy as np\n", |
| 31 | + "import os\n", |
| 32 | + "from imageio import imread" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": null, |
| 38 | + "id": "908f9ffd", |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "### --- BEGIN CONFIG --- ###\n", |
| 43 | + "# Choose only one of the following expanders.\n", |
| 44 | + "#expander_type = 'conventional_16x'\n", |
| 45 | + "#expander_type = 'conventional_64x'\n", |
| 46 | + "#expander_type = 'random_16x'\n", |
| 47 | + "#expander_type = 'random_64x'\n", |
| 48 | + "#expander_type = 'neural_tri_16x'\n", |
| 49 | + "expander_type = 'neural_tri_64x'\n", |
| 50 | + "\n", |
| 51 | + "# Choose only one of the following target images.\n", |
| 52 | + "#target_img_name = '000'\n", |
| 53 | + "target_img_name = '001'\n", |
| 54 | + "### --- END CONFIG --- ###\n", |
| 55 | + "\n", |
| 56 | + "if (expander_type == 'random_16x') or (expander_type == 'neural_tri_16x'):\n", |
| 57 | + " correction_factor = np.array([1.0,1.0,1.2])\n", |
| 58 | + " top_percentile = 99.9\n", |
| 59 | + " eff_corners = None\n", |
| 60 | + "elif (expander_type == 'conventional_16x'):\n", |
| 61 | + " correction_factor = np.array([1.0,1.0,1.2])\n", |
| 62 | + " top_percentile = 99.9\n", |
| 63 | + " eff_corners = [144,144,240,240]\n", |
| 64 | + "elif (expander_type == 'random_64x') or (expander_type == 'neural_tri_64x'):\n", |
| 65 | + " correction_factor = np.array([1.0,1.1,1.6])\n", |
| 66 | + " top_percentile = 99.0\n", |
| 67 | + " eff_corners = None\n", |
| 68 | + "elif (expander_type == 'conventional_64x'):\n", |
| 69 | + " correction_factor = np.array([1.0,1.1,1.2])\n", |
| 70 | + " top_percentile = 99.9\n", |
| 71 | + " eff_corners = [336,336,432,432]\n", |
| 72 | + "else:\n", |
| 73 | + " assert('Undefined expander.')\n", |
| 74 | + "print(eff_corners)" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": null, |
| 80 | + "id": "f1b94ccc", |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "def get_target_img(img_name):\n", |
| 85 | + " img = imread(img_name)\n", |
| 86 | + " if img.dtype == 'uint8':\n", |
| 87 | + " img = img.astype(np.float32) / 255.0\n", |
| 88 | + " elif img.dtype == 'uint16':\n", |
| 89 | + " img = img.astype(np.float32) / 65535.0\n", |
| 90 | + " elif img.dtype == 'float32':\n", |
| 91 | + " img = img / 1.0\n", |
| 92 | + " else:\n", |
| 93 | + " assert('Invalid image type')\n", |
| 94 | + " if len(img.shape) == 2:\n", |
| 95 | + " img = np.stack([img, img, img], axis=-1)\n", |
| 96 | + " return img\n", |
| 97 | + "\n", |
| 98 | + "def white_balance(cap_img, target_img, correction_factor, top_percentile, eff_corners = None):\n", |
| 99 | + " # Normalize so that max == 1.\n", |
| 100 | + " # We are only adjusting the ratios between the colors, not the overall brightness.\n", |
| 101 | + " if eff_corners == None:\n", |
| 102 | + " scale_factors = np.mean(target_img, axis=(0,1)) / np.mean(cap_img, axis=(0,1))\n", |
| 103 | + " else:\n", |
| 104 | + " cap_img_eff = cap_img[eff_corners[0]:eff_corners[2],eff_corners[1]:eff_corners[3],:]\n", |
| 105 | + " scale_factors = np.mean(target_img, axis=(0,1)) / np.mean(cap_img_eff, axis=(0,1))\n", |
| 106 | + "\n", |
| 107 | + " scale_factors = scale_factors / np.max(scale_factors)\n", |
| 108 | + " cap_img = cap_img * scale_factors\n", |
| 109 | + "\n", |
| 110 | + " # Additional manual color balancing.\n", |
| 111 | + " cap_img = cap_img * correction_factor\n", |
| 112 | + "\n", |
| 113 | + " # Scale overall brightness so that top percentile of pixels are clipped.\n", |
| 114 | + " top_scale = np.percentile(cap_img, top_percentile)\n", |
| 115 | + " cap_img = cap_img / top_scale\n", |
| 116 | + " return np.clip(cap_img, 0.0, 1.0)" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "id": "41a8642c", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "### Display Intensity Scaled Experimentally Captured Hologram ###" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": null, |
| 130 | + "id": "8acda60c", |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [ |
| 134 | + "target_img = get_target_img(os.path.join('Target_Images',target_img_name+'.png'))\n", |
| 135 | + "r_cap = np.load(os.path.join('Data',expander_type,'r',target_img_name+'.npy'))\n", |
| 136 | + "g_cap = np.load(os.path.join('Data',expander_type,'g',target_img_name+'.npy'))\n", |
| 137 | + "b_cap = np.load(os.path.join('Data',expander_type,'b',target_img_name+'.npy'))\n", |
| 138 | + "rgb_cap = np.stack([r_cap, g_cap, b_cap], axis=-1)\n", |
| 139 | + "rgb_cap_wb = np.fliplr(white_balance(rgb_cap, target_img, correction_factor, top_percentile, eff_corners))\n", |
| 140 | + "\n", |
| 141 | + "plt.figure()\n", |
| 142 | + "plt.title('Experimentally Captured Hologram')\n", |
| 143 | + "plt.imshow(rgb_cap_wb)" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": null, |
| 149 | + "id": "7fc758bd", |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [], |
| 152 | + "source": [] |
| 153 | + } |
| 154 | + ], |
| 155 | + "metadata": { |
| 156 | + "kernelspec": { |
| 157 | + "display_name": "Python 3 (ipykernel)", |
| 158 | + "language": "python", |
| 159 | + "name": "python3" |
| 160 | + }, |
| 161 | + "language_info": { |
| 162 | + "codemirror_mode": { |
| 163 | + "name": "ipython", |
| 164 | + "version": 3 |
| 165 | + }, |
| 166 | + "file_extension": ".py", |
| 167 | + "mimetype": "text/x-python", |
| 168 | + "name": "python", |
| 169 | + "nbconvert_exporter": "python", |
| 170 | + "pygments_lexer": "ipython3", |
| 171 | + "version": "3.10.10" |
| 172 | + } |
| 173 | + }, |
| 174 | + "nbformat": 4, |
| 175 | + "nbformat_minor": 5 |
| 176 | +} |
0 commit comments