{"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":"* 每次验证结果的时候都重新分割一次测试数据集实在是太浪费时间了，于是写一段脚本来完成分割，注意kaggle里面是不能批量下载生成的文件的，所以我还是建议你本地执行这个脚本，然后上传成为自己的数据集，以便使用。","metadata":{}},{"cell_type":"code","source":"'''\nimport os\nimport SimpleITK\nimport numpy as np\nimport cv2\nfrom tqdm import tqdm\nimport shutil\n \ndef convert_from_dicom_to_jpg(img,low_window,high_window,save_path):\n    lungwin = np.array([low_window*1.,high_window*1.])\n    newimg = (img-lungwin[0])/(lungwin[1]-lungwin[0])    #归一化\n    newimg = (newimg*255).astype('uint8')                #将像素值扩展到[0,255]\n    stacked_img = np.stack((newimg,) * 3, axis=-1)\n    cv2.imwrite(save_path, stacked_img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])\n \nif __name__ == '__main__':\n    #dicom文件目录\n    dicom_dir = '../input/siim-covid19-detection/test/'\n \n    path = \"./test/study/\"\n    if os.path.exists(path):\n        shutil.rmtree(path)\n    os.makedirs(path)\n    picsum = 0\n    for dirname, _, filenames in tqdm(os.walk(dicom_dir)):\n        for i in filenames:\n            picsum += 1\n            dcm_image_path = os.path.join(dirname,i)  # 读取dicom文件\n            name, _ = os.path.splitext(i)\n            output_jpg_path = os.path.join(path, name+'.png')\n            ds_array = SimpleITK.ReadImage(dcm_image_path)  # 读取dicom文件的相关信息\n            img_array = SimpleITK.GetArrayFromImage(ds_array)  # 获取array\n            # SimpleITK读取的图像数据的坐标顺序为zyx，即从多少张切片到单张切片的宽和高，此处我们读取单张，因此img_array的shape\n            # 类似于 （1，height，width）的形式\n            shape = img_array.shape\n            img_array = np.reshape(img_array, (shape[1], shape[2]))  # 获取array中的height和width\n            high = np.max(img_array)\n            low = np.min(img_array)\n            convert_from_dicom_to_jpg(img_array, low, high, output_jpg_path)  # 调用函数，转换成jpg文件并保存到对应的路径\n    print('图片总数：', picsum)\n'''","metadata":{"execution":{"iopub.status.busy":"2021-06-10T01:17:50.978372Z","iopub.execute_input":"2021-06-10T01:17:50.979056Z","iopub.status.idle":"2021-06-10T01:17:50.993499Z","shell.execute_reply.started":"2021-06-10T01:17:50.978959Z","shell.execute_reply":"2021-06-10T01:17:50.992614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!conda install '/kaggle/input/pydicom-conda-helper/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/conda-4.10.1-py37h89c1867_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/openssl-1.1.1k-h7f98852_0.tar.bz2' -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2021-06-10T01:17:50.995469Z","iopub.execute_input":"2021-06-10T01:17:50.995856Z","iopub.status.idle":"2021-06-10T01:19:11.448109Z","shell.execute_reply.started":"2021-06-10T01:17:50.995810Z","shell.execute_reply":"2021-06-10T01:19:11.446802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm\n\nimport numpy as np\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\ndef read_xray(path, voi_lut = True, fix_monochrome = True):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"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\n\ndef resize(array, size, keep_ratio=False, resample=Image.LANCZOS):\n    # Original from: https://www.kaggle.com/xhlulu/vinbigdata-process-and-resize-to-image\n    im = Image.fromarray(array)\n    \n    if keep_ratio:\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    \n    return im","metadata":{"execution":{"iopub.status.busy":"2021-06-10T01:19:11.450699Z","iopub.execute_input":"2021-06-10T01:19:11.451146Z","iopub.status.idle":"2021-06-10T01:19:11.768168Z","shell.execute_reply.started":"2021-06-10T01:19:11.451089Z","shell.execute_reply":"2021-06-10T01:19:11.767300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split = 'test'\nsave_dir = f'./{split}/'\n\nos.makedirs(save_dir, exist_ok=True)\n\nsave_dir = f'./{split}/study/'\nos.makedirs(save_dir, exist_ok=True)\n\npicsum = 0\nfor dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n    for file in filenames:\n        picsum += 1\n        #print(picsum)\n        # set keep_ratio=True to have original aspect ratio\n        xray = read_xray(os.path.join(dirname, file))\n        im = resize(xray, size=512)  \n        study = dirname.split('/')[-2] + '_study.png'\n        #study = dirname.split('/')[-2] + '_study'\n        im.save(os.path.join(save_dir, study))\nprint(picsum)","metadata":{"execution":{"iopub.status.busy":"2021-06-10T01:20:27.381918Z","iopub.execute_input":"2021-06-10T01:20:27.382266Z","iopub.status.idle":"2021-06-10T01:20:33.725226Z","shell.execute_reply.started":"2021-06-10T01:20:27.382237Z","shell.execute_reply":"2021-06-10T01:20:33.723556Z"},"trusted":true},"execution_count":null,"outputs":[]}]}