{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Classes\n\n> 0 - Aortic enlargement  <br>\n> 1 - Atelectasis  <br>\n> 2 - Calcification  <br>\n> 3 - Cardiomegaly  <br>\n> 4 - Consolidation  <br>\n> 5 - ILD  <br>\n> 6 - Infiltration  <br>\n> 7 - Lung Opacity  <br>\n> 8 - Nodule/Mass  <br>\n> 9 - Other lesion  <br>\n> 10 - Pleural effusion  <br>\n> 11 - Pleural thickening  <br>\n> 12 - Pneumothorax  <br>\n> 13 - Pulmonary fibrosis  <br>\n> 14 - \"No finding\" observation was intended to capture the absence of all findings above <br>\n\n\n`train.csv` -   the train set metatdata,with one row for each object, including a class and a bounding box\n\n\n### Train columns \n`image_id` - unique image indeitifier <br>\n`class_name` - the name of the class of detected object <br>\n`class_id` - the ID of the class of detected object <br>\n`rad_id` - the ID of the radiologist that made the observation<br>\n`x_min` - minimum X coordinate of the object's bounding box<br>\n`y_min` - minimum Y coordinate of the object's bounding box<br>\n`x_max` - maximum X coordinate of the object's bounding box<br>\n`y_max` - maximum Y coordinate of the object's bounding box<br>\n\n\n\n","metadata":{}},{"cell_type":"markdown","source":"## Read Dicom file with Pydicom","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-07-25T12:32:48.68598Z","iopub.execute_input":"2021-07-25T12:32:48.68628Z","iopub.status.idle":"2021-07-25T12:32:48.712357Z","shell.execute_reply.started":"2021-07-25T12:32:48.686252Z","shell.execute_reply":"2021-07-25T12:32:48.710291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_xray(path, voi_lut = True, fix_monochrome = True):\n    \n    dicom = pydicom.read_file(path)\n   \n    if voi_lut: # human friendly view\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n        \n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n\n    print(\"1. before\")\n    print(data)\n    print(\" \")\n    \n    '''\n    data = data - np.min(data)\n    print(\"2. data - np.min(data)\")\n    print(data)\n    print(np.max(data))\n    print(\" \")\n    \n    \n    data = data / np.max(data)\n    print(\"3. data / np.max(data)\")\n    print(data)\n    print(\" \")\n    \n\n    data = (data).astype(np.uint8)\n    print(\"4. (data * 255).astype(np.uint8) \") \n    # float32 ( uint8 : dicom file X )\n    # 12-18bit \n    print(data)\n    print(\" \")\n    '''\n        \n    return data","metadata":{"execution":{"iopub.status.busy":"2021-07-25T12:33:24.344921Z","iopub.execute_input":"2021-07-25T12:33:24.345301Z","iopub.status.idle":"2021-07-25T12:33:24.351343Z","shell.execute_reply.started":"2021-07-25T12:33:24.345268Z","shell.execute_reply":"2021-07-25T12:33:24.350602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = read_xray('../input/vinbigdata-chest-xray-abnormalities-detection/test/002a34c58c5b758217ed1f584ccbcfe9.dicom', fix_monochrome = False)\nplt.figure(figsize = (10,10))\nplt.imshow(img, \"gray\" )","metadata":{"execution":{"iopub.status.busy":"2021-07-25T12:33:31.503018Z","iopub.execute_input":"2021-07-25T12:33:31.50353Z","iopub.status.idle":"2021-07-25T12:33:33.620284Z","shell.execute_reply.started":"2021-07-25T12:33:31.503498Z","shell.execute_reply":"2021-07-25T12:33:33.619618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## With SimpleITK","metadata":{}},{"cell_type":"code","source":"import scipy.ndimage as ndimage\nimport cv2\nimport numpy as np\nimport glob \nimport matplotlib.pyplot as plt\nimport os\nimport SimpleITK as sitk","metadata":{"execution":{"iopub.status.busy":"2021-07-21T04:43:53.909902Z","iopub.execute_input":"2021-07-21T04:43:53.910234Z","iopub.status.idle":"2021-07-21T04:43:53.914716Z","shell.execute_reply.started":"2021-07-21T04:43:53.910207Z","shell.execute_reply":"2021-07-21T04:43:53.913641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_2D = sitk.Image(64, 64, sitk.sitkFloat32)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T04:43:55.428365Z","iopub.execute_input":"2021-07-21T04:43:55.428702Z","iopub.status.idle":"2021-07-21T04:43:55.43202Z","shell.execute_reply.started":"2021-07-21T04:43:55.428672Z","shell.execute_reply":"2021-07-21T04:43:55.431405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm = sitk.ReadImage('../input/vinbigdata-chest-xray-abnormalities-detection/test/002a34c58c5b758217ed1f584ccbcfe9.dicom')","metadata":{"execution":{"iopub.status.busy":"2021-07-21T04:43:55.930269Z","iopub.execute_input":"2021-07-21T04:43:55.930747Z","iopub.status.idle":"2021-07-21T04:43:56.346763Z","shell.execute_reply.started":"2021-07-21T04:43:55.930716Z","shell.execute_reply":"2021-07-21T04:43:56.34593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dcm)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T04:43:56.411806Z","iopub.execute_input":"2021-07-21T04:43:56.412142Z","iopub.status.idle":"2021-07-21T04:43:56.41756Z","shell.execute_reply.started":"2021-07-21T04:43:56.412113Z","shell.execute_reply":"2021-07-21T04:43:56.416552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Width: \", dcm.GetWidth())\nprint(\"Height:\", dcm.GetHeight())\nprint(\"Depth: \", dcm.GetDepth())\nprint(\"Dimension:\", dcm.GetDimension())\nprint(\"Pixel ID: \", dcm.GetPixelIDValue())\nprint(\"Pixel ID Type:\", dcm.GetPixelIDTypeAsString())","metadata":{"execution":{"iopub.status.busy":"2021-07-21T04:44:01.350075Z","iopub.execute_input":"2021-07-21T04:44:01.350402Z","iopub.status.idle":"2021-07-21T04:44:01.359103Z","shell.execute_reply.started":"2021-07-21T04:44:01.350373Z","shell.execute_reply":"2021-07-21T04:44:01.358291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k in dcm.GetMetaDataKeys():\n    v = dcm.GetMetaData(k)\n    print(\"({0}) == \\\"{1}\\\"\".format(k,v))","metadata":{"execution":{"iopub.status.busy":"2021-07-21T04:44:02.405306Z","iopub.execute_input":"2021-07-21T04:44:02.406007Z","iopub.status.idle":"2021-07-21T04:44:02.414582Z","shell.execute_reply.started":"2021-07-21T04:44:02.405959Z","shell.execute_reply":"2021-07-21T04:44:02.413769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"npy = sitk.GetArrayFromImage(dcm)\nprint(npy.shape)\nprint(npy[0])\n#npy = npy.reshape(( 2345,2584)) # W,H,C\nplt.imshow(npy[0],cmap = plt.cm.gray)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T04:47:23.353107Z","iopub.execute_input":"2021-07-21T04:47:23.353481Z","iopub.status.idle":"2021-07-21T04:47:24.049779Z","shell.execute_reply.started":"2021-07-21T04:47:23.353448Z","shell.execute_reply":"2021-07-21T04:47:24.048924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}