|
| 1 | +#include <tclap/CmdLine.h> |
| 2 | +#include <iostream> |
| 3 | +#include <string> |
| 4 | + |
| 5 | +#include <itkImage.h> |
| 6 | +#include <itkVectorImage.h> |
| 7 | +#include <itkImageRegionIterator.h> |
| 8 | + |
| 9 | +#include <animaReadWriteFunctions.h> |
| 10 | + |
| 11 | +/** |
| 12 | + * @brief Converts an input image (either vectorial or not) into a mask |
| 13 | + * The output mask will be 0 in the voxels where the input image is 0 (or a null vector) and 1 elsewhere |
| 14 | + */ |
| 15 | +int main(int argc, char **argv) |
| 16 | +{ |
| 17 | + TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION); |
| 18 | + |
| 19 | + //Define arguments |
| 20 | + TCLAP::ValueArg<std::string> inArg("i","inputfile","input image",true,"","input image",cmd); |
| 21 | + TCLAP::ValueArg<std::string> outArg("o","outputfile","output mask",true,"","output mask",cmd); |
| 22 | + |
| 23 | + //Try to parse |
| 24 | + try |
| 25 | + { |
| 26 | + cmd.parse(argc,argv); |
| 27 | + } |
| 28 | + catch (TCLAP::ArgException& e) |
| 29 | + { |
| 30 | + std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl; |
| 31 | + return(1); |
| 32 | + } |
| 33 | + |
| 34 | + //Define types |
| 35 | + typedef itk::Image <double,3> ImageType; |
| 36 | + typedef itk::Image <double,4> Image4DType; |
| 37 | + typedef itk::VectorImage <double,3> VectorImageType; |
| 38 | + |
| 39 | + //Read input image |
| 40 | + itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(inArg.getValue().c_str(), |
| 41 | + itk::IOFileModeEnum::ReadMode); |
| 42 | + |
| 43 | + imageIO->SetFileName(inArg.getValue()); |
| 44 | + imageIO->ReadImageInformation(); |
| 45 | + |
| 46 | + //Initialize the output image (which is a mask) and allocate memory |
| 47 | + ImageType::Pointer outImage = ImageType::New(); |
| 48 | + |
| 49 | + //Determine if the input image is a vector image (each voxel value is a vector) or not |
| 50 | + bool vectorImage = (imageIO->GetNumberOfComponents() > 1); |
| 51 | + |
| 52 | + if (vectorImage) |
| 53 | + { |
| 54 | + //Case of a vector image |
| 55 | + |
| 56 | + //Read input image and store in the correct format |
| 57 | + VectorImageType::Pointer image = anima::readImage <VectorImageType> (inArg.getValue()); |
| 58 | + |
| 59 | + //Set the output image with the same properties as the input image |
| 60 | + outImage->Initialize(); |
| 61 | + outImage->SetDirection(image->GetDirection()); |
| 62 | + outImage->SetSpacing(image->GetSpacing()); |
| 63 | + outImage->SetOrigin(image->GetOrigin()); |
| 64 | + |
| 65 | + //Allocate the correct size for output image. region (image->GetLargestPossibleRegion()) is the whole image |
| 66 | + VectorImageType::RegionType region = image->GetLargestPossibleRegion(); |
| 67 | + outImage->SetRegions(region); |
| 68 | + outImage->Allocate(); |
| 69 | + |
| 70 | + //Create a vector with as many components as there are for each voxel, and with each component equal to 0 |
| 71 | + VectorImageType::PixelType zeroVec(image->GetNumberOfComponentsPerPixel()); |
| 72 | + zeroVec.Fill(0); |
| 73 | + |
| 74 | + //Create iterators on input and output images |
| 75 | + itk::ImageRegionIterator <VectorImageType> inItr(image,region); |
| 76 | + itk::ImageRegionIterator <ImageType> outItr(outImage,outImage->GetLargestPossibleRegion()); |
| 77 | + |
| 78 | + //Main algorithm |
| 79 | + while (!outItr.IsAtEnd()) |
| 80 | + { |
| 81 | + if(inItr.Get() == zeroVec) |
| 82 | + { |
| 83 | + //The voxel in the input image is a constant vector equal to 0, so this voxel in the output mask will be 0. |
| 84 | + outItr.Set(0); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + //The voxel in the input image is not a constant vector equal to 0, so this voxel in the output mask will be 1. |
| 89 | + outItr.Set(1); |
| 90 | + } |
| 91 | + |
| 92 | + //Move to following voxel |
| 93 | + ++inItr; |
| 94 | + ++outItr; |
| 95 | + } |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + //Case of a non-vector image |
| 100 | + |
| 101 | + //Read input image and store in the correct format |
| 102 | + ImageType::Pointer image = anima::readImage <ImageType> (inArg.getValue()); |
| 103 | + |
| 104 | + //Set the output image with the same properties as the input image |
| 105 | + outImage->Initialize(); |
| 106 | + outImage->SetDirection(image->GetDirection()); |
| 107 | + outImage->SetSpacing(image->GetSpacing()); |
| 108 | + outImage->SetOrigin(image->GetOrigin()); |
| 109 | + |
| 110 | + //Allocate the correct size for output image. largestRegion (image->GetLargestPossibleRegion()) is the whole image |
| 111 | + ImageType::RegionType largestRegion = image->GetLargestPossibleRegion(); |
| 112 | + outImage->SetRegions(largestRegion); |
| 113 | + outImage->Allocate(); |
| 114 | + outImage->FillBuffer(0.0); |
| 115 | + |
| 116 | + //Create iterators on input and output images |
| 117 | + itk::ImageRegionIterator <ImageType> inItr(image,image->GetLargestPossibleRegion()); |
| 118 | + itk::ImageRegionIterator <ImageType> outItr(outImage,outImage->GetLargestPossibleRegion()); |
| 119 | + |
| 120 | + //Main algorithm |
| 121 | + while (!outItr.IsAtEnd()) |
| 122 | + { |
| 123 | + if(inItr.Get() == 0.0) |
| 124 | + { |
| 125 | + //The voxel in the input image is 0, so this voxel in the output mask will be 0. |
| 126 | + outItr.Set(0); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + //The voxel in the input image is not 0, so this voxel in the output mask will be 1. |
| 131 | + outItr.Set(1); |
| 132 | + } |
| 133 | + |
| 134 | + //Move to following voxel |
| 135 | + ++inItr; |
| 136 | + ++outItr; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + //Write output image and exit |
| 141 | + anima::writeImage <ImageType> (outArg.getValue(),outImage); |
| 142 | + return EXIT_SUCCESS; |
| 143 | +} |
0 commit comments