{"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":"import json\n\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport pydicom\n\nfrom keras import layers\nfrom keras.applications import DenseNet121, ResNet50V2, InceptionV3\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import Callback, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.initializers import Constant\nfrom keras.utils import Sequence\nfrom keras.models import Sequential\nfrom keras.optimizers import Adam\nfrom keras.models import Model, load_model\nfrom keras.layers import GlobalAveragePooling2D, Dense, Activation, concatenate, Dropout\nfrom keras.initializers import glorot_normal, he_normal\nfrom keras.regularizers import l2\n\nimport keras.metrics as M\nimport tensorflow_addons as tfa\nimport pickle\n\nfrom keras import backend as K\n\nimport tensorflow as tf\nfrom tensorflow.python.ops import array_ops\n\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split, StratifiedKFold\n\nimport warnings\nwarnings.filterwarnings(action='once')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-25T16:10:54.39544Z","iopub.execute_input":"2023-10-25T16:10:54.395843Z","iopub.status.idle":"2023-10-25T16:10:54.407906Z","shell.execute_reply.started":"2023-10-25T16:10:54.395813Z","shell.execute_reply":"2023-10-25T16:10:54.406454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ndf = pd.read_csv('/kaggle/input/labelout/sop_instance_mapping (1).csv')\n\ntrain_df, test_df = train_test_split(df, test_size=0.2, random_state=46)\n\ntrain_df = train_df[[\"Image_Path\",\"labelName\",\"data\"]]\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-25T16:10:54.410513Z","iopub.execute_input":"2023-10-25T16:10:54.411005Z","iopub.status.idle":"2023-10-25T16:10:54.481154Z","shell.execute_reply.started":"2023-10-25T16:10:54.410942Z","shell.execute_reply":"2023-10-25T16:10:54.480251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def map_to_gradient(grey_img):\n    rainbow_img = np.zeros((grey_img.shape[0], grey_img.shape[1], 3))\n    rainbow_img[:, :, 0] = np.clip(4 * grey_img - 2, 0, 1.0) * (grey_img > 0) * (grey_img <= 1.0)\n    rainbow_img[:, :, 1] =  np.clip(4 * grey_img * (grey_img <=0.75), 0,1) + np.clip((-4*grey_img + 4) * (grey_img > 0.75), 0, 1)\n    rainbow_img[:, :, 2] = np.clip(-4 * grey_img + 2, 0, 1.0) * (grey_img > 0) * (grey_img <= 1.0)\n    return rainbow_img\n\ndef rainbow_window(dcm):\n    grey_img = window_image(dcm, 40, 80)\n    return map_to_gradient(grey_img)\n\ndef sigmoid_window(dcm, window_center, window_width, U=1.0, eps=(1.0 / 255.0)):\n    img = dcm.pixel_array\n    img = cp.array(np.array(img))\n    _, _, intercept, slope = get_windowing(dcm)\n    img = img * slope + intercept\n    ue = cp.log((U / eps) - 1.0)\n    W = (2 / window_width) * ue\n    b = ((-2 * window_center) / window_width) * ue\n    z = W * img + b\n    img = U / (1 + cp.power(np.e, -1.0 * z))\n    img = (img - cp.min(img)) / (cp.max(img) - cp.min(img))\n    return cp.asnumpy(img)\n\ndef sigmoid_bsb_window(dcm):\n    brain_img = sigmoid_window(dcm, 40, 80)\n    subdural_img = sigmoid_window(dcm, 80, 200)\n    bone_img = sigmoid_window(dcm, 600, 2000)\n\n    bsb_img = np.zeros((brain_img.shape[0], brain_img.shape[1], 3))\n    bsb_img[:, :, 0] = brain_img\n    bsb_img[:, :, 1] = subdural_img\n    bsb_img[:, :, 2] = bone_img\n    return bsb_img\n\ndef window_image(dcm, window_center, window_width):\n    _, _, intercept, slope = get_windowing(dcm)\n    img = dcm.pixel_array * slope + intercept\n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    img[img < img_min] = img_min\n    img[img > img_max] = img_max\n    img = (img - np.min(img)) / (np.max(img) - np.min(img))\n    return img\n\ndef bsb_window(dcm):\n    brain_img = window_image(dcm, 40, 80)\n    subdural_img = window_image(dcm, 80, 200)\n    bone_img = window_image(dcm, 600, 2000)\n\n    bsb_img = np.zeros((brain_img.shape[0], brain_img.shape[1], 3))\n    bsb_img[:, :, 0] = brain_img\n    bsb_img[:, :, 1] = subdural_img\n    bsb_img[:, :, 2] = bone_img\n    return bsb_img\n\ndef get_first_of_dicom_field_as_int(x):\n    #get x[0] as in int is x is a 'pydicom.multival.MultiValue', otherwise get int(x)\n    if type(x) == pydicom.multival.MultiValue:\n        return int(x[0])\n    else:\n        return int(x)\n\ndef get_windowing(data):\n    dicom_fields = [data[('0028','1050')].value, #window center\n                    data[('0028','1051')].value, #window width\n                    data[('0028','1052')].value, #intercept\n                    data[('0028','1053')].value] #slope\n    return [get_first_of_dicom_field_as_int(x) for x in dicom_fields]","metadata":{"execution":{"iopub.status.busy":"2023-10-25T16:19:19.926469Z","iopub.execute_input":"2023-10-25T16:19:19.926975Z","iopub.status.idle":"2023-10-25T16:19:19.952686Z","shell.execute_reply.started":"2023-10-25T16:19:19.926924Z","shell.execute_reply":"2023-10-25T16:19:19.951141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(file,type=\"WINDOW\"):\n    dcm = pydicom.dcmread(file)\n    if type == \"WINDOW\":\n        window_center , window_width, intercept, slope = get_windowing(dcm)\n        w = window_image(dcm, window_center, window_width)\n        win_img = np.repeat(w[:, :, np.newaxis], 3, axis=2)\n        #return win_img\n    elif type == \"SIGMOID\":\n        window_center , window_width, intercept, slope = get_windowing(dcm)\n        test_img = dcm.pixel_array\n        w = sigmoid_window(dcm, window_center, window_width)\n        win_img = np.repeat(w[:, :, np.newaxis], 3, axis=2)\n        #return win_img\n    elif type == \"BSB\":\n        win_img = bsb_window(dcm)\n        #return win_img\n    elif type == \"SIGMOID_BSB\":\n        win_img = sigmoid_bsb_window(dcm)\n    elif type == \"GRADIENT\":\n        win_img = rainbow_window(dcm)\n        #return win_img\n    else:\n        win_img = dcm.pixel_array\n    resized = cv2.resize(win_img,(512,512))\n    return resized","metadata":{"execution":{"iopub.status.busy":"2023-10-25T16:19:25.820059Z","iopub.execute_input":"2023-10-25T16:19:25.820458Z","iopub.status.idle":"2023-10-25T16:19:25.833137Z","shell.execute_reply.started":"2023-10-25T16:19:25.820429Z","shell.execute_reply":"2023-10-25T16:19:25.831602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p='/kaggle/input/qureai-headct/qct07/CQ500CT237 CQ500CT237/Unknown Study/CT PLAIN/CT000016.dcm'\n\nk=preprocess(p,type=\"WINDOW\")\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-25T16:19:30.108622Z","iopub.execute_input":"2023-10-25T16:19:30.10903Z","iopub.status.idle":"2023-10-25T16:19:30.225803Z","shell.execute_reply.started":"2023-10-25T16:19:30.108999Z","shell.execute_reply":"2023-10-25T16:19:30.224547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pydicom\nimport os\n\n\n# Function to preprocess the image array\ndef pre_process_im_arr(image_array):\n    # Define window levels and widths for brain tissue\n    window_level = 40  # Adjust this value based on your data\n    window_width = 80  # Adjust this value based on your data\n\n    # Apply windowing to the CT scan\n    min_value = window_level - window_width / 2\n    max_value = window_level + window_width / 2\n    image_array[image_array < min_value] = min_value\n    image_array[image_array > max_value] = max_value\n\n    # Normalize the HU values to the range [0, 255]\n    image_array = ((image_array - min_value) / (max_value - min_value) * 255).astype(np.uint8)\n\n    # Apply morphology dilation to remove noise\n    kernel = np.ones((3, 3), np.uint8)  # Adjust the kernel size as needed\n    dilated_image = cv2.dilate(image_array, kernel, iterations=1)\n\n    # Squeeze the extra dimension to convert it into a 2D array\n    dilated_image = dilated_image.squeeze()\n\n    return dilated_image\n\n\n\n# Function to load a DICOM image\ndef load_dicom_image(dicom_path):\n    ds = pydicom.dcmread(dicom_path)\n    image_array = ds.pixel_array\n    return image_array\n\n# Specify the path to your DICOM image\ndicom_path = '/kaggle/input/qureai-headct/qct07/CQ500CT237 CQ500CT237/Unknown Study/CT PLAIN/CT000016.dcm'\nimage_array = load_dicom_image(dicom_path)\n\n# Preprocess the DICOM image\npreprocessed_image = pre_process_im_arr(image_array)\n\n# Display the preprocessed image using Matplotlib\nplt.imshow(preprocessed_image, cmap='gray')\nplt.axis('off')  # Turn off axis labels\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-25T16:10:54.618735Z","iopub.status.idle":"2023-10-25T16:10:54.619214Z","shell.execute_reply.started":"2023-10-25T16:10:54.618979Z","shell.execute_reply":"2023-10-25T16:10:54.619007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pydicom\n\n# Function to apply windowing to a DICOM image\ndef window_image(image, window_center, window_width):\n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    windowed_image = image.copy()\n    windowed_image[windowed_image < img_min] = img_min\n    windowed_image[windowed_image > img_max] = img_max\n    return windowed_image\n\n# Function to display a DICOM image\ndef display_dicom_image(dicom_path, window_center, window_width):\n    # Load the DICOM image\n    ds = pydicom.dcmread(dicom_path)\n    image_array = ds.pixel_array\n\n    # Apply windowing\n    windowed_image = window_image(image_array, window_center, window_width)\n\n    # Display the image using Matplotlib\n    plt.imshow(windowed_image, cmap='gray')\n    plt.axis('off')  # Turn off axis labels\n    plt.show()\n\n# Specify the path to your DICOM image and windowing parameters\ndicom_path = \"/kaggle/input/qureai-headct/qct01/CQ500CT0 CQ500CT0/Unknown Study/CT 4cc sec 150cc D3D on/CT000011.dcm\"\nwindow_center = 40  # Adjust this value based on your data\nwindow_width = 80  # Adjust this value based on your data\n\n# Display the DICOM image with windowing\ndisplay_dicom_image(dicom_path, window_center, window_width)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-25T16:10:54.621226Z","iopub.status.idle":"2023-10-25T16:10:54.622318Z","shell.execute_reply.started":"2023-10-25T16:10:54.622102Z","shell.execute_reply":"2023-10-25T16:10:54.622124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Replace 'your_dicom_file.dcm' with the actual path to your DICOM file\nfile_path ='/kaggle/input/qureai-headct/qct19/CQ500CT95 CQ500CT95/Unknown Study/CT PRE CONTRAST 5MM STD/CT000003.dcm'\npreprocessed_image = preprocess(file_path, type=\"WINDOW\")\n\nplt.imshow(preprocessed_image)\nplt.show()\n# Now you have the preprocessed image, which you can use as needed\n","metadata":{"execution":{"iopub.status.busy":"2023-10-25T16:10:54.623605Z","iopub.status.idle":"2023-10-25T16:10:54.624644Z","shell.execute_reply.started":"2023-10-25T16:10:54.624429Z","shell.execute_reply":"2023-10-25T16:10:54.624451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bounding_box = {'x': 130.10112, 'y': 108.58427, 'width': 54.65169, 'height': 89.16854}\n\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\n\n# Display the preprocessed image\nplt.imshow(preprocessed_image)\n\n# Create a Rectangle patch for the bounding box\nrect = patches.Rectangle(\n    (bounding_box['x'], bounding_box['y']),\n    bounding_box['width'],\n    bounding_box['height'],\n    linewidth=2,\n    edgecolor='r',\n    facecolor='none'  # Transparent fill\n)\n\n# Add the rectangle to the plot\nplt.gca().add_patch(rect)\n\n# Show the image with the bounding box\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-25T16:10:54.625757Z","iopub.status.idle":"2023-10-25T16:10:54.626203Z","shell.execute_reply.started":"2023-10-25T16:10:54.626004Z","shell.execute_reply":"2023-10-25T16:10:54.626023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(preprocessed_image.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T16:10:54.628506Z","iopub.status.idle":"2023-10-25T16:10:54.629077Z","shell.execute_reply.started":"2023-10-25T16:10:54.628765Z","shell.execute_reply":"2023-10-25T16:10:54.62879Z"},"trusted":true},"execution_count":null,"outputs":[]}]}