{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-05T07:17:25.258388Z","iopub.execute_input":"2022-09-05T07:17:25.258773Z","iopub.status.idle":"2022-09-05T07:17:25.265399Z","shell.execute_reply.started":"2022-09-05T07:17:25.258742Z","shell.execute_reply":"2022-09-05T07:17:25.264458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/input/rsna-2022-cervical-spine-fracture-detection')","metadata":{"execution":{"iopub.status.busy":"2022-09-05T07:29:17.662255Z","iopub.execute_input":"2022-09-05T07:29:17.663017Z","iopub.status.idle":"2022-09-05T07:29:17.674911Z","shell.execute_reply.started":"2022-09-05T07:29:17.662977Z","shell.execute_reply":"2022-09-05T07:29:17.673447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pydicom\nfrom pydicom.data import get_testdata_files\nfilename =('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/1.dcm')\nds = pydicom.dcmread(filename)\npic = ds.pixel_array\nplt.imshow(pic,cmap = plt.cm.gray)\n#print(\"dicom size:\",ds.shape)\nprint(\"pic size:\",pic.shape)\nprint(ds)","metadata":{"execution":{"iopub.status.busy":"2022-09-06T05:22:03.550887Z","iopub.execute_input":"2022-09-06T05:22:03.55197Z","iopub.status.idle":"2022-09-06T05:22:03.828118Z","shell.execute_reply.started":"2022-09-06T05:22:03.551925Z","shell.execute_reply":"2022-09-06T05:22:03.82681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#openCV可用格式\nimport matplotlib.pyplot as plt\nfrom pydicom import dcmread\nfrom pydicom.data import get_testdata_files\n\n# 取得 Pydicom 附帶的 DICOM 測試影像路徑\nfilename =('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/1.dcm')\n#filename = get_testdata_files('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/1.dcm')\n#filename = get_testdata_files(\"1.dcm\")\n\n# 讀取 DICOM 檔案\nds = dcmread(filename)\nds_arr = ds.pixel_array\nlens = ds_arr.shape[0]*ds_arr.shape[1]\narr_temp = np.reshape(ds_arr,(lens,))\nmax_val = max(arr_temp)\nmin_val = min(arr_temp)\nimg_arr = (ds_arr-min_val)/(max_val-min_val)\nplt.imshow(img_arr,cmap = plt.cm.bone)\n\nprint(\"shape:\",ds.pixel_array.shape)\nprint(\"type:\",ds.pixel_array.dtype)\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-06T03:56:05.915085Z","iopub.execute_input":"2022-09-06T03:56:05.915488Z","iopub.status.idle":"2022-09-06T03:56:06.230273Z","shell.execute_reply.started":"2022-09-06T03:56:05.915455Z","shell.execute_reply":"2022-09-06T03:56:06.228879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c_train = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\nc_train","metadata":{"execution":{"iopub.status.busy":"2022-09-05T07:30:28.851977Z","iopub.execute_input":"2022-09-05T07:30:28.852543Z","iopub.status.idle":"2022-09-05T07:30:28.890767Z","shell.execute_reply.started":"2022-09-05T07:30:28.852503Z","shell.execute_reply":"2022-09-05T07:30:28.889846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport pydicom\nimport os\n\narray_of_img = []\ndef read_directory(directory_name):\n     for filename in os.listdir(r\"./\"+directory_name):\n        img = pydicom.dcmread(directory_name + \"/\" + filename)\n        img_arr_temp = img.pixel_array\n        img_arr = np.expand_dims(img_arr_temp, axis=0)\n        img_arr = img_arr.transpose(1,2,0)\n        #print(img_arr.shape)\n        array_of_img.append(img_arr)\n        #print(array_of_img)\n        \nread_directory(\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001\")\ndata_arr = np.array(array_of_img)\n#img_arr = img_arr.transpose(2,0,1)\nprint(data_arr.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-06T05:04:04.828613Z","iopub.execute_input":"2022-09-06T05:04:04.8291Z","iopub.status.idle":"2022-09-06T05:04:05.602249Z","shell.execute_reply.started":"2022-09-06T05:04:04.829061Z","shell.execute_reply":"2022-09-06T05:04:05.600921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install pylibjpeg","metadata":{"execution":{"iopub.status.busy":"2022-09-06T11:41:03.511253Z","iopub.execute_input":"2022-09-06T11:41:03.512181Z","iopub.status.idle":"2022-09-06T11:41:14.694273Z","shell.execute_reply.started":"2022-09-06T11:41:03.512134Z","shell.execute_reply":"2022-09-06T11:41:14.692633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install python-gdcm","metadata":{"execution":{"iopub.status.busy":"2022-09-06T11:41:52.972111Z","iopub.execute_input":"2022-09-06T11:41:52.9726Z","iopub.status.idle":"2022-09-06T11:42:04.122398Z","shell.execute_reply.started":"2022-09-06T11:41:52.972556Z","shell.execute_reply":"2022-09-06T11:42:04.1211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport pydicom\nimport os\nfrom libjpeg import decode\nimport gdcm\n\nfilepath = []\narray_of_img = []\ndef read_all_file(directory_path):\n    for pathname in os.listdir(r\"./\"+directory_path):\n        filepath = directory_path + \"/\" + pathname\n        for filename in os.listdir(r\"./\"+filepath):\n            img = pydicom.dcmread(filepath + \"/\" + filename)         \n            #print(filepath + \"/\" + filename)\n            img.decompress(\"pylibjpeg\")\n            img_arr_temp = img.pixel_array\n            img_arr = np.expand_dims(img_arr_temp, axis=0)\n            img_arr = img_arr.transpose(1,2,0)\n            #print(img_arr.shape)\n            array_of_img.append(img_arr)\n            #print(array_of_img)\n        \nread_all_file(\"../input/rsna-2022-cervical-spine-fracture-detection/train_images\")\n\ndata_arr = np.array(array_of_img)\nprint(data_arr.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-06T11:42:20.255365Z","iopub.execute_input":"2022-09-06T11:42:20.255795Z","iopub.status.idle":"2022-09-06T11:42:20.295941Z","shell.execute_reply.started":"2022-09-06T11:42:20.255763Z","shell.execute_reply":"2022-09-06T11:42:20.294497Z"},"trusted":true},"execution_count":null,"outputs":[]}]}