diff --git a/CMakeExternals/DCMQI.patch b/CMakeExternals/DCMQI.patch index d210f9d3b6..67bf061e2c 100644 --- a/CMakeExternals/DCMQI.patch +++ b/CMakeExternals/DCMQI.patch @@ -1,31 +1,56 @@ diff --git a/CMake/DCMQIConfig.cmake.in b/CMake/DCMQIConfig.cmake.in -index 7cf66b2..c0d7827 100644 +index 7cf66b2..c0d7827 --- a/CMake/DCMQIConfig.cmake.in +++ b/CMake/DCMQIConfig.cmake.in @@ -1,7 +1,6 @@ @PACKAGE_INIT@ set_and_check(DCMTK_DIR "@PACKAGE_DCMTK_DIR_CONFIG@") -set_and_check(SlicerExecutionModel_DIR "@PACKAGE_SlicerExecutionModel_DIR_CONFIG@") set_and_check(DCMQI_CONFIG_DIR "@PACKAGE_CONFIG_DIR_CONFIG@") set_and_check(DCMQI_TARGETS "@PACKAGE_CONFIG_DIR_CONFIG@/DCMQITargets.cmake") diff --git a/CMakeLists.txt b/CMakeLists.txt -index 91365f5..a8c8744 100644 +index 9c6dcc2..20a90d5 --- a/CMakeLists.txt +++ b/CMakeLists.txt -@@ -304,7 +304,6 @@ if(NOT Slicer_DIR) +@@ -279,7 +279,6 @@ if(NOT Slicer_DIR) # For a build tree set(CONFIG_DIR_CONFIG ${CMAKE_BINARY_DIR}) set(DCMTK_DIR_CONFIG ${DCMTK_DIR}) - set(SlicerExecutionModel_DIR_CONFIG ${SlicerExecutionModel_DIR}) set(${PROJECT_NAME}_CONFIG_CODE "####### Expanded from \@DCMQI_CONFIG_CODE\@ #######\n") set(${PROJECT_NAME}_CONFIG_CODE "find_package(DCMTK NO_MODULE REQUIRED)\n") set(${PROJECT_NAME}_CONFIG_CODE "${${PROJECT_NAME}_CONFIG_CODE}##################################################") -@@ -316,7 +315,6 @@ if(NOT Slicer_DIR) +@@ -291,7 +290,6 @@ if(NOT Slicer_DIR) PATH_VARS CONFIG_DIR_CONFIG DCMTK_DIR_CONFIG - SlicerExecutionModel_DIR_CONFIG NO_CHECK_REQUIRED_COMPONENTS_MACRO ) + +diff --git a/libsrc/CMakeLists.txt b/libsrc/CMakeLists.txt +index 4d25e05..057326e +--- a/libsrc/CMakeLists.txt ++++ b/libsrc/CMakeLists.txt +@@ -2,6 +2,7 @@ + #----------------------------------------------------------------------------- + include(FindGit) + include(dcmqiMacroExtractRepositoryInfo) ++include(CheckCXXCompilerFlag) + + dcmqiMacroExtractRepositoryInfo(VAR_PREFIX dcmqi) + configure_file( +@@ -37,6 +38,11 @@ add_library(${lib_name} STATIC + ${ADDITIONAL_SRCS} + ) + ++CHECK_CXX_COMPILER_FLAG("-fPIC" CXX_HAS_FPIC) ++if(CXX_HAS_FPIC) ++ target_compile_options(${lib_name} PRIVATE "-fPIC") ++endif() ++ + if(DCMQI_LIBRARY_PROPERTIES) + set_target_properties(${lib_name} PROPERTIES ${DCMQI_LIBRARY_PROPERTIES}) + endif() diff --git a/Modules/Multilabel/autoload/IO/mitkDICOMSegmentationIO.cpp b/Modules/Multilabel/autoload/IO/mitkDICOMSegmentationIO.cpp index d47d34865b..865c70b03d 100644 --- a/Modules/Multilabel/autoload/IO/mitkDICOMSegmentationIO.cpp +++ b/Modules/Multilabel/autoload/IO/mitkDICOMSegmentationIO.cpp @@ -1,478 +1,477 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef __mitkDICOMSegmentationIOWriter__cpp #define __mitkDICOMSegmentationIOWriter__cpp #include "mitkDICOMSegmentationIO.h" #include "mitkBasePropertySerializer.h" #include "mitkDICOMProperty.h" #include "mitkIOMimeTypes.h" #include "mitkImageAccessByItk.h" #include "mitkImageCast.h" #include "mitkLabelSetIOHelper.h" #include "mitkLabelSetImageConverter.h" #include "mitkProperties.h" #include "mitkPropertyNameHelper.h" #include "mitkStringProperty.h" #include // itk #include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include "itkMetaDataDictionary.h" #include "itkMetaDataObject.h" #include "itkNrrdImageIO.h" #include "itkThresholdImageFilter.h" // dcmqi #include #include namespace mitk { DICOMSegmentationIO::DICOMSegmentationIO() : AbstractFileIO(LabelSetImage::GetStaticNameOfClass(), IOMimeTypes::DICOM_MIMETYPE(), "DICOM Segmentation") { AbstractFileWriter::SetRanking(10); AbstractFileReader::SetRanking(10); this->RegisterService(); } IFileIO::ConfidenceLevel DICOMSegmentationIO::GetWriterConfidenceLevel() const { if (AbstractFileIO::GetWriterConfidenceLevel() == Unsupported) return Unsupported; const LabelSetImage *input = static_cast(this->GetInput()); if (input) return Supported; else return Unsupported; } void DICOMSegmentationIO::Write() { ValidateOutputLocation(); mitk::LocaleSwitch localeSwitch("C"); LocalFile localFile(this); const std::string path = localFile.GetFileName(); auto input = dynamic_cast(this->GetInput()); if (input == nullptr) mitkThrow() << "Cannot write non-image data"; // Get DICOM information from referenced image vector dcmDatasets; DcmFileFormat *readFileFormat = new DcmFileFormat(); try { // TODO: Generate dcmdataset witk DICOM tags from property list mitk::StringLookupTableProperty::Pointer filesProp = dynamic_cast(input->GetProperty("files").GetPointer()); if (filesProp.IsNull()) { MITK_ERROR << "No property with dicom file path."; return; } StringLookupTable filesLut = filesProp->GetValue(); const StringLookupTable::LookupTableType &map = filesLut.GetLookupTable(); for (auto it = map.begin(); it != map.end(); ++it) { const char *fileName = (it->second).c_str(); if (readFileFormat->loadFile(fileName, EXS_Unknown).good()) dcmDatasets.push_back(readFileFormat->getAndRemoveDataset()); } } catch (const std::exception &e) { mitkThrow() << e.what(); } //Iterate over all layers. For each adcm file is generated for (unsigned int layer = 0; layer < input->GetNumberOfLayers(); ++layer) { vector segmentations; try { - const LabelSet *labelSet = input->GetLabelSet(layer); - for (int label = 1; label < input->GetNumberOfLabels(layer); ++label) + for (unsigned int label = 1; label < input->GetNumberOfLabels(layer); ++label) { typedef itk::CastImageFilter castItkImageFilterType; ImageToItk::Pointer imageToItkFilter = ImageToItk::New(); // BUG: It must be the layer image, but there are some errors with it (dcmqi: generate the dcmSeg "No frame data available") --> input->GetLayerImage(layer) imageToItkFilter->SetInput(input); imageToItkFilter->Update(); castItkImageFilterType::Pointer castFilter = castItkImageFilterType::New(); castFilter->SetInput(imageToItkFilter->GetOutput()); castFilter->Update(); itkInternalImageType::Pointer itkLabelImage = castFilter->GetOutput(); itk::ThresholdImageFilter::Pointer thresholdFilter = itk::ThresholdImageFilter::New(); thresholdFilter->SetInput(itkLabelImage); thresholdFilter->ThresholdOutside(label, label); thresholdFilter->SetOutsideValue(0); thresholdFilter->Update(); itkInternalImageType::Pointer segmentImage = thresholdFilter->GetOutput(); segmentImage->DisconnectPipeline(); segmentations.push_back(segmentImage); } } catch (const itk::ExceptionObject &e) { MITK_ERROR << e.GetDescription() << endl; return; } // Create segmentation meta information const std::string &tmpMetaInfoFile = this->CreateMetaDataJsonFile(layer); MITK_INFO << "Writing image: " << path << std::endl; try { // Convert itk image to dicom image dcmqi::ImageSEGConverter *converter = new dcmqi::ImageSEGConverter(); DcmDataset *result = converter->itkimage2dcmSegmentation(dcmDatasets, segmentations, tmpMetaInfoFile); // Todo for each layer // write dicom file DcmFileFormat dcmFileFormat(result); std::string filePath = path.substr(0, path.find_last_of(".")); // if there is more than one layer, we have to write more than 1 dicom file if (input->GetNumberOfLayers() != 1) filePath = filePath + std::to_string(layer) + ".dcm"; else filePath = filePath + ".dcm"; dcmFileFormat.saveFile(filePath.c_str(), EXS_LittleEndianExplicit); } catch (const std::exception &e) { mitkThrow() << e.what(); } } // end image write if (readFileFormat) delete readFileFormat; - for (int i = 0; i < dcmDatasets.size(); i++) + for (unsigned int i = 0; i < dcmDatasets.size(); i++) { delete dcmDatasets[i]; } } IFileIO::ConfidenceLevel DICOMSegmentationIO::GetReaderConfidenceLevel() const { if (AbstractFileIO::GetReaderConfidenceLevel() == Unsupported) return Unsupported; const std::string fileName = this->GetLocalFileName(); DcmFileFormat dcmFileFormat; OFCondition status = dcmFileFormat.loadFile(fileName.c_str()); if (status.bad()) return Unsupported; OFString modality; if (dcmFileFormat.getDataset()->findAndGetOFString(DCM_Modality, modality).good()) { if (modality.compare("SEG") == 0) return Supported; else return Unsupported; } return Unsupported; } std::vector DICOMSegmentationIO::Read() { mitk::LocaleSwitch localeSwitch("C"); LabelSetImage::Pointer labelSetImage; const std::string path = this->GetLocalFileName(); MITK_INFO << "loading " << path << std::endl; // Check to see if we can read the file given the name or prefix if (path.empty()) { mitkThrow() << "Empty filename in mitk::ItkImageIO "; } try { DcmFileFormat dcmFileFormat; OFCondition status = dcmFileFormat.loadFile(path.c_str()); if (status.bad()) MITK_ERROR << "Can't read the input file!"; DcmDataset *dataSet = dcmFileFormat.getDataset(); if (dataSet == nullptr) MITK_ERROR << "Can't read data from input file!"; dcmqi::ImageSEGConverter *converter = new dcmqi::ImageSEGConverter(); pair, string> dcmqiOutput = converter->dcmSegmentation2itkimage(dataSet); dcmqi::JSONSegmentationMetaInformationHandler metaInfo(dcmqiOutput.second.c_str()); metaInfo.read(); MITK_INFO << "Input " << metaInfo.getJSONOutputAsString(); map segItkImages = dcmqiOutput.first; vector>::const_iterator segmentIter = metaInfo.segmentsAttributesMappingList.begin(); for (auto &element : segItkImages) { // Get the labeled image and cast it to mitkImage typedef itk::CastImageFilter castItkImageFilterType; castItkImageFilterType::Pointer castFilter = castItkImageFilterType::New(); castFilter->SetInput(element.second); castFilter->Update(); Image::Pointer layerImage; CastToMitkImage(castFilter->GetOutput(), layerImage); // Get pixel value of the label itkInternalImageType::ValueType segValue = 1; typedef itk::ImageRegionIterator IteratorType; IteratorType iter(element.second, element.second->GetLargestPossibleRegion()); iter.GoToBegin(); while (!iter.IsAtEnd()) { itkInputImageType::PixelType value = iter.Get(); if (value != 0) { segValue = value; break; } ++iter; } // Get the label information from segment attributes map segmentMap = (*segmentIter); map::const_iterator segmentMapIter = (*segmentIter).begin(); dcmqi::SegmentAttributes *segmentAttr = (*segmentMapIter).second; OFString labelName; if (segmentAttr->getSegmentedPropertyTypeCodeSequence() != nullptr) segmentAttr->getSegmentedPropertyTypeCodeSequence()->getCodeMeaning(labelName); else { labelName = std::to_string(segmentAttr->getLabelID()).c_str(); if (labelName.empty()) labelName = "Unnamed"; } float tmp[3] = { 0.0, 0.0, 0.0 }; if (segmentAttr->getRecommendedDisplayRGBValue() != nullptr) { tmp[0] = segmentAttr->getRecommendedDisplayRGBValue()[0] / 255.0; tmp[1] = segmentAttr->getRecommendedDisplayRGBValue()[1] / 255.0; tmp[2] = segmentAttr->getRecommendedDisplayRGBValue()[2] / 255.0; } // if labelSetImage do not exists (first image) if (labelSetImage.IsNull()) { // Initialize the labelSetImage with the read image labelSetImage = LabelSetImage::New(); labelSetImage->InitializeByLabeledImage(layerImage); // Already a label generated, so set the information to this Label *activeLabel = labelSetImage->GetActiveLabel(labelSetImage->GetActiveLayer()); activeLabel->SetName(labelName.c_str()); activeLabel->SetColor(Color(tmp)); activeLabel->SetValue(segValue); } else { // Add a new layer to the labelSetImage. Background label is set automatically labelSetImage->AddLayer(layerImage); // Add new label Label *newLabel = new Label; newLabel->SetName(labelName.c_str()); newLabel->SetColor(Color(tmp)); newLabel->SetValue(segValue); labelSetImage->GetLabelSet(labelSetImage->GetActiveLayer())->AddLabel(newLabel); } ++segmentIter; } } catch (const std::exception &e) { mitkThrow() << e.what(); } if (labelSetImage->GetNumberOfLayers() > 1 && labelSetImage->GetActiveLayer() != 0) labelSetImage->SetActiveLayer(0); std::vector result; result.push_back(labelSetImage.GetPointer()); return result; } const std::string mitk::DICOMSegmentationIO::CreateMetaDataJsonFile(int layer) { const mitk::LabelSetImage *image = dynamic_cast(this->GetInput()); const std::string output; dcmqi::JSONSegmentationMetaInformationHandler handler; handler.setContentCreatorName("MITK"); handler.setClinicalTrialSeriesID("Session 1"); handler.setClinicalTrialTimePointID("0"); std::string seriesDescription = ""; image->GetPropertyList()->GetStringProperty("name", seriesDescription); if (seriesDescription.empty()) seriesDescription = "Segmentation"; handler.setSeriesDescription(seriesDescription); handler.setSeriesNumber("34" + std::to_string(layer)); // TODO:Create own series number handler.setInstanceNumber("1"); handler.setBodyPartExamined(""); const LabelSet *labelSet = image->GetLabelSet(layer); - for (int i = 1; i < image->GetNumberOfLabels(); ++i) + for (unsigned int i = 1; i < image->GetNumberOfLabels(); ++i) { const Label *label = labelSet->GetLabel(i); if (label != nullptr) { mitk::DICOMTagPath segmentNumberPath; segmentNumberPath.AddElement(0x0062, 0x0002).AddElement(0x0062, 0x0004); StringProperty *segmentNumberProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentNumberPath).c_str())); mitk::DICOMTagPath segmentLabelPath; segmentLabelPath.AddElement(0x0062, 0x0002).AddElement(0x0062, 0x0005); StringProperty *segmentLabelProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentLabelPath).c_str())); mitk::DICOMTagPath segmentAlgorithmTypePath; segmentAlgorithmTypePath.AddElement(0x0062, 0x0002).AddElement(0x0062, 0x0008); StringProperty *algorithmTypeProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentAlgorithmTypePath).c_str())); mitk::DICOMTagPath segmentCategoryCodeValuePath; segmentCategoryCodeValuePath.AddElement(0x0062, 0x0002).AddElement(0x0062, 0x0003).AddElement(0x008, 0x0100); StringProperty *segmentCategoryCodeValueProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentCategoryCodeValuePath).c_str())); mitk::DICOMTagPath segmentCategoryCodeSchemePath; segmentCategoryCodeSchemePath.AddElement(0x0062, 0x0002).AddElement(0x0062, 0x0003).AddElement(0x008, 0x0102); StringProperty *segmentCategoryCodeSchemeProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentCategoryCodeSchemePath).c_str())); mitk::DICOMTagPath segmentCategoryCodeMeaningPath; segmentCategoryCodeMeaningPath.AddElement(0x0062, 0x0002).AddElement(0x0062, 0x0003).AddElement(0x008, 0x0104); StringProperty *segmentCategoryCodeMeaningProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentCategoryCodeMeaningPath).c_str())); mitk::DICOMTagPath segmentTypeCodeValuePath; segmentTypeCodeValuePath.AddElement(0x0062, 0x0002).AddElement(0x0062, 0x000F).AddElement(0x008, 0x0100); StringProperty *segmentTypeCodeValueProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentTypeCodeValuePath).c_str())); mitk::DICOMTagPath segmentTypeCodeSchemePath; segmentTypeCodeSchemePath.AddElement(0x0062, 0x0002).AddElement(0x0062, 0x000F).AddElement(0x008, 0x0102); StringProperty *segmentTypeCodeSchemeProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentTypeCodeSchemePath).c_str())); mitk::DICOMTagPath segmentTypeCodeMeaningPath; segmentTypeCodeMeaningPath.AddElement(0x0062, 0x0002).AddElement(0x0062, 0x000F).AddElement(0x008, 0x0104); StringProperty *segmentTypeCodeMeaningProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentTypeCodeMeaningPath).c_str())); mitk::DICOMTagPath segmentModifierCodeValuePath; segmentModifierCodeValuePath.AddElement(0x0062, 0x0002) .AddElement(0x0062, 0x000F) .AddElement(0x0062, 0x0011) .AddElement(0x008, 0x0100); StringProperty *segmentModifierCodeValueProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentModifierCodeValuePath).c_str())); mitk::DICOMTagPath segmentModifierCodeSchemePath; segmentModifierCodeSchemePath.AddElement(0x0062, 0x0002) .AddElement(0x0062, 0x000F) .AddElement(0x0062, 0x0011) .AddElement(0x008, 0x0102); StringProperty *segmentModifierCodeSchemeProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentModifierCodeSchemePath).c_str())); mitk::DICOMTagPath segmentModifierCodeMeaningPath; segmentModifierCodeMeaningPath.AddElement(0x0062, 0x0002) .AddElement(0x0062, 0x000F) .AddElement(0x0062, 0x0011) .AddElement(0x008, 0x0104); StringProperty *segmentModifierCodeMeaningProp = dynamic_cast( label->GetProperty(mitk::DICOMTagPathToPropertyName(segmentModifierCodeMeaningPath).c_str())); int labelId = std::stoi(segmentNumberProp->GetValue()); dcmqi::SegmentAttributes *segAttr = handler.createAndGetNewSegment(labelId); if (segAttr != nullptr) { segAttr->setSegmentDescription(segmentLabelProp->GetValueAsString()); segAttr->setSegmentAlgorithmType(algorithmTypeProp->GetValueAsString()); segAttr->setSegmentAlgorithmName("MITK Segmentation"); if (segmentCategoryCodeValueProp != nullptr && segmentCategoryCodeSchemeProp != nullptr && segmentCategoryCodeMeaningProp != nullptr) segAttr->setSegmentedPropertyCategoryCodeSequence(segmentCategoryCodeValueProp->GetValueAsString(), segmentCategoryCodeSchemeProp->GetValueAsString(), segmentCategoryCodeMeaningProp->GetValueAsString()); if (segmentTypeCodeValueProp != nullptr && segmentTypeCodeSchemeProp != nullptr && segmentTypeCodeMeaningProp != nullptr) { segAttr->setSegmentedPropertyTypeCodeSequence(segmentTypeCodeValueProp->GetValueAsString(), segmentTypeCodeSchemeProp->GetValueAsString(), segmentTypeCodeMeaningProp->GetValueAsString()); handler.setBodyPartExamined(segmentTypeCodeMeaningProp->GetValueAsString()); } if (segmentModifierCodeValueProp != nullptr && segmentModifierCodeSchemeProp != nullptr && segmentModifierCodeMeaningProp != nullptr) segAttr->setSegmentedPropertyTypeModifierCodeSequence(segmentModifierCodeValueProp->GetValueAsString(), segmentModifierCodeSchemeProp->GetValueAsString(), segmentModifierCodeMeaningProp->GetValueAsString()); Color color = label->GetColor(); segAttr->setRecommendedDisplayRGBValue(color[0] * 255, color[1] * 255, color[2] * 255); } } } return handler.getJSONOutputAsString(); } DICOMSegmentationIO *DICOMSegmentationIO::IOClone() const { return new DICOMSegmentationIO(*this); } } // namespace #endif //__mitkLabelSetImageWriter__cpp