{"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":"**Digital Imaging and Communications in Medicine (DICOM) is the standard for the communication and management of medical imaging information and related data.**\n\n\nDicom data is not human friendly like jpg or png images.A DICOM file predominantly consists of a header and image pixel intensity data packed into a single file. \n\nThe information within the header is organized as a standardized series of tags. By extracting data from these tags one can access important information regarding the patient demographics, study parameters and a lot more\n\npydicom is a pure Python package for working with DICOM files\n","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\n\n\ndef read_xray(path, voi_lut = True, fix_monochrome = True):\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to 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               \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        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n        \n    return data\n","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:16:20.208385Z","iopub.execute_input":"2021-05-22T17:16:20.208778Z","iopub.status.idle":"2021-05-22T17:16:20.408091Z","shell.execute_reply.started":"2021-05-22T17:16:20.208693Z","shell.execute_reply":"2021-05-22T17:16:20.407376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = read_xray('../input/siim-covid19-detection/train/00b33b3eb8d9/6c8b814c685b/12a2dfb55b6f.dcm')\nplt.figure(figsize = (12,12))\nplt.imshow(img, 'gray')","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:18:55.770196Z","iopub.execute_input":"2021-05-22T17:18:55.770619Z","iopub.status.idle":"2021-05-22T17:18:57.692659Z","shell.execute_reply.started":"2021-05-22T17:18:55.770577Z","shell.execute_reply":"2021-05-22T17:18:57.687765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"what if we do not fixed monochrome issue?","metadata":{}},{"cell_type":"code","source":"img = read_xray('../input/siim-covid19-detection/train/00b33b3eb8d9/6c8b814c685b/12a2dfb55b6f.dcm', fix_monochrome = False)\nplt.figure(figsize = (12,12))\nplt.imshow(img, 'gray')","metadata":{"execution":{"iopub.status.busy":"2021-05-22T17:19:15.129273Z","iopub.execute_input":"2021-05-22T17:19:15.129607Z","iopub.status.idle":"2021-05-22T17:19:16.265163Z","shell.execute_reply.started":"2021-05-22T17:19:15.129577Z","shell.execute_reply":"2021-05-22T17:19:16.264165Z"},"trusted":true},"execution_count":null,"outputs":[]}]}