mask2MTPL.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. import argparse
  3. import subprocess
  4. def main():
  5. parser = argparse.ArgumentParser(description="Resample multiple flo images using a reference image and an affine transformation file.")
  6. parser.add_argument("-i", "--input", help="Input folder containing flo images", required=True)
  7. parser.add_argument("-o", "--output", help="Output folder for resampled images", required=True)
  8. args = parser.parse_args()
  9. # Get the script's location
  10. script_location = os.path.dirname(os.path.realpath(__file__))
  11. # Construct paths to reference image and affine transformation file
  12. ref_image_path = os.path.join(script_location, "..", "output", "C57BL6_mouse.iso_registerd.nii.gz")
  13. affine_trafo_path = os.path.join(script_location, "..", "output", "affineTrafo.txt")
  14. # Create the output folder if it doesn't exist
  15. if not os.path.exists(args.output):
  16. os.makedirs(args.output)
  17. # List all flo images in the input folder
  18. flo_images = [os.path.join(args.input, image) for image in os.listdir(args.input) if image.endswith(".nii")]
  19. # Loop through flo images and perform resampling
  20. for flo_image in flo_images:
  21. output_filename = os.path.basename(flo_image).replace(".nii", "_registered.nii")
  22. output_path = os.path.join(args.output, output_filename)
  23. # Run the resample command
  24. command = [
  25. "reg_resample",
  26. "-ref", ref_image_path,
  27. "-flo", flo_image,
  28. "-res", output_path,
  29. "-trans", affine_trafo_path,
  30. "-inter", "0"
  31. ]
  32. subprocess.run(command)
  33. if __name__ == "__main__":
  34. main()