/usr/include/OTB-5.8/otbSEMClassifier.txx is in libotb-dev 5.8.0+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | /*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
Some parts of this code are covered by the IMT copyright.
See IMTCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbSEMClassifier_txx
#define otbSEMClassifier_txx
#include <cstdlib>
#include <iostream>
#include "itkNumericTraits.h"
#include "itkImageIterator.h"
#include "itkImageRegionIterator.h"
#include "otbMacro.h"
// default mixture model
#include "otbSEMClassifier.h"
namespace otb
{
template<class TInputImage, class TOutputImage>
SEMClassifier<TInputImage, TOutputImage>
::SEMClassifier()
{
m_TerminationCode = NOT_CONVERGED;
m_ExternalLabels = 0;
m_ComponentDeclared = 0;
m_Sample = ITK_NULLPTR;
m_NbSamples = 0;
m_SampleList = ITK_NULLPTR;
m_NbChange = 0;
m_TerminationThreshold = 1E-5;
m_Neighborhood = 1;
m_OutputImage = ITK_NULLPTR;
m_Output = ITK_NULLPTR;
}
template<class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
const unsigned int nbClasses = this->GetNumberOfClasses();
for (unsigned int componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
{
os << indent << "Component num " << componentIndex;
os << " (prop " << m_Proportions[componentIndex] << ") ";
m_ComponentVector[componentIndex]->ShowParameters(os, indent);
}
}
template<class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::SetNeighborhood(int neighborhood)
{
m_Neighborhood = 2 * (neighborhood / 2) + 1;
if (m_Neighborhood < 1) m_Neighborhood = 1;
}
template<class TInputImage, class TOutputImage>
int
SEMClassifier<TInputImage, TOutputImage>
::GetNeighborhood()
{
return m_Neighborhood;
}
template<class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::SetInitialProportions(ProportionVectorType& proportions)
{
m_InitialProportions = proportions;
m_ExternalLabels = 0;
}
template<class TInputImage, class TOutputImage>
typename SEMClassifier<TInputImage, TOutputImage>::ProportionVectorType*
SEMClassifier<TInputImage, TOutputImage>
::GetInitialProportions()
{
return m_InitialProportions;
}
template<class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::SetClassLabels(OutputType * labels)
{
if (m_Sample == NULL)
{
m_ClassLabels.resize(labels->Size());
ClassLabelVectorType::iterator iterClassLabel = m_ClassLabels.begin();
typename OutputType::ConstIterator iterLabels = labels->Begin();
typename OutputType::InstanceIdentifier id = 0;
do
{
*iterClassLabel = iterLabels->GetClassLabel(id);
++iterLabels;
++iterClassLabel;
id++;
}
while (iterLabels != labels->End());
m_ExternalLabels = 1;
}
else if (m_ClassLabels.size() == m_NbSamples) //FIXME check if this
//is really the right condition
{
m_ClassLabels.resize(labels->Size());
ClassLabelVectorType::iterator iterClassLabel = m_ClassLabels.begin();
typename OutputType::iterator iterLabels = labels->Begin();
typename OutputType::InstanceIdentifier id = 0;
do
{
*iterClassLabel = iterLabels->GetClassLabel(id);
++iterLabels;
++iterClassLabel;
id++;
}
while (iterLabels != labels->End());
m_ExternalLabels = 1;
}
else
{
otbMsgDebugMacro(<< "m_ClassLabels size = " << GetClassLabels().size() << " / m_Sample size = " << m_NbSamples);
throw itk::ExceptionObject(__FILE__, __LINE__, "Vector size mismatch", ITK_LOCATION);
}
}
template<class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::SetClassLabels(TOutputImage * imgLabels)
{
otbMsgDebugMacro(<< "Initializing segmentation from an external image");
typename TInputImage::SizeType size = imgLabels->GetBufferedRegion().GetSize();
int theSize = (int) size[0] * size[1];
if (m_Sample == ITK_NULLPTR)
{
m_ClassLabels.resize(theSize);
typename itk::ImageRegionIterator<TOutputImage> imgLabelIter(imgLabels,
imgLabels->GetBufferedRegion());
imgLabelIter.GoToBegin();
typename itk::ImageRegionIterator<TOutputImage> imgLabelIterEnd(imgLabels,
imgLabels->GetBufferedRegion());
imgLabelIterEnd.GoToEnd();
ClassLabelVectorType::iterator iterClassLabel = m_ClassLabels.begin();
do
{
*iterClassLabel = imgLabelIter.Get();
++imgLabelIter;
++iterClassLabel;
}
while (imgLabelIter != imgLabelIterEnd);
m_ExternalLabels = 1;
}
else if (theSize == m_NbSamples)
{
m_ClassLabels.resize(theSize);
typename itk::ImageRegionIterator<TOutputImage> imgLabelIter(imgLabels,
imgLabels->GetBufferedRegion());
imgLabelIter.GoToBegin();
typename itk::ImageRegionIterator<TOutputImage> imgLabelIterEnd(imgLabels,
imgLabels->GetBufferedRegion());
imgLabelIterEnd.GoToEnd();
ClassLabelVectorType::iterator iterClassLabel = m_ClassLabels.begin();
do
{
*iterClassLabel = imgLabelIter.Get();
++imgLabelIter;
++iterClassLabel;
}
while (imgLabelIter != imgLabelIterEnd);
m_ExternalLabels = 1;
}
else
{
otbMsgDebugMacro(
<< "m_ClassLabels size = " << GetClassLabels().size() << " size of the image = " << theSize <<
" / m_Sample size = " << m_NbSamples);
throw itk::ExceptionObject(__FILE__, __LINE__, "Vector size mismatch", ITK_LOCATION);
}
}
template <class TInputImage, class TOutputImage>
typename SEMClassifier<TInputImage, TOutputImage>::ClassLabelVectorType&
SEMClassifier<TInputImage, TOutputImage>
::GetClassLabels()
{
return m_ClassLabels;
}
template<class TInputImage, class TOutputImage>
typename SEMClassifier<TInputImage, TOutputImage>::ProportionVectorType*
SEMClassifier<TInputImage, TOutputImage>
::GetProportions()
{
return &m_Proportions;
}
template<class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::SetSample(const TInputImage* sample)
{
m_Sample = sample;
m_NbSamples = 0;
m_SampleList = SampleType::New();
m_SampleList->SetMeasurementVectorSize(m_Sample->GetVectorLength());
itk::ImageRegionIterator<TInputImage> imgIter((TInputImage *) m_Sample,
m_Sample->GetBufferedRegion());
imgIter.GoToBegin();
itk::ImageRegionIterator<TInputImage> imgIterEnd((TInputImage *) m_Sample,
m_Sample->GetBufferedRegion());
imgIterEnd.GoToEnd();
do
{
m_SampleList->PushBack(imgIter.Get());
++m_NbSamples;
++imgIter;
}
while (imgIter != imgIterEnd);
if (m_ExternalLabels)
{
typename TInputImage::SizeType size = m_Sample->GetBufferedRegion().GetSize();
if ((size[0] * size[1]) != m_ClassLabels.size())
throw itk::ExceptionObject(__FILE__, __LINE__,
"Vector size mismatch", ITK_LOCATION);
}
}
template<class TInputImage, class TOutputImage>
const TInputImage *
SEMClassifier<TInputImage, TOutputImage>
::GetSample() const
{
return m_Sample;
}
template<class TInputImage, class TOutputImage>
typename SEMClassifier<TInputImage, TOutputImage>::SampleType *
SEMClassifier<TInputImage, TOutputImage>
::GetSampleList() const
{
return m_SampleList;
}
template<class TInputImage, class TOutputImage>
int
SEMClassifier<TInputImage, TOutputImage>
::GetCurrentIteration()
{
return m_CurrentIteration;
}
template<class TInputImage, class TOutputImage>
int
SEMClassifier<TInputImage, TOutputImage>
::AddComponent(int id, ComponentType * component)
{
m_ComponentVector[id] = component;
m_ComponentDeclared = 1;
return static_cast<int>(m_ComponentVector.size());
}
template <class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage,TOutputImage>
::Modified() const
{
Superclass::Modified();
if (m_ComponentDeclared == 1)
otbMsgDebugMacro(
<< "Previous component declarations will be lost since called before SetNumberOfClasses");
m_ComponentVector.clear();
m_ComponentVector.resize(this->GetNumberOfClasses());
m_ComponentDeclared = 0;
}
template <class TInputImage, class TOutputImage>
TOutputImage *
SEMClassifier<TInputImage, TOutputImage>
::GetOutputImage()
{
return m_OutputImage;
}
template <class TInputImage, class TOutputImage>
typename SEMClassifier<TInputImage, TOutputImage>::OutputType *
SEMClassifier<TInputImage, TOutputImage>
::GetOutput()
{
return m_Output;
}
template <class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::InitParameters()
{
unsigned int nbClasses = this->GetNumberOfClasses();
if (!m_ExternalLabels)
{
m_ClassLabels.resize(m_NbSamples);
if (static_cast<unsigned int>(m_InitialProportions.size()) != nbClasses)
{
unsigned int label;
for (typename ClassLabelVectorType::iterator labelIter = m_ClassLabels.begin();
labelIter != m_ClassLabels.end();
++labelIter)
{
//label = (int) floor( 0.5 + nbClassesDbl * ran / double(RAND_MAX+1) );
label = rand() % nbClasses;
if (label >= nbClasses)
{
label = nbClasses - 1;
}
*labelIter = label;
}
}
else
{
// Be sure, the sum of initial proportion remains to 1
double sumProportion = 0.0;
typename ProportionVectorType::iterator iterProportion = m_InitialProportions.begin();
do
{
sumProportion += *iterProportion;
}
while (++iterProportion != m_InitialProportions.end());
if (sumProportion != 1.0)
{
for (iterProportion = m_InitialProportions.begin();
iterProportion != m_InitialProportions.end();
++iterProportion)
*iterProportion /= sumProportion;
}
// non uniform random sampling according to m_InitialProportions
double sample;
double cumulativeProportion;
for (typename ClassLabelVectorType::iterator labelIter = m_ClassLabels.begin();
labelIter != m_ClassLabels.end();
++labelIter)
{
cumulativeProportion = 0.0;
sample = double(rand()) / (double(RAND_MAX) + 1.0);
*labelIter = nbClasses - 1;
for (unsigned int componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
{
if (cumulativeProportion <= sample
&& sample < cumulativeProportion + m_InitialProportions[componentIndex])
{
*labelIter = componentIndex;
break;
}
else cumulativeProportion += m_InitialProportions[componentIndex];
}
}
}
}
m_Proportions.resize(nbClasses);
m_Proba.resize(nbClasses);
for (unsigned int i = 0; i < nbClasses; ++i)
m_Proba[i].resize(m_NbSamples);
if (!m_ComponentDeclared)
{
otbMsgDebugMacro(<< "default mixture initialization with " << nbClasses
<< " Gaussian components");
typedef otb::Statistics::GaussianModelComponent<ClassSampleType> GaussianType;
for (unsigned int componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
{
typename GaussianType::Pointer comp = GaussianType::New();
AddComponent(componentIndex, comp);
}
}
otbMsgDevMacro(<< "num class : " << nbClasses);
otbMsgDevMacro(<< "num sample : " << GetSampleList()->Size());
otbMsgDevMacro(<< "num labels : " << GetClassLabels().size());
otbMsgDevMacro(<< "contextual neighborhood : " << m_Neighborhood);
otbMsgDevMacro(<< "terminationThreshold : " << m_TerminationThreshold);
}
template <class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::PerformStochasticProcess()
{
unsigned int nbClasses = this->GetNumberOfClasses();
double x, y, z;
m_NbChange = 0;
int posSample = 0;
for (typename ClassLabelVectorType::iterator iter = m_ClassLabels.begin();
iter != m_ClassLabels.end();
++iter)
{
x = double(rand()) / (double(RAND_MAX) + 1.0);
z = 0.0;
for (unsigned int componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
{
y = z;
z += m_Proba[componentIndex][posSample];
if ((y < x) && (x <= z))
{
if (static_cast<unsigned int>(componentIndex) != *iter) m_NbChange++;
*iter = componentIndex;
break;
}
}
posSample++;
}
switch (GetCurrentIteration())
{
case 0:
case 1:
otbMsgDebugMacro(<< "Doing iteration " << GetCurrentIteration());
break;
default:
otbMsgDebugMacro(<< m_NbChange << " sample change at iteration "
<< GetCurrentIteration());
}
}
template<class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::PerformExpectationProcess()
{
unsigned int nbClasses = this->GetNumberOfClasses();
unsigned int componentIndex;
for (componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
m_Proportions[componentIndex] = 0.0;
std::vector<typename ClassSampleType::Pointer> coeffByClass;
for (componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
{
coeffByClass.push_back(ClassSampleType::New());
coeffByClass[componentIndex]->SetMeasurementVectorSize(
m_SampleList->GetMeasurementVectorSize());
coeffByClass[componentIndex]->SetSample(m_SampleList);
}
typename SampleType::ConstIterator iterSample = m_SampleList->Begin();
typename SampleType::ConstIterator lastSample = m_SampleList->End();
ClassLabelVectorType::iterator iterLabel = m_ClassLabels.begin();
ClassLabelVectorType::iterator lastLabel = m_ClassLabels.end();
typename SampleType::InstanceIdentifier id = 0;
do
{
coeffByClass[*iterLabel]->AddInstance(id);
m_Proportions[*iterLabel] += 1.0;
id++;
}
while (++iterSample != lastSample && ++iterLabel != lastLabel);
for (componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
{
if (m_Proportions[componentIndex] == 0.0)
{
std::cerr << "No sample on class " << componentIndex;
// std::cerr << " in " << __PRETTY_FUNCTION__ << std::endl;
std::cerr << " in " << ITK_LOCATION << std::endl;
continue;
}
m_ComponentVector[componentIndex]->SetSample(coeffByClass[componentIndex]);
m_ComponentVector[componentIndex]->Update();
}
for (componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
m_Proportions[componentIndex] /= static_cast<double> (m_NbSamples);
}
template <class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::PerformMaximizationProcess()
{
unsigned int nbClasses = this->GetNumberOfClasses();
int i, j, a, b;
int voisinage = m_Neighborhood / 2;
unsigned int componentIndex;
double sumPdf, aPdf;
double neighborhoodWeight = (double) m_Neighborhood * m_Neighborhood;
int line, cols;
typename TInputImage::SizeType size = m_Sample->GetBufferedRegion().GetSize();
cols = (int) size[0];
line = (int) size[1];
std::vector<double> pdf(nbClasses);
std::vector<double> localWeight(nbClasses);
std::vector<double> localCount(nbClasses);
typename SampleType::ConstIterator iterSample = m_SampleList->Begin();
typename SampleType::ConstIterator lastSample = m_SampleList->End();
MeasurementVectorType measurementVector;
typename SampleType::InstanceIdentifier id = 0;
do
{
id = iterSample.GetInstanceIdentifier();
for (componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
localCount[componentIndex] = 0.0;
i = id / cols;
j = id % cols;
for (a = (i - voisinage); a <= (i + voisinage); a++)
for (b = (j - voisinage); b <= (j + voisinage); b++)
{
if (a < 0 || a >= line) continue;
if (b < 0 || b >= cols) continue;
localCount[m_ClassLabels[a * cols + b]] += 1.0;
}
for (componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
localWeight[componentIndex] = localCount[componentIndex]
/ neighborhoodWeight;
sumPdf = 0.0;
for (componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
{
measurementVector = iterSample.GetMeasurementVector();
aPdf = localWeight[componentIndex]
* m_ComponentVector[componentIndex]->Pdf(measurementVector);
sumPdf += aPdf;
pdf[componentIndex] = aPdf;
}
for (componentIndex = 0; componentIndex < nbClasses; ++componentIndex)
{
if (sumPdf == 0.0) m_Proba[componentIndex][iterSample.GetInstanceIdentifier()] = 0.0;
else
m_Proba[componentIndex][iterSample.GetInstanceIdentifier()]
= pdf[componentIndex] / sumPdf;
}
}
while (++iterSample != lastSample);
}
template <class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::GetMaximumAposterioriLabels()
{
unsigned int nbClasses = this->GetNumberOfClasses();
// Class results initialization
m_Output = OutputType::New();
m_Output->SetSample(this->GetSampleList());
// m_Output->Resize(this->GetSampleList()->Size()); //FIXME check if
// still necessary
m_Output->SetNumberOfClasses(nbClasses);
// Image results classification
m_OutputImage = TOutputImage::New();
m_OutputImage->SetRegions(GetSample()->GetBufferedRegion());
m_OutputImage->Allocate();
int cluster;
unsigned int componentIndex;
typename SampleType::ConstIterator sampleIter = this->GetSampleList()->Begin();
typename SampleType::ConstIterator sampleIterEnd = this->GetSampleList()->End();
typename OutputType::ConstIterator outputIter = m_Output->Begin();
typename OutputType::ConstIterator outputIterEnd = m_Output->End();
typename itk::ImageRegionIterator<TOutputImage> imgOutputIter(m_OutputImage,
m_OutputImage->GetBufferedRegion());
imgOutputIter.GoToBegin();
typename itk::ImageRegionIterator<TOutputImage> imgOutputIterEnd(m_OutputImage,
m_OutputImage->GetBufferedRegion());
imgOutputIterEnd.GoToEnd();
do
{
cluster = 0;
for (componentIndex = 1; componentIndex < nbClasses; ++componentIndex)
{
if (m_Proba[componentIndex][sampleIter.GetInstanceIdentifier()]
> m_Proba[cluster][sampleIter.GetInstanceIdentifier()]) cluster = componentIndex;
}
m_Output->AddInstance(cluster, sampleIter.GetInstanceIdentifier());
imgOutputIter.Set(cluster);
}
while (++sampleIter != sampleIterEnd
&& ++outputIter != outputIterEnd
&& ++imgOutputIter != imgOutputIterEnd);
}
template<class TInputImage, class TOutputImage>
void
SEMClassifier<TInputImage, TOutputImage>
::Update()
{
InitParameters();
m_CurrentIteration = 0;
m_TerminationCode = NOT_CONVERGED;
int oldNbChange = 0;
double step;
do
{
oldNbChange = m_NbChange;
PerformStochasticProcess();
PerformExpectationProcess();
PerformMaximizationProcess();
step = static_cast<double>(oldNbChange - m_NbChange);
if (step >= 0.0)
{
if ((step / static_cast<double>(m_NbSamples))
< GetTerminationThreshold())
{
m_TerminationCode = CONVERGED;
if (oldNbChange != 0) break;
}
}
}
while (++m_CurrentIteration < m_MaximumIteration);
GetMaximumAposterioriLabels();
}
} // end of namesapce otb
#endif
|