-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add of a feature to create a mask from an image.\nWritten by Thoms Du…
…rantel
- Loading branch information
Gregoire Ville
committed
Apr 29, 2024
1 parent
185203f
commit 7ca5683
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Anima/math-tools/common_tools/image_to_mask/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
if(BUILD_TOOLS) | ||
|
||
project(animaImageToMask) | ||
|
||
## ############################################################################# | ||
## List Sources | ||
## ############################################################################# | ||
|
||
list_source_files(${PROJECT_NAME} | ||
${CMAKE_CURRENT_SOURCE_DIR} | ||
) | ||
|
||
## add executable | ||
## ############################################################################# | ||
|
||
add_executable(${PROJECT_NAME} | ||
${${PROJECT_NAME}_CFILES} | ||
) | ||
|
||
|
||
## ############################################################################# | ||
## Link | ||
## ############################################################################# | ||
|
||
target_link_libraries(${PROJECT_NAME} | ||
${ITKIO_LIBRARIES} | ||
) | ||
|
||
## ############################################################################# | ||
## install | ||
## ############################################################################# | ||
|
||
set_exe_install_rules(${PROJECT_NAME}) | ||
|
||
endif() |
108 changes: 108 additions & 0 deletions
108
Anima/math-tools/common_tools/image_to_mask/animaImageToMask.cxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include <tclap/CmdLine.h> | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include <itkImage.h> | ||
#include <itkVectorImage.h> | ||
#include <itkImageRegionIterator.h> | ||
|
||
#include <animaReadWriteFunctions.h> | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION); | ||
|
||
TCLAP::ValueArg<std::string> inArg("i","inputfile","input image",true,"","input image",cmd); | ||
TCLAP::ValueArg<std::string> outArg("o","outputfile","output mask",true,"","output mask",cmd); | ||
|
||
try | ||
{ | ||
cmd.parse(argc,argv); | ||
} | ||
catch (TCLAP::ArgException& e) | ||
{ | ||
std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl; | ||
return(1); | ||
} | ||
|
||
typedef itk::Image <unsigned int,3> ImageType; | ||
typedef itk::Image <double,4> Image4DType; | ||
typedef itk::VectorImage <double,3> VectorImageType; | ||
|
||
itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(inArg.getValue().c_str(), | ||
itk::IOFileModeEnum::ReadMode); | ||
|
||
imageIO->SetFileName(inArg.getValue()); | ||
imageIO->ReadImageInformation(); | ||
|
||
bool vectorImage = (imageIO->GetNumberOfComponents() > 1); | ||
|
||
if (vectorImage) | ||
{ | ||
// Vectorial case | ||
VectorImageType::Pointer image = anima::readImage <VectorImageType> (inArg.getValue()); | ||
|
||
ImageType::Pointer outImage = ImageType::New(); | ||
outImage->Initialize(); | ||
// outImage->SetNumberOfComponentsPerPixel(image->GetNumberOfComponentsPerPixel()); | ||
outImage->SetDirection(image->GetDirection()); | ||
outImage->SetSpacing(image->GetSpacing()); | ||
outImage->SetOrigin(image->GetOrigin()); | ||
|
||
VectorImageType::RegionType region = image->GetLargestPossibleRegion(); | ||
outImage->SetRegions(region); | ||
outImage->Allocate(); | ||
|
||
VectorImageType::PixelType zeroVec(image->GetNumberOfComponentsPerPixel()); | ||
zeroVec.Fill(0); | ||
|
||
itk::ImageRegionIterator <VectorImageType> inItr(image,region); | ||
itk::ImageRegionIterator <ImageType> outItr(outImage,outImage->GetLargestPossibleRegion()); | ||
|
||
while (!outItr.IsAtEnd()) | ||
{ | ||
if(inItr.Get() == zeroVec) | ||
outItr.Set(0); | ||
else | ||
outItr.Set(1); | ||
|
||
++inItr; | ||
++outItr; | ||
} | ||
|
||
anima::writeImage <ImageType> (outArg.getValue(),outImage); | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|
||
ImageType::Pointer image = anima::readImage <ImageType> (inArg.getValue()); | ||
|
||
ImageType::Pointer outImage = ImageType::New(); | ||
outImage->Initialize(); | ||
outImage->SetDirection(image->GetDirection()); | ||
outImage->SetSpacing(image->GetSpacing()); | ||
outImage->SetOrigin(image->GetOrigin()); | ||
|
||
ImageType::RegionType largestRegion = image->GetLargestPossibleRegion(); | ||
outImage->SetRegions(largestRegion); | ||
outImage->Allocate(); | ||
outImage->FillBuffer(0.0); | ||
|
||
itk::ImageRegionIterator <ImageType> inItr(image,image->GetLargestPossibleRegion()); | ||
itk::ImageRegionIterator <ImageType> outItr(outImage,outImage->GetLargestPossibleRegion()); | ||
|
||
while (!outItr.IsAtEnd()) | ||
{ | ||
if(inItr.Get() == 0.0) | ||
outItr.Set(0); | ||
else | ||
outItr.Set(1); | ||
|
||
++inItr; | ||
++outItr; | ||
} | ||
|
||
anima::writeImage <ImageType> (outArg.getValue(),outImage); | ||
|
||
return EXIT_SUCCESS; | ||
} |