MCPcopy Create free account
hub / github.com/MITK/MITK / DoRead

Method DoRead

Modules/Multilabel/autoload/IO/mitkMultiLabelSegmentationStackReader.cpp:194–327  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

192 }
193
194 std::vector<mitk::BaseData::Pointer> mitk::MultiLabelSegmentationStackReader::DoRead()
195 {
196 std::vector<BaseData::Pointer> result;
197
198 std::ifstream input(this->GetLocalFileName());
199 if (!input.is_open())
200 {
201 return result;
202 }
203
204 auto filePathBase = itksys::SystemTools::GetFilenamePath(this->GetLocalFileName());
205
206 nlohmann::json fileContent;
207 try
208 {
209 input >> fileContent;
210 }
211 catch (const nlohmann::json::parse_error& e)
212 {
213 MITK_ERROR << "Cannot reader data due to parsing error. Parse error: " << e.what() << '\n';
214 throw;
215 }
216
217 //check version
218 int version = 0;
219
220 if (!MultiLabelIOHelper::GetValueFromJson<int>(fileContent, "version", version))
221 {
222 MITK_INFO << "Data has unknown version. Assuming that it can be read. Result might be invalid.";
223 }
224
225 if (version > MULTILABEL_SEGMENTATION_VERSION_VALUE)
226 {
227 mitkThrow() << "Data to read has unsupported version. Software is to old to ensure correct reading. Please use a compatible version of MITK or store data in another format. Version of data: " << version << "; Supported versions up to: "<<MULTILABEL_SEGMENTATION_VERSION_VALUE;
228 }
229
230 auto segmentation = MultiLabelSegmentation::New();
231 bool segInitialized = false;
232
233 //get pixel content
234 auto groupInfos = MultiLabelIOHelper::DeserializeMultiLabelGroupsFromJSON(fileContent["groups"]);
235
236 for (const auto& groupInfo : groupInfos)
237 {
238 auto groupImage = LoadImageBasedOnFileProperty(groupInfo.properties, filePathBase);
239 auto cleanedLabels = MultiLabelSegmentation::ConvertLabelVectorConst(MultiLabelIOHelper::CloneLabelsWithoutMetaProperties(groupInfo.labels));
240
241 if (!segInitialized)
242 {
243 //this is the first group, we need to initialize the segmentation first to ensure the
244 //correct geometry.
245 mitk::Image::Pointer initImage = groupImage;
246 if (initImage.IsNull())
247 {
248 //seems to be a stack only defined with label images
249 //search for the first defined file and use it for initialization.
250 //not the most efficient way, but it is robust.
251 auto firstFileName = FindFirstFileInJson(fileContent["groups"]);

Callers

nothing calls this directly

Calls 15

FindFirstFileInJsonFunction · 0.85
LoadImageBasedOnFileNameFunction · 0.85
MakeLabelsGroupMappingFunction · 0.85
MakeLabelMappingFunction · 0.85
GetLocalFileNameMethod · 0.80
whatMethod · 0.80
AddGroupMethod · 0.80
IsNotNullMethod · 0.80
GetConstLabelsByValueMethod · 0.80
GetLabelValuesByGroupMethod · 0.80

Tested by

no test coverage detected