|
| 1 | +#!/usr/bin/env python |
| 2 | +import argparse |
| 3 | +import itk |
| 4 | +from itk import RTK as rtk |
| 5 | + |
| 6 | + |
| 7 | +def build_parser(): |
| 8 | + # argument parsing |
| 9 | + parser = rtk.RTKArgumentParser( |
| 10 | + description="Subselect projections from a stack and write updated geometry." |
| 11 | + ) |
| 12 | + |
| 13 | + parser.add_argument( |
| 14 | + "--geometry", "-g", help="XML geometry file name", type=str, required=True |
| 15 | + ) |
| 16 | + parser.add_argument( |
| 17 | + "--out_geometry", help="Output geometry file name", type=str, required=True |
| 18 | + ) |
| 19 | + parser.add_argument( |
| 20 | + "--out_proj", help="Output projections stack file name", type=str, required=True |
| 21 | + ) |
| 22 | + |
| 23 | + parser.add_argument( |
| 24 | + "--first", "-f", help="First projection index", type=int, default=0 |
| 25 | + ) |
| 26 | + parser.add_argument("--last", "-l", help="Last projection index", type=int) |
| 27 | + parser.add_argument( |
| 28 | + "--step", "-s", help="Step between projections", type=int, default=1 |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "--list", |
| 32 | + help="List of projection indices to keep (0-based)", |
| 33 | + type=int, |
| 34 | + nargs="+", |
| 35 | + ) |
| 36 | + # RTK specific groups |
| 37 | + rtk.add_rtkinputprojections_group(parser) |
| 38 | + |
| 39 | + # Parse the command line arguments |
| 40 | + return parser |
| 41 | + |
| 42 | + |
| 43 | +def process(args_info: argparse.Namespace): |
| 44 | + OutputPixelType = itk.F |
| 45 | + Dimension = 3 |
| 46 | + OutputImageType = itk.Image[OutputPixelType, Dimension] |
| 47 | + |
| 48 | + # Projections reader |
| 49 | + reader = rtk.ProjectionsReader[OutputImageType].New() |
| 50 | + rtk.SetProjectionsReaderFromArgParse(reader, args_info) |
| 51 | + |
| 52 | + # Geometry |
| 53 | + if args_info.verbose: |
| 54 | + print(f"Reading geometry information from {args_info.geometry}...") |
| 55 | + geometry = rtk.read_geometry(args_info.geometry) |
| 56 | + |
| 57 | + # Compute the indices of the selected projections |
| 58 | + indices = [] |
| 59 | + n = len(geometry.GetGantryAngles()) |
| 60 | + if args_info.last: |
| 61 | + n = min(args_info.last, n) |
| 62 | + if args_info.list: |
| 63 | + for i in args_info.list: |
| 64 | + indices.append(i) |
| 65 | + else: |
| 66 | + start = args_info.first |
| 67 | + step = args_info.step |
| 68 | + for noProj in range(start, n, step): |
| 69 | + indices.append(noProj) |
| 70 | + |
| 71 | + # Output RTK geometry object |
| 72 | + outputGeometry = rtk.ThreeDCircularProjectionGeometry.New() |
| 73 | + |
| 74 | + # Output projections object |
| 75 | + source = rtk.ConstantImageSource[OutputImageType].New() |
| 76 | + source.SetInformationFromImage(reader.GetOutput()) |
| 77 | + outputSize = itk.size(reader.GetOutput()) |
| 78 | + outputSize_list = [s for s in outputSize] |
| 79 | + outputSize_list[Dimension - 1] = len(indices) |
| 80 | + source.SetSize(outputSize_list) |
| 81 | + source.SetConstant(0) |
| 82 | + source.Update() |
| 83 | + |
| 84 | + # Fill in the outputGeometry and the output projections |
| 85 | + paste = itk.PasteImageFilter[OutputImageType].New() |
| 86 | + paste.SetSourceImage(reader.GetOutput()) |
| 87 | + paste.SetDestinationImage(source.GetOutput()) |
| 88 | + |
| 89 | + for i, src_idx in enumerate(indices): |
| 90 | + # If it is not the first projection, we need to use the output of the paste filter as input |
| 91 | + if i: |
| 92 | + pimg = paste.GetOutput() |
| 93 | + pimg.DisconnectPipeline() |
| 94 | + paste.SetDestinationImage(pimg) |
| 95 | + |
| 96 | + sourceRegion = reader.GetOutput().GetLargestPossibleRegion() |
| 97 | + sourceRegion.SetIndex(Dimension - 1, src_idx) |
| 98 | + sourceRegion.SetSize(Dimension - 1, 1) |
| 99 | + paste.SetSourceRegion(sourceRegion) |
| 100 | + |
| 101 | + destinationIndex = reader.GetOutput().GetLargestPossibleRegion().GetIndex() |
| 102 | + destinationIndex.SetElement(Dimension - 1, i) |
| 103 | + paste.SetDestinationIndex(destinationIndex) |
| 104 | + |
| 105 | + paste.Update() |
| 106 | + |
| 107 | + # Fill in the output geometry object |
| 108 | + outputGeometry.SetRadiusCylindricalDetector( |
| 109 | + geometry.GetRadiusCylindricalDetector() |
| 110 | + ) |
| 111 | + outputGeometry.AddProjectionInRadians( |
| 112 | + geometry.GetSourceToIsocenterDistances()[src_idx], |
| 113 | + geometry.GetSourceToDetectorDistances()[src_idx], |
| 114 | + geometry.GetGantryAngles()[src_idx], |
| 115 | + geometry.GetProjectionOffsetsX()[src_idx], |
| 116 | + geometry.GetProjectionOffsetsY()[src_idx], |
| 117 | + geometry.GetOutOfPlaneAngles()[src_idx], |
| 118 | + geometry.GetInPlaneAngles()[src_idx], |
| 119 | + geometry.GetSourceOffsetsX()[src_idx], |
| 120 | + geometry.GetSourceOffsetsY()[src_idx], |
| 121 | + ) |
| 122 | + outputGeometry.SetCollimationOfLastProjection( |
| 123 | + geometry.GetCollimationUInf()[src_idx], |
| 124 | + geometry.GetCollimationUSup()[src_idx], |
| 125 | + geometry.GetCollimationVInf()[src_idx], |
| 126 | + geometry.GetCollimationVSup()[src_idx], |
| 127 | + ) |
| 128 | + |
| 129 | + # Geometry writer |
| 130 | + if args_info.verbose: |
| 131 | + print(f"Writing geometry information in {args_info.out_geometry}...") |
| 132 | + rtk.write_geometry(outputGeometry, args_info.out_geometry) |
| 133 | + |
| 134 | + # Write |
| 135 | + if args_info.verbose: |
| 136 | + print(f"Writing selected projections to {args_info.out_proj}...") |
| 137 | + itk.imwrite(paste.GetOutput(), args_info.out_proj) |
| 138 | + |
| 139 | + |
| 140 | +def main(argv=None): |
| 141 | + parser = build_parser() |
| 142 | + args_info = parser.parse_args(argv) |
| 143 | + process(args_info) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main() |
0 commit comments