Ejemplo n.º 1
0
void test_iterators(test_harness& t, sam::alignment& aln) {
    sam::alignment::iterator it;
    it = aln.begin();
    sam::alignment::iterator it1 = it;
    sam::alignment::iterator it2(it);

    sam::alignment::const_iterator cit;
    cit = aln.begin();
    sam::alignment::const_iterator cit1 = cit;
    sam::alignment::const_iterator cit2(cit);

    t.check(it1 == it2, "iterator.==");
    t.check(!(it1 != it2), "iterator.!=");

    t.check(cit1 == cit2, "const_iterator.==");
    t.check(!(cit1 != cit2), "const_iterator.!=");

    t.check(it1 == cit2, "iterator__const_iterator.==");
    t.check(cit1 == it2, "const_iterator__iterator.==");
    t.check(!(it1 != cit2), "iterator__const_iterator.!=");
    t.check(!(cit1 != it2), "const_iterator__iterator.!=");

    std::cout << "begin: " << aln.begin() << ", end: " << aln.end() << "\n";

    std::string foo = "foo";
    int bar = 37;

    aln.dump_on(std::cout);
    std::cout << aln << "\npush_back.X1\n";
    aln.push_back("X1", foo);
    aln.dump_on(std::cout, aln.find("X1"));
    std::cout << aln << "\npush_back.X2\n";
    aln.push_back("X2", bar);
    //aln.push_back("X3", cit2);

    aln.dump_on(std::cout);
    std::cout << aln << "\ninsert.Y1\n";
    aln.insert(aln.begin(), "Y1", foo);
    aln.dump_on(std::cout, aln.begin());
    std::cout << aln << "\ninsert.Y2\n";
    aln.insert(aln.begin(), "Y2", bar);
    //aln.insert(aln.begin(), "Y3", cit2);

    //aln.replace(aln.begin(), aln.end(), "Y3", it2);

    aln.dump_on(std::cout);
    std::cout << aln << "\nset_aux.Z1\n";
    aln.set_aux("Z1", foo);
    aln.dump_on(std::cout);
    std::cout << aln << "\nset_aux.Z2\n";
    aln.set_aux("Z2", bar);
    aln.dump_on(std::cout);
    std::cout << aln << "\nset_aux.Z3\n";
    sam::alignment aln2 = aln;
    cit2 = aln2.find("X1");
    aln.set_aux("Z3", cit2);

    std::cout << "begin: " << aln.begin() << ", end: " << aln.end() << "\nauxen:";
    for (cit = aln.begin(); cit != aln.end(); ++cit)
        std::cout << " {" << *cit << "}";
    std::cout << "\n";

    it1 = aln.begin(), ++it1, ++it1;
    aln.dump_on(std::cout, it1);
    std::cout << aln << "\nset_aux.it1\n";
    aln.set_aux(it1, foo);
    aln.dump_on(std::cout, it1);
    std::cout << aln << "\nset_aux.it1\n";
    aln.set_aux(it1, bar);
    //aln.set_aux(it1, cit2);

    it = aln.begin();
    std::cout << "And finally...\n";
    aln.dump_on(std::cout, it);
    std::cout << "pre:  it: " << it << "\n";
    it++;
    std::cout << "post: it: " << it << "\n";
    aln.dump_on(std::cout, it);
    std::cout << "End of test_iterators()\n";
}
  mitk::Image::Pointer PartialVolumeAnalysisClusteringCalculator::CaculateAngularErrorImage(
      mitk::Image::Pointer comp1, mitk::Image::Pointer comp2, mitk::Image::Pointer probImg) const
  {

    // cast input images to itk
    typedef itk::Image<float, 3> ImageType;
    typedef mitk::ImageToItk<ImageType> CastType;
    CastType::Pointer caster = CastType::New();
    caster->SetInput(comp1);
    caster->Update();
    ImageType::Pointer comp1Image = caster->GetOutput();

    caster = CastType::New();
    caster->SetInput(comp2);
    caster->Update();
    ImageType::Pointer comp2Image = caster->GetOutput();

    caster = CastType::New();
    caster->SetInput(probImg);
    caster->Update();
    ImageType::Pointer probImage = caster->GetOutput();

    // figure out maximum probability for fiber class
    float maxProb = 0;
    itk::ImageRegionConstIterator<ImageType>
        itprob(probImage, probImage->GetLargestPossibleRegion());
    itprob.GoToBegin();
    while( !itprob.IsAtEnd() )
    {
      maxProb = itprob.Get() > maxProb ? itprob.Get() : maxProb;
      ++itprob;
    }

    // generate a list sample of angles at positions
    // where the fiber-prob is higher than .2*maxprob
    typedef float MeasurementType;
    const unsigned int MeasurementVectorLength = 2;
    typedef itk::Vector< MeasurementType , MeasurementVectorLength >
                                                                 MeasurementVectorType;
    typedef itk::Statistics::ListSample< MeasurementVectorType > ListSampleType;
    ListSampleType::Pointer listSample = ListSampleType::New();
    listSample->SetMeasurementVectorSize( MeasurementVectorLength );

    itk::ImageRegionIterator<ImageType>
        it1(comp1Image, comp1Image->GetLargestPossibleRegion());
    itk::ImageRegionIterator<ImageType>
        it2(comp2Image, comp2Image->GetLargestPossibleRegion());

    it1.GoToBegin();
    it2.GoToBegin();
    itprob.GoToBegin();

    while( !itprob.IsAtEnd() )
    {
      if(itprob.Get() > 0.2 * maxProb)
      {
        MeasurementVectorType mv;
        mv[0] = ( MeasurementType ) it1.Get();
        mv[1] = ( MeasurementType ) it2.Get();
        listSample->PushBack(mv);
      }
      ++it1;
      ++it2;
      ++itprob;
    }

    // generate a histogram from the list sample
    typedef float HistogramMeasurementType;
    typedef itk::Statistics::Histogram< HistogramMeasurementType, itk::Statistics::DenseFrequencyContainer2 > HistogramType;
    typedef itk::Statistics::SampleToHistogramFilter< ListSampleType, HistogramType > GeneratorType;
    GeneratorType::Pointer generator = GeneratorType::New();

    GeneratorType::HistogramType::SizeType size(2);
    size.Fill(30);
    generator->SetHistogramSize( size );

    generator->SetInput( listSample );
    generator->SetMarginalScale( 10.0 );

    generator->Update();

    // look for frequency mode in the histogram
    GeneratorType::HistogramType::ConstPointer histogram = generator->GetOutput();
    GeneratorType::HistogramType::ConstIterator iter = histogram->Begin();
    float maxFreq = 0;
    MeasurementVectorType maxValue;
    maxValue.Fill(0);
    while ( iter != histogram->End() )
    {
      if(iter.GetFrequency() > maxFreq)
      {
        maxFreq = iter.GetFrequency();
        maxValue[0] = iter.GetMeasurementVector()[0];
        maxValue[1] = iter.GetMeasurementVector()[1];
      }
      ++iter;
    }

    // generate return image that contains the angular
    // error of the voxels to the histogram max measurement
    ImageType::Pointer returnImage = ImageType::New();
    returnImage->SetSpacing( comp1Image->GetSpacing() );   // Set the image spacing
    returnImage->SetOrigin( comp1Image->GetOrigin() );     // Set the image origin
    returnImage->SetDirection( comp1Image->GetDirection() );  // Set the image direction
    returnImage->SetRegions( comp1Image->GetLargestPossibleRegion() );
    returnImage->Allocate();

    itk::ImageRegionConstIterator<ImageType>
        cit1(comp1Image, comp1Image->GetLargestPossibleRegion());

    itk::ImageRegionConstIterator<ImageType>
        cit2(comp2Image, comp2Image->GetLargestPossibleRegion());

    itk::ImageRegionIterator<ImageType>
        itout(returnImage, returnImage->GetLargestPossibleRegion());

    cit1.GoToBegin();
    cit2.GoToBegin();
    itout.GoToBegin();

    vnl_vector<float> v(3);
    v[0] = cos( maxValue[0] ) * sin( maxValue[1] );
    v[1] = sin( maxValue[0] ) * sin( maxValue[1] );
    v[2] = cos( maxValue[1] );
//    MITK_INFO << "max vector: " << v;
    while( !cit1.IsAtEnd() )
    {
      vnl_vector<float> v1(3);
      v1[0] = cos( cit1.Get() ) * sin( cit2.Get() );
      v1[1] = sin( cit1.Get() ) * sin( cit2.Get() );
      v1[2] = cos( cit2.Get() );

      itout.Set(fabs(angle(v,v1)));
//      MITK_INFO << "ang_error " << v1 << ": " << fabs(angle(v,v1));

      ++cit1;
      ++cit2;
      ++itout;
    }

    mitk::Image::Pointer retval = mitk::Image::New();
    retval->InitializeByItk(returnImage.GetPointer());
    retval->SetVolume(returnImage->GetBufferPointer());
    return retval;
  }