{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# DICOM Image Annotation Visualization\n\nThis notebook demonstrates how to visualize annotations on DICOM images from the RSNA 2024 Lumbar Spine Degenerative Classification dataset. It's designed to help understand how to read, process, and display medical imaging data along with annotations such as coordinates pointing to specific anatomical features.\n\n## Dependencies\n\nFirst, we import all the necessary libraries required to handle data reading, processing, and visualization.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-20T22:31:14.347294Z","iopub.execute_input":"2024-05-20T22:31:14.347682Z","iopub.status.idle":"2024-05-20T22:31:22.992792Z","shell.execute_reply.started":"2024-05-20T22:31:14.347651Z","shell.execute_reply":"2024-05-20T22:31:22.991656Z"}}},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport pydicom\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Circle\nfrom skimage import io\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T22:34:26.536319Z","iopub.execute_input":"2024-05-20T22:34:26.536752Z","iopub.status.idle":"2024-05-20T22:34:26.542187Z","shell.execute_reply.started":"2024-05-20T22:34:26.536721Z","shell.execute_reply":"2024-05-20T22:34:26.541153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Annotations\nHere, we load the annotations data from a CSV file, filtering for a specific study of interest.","metadata":{}},{"cell_type":"code","source":"annotations_path = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_label_coordinates.csv'\nannotations_df = pd.read_csv(annotations_path)\nannotations_df = annotations_df[annotations_df['study_id'] == 4003253]","metadata":{"execution":{"iopub.status.busy":"2024-05-20T22:35:00.486648Z","iopub.execute_input":"2024-05-20T22:35:00.487380Z","iopub.status.idle":"2024-05-20T22:35:00.562317Z","shell.execute_reply.started":"2024-05-20T22:35:00.487340Z","shell.execute_reply":"2024-05-20T22:35:00.561082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Define Visualization Function\nThe function visualize_annotations reads DICOM files based on the annotations provided and visualizes these annotations on the images. It demonstrates loading DICOM files, normalizing them for better visibility, and marking the regions of interest.","metadata":{}},{"cell_type":"code","source":"def visualize_annotations(annotations_df, image_folder_base):\n    \"\"\"Visualize the annotations on the DICOM images.\"\"\"\n    for _, row in annotations_df.iterrows():\n        study_id = row['study_id']\n        series_id = row['series_id']\n        instance_number = row['instance_number']\n        condition = row['condition']\n        level = row['level']\n        x = row['x']\n        y = row['y']\n        \n        # Path to the DICOM file\n        image_path = os.path.join(image_folder_base, str(study_id), str(series_id), f\"{instance_number}.dcm\")\n        \n        # Load the DICOM image\n        dicom_image = pydicom.dcmread(image_path)\n        image = dicom_image.pixel_array\n        \n        # Normalize the image for display\n        image = (image - np.min(image)) / (np.max(image) - np.min(image)) * 255\n        image = image.astype(np.uint8)\n        \n        # Display the image\n        fig, ax = plt.subplots(1)\n        ax.imshow(image, cmap='gray')\n        \n        # Add a circle around the region of interest\n        circ = Circle((x, y), radius=10, color='red', fill=False)\n        ax.add_patch(circ)\n        \n        # Add a title with annotation details\n        ax.set_title(f\"Study ID: {study_id}, Series ID: {series_id}, Instance: {instance_number}, Condition: {condition}, Level: {level}\")\n        \n        plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T22:35:29.288704Z","iopub.execute_input":"2024-05-20T22:35:29.289199Z","iopub.status.idle":"2024-05-20T22:35:29.298785Z","shell.execute_reply.started":"2024-05-20T22:35:29.289163Z","shell.execute_reply":"2024-05-20T22:35:29.297415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set Base Directory and Visualize\nFinally, we set the base directory where the DICOM images are stored and call our visualization function.\n\n","metadata":{}},{"cell_type":"code","source":"image_folder_base = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'  # Update to your DICOM image directory\nvisualize_annotations(annotations_df, image_folder_base)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T22:35:54.446569Z","iopub.execute_input":"2024-05-20T22:35:54.446972Z","iopub.status.idle":"2024-05-20T22:36:02.445742Z","shell.execute_reply.started":"2024-05-20T22:35:54.446940Z","shell.execute_reply":"2024-05-20T22:36:02.444635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}