Example #1
0
bool LFFieldList::IsEqual( const LFLeafStruct& target ) const
/***********************************************************/
{
    WCPtrConstSListIter<LFSubField> refIter( _subFieldList );
    WCPtrConstSListIter<LFSubField> targetIter( dc(target)._subFieldList );
    while ( ++targetIter && ++refIter ) {
        if ( ! refIter.current() -> IsEquivalent( *targetIter.current() ) ) {
            return false;
        }
    }
    return true;
}
static void OverwriteImage(itk::Image< ipMITKSegmentationTYPE, 2 >::Pointer source, itk::Image< ipMITKSegmentationTYPE, 2 >::Pointer target)
{
  typedef itk::Image< ipMITKSegmentationTYPE, 2 > ItkImageType;
  typedef itk::ImageRegionIterator<ItkImageType> ImageIteratorType;

  ImageIteratorType sourceIter(source, source->GetLargestPossibleRegion());
  ImageIteratorType targetIter(target, target->GetLargestPossibleRegion());
  while ( ! sourceIter.IsAtEnd())
  {
    targetIter.Set(sourceIter.Get());
    ++sourceIter;
    ++targetIter;
  }
}
void mitk::TumorInvasionClassification::SelectTrainingSamples(mitk::DataCollection *collection, unsigned int mode)
{
  srand (time(NULL));
  MITK_INFO << "LearnProgressionFeatures: Selecting training voxels.";
  switch (mode) {
  case 0:
  {
    MITK_INFO << " Selection Mode " << mode << " use all tumor voxels, healthy: 50% vicinity / 50% far away";

    CollectionDilation::DilateBinaryByName(collection,m_TargetID,2,0,"EXCLUDE");
    CollectionDilation::ErodeBinaryByName(collection,m_TargetID,1,0,"ERODE");
    CollectionDilation::DilateBinaryByName(collection,m_TargetID,m_TargetDil2D,m_TargetDil3D,"TRAIN");
    DataCollectionImageIterator<unsigned char,3> gtvIter(collection, m_TumorID);
    DataCollectionImageIterator<unsigned char,3> brainMaskIter(collection, m_MaskID);
    DataCollectionImageIterator<unsigned char,3> targetIter(collection, m_TargetID);
    DataCollectionImageIterator<unsigned char,3> targetDil(collection, m_TargetID+"TRAIN");

    // Count Healthy/ Tumor voxels
    // i.o. to decide how many of each are taken
    unsigned int totalTumor = 0;
    unsigned int totalHealthyClose = 0;
    unsigned int totalHealthy= 0;

    while (!brainMaskIter.IsAtEnd())
    {
      if (brainMaskIter.GetVoxel() != 0)
      {
        if (targetIter.GetVoxel() == 1 && gtvIter.GetVoxel() == 0 )
          ++totalTumor;

        if (targetIter.GetVoxel() == 0 && targetDil.GetVoxel()==1 && gtvIter.GetVoxel() == 0)
          ++totalHealthyClose;

        if (targetIter.GetVoxel() == 0  && gtvIter.GetVoxel() == 0 && targetDil.GetVoxel()==0)
          ++totalHealthy; // healthy but not close

      }
      ++brainMaskIter;
      ++targetIter;
      ++targetDil;
      ++gtvIter;
    }
    brainMaskIter.ToBegin();
    targetIter.ToBegin();
    targetDil.ToBegin();
    gtvIter.ToBegin();
    // Total of healthy samples that is to be collected
    unsigned int targetHealthy = totalTumor * m_ClassRatio;
    // Determines which portion of the healthy samples is drawn from the immediate vicinity of the newly grown tumor
    ScalarType ratioClose = .5;

    // Compute probabilities thresholds for choosing a close healthy voxel / any healthy voxel
    ScalarType thHealthyClose =  std::min(1.0 ,(targetHealthy*ratioClose)/totalHealthyClose);
    ScalarType thHealthyAny =  std::min(1.0 ,(targetHealthy*(1.0-ratioClose))/totalHealthy);
    // Some stats
    {
      MITK_INFO << "Total # Tumor Voxels " << totalTumor;
      MITK_INFO << "Total # Healthy Voxels" << totalHealthy;
      MITK_INFO << "Target Ratio " << m_ClassRatio;
      MITK_INFO << "Ratio for healthy close: " << thHealthyClose;
      MITK_INFO << "Ratio for healthy any other: " << thHealthyAny;
    }

    // DEBUG count occurances to compare
    unsigned int tumor = 0;
    unsigned int healthy = 0;
    while (!brainMaskIter.IsAtEnd())
    {
      if (brainMaskIter.GetVoxel() != 0)
      {
        if (targetIter.GetVoxel() == 1)
        { // choose tumor voxels for training
          if (gtvIter.GetVoxel() == 0) // tumor always
          {
            targetIter.SetVoxel(brainMaskIter.GetVoxel()+targetIter.GetVoxel());
            ++tumor;
          }
          else
            targetIter.SetVoxel(0);
        }
        else
        { // choose healty tissue voxels for training
          ScalarType rndVal =  (float)rand()/(float)(RAND_MAX) ; //(0..1)
          if (gtvIter.GetVoxel() == 0 &&  ((targetDil.GetVoxel()==1 && rndVal <= thHealthyClose)))
          {
            targetIter.SetVoxel(brainMaskIter.GetVoxel()+targetIter.GetVoxel());
            ++healthy;
          }
          else if (((targetDil.GetVoxel()==0 &&  rndVal <= thHealthyAny)))
          {
            targetIter.SetVoxel(brainMaskIter.GetVoxel()+targetIter.GetVoxel());
            ++healthy;
          }
          else
            targetIter.SetVoxel(0);
        }
      }
      else
        targetIter.SetVoxel(0);

      ++brainMaskIter;
      ++targetIter;
      ++gtvIter;
      ++targetDil;
    }
    MITK_INFO << "Training with Samples #Tumor " << tumor << " / healthy # "<< healthy;
  }
    break;
  default:
  {
    MITK_INFO << " Selection Mode " << mode << " Exclude voxels in border regions, healthy: 50% vicinity / 50% far away";

    //weights
    ScalarType tumorWeight = 1;
    //ScalarType unsureRegion = .25;
    ScalarType healthyTissue = 1;

    EnsureDataImageInCollection(collection, m_TumorID, "WEIGHTS");

    CollectionDilation::DilateBinaryByName(collection,m_TargetID,1,0,"EXCLUDE");
    CollectionDilation::ErodeBinaryByName(collection,m_TargetID,1,0,"ERODE");
    CollectionDilation::DilateBinaryByName(collection,m_TargetID+"EXCLUDE",m_TargetDil2D,m_TargetDil3D,"TRAIN");
    DataCollectionImageIterator<unsigned char,3> gtvIter(collection, m_TumorID);
    DataCollectionImageIterator<unsigned char,3> brainMaskIter(collection, m_MaskID);
    DataCollectionImageIterator<unsigned char,3> targetIter(collection, m_TargetID);
    DataCollectionImageIterator<unsigned char,3> targetDil(collection, m_TargetID+"EXCLUDETRAIN");
    DataCollectionImageIterator<unsigned char,3> excludeTumorIter(collection, m_TargetID+"ERODE");
    DataCollectionImageIterator<unsigned char,3> excludeHealthyIter(collection, m_TargetID+"EXCLUDE");

    DataCollectionImageIterator<double,3> weightsIter(collection, "WEIGHTS");

    // Count Healthy/ Tumor voxels
    // i.o. to decide how many of each are taken
    unsigned int totalTumor = 0;
    unsigned int totalHealthyClose = 0;
    unsigned int totalHealthyNonClose= 0;
    unsigned int totalHealthy= 0;

    while (!brainMaskIter.IsAtEnd())
    {
      if (brainMaskIter.GetVoxel() != 0)
      {
        if (targetIter.GetVoxel() == 1 && gtvIter.GetVoxel() == 0  && excludeTumorIter.GetVoxel() == 1)
          ++totalTumor;

        if (excludeHealthyIter.GetVoxel() == 0 && targetDil.GetVoxel()==1 && gtvIter.GetVoxel() == 0)
          ++totalHealthyClose;

        if (excludeHealthyIter.GetVoxel() == 0  && gtvIter.GetVoxel() == 0 && targetDil.GetVoxel()==0)
          ++totalHealthyNonClose; // healthy but not close

        if (excludeHealthyIter.GetVoxel() == 0  && gtvIter.GetVoxel() == 0 && targetIter.GetVoxel()==0)
          ++totalHealthy; // healthy
      }
      ++brainMaskIter;
      ++targetIter;
      ++targetDil;
      ++gtvIter;
      ++excludeHealthyIter;
      ++excludeTumorIter;
    }
    brainMaskIter.ToBegin();
    targetIter.ToBegin();
    targetDil.ToBegin();
    gtvIter.ToBegin();
    excludeHealthyIter.ToBegin();
    excludeTumorIter.ToBegin();

    // Total of healthy samples that is to be collected
    unsigned int targetHealthy = (tumorWeight/healthyTissue) * totalTumor * m_ClassRatio;
    // Determines which portion of the healthy samples is drawn from the immediate vicinity of the newly grown tumor
    ScalarType ratioClose = .5;

    // Compute probabilities thresholds for choosing a close healthy voxel / any healthy voxel
    ScalarType thHealthyClose =  std::min(1.0 ,((double)targetHealthy*ratioClose)/totalHealthyClose);
    ScalarType thHealthyAny =  std::min(1.0 ,((double)targetHealthy*(1.0-ratioClose))/totalHealthyNonClose);
    // Some stats
    {
      MITK_INFO << "Total Tumor " << totalTumor;
      MITK_INFO << "Total healthy " << totalHealthyNonClose;
      MITK_INFO << "Total healthy close " << totalHealthyClose;
      MITK_INFO << "Total healthy non-close " << totalHealthyNonClose;
      MITK_INFO << "Target Ratio " << m_ClassRatio;

      MITK_INFO << "Target Healthy " << targetHealthy;
      MITK_INFO << "Probabilty close " << thHealthyClose;
      MITK_INFO << "Probabilty any other " << thHealthyAny;
    }

    // DEBUG count occurances to compare
    unsigned int potentialClose = 0;
    unsigned int selectedClose = 0;
    unsigned int tumor = 0;
    unsigned int healthy = 0;
    while (!brainMaskIter.IsAtEnd())
    {
      weightsIter.SetVoxel(0);
      if (brainMaskIter.GetVoxel() != 0)
      {
        if (targetIter.GetVoxel() == 1)
        { // choose tumor voxels for training
          if (gtvIter.GetVoxel() == 0 && excludeTumorIter.GetVoxel() == 1) // tumor always
          {
            targetIter.SetVoxel(brainMaskIter.GetVoxel()+targetIter.GetVoxel());
            weightsIter.SetVoxel(tumorWeight);
            ++tumor;
          }
          else if (gtvIter.GetVoxel() == 0 )
          {
            weightsIter.SetVoxel(0); //.1);
            targetIter.SetVoxel(0); //2);
          }
        }
        else
        { // choose healty tissue voxels for training
          if (gtvIter.GetVoxel() == 0 &&  ((excludeHealthyIter.GetVoxel() == 0 && targetDil.GetVoxel()==1 )))
          {
           if ((float)rand()/(float)(RAND_MAX)  < thHealthyClose)
            {
              targetIter.SetVoxel(brainMaskIter.GetVoxel()+targetIter.GetVoxel());
              weightsIter.SetVoxel(1);
              ++healthy;
            }
          }
          else if (((targetDil.GetVoxel()==0 && excludeHealthyIter.GetVoxel() == 0 )))
          {
            if ((float)rand()/(float)(RAND_MAX)  < thHealthyAny)
            {
              targetIter.SetVoxel(brainMaskIter.GetVoxel()+targetIter.GetVoxel());
              weightsIter.SetVoxel(1);
              ++healthy;
            }
          }
          else if ((gtvIter.GetVoxel() == 0 &&  excludeHealthyIter.GetVoxel() == 1) )
          {
            targetIter.SetVoxel( 0);//brainMaskIter.GetVoxel()+targetIter.GetVoxel());
            weightsIter.SetVoxel(0);//.1);
          }
          else
          {
            targetIter.SetVoxel(0);
            weightsIter.SetVoxel(0);
          }

          if (gtvIter.GetVoxel() == 0 &&  ((excludeHealthyIter.GetVoxel() == 0 && targetDil.GetVoxel()==1 )))
          {
            potentialClose++;

            if ((float)rand()/(float)(RAND_MAX)  < thHealthyClose)
              selectedClose++;
          }
        }
      }
      else
      {
        targetIter.SetVoxel(0);
        weightsIter.SetVoxel(0);
      }

      if (gtvIter.GetVoxel() != 0)
        targetIter.SetVoxel(0);

      ++brainMaskIter;
      ++targetIter;
      ++gtvIter;
      ++targetDil;
      ++excludeHealthyIter;
      ++excludeTumorIter;
      ++weightsIter;
    }
    MITK_INFO << "Training with Samples #Tumor " << tumor << " / healthy # "<< healthy;
    MITK_INFO << "Potential Close" << potentialClose;
    MITK_INFO << "Selected Close" << selectedClose;
  }
    break;
  }

}