Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write image as a sequence #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
65 changes: 65 additions & 0 deletions include/util.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,71 @@
#include <itkLogger.h>
#include <itkImageFileWriter.h>
#include <itkImageFileReader.h>
#include <itkExtractImageFilter.h>
#include <type_traits>
#include <concepts>

#include "config.h"


template<typename T> requires std::integral<T>
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<typename TImage>
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<ImagePixelType, Dimension-1>;

using FileSequenceWriterType = itk::ImageFileWriter<ImagePlaneType>;
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<TImage, ImagePlaneType>;
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<typename TImage>
void writeImage(const std::string & filePath, const typename TImage::Pointer &image,
Expand Down