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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*
//
// Copyright 1997-2009 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 <System/cmtkProgressConsole.h>
#include <System/cmtkThreads.h>
#include <System/cmtkTimers.h>
#include <System/cmtkStrUtility.h>
#include <IO/cmtkClassStreamInput.h>
#include <IO/cmtkClassStreamOutput.h>
#include <IO/cmtkTypedStreamStudylist.h>
#include <IO/cmtkVolumeIO.h>
#include <Base/cmtkActiveDeformationModel.h>
#include <Base/cmtkUniformVolume.h>
#include <Base/cmtkFilterMask.h>
#include <Registration/cmtkReformatVolume.h>
#include <string.h>
#include <map>
#include <string>
#include <vector>
#include <list>
#include <iostream>
bool Labels = false;
bool Jacobian = false;
bool AutoScale = false;
cmtk::ScalarDataType UserDataType = cmtk::TYPE_NONE;
cmtk::Interpolators::InterpolationEnum Interpolation = cmtk::Interpolators::LINEAR;
const char* ReplaceFrom;
std::map<std::string,std::string> ReplaceMap;
void AddReplaceFrom( const char* arg )
{
ReplaceFrom = arg;
}
void AddReplaceTo( const char* arg )
{
ReplaceMap[std::string(ReplaceFrom)] = std::string( arg );
}
const char* OutImageName = NULL;
const char* OutWarpName = NULL;
const char* InWarpName = NULL;
bool IncludeScale = true;
bool IncludeReferenceModel = true;
bool IncludeReferenceData = true;
bool WriteIncludeAffine = false;
float PaddingValue = 0;
bool SetPaddingValue = false;
float NewPaddingValue = 0;
bool ReplacePaddingValue = false;
cmtk::Types::DataItem PadOutValue = 0;
bool HavePadOutValue = false;
int
doMain( int argc, const char* argv[] )
{
std::list<const char*> inFileList;
try
{
cmtk::CommandLine cl;
cl.SetProgramInfo( cmtk::CommandLine::PRG_TITLE, "Average using ADM" );
cl.SetProgramInfo( cmtk::CommandLine::PRG_DESCR, "Compute average-shape average-intensity images and deformation maps using an active deformation model." );
cl.SetProgramInfo( cmtk::CommandLine::PRG_SYNTX, "avg_adm [options] list0 [list1 ...]" );
typedef cmtk::CommandLine::Key Key;
cl.AddSwitch( Key( 'l', "label" ), &Labels, true, "Label mode (as opposed to grey)" );
cl.AddOption( Key( "set-padding" ), &PaddingValue, "Set padding value in input files", &SetPaddingValue );
cl.AddOption( Key( "replace-padding" ), &NewPaddingValue, "Replace padding value in input files", &ReplacePaddingValue );
cl.AddOption( Key( 'p', "pad-out" ), &PadOutValue, "Padding value for output file", &HavePadOutValue );
cl.AddSwitch( Key( 'j', "jacobian" ), &Jacobian, true, "Average Jacobian determinants of deformations." );
cl.AddSwitch( Key( 'a', "auto-scale" ), &AutoScale, true, "Auto-scale image intensities" );
cl.AddSwitch( Key( "char" ), &UserDataType, cmtk::TYPE_CHAR, "Output is 8 bits, signed [default: automatic]" );
cl.AddSwitch( Key( "byte" ), &UserDataType, cmtk::TYPE_BYTE, "Output is 8 bits, unsigned" );
cl.AddSwitch( Key( "short" ), &UserDataType, cmtk::TYPE_SHORT, "Output is 16 bits, signed" );
cl.AddSwitch( Key( "ushort" ), &UserDataType, cmtk::TYPE_USHORT, "Output is 16 bits, unsigned" );
cl.AddSwitch( Key( "int" ), &UserDataType, cmtk::TYPE_INT, "Output is 32 bits signed" );
cl.AddSwitch( Key( "float" ), &UserDataType, cmtk::TYPE_FLOAT, "Output is 32 bits floating point" );
cl.AddSwitch( Key( "double" ), &UserDataType, cmtk::TYPE_DOUBLE, "Output is 64 bits floating point\n" );
cl.AddSwitch( Key( 'S', "no-scale-model" ), &IncludeScale, false, "Exclude scale from statistical deformation model" );
cl.AddSwitch( Key( 'R', "no-ref-model" ), &IncludeReferenceModel, false, "Exclude reference coordinate frame from deformation model" );
cl.AddSwitch( Key( 'r', "no-ref-data" ), &IncludeReferenceData, false, "Exclude reference data from averaging" );
cl.AddOption( Key( 'o', "output" ), &OutImageName, "Output file name [FORMAT:]path" );
cl.AddOption( Key( 'I', "input-warp" ), &InWarpName, "Input file name for average warp" );
cl.AddOption( Key( 'O', "output-warp" ), &OutWarpName, "Output file name for average warp" );
cl.AddSwitch( Key( 'A', "with-affine" ), &WriteIncludeAffine, true, "Include affine component of first warp in output" );
cl.AddSwitch( Key( "nn" ), &Interpolation, cmtk::Interpolators::NEAREST_NEIGHBOR, "Use nearest neigbor interpolation" );
cl.AddSwitch( Key( "cubic" ), &Interpolation, cmtk::Interpolators::CUBIC, "Use tri-cubic interpolation" );
cl.AddCallback( Key( "replace-from" ), AddReplaceFrom, "Replace from pattern" );
cl.AddCallback( Key( "replace-to" ), AddReplaceTo, "Replace to pattern" );
cl.Parse( argc, const_cast<const char**>( argv ) );
inFileList.push_back( cl.GetNext() );
const char* next = cl.GetNextOptional();
while ( next )
{
inFileList.push_back( next );
next = cl.GetNextOptional();
}
}
catch ( const cmtk::CommandLine::Exception& e )
{
cmtk::StdErr << e;
throw cmtk::ExitException( 1 );
}
std::vector<cmtk::SplineWarpXform::SmartPtr> warpList;
std::list<cmtk::SplineWarpXform::SmartPtr> splineWarpList;
std::vector<cmtk::UniformVolume::SmartPtr> volumeList;
cmtk::TypedStreamStudylist studylist;
char* referenceStudy = NULL;
size_t idx = 0;
for ( std::list<const char*>::const_iterator inFile = inFileList.begin(); inFile != inFileList.end(); ++inFile )
{
cmtk::DebugOutput( 1 ) << "Opening studylist" << *inFile << "\n";
if ( !studylist.Read( *inFile ) )
{
cmtk::StdErr << "ERROR: Unable to read studylist " << *inFile << "\n";
throw cmtk::ExitException( 1 );
}
if ( ! idx )
{
referenceStudy = strdup( studylist.GetReferenceStudyPath() );
}
else
{
if ( strcmp( studylist.GetReferenceStudyPath(), referenceStudy ) )
{
cmtk::StdErr.printf( "ERROR: Studylist #%u has a different reference study.\n", idx );
//continue;
throw cmtk::ExitException( 1 );
}
}
cmtk::SplineWarpXform::SmartPtr splineWarp = cmtk::SplineWarpXform::SmartPtr::DynamicCastFrom( studylist.GetWarpXform() );
if ( splineWarp )
{
std::string actualPath = cmtk::StrReplaceByRules( studylist.GetFloatingStudyPath(), ReplaceMap );
cmtk::UniformVolume::SmartPtr nextVolume;
if ( OutImageName && !Jacobian )
// no out image, no need for input images
nextVolume = cmtk::UniformVolume::SmartPtr( cmtk::VolumeIO::ReadOriented( actualPath ) );
if ( OutImageName && !Jacobian && !nextVolume )
{
cmtk::StdErr.printf( "WARNING: Cannot read volume %s in studylist #%u.\n", actualPath.c_str(), idx );
}
else
{
// everything worked out, so let's push this item onto lists
if ( SetPaddingValue )
{
nextVolume->GetData()->SetPaddingValue( PaddingValue );
if ( ReplacePaddingValue )
{
nextVolume->GetData()->ReplacePaddingData( NewPaddingValue );
}
}
warpList.push_back( splineWarp );
splineWarpList.push_back( splineWarp );
if ( OutImageName )
volumeList.push_back( nextVolume );
// and increase valid warp counter.
++idx;
}
}
else
{
cmtk::StdErr.printf( "ERROR: Studylist #%u has no spline warp.\n", idx );
throw cmtk::ExitException( 1 );
}
}
std::string actualPath = cmtk::StrReplaceByRules( studylist.GetReferenceStudyPath(), ReplaceMap );
cmtk::UniformVolume::SmartPtr refVolume( cmtk::VolumeIO::ReadOriented( actualPath ) );
if ( ! refVolume )
{
cmtk::StdErr.printf( "ERROR: Cannot read reference volume %s.\n", actualPath.c_str() );
throw cmtk::ExitException( 1 );
}
// everything worked out, so let's push this item onto lists
if ( SetPaddingValue )
{
refVolume->GetData()->SetPaddingValue( PaddingValue );
if ( ReplacePaddingValue )
{
refVolume->GetData()->ReplacePaddingData( NewPaddingValue );
}
}
if ( AutoScale )
{
if ( Jacobian )
{
cmtk::StdErr << "Warning: we don't really use image data for Jacobian map -- \nSkipping auto normalization step.\n";
}
else
{
cmtk::Types::DataItem meanRef, varRef;
refVolume->GetData()->GetStatistics( meanRef, varRef );
for ( size_t volIdx = 0; volIdx < volumeList.size(); ++volIdx )
{
cmtk::TypedArray::SmartPtr data( volumeList[volIdx]->GetData() );
cmtk::Types::DataItem meanFlt, varFlt;
data->GetStatistics( meanFlt, varFlt );
cmtk::DebugOutput( 1 ).GetStream().printf( "Before auto-rescale: [1] %f +/- %f, [2] %f +/- %f\n", meanRef, sqrt( varRef ), meanFlt, sqrt( varFlt ) );
// auto rescale, that is, determine scaling factor and offset so that
// after scaling, the intensities in both images have the same mean and
// standard deviation. Note that due to the squaring of values in
// std.dev. computation, the spread will not be exactly identical.
const cmtk::Types::DataItem factor = sqrt(varRef) / sqrt(varFlt);
data->Rescale( factor, meanRef - factor * meanFlt );
if ( cmtk::DebugOutput::GetGlobalLevel() > 0 )
{
data->GetStatistics( meanFlt, varFlt );
cmtk::DebugOutput( 1 ).GetStream().printf( "After auto-rescale: [1] %f +/- %f, [2] %f +/- %f\n", meanRef, sqrt( varRef ), meanFlt, sqrt( varFlt ) );
}
}
}
}
if ( Labels )
{
refVolume->GetData()->SetDataClass( cmtk::DATACLASS_LABEL );
}
cmtk::WarpXform::SmartPtr warpXform;
if ( InWarpName )
{
cmtk::ClassStreamInput stream( InWarpName );
if ( stream.IsValid() )
{
stream >> warpXform;
}
else
{
cmtk::StdErr << "ERROR: cannot read transformation from " << InWarpName << "\n";
throw cmtk::ExitException( 1 );
}
}
else
{
cmtk::SmartPointer<cmtk::SplineActiveDeformationModel> adm( new cmtk::SplineActiveDeformationModel( splineWarpList, 0, IncludeScale, IncludeReferenceModel ) );
adm->Compose();
warpXform = cmtk::WarpXform::SmartPtr::DynamicCastFrom( adm );
}
if ( OutWarpName )
{
if ( WriteIncludeAffine )
{
try
{
warpXform->ReplaceInitialAffine( (*(warpList.begin()))->GetInitialAffineXform() );
}
catch ( const cmtk::AffineXform::MatrixType::SingularMatrixException& )
{
cmtk::StdErr << "ERROR: singular matrix encountered in call to cmtk::WarpXform::ReplaceInitialAffine()\n";
throw cmtk::ExitException( 1 );
}
}
cmtk::ClassStreamOutput stream( OutWarpName, cmtk::ClassStreamOutput::MODE_WRITE );
if ( stream.IsValid() )
{
stream << warpXform;
}
else
{
cmtk::StdErr << "ERROR: cannot write to " << OutWarpName << "\n";
}
}
if ( OutImageName )
{
cmtk::ProgressConsole progressIndicator;
cmtk::ReformatVolume reformat;
reformat.SetReferenceVolume( refVolume );
reformat.SetWarpXform( warpXform );
reformat.SetInterpolation( Interpolation );
reformat.SetUsePaddingValue( HavePadOutValue );
reformat.SetPaddingValue( PadOutValue );
if ( UserDataType != cmtk::TYPE_NONE )
reformat.SetUserDataType( UserDataType );
cmtk::UniformVolume::SmartPtr average;
if ( Jacobian )
{
average = cmtk::UniformVolume::SmartPtr( reformat.GetTransformedReferenceJacobianAvg( &warpList, NULL /*origin*/, IncludeReferenceData ) );
}
else
{
average = cmtk::UniformVolume::SmartPtr( reformat.GetTransformedReference( &warpList, &volumeList, NULL /*origin*/, IncludeReferenceData ) );
}
cmtk::VolumeIO::Write( *average, OutImageName );
}
free( referenceStudy );
return 0;
}
#include "cmtkSafeMain"
|