1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkObjectByObjectLabelMapFilter_hxx
#define itkObjectByObjectLabelMapFilter_hxx
#include "itkNumericTraits.h"
#include "itkLabelObject.h"
#include "itkLabelMap.h"
#include "itkBinaryImageToLabelMapFilter.h"
#include "itkLabelImageToLabelMapFilter.h"
#include "itkLabelMapToBinaryImageFilter.h"
#include "itkLabelMapToLabelImageFilter.h"
#include "itkLabelSelectionLabelMapFilter.h"
#include "itkAutoCropLabelMapFilter.h"
namespace itk
{
template <typename TInputImage,
typename TOutputImage,
typename TInputFilter,
typename TOutputFilter,
typename TInternalInputImageType,
typename TInternalOutputImageType>
ObjectByObjectLabelMapFilter<TInputImage,
TOutputImage,
TInputFilter,
TOutputFilter,
TInternalInputImageType,
TInternalOutputImageType>::ObjectByObjectLabelMapFilter()
{
m_ConstrainPaddingToImage = true;
m_PadSize.Fill(1);
m_BinaryInternalOutput = false;
m_KeepLabels = true;
m_InternalForegroundValue = itk::NumericTraits<InternalOutputPixelType>::max();
m_InputFilter = nullptr;
m_OutputFilter = nullptr;
m_Select = SelectType::New();
// be sure to *not* use the label objects internally
m_Select->SetInPlace(false);
m_Select->SetNumberOfWorkUnits(1);
m_Crop = CropType::New();
m_Crop->SetInput(m_Select->GetOutput());
m_Crop->SetNumberOfWorkUnits(1);
m_Pad = PadType::New();
m_Pad->SetInput(m_Crop->GetOutput());
m_LM2BI = LM2BIType::New();
m_LM2BI->SetInput(m_Pad->GetOutput());
m_LM2BI->SetNumberOfWorkUnits(1);
m_LI2LM = LI2LMType::New();
m_LI2LM->SetNumberOfWorkUnits(1);
m_BI2LM = BI2LMType::New();
m_BI2LM->SetNumberOfWorkUnits(1);
// to be sure that no one will use an uninitialized value
m_Label = InputImagePixelType{};
}
template <typename TInputImage,
typename TOutputImage,
typename TInputFilter,
typename TOutputFilter,
typename TInternalInputImageType,
typename TInternalOutputImageType>
void
ObjectByObjectLabelMapFilter<TInputImage,
TOutputImage,
TInputFilter,
TOutputFilter,
TInternalInputImageType,
TInternalOutputImageType>::SetFilter(InputFilterType * filter)
{
auto * outputFilter = dynamic_cast<OutputFilterType *>(filter);
if (outputFilter == nullptr && filter != nullptr)
{
// TODO: can it be replaced by a concept check ?
itkExceptionMacro("Wrong output filter type. Use SetOutputFilter() and SetInputFilter() instead of SetFilter() "
"when input and output filter types are different.");
}
this->SetInputFilter(filter);
this->SetOutputFilter(outputFilter);
}
template <typename TInputImage,
typename TOutputImage,
typename TInputFilter,
typename TOutputFilter,
typename TInternalInputImageType,
typename TInternalOutputImageType>
void
ObjectByObjectLabelMapFilter<TInputImage,
TOutputImage,
TInputFilter,
TOutputFilter,
TInternalInputImageType,
TInternalOutputImageType>::SetInputFilter(InputFilterType * filter)
{
if (m_InputFilter != filter)
{
this->Modified();
m_InputFilter = filter;
}
}
template <typename TInputImage,
typename TOutputImage,
typename TInputFilter,
typename TOutputFilter,
typename TInternalInputImageType,
typename TInternalOutputImageType>
void
ObjectByObjectLabelMapFilter<TInputImage,
TOutputImage,
TInputFilter,
TOutputFilter,
TInternalInputImageType,
TInternalOutputImageType>::SetOutputFilter(OutputFilterType * filter)
{
if (m_OutputFilter != filter)
{
this->Modified();
m_OutputFilter = filter;
}
}
template <typename TInputImage,
typename TOutputImage,
typename TInputFilter,
typename TOutputFilter,
typename TInternalInputImageType,
typename TInternalOutputImageType>
void
ObjectByObjectLabelMapFilter<TInputImage,
TOutputImage,
TInputFilter,
TOutputFilter,
TInternalInputImageType,
TInternalOutputImageType>::GenerateData()
{
if (!m_InputFilter)
{
itkExceptionMacro("InputFilter must be set.");
}
if (!m_OutputFilter)
{
itkExceptionMacro("OutputFilter must be set.");
}
this->AllocateOutputs();
LabelMapType * output = this->GetOutput();
// preserve the background value
output->SetBackgroundValue(this->GetInput()->GetBackgroundValue());
output->ClearLabels();
// give the input bg value as default bg value to the output
this->GetOutput()->SetBackgroundValue(this->GetInput()->GetBackgroundValue());
// set the input image of the first filter of our internal pipeline
m_Select->SetInput(this->GetInput());
// configure the pipeline to produce a constrained border or not.
// auto crop filter "CropBorder" feature constrain the padding to the input image,
// and the pad filter don't constrain it, so use one or the other
if (m_ConstrainPaddingToImage)
{
m_Crop->SetCropBorder(m_PadSize);
SizeType zero;
zero.Fill(0);
m_Pad->SetPadSize(zero);
}
else
{
SizeType zero;
zero.Fill(0);
m_Crop->SetCropBorder(zero);
m_Pad->SetPadSize(m_PadSize);
}
// plug the pipeline provided by the user and our internal one
m_InputFilter->SetInput(m_LM2BI->GetOutput());
// set the input to both kind of filters in charge of creating the label object
// so we don't have to implement a condition
m_LI2LM->SetInput(m_OutputFilter->GetOutput());
m_BI2LM->SetInput(m_OutputFilter->GetOutput());
m_LM2BI->SetForegroundValue(m_InternalForegroundValue);
m_BI2LM->SetInputForegroundValue(m_InternalForegroundValue);
// initialize the progress reporter
ProgressReporter progress(this, 0, this->GetLabelMap()->GetNumberOfLabelObjects());
// initialize the iterator
typename InputImageType::ConstIterator inIt(this->GetInput());
while (!inIt.IsAtEnd())
{
// inform the user that we are beginning a new object
m_Label = inIt.GetLabel();
this->InvokeEvent(IterationEvent());
// select our object
m_Select->SetLabel(m_Label);
// TODO: remove the next line - it shouldn't be required.
// It seems to be a bug in the autocrop filter :-(
m_Crop->Modified();
// to store the label objects
LabelMapType * labelMap;
// to be reused later
const typename OutputImageType::LabelObjectType * inLo = inIt.GetLabelObject();
// update the pipeline
if (m_BinaryInternalOutput)
{
m_BI2LM->UpdateLargestPossibleRegion();
labelMap = m_BI2LM->GetOutput();
}
else
{
m_LI2LM->UpdateLargestPossibleRegion();
labelMap = m_LI2LM->GetOutput();
}
// std::cout << "label: " << m_Label + 0.0 << " " << inLo->GetLabel() + 0.0 << std::endl;
// stole the label objects from the last filter of the pipeline, to put them in the output
// label map of the current filter
if (m_KeepLabels)
{
// try to keep the current label. That's easy if a single object is produced.
// If more than one is produced, it is pushed in the label map without trying
// to get a specific label. If a label is already there, it means that a previous
// object has stolen the label, so the label of the thief must be changed.
typename LabelMapType::Iterator outIt(labelMap);
if (!outIt.IsAtEnd())
{
LabelObjectType * outLo = outIt.GetLabelObject();
if (output->HasLabel(m_Label))
{
// the label has been stolen by a previously split object. Just move that object elsewhere
// to free the label
typename LabelObjectType::Pointer lotmp = output->GetLabelObject(m_Label);
output->RemoveLabelObject(lotmp);
outLo->SetLabel(m_Label);
outLo->template CopyAttributesFrom<LabelObjectType>(inLo);
output->AddLabelObject(outLo);
output->PushLabelObject(lotmp);
}
else
{
outLo->SetLabel(m_Label);
outLo->template CopyAttributesFrom<LabelObjectType>(inLo);
output->AddLabelObject(outLo);
}
// then push the other objects
++outIt;
while (!outIt.IsAtEnd())
{
outLo = outIt.GetLabelObject();
outLo->template CopyAttributesFrom<LabelObjectType>(inLo);
output->PushLabelObject(outLo);
++outIt;
}
}
else
{
// std::cout << "no result!" << std::endl;
}
}
else
{
// don't try to preserve the label - simply push the label objects as they come
typename LabelMapType::Iterator outIt(labelMap);
while (!outIt.IsAtEnd())
{
LabelObjectType * outLo = outIt.GetLabelObject();
outLo->template CopyAttributesFrom<LabelObjectType>(inLo);
output->PushLabelObject(outLo);
++outIt;
}
}
// and proceed the next object
++inIt;
progress.CompletedPixel();
}
}
template <typename TInputImage,
typename TOutputImage,
typename TInputFilter,
typename TOutputFilter,
typename TInternalInputImageType,
typename TInternalOutputImageType>
void
ObjectByObjectLabelMapFilter<TInputImage,
TOutputImage,
TInputFilter,
TOutputFilter,
TInternalInputImageType,
TInternalOutputImageType>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "ConstrainPaddingToImage: " << m_ConstrainPaddingToImage << std::endl;
os << indent << "PadSize: " << m_PadSize << std::endl;
os << indent << "BinaryInternalOutput: " << m_BinaryInternalOutput << std::endl;
os << indent << "KeepLabels: " << m_KeepLabels << std::endl;
os << indent << "InternalForegroundValue: "
<< static_cast<typename NumericTraits<InternalOutputPixelType>::PrintType>(m_InternalForegroundValue) << std::endl;
os << indent << "InputFilter: " << m_InputFilter << std::endl;
os << indent << "OutputFilter: " << m_OutputFilter << std::endl;
os << indent << "Label: " << static_cast<typename NumericTraits<InputImagePixelType>::PrintType>(m_Label)
<< std::endl;
}
} // end namespace itk
#endif
|