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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkRGBGibbsPriorFilter.txx,v $
Language: C++
Date: $Date: 2008-01-27 18:29:24 $
Version: $Revision: 1.47 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 _itkRGBGibbsPriorFilter_txx
#define _itkRGBGibbsPriorFilter_txx
#include "itkRGBGibbsPriorFilter.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define RGBGibbsPriorFilterNeedsDebugging 1
namespace itk
{
/* Set intial value of some parameters in the constructor */
template <typename TInputImage, typename TClassifiedImage>
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::RGBGibbsPriorFilter(void):
m_InputImage(0),
m_TrainingImage(0),
m_LabelledImage(0),
m_NumberOfClasses(0),
m_MaximumNumberOfIterations(10),
m_ClassifierPtr(0),
m_BoundaryGradient(7),
m_BoundaryWeight(1),
m_GibbsPriorWeight(1),
m_StartRadius(10),
m_RecursiveNumber(0),
m_LabelStatus(0),
m_MediumImage(0),
m_Temp(0),
m_ImageWidth(0),
m_ImageHeight(0),
m_ImageDepth(0),
m_ClusterSize(10),
m_ObjectLabel(1),
m_VecDim(0),
m_LowPoint(),
m_Region(NULL),
m_RegionCount(NULL),
m_CliqueWeight_1(0.0),
m_CliqueWeight_2(0.0),
m_CliqueWeight_3(0.0),
m_CliqueWeight_4(0.0),
m_CliqueWeight_5(0.0),
m_CliqueWeight_6(0.0),
m_ObjectThreshold(5.0)
{
m_StartPoint.Fill(0);
}
template <typename TInputImage, typename TClassifiedImage>
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::~RGBGibbsPriorFilter()
{
if (m_Region)
{
delete [] m_Region;
}
if (m_RegionCount)
{
delete [] m_RegionCount;
}
if (m_LabelStatus)
{
delete [] m_LabelStatus;
}
}
/* Set the labelled image. */
template<typename TInputImage, typename TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::SetLabelledImage(LabelledImageType image)
{
m_LabelledImage = image;
this->Allocate();
}
/* GenerateMediumImage method. */
template <class TInputImage, class TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::GenerateMediumImage()
{
InputImageConstPointer input = this->GetInput();
m_MediumImage = TInputImage::New() ;
m_MediumImage->SetLargestPossibleRegion( input->GetLargestPossibleRegion() );
m_MediumImage->SetRequestedRegionToLargestPossibleRegion();
m_MediumImage->SetBufferedRegion( m_MediumImage->GetRequestedRegion() );
m_MediumImage->Allocate();
}
/* Allocate the memeory for classified image. */
template<class TInputImage, class TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::Allocate()
{
/* Get the image width/height and depth. */
InputImageSizeType inputImageSize = m_InputImage->GetBufferedRegion().GetSize();
m_ImageWidth = inputImageSize[0];
m_ImageHeight = inputImageSize[1];
m_ImageDepth = inputImageSize[2];
const unsigned int numberOfPixels = m_ImageWidth*m_ImageHeight*m_ImageDepth;
m_LabelStatus = new LabelType[ numberOfPixels ];
for( unsigned int index = 0; index < numberOfPixels; index++ )
{
m_LabelStatus[index]=1;
}
}
/* Smooth the image in piecewise fashion. */
template <typename TInputImage, typename TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::GreyScalarBoundary(LabelledImageIndexType Index3D)
{
int change;
int signs[4];
int x;
int numx;
LabelType origin;
LabelType neighbors[4];
for (unsigned int rgb = 0; rgb < m_VecDim; rgb++)
{
origin = static_cast<LabelType>( m_InputImage->GetPixel( Index3D )[rgb] );
unsigned int j = 0;
for(unsigned int i = 0; i < ImageDimension-1; i++)
{
Index3D[i]--;
neighbors[j] = static_cast<LabelType>( m_InputImage->GetPixel( Index3D )[rgb] );
Index3D[i]++;
j++;
Index3D[i]++;
neighbors[j] = static_cast<LabelType>( m_InputImage->GetPixel( Index3D )[rgb] );
Index3D[i]--;
j++;
}
for (unsigned int ii=0; ii<4; ii++)
{
signs[ii] = 0;
}
/* Compute the minimum points of piecewise smoothness */
m_LowPoint[rgb] = origin;
change = 1;
x = origin;
numx = 1;
while ( change > 0 )
{
change = 0;
for (unsigned int i=0; i<4; i++)
{
if (signs[i] == 0)
{
const LabelType difference =
static_cast< LabelType > ( vnl_math_abs(m_LowPoint[rgb] - neighbors[i]) );
if ( difference < m_BoundaryGradient )
{
numx++;
x += neighbors[i];
signs[i]++;
change++;
}
}
}
m_LowPoint[rgb] = x/numx;
for (unsigned int i=0; i<4; i++)
{
if (signs[i] == 1)
{
const LabelType difference =
static_cast< LabelType > ( vnl_math_abs(m_LowPoint[rgb] - neighbors[i]) );
if ( difference > m_BoundaryGradient )
{
numx--;
x -= neighbors[i];
signs[i]--;
change++;
}
}
}
m_LowPoint[rgb] = x/numx;
}
}
}
/* Set the classifier. */
template<typename TInputImage, typename TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::SetClassifier( typename ClassifierType::Pointer ptrToClassifier )
{
m_ClassifierPtr = ptrToClassifier;
m_ClassifierPtr->SetNumberOfClasses( m_NumberOfClasses );
}
/* Set the training image. */
template<typename TInputImage, typename TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::SetTrainingImage( TrainingImageType image )
{
m_TrainingImage = image;
}
/* Check if 2 number are identical. */
template <typename TInputImage, typename TClassifiedImage>
int
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::Sim(int a, int b)
{
if (a == b) return 1;
return 0;
}
/* GibbsTotalEnergy method that minimizes the local characteristic(f_2) term
* in the energy function. */
template <typename TInputImage, typename TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::GibbsTotalEnergy(int i)
{
LabelledImageIndexType offsetIndex3D;
offsetIndex3D.Fill(0);
int size = m_ImageWidth * m_ImageHeight * m_ImageDepth;
int frame = m_ImageWidth * m_ImageHeight;
int rowsize = m_ImageWidth;
double energy[2];
double difenergy;
LabelType label;
LabelType originlabel;
LabelType f[8];
unsigned int j;
unsigned int k;
unsigned int neighborcount=0;
offsetIndex3D[2] = i / frame;
offsetIndex3D[1] = (i % frame) / rowsize;
offsetIndex3D[0] = (i % frame) % rowsize;
if ((i > rowsize - 1)&&((i%rowsize) != rowsize - 1)&&
(i < size - rowsize)&&((i%rowsize) != 0)) {
offsetIndex3D[0]--;
offsetIndex3D[1]--;
f[neighborcount++] = (int)m_LabelledImage->GetPixel( offsetIndex3D );
offsetIndex3D[0]++;
f[neighborcount++] = (int)m_LabelledImage->GetPixel( offsetIndex3D );
offsetIndex3D[0]++;
f[neighborcount++] = (int)m_LabelledImage->GetPixel( offsetIndex3D );
offsetIndex3D[1]++;
f[neighborcount++] = (int)m_LabelledImage->GetPixel( offsetIndex3D );
offsetIndex3D[1]++;
f[neighborcount++] = (int)m_LabelledImage->GetPixel( offsetIndex3D );
offsetIndex3D[0]--;
f[neighborcount++] = (int)m_LabelledImage->GetPixel( offsetIndex3D );
offsetIndex3D[0]--;
f[neighborcount++] = (int)m_LabelledImage->GetPixel( offsetIndex3D );
offsetIndex3D[1]--;
f[neighborcount] = (int)m_LabelledImage->GetPixel( offsetIndex3D );
}
k = 0;
for( j=0; j<8; j++ )
{
if (f[j] == m_ObjectLabel )
{
k++;
}
}
bool changeflag = (k > 3);
for(unsigned int jj = 0; jj < 2; jj++)
{
energy[jj] = 0;
energy[jj] += GibbsEnergy(i, 0, jj);
energy[jj] += GibbsEnergy(i+rowsize+1, 1, jj);
energy[jj] += GibbsEnergy(i+rowsize, 2, jj);
energy[jj] += GibbsEnergy(i+rowsize-1, 3, jj);
energy[jj] += GibbsEnergy(i-1, 4, jj);
energy[jj] += GibbsEnergy(i-rowsize-1, 5, jj);
energy[jj] += GibbsEnergy(i-rowsize, 6, jj);
energy[jj] += GibbsEnergy(i-rowsize+1, 7, jj);
energy[jj] += GibbsEnergy(i+1, 8, jj);
if ( m_LabelStatus[i] == jj )
{
energy[jj] += -3;
}
else
{
energy[jj] += 3;
}
}
if ( energy[0] < energy[1] )
{
label = 0;
}
else
{
label = 1;
}
originlabel = m_LabelledImage->GetPixel(offsetIndex3D);
if (originlabel != label)
{
m_LabelledImage->SetPixel(offsetIndex3D, label);
}
else {
if (changeflag) {
difenergy = energy[label]-energy[1-label];
double rand_num = (double) (rand()/32768.0);
double energy_num = (double) (exp((double) (difenergy*0.5*size/(2*size-m_Temp))));
if ( rand_num < energy_num ) m_LabelledImage->SetPixel(offsetIndex3D, 1-label);
}
}
}
template <typename TInputImage, typename TClassifiedImage>
double
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::GibbsEnergy(unsigned int i, unsigned int k, unsigned int k1)
{
LabelledImageRegionIterator
labelledImageIt(m_LabelledImage, m_LabelledImage->GetBufferedRegion());
LabelType f[8];
int j;
unsigned int neighborcount = 0;
int simnum = 0;
int changenum = 0;
bool changeflag;
double res = 0.0;
LabelledImageIndexType offsetIndex3D;
offsetIndex3D.Fill(0);
LabelledImagePixelType labelledPixel = 0;
const unsigned int size = m_ImageWidth * m_ImageHeight * m_ImageDepth;
const unsigned int frame = m_ImageWidth * m_ImageHeight;
const unsigned int rowsize = m_ImageWidth;
offsetIndex3D[1] = (i % frame) / rowsize;
offsetIndex3D[0] = (i % frame) % rowsize;
if (k != 0)
{
labelledPixel =
( LabelledImagePixelType ) m_LabelledImage->GetPixel( offsetIndex3D );
}
if ( ( i > rowsize - 1 )
&& ( (i%rowsize) != rowsize - 1 )
&& ( i < size - rowsize )
&& ( (i%rowsize) != 0 ) )
{
offsetIndex3D[0]--;
offsetIndex3D[1]--;
f[neighborcount++] = static_cast< LabelType > ( m_LabelledImage->GetPixel( offsetIndex3D ) );
offsetIndex3D[0]++;
f[neighborcount++] = static_cast< LabelType > ( m_LabelledImage->GetPixel( offsetIndex3D ) );
offsetIndex3D[0]++;
f[neighborcount++] = static_cast< LabelType > ( m_LabelledImage->GetPixel( offsetIndex3D ) );
offsetIndex3D[1]++;
f[neighborcount++] = static_cast< LabelType > ( m_LabelledImage->GetPixel( offsetIndex3D ) );
offsetIndex3D[1]++;
f[neighborcount++] = static_cast< LabelType > ( m_LabelledImage->GetPixel( offsetIndex3D ) );
offsetIndex3D[0]--;
f[neighborcount++] = static_cast< LabelType > ( m_LabelledImage->GetPixel( offsetIndex3D ) );
offsetIndex3D[0]--;
f[neighborcount++] = static_cast< LabelType > ( m_LabelledImage->GetPixel( offsetIndex3D ) );
offsetIndex3D[1]--;
f[neighborcount++] = static_cast< LabelType > ( m_LabelledImage->GetPixel( offsetIndex3D ) );
}
/* Pixels at the edge of image will be dropped. */
if (neighborcount != 8)
{
return 0.0;
}
if (k != 0)
{
f[k-1] = k1;
}
else
{
labelledPixel = k1;
}
changeflag = (f[0] == labelledPixel);
/* Assuming we are segmenting objects with smooth boundaries, we give
* weight to local characteristics */
for( j=0; j<8 ;j++ )
{
if ( (f[j] == labelledPixel) != changeflag )
{
changenum++;
changeflag = !changeflag;
}
if (changeflag)
{
if (j%2==0)
{
res -= 0.7;
}
else
{
res -= 1.0;
}
simnum++;
}
}
if (changenum < 3)
{
if ( (simnum==4)||(simnum==5) )
{
return res -= m_CliqueWeight_2;
}
}
if ( simnum==8 )
{
return res -= m_CliqueWeight_4;
}
else return res -=m_CliqueWeight_6;
}
template<class TInputImage, class TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::GenerateData()
{
/* First run the Gaussian classifier calculator and
* generate the Gaussian model for the different classes.
* Then generate the initial labelled dataset.*/
m_InputImage = this->GetInput();
m_VecDim = InputPixelType::GetVectorDimension();
GenerateMediumImage();
/* Pass the input image and training image set to the
* classifier. For the first iteration, use the original image.
* In the following loops, you can use the result provided by a segmentation
* method such as the deformable model. */
m_ClassifierPtr->SetInputImage( m_InputImage );
/* Create the training image using the original image or the output
* of a segmentation method such as the deformable model. */
// m_ClassifierPtr->SetTrainingImage( m_TrainingImage );
/* Run the Gaussian classifier algorithm. */
m_ClassifierPtr->Update();
SetLabelledImage( m_ClassifierPtr->GetClassifiedImage() );
ApplyGPImageFilter();
/* Set the output labelled image and allocate the memory. */
LabelledImageType outputPtr = this->GetOutput();
if (m_RecursiveNumber == 0) {
outputPtr->SetLargestPossibleRegion( m_InputImage->GetLargestPossibleRegion() );
outputPtr->SetBufferedRegion( m_InputImage->GetLargestPossibleRegion() );
}
/* Allocate the output buffer memory. */
outputPtr->Allocate();
/* Copy labelled result to the Output buffer and set the iterators of
* the processed image. */
LabelledImageRegionIterator
labelledImageIt( m_LabelledImage, m_LabelledImage->GetBufferedRegion() );
/* Set the iterators of the output image buffer. */
LabelledImageRegionIterator
outImageIt( outputPtr, outputPtr->GetBufferedRegion() );
while ( !outImageIt.IsAtEnd() )
{
LabelledImagePixelType labelvalue =
( LabelledImagePixelType ) labelledImageIt.Get();
outImageIt.Set( labelvalue );
++labelledImageIt;
++outImageIt;
}
m_RecursiveNumber++;
}
template<class TInputImage, class TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::ApplyGPImageFilter()
{
/* Minimize f_1 and f_3. */
MinimizeFunctional();
}
template<class TInputImage, class TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::MinimizeFunctional()
{
/* This implementation uses the SA algorithm. */
ApplyGibbsLabeller();
RegionEraser();
#ifndef RGBGibbsPriorFilterNeedsDebugging
const unsigned int size = m_ImageWidth * m_ImageHeight * m_ImageDepth;
const unsigned int rowsize = m_ImageWidth;
m_Temp = 0;
srand( static_cast<unsigned int>(time(NULL)) );
while ( m_Temp < 2 * size )
{
unsigned int randomPixel = static_cast<unsigned int>( size * rand() / RAND_MAX );
if ((randomPixel > (rowsize - 1)) && (randomPixel < (size - rowsize))
&& (randomPixel%rowsize != 0) && (randomPixel%rowsize != rowsize-1))
{
GibbsTotalEnergy(randomPixel); // minimized f_2;
}
m_Temp++;
}
#endif
}
template<class TInputImage, class TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::ApplyGibbsLabeller()
{
/* Set the iterators and the pixel type definition for the input image. */
InputImageRegionConstIterator inputImageIt(m_InputImage,
m_InputImage->GetBufferedRegion() );
InputImageRegionIterator mediumImageIt(m_MediumImage,
m_MediumImage->GetBufferedRegion() );
/* Set the iterators and the pixel type definition for the classified image. */
LabelledImageRegionIterator
labelledImageIt(m_LabelledImage, m_LabelledImage->GetBufferedRegion());
/* Variable to store the origin pixel vector value. */
InputImagePixelType OriginPixelVec;
/* Variable to store the modified pixel vector value. */
InputImagePixelType ChangedPixelVec;
/* Set a variable to store the offset index. */
LabelledImageIndexType offsetIndex3D; offsetIndex3D.Fill(0);
double * dist = new double[m_NumberOfClasses];
const unsigned int size = m_ImageWidth * m_ImageHeight * m_ImageDepth;
const unsigned int frame = m_ImageWidth * m_ImageHeight;
const unsigned int rowsize = m_ImageWidth;
inputImageIt.GoToBegin();
mediumImageIt.GoToBegin();
labelledImageIt.GoToBegin();
unsigned int i = 0;
while ( !inputImageIt.IsAtEnd() )
{
offsetIndex3D[2] = i / frame;
offsetIndex3D[1] = (i % frame) / rowsize;
offsetIndex3D[0] = (i % frame) % rowsize;
if ((i > (rowsize - 1)) && (i < (size - rowsize))
&& (i%rowsize != 0) && (i%rowsize != rowsize-1))
{
OriginPixelVec = inputImageIt.Get();
GreyScalarBoundary(offsetIndex3D);
for (unsigned int rgb = 0; rgb < m_VecDim; rgb++)
{
ChangedPixelVec[rgb] = m_LowPoint[rgb];
}
/* mediumImageIt.Set(ChangedPixelVec); */
}
else
{
ChangedPixelVec = inputImageIt.Get();
}
const std::vector<double> & distValue =
m_ClassifierPtr->GetPixelMembershipValue( ChangedPixelVec );
LabelType pixLabel;
if (distValue[1] > m_ObjectThreshold) pixLabel = 0;
else pixLabel = 1;
labelledImageIt.Set( pixLabel );
i++;
++labelledImageIt;
++inputImageIt;
++mediumImageIt;
}
delete []dist;
}
/* Remove the tiny bias inside the object region. */
template<class TInputImage, class TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::RegionEraser()
{
const unsigned int size = m_ImageWidth * m_ImageHeight * m_ImageDepth;
if (m_Region)
{
delete [] m_Region;
}
m_Region = new unsigned short[size];
if (m_RegionCount)
{
delete [] m_RegionCount;
}
m_RegionCount = new unsigned short[size];
unsigned short *valid_region_counter = new unsigned short[size];
LabelledImageRegionIterator
labelledImageIt(m_LabelledImage, m_LabelledImage->GetBufferedRegion());
for ( unsigned int r=0; r<size; r++ )
{
m_Region[r] = 0;
m_RegionCount[r] = 1;
valid_region_counter[r] = 0;
}
LabelType i = NumericTraits< LabelType >::Zero;
LabelType k = NumericTraits< LabelType >::Zero;
LabelType l = NumericTraits< LabelType >::Zero;
LabelType label;
while ( !labelledImageIt.IsAtEnd() )
{
if ( m_Region[i] == 0 )
{
label = labelledImageIt.Get();
if ( LabelRegion(i, ++l, label) > m_ClusterSize )
{
valid_region_counter[k] = l;
k++;
}
}
i++;
++labelledImageIt;
}
i = 0;
unsigned int j;
labelledImageIt.GoToBegin();
while ( !labelledImageIt.IsAtEnd() )
{
j = 0;
while ( (m_Region[i] != valid_region_counter[j]) && (j < k) )
{
j++;
}
if (j == k)
{
label = labelledImageIt.Get();
labelledImageIt.Set(1-label);
}
i++;
++labelledImageIt;
}
delete []valid_region_counter;
}
template<class TInputImage, class TClassifiedImage>
unsigned int
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::LabelRegion(int i, int l, int change)
{
unsigned int count = 1;
int m;
const unsigned int frame = m_ImageWidth * m_ImageHeight;
const unsigned int rowsize = m_ImageWidth;
LabelledImageIndexType offsetIndex3D;
offsetIndex3D.Fill(0);
m_Region[i] = l;
offsetIndex3D[2] = i / frame;
offsetIndex3D[1] = (i % frame) / rowsize;
offsetIndex3D[0] = (i % frame) % rowsize;
if (offsetIndex3D[0] > 0)
{
offsetIndex3D[0]--;
m = m_LabelledImage->GetPixel(offsetIndex3D);
if ((m==change)&&(m_Region[i-1]==0))
{
count += LabelRegion(i-1, l, change);
}
offsetIndex3D[0]++;
}
if (offsetIndex3D[0] < static_cast<IndexValueType>( m_ImageWidth-1 ) )
{
offsetIndex3D[0]++;
m = m_LabelledImage->GetPixel(offsetIndex3D);
if ((m==change)&&(m_Region[i+1]==0))
{
count += LabelRegion(i+1, l, change);
}
offsetIndex3D[0]--;
}
if (offsetIndex3D[1] > 0)
{
offsetIndex3D[1]--;
m = m_LabelledImage->GetPixel(offsetIndex3D);
if ((m==change)&&(m_Region[i-rowsize]==0))
{
count += LabelRegion(i-rowsize, l, change);
}
offsetIndex3D[1]++;
}
if (offsetIndex3D[1] < static_cast<IndexValueType>( m_ImageHeight-1 ) )
{
offsetIndex3D[1]++;
m = m_LabelledImage->GetPixel(offsetIndex3D);
if ((m==change)&&(m_Region[i+rowsize]==0))
{
count += LabelRegion(i+rowsize, l, change);
}
offsetIndex3D[1]--;
}
return count;
}
template<class TInputImage, class TClassifiedImage>
void
RGBGibbsPriorFilter<TInputImage, TClassifiedImage>
::PrintSelf( std::ostream& os, Indent indent ) const
{
Superclass::PrintSelf(os,indent);
os << indent << "NumberOfClasses: "
<< m_NumberOfClasses << std::endl;
os << indent << "MaximumNumberOfIterations: "
<< m_MaximumNumberOfIterations << std::endl;
os << indent << "ObjectThreshold: "
<< m_ObjectThreshold << std::endl;
os << indent << "BoundaryGradient: "
<< m_BoundaryGradient << std::endl;
os << indent << "CliqueWeight_1: "
<< m_CliqueWeight_1 << std::endl;
os << indent << "CliqueWeight_2: "
<< m_CliqueWeight_2 << std::endl;
os << indent << "CliqueWeight_3: "
<< m_CliqueWeight_3 << std::endl;
os << indent << "CliqueWeight_4: "
<< m_CliqueWeight_4 << std::endl;
os << indent << "CliqueWeight_5: "
<< m_CliqueWeight_5 << std::endl;
os << indent << "CliqueWeight_6: "
<< m_CliqueWeight_6 << std::endl;
os << indent << "ClusterSize: "
<< m_ClusterSize << std::endl;
os << indent << "ObjectLabel: "
<< m_ObjectLabel << std::endl;
os << indent << "StartPoint: "
<< m_StartPoint << std::endl;
if (m_TrainingImage)
{
os << "TraingImage: " << m_TrainingImage;
}
if (m_LabelledImage)
{
os << "TrainingImage: " << m_TrainingImage;
}
if (m_ClassifierPtr)
{
os << "ClassifierPtr: " << m_ClassifierPtr;
}
}
} /* end namespace itk. */
#endif
|