11 #include "PrintImageCLP.h" 12 #include "itkImageRegionIteratorWithIndex.h" 32 template <
class ImageType,
class Image2Type>
34 ConvertImage (
const itk::SmartPointer<ImageType>& image, itk::SmartPointer<Image2Type>& imageOut )
55 template <
class IndexType>
57 IsOutsideBox (
const IndexType& index,
const std::vector<int>& box )
59 return index[0]<box[0] || index[0]>box[1] || index[1]<box[2] || index[1]>box[3] || index[2]<box[4] || index[2]>box[5];
62 template <
class ImageType,
class ReaderType,
class Image0Type=ImageType,
class Reader0Type=ReaderType>
67 typename ImageType::Pointer image = ImageType::New();
68 itk::ReadImage<ImageType, ReaderType>(_InputImageFile, image);
75 std::vector<int> realBox(_Box);
76 realBox[0] = realBox[0]<0 ? 0 : (realBox[0]>size[0]-1 ? size[0]-1 : realBox[0]);
77 realBox[1] = realBox[1]==-1 ? size[0]-1 : (realBox[1]<0 ? 0 : (realBox[1]>size[0]-1 ? size[0]-1 : realBox[1]) );
78 realBox[2] = realBox[2]<0 ? 0 : (realBox[2]>size[1]-1 ? size[1]-1 : realBox[2]);
79 realBox[3] = realBox[3]==-1 ? size[1]-1 : (realBox[3]<0 ? 0 : (realBox[3]>size[1]-1 ? size[1]-1 : realBox[3]) );
80 realBox[4] = realBox[4]<0 ? 0 : (realBox[4]>size[2]-1 ? size[2]-1 : realBox[4]);
81 realBox[5] = realBox[5]==-1 ? size[2]-1 : (realBox[5]<0 ? 0 : (realBox[5]>size[2]-1 ? size[2]-1 : realBox[5]) );
85 typedef itk::Image<double, 3> ScalarImageType;
86 ScalarImageType::Pointer mask = ScalarImageType::New();
87 itk::ImageRegionIteratorWithIndex<ScalarImageType> maskIt;
88 if (_MaskImageFile!=
"")
90 itk::ReadImage<ScalarImageType>(_MaskImageFile, mask);
94 maskIt = itk::ImageRegionIteratorWithIndex<ScalarImageType> (mask, mask->GetLargestPossibleRegion());
97 utlGlobalException((_DifferencePercent==1 || _DifferencePercent==2) && _BaseImageFile==
"",
"need to set _BaseImageFile");
99 typename ImageType::IndexType index;
100 itk::VariableLengthVector<double> vec;
102 std::cout <<
"\nImage option:" << std::endl;
103 for (it.GoToBegin(), maskIt.GoToBegin();
107 if (_MaskImageFile!=
"" && maskIt.Get()<=0)
110 index = it.GetIndex();
116 if (!_PrintAllVoxels && vec.GetNorm()<1e-8)
119 if (_DifferencePercent!=2)
121 std::ostringstream oss;
122 oss <<
"Image1 " << index <<
" ";
124 if (_OrientationsFile==
"" && std::fabs(_Power-1.0)>1e-10)
127 for (
int i = 0; i < vec.Size(); ++i )
128 vec[i] = std::pow(vec[i], _Power);
135 if (_BaseImageFile!=
"")
137 std::cout <<
"\nImage difference option:" << std::endl;
139 typename Image0Type::Pointer image0 = Image0Type::New();
143 itk::ReadImage<Image0Type, Reader0Type>(_BaseImageFile, image0);
149 typename NDImageType::Pointer imageTmp = NDImageType::New();
150 itk::ReadImage<NDImageType>(_BaseImageFile, imageTmp);
155 typename VectorImageType::Pointer imageTmp = VectorImageType::New();
156 itk::ReadImage<VectorImageType>(_BaseImageFile, imageTmp);
168 itk::VariableLengthVector<double> vec0, diff;
170 for (it.GoToBegin(), it0.GoToBegin(), maskIt.GoToBegin();
172 ++it, ++it0, ++maskIt)
174 if (_MaskImageFile!=
"" && maskIt.Get()<=0)
177 index = it.GetIndex();
184 if (_OrientationsFile==
"" && std::fabs(_Power-1.0)>1e-10)
187 for (
int i = 0; i < vec.Size(); ++i )
189 vec[i] = std::pow(vec[i],_Power);
190 vec0[i] = std::pow(vec0[i],_Power);
194 for (
int jj = 0; jj < diff.Size(); ++jj )
195 diff[jj] = std::fabs(diff[jj]);
197 if (!_PrintAllVoxels && (diff.GetNorm()<1e-8 || vec0.GetNorm()<1e-8 || vec.GetNorm()<1e-8))
200 if (_DifferencePercent!=2)
202 std::ostringstream oss;
203 oss <<
"difference " << index <<
" ";
209 if (_DifferencePercent==1 || _DifferencePercent==2)
211 std::cout <<
"\nImage difference percentage option:" << std::endl;
213 std::vector<double> perVec;
214 for (it.GoToBegin(), it0.GoToBegin(), maskIt.GoToBegin();
216 ++it, ++it0, ++maskIt)
218 if (_MaskImageFile!=
"" && maskIt.Get()<=0)
221 index = it.GetIndex();
228 if (_OrientationsFile==
"" && std::fabs(_Power-1.0)>1e-10)
230 for (
int i = 0; i < vec.Size(); ++i )
232 vec[i] = std::pow(vec[i],_Power);
233 vec0[i] = std::pow(vec0[i],_Power);
237 for (
int jj = 0; jj < diff.Size(); ++jj )
238 diff[jj] = std::fabs(diff[jj]);
240 if (!_PrintAllVoxels && (diff.GetNorm()<1e-8 || vec0.GetNorm()<1e-8 || vec.GetNorm()<1e-8))
243 double diffPer = diff.GetNorm()/vec0.GetNorm();
244 if (_DifferencePercent==1)
246 std::ostringstream oss;
247 oss <<
"difference percentage " << index <<
" ";
248 std::cout << oss.str() <<
": " << diffPer << std::endl;
250 perVec.push_back(diffPer);
259 std::cout <<
"\nDTI option:" << std::endl;
260 itk::VariableLengthVector<double> dt(9);
261 for (it.GoToBegin(), maskIt.GoToBegin();
265 if (_MaskImageFile!=
"" && maskIt.Get()<=0)
268 index = it.GetIndex();
274 if (!_PrintAllVoxels && vec.GetNorm()<1e-8)
277 if (_DifferencePercent!=2)
279 std::ostringstream oss;
280 oss <<
"DTI " << index <<
" ";
285 if (_TensorStorageFormat==
"9D") dt=vec;
291 if (_OrientationsFile!=
"")
293 std::cout <<
"\nSpherical function option:" << std::endl;
295 typedef utl_shared_ptr<MatrixType> MatrixPointer;
303 VectorType sf, sh(vecSize);
304 for (it.GoToBegin(), maskIt.GoToBegin();
308 if (_MaskImageFile!=
"" && maskIt.Get()<=0)
311 index = it.GetIndex();
317 if (!_PrintAllVoxels && vec.GetNorm()<1e-8)
321 std::ostringstream oss;
322 oss <<
"SF " << index <<
" ";
323 sf = (*basisMatrix) * sh;
325 if (std::fabs(_Power-1.0)>1e-10)
327 for (
int i = 0; i < sf.Size(); ++i )
328 sf[i] = std::pow(sf[i], _Power);
340 main (
int argc,
char const* argv[])
349 return PrintImage<VectorImageType, itk::ImageFileReader<VectorImageType>>(argc, argv);
351 return PrintImage<NDImageType, itk::ImageFileReader<NDImageType>>(argc, argv);
353 return PrintImage<SparseImageType, itk::SpatiallyDenseSparseVectorImageFileReader<SparseImageType>>(argc, argv);
355 return PrintImage<VariableLengthImageType, itk::VariableLengthVectorImageFileReader<VariableLengthImageType>>(argc, argv);
int GetImageType(const std::string &filename)
itk::Image< itk::VariableLengthVector< ScalarType >, 3 > VariableLengthImageType
helper functions specifically used in dmritool
void VectorToVector(const T1 &v1, T2 &v2, const int N)
void ConvertImage< NDImageType, VectorImageType >(const itk::SmartPointer< NDImageType > &image, itk::SmartPointer< VectorImageType > &imageOut)
void MultiVolumeToVectorImage(const SmartPointer< Image< ScalarType, Dim+1 > > &image1, SmartPointer< VectorImage< ScalarType, Dim > > &image2)
int DimToRankSH(const int dimm)
void ConvertImage(const itk::SmartPointer< ImageType > &image, itk::SmartPointer< Image2Type > &imageOut)
void ConvertImage< VectorImageType, NDImageType >(const itk::SmartPointer< VectorImageType > &image, itk::SmartPointer< NDImageType > &imageOut)
std::shared_ptr< NDArray< T, 2 > > ComputeSHMatrix(const unsigned int rank, const NDArray< T, 2 > &grad, const int mode)
int PrintImage(int argc, char const *argv[])
#define utlGlobalException(cond, expout)
itk::VectorImage< ScalarType, 3 > VectorImageType
#define utlPrintVar2(cond, var1, var2)
bool IsSameVector(const std::vector< T > &vec1, const std::vector< T > &vec2, const double eps=1e-10)
std::vector< int > GetVectorImage3DVolumeSize(const SmartPointer< ImageType > &image)
int main(int argc, char const *argv[])
Print image.
bool IsOutsideBox(const IndexType &index, const std::vector< int > &box)
void GetVector(PixelVectorType &vec, const int offIndex=0) const
int GetVectorImageVectorSize(const SmartPointer< ImageType > &image, const int axis=-1)
void PrintVector(const std::vector< T > &vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
void ConvertTensor6DTo9D(const V1Type &v6d, V2Type &v9d, int v6dStoreWay)
itk::SpatiallyDenseSparseVectorImage< ScalarType, 3 > SparseImageType
void VectorToMultiVolumeImage(const SmartPointer< VectorImage< ScalarType, Dim > > &image1, SmartPointer< Image< ScalarType, Dim+1 > > &image2)
itk::Image< ScalarType, 4 > NDImageType
An n-dimensional vector image with a sparse memory model.
A multi-dimensional iterator templated over image type. It provides the same interfaces for both itk:...