Last active
October 19, 2022 12:17
-
-
Save enakai00/2b42aaef56df1ad66028b156b56f1357 to your computer and use it in GitHub Desktop.
windowing_test.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/enakai00/2b42aaef56df1ad66028b156b56f1357/windowing_test.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt" | |
], | |
"metadata": { | |
"id": "20OegdHJYXyD" | |
}, | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"ImageDepth = 65535 # 画像データの解像度 [0, ImageDepth]\n", | |
"DisplayDepth = 4096 # ディスプレイの解像度 [0, DisplayDepth]" | |
], | |
"metadata": { | |
"id": "zaEe2sICCnWO" | |
}, | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 補正してから反転\n", | |
"def window_invert(image, wc, ww):\n", | |
" iinfo = np.iinfo(image.dtype)\n", | |
" bottom = wc - ww // 2\n", | |
" top = wc + ww // 2\n", | |
" image = np.clip(image, bottom, top) # [bottom, top] の外側を切り詰める\n", | |
" image = np.interp(image, (bottom, top), (0, DisplayDepth)) # [bottom, top] を [0, DisplayDepth] に拡大する(ここで、丸めが生じる)\n", | |
" image = DisplayDepth - image # 全体を反転する\n", | |
" return image.astype(iinfo)" | |
], | |
"metadata": { | |
"id": "SG8tRHO0ZPBP" | |
}, | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 反転してから補正\n", | |
"def invert_window(image, wc, ww):\n", | |
" iinfo = np.iinfo(image.dtype)\n", | |
" image = ImageDepth - image # 全体を反転する\n", | |
" wc = ImageDepth - wc # WindowCenter も対応させて反転する\n", | |
" bottom = wc - ww // 2\n", | |
" top = wc + ww // 2\n", | |
" image = np.clip(image, bottom, top) # [bottom, top] の外側を切り詰める\n", | |
" image = np.interp(image, (bottom, top), (0, DisplayDepth)) # [bottom, top] を [0, DisplayDepth] に拡大する(ここで、丸めが生じる)\n", | |
" return image.astype(iinfo)" | |
], | |
"metadata": { | |
"id": "HlmI7n86YfJ_" | |
}, | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"wc = 3308\n", | |
"ww = 2048\n", | |
"image = np.random.randint(0, ImageDepth, (255, 255)) # 乱数で画像を生成\n", | |
"image0 = window_invert(image.copy(), wc, ww) # 補正してから反転\n", | |
"image1 = invert_window(image.copy(), wc, ww) # 反転してから補正" | |
], | |
"metadata": { | |
"id": "RlrUIt-JaM7a" | |
}, | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"(image0 == image1).mean() # 2つの結果の一致度を計算 (100% 一致で 1.0)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "DH4k68Ofb5wE", | |
"outputId": "465ee4d4-5c3b-4e1a-e215-cef135bdcce7" | |
}, | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"1.0" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 8 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment