38 #ifndef __itkStandardImageRepresenter_hxx
39 #define __itkStandardImageRepresenter_hxx
44 #include <itkImageDuplicator.h>
45 #include <itkImageIterator.h>
46 #include <itkImageRegionConstIterator.h>
47 #include <itkImageRegionIterator.h>
48 #include <itkImageFileReader.h>
49 #include <itkImageFileWriter.h>
52 #include <itkVector.h>
54 #include "HDF5Utils.h"
55 #include "StatismoUtils.h"
57 #include "itkStandardImageRepresenter.h"
62 template <
class TPixel,
unsigned ImageDimension>
63 StandardImageRepresenter<TPixel, ImageDimension>::StandardImageRepresenter()
66 template <
class TPixel,
unsigned ImageDimension>
67 StandardImageRepresenter<TPixel, ImageDimension>::~StandardImageRepresenter() {
70 template <
class TPixel,
unsigned ImageDimension>
71 StandardImageRepresenter<TPixel, ImageDimension>*
72 StandardImageRepresenter<TPixel, ImageDimension>::Clone()
const {
74 StandardImageRepresenter* clone =
new StandardImageRepresenter();
77 DatasetPointerType clonedReference = this->CloneDataset(m_reference);
78 clone->SetReference(clonedReference);
84 template <
class TPixel,
unsigned ImageDimension>
86 StandardImageRepresenter<TPixel, ImageDimension>::Load(
const H5::Group& fg) {
89 if (repName ==
"vtkStructuredPointsRepresenter" || repName ==
"itkImageRepresenter" || repName ==
"itkVectorImageRepresenter") {
90 this->SetReference(LoadRefLegacy(fg));
92 this->SetReference(LoadRef(fg));
98 template <
class TPixel,
unsigned ImageDimension>
99 typename StandardImageRepresenter<TPixel, ImageDimension>::ImageType::Pointer
100 StandardImageRepresenter<TPixel, ImageDimension>::LoadRef(
const H5::Group& fg)
const {
104 if (readImageDimension != ImageDimension) {
109 statismo::VectorType originVec;
111 typename ImageType::PointType origin;
112 for (
unsigned i = 0; i < ImageDimension; i++) {
113 origin[i] = originVec[i];
116 statismo::VectorType spacingVec;
118 typename ImageType::SpacingType spacing;
119 for (
unsigned i = 0; i < ImageDimension; i++) {
120 spacing[i] = spacingVec[i];
123 typename statismo::GenericEigenType<int>::VectorType sizeVec;
124 statismo::HDF5Utils::readVectorOfType<int>(fg,
"size", sizeVec);
125 typename ImageType::SizeType size;
126 for (
unsigned i = 0; i < ImageDimension; i++) {
127 size[i] = sizeVec[i];
130 statismo::MatrixType directionMat;
132 typename ImageType::DirectionType direction;
133 for (
unsigned i = 0; i < directionMat.rows(); i++) {
134 for (
unsigned j = 0; j < directionMat.rows(); j++) {
135 direction[i][j] = directionMat(i,j);
139 H5::Group pdGroup = fg.openGroup(
"./pointData");
141 if (readPixelDimension != GetDimensions()) {
145 typename statismo::GenericEigenType<double>::MatrixType pixelMatDouble;
146 statismo::HDF5Utils::readMatrixOfType<double>(pdGroup,
"pixelValues", pixelMatDouble);
148 typename ImageType::Pointer newImage = ImageType::New();
149 typename ImageType::IndexType start;
153 H5::DataSet ds = pdGroup.openDataSet(
"pixelValues");
155 if (type != PixelConversionTrait<TPixel>::GetDataType()) {
156 std::cout <<
"Warning: The datatype specified for the scalars does not match the TPixel template argument used in this representer." << std::endl;
159 typename ImageType::RegionType region(start, size);
160 newImage->SetRegions(region);
161 newImage->Allocate();
162 newImage->SetOrigin(origin);
163 newImage->SetSpacing(spacing);
164 newImage->SetDirection(direction);
167 itk::ImageRegionIterator<DatasetType> it(newImage, newImage->GetLargestPossibleRegion());
169 for (
unsigned i = 0; !it.IsAtEnd(); ++it, i++) {
170 TPixel v = PixelConversionTrait<TPixel>::FromVector(pixelMat.col(i));
177 template <
class TPixel,
unsigned ImageDimension>
178 typename StandardImageRepresenter<TPixel, ImageDimension>::ImageType::Pointer
179 StandardImageRepresenter<TPixel, ImageDimension>::LoadRefLegacy(
const H5::Group& fg)
const {
181 std::string tmpfilename;
182 tmpfilename = statismo::Utils::CreateTmpName(
".vtk");
185 typename itk::ImageFileReader<ImageType>::Pointer reader = itk::ImageFileReader<ImageType>::New();
186 reader->SetFileName(tmpfilename);
189 }
catch (itk::ImageFileReaderException& e) {
192 typename DatasetType::Pointer img = reader->GetOutput();
194 std::remove(tmpfilename.c_str());
200 template <
class TPixel,
unsigned ImageDimension>
203 m_reference = reference;
205 typename DomainType::DomainPointsListType domainPoints;
206 itk::ImageRegionConstIterator<DatasetType> it(reference, reference->GetLargestPossibleRegion());
209 it.IsAtEnd() == false
212 reference->TransformIndexToPhysicalPoint(it.GetIndex(), pt);
213 domainPoints.push_back(pt);
219 template <
class TPixel,
unsigned ImageDimension>
222 statismo::VectorType v(PointType::GetPointDimension());
223 for (
unsigned i = 0; i < PointType::GetPointDimension(); i++) {
232 template <
class TPixel,
unsigned ImageDimension>
233 typename StandardImageRepresenter<TPixel, ImageDimension>::DatasetPointerType
237 typename itk::ImageDuplicator<ImageType>::Pointer duplicator = itk::ImageDuplicator<ImageType>::New();
238 duplicator->SetInputImage(image);
239 duplicator->Update();
240 return duplicator->GetOutput();
244 template <
class TPixel,
unsigned ImageDimension>
246 StandardImageRepresenter<TPixel, ImageDimension>::SampleToSampleVector(DatasetConstPointerType image)
const {
247 statismo::VectorType sample(this->GetNumberOfPoints() * GetDimensions());
248 itk::ImageRegionConstIterator<DatasetType> it(image, image->GetLargestPossibleRegion());
252 it.IsAtEnd() ==
false;
255 statismo::VectorType sampleAtPt = PixelConversionTrait<TPixel>::ToVector(it.Value());
256 for (
unsigned j = 0; j < GetDimensions(); j++) {
257 unsigned idx = this->MapPointIdToInternalIdx(i, j);
258 sample[idx] = sampleAtPt[j];
266 template <
class TPixel,
unsigned ImageDimension>
267 typename StandardImageRepresenter<TPixel, ImageDimension>::DatasetPointerType
268 StandardImageRepresenter<TPixel, ImageDimension>::SampleVectorToSample(
const statismo::VectorType& sample)
const {
270 typedef itk::ImageDuplicator< DatasetType > DuplicatorType;
271 typename DuplicatorType::Pointer duplicator = DuplicatorType::New();
272 duplicator->SetInputImage(this->m_reference);
273 duplicator->Update();
274 DatasetPointerType clonedImage = duplicator->GetOutput();
276 itk::ImageRegionIterator<DatasetType> it(clonedImage, clonedImage->GetLargestPossibleRegion());
278 for (
unsigned i = 0; !it.IsAtEnd(); ++it, i++) {
280 statismo::VectorType valAtPoint(GetDimensions());
281 for (
unsigned d = 0; d < GetDimensions(); d++) {
282 unsigned idx = this->MapPointIdToInternalIdx(i, d);
283 valAtPoint[d] = sample[idx];
285 ValueType v = PixelConversionTrait<TPixel>::FromVector(valAtPoint);
292 template <
class TPixel,
unsigned ImageDimension>
293 typename StandardImageRepresenter<TPixel, ImageDimension>::ValueType
294 StandardImageRepresenter<TPixel, ImageDimension>::PointSampleFromSample(DatasetConstPointerType sample,
unsigned ptid)
const {
295 if (ptid >= GetDomain().GetNumberOfPoints()) {
300 PointType pt = GetDomain().GetDomainPoints()[ptid];
301 typename ImageType::IndexType idx;
302 sample->TransformPhysicalPointToIndex(pt, idx);
303 ValueType value = sample->GetPixel(idx);
308 template <
class TPixel,
unsigned ImageDimension>
309 typename StandardImageRepresenter<TPixel, ImageDimension>::ValueType
310 StandardImageRepresenter<TPixel, ImageDimension>::PointSampleVectorToPointSample(
const statismo::VectorType& pointSample)
const {
311 return PixelConversionTrait<TPixel>::FromVector(pointSample);
314 template <
class TPixel,
unsigned ImageDimension>
316 StandardImageRepresenter<TPixel, ImageDimension>::PointSampleToPointSampleVector(
const ValueType& v)
const {
317 return PixelConversionTrait<TPixel>::ToVector(v);
321 template <
class TPixel,
unsigned ImageDimension>
323 StandardImageRepresenter<TPixel, ImageDimension>::Save(
const H5::Group& fg)
const {
325 typename ImageType::PointType origin = m_reference->GetOrigin();
326 statismo::VectorType originVec(ImageDimension);
327 for (
unsigned i = 0; i < ImageDimension; i++) {
328 originVec(i) = origin[i];
332 typename ImageType::SpacingType spacing = m_reference->GetSpacing();
333 statismo::VectorType spacingVec(ImageDimension);
334 for (
unsigned i = 0; i < ImageDimension; i++) {
335 spacingVec(i) = spacing[i];
340 statismo::GenericEigenType<int>::VectorType sizeVec(ImageDimension);
341 for (
unsigned i = 0; i < ImageDimension; i++) {
342 sizeVec(i) = m_reference->GetLargestPossibleRegion().GetSize()[i];
344 statismo::HDF5Utils::writeVectorOfType<int>(fg,
"size", sizeVec);
346 typename ImageType::DirectionType direction = m_reference->GetDirection();
347 statismo::MatrixType directionMat(ImageDimension, ImageDimension);
348 for (
unsigned i = 0; i < ImageDimension; i++) {
349 for (
unsigned j = 0; j < ImageDimension; j++) {
350 directionMat(i,j) = direction[i][j];
357 H5::Group pdGroup = fg.createGroup(
"pointData");
361 typedef statismo::GenericEigenType<double>::MatrixType DoubleMatrixType;
362 statismo::MatrixType pixelMat(GetDimensions(), GetNumberOfPoints());
364 itk::ImageRegionIterator<DatasetType> it(m_reference, m_reference->GetLargestPossibleRegion());
367 it.IsAtEnd() ==
false;
369 pixelMat.col(i) = PixelConversionTrait<TPixel>::ToVector(it.Get());
372 DoubleMatrixType pixelMatDouble = pixelMat.cast<
double>();
373 H5::DataSet ds = statismo::HDF5Utils::writeMatrixOfType<double>(pdGroup,
"pixelValues", pixelMatDouble);
379 template <
class TPixel,
unsigned ImageDimension>
381 StandardImageRepresenter<TPixel, ImageDimension>::GetNumberOfPoints()
const {
382 return m_reference->GetLargestPossibleRegion().GetNumberOfPixels();
386 template <
class TPixel,
unsigned ImageDimension>
388 StandardImageRepresenter<TPixel, ImageDimension>::GetPointIdForPoint(
const PointType& pt)
const {
390 typename DatasetType::IndexType idx;
391 bool ptInImage = this->m_reference->TransformPhysicalPointToIndex(pt, idx);
393 typename DatasetType::SizeType size = this->m_reference->GetLargestPossibleRegion().GetSize();
401 for (
unsigned int i=0; i<ImageType::ImageDimension; ++i) {
403 if(idx[i] < -1 || idx[i] > size[i]) {
407 if(idx[i] == -1) idx[i] = 0;
408 if(idx[i] == size[i]) idx[i] = size[i] - 1;
414 unsigned int index=0;
415 for (
unsigned int i=0; i<ImageType::ImageDimension; ++i) {
416 unsigned int multiplier=1;
417 for (
int d=i-1; d>=0; --d) {
420 index+=multiplier*idx[i];
426 template <
class TPixel,
unsigned ImageDimension>
427 typename StandardImageRepresenter<TPixel, ImageDimension>::DatasetPointerType
428 StandardImageRepresenter<TPixel, ImageDimension>::CloneDataset(DatasetConstPointerType d)
const {
429 typedef itk::ImageDuplicator< DatasetType > DuplicatorType;
430 typename DuplicatorType::Pointer duplicator = DuplicatorType::New();
431 duplicator->SetInputImage(d);
432 duplicator->Update();
433 DatasetPointerType clone = duplicator->GetOutput();
434 clone->DisconnectPipeline();
A representer for scalar and vector valued images.
Definition: itkStandardImageRepresenter.h:63
static void writeIntAttribute(const H5::H5Object &fg, const char *name, int value)
Definition: HDF5Utils.hxx:406
static void readMatrix(const H5::CommonFG &fg, const char *name, MatrixType &matrix)
Definition: HDF5Utils.hxx:154
static H5::DataSet writeMatrix(const H5::CommonFG &fg, const char *name, const MatrixType &matrix)
Definition: HDF5Utils.hxx:248
statismo::VectorType PointToVector(const PointType &pt) const
Definition: itkStandardImageRepresenter.hxx:221
static std::string readStringAttribute(const H5::H5Object &group, const char *name)
Definition: HDF5Utils.hxx:397
void SetReference(ImageType *ds)
Definition: itkStandardImageRepresenter.hxx:202
static H5::DataSet writeInt(const H5::CommonFG &fg, const char *name, int value)
Definition: HDF5Utils.hxx:426
Generic Exception class for the statismo Library.
Definition: Exceptions.h:68
static void readVector(const H5::CommonFG &fg, const char *name, unsigned nElements, VectorType &vector)
Definition: HDF5Utils.hxx:296
static H5::DataSet writeVector(const H5::CommonFG &fg, const char *name, const VectorType &vector)
Definition: HDF5Utils.hxx:363
float ScalarType
the type that is used for all vector and matrices throughout the library.
Definition: CommonTypes.h:60
static void getFileFromHDF5(const H5::CommonFG &fg, const char *name, const char *filename)
Definition: HDF5Utils.hxx:462
static int readInt(const H5::CommonFG &fg, const char *name)
Definition: HDF5Utils.hxx:434
static int readIntAttribute(const H5::H5Object &group, const char *name)
Definition: HDF5Utils.hxx:416