| 456 | |
| 457 | |
| 458 | std::pair<mitk::Image::Pointer, mitk::IDToLabelClassNameMapType> mitk::CreateLabelClassMap(const MultiLabelSegmentation* segmentation, MultiLabelSegmentation::GroupIndexType groupID, const MultiLabelSegmentation::LabelValueVectorType& selectedLabels) |
| 459 | { |
| 460 | if (nullptr == segmentation) mitkThrow() << "Error, cannot create label class map. Passed segmentation is nullptr."; |
| 461 | if (!segmentation->ExistGroup(groupID)) mitkThrow() << "Error, cannot create label class map. GroupID is invalid. Invalid ID: " << groupID; |
| 462 | |
| 463 | auto map = mitk::Image::New(); |
| 464 | |
| 465 | // map->Initialize(segmentation) does not work here if this label set image has a single slice, |
| 466 | // since the map would be automatically flattened to a 2-d image, whereas we expect the |
| 467 | // original dimension of this label set image. Hence, initialize the map more explicitly: |
| 468 | map->Initialize(MultiLabelSegmentation::GetPixelType(), segmentation->GetDimension(), segmentation->GetDimensions().data()); |
| 469 | map->SetTimeGeometry(segmentation->GetTimeGeometry()->Clone()); |
| 470 | |
| 471 | ClearImageBuffer(map); |
| 472 | |
| 473 | // get relevant labels (as intersect of groupLabels and selectedLabels |
| 474 | auto groupValues = segmentation->GetLabelValuesByGroup(groupID); |
| 475 | auto relevantDetectLamba = [&selectedLabels](MultiLabelSegmentation::LabelValueVectorType result, MultiLabelSegmentation::LabelValueType element) |
| 476 | { |
| 477 | if (std::find(selectedLabels.begin(), selectedLabels.end(), element) != selectedLabels.end()) |
| 478 | { |
| 479 | result.push_back(element); |
| 480 | } |
| 481 | return result; |
| 482 | }; |
| 483 | |
| 484 | auto relevantGroupValues = std::accumulate(groupValues.begin(), |
| 485 | groupValues.end(), |
| 486 | MultiLabelSegmentation::LabelValueVectorType(), |
| 487 | relevantDetectLamba); |
| 488 | |
| 489 | // construct class mapping |
| 490 | auto classToValueMap = LabelSetImageHelper::SplitLabelValuesByClassName(segmentation, groupID, relevantGroupValues); |
| 491 | |
| 492 | ConstLabelVector destLabels; |
| 493 | LabelValueMappingVector transferMapping; |
| 494 | IDToLabelClassNameMapType classLookUp; |
| 495 | |
| 496 | for (const auto& [className, labelValues] : classToValueMap) |
| 497 | { |
| 498 | MultiLabelSegmentation::LabelValueType classValue = classLookUp.size() + 1; |
| 499 | classLookUp.insert(std::make_pair(classValue, className)); |
| 500 | destLabels.push_back(Label::New(classValue, className)); |
| 501 | for (const auto& labelValue : labelValues) |
| 502 | { |
| 503 | transferMapping.emplace_back(std::make_pair(labelValue, classValue)); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | TransferLabelContent(segmentation->GetGroupImage(groupID), map.GetPointer(), |
| 508 | destLabels, MultiLabelSegmentation::UNLABELED_VALUE, MultiLabelSegmentation::UNLABELED_VALUE, false, transferMapping, MultiLabelSegmentation::MergeStyle::Replace, MultiLabelSegmentation::OverwriteStyle::IgnoreLocks); |
| 509 | |
| 510 | return std::make_pair(map, classLookUp); |
| 511 | } |
| 512 | |
| 513 | std::pair<mitk::Image::Pointer, mitk::IDToLabelClassNameMapType> mitk::CreateLabelClassMap(const MultiLabelSegmentation* segmentation, MultiLabelSegmentation::GroupIndexType groupID) |
| 514 | { |
nothing calls this directly
no test coverage detected