1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/*
//
// Copyright 1997-2010 Torsten Rohlfing
//
// Copyright 2004-2014 SRI International
//
// This file is part of the Computational Morphometry Toolkit.
//
// http://www.nitrc.org/projects/cmtk/
//
// The Computational Morphometry Toolkit is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// The Computational Morphometry Toolkit is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with the Computational Morphometry Toolkit. If not, see
// <http://www.gnu.org/licenses/>.
//
// $Revision: 5436 $
//
// $LastChangedDate: 2018-12-10 19:01:20 -0800 (Mon, 10 Dec 2018) $
//
// $LastChangedBy: torstenrohlfing $
//
*/
#include <cmtkconfig.h>
#include <System/cmtkCommandLine.h>
#include <System/cmtkExitException.h>
#include <System/cmtkConsole.h>
#include <System/cmtkDebugOutput.h>
#include <Base/cmtkUniformVolume.h>
#include <Base/cmtkVector3D.h>
#include <Registration/cmtkAffineRegistration.h>
#include <Registration/cmtkProtocolCallback.h>
#include <Recon/cmtkPointSpreadFunctionBox.h>
#include <Recon/cmtkPointSpreadFunctionGaussian.h>
#include <Recon/cmtkDeblurringVolumeReconstruction.h>
#include <IO/cmtkVolumeIO.h>
#include <IO/cmtkClassStreamInput.h>
#include <IO/cmtkClassStreamOutput.h>
#include <IO/cmtkClassStreamAffineXform.h>
#include <algorithm>
#include <map>
#include <vector>
std::string InputFilePath;
std::string OutputFilePath;
int InterleaveAxis = -1;
unsigned int NumberOfPasses = 2;
int RegistrationMetric = 4; //MSD
double InjectionKernelSigma = 1;
int InjectionKernelRadius = 4;
enum {
DEBLURRING_BOX = 1,
DEBLURRING_GAUSSIAN = 2
};
int DeblurringKernel = 0;
cmtk::Types::Coordinate PointSpreadFunctionScale = 1.0;
bool FourthOrderError = false;
int NumberOfIterations = 20;
bool RegionalIntensityTruncation = true;
double ConstraintWeightLNorm = 0;
std::string ReferenceImagePath;
std::string InjectedImagePath;
std::string ExportXformsPath;
std::string ImportXformsPath;
std::map<size_t,float> PassWeights;
bool WriteImagesAsFloat = false;
void
CallbackSetPassWeight( const char* argv )
{
int pass = 0;
float weight = 1.0;
if ( 2 == sscanf( argv, "%4d:%10f", &pass, &weight ) )
{
PassWeights[pass] = weight;
}
else
{
cmtk::StdErr << "ERROR: pass weights must be given as 'pass:weight', where 'pass' is an integer and 'weight' is a number between 0 and 1.\n"
<< " Parameter provided was '" << argv << "'\n";
throw cmtk::ExitException( 1 );
}
}
template<class TPSF>
cmtk::UniformVolume::SmartPtr
GetReconstructedImage( cmtk::UniformVolume::SmartPtr& volume, cmtk::UniformVolume::SmartPtr& refImage, std::vector<cmtk::Xform::SmartPtr>& xformsToPassImages )
{
if ( InterleaveAxis < 0 )
InterleaveAxis = cmtk::VolumeInjectionReconstruction::GuessInterleaveAxis( volume );
cmtk::DeblurringVolumeReconstruction<TPSF> volRecon( volume, NumberOfPasses, InterleaveAxis, PointSpreadFunctionScale );
for ( std::map<size_t,float>::const_iterator it = PassWeights.begin(); it != PassWeights.end(); ++it )
{
volRecon.SetPassWeight( it->first, it->second );
}
if ( refImage )
volRecon.SetReferenceImage( refImage );
volRecon.SetUseRegionalIntensityTruncation( RegionalIntensityTruncation );
volRecon.SetUseFourthOrderError( FourthOrderError );
volRecon.SetConstraintWeightLNorm( ConstraintWeightLNorm );
if ( xformsToPassImages.size() == NumberOfPasses )
{
volRecon.SetTransformationsToPassImages( xformsToPassImages );
}
else
{
cmtk::DebugOutput( 2 ) << "Computing transformations between passes...\n";
volRecon.ComputeTransformationsToPassImages( RegistrationMetric );
xformsToPassImages = volRecon.GetTransformationsToPassImages();
}
if ( !ExportXformsPath.empty() )
{
cmtk::ClassStreamOutput stream( ExportXformsPath, cmtk::ClassStreamOutput::MODE_WRITE );
if ( stream.IsValid() )
{
cmtk::DebugOutput( 2 ) << "Exporting transformations between passes to " << ExportXformsPath << "\n";
for ( unsigned int pass = 0; pass < NumberOfPasses; ++pass )
{
stream << dynamic_cast<cmtk::AffineXform&>( *xformsToPassImages[pass] );
}
}
else
{
cmtk::StdErr << "ERROR: Could not open transformation file" << ExportXformsPath << "\n";
}
}
cmtk::DebugOutput( 2 ) << "Volume injection...\n";
try
{
volRecon.VolumeInjectionIsotropic( InjectionKernelSigma, InjectionKernelRadius );
}
catch ( const cmtk::AffineXform::MatrixType::SingularMatrixException& )
{
cmtk::StdErr << "ERROR: singular coordinate transformation matrix encountered in cmtk::DeblurringVolumeReconstruction::VolumeInjectionIsotropic\n";
throw cmtk::ExitException( 1 );
}
if ( !InjectedImagePath.empty() )
{
cmtk::UniformVolume::SmartPtr outputImage = volRecon.GetCorrectedImage();
if ( !WriteImagesAsFloat && outputImage->GetData()->GetType() != volume->GetData()->GetType() )
{
outputImage = cmtk::UniformVolume::SmartPtr( outputImage->CloneGrid() );
outputImage->SetData( cmtk::TypedArray::SmartPtr( volRecon.GetCorrectedImage()->GetData()->Convert( volume->GetData()->GetType() ) ) );
}
cmtk::VolumeIO::Write( *outputImage, InjectedImagePath );
}
volRecon.Optimize( NumberOfIterations );
return volRecon.GetCorrectedImage();
}
int
doMain( const int argc, const char* argv[] )
{
try
{
cmtk::CommandLine cl;
cl.SetProgramInfo( cmtk::CommandLine::PRG_TITLE, "Fix interleaved motion using joint iterative deblurring" );
cl.SetProgramInfo( cmtk::CommandLine::PRG_DESCR, "This tool splits an interleaved input image into the pass images, co-registers them, and reconstructs a motion-corrected image" );
cl.SetProgramInfo( cmtk::CommandLine::PRG_SYNTX, "jidb [options] inImage outImage" );
typedef cmtk::CommandLine::Key Key;
cl.BeginGroup( "interleave", "Interleaving Options" );
cmtk::CommandLine::EnumGroup<int>::SmartPtr interleaveGroup = cl.AddEnum( "interleave-axis", &InterleaveAxis, "Define interleave axis: this is the through-slice direction of the acquisition." );
interleaveGroup->AddSwitch( Key( "guess-from-input" ), -1, "Guess from input image" );
interleaveGroup->AddSwitch( Key( 'a', "axial" ), (int)cmtk::AXIS_Z, "Interleaved axial images" );
interleaveGroup->AddSwitch( Key( 's', "sagittal" ),(int)cmtk::AXIS_X, "Interleaved sagittal images" );
interleaveGroup->AddSwitch( Key( 'c', "coronal" ), (int)cmtk::AXIS_Y, "Interleaved coronal images" );
interleaveGroup->AddSwitch( Key( 'x', "interleave-x" ), (int)cmtk::AXIS_X, "Interleaved along x axis" );
interleaveGroup->AddSwitch( Key( 'y', "interleave-y" ), (int)cmtk::AXIS_Y, "Interleaved along y axis" );
interleaveGroup->AddSwitch( Key( 'z', "interleave-z" ), (int)cmtk::AXIS_Z, "Interleaved along z axis" );
cl.AddOption( Key( 'p', "passes" ), &NumberOfPasses, "Number of interleaved passes" );
cl.AddCallback( Key( 'W', "pass-weight" ), CallbackSetPassWeight, "Set contribution weight for a pass in the form 'pass:weight'" );
cl.EndGroup();
cl.BeginGroup( "motion", "Motion Correction / Registration Options" );
cl.AddOption( Key( 'R', "reference-image" ), &ReferenceImagePath, "Use a separate high-resolution reference image for registration" )->SetProperties( cmtk::CommandLine::PROPS_IMAGE );
cmtk::CommandLine::EnumGroup<int>::SmartPtr
metricGroup = cl.AddEnum( "registration-metric", &RegistrationMetric, "Registration metric for motion estimation by image-to-image registration." );
metricGroup->AddSwitch( Key( "nmi" ), 0, "Use Normalized Mutual Information for pass-to-refereence registration" );
metricGroup->AddSwitch( Key( "mi" ), 1, "Use standard Mutual Information for pass-to-refereence registration" );
metricGroup->AddSwitch( Key( "cr" ), 2, "Use Correlation Ratio for pass-to-refereence registration" );
metricGroup->AddSwitch( Key( "msd" ), 4, "Use Mean Squared Differences for pass-to-refereence registration" );
metricGroup->AddSwitch( Key( "cc" ), 5, "Use Cross-Correlation for pass-to-refereence registration" );
cl.AddOption( Key( "import-xforms-path" ), &ImportXformsPath, "Path of file from which to import transformations between passes." )
->SetProperties( cmtk::CommandLine::PROPS_FILENAME );
cl.AddOption( Key( "export-xforms-path" ), &ExportXformsPath, "Path of file to which to export transformations between passes." )
->SetProperties( cmtk::CommandLine::PROPS_FILENAME | cmtk::CommandLine::PROPS_OUTPUT );
cl.BeginGroup( "inject", "Volume Injection Options" );
cl.AddOption( Key( 'S', "injection-kernel-sigma" ), &InjectionKernelSigma, "Standard deviation of Gaussian kernel for volume injection" );
cl.AddOption( Key( 'r', "injection-kernel-radius" ), &InjectionKernelRadius, "Truncation radius of injection kernel" );
cl.BeginGroup( "deblur", "Deblurring Options" );
cmtk::CommandLine::EnumGroup<int>::SmartPtr psfGroup =
cl.AddEnum( "psf", &DeblurringKernel, "Kernel for the inverse interpolation reconstruction" );
psfGroup->AddSwitch( Key( "box" ), (int)DEBLURRING_BOX, "Box-shaped PSF" );
psfGroup->AddSwitch( Key( "gaussian" ), (int)DEBLURRING_GAUSSIAN, "Gaussian-shaped PSF" );
cl.AddOption( Key( "psf-scale" ), &PointSpreadFunctionScale, "Set global scale factor for point spread function size, which itself is derived from the input image." );
cl.EndGroup();
cl.BeginGroup( "optimize", "Optimization Options" );
cl.AddSwitch( Key( 'f', "fourth-order-error" ), &FourthOrderError, true, "Use fourth-order (rather than second-order) error for optimization." );
cl.AddOption( Key( 'n', "num-iterations" ), &NumberOfIterations, "Maximum number of inverse interpolation iterations" );
cl.AddSwitch( Key( 'T', "no-truncation" ), &RegionalIntensityTruncation, false, "Turn off regional intensity truncatrion" );
cl.AddOption( Key( "l-norm-weight" ), &ConstraintWeightLNorm, "Set constraint weight for Tikhonov-type L-Norm regularization (0 disables constraint)" );
cl.EndGroup();
cl.BeginGroup( "output", "Output Options" );
cl.AddOption( Key( "write-injected-image" ), &InjectedImagePath, "Write initial volume injection image to path" )->SetProperties( cmtk::CommandLine::PROPS_IMAGE | cmtk::CommandLine::PROPS_OUTPUT );;
cl.AddSwitch( Key( 'F', "write-images-as-float" ), &WriteImagesAsFloat, true, "Write output images as floating point [default: same as input]" );
cl.AddParameter( &InputFilePath, "InputImage", "Input image path" )->SetProperties( cmtk::CommandLine::PROPS_IMAGE );
cl.AddParameter( &OutputFilePath, "OutputImage", "Output image path" )->SetProperties( cmtk::CommandLine::PROPS_IMAGE | cmtk::CommandLine::PROPS_OUTPUT );
cl.Parse( argc, argv );
}
catch ( const cmtk::CommandLine::Exception& e )
{
cmtk::StdErr << e << "\n";
throw cmtk::ExitException( 1 );
}
/*
// Read input image
*/
cmtk::UniformVolume::SmartPtr volume( cmtk::VolumeIO::ReadOriented( InputFilePath ) );
if ( ! volume || ! volume->GetData() )
{
cmtk::StdErr << "ERROR: Could not read image " << InputFilePath << "\n";
throw cmtk::ExitException( 1 );
}
cmtk::UniformVolume::SmartPtr refImage;
if ( !ReferenceImagePath.empty() )
{
refImage = cmtk::UniformVolume::SmartPtr( cmtk::VolumeIO::ReadOriented( ReferenceImagePath ) );
if ( ! refImage || ! refImage->GetData() )
{
cmtk::StdErr << "ERROR: Could not read image " << ReferenceImagePath << "\n";
throw cmtk::ExitException( 1 );
}
}
std::vector<cmtk::Xform::SmartPtr> xformsToPassImages;
if ( !ImportXformsPath.empty() )
{
cmtk::ClassStreamInput stream( ImportXformsPath );
if ( stream.IsValid() )
{
cmtk::DebugOutput( 1 ) << "Importing transformations between passes from " << ImportXformsPath << "\n";
cmtk::AffineXform xform;
for ( unsigned int pass = 0; pass < NumberOfPasses; ++pass )
{
try
{
stream >> xform;
}
catch ( const cmtk::Exception& ex )
{
cmtk::StdErr << "ERROR: " << ex.what() << "\n";
throw cmtk::ExitException( 1 );
}
xformsToPassImages.push_back( cmtk::Xform::SmartPtr( xform.Clone() ) );
}
}
else
{
cmtk::StdErr << "ERROR: Could not open transformation file" << ImportXformsPath << "\n";
throw cmtk::ExitException( 1 );
}
}
cmtk::UniformVolume::SmartPtr correctedVolume;
switch ( DeblurringKernel )
{
case DEBLURRING_BOX:
default:
correctedVolume = GetReconstructedImage<cmtk::PointSpreadFunctions::Box>( volume, refImage, xformsToPassImages );
break;
case DEBLURRING_GAUSSIAN:
correctedVolume = GetReconstructedImage<cmtk::PointSpreadFunctions::Gaussian>( volume, refImage, xformsToPassImages );
break;
}
cmtk::UniformVolume::SmartPtr outputImage = correctedVolume;
if ( !WriteImagesAsFloat && outputImage->GetData()->GetType() != volume->GetData()->GetType() )
{
outputImage = cmtk::UniformVolume::SmartPtr( outputImage->CloneGrid() );
outputImage->SetData( cmtk::TypedArray::SmartPtr( correctedVolume->GetData()->Convert( volume->GetData()->GetType() ) ) );
}
cmtk::VolumeIO::Write( *outputImage, OutputFilePath );
return 0;
}
#include "cmtkSafeMain"
|