{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPNfGg8aMk8mnFKmCpLrTgL",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"23c45496636b4e9b92dfaf054b17bc5c": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HBoxModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HBoxModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HBoxView",
"box_style": "",
"children": [
"IPY_MODEL_c1c065040d0941aa92dbd2a83d290fd9",
"IPY_MODEL_71adc5bfa0d344f5960d71d59343786e",
"IPY_MODEL_de7e1ff008124bc8997072c4bbf77be2"
],
"layout": "IPY_MODEL_70b407afe33d48e5bad0ce5bacab45d1"
}
},
"c1c065040d0941aa92dbd2a83d290fd9": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HTMLModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HTMLView",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_c81ed660af7543e49c45ce55a5a1a130",
"placeholder": "",
"style": "IPY_MODEL_f0430b4cf0824c96bd6e495d8c9604b7",
"value": "RealESRGAN_x4.pth: 100%"
}
},
"71adc5bfa0d344f5960d71d59343786e": {
"model_module": "@jupyter-widgets/controls",
"model_name": "FloatProgressModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "FloatProgressModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "ProgressView",
"bar_style": "success",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_b0b69db8355b4a2782ab65580a986842",
"max": 67040989,
"min": 0,
"orientation": "horizontal",
"style": "IPY_MODEL_c32d8b247e214cd8b8c6592314626787",
"value": 67040989
}
},
"de7e1ff008124bc8997072c4bbf77be2": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HTMLModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HTMLView",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_20114f328dad478ea5ce19c9c6bd0ffb",
"placeholder": "",
"style": "IPY_MODEL_d9618ddb3d174542a593477b49eb3249",
"value": " 67.0M/67.0M [00:04<00:00, 17.7MB/s]"
}
},
"70b407afe33d48e5bad0ce5bacab45d1": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"c81ed660af7543e49c45ce55a5a1a130": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"f0430b4cf0824c96bd6e495d8c9604b7": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "DescriptionStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"description_width": ""
}
},
"b0b69db8355b4a2782ab65580a986842": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"c32d8b247e214cd8b8c6592314626787": {
"model_module": "@jupyter-widgets/controls",
"model_name": "ProgressStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "ProgressStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"bar_color": null,
"description_width": ""
}
},
"20114f328dad478ea5ce19c9c6bd0ffb": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"d9618ddb3d174542a593477b49eb3249": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "DescriptionStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"description_width": ""
}
}
}
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"source": [
"# Upscaling brain images using `brainslicer` and `Real-ESRGAN`\n",
"\n",
"[![GitHub Stars](https://img.shields.io/badge/brainslicer_ESRGAN-v1.0-blue?logo=github)](https://github.com/sohaamir/brainslicer)\n",
"\n",
"This notebook provides an integrated workflow towards generating upscaled brain images. NIFTIs, hosted on Google Drive, are firstly sliced using `brainslicer`.\n",
"\n",
"`brainslicer [-h] [--plane {axial,sagittal,coronal}] [--brightness BRIGHTNESS] [--contrast CONTRAST] [--colourmap {magma,inferno,plasma}] file_path slice_number`\n",
"\n",
"For more information and usage examples, use the `-h` or `--help` flags.\n",
"\n",
"The slices can then be upscaled using [Real-ESRGAN](https://arxiv.org/abs/2107.10833), a Generative Adversarial Network capable of recovering high resolution images from low resolution ones.\n",
"\n",
"You can apply `Real-ESRGAN` to sliced images on both your Google Drive and locally on your computer.\n",
"\n",
"### **Pre-requisites**\n",
"\n",
"1. I would strongly advocate changing the runtime from 'CPU' to 'T4 GPU'. This significantly shorten the amount of time taken to process an image (from 2-3 minutes to a few seconds).\n",
"\n",
"2. You will need to download the Real-ESRGAN model weights and put them into a subdirectory within your `brainslicer` folder. The weights can be found [here](https://drive.google.com/drive/folders/1Sh3bxNo2V2KVhoRW272WkPLUnc54YWtr).\n",
"\n",
"3. You can choose to run `brainslicer` and `Real-ESRGAN` on files that are located on your local machine, or on Google Drive, however it is easier and quicker to do the latter. You can set up your directory with the following structure:\n",
"\n",
"```\n",
"brainslicer/\n",
"├── brainslices/\n",
"├── niftis/\n",
"├── Real-ESRGAN-colab/\n",
"├── weights/\n",
"└── brainslicer_upscale.ipynb\n",
"```\n",
"\n",
"Please note that after generating an image, if you want to generate another, you will have to change back into the original working directory e.g., `cd% /gdrive/MyDrive/projects/brainslicer`. This is because you are changing the location of the directory in the notebook."
],
"metadata": {
"id": "oREC2LbYsU0s"
}
},
{
"cell_type": "markdown",
"source": [
"## 1. Install the `brainslicer` package and it's dependencies"
],
"metadata": {
"id": "pgr2NRt7BHk7"
}
},
{
"cell_type": "code",
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
],
"metadata": {
"id": "JYQDW_RdD3u-"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Install brainslicer from GitHub\n",
"%%capture\n",
"!pip install git+https://github.com/sohaamir/brainslicer.git\n",
"!pip install nibabel numpy Pillow matplotlib"
],
"metadata": {
"id": "ssMU3xeVT6Hj"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# pass the help argument for instructions\n",
"!brainslicer --help"
],
"metadata": {
"id": "24zmdBF8kVOd",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "cc3deea4-e171-4bf7-ee39-9eab7e648f4f"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"usage: brainslicer [-h] [--plane {axial,sagittal,coronal}] [--brightness BRIGHTNESS]\n",
" [--contrast CONTRAST] [--colourmap COLOURMAP] [--list-colourmaps]\n",
" file_path slice_number\n",
"\n",
"Brainslicer is a command-line utility designed to aid in the examination of NIFTI anatomical images. \n",
"It provides the following features:\n",
"\n",
"* Slice Selection: Display slices from axial, sagittal, or coronal planes.\n",
"* Brightness and Contrast Control: Fine-tune image appearance for better visualization.\n",
"* Colourmap Customization: Choose from a variety of colourmaps to highlight different aspects of the image data.\n",
"\n",
"For more information and usage examples, use the -h or --help flags.\n",
"\n",
"options:\n",
" -h, --help show this help message and exit\n",
" --plane {axial,sagittal,coronal}\n",
" Plane of the slice\n",
"\n",
"Required arguments:\n",
" file_path Path to the NIFTI file\n",
" slice_number Index of the slice\n",
"\n",
"Optional arguments:\n",
" --brightness BRIGHTNESS, -b BRIGHTNESS\n",
" Brightness adjustment factor (default: 1.0)\n",
" --contrast CONTRAST, -con CONTRAST\n",
" Contrast adjustment factor (default: 1.0)\n",
" --colourmap COLOURMAP, -cmap COLOURMAP\n",
" Colourmap to use for displaying the slice (default: 'gray').\n",
" --list-colourmaps List all available colourmaps\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## 2. Change to your relevant directory and run `brainslicer`"
],
"metadata": {
"id": "kArHM5cUBd4a"
}
},
{
"cell_type": "code",
"source": [
"# Mount your Google Drive and change to your brainslicer directory (you will have to give permissions)\n",
"from google.colab import drive, files\n",
"\n",
"drive.mount('/gdrive')\n",
"%cd /gdrive/MyDrive/projects/brainslicer\n",
"\n",
"# drive.mount('/content/drive')\n",
"#%cd /content/drive/MyDrive/projects/brainslicer/"
],
"metadata": {
"id": "34FYZxmCAXwx",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "8a138a1d-1e0e-4c97-feca-6ef66b3034c4"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /gdrive\n",
"/gdrive/MyDrive/projects/brainslicer\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# It may be useful to print out the dimensions of the NIFTI first (sagittal, coronal, axial)\n",
"import nibabel as nib\n",
"\n",
"# Load the NIFTI file\n",
"nifti_file_path = 'niftis/aamir_T1.nii'\n",
"nifti_img = nib.load(nifti_file_path)\n",
"\n",
"# Get dimensions of the NIFTI image\n",
"nifti_dimensions = nifti_img.header.get_data_shape()\n",
"print(nifti_dimensions)"
],
"metadata": {
"id": "erxjvHRuFVYf",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "0d1a227c-bc1e-4c08-e8c8-20b5a71cd4c1"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"(192, 256, 256)\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Run brainslicer using the CLI\n",
"!brainslicer niftis/aamir_T1.nii 100 --plane sagittal --brightness 2.0 --contrast 1.5 --colourmap gray"
],
"metadata": {
"id": "95CuoPU7E7Zx",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7ec922df-329a-463e-b65a-e03d7e6c9f14"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Image saved as 'brainslices/aamir_T1_100_sagittal_brightness_2.0_contrast_1.5_gray.png'.\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"You can also choose to upload NIFTIs from your local machine, but it takes quite a while..."
],
"metadata": {
"id": "xW_jUcceBwjU"
}
},
{
"cell_type": "code",
"source": [
"from google.colab import files\n",
"\n",
"uploaded = files.upload()"
],
"metadata": {
"id": "-xRgQfZP_ysd"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 3. Display the sliced image"
],
"metadata": {
"id": "-IBefNqaCV8Y"
}
},
{
"cell_type": "code",
"source": [
"import os\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.image as mpimg\n",
"\n",
"output_dir = '/gdrive/MyDrive/projects/brainslicer/brainslices' # Your output directory\n",
"\n",
"# Find the latest file in the output directory\n",
"latest_file = None\n",
"latest_timestamp = 0\n",
"\n",
"for filename in os.listdir(output_dir):\n",
" if filename.endswith('.png'):\n",
" filepath = os.path.join(output_dir, filename)\n",
" file_timestamp = os.path.getmtime(filepath) # Get file modification timestamp\n",
" if file_timestamp > latest_timestamp:\n",
" latest_timestamp = file_timestamp\n",
" latest_file = filepath\n",
"\n",
"# Check if an image was found\n",
"if latest_file:\n",
" img = mpimg.imread(latest_file)\n",
"\n",
" # Display the image\n",
" plt.figure(figsize=(8,6))\n",
" plt.imshow(img, cmap='gray')\n",
" plt.axis('off')\n",
" plt.title('Latest Brainslicer Output')\n",
" plt.show()\n",
"else:\n",
" print(\"No image found in the 'brainslices' directory.\")"
],
"metadata": {
"id": "DbnmJ39mgb8x",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 521
},
"outputId": "9176eb40-bde6-4a52-92a1-b35ee8b66e5c"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"