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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
/*
* Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 otbLabelImageRegionPruningFilter_txx
#define otbLabelImageRegionPruningFilter_txx
#include "otbLabelImageRegionPruningFilter.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkImageRegionIterator.h"
#include "itkProgressReporter.h"
namespace otb
{
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::LabelImageRegionPruningFilter()
{
m_NumberOfComponentsPerPixel=0;
m_MinRegionSize=100;
this->SetNumberOfRequiredInputs( 2 );
this->SetNumberOfRequiredOutputs(2);
this->SetNthOutput(0, OutputLabelImageType::New());
this->SetNthOutput(1, OutputClusteredImageType::New());
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
void
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::SetInputLabelImage( const TInputLabelImage * labelImage)
{
// Process object is not const-correct so the const casting is required.
this->SetNthInput(0, const_cast<TInputLabelImage *>( labelImage ));
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
void
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::SetInputSpectralImage( const TInputSpectralImage * spectralImage)
{
// Process object is not const-correct so the const casting is required.
this->SetNthInput(1, const_cast<TInputSpectralImage *>( spectralImage ));
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
TInputLabelImage *
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::GetInputLabelImage()
{
return dynamic_cast<TInputLabelImage*>(itk::ProcessObject::GetInput(0));
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
TInputSpectralImage *
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::GetInputSpectralImage()
{
return dynamic_cast<TInputSpectralImage*>(itk::ProcessObject::GetInput(1));
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::~LabelImageRegionPruningFilter()
{
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
TOutputLabelImage *
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::GetLabelOutput()
{
if (this->GetNumberOfOutputs() < 1)
{
return ITK_NULLPTR;
}
return static_cast<OutputLabelImageType *>(this->itk::ProcessObject::GetOutput(0));
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
const TOutputLabelImage *
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::GetLabelOutput() const
{
if (this->GetNumberOfOutputs() < 1)
{
return 0;
}
return static_cast<OutputLabelImageType *>(this->itk::ProcessObject::GetOutput(0));
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
typename LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>::OutputClusteredImageType *
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::GetClusteredOutput()
{
if (this->GetNumberOfOutputs() < 2)
{
return ITK_NULLPTR;
}
return static_cast<OutputClusteredImageType *>(this->itk::ProcessObject::GetOutput(1));
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
const typename LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>::OutputClusteredImageType *
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::GetClusteredOutput() const
{
if (this->GetNumberOfOutputs() < 2)
{
return 0;
}
return static_cast<OutputClusteredImageType *>(this->itk::ProcessObject::GetOutput(1));
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
void
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::GenerateOutputInformation()
{
Superclass::GenerateOutputInformation();
unsigned int numberOfComponentsPerPixel = this->GetInputSpectralImage()->GetNumberOfComponentsPerPixel();
if(this->GetClusteredOutput())
{
this->GetClusteredOutput()->SetNumberOfComponentsPerPixel(numberOfComponentsPerPixel);
}
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
void
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::EnlargeOutputRequestedRegion( itk::DataObject *itkNotUsed(output) )
{
// This filter requires all of the output images in the buffer.
for ( unsigned int j = 0; j < this->GetNumberOfOutputs(); j++ )
{
if ( this->itk::ProcessObject::GetOutput(j) )
{
this->itk::ProcessObject::GetOutput(j)->SetRequestedRegionToLargestPossibleRegion();
}
}
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
void
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::GenerateData()
{
typename InputSpectralImageType::Pointer spectralImage = this->GetInputSpectralImage();
typename InputLabelImageType::Pointer inputLabelImage = this->GetInputLabelImage();
typename OutputLabelImageType::Pointer outputLabelImage = this->GetLabelOutput();
typename OutputClusteredImageType::Pointer outputClusteredImage = this->GetClusteredOutput();
// Allocate output
outputLabelImage->SetBufferedRegion( outputLabelImage->GetRequestedRegion() );
outputLabelImage->Allocate();
outputClusteredImage->SetBufferedRegion( outputClusteredImage->GetRequestedRegion() );
outputClusteredImage->Allocate();
m_NumberOfComponentsPerPixel = spectralImage->GetNumberOfComponentsPerPixel();
//std::cout << "Copy input label image to output label image" << std::endl;
// Copy input label image to output label image
typename itk::ImageRegionConstIterator<InputLabelImageType> inputIt(inputLabelImage, outputLabelImage->GetRequestedRegion());
typename itk::ImageRegionIterator<OutputLabelImageType> outputIt(outputLabelImage, outputLabelImage->GetRequestedRegion());
inputIt.GoToBegin();
outputIt.GoToBegin();
while(!inputIt.IsAtEnd())
{
outputIt.Set(inputIt.Get());
++inputIt;
++outputIt;
}
RegionAdjacencyMapType regionAdjacencyMap = LabelImageToRegionAdjacencyMap(outputLabelImage);
unsigned int regionCount = regionAdjacencyMap.size() - 1;
// Initialize arrays for mode information
m_CanonicalLabels.clear();
m_CanonicalLabels.resize(regionCount+1);
m_Modes.clear();
m_Modes.reserve(regionCount+1);
for(unsigned int i = 0; i < regionCount+1; ++i)
{
m_Modes.push_back( SpectralPixelType(m_NumberOfComponentsPerPixel) );
}
m_PointCounts.clear();
m_PointCounts.resize(regionCount+1); // = std::vector<unsigned int>(regionCount+1);
// Associate each label to a spectral value, a canonical label and a point count
typename itk::ImageRegionConstIterator<InputLabelImageType> inputItWithIndex(inputLabelImage, outputLabelImage->GetRequestedRegion());
inputItWithIndex.GoToBegin();
while(!inputItWithIndex.IsAtEnd())
{
LabelType label = inputItWithIndex.Get();
// if label has not been initialized yet ..
if(m_PointCounts[label] == 0)
{
// m_CanonicalLabels[label] = label;
m_Modes[label] = spectralImage->GetPixel(inputItWithIndex.GetIndex());
}
m_PointCounts[label]++;
++inputItWithIndex;
}
// Region Pruning
bool finishedPruning = false;
unsigned int pruneIterations = 0;
unsigned int minRegionCount = 0;
do{
minRegionCount = 0;
// Initialize Canonical Labels
for(LabelType curLabel = 1; curLabel <= regionCount; ++curLabel)
{
m_CanonicalLabels[curLabel] = curLabel;
/* if(m_PointCounts[curLabel] <= m_MinRegionSize)
{
minRegionCount++;
}*/
}
// std::cout<<minRegionCount<<" regions are too be fused"<<std::endl;
// Iterate over all regions
for(LabelType curLabel = 1; curLabel <= regionCount; ++curLabel)
{
if((m_PointCounts[curLabel] ==0) || (m_PointCounts[curLabel] > m_MinRegionSize))
{
// do not process empty regions
continue;
}
minRegionCount++;
// std::cout<<" point in label "<<curLabel<<" "<<m_PointCounts[curLabel]<<std::endl;
const SpectralPixelType & curSpectral = m_Modes[curLabel];
// Iterate over all adjacent regions and check for merge
typename AdjacentLabelsContainerType::const_iterator adjIt = regionAdjacencyMap[curLabel].begin();
LabelType neighborCandidate=0;
RealType bestNorm2=itk::NumericTraits< float >::max();
while (adjIt != regionAdjacencyMap[curLabel].end())
{
LabelType adjLabel = *adjIt;
assert(adjLabel <= regionCount);
const SpectralPixelType & adjSpectral = m_Modes[adjLabel];
// Check condition to merge regions
//find the spectrally nearest adjacent label
RealType norm2 = 0;
for(unsigned int comp = 0; comp < m_NumberOfComponentsPerPixel; ++comp)
{
RealType e;
e = (curSpectral[comp] - adjSpectral[comp]);
norm2 += e*e;
}
if(norm2 < bestNorm2)
{
bestNorm2=norm2;
neighborCandidate=adjLabel;
};
++adjIt;
} // end of loop over adjacent labels
if(neighborCandidate!=0)
{
// Find canonical label for current region
LabelType curCanLabel = curLabel; //m_CanonicalLabels[curLabel];
while(m_CanonicalLabels[curCanLabel] != curCanLabel)
{
curCanLabel = m_CanonicalLabels[curCanLabel];
}
// Find canonical label for adjacent region
LabelType adjCanLabel =neighborCandidate; //m_CanonicalLabels[curLabel];
while(m_CanonicalLabels[adjCanLabel] != adjCanLabel)
{
adjCanLabel = m_CanonicalLabels[adjCanLabel];
}
// Assign same canonical label to both regions
if(curCanLabel < adjCanLabel)
{
m_CanonicalLabels[adjCanLabel] = curCanLabel;
}
else
{
m_CanonicalLabels[m_CanonicalLabels[curCanLabel]] = adjCanLabel;
m_CanonicalLabels[curCanLabel] = adjCanLabel;
}
}
} // end of loop over labels
//std::cout << "Simplify the table of canonical labels" << std::endl;
/* Simplify the table of canonical labels */
for(LabelType i = 1; i < regionCount+1; ++i)
{
LabelType can = i;
while(m_CanonicalLabels[can] != can)
{
can = m_CanonicalLabels[can];
}
m_CanonicalLabels[i] = can;
}
//std::cout << "merge regions with same canonical label" << std::endl;
/* Merge regions with same canonical label */
/* - update modes and point counts */
std::vector<SpectralPixelType> newModes;
newModes.reserve(regionCount+1); //(regionCount+1, SpectralPixelType(m_NumberOfComponentsPerPixel));
for(unsigned int i = 0; i < regionCount+1; ++i)
{
newModes.push_back( SpectralPixelType(m_NumberOfComponentsPerPixel) );
}
std::vector<unsigned int> newPointCounts(regionCount+1);
for(unsigned int i = 1; i < regionCount+1; ++i)
{
newModes[i].Fill(0);
newPointCounts[i] = 0;
}
for(unsigned int i = 1; i < regionCount+1; ++i)
{
LabelType canLabel = m_CanonicalLabels[i];
unsigned int nPoints = m_PointCounts[i];
for(unsigned int comp = 0; comp < m_NumberOfComponentsPerPixel; ++comp)
{
newModes[canLabel][comp] += nPoints * m_Modes[i][comp];
}
newPointCounts[canLabel] += nPoints;
}
//std::cout << "re-labeling" << std::endl;
/* re-labeling */
std::vector<LabelType> newLabels(regionCount+1);
std::vector<bool> newLabelSet(regionCount+1);
for(unsigned int i = 1; i < regionCount+1; ++i)
{
newLabelSet[i] = false;
}
LabelType label = 0;
for(unsigned int i = 1; i < regionCount+1; ++i)
{
LabelType canLabel = m_CanonicalLabels[i];
if(newLabelSet[canLabel] == false)
{
newLabelSet[canLabel] = true;
label++;
newLabels[canLabel] = label;
unsigned int nPoints = newPointCounts[canLabel];
for(unsigned int comp = 0; comp < m_NumberOfComponentsPerPixel; ++comp)
{
m_Modes[label][comp] = newModes[canLabel][comp] / nPoints;
}
m_PointCounts[label] = newPointCounts[canLabel];
}
}
unsigned int oldRegionCount = regionCount;
regionCount = label;
/* reassign labels in label image */
outputIt.GoToBegin();
while(!outputIt.IsAtEnd())
{
LabelType l = outputIt.Get();
LabelType canLabel;
itkAssertOrThrowMacro(m_CanonicalLabels[l] <= oldRegionCount,"Found a label greater than region count")
canLabel = newLabels[m_CanonicalLabels[l]];
outputIt.Set( canLabel );
++outputIt;
}
finishedPruning = !minRegionCount || regionCount == 1 || pruneIterations>=10;
// only one iteration for now
if(!finishedPruning)
{
/* Update adjacency table */
regionAdjacencyMap = LabelImageToRegionAdjacencyMap(outputLabelImage);
}
pruneIterations++;
}while(!finishedPruning);
// end of main iteration loop
// std::cout << "merge iterations: " << mergeIterations << std::endl;
// std::cout << "number of label objects: " << regionCount << std::endl;
// Generate clustered output
itk::ImageRegionIterator<OutputClusteredImageType> outputClusteredIt(outputClusteredImage, outputClusteredImage->GetRequestedRegion() );
outputClusteredIt.GoToBegin();
outputIt.GoToBegin();
while( !outputClusteredIt.IsAtEnd() )
{
LabelType label = outputIt.Get();
const SpectralPixelType & p = m_Modes[ label ];
outputClusteredIt.Set(p);
++outputClusteredIt;
++outputIt;
}
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
void
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Minimum Region Size: " << m_MinRegionSize << std::endl;
}
template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage, class TOutputClusteredImage>
typename LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>::RegionAdjacencyMapType
LabelImageRegionPruningFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage, TOutputClusteredImage>
::LabelImageToRegionAdjacencyMap(typename OutputLabelImageType::Pointer labelImage)
{
// declare the output map
RegionAdjacencyMapType ram;
// Find the maximum label value
itk::ImageRegionConstIterator<OutputLabelImageType> it(labelImage, labelImage->GetRequestedRegion());
it.GoToBegin();
LabelType maxLabel = 0;
while(!it.IsAtEnd())
{
LabelType label = it.Get();
maxLabel = vcl_max(maxLabel, label);
++it;
}
// Set the size of the adjacency map
ram.resize(maxLabel+1);
// set the image region without bottom and right borders so that bottom and
// right neighbors always exist
RegionType regionWithoutBottomRightBorders = labelImage->GetRequestedRegion();
SizeType size = regionWithoutBottomRightBorders.GetSize();
for(unsigned int d = 0; d < ImageDimension; ++d) size[d] -= 1;
regionWithoutBottomRightBorders.SetSize(size);
itk::ImageRegionConstIteratorWithIndex<OutputLabelImageType> inputIt(labelImage, regionWithoutBottomRightBorders);
inputIt.GoToBegin();
while(!inputIt.IsAtEnd())
{
const InputIndexType & index = inputIt.GetIndex();
LabelType label = inputIt.Get();
// check neighbors
for(unsigned int d = 0; d < ImageDimension; ++d)
{
InputIndexType neighborIndex = index;
neighborIndex[d]++;
LabelType neighborLabel = labelImage->GetPixel(neighborIndex);
// add adjacency if different labels
if(neighborLabel != label)
{
ram[label].insert(neighborLabel);
ram[neighborLabel].insert(label);
}
}
++inputIt;
}
return ram;
}
} // end namespace otb
#endif
|