{"cells":[{"metadata":{},"cell_type":"markdown","source":"Maybe useful"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n# import os\nimport pydicom\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n# import matplotlib.pyplot as plt\n# from skimage import exposure\n# import cv2\nimport warnings\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### ↓Refer [here](https://www.kaggle.com/trungthanhnguyen0502/eda-vinbigdata-chest-x-ray-abnormalities) "},{"metadata":{"trusted":true},"cell_type":"code","source":"def dicom2array(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)\n    # VOI LUT (if available by DICOM device) is used to\n    # transform raw DICOM data to \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_width_height(dicom_paths):\n    w_h = pd.DataFrame(columns=['id', 'height', 'width'])\n    for path in tqdm(dicom_paths):\n        img = dicom2array(path)\n        temp = path[61:93]\n        temp_df = {'id':temp , 'height':img.shape[0] , 'width':img.shape[1]}\n#         print(temp_df)\n        w_h = w_h.append(temp_df , ignore_index=True)\n#    print(w_h)\n    return w_h","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset_dir = '../input/vinbigdata-chest-xray-abnormalities-detection'\ndicom_paths = glob(f'{dataset_dir}/train/*.dicom')\n#dicom_paths[0][61:93]  # 从文件夹信息获取片子的id","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### ↓Take about **`5.5`** hours."},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n# w = get_width_height(dicom_paths[:15])\nw = get_width_height(dicom_paths)\nw.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"w.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"w.to_csv('./width_height.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"s = pd.read_csv('./width_height.csv',index_col = 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"s.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## But this way to get width and height maybe cause loss of information from **`.dicom`**.\n## To be verified..."}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}