{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":22126430,"sourceType":"kernelVersion"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nfrom PIL import Image\nimport nibabel as nib\nfrom tqdm import tqdm\nfrom concurrent.futures import ThreadPoolExecutor\nimport glob\nfrom scipy import ndimage\n\ndef load_image(image_path):\n    \"\"\"Function to load a single image.\"\"\"\n    image = np.array(Image.open(image_path))\n    image = image - np.min(image)\n    image = image / np.max(image)\n    image = (image*255).astype(np.uint8)\n    return image\n\ndef convert_tiff_to_nifti(folder_path, output_file):\n    # List all TIFF files in the folder\n    tiff_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) \n                  if f.endswith('.tif') or f.endswith('.tiff')]\n    tiff_files.sort()  # Sort the files to maintain the order\n    # Read the first image to determine the shape\n    example_image = Image.open(tiff_files[0])\n    image_shape = (example_image.size[1], example_image.size[0], len(tiff_files))\n\n    # Create an empty array to hold all image data\n    all_images = np.empty(image_shape, dtype=np.uint8)\n    print(all_images.shape)\n    # Load each TIFF file in parallel and add it to the array\n    with ThreadPoolExecutor() as executor:\n        for i, image_array in tqdm(enumerate(executor.map(load_image, tiff_files)), \n                                   total=len(tiff_files), desc=\"Loading images\"):\n            all_images[:, :, i] = image_array\n    \n    # Create a NIfTI image (this example assumes no specific affine transformation)\n    nifti_image = nib.Nifti1Image(ndimage.zoom(all_images, 0.1) , affine=np.eye(4))\n\n    # Save the NIfTI image\n    nib.save(nifti_image, output_file)\n\n# Usage\nfor folder in glob.glob(\"/kaggle/input/blood-vessel-segmentation/train/*/*\"):\n    convert_tiff_to_nifti(folder, folder.split('/')[5]+'_'+folder.split('/')[6]+'_zommed_out.nii')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-17T21:48:22.490721Z","iopub.execute_input":"2023-11-17T21:48:22.491106Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nfrom PIL import Image\nimport nibabel as nib\nfrom tqdm import tqdm\nfrom concurrent.futures import ThreadPoolExecutor\nimport glob\n\ndef load_image(image_path):\n    \"\"\"Function to load a single image.\"\"\"\n    image = np.array(Image.open(image_path))\n    image = image - np.min(image)\n    image = image / np.max(image)\n    image = (image*255).astype(np.uint8)\n    return image\n\ndef convert_tiff_to_nifti(folder_path, output_file):\n    # List all TIFF files in the folder\n    tiff_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) \n                  if f.endswith('.tif') or f.endswith('.tiff')]\n    tiff_files.sort()  # Sort the files to maintain the order\n    # Read the first image to determine the shape\n    example_image = Image.open(tiff_files[0])\n    image_shape = (example_image.size[1], example_image.size[0], len(tiff_files))\n\n    # Create an empty array to hold all image data\n    all_images = np.empty(image_shape, dtype=np.uint8)\n    print(all_images.shape)\n    # Load each TIFF file in parallel and add it to the array\n    with ThreadPoolExecutor() as executor:\n        for i, image_array in tqdm(enumerate(executor.map(load_image, tiff_files)), \n                                   total=len(tiff_files), desc=\"Loading images\"):\n            all_images[:, :, i] = image_array\n\n    # Create a NIfTI image (this example assumes no specific affine transformation)\n    nifti_image = nib.Nifti1Image(all_images, affine=np.eye(4))\n\n    # Save the NIfTI image\n    nib.save(nifti_image, output_file)\n\n# Usage\nfor folder in glob.glob(\"/kaggle/input/blood-vessel-segmentation/train/*/*\"):\n    convert_tiff_to_nifti(folder, folder.split('/')[5]+'_'+folder.split('/')[6]+'.nii')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-17T21:44:47.010797Z","iopub.status.idle":"2023-11-17T21:44:47.011615Z","shell.execute_reply.started":"2023-11-17T21:44:47.011365Z","shell.execute_reply":"2023-11-17T21:44:47.011389Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from nilearn.image import resample_img\nfrom nilearn import plotting\nimport nibabel as nib\n\nimage_=nib.load(\"/kaggle/working/kidney_1_dense_labels_zommed_out.nii\")\nlabel_=nib.load(\"/kaggle/working/kidney_1_dense_images_zommed_out.nii\")\n\nplotting.view_img(label_, bg_img = image_, cmap='gray', symmetric_cmap=False, view_type=\"contours\")\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-17T22:05:44.271237Z","iopub.execute_input":"2023-11-17T22:05:44.271785Z","iopub.status.idle":"2023-11-17T22:05:47.666631Z","shell.execute_reply.started":"2023-11-17T22:05:44.271741Z","shell.execute_reply":"2023-11-17T22:05:47.664924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}