| 109 | |
| 110 | |
| 111 | mitk::DataNode::Pointer mitk::LabelSetImageHelper::CreateNewSegmentationNode(const DataNode* referenceNode, |
| 112 | const Image* initialSegmentationImage, const std::string& segmentationName, const DataStorage* dataStorage) |
| 113 | { |
| 114 | std::string newSegmentationName = segmentationName; |
| 115 | if (newSegmentationName.empty()) |
| 116 | { |
| 117 | newSegmentationName = (nullptr!= referenceNode)? referenceNode->GetName() : "unknown"; |
| 118 | |
| 119 | if (!newSegmentationName.empty()) |
| 120 | newSegmentationName.append("-"); |
| 121 | |
| 122 | newSegmentationName.append("labels"); |
| 123 | } |
| 124 | |
| 125 | if (dataStorage != nullptr && dataStorage->GetNamedNode(newSegmentationName) != nullptr) |
| 126 | { |
| 127 | int id = 2; |
| 128 | std::string suffix = "-" + std::to_string(id); |
| 129 | |
| 130 | while (dataStorage->GetNamedNode(newSegmentationName + suffix) != nullptr) |
| 131 | suffix = "-" + std::to_string(++id); |
| 132 | |
| 133 | newSegmentationName.append(suffix); |
| 134 | } |
| 135 | |
| 136 | if (nullptr == initialSegmentationImage) |
| 137 | { |
| 138 | return nullptr; |
| 139 | } |
| 140 | |
| 141 | auto newLabelSetImage = mitk::MultiLabelSegmentation::New(); |
| 142 | try |
| 143 | { |
| 144 | newLabelSetImage->Initialize(initialSegmentationImage); |
| 145 | } |
| 146 | catch (mitk::Exception &e) |
| 147 | { |
| 148 | mitkReThrow(e) << "Could not initialize new label set image."; |
| 149 | return nullptr; |
| 150 | } |
| 151 | |
| 152 | auto newSegmentationNode = CreateEmptySegmentationNode(newSegmentationName); |
| 153 | newSegmentationNode->SetData(newLabelSetImage); |
| 154 | |
| 155 | if (referenceNode != nullptr) |
| 156 | { |
| 157 | if (auto referenceImage = dynamic_cast<const Image*>(referenceNode->GetData())) |
| 158 | { |
| 159 | SetupDerivedSegmentation(newLabelSetImage, referenceImage); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return newSegmentationNode; |
| 164 | } |
| 165 | |
| 166 | void mitk::LabelSetImageHelper::SetupDerivedSegmentation(MultiLabelSegmentation* seg, |
| 167 | const Image* source) |
nothing calls this directly
no test coverage detected