From 2835ff98b414cc4a7f32d2a1a0bbf14313ee66fa Mon Sep 17 00:00:00 2001 From: tabish Date: Wed, 6 Nov 2024 11:30:52 -0500 Subject: [PATCH] Write image as a sequence --- CMakeLists.txt | 2 +- include/util.hxx | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1489bd3..a454efa 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ endif() message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}") -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(DEBUG_CXX_FLAGS "-ggdb;-O1;-Wall;-Wextra;-march=native;-fPIC") set(RELEASE_CXX_FLAGS "-O3;-Wall;-march=native;-fPIC;-ffast-math") diff --git a/include/util.hxx b/include/util.hxx index 90dc3df..f5301cb 100644 --- a/include/util.hxx +++ b/include/util.hxx @@ -25,6 +25,71 @@ #include #include #include +#include +#include +#include + +#include "config.h" + + +template requires std::integral +std::string int_to_string(T n, int width = 5){ + std::ostringstream ss; + ss.width(width); + ss.fill('0'); + ss << n; + return ss.str(); +} + +template +void writeImageSequence(const std::string & filePath, const typename TImage::Pointer &image, + const itk::Logger::Pointer &logger) { + using ImagePixelType = typename TImage::PixelType; + constexpr unsigned Dimension = TImage::ImageDimension; + using ImagePlaneType = itk::Image; + + using FileSequenceWriterType = itk::ImageFileWriter; + auto writer = FileSequenceWriterType::New(); + + auto totalRegion = image->GetLargestPossibleRegion(); + auto index = totalRegion.GetIndex(); + auto fullSize = totalRegion.GetSize(); + int planeCount = fullSize[Dimension-1]; + + using ExtractFilterType = itk::ExtractImageFilter; + auto extractFilter = ExtractFilterType::New(); + extractFilter->SetDirectionCollapseToSubmatrix(); + extractFilter->SetInput(image); + + typename TImage::RegionType desiredPlane; + + fullSize[Dimension-1] = 0; + desiredPlane.SetSize(fullSize); + + auto stem = fs::path(filePath).stem(); + auto extension = fs::path(filePath).extension(); + + for(int plane = 0;plane < planeCount; ++plane){ + auto fname = (stem.string() +"-" + int_to_string(plane) + extension.string() ); + fs::path planePath = fs::path(filePath).parent_path()/ fname; + auto currentIndex = index; + currentIndex[Dimension-1] += plane; + desiredPlane.SetIndex(currentIndex); + + extractFilter->SetExtractionRegion(desiredPlane); + //extractFilter->Update(); + writer->SetFileName(planePath ); + writer->SetInput( extractFilter->GetOutput() ); + try { + writer->Update(); + logger->Debug( "Wrote file " + std::string(writer->GetFileName()) + "\n"); + }catch (itk::ExceptionObject &e){ + logger->Critical( "Failed writing " + std::string(writer->GetFileName()) + "\n"); + logger->Critical( std::string(e.what()) + "\n"); + } + } +} + template void writeImage(const std::string & filePath, const typename TImage::Pointer &image,