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
|
/*=========================================================================
Program: Visualization Toolkit
Module: ImageWeightedSum.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkImageReader.h"
#include "vtkImageWeightedSum.h"
#include "vtkDoubleArray.h"
#include "vtkImageMathematics.h"
#include "vtkImageData.h"
#include "vtkImageShiftScale.h"
#include "vtkStructuredPointsWriter.h"
#include "vtkTestUtilities.h"
#include "vtkRegressionTestImage.h"
int ImageWeightedSum(int argc, char *argv[])
{
int rval = 0;
char* fname =
vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/headsq/quarter");
vtkImageReader *reader = vtkImageReader::New();
reader->SetDataByteOrderToLittleEndian();
reader->SetDataExtent(0,63,0,63,1,93);
reader->SetDataSpacing(3.2, 3.2, 1.5);
reader->SetFilePrefix(fname);
reader->SetDataMask(0x7fff);
delete [] fname;
// Test when weight is equal to 0
vtkImageWeightedSum *sum = vtkImageWeightedSum::New();
sum->SetWeight(0,0.);
sum->AddInputConnection( reader->GetOutputPort() );
sum->Update();
double range[2];
sum->GetOutput()->GetScalarRange( range );
if( range[0] != 0 || range[1] != 0 )
{
cerr << "Range: " << range[0] << "," << range[1] << endl;
rval++;
}
// Set dummy values
vtkDoubleArray *weights = vtkDoubleArray::New();
weights->SetNumberOfTuples(5);
weights->SetValue(0, 10.0);
weights->SetValue(1, 20.0);
weights->SetValue(2, 30.0);
weights->SetValue(3, 40.0);
weights->SetValue(4, 50.0);
// Pass in the same images multiple times
sum->RemoveAllInputs();
sum->SetWeights( weights );
sum->AddInputConnection( reader->GetOutputPort() );
sum->AddInputConnection( reader->GetOutputPort() );
sum->AddInputConnection( reader->GetOutputPort() );
sum->AddInputConnection( reader->GetOutputPort() );
sum->AddInputConnection( reader->GetOutputPort() );
// Subtract the original image
vtkImageMathematics *math = vtkImageMathematics::New();
math->SetOperationToSubtract();
math->SetInputConnection( 0, reader->GetOutputPort() );
math->SetInputConnection( 1, sum->GetOutputPort() );
math->Update();
math->GetOutput()->GetScalarRange( range );
if( range[0] != 0 || range[1] != 0 )
{
cerr << "Range: " << range[0] << "," << range[1] << endl;
rval++;
}
// Get scalar range:
reader->GetOutput()->GetScalarRange( range );
vtkImageShiftScale *shift = vtkImageShiftScale::New();
shift->SetInputConnection( reader->GetOutputPort() );
shift->SetScale( 1./(range[1]-range[0]));
shift->SetShift( -range[0] );
shift->SetOutputScalarTypeToDouble ();
// Test multiple scalar type inputs
sum->RemoveAllInputs();
weights->SetNumberOfTuples(2);
weights->SetValue(0, 0.0);
weights->SetValue(1, 1.0);
sum->AddInputConnection( reader->GetOutputPort() );
sum->AddInputConnection( shift->GetOutputPort() );
math->SetInputConnection( 0, shift->GetOutputPort() );
math->SetInputConnection( 1, sum->GetOutputPort() );
//math->Update();
//math->GetOutput()->GetScalarRange( range );
//if( range[0] != 0 || range[1] != 0 )
// {
// cerr << "Range2: " << range[0] << "," << range[1] << endl;
// rval++;
// }
// Cleanup
reader->Delete();
weights->Delete();
sum->Delete();
math->Delete();
shift->Delete();
return rval;
}
|