From: David Sarrut Date: Mon, 26 Sep 2011 07:04:33 +0000 (+0200) Subject: File to manage a list of RelativePosition filters X-Git-Tag: v1.3.0~227 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=adeedbea1fbf8bc34e2c9565867b992ad13ccc57;p=clitk.git File to manage a list of RelativePosition filters --- diff --git a/segmentation/clitkRelativePositionList.h b/segmentation/clitkRelativePositionList.h new file mode 100644 index 0000000..20c6b77 --- /dev/null +++ b/segmentation/clitkRelativePositionList.h @@ -0,0 +1,109 @@ +/*========================================================================= + 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://oncora1.lyon.fnclcc.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 CLITKRELATIVEPOSITIONLIST_H +#define CLITKRELATIVEPOSITIONLIST_H + +// clitk +#include "clitkSegmentationUtils.h" +#include "clitkFilterWithAnatomicalFeatureDatabaseManagement.h" +#include "clitkRelativePosition_ggo.h" + +namespace clitk { + + /*-------------------------------------------------------------------- + Manage a list of RelativePosition operations, to be performed on + the same input image, with different objects and parameters. + ------------------------------------------------------------------*/ + + template + class ITK_EXPORT RelativePositionList: + public virtual clitk::FilterBase, + public clitk::FilterWithAnatomicalFeatureDatabaseManagement, + public itk::ImageToImageFilter + { + + public: + /** Standard class typedefs. */ + typedef itk::ImageToImageFilter Superclass; + typedef RelativePositionList Self; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(RelativePositionList, ImageToImageFilter); + + /** Some convenient typedefs. */ + typedef TImageType ImageType; + typedef typename ImageType::ConstPointer ImageConstPointer; + typedef typename ImageType::Pointer ImagePointer; + typedef typename ImageType::RegionType ImageRegionType; + typedef typename ImageType::PixelType ImagePixelType; + typedef typename ImageType::SizeType ImageSizeType; + typedef typename ImageType::IndexType ImageIndexType; + typedef typename ImageType::PointType ImagePointType; + typedef struct args_info_clitkRelativePosition ArgsInfoType; + typedef SliceBySliceRelativePositionFilter SliceRelPosFilterType; + typedef AddRelativePositionConstraintToLabelImageFilter RelPosFilterType; + + /** ImageDimension constants */ + itkStaticConstMacro(ImageDimension, unsigned int, ImageType::ImageDimension); + FILTERBASE_INIT; + + itkGetConstMacro(BackgroundValue, ImagePixelType); + itkGetConstMacro(ForegroundValue, ImagePixelType); + itkSetMacro(BackgroundValue, ImagePixelType); + itkSetMacro(ForegroundValue, ImagePixelType); + + itkSetMacro(InputName, std::string); + itkGetConstMacro(InputName, std::string); + + void Read(std::string filename); + void SetFilterOptions(typename RelPosFilterType::Pointer filter, ArgsInfoType & options); + + protected: + RelativePositionList(); + virtual ~RelativePositionList() {} + + private: + RelativePositionList(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + void GenerateInputRequestedRegion(); + void GenerateOutputInformation(); + void GenerateData(); + + std::string m_InputName; + ImagePixelType m_BackgroundValue; + ImagePixelType m_ForegroundValue; + typename SliceRelPosFilterType::Pointer mFilter; + std::vector mArgsInfoList; + ImagePointer m_working_input; + + }; // end class + //-------------------------------------------------------------------- + +} // end namespace clitk +//-------------------------------------------------------------------- + +#include "clitkRelativePositionList.txx" + +#endif diff --git a/segmentation/clitkRelativePositionList.txx b/segmentation/clitkRelativePositionList.txx new file mode 100644 index 0000000..aff21e4 --- /dev/null +++ b/segmentation/clitkRelativePositionList.txx @@ -0,0 +1,224 @@ +/*========================================================================= + 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://oncora1.lyon.fnclcc.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 + ======================================================================-====*/ + + +//-------------------------------------------------------------------- +template +clitk::RelativePositionList:: +RelativePositionList() { +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void +clitk::RelativePositionList:: +Read(std::string filename) { + /* + The goal here is to read a text file that contains options for the + RelativePosition filter. The text file contains options in the + same form of the config file of the clitkRelativePosition tool. In + this text file, each time a "object" option is found, a new set of + options is considered. + */ + + // Open the file + std::ifstream is; + openFileForReading(is, filename); + + // Read input -> the structure name that will be used as input + // (initial support) + skipComment(is); + std::string s; + is >> s; + if (s != "input") { + FATAL("while reading RelativePositionList file. This file must start with 'input = '"); + exit(0); + } + is >> s; + if (s != "=") { + FATAL("while reading RelativePositionList file. This file must start with 'input = '"); + exit(0); + } + std::string name; + is >> name; + skipComment(is); + + // Create a temporary filename + char n[] = "tmp_clitkRelativePosition_XXXXXX"; + mkstemp(n); // tmpnam(n); + std::string tmpfilename(n); + + // Loop on the file text ; Every time we see a "object" token, we + // split the file. + while (is) { + bool stop=false; + std::ostringstream ss; + // first part of ss is the last 'object = ' found, stored in s + ss << s << std::endl; + while (!stop) { + skipComment(is); + if (!is) stop = true; + else { + std::getline(is, s); + // DD(s); + if (s.find("object") != std::string::npos) stop=true; + else ss << s << std::endl; + if (!is) stop = true; + } + } + std::string text = ss.str(); + if (text.size() > 10) { // if too small, it is not a list of option + std::ofstream os; + openFileForWriting(os, tmpfilename); + os << text; + os.close(); + + // Create a struct to store options + ArgsInfoType args_info; + args_info.input_given = 1; + args_info.input_arg = new char[1]; + args_info.output_given = 1; + args_info.output_arg = new char[1]; + std::vector writable(tmpfilename.size() + 1); + std::copy(tmpfilename.begin(), tmpfilename.end(), writable.begin()); + cmdline_parser_clitkRelativePosition_configfile(&writable[0], &args_info, 1, 1, 0); + + // Store the args + mArgsInfoList.push_back(args_info); + + // Delete the temporary file + std::remove(tmpfilename.c_str()); + } + } + + // Set the main input name + SetInputName(name); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void +clitk::RelativePositionList:: +GenerateInputRequestedRegion() +{ + // Call default + itk::ImageToImageFilter::GenerateInputRequestedRegion(); + // Get input pointers and set requested region to common region + ImagePointer input1 = dynamic_cast(itk::ProcessObject::GetInput(0)); + input1->SetRequestedRegion(input1->GetLargestPossibleRegion()); +} +//-------------------------------------------------------------------- + + + +//-------------------------------------------------------------------- +template +void +clitk::RelativePositionList:: +GenerateOutputInformation() { + + // Get input + m_working_input = dynamic_cast(itk::ProcessObject::GetInput(0)); + + // Loop on RelativePositionList of operations + std::string s = GetInputName(); + for(uint i=0; i(&*relPosFilter)->SetDirection(2); + } + else { + relPosFilter = clitk::AddRelativePositionConstraintToLabelImageFilter::New(); + } + relPosFilter->VerboseStepFlagOff(); + relPosFilter->WriteStepFlagOff(); + relPosFilter->SetVerboseImageSizeFlag(GetVerboseImageSizeFlag()); + relPosFilter->SetInput(m_working_input); + SetFilterOptions(relPosFilter, mArgsInfoList[i]); + //relPosFilter->PrintOptions(); + relPosFilter->Update(); + m_working_input = relPosFilter->GetOutput(); + StopCurrentStep(m_working_input); + } +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void +clitk::RelativePositionList:: +GenerateData() +{ + // Final Step -> set output + this->GraftOutput(m_working_input); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void +clitk::RelativePositionList:: +SetFilterOptions(typename RelPosFilterType::Pointer filter, + ArgsInfoType & options) { + + if (options.orientation_given != 1) { + DD("ERRROR DEBUG TODO no more than 1 orientation yet"); + exit(0); + } + + ImagePointer object = GetAFDB()->template GetImage(options.object_arg); + filter->SetInputObject(object); + filter->SetFuzzyThreshold(options.threshold_arg); + filter->SetInverseOrientationFlag(options.inverse_flag); // MUST BE BEFORE AddOrientationTypeString + for(uint i=0; iAddOrientationTypeString(options.orientation_arg[i]); + filter->SetIntermediateSpacing(options.spacing_arg); + if (options.spacing_arg == -1) filter->IntermediateSpacingFlagOff(); + filter->IntermediateSpacingFlagOn(); + + if (options.sliceBySlice_flag) { + SliceRelPosFilterType * f = dynamic_cast(&*filter); + f->SetUniqueConnectedComponentBySliceFlag(options.uniqueCCL_flag); + f->SetObjectCCLSelectionFlag(options.uniqueObjectCCL_flag); + f->IgnoreEmptySliceObjectFlagOn(); + //filter->SetObjectCCLSelectionDimension(0); + //filter->SetObjectCCLSelectionDirection(-1); + //filter->SetAutoCropFlag(false); + } + filter->SetAutoCropFlag(!options.noAutoCrop_flag); +} +//-------------------------------------------------------------------- diff --git a/segmentation/clitkStructuresExtractionFilter.h b/segmentation/clitkStructuresExtractionFilter.h new file mode 100644 index 0000000..3e7d303 --- /dev/null +++ b/segmentation/clitkStructuresExtractionFilter.h @@ -0,0 +1,120 @@ +/*========================================================================= + 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 CLITKSTRUCTURESEXTRACTIONFILTER_H +#define CLITKSTRUCTURESEXTRACTIONFILTER_H + +// clitk +#include "clitkFilterWithAnatomicalFeatureDatabaseManagement.h" +#include "clitkFilterBase.h" +#include "clitkRelativePositionList.h" + +namespace clitk { + + //-------------------------------------------------------------------- + /* + - Convenient class to add some capabilities to a filter : + FilterBase, FilterWithAnatomicalFeatureDatabaseManagement and + RelativePositionList + */ + //-------------------------------------------------------------------- + template + class StructuresExtractionFilter: + public virtual FilterBase, + public clitk::FilterWithAnatomicalFeatureDatabaseManagement, + public itk::ImageToImageFilter > + { + public: + // Standard class typedefs + typedef StructuresExtractionFilter Self; + typedef itk::ImageToImageFilter > Superclass; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + // Run-time type information (and related methods) + itkTypeMacro(StructuresExtractionFilter, ImageToImageFilter); + + /** Some convenient typedefs. */ + typedef TImageType ImageType; + typedef typename ImageType::ConstPointer ImageConstPointer; + typedef typename ImageType::Pointer ImagePointer; + typedef typename ImageType::RegionType ImageRegionType; + typedef typename ImageType::PixelType ImagePixelType; + typedef typename ImageType::SizeType ImageSizeType; + typedef typename ImageType::IndexType ImageIndexType; + typedef typename ImageType::PointType ImagePointType; + + typedef uchar MaskImagePixelType; + typedef itk::Image MaskImageType; + typedef typename MaskImageType::Pointer MaskImagePointer; + typedef typename MaskImageType::RegionType MaskImageRegionType; + typedef typename MaskImageType::SizeType MaskImageSizeType; + typedef typename MaskImageType::IndexType MaskImageIndexType; + typedef typename MaskImageType::PointType MaskImagePointType; + + typedef itk::Image MaskSliceType; + typedef typename MaskSliceType::Pointer MaskSlicePointer; + typedef typename MaskSliceType::PointType MaskSlicePointType; + typedef typename MaskSliceType::RegionType MaskSliceRegionType; + typedef typename MaskSliceType::SizeType MaskSliceSizeType; + typedef typename MaskSliceType::IndexType MaskSliceIndexType; + + /** ImageDimension constants */ + itkStaticConstMacro(ImageDimension, unsigned int, ImageType::ImageDimension); + FILTERBASE_INIT; + + itkGetConstMacro(BackgroundValue, MaskImagePixelType); + itkGetConstMacro(ForegroundValue, MaskImagePixelType); + itkSetMacro(BackgroundValue, MaskImagePixelType); + itkSetMacro(ForegroundValue, MaskImagePixelType); + + // RelativePositionList management + void AddRelativePositionListFilename(std::string s); + MaskImagePointer ApplyRelativePositionList(std::string name, MaskImageType * input); + + protected: + StructuresExtractionFilter(); + virtual ~StructuresExtractionFilter() {} + + virtual void GenerateData() {} + + MaskImagePixelType m_BackgroundValue; + MaskImagePixelType m_ForegroundValue; + + // RelativePositionList + std::vector mListOfRelativePositionListFilename; + typedef clitk::RelativePositionList RelPosListType; + typedef typename RelPosListType::Pointer RelPosListPointer; + std::map mMapOfRelativePositionList; + + private: + StructuresExtractionFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + }; // end class + //-------------------------------------------------------------------- + +} // end namespace clitk +//-------------------------------------------------------------------- + +#include "clitkStructuresExtractionFilter.txx" + +#endif // CLITKSTRUCTURESEXTRACTIONFILTER_H diff --git a/segmentation/clitkStructuresExtractionFilter.txx b/segmentation/clitkStructuresExtractionFilter.txx new file mode 100644 index 0000000..761138e --- /dev/null +++ b/segmentation/clitkStructuresExtractionFilter.txx @@ -0,0 +1,81 @@ +/*========================================================================= + 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 + ===========================================================================**/ + +// clitk +#include "clitkStructuresExtractionFilter.h" + +//-------------------------------------------------------------------- +template +clitk::StructuresExtractionFilter:: +StructuresExtractionFilter(): + clitk::FilterBase(), + clitk::FilterWithAnatomicalFeatureDatabaseManagement(), + itk::ImageToImageFilter() +{ + SetBackgroundValue(0); + SetForegroundValue(1); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void +clitk::StructuresExtractionFilter:: +AddRelativePositionListFilename(std::string s) { + mListOfRelativePositionListFilename.push_back(s); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +typename clitk::StructuresExtractionFilter::MaskImagePointer +clitk::StructuresExtractionFilter:: +ApplyRelativePositionList(std::string name, MaskImageType * input) +{ + // Create all RelativePositionList + for(int i=0; iSetAFDB(GetAFDB()); + rpl->Read(mListOfRelativePositionListFilename[i]); + std::string s = rpl->GetInputName(); + mMapOfRelativePositionList[s] = rpl; + } + + RelPosListPointer relpos; + if (mMapOfRelativePositionList.find(name) == mMapOfRelativePositionList.end()) { + std::cerr << "Warning: I do not find '" << name << "' in the RelativePositionList." << std::endl; + //DD("Not find !"); // do nothing + } + else { + relpos = mMapOfRelativePositionList[name]; + relpos->SetVerboseStepFlag(GetVerboseStepFlag()); + relpos->SetCurrentStepBaseId(GetCurrentStepBaseId()); + relpos->SetCurrentStepId(GetCurrentStepId()); + relpos->SetCurrentStepNumber(GetCurrentStepNumber()); + relpos->SetWriteStepFlag(GetWriteStepFlag()); + relpos->SetInput(input); + relpos->Update(); + input = relpos->GetOutput(); + } + SetCurrentStepNumber(relpos->GetCurrentStepNumber()); + return input; +} +//-------------------------------------------------------------------- +