int
main(int argc, char * argv[])
{
if (argc < 3)
{
std::cerr << "Missing parameters. " << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " input4DImageFile output3DImageFile"
<< std::endl;
return EXIT_FAILURE;
}
using PixelType = unsigned char;
reader4D->SetFileName(argv[1]);
try
{
reader4D->Update();
}
{
std::cerr << "Error reading the image" << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
using Spacing3DType = Image3DType::SpacingType;
using Spacing4DType = Image4DType::SpacingType;
Index3DType index3D;
Size3DType size3D;
Spacing3DType spacing3D;
Origin3DType origin3D;
Index4DType index4D = region4D.
GetIndex();
Size4DType size4D = region4D.GetSize();
Spacing4DType spacing4D = image4D->GetSpacing();
Origin4DType origin4D = image4D->GetOrigin();
for (unsigned int i = 0; i < 3; ++i)
{
size3D[i] = size4D[i];
index3D[i] = index4D[i];
spacing3D[i] = spacing4D[i];
origin3D[i] = origin4D[i];
}
image3D->SetSpacing(spacing3D);
image3D->SetOrigin(origin3D);
Region3DType region3D;
region3D.SetSize(size3D);
image3D->SetRegions(region3D);
image3D->Allocate();
const unsigned int timeLength = region4D.GetSize()[3];
IteratorType it(image4D, region4D);
it.SetDirection(3);
it.GoToBegin();
while (!it.IsAtEnd())
{
SumType sum{};
it.GoToBeginOfLine();
index4D = it.GetIndex();
while (!it.IsAtEndOfLine())
{
sum += it.Get();
++it;
}
const MeanType mean =
static_cast<MeanType>(sum) / static_cast<MeanType>(timeLength);
index3D[0] = index4D[0];
index3D[1] = index4D[1];
index3D[2] = index4D[2];
image3D->SetPixel(index3D, static_cast<PixelType>(mean));
it.NextLine();
}
writer3D->SetFileName(argv[2]);
writer3D->SetInput(image3D);
try
{
writer3D->Update();
}
{
std::cerr << "Error writing the image" << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}