From 7b90d946eabbf39df09e3b7d041e70eae4c40196 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Presles=20Benoi=CC=82t?= Date: Tue, 16 Dec 2014 11:42:37 +0100 Subject: [PATCH] new tool to calculate the barycenter of a binary image --- tools/CMakeLists.txt | 5 + tools/clitkImageBarycenter.cxx | 50 ++++++ tools/clitkImageBarycenter.ggo | 12 ++ tools/clitkImageBarycenterGenericFilter.h | 69 ++++++++ tools/clitkImageBarycenterGenericFilter.txx | 175 ++++++++++++++++++++ 5 files changed, 311 insertions(+) create mode 100644 tools/clitkImageBarycenter.cxx create mode 100644 tools/clitkImageBarycenter.ggo create mode 100644 tools/clitkImageBarycenterGenericFilter.h create mode 100644 tools/clitkImageBarycenterGenericFilter.txx diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 6bd72a3..016b26a 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -325,6 +325,11 @@ if(CLITK_BUILD_TOOLS) target_link_libraries(clitkBlurImage clitkCommon ) set(TOOLS_INSTALL ${TOOLS_INSTALL} clitkBlurImage) + WRAP_GGO(clitkImageBarycenter_GGO_C clitkImageBarycenter.ggo) + add_executable(clitkImageBarycenter clitkImageBarycenter.cxx ${clitkImageBarycenter_GGO_C}) + target_link_libraries(clitkImageBarycenter clitkCommon ) + set(TOOLS_INSTALL ${TOOLS_INSTALL} clitkBlurImage) + #========================================================= option(CLITK_USE_ROOT "Build experimental tools using root" OFF) if (CLITK_USE_ROOT) diff --git a/tools/clitkImageBarycenter.cxx b/tools/clitkImageBarycenter.cxx new file mode 100644 index 0000000..03dea82 --- /dev/null +++ b/tools/clitkImageBarycenter.cxx @@ -0,0 +1,50 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ + +/* ================================================= + * @file clitkImageBarycenterGenericFilter.txx + * @author xxx + * @date 29 June 2029 + * + * @brief ImageBarycenter an image + * + ===================================================*/ + +// clitk +#include "clitkImageBarycenter_ggo.h" +#include "clitkImageBarycenterGenericFilter.h" + +//-------------------------------------------------------------------- +int main(int argc, char * argv[]) +{ + + // Init command line + GGO(clitkImageBarycenter, args_info); + CLITK_INIT; + + // Filter + typedef clitk::ImageBarycenterGenericFilter FilterType; + FilterType::Pointer filter = FilterType::New(); + + filter->SetArgsInfo(args_info); + filter->Update(); + + return EXIT_SUCCESS; +}// end main + +//-------------------------------------------------------------------- diff --git a/tools/clitkImageBarycenter.ggo b/tools/clitkImageBarycenter.ggo new file mode 100644 index 0000000..b366eb9 --- /dev/null +++ b/tools/clitkImageBarycenter.ggo @@ -0,0 +1,12 @@ +#File clitkImageBarycenter.ggo +package "clitkImageBarycenter" +version "1.0" +purpose "" + +option "config" - "Config file" string optional +option "verbose" v "Verbose" flag off + +option "imagetypes" - "Display allowed image types" flag off + +option "input" i "Input image filename" string required +option "output" o "Output image filename" string required diff --git a/tools/clitkImageBarycenterGenericFilter.h b/tools/clitkImageBarycenterGenericFilter.h new file mode 100644 index 0000000..695be52 --- /dev/null +++ b/tools/clitkImageBarycenterGenericFilter.h @@ -0,0 +1,69 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ +#ifndef CLITKFooIMAGEGENERICFILTER_H +#define CLITKFooIMAGEGENERICFILTER_H +#include "clitkIO.h" +#include "clitkImageToImageGenericFilter.h" + +//-------------------------------------------------------------------- +namespace clitk +{ + +template +class ITK_EXPORT ImageBarycenterGenericFilter: + public ImageToImageGenericFilter > +{ + +public: + + //-------------------------------------------------------------------- + ImageBarycenterGenericFilter(); + + //-------------------------------------------------------------------- + typedef ImageBarycenterGenericFilter Self; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + //-------------------------------------------------------------------- + // Method for creation through the object factory + // and Run-time type information (and related methods) + itkNewMacro(Self); + itkTypeMacro(ImageBarycenterGenericFilter, LightObject); + + //-------------------------------------------------------------------- + void SetArgsInfo(const args_info_type & a); + + //-------------------------------------------------------------------- + // Main function called each time the filter is updated + template + void UpdateWithInputImageType(); + +protected: + template void InitializeImageType(); + args_info_type mArgsInfo; + +}; // end class +//-------------------------------------------------------------------- + +} // end namespace clitk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitkImageBarycenterGenericFilter.txx" +#endif + +#endif // #define clitkImageBarycenterGenericFilter_h diff --git a/tools/clitkImageBarycenterGenericFilter.txx b/tools/clitkImageBarycenterGenericFilter.txx new file mode 100644 index 0000000..e4a6eaf --- /dev/null +++ b/tools/clitkImageBarycenterGenericFilter.txx @@ -0,0 +1,175 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ +#ifndef clitkImageBarycenterGenericFilter_txx +#define clitkImageBarycenterGenericFilter_txx + +/* ================================================= + * @file clitkImageBarycenterGenericFilter.txx + * @author Jef Vandemeulebroucke + * @date 29 june 2009 + * + * @brief + * + ===================================================*/ + +// itk include +#include "itkConnectedComponentImageFilter.h" +#include "itkLabelImageToShapeLabelMapFilter.h" +#include + +namespace clitk +{ + +//-------------------------------------------------------------------- +template +ImageBarycenterGenericFilter::ImageBarycenterGenericFilter(): + ImageToImageGenericFilter("ImageBarycenter") +{ + InitializeImageType<2>(); + InitializeImageType<3>(); + //InitializeImageType<4>(); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +template +void ImageBarycenterGenericFilter::InitializeImageType() +{ + ADD_DEFAULT_IMAGE_TYPES(Dim); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void ImageBarycenterGenericFilter::SetArgsInfo(const args_info_type & a) +{ + mArgsInfo=a; + this->SetIOVerbose(mArgsInfo.verbose_flag); + if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes(); + + if (mArgsInfo.input_given) { + this->SetInputFilename(mArgsInfo.input_arg); + } + if (mArgsInfo.output_given) { + this->SetOutputFilename(mArgsInfo.output_arg); + } +} +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +// Update with the number of dimensions and the pixeltype +//-------------------------------------------------------------------- +template +template +void +ImageBarycenterGenericFilter::UpdateWithInputImageType() +{ + + // Reading input + typename InputImageType::Pointer input = this->template GetInput(0); + + // Main filter + typedef typename InputImageType::PixelType PixelType; + typedef itk::Image OutputImageType; + + // Filter + typedef unsigned short LabelType; + typedef itk::ShapeLabelObject< LabelType, InputImageType::ImageDimension > ShapeLabelObjectType; + typedef itk::LabelMap< ShapeLabelObjectType > LabelMapType; + + typedef itk::ConnectedComponentImageFilter ConnectedComponentImageFilterType; + typedef itk::LabelImageToShapeLabelMapFilter< OutputImageType, LabelMapType> I2LType; + typename ConnectedComponentImageFilterType::Pointer connected = ConnectedComponentImageFilterType::New (); + connected->SetInput(input); + connected->Update(); + typedef itk::LabelImageToShapeLabelMapFilter< OutputImageType, LabelMapType> I2LType; + typename I2LType::Pointer i2l = I2LType::New(); + i2l->SetInput( connected->GetOutput() ); + i2l->SetComputePerimeter(true); + i2l->Update(); + LabelMapType *labelMap = i2l->GetOutput(); + std::cout << "The input image has " << labelMap->GetNumberOfLabelObjects() << " labels." << std::endl; + // Retrieve all attributes + //Only 1 object so: + unsigned int n = 0; + //for (unsigned int n = 0; n < labelMap->GetNumberOfLabelObjects(); ++n) + //{ + ShapeLabelObjectType *labelObject = labelMap->GetNthLabelObject(n); + std::cout << "Label: " + << typename itk::NumericTraits< typename LabelMapType::LabelType >::PrintType(labelObject->GetLabel()) << std::endl; + //std::cout << " BoundingBox: " + // << labelObject->GetBoundingBox() << std::endl; + //std::cout << " NumberOfPixels: " + // << labelObject->GetNumberOfPixels() << std::endl; + //std::cout << " PhysicalSize: " + // << labelObject->GetPhysicalSize() << std::endl; + std::cout << " Centroid: " + << labelObject->GetCentroid() << std::endl; + //std::cout << " NumberOfPixelsOnBorder: " + // << labelObject->GetNumberOfPixelsOnBorder() << std::endl; + //std::cout << " PerimeterOnBorder: " + // << labelObject->GetPerimeterOnBorder() << std::endl; + //std::cout << " FeretDiameter: " + // << labelObject->GetFeretDiameter() << std::endl; + //std::cout << " PrincipalMoments: " + // << labelObject->GetPrincipalMoments() << std::endl; + //std::cout << " PrincipalAxes: " + // << labelObject->GetPrincipalAxes() << std::endl; + //std::cout << " Elongation: " + // << labelObject->GetElongation() << std::endl; + //std::cout << " Perimeter: " + // << labelObject->GetPerimeter() << std::endl; + //std::cout << " Roundness: " + // << labelObject->GetRoundness() << std::endl; + //std::cout << " EquivalentSphericalRadius: " + // << labelObject->GetEquivalentSphericalRadius() << std::endl; + //std::cout << " EquivalentSphericalPerimeter: " + // << labelObject->GetEquivalentSphericalPerimeter() << std::endl; + //std::cout << " EquivalentEllipsoidDiameter: " + // << labelObject->GetEquivalentEllipsoidDiameter() << std::endl; + //std::cout << " Flatness: " + // << labelObject->GetFlatness() << std::endl; + //std::cout << " PerimeterOnBorderRatio: " + // << labelObject->GetPerimeterOnBorderRatio() << std::endl; + //} + + typename OutputImageType::Pointer outputImage = OutputImageType::New(); + typename OutputImageType::RegionType region_outputImage(input->GetLargestPossibleRegion()); + outputImage->SetRegions(region_outputImage); + outputImage->SetOrigin(input->GetOrigin()); + outputImage->SetSpacing(input->GetSpacing()); + outputImage->SetDirection(input->GetDirection()); + outputImage->Allocate(); + outputImage->FillBuffer(0); + + typename InputImageType::IndexType barycenterIndex; + input->TransformPhysicalPointToIndex(labelObject->GetCentroid(),barycenterIndex); + + outputImage->SetPixel(barycenterIndex,1); + // Write/Save results + this->template SetNextOutput(outputImage); +} +//-------------------------------------------------------------------- + + +}//end clitk + +#endif //#define clitkImageBarycenterGenericFilter_txx -- 2.47.1