{"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":"I have spent some time trying to figure out how to convert the test images and get them to match the converted training images already offered in various datasets.\n\nIt may already be offered (I am too lazy to check and I guess a good thing cannot be repeated too often) but here is the code to convert and crop if desired the images in this competition.\n\nUses dicomsdl for fast processing. I've shown in this notebook how to use the cool \"Frozen packages for offline use\" (https://www.kaggle.com/code/hey24sheep/frozen-packages-for-offline-use) for this offline competition.\n\nExpect the notebook to run for quite some time.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom PIL import Image\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-12-20T21:20:53.070608Z","iopub.execute_input":"2022-12-20T21:20:53.071113Z","iopub.status.idle":"2022-12-20T21:20:53.092698Z","shell.execute_reply.started":"2022-12-20T21:20:53.07102Z","shell.execute_reply":"2022-12-20T21:20:53.091746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install dicomsdl --find-links /kaggle/input/frozen-packages-for-offline-use/frozen_packages --no-index\nimport dicomsdl","metadata":{"execution":{"iopub.status.busy":"2022-12-20T21:20:53.094964Z","iopub.execute_input":"2022-12-20T21:20:53.095327Z","iopub.status.idle":"2022-12-20T21:21:05.511095Z","shell.execute_reply.started":"2022-12-20T21:20:53.095271Z","shell.execute_reply":"2022-12-20T21:21:05.510041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\n\nIMAGE_PATH = \"/kaggle/input/rsna-breast-cancer-detection/train_images/\"\nOUTPUT_PATH = \"/kaggle/working/output_images/\"\nIMAGE_DIM = 1024\nIMAGE_DEPTH = 3\nCROP_THRESHOLD = 20","metadata":{"execution":{"iopub.status.busy":"2022-12-20T21:21:05.512787Z","iopub.execute_input":"2022-12-20T21:21:05.513596Z","iopub.status.idle":"2022-12-20T21:21:05.633051Z","shell.execute_reply.started":"2022-12-20T21:21:05.513557Z","shell.execute_reply":"2022-12-20T21:21:05.632239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_image(image_array, crop):\n    min_x = 10000\n    min_y = 10000\n    max_x = 0\n    max_y = 0\n\n    i = 0\n    for val in np.amax(image_array, axis=0):\n        if val > CROP_THRESHOLD:\n            if min_x > i:\n                min_x = i\n            if max_x < i:\n                max_x = i\n        i += 1\n\n    i = 0\n    for val in np.amax(image_array, axis=1):\n        if val > CROP_THRESHOLD:\n            if min_y > i:\n                min_y = i\n            if max_y < i:\n                max_y = i\n        i += 1\n\n    img = np.zeros((image_array.shape[0], image_array.shape[1], 3)).astype(np.uint8)\n    img[:, :, 0] = image_array\n    img[:, :, 1] = image_array\n    img[:, :, 2] = image_array\n\n    if crop:\n        return Image.fromarray(img).crop((min_x, min_y, max_x, max_y)).resize((IMAGE_DIM, IMAGE_DIM), Image.NEAREST)\n    else:\n        return Image.fromarray(img).resize((IMAGE_DIM, IMAGE_DIM), Image.NEAREST)","metadata":{"execution":{"iopub.status.busy":"2022-12-20T21:21:05.63495Z","iopub.execute_input":"2022-12-20T21:21:05.635552Z","iopub.status.idle":"2022-12-20T21:21:05.644421Z","shell.execute_reply.started":"2022-12-20T21:21:05.635505Z","shell.execute_reply":"2022-12-20T21:21:05.643443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In the call to crop_image you can set crop=False if you don't want any cropping performed, only resizing.","metadata":{}},{"cell_type":"code","source":"def convert_train_images():\n    if not os.path.isdir(OUTPUT_PATH):\n        os.mkdir('output_images')\n\n    for index, row in train_df.iterrows():\n        dicom1 = dicomsdl.open(IMAGE_PATH + str(row['patient_id']) + '/' + str(row['image_id']) + '.dcm')\n        image1 = dicom1.pixelData(storedvalue=True)\n\n        img_max = np.amax(image1)\n        img_min = np.amin(image1)\n        img = ((image1 - img_min) / (img_max - img_min)) * 255\n\n        if img.mean() > 128:  # Inverted image detected. Invert again\n            img = 255 - img\n\n        img = img.astype(np.uint8)\n        img = crop_image(img, crop=True)\n\n        if not os.path.isdir(OUTPUT_PATH + str(row['patient_id']) + '/'):\n            os.mkdir(OUTPUT_PATH + str(row['patient_id']) + '/')\n        img.save(OUTPUT_PATH + str(row['patient_id']) + '/' + str(row['image_id']) + '.png')\n        \n        # remove the code below to process all images rather than the first few\n        if row['patient_id'] == 10102:\n            break","metadata":{"execution":{"iopub.status.busy":"2022-12-20T21:21:05.645683Z","iopub.execute_input":"2022-12-20T21:21:05.646745Z","iopub.status.idle":"2022-12-20T21:21:05.657571Z","shell.execute_reply.started":"2022-12-20T21:21:05.646696Z","shell.execute_reply":"2022-12-20T21:21:05.656366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convert_train_images()","metadata":{"execution":{"iopub.status.busy":"2022-12-20T21:21:05.658855Z","iopub.execute_input":"2022-12-20T21:21:05.659175Z","iopub.status.idle":"2022-12-20T21:22:25.762135Z","shell.execute_reply.started":"2022-12-20T21:21:05.659147Z","shell.execute_reply":"2022-12-20T21:22:25.760836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}