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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
/*=========================================================================
*
* 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 itkLabelMapMaskImageFilter_hxx
#define itkLabelMapMaskImageFilter_hxx
#include "itkNumericTraits.h"
#include "itkProgressReporter.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageRegionIterator.h"
#include "itkImageAlgorithm.h"
namespace itk
{
template <typename TInputImage, typename TOutputImage>
LabelMapMaskImageFilter<TInputImage, TOutputImage>::LabelMapMaskImageFilter()
: m_Label(NumericTraits<InputImagePixelType>::OneValue())
, m_BackgroundValue(OutputImagePixelType{})
{
this->SetNumberOfRequiredInputs(2);
m_CropBorder.Fill(0);
this->DynamicMultiThreadingOff();
}
template <typename TInputImage, typename TOutputImage>
void
LabelMapMaskImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion()
{
// Call the superclass' implementation of this method
Superclass::GenerateInputRequestedRegion();
// We need the whole input
InputImagePointer input = const_cast<InputImageType *>(this->GetInput());
if (!input)
{
return;
}
input->SetRequestedRegion(input->GetLargestPossibleRegion());
}
template <typename TInputImage, typename TOutputImage>
void
LabelMapMaskImageFilter<TInputImage, TOutputImage>::GenerateOutputInformation()
{
if (m_Crop)
{
const InputImageType * input = this->GetInput();
if (!(input->GetMTime() > m_CropTimeStamp) && !(this->GetMTime() > m_CropTimeStamp))
{
// Early exit, crop sizes already computed
return;
}
// First, call the default implementation not to forget anything
Superclass::GenerateOutputInformation();
// Update the input if needed
if (input->GetSource())
{
ProcessObject * upstream = input->GetSource();
if (upstream)
{
// this->SetInput(nullptr);
// std::cout << "Update the input (again?)." << std::endl;
upstream->Update();
// this->SetInput(input);
}
}
// Prefetch image region and size
InputImageRegionType cropRegion = input->GetLargestPossibleRegion();
// Now the output image size can be computed
if (m_Negated)
{
if (input->GetBackgroundValue() != m_Label)
{
// The "bad" case - the zone outside the object is at least partially
// covered by the background, which is not explicitly defined.
// simply do nothing for now
// TODO: implement that part
itkWarningMacro(
<< "Cropping according to background label is not yet implemented. The full image will be used.");
}
else
{
// Compute the bounding box of all the objects which don't have that label
IndexType mins;
mins.Fill(NumericTraits<IndexValueType>::max());
IndexType maxs;
maxs.Fill(NumericTraits<IndexValueType>::NonpositiveMin());
for (typename InputImageType::ConstIterator loit(this->GetInput()); !loit.IsAtEnd(); ++loit)
{
if (loit.GetLabel() != m_Label)
{
// Iterate over all the lines
typename LabelObjectType::ConstLineIterator lit(loit.GetLabelObject());
while (!lit.IsAtEnd())
{
const IndexType & idx = lit.GetLine().GetIndex();
LengthType length = lit.GetLine().GetLength();
// Update the mins and maxs
for (unsigned int i = 0; i < ImageDimension; ++i)
{
if (idx[i] < mins[i])
{
mins[i] = idx[i];
}
if (idx[i] > maxs[i])
{
maxs[i] = idx[i];
}
}
// Must fix the max for the axis 0
if (idx[0] + (OffsetValueType)length > maxs[0])
{
maxs[0] = idx[0] + length - 1;
}
++lit;
}
}
}
// Final computation
SizeType regionSize;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
regionSize[i] = maxs[i] - mins[i] + 1;
}
cropRegion.SetIndex(mins);
cropRegion.SetSize(regionSize);
}
}
else
{
if (input->GetBackgroundValue() == m_Label)
{
// The other "bad" case - the label we want is not defined as a label object,
// but implicitly, in the zones not defined.
// simply do nothing for now
// TODO: implement that part
itkWarningMacro(
<< "Cropping according to background label is not yet implemented. The full image will be used.");
}
else
{
// Just find the bounding box of the object with that label
const LabelObjectType * labelObject = input->GetLabelObject(m_Label);
IndexType mins;
mins.Fill(NumericTraits<IndexValueType>::max());
IndexType maxs;
maxs.Fill(NumericTraits<IndexValueType>::NonpositiveMin());
// Iterate over all the lines
typename LabelObjectType::ConstLineIterator lit(labelObject);
while (!lit.IsAtEnd())
{
const IndexType & idx = lit.GetLine().GetIndex();
LengthType length = lit.GetLine().GetLength();
// Update the mins and maxs
for (unsigned int i = 0; i < ImageDimension; ++i)
{
if (idx[i] < mins[i])
{
mins[i] = idx[i];
}
if (idx[i] > maxs[i])
{
maxs[i] = idx[i];
}
}
// Must fix the max for the axis 0
if (idx[0] + (OffsetValueType)length > maxs[0])
{
maxs[0] = idx[0] + length - 1;
}
++lit;
}
// Final computation
SizeType regionSize;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
regionSize[i] = maxs[i] - mins[i] + 1;
}
cropRegion.SetIndex(mins);
cropRegion.SetSize(regionSize);
}
}
// Pad by the crop border, but take care to not be larger than the largest
// possible region of the input image
cropRegion.PadByRadius(m_CropBorder);
cropRegion.Crop(input->GetLargestPossibleRegion());
// Finally set that region as the largest output region
this->GetOutput()->SetLargestPossibleRegion(cropRegion);
m_CropTimeStamp.Modified();
}
else
{
// No crop -> use the default implementation
Superclass::GenerateOutputInformation();
}
}
template <typename TInputImage, typename TOutputImage>
void
LabelMapMaskImageFilter<TInputImage, TOutputImage>::EnlargeOutputRequestedRegion(DataObject *)
{
this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
}
template <typename TInputImage, typename TOutputImage>
void
LabelMapMaskImageFilter<TInputImage, TOutputImage>::GenerateData()
{
this->UpdateProgress(0.0f);
this->AllocateOutputs();
this->BeforeThreadedGenerateData();
this->UpdateProgress(0.05f);
this->GetMultiThreader()->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
this->GetMultiThreader()->template ParallelizeImageRegion<OutputImageDimension>(
this->GetOutput()->GetRequestedRegion(),
[this](const OutputImageRegionType & outputRegionForThread) {
this->DynamicThreadedGenerateData(outputRegionForThread);
},
nullptr);
this->UpdateProgress(0.5f);
auto * inImage = const_cast<InputImageType *>(this->GetInput());
if (inImage->GetBackgroundValue() == m_Label)
{
// delegate to the superclass implementation to use the thread support for the label objects
this->GetMultiThreader()->template ParallelizeImageRegion<OutputImageDimension>(
this->GetOutput()->GetRequestedRegion(),
[this](const OutputImageRegionType & outputRegionForThread) {
this->SuperclassDynamicTGD(outputRegionForThread);
},
nullptr);
}
else
{
const LabelObjectType * labelObject = this->GetLabelMap()->GetLabelObject(m_Label);
const OutputImageType * input2 = this->GetFeatureImage();
OutputImageType * output = this->GetOutput();
if (!m_Negated)
{
typename LabelObjectType::ConstIndexIterator it(labelObject);
while (!it.IsAtEnd())
{
const IndexType & idx = it.GetIndex();
output->SetPixel(idx, input2->GetPixel(idx));
++it;
}
}
else
{
// And mark the label object as background
// Should we take care to not write outside the image ?
bool testIdxIsInside = m_Crop && (inImage->GetBackgroundValue() == m_Label) ^ m_Negated;
RegionType outputRegion = output->GetLargestPossibleRegion();
typename LabelObjectType::ConstIndexIterator it(labelObject);
while (!it.IsAtEnd())
{
const IndexType & idx = it.GetIndex();
if (!testIdxIsInside || outputRegion.IsInside(idx))
{
output->SetPixel(idx, m_BackgroundValue);
}
++it;
}
}
}
this->UpdateProgress(0.99f);
this->AfterThreadedGenerateData();
this->UpdateProgress(1.0f);
}
template <typename TInputImage, typename TOutputImage>
void
LabelMapMaskImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData(
const OutputImageRegionType & outputRegionForThread)
{
OutputImageType * output = this->GetOutput();
auto * input = const_cast<InputImageType *>(this->GetInput());
const OutputImageType * input2 = this->GetFeatureImage();
// Keep the values from the feature image if the same pixel in the label image
// equals the label given by the user. The other pixels are set to the background value.
if ((input->GetBackgroundValue() == m_Label) ^ m_Negated)
{
// The user wants the mask to be the background of the label collection image
// copy the feature image to the output image
// Copy input2 region to output
ImageAlgorithm::Copy(input2, output, outputRegionForThread, outputRegionForThread);
}
else
{
ImageRegionIterator<OutputImageType> outputIt(output, outputRegionForThread);
for (outputIt.GoToBegin(); !outputIt.IsAtEnd(); ++outputIt)
{
outputIt.Set(m_BackgroundValue);
}
}
}
template <typename TInputImage, typename TOutputImage>
void
LabelMapMaskImageFilter<TInputImage, TOutputImage>::ThreadedProcessLabelObject(LabelObjectType * labelObject)
{
OutputImageType * output = this->GetOutput();
auto * input = const_cast<InputImageType *>(this->GetInput());
const OutputImageType * input2 = this->GetFeatureImage();
if (!m_Negated)
{
// Keep the values from the feature image if the same pixel in the label image
// equals the label given by the user. The other pixels are set to the background value.
// Should we take care to not write outside the image ?
bool testIdxIsInside = m_Crop && (input->GetBackgroundValue() == m_Label) ^ m_Negated;
RegionType outputRegion = output->GetLargestPossibleRegion();
// The user wants the mask to be the background of the label collection image
typename LabelObjectType::ConstIndexIterator it(labelObject);
while (!it.IsAtEnd())
{
const IndexType & idx = it.GetIndex();
if (!testIdxIsInside || outputRegion.IsInside(idx))
{
output->SetPixel(idx, m_BackgroundValue);
}
++it;
}
}
else
{
// Keep the pixels from the feature image if the same pixel from the label image
// is not equal to the label provided by the user. The pixels with the label provided by the
// user are set to the background value
// And copy the feature image where the label objects are
typename LabelObjectType::ConstIndexIterator it(labelObject);
while (!it.IsAtEnd())
{
const IndexType & idx = it.GetIndex();
output->SetPixel(idx, input2->GetPixel(idx));
++it;
}
}
}
template <typename TInputImage, typename TOutputImage>
void
LabelMapMaskImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Label: " << static_cast<typename NumericTraits<LabelType>::PrintType>(m_Label) << std::endl;
os << indent
<< "BackgroundValue: " << static_cast<typename NumericTraits<OutputImagePixelType>::PrintType>(m_BackgroundValue)
<< std::endl;
os << indent << "Negated: " << m_Negated << std::endl;
os << indent << "Crop: " << m_Crop << std::endl;
os << indent << "CropBorder: " << m_CropBorder << std::endl;
os << indent << "CropTimeStamp: " << static_cast<typename NumericTraits<TimeStamp>::PrintType>(m_CropTimeStamp)
<< std::endl;
}
} // end namespace itk
#endif
|