diff --git a/apps/VoxelizerTool/VoxelizerToolHelper.cpp b/apps/VoxelizerTool/VoxelizerToolHelper.cpp index fc598ab..fb0e375 100644 --- a/apps/VoxelizerTool/VoxelizerToolHelper.cpp +++ b/apps/VoxelizerTool/VoxelizerToolHelper.cpp @@ -1,285 +1,249 @@ // ----------------------------------------------------------------------- // RTToolbox - DKFZ radiotherapy quantitative evaluation library // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See rttbCopyright.txt or // http://www.dkfz.de/en/sidt/projects/rttb/copyright.html // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notices for more information. // //------------------------------------------------------------------------ /*! // @file // @version $Revision: 1127 $ (last changed revision) // @date $Date: 2015-10-01 13:33:33 +0200 (Do, 01 Okt 2015) $ (last change date) // @author $Author: hentsch $ (last changed by) */ #include #include "rttbBoostMaskAccessor.h" #include "itkMacro.h" #include "VoxelizerToolHelper.h" #include "VoxelizerToolApplicationData.h" #include "rttbITKImageMaskAccessorConverter.h" #include "rttbImageWriter.h" #include "itkBinaryThresholdImageFilter.h" #include "itkAddImageFilter.h" +#include "rttbVOIindexIdentifier.h" + #include #include #include #include "rttbDicomFileStructureSetGenerator.h" -std::vector rttb::apps::voxelizerTool::filterForExpression(const std::vector& listOfExpressions, - const std::string& inputExpression) -{ - std::vector listOfFoundElements; - - for (unsigned int j = 0; j < listOfExpressions.size(); j++) - { - std::regex e(boost::algorithm::to_lower_copy(inputExpression)); - std::string s = boost::algorithm::to_lower_copy(listOfExpressions.at(j)); - - if (std::regex_match(s, e)) - { - listOfFoundElements.push_back(j); - } - } - - return listOfFoundElements; -} - void rttb::apps::voxelizerTool::removeSpecialCharacters(std::string& label) { //Replace / to avoid problems with directories (struct "Magen/DD" --> Magen/DD.mhd), delete trailing . to avoid filenames with two trailing points (Niere re. --> Niere re..mhd) while (label.find("/") != std::string::npos) { label.replace(label.find("/"), 1, "_"); } if (*label.rbegin() == '.') { label.replace(label.size() - 1, 1, ""); } } std::string rttb::apps::voxelizerTool::getFilenameWithoutEnding(const std::string& outfilename) { boost::filesystem::path p(outfilename); return p.replace_extension("").string(); } std::string rttb::apps::voxelizerTool::getFileEnding(const std::string& outfilename) { boost::filesystem::path p(outfilename); return p.extension().string(); } -std::vector rttb::apps::voxelizerTool::getAllLabels( - core::StructureSetGeneratorInterface::StructureSetPointer& structureSetPtr) -{ - std::vector allLabels; - - if (structureSetPtr != NULL) - { - for (int j = 0; j < structureSetPtr->getNumberOfStructures(); j++) - { - allLabels.push_back(structureSetPtr->getStructure(j)->getLabel()); - } - } - - return allLabels; -} - rttb::core::MaskAccessorInterface::MaskAccessorPointer rttb::apps::voxelizerTool::createMask( rttb::core::DoseAccessorInterface::DoseAccessorPointer& doseAccessorPtr, rttb::core::StructureSetGeneratorInterface::StructureSetPointer& structureSetPtr, bool strict, unsigned int indexOfStructure) { rttb::core::MaskAccessorInterface::MaskAccessorPointer maskAccessorPtr; if (doseAccessorPtr != nullptr && structureSetPtr != nullptr) { maskAccessorPtr = boost::make_shared (structureSetPtr->getStructure(indexOfStructure), doseAccessorPtr->getGeometricInfo(), strict); maskAccessorPtr->updateMask(); } return maskAccessorPtr; } void rttb::apps::voxelizerTool::writeMaskToFile(std::vector& maskVector, const std::string& outputFileName, bool voxelization) { if (!maskVector.empty()) { io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer itkImage; if (maskVector.size() > 1) { itkImage = addMultipleStructsToImage(maskVector); } else { io::itk::ITKImageMaskAccessorConverter maskAccessorConverter(maskVector.at(0)); maskAccessorConverter.process(); itkImage = maskAccessorConverter.getITKImage(); } if (voxelization) { itkImage = applyThresholdFilter(itkImage); } io::itk::ImageWriter writer(outputFileName, itkImage); writer.writeFile(); } } rttb::io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer rttb::apps::voxelizerTool::addMultipleStructsToImage( std::vector& maskVector) { std::vector listOfITKImages; for (int i = 0; i < maskVector.size(); i++) { io::itk::ITKImageMaskAccessorConverter maskAccessorConverter(maskVector.at(i)); maskAccessorConverter.process(); listOfITKImages.push_back(maskAccessorConverter.getITKImage()); } itk::AddImageFilter , itk::Image>::Pointer addFilter = itk::AddImageFilter , itk::Image>::New(); io::itk::ITKImageMaskAccessor::ITKMaskImageType::Pointer filterResult; for (int k = 1; k < listOfITKImages.size(); k++) { if (k == 1) { addFilter->SetInput1(listOfITKImages.at(0)); } else { addFilter->SetInput1(filterResult); } addFilter->SetInput2(listOfITKImages.at(k)); addFilter->Update(); filterResult = addFilter->GetOutput(); } io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer filterResultConst(filterResult); return filterResultConst; } rttb::io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer rttb::apps::voxelizerTool::applyThresholdFilter( io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer& itkImage) { itk::BinaryThresholdImageFilter< itk::Image, itk::Image >::Pointer filter = itk::BinaryThresholdImageFilter< itk::Image, itk::Image >::New(); filter->SetInput(itkImage); filter->SetLowerThreshold(0.5); filter->SetUpperThreshold(1.0); filter->SetInsideValue(1.0); filter->Update(); return filter->GetOutput(); } void rttb::apps::voxelizerTool::writeITKImageToFile(io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer& itkImage, const std::string& outputfilename, bool useCompression) { itk::ImageFileWriter< itk::Image >::Pointer writer = itk::ImageFileWriter< itk::Image >::New(); writer->SetFileName(outputfilename); writer->SetUseCompression(useCompression); writer->SetInput(itkImage); writer->Update(); } void rttb::apps::voxelizerTool::processData(rttb::apps::voxelizerTool::ApplicationData& appData) { std::cout << "done." << std::endl; std::cout << "searching for structs..."; - std::vector listOfCorrectElements, indexOfCorrectElements; + std::vector listOfCorrectElements, indexOfCorrectElements; - std::vector labelVector = getAllLabels(appData._struct); + indexOfCorrectElements = rttb::masks::VOIindexIdentifier::getIndicesByVoiRegex(appData._struct, appData._regEx); - indexOfCorrectElements = filterForExpression(labelVector, appData._regEx); std::copy(indexOfCorrectElements.begin(), indexOfCorrectElements.end(), std::back_inserter(listOfCorrectElements)); std::cout << "done." << std::endl; if (!listOfCorrectElements.empty()) { std::vector maskVector; if (appData._addStructures) { for (unsigned int i = 0; i < listOfCorrectElements.size(); i++) { - unsigned int labelIndex = listOfCorrectElements.at(i); maskVector.push_back(createMask(appData._dose, appData._struct, - !appData._noStrictVoxelization, labelIndex)); + !appData._noStrictVoxelization, listOfCorrectElements.at(i))); } writeMaskToFile(maskVector, appData._outputFilename, appData._booleanVoxelization); } else { unsigned int maxIterationCount = 1; if (appData._multipleStructs) { maxIterationCount = listOfCorrectElements.size(); } for (unsigned int i = 0; i < maxIterationCount; i++) { std::cout << "creating mask..."; maskVector.push_back(createMask(appData._dose, appData._struct, !appData._noStrictVoxelization, listOfCorrectElements.at(i))); std::cout << "done" << std::endl; - int labelIndex = listOfCorrectElements.at(i); - std::string labelOfInterest = labelVector.at(labelIndex); - rttb::apps::voxelizerTool::removeSpecialCharacters(labelOfInterest); + std::string labelOfInterest = rttb::masks::VOIindexIdentifier::getVoiNameByIndex(appData._struct, i); + removeSpecialCharacters(labelOfInterest); std::string outputName = appData._outputFilename; if (appData._multipleStructs) { - std::string fileName = rttb::apps::voxelizerTool::getFilenameWithoutEnding( + std::string fileName = getFilenameWithoutEnding( appData._outputFilename); - std::string fileEnding = rttb::apps::voxelizerTool::getFileEnding(appData._outputFilename); + std::string fileEnding = getFileEnding(appData._outputFilename); outputName = fileName + "_" + labelOfInterest + fileEnding; } writeMaskToFile(maskVector, outputName, appData._booleanVoxelization); } } } else { std::cout << "No struct found" << std::endl; } } \ No newline at end of file diff --git a/apps/VoxelizerTool/VoxelizerToolHelper.h b/apps/VoxelizerTool/VoxelizerToolHelper.h index ae3ba7e..61c770c 100644 --- a/apps/VoxelizerTool/VoxelizerToolHelper.h +++ b/apps/VoxelizerTool/VoxelizerToolHelper.h @@ -1,78 +1,71 @@ // ----------------------------------------------------------------------- // RTToolbox - DKFZ radiotherapy quantitative evaluation library // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See rttbCopyright.txt or // http://www.dkfz.de/en/sidt/projects/rttb/copyright.html // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notices for more information. // //------------------------------------------------------------------------ /*! // @file // @version $Revision: 1127 $ (last changed revision) // @date $Date: 2015-10-01 13:33:33 +0200 (Do, 01 Okt 2015) $ (last change date) // @author $Author: hentsch $ (last changed by) */ #include #include #include "rttbStructureSetGeneratorInterface.h" #include "rttbMaskAccessorInterface.h" #include "rttbITKImageMaskAccessor.h" #include "rttbDoseAccessorInterface.h" namespace rttb { namespace apps { namespace voxelizerTool { class ApplicationData; void processData(ApplicationData& appData); - /**@brief ListofExpression contains input expression - @return a vector of found labels - */ - std::vector filterForExpression(const std::vector& listOfExpressions, - const std::string& inputExpression); /**@brief Search the label with the position from index @return a label from the list as string */ void removeSpecialCharacters(std::string& label); - std::vector getAllLabels(rttb::core::StructureSetGeneratorInterface::StructureSetPointer& structureSetPtr); - /**@brief create a mask with _rtStructureSet and _doseAccessor object. @return a mask object */ core::MaskAccessorInterface::MaskAccessorPointer createMask( core::DoseAccessorInterface::DoseAccessorPointer& doseAccessorPtr, core::StructureSetGeneratorInterface::StructureSetPointer& structureSetPtr, bool strict, unsigned int indexOfStructure); /**@brief write the mask into the outputfile @param Outputfilename */ void writeMaskToFile(std::vector& maskVector, const std::string& outputFileName, bool voxelization); io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer addMultipleStructsToImage( std::vector& maskVector); io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer applyThresholdFilter( io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer& itkImage); void writeITKImageToFile(io::itk::ITKImageMaskAccessor::ITKMaskImageType::ConstPointer& itkImage, const std::string& outputfilename, bool useCompression); std::string getFilenameWithoutEnding(const std::string& outfilename); std::string getFileEnding(const std::string& outfilename); } } } \ No newline at end of file