[Insight-users] Help with otsumultiplethresholdfilter

David Doria daviddoria at gmail.com
Thu Jun 30 15:03:31 EDT 2011


n Thu, Jun 30, 2011 at 2:50 PM, barbababa <tonimuusimaki at gmail.com> wrote:
> Hello all!
>
> Help needed! Why do i get an image with a value -4.316e8 everywhere?
> It won't change no matter what i do! I am trying to make an image, with
> different
> values for  different thresholds. For example if the threshold is 5, then
> all pixels in the
> segmentated image below 5 is 1 and all above 5 is 2.
>
> thanks,
> toni
>
>
>
>        typedef itk::Image&lt;float, 2&gt; ImageType;
>                ImageType::Pointer sumimage = ImageType::New();
>                ImageType::Pointer sumimage1 = ImageType::New();
>                ImageType::SizeType size;
>
> //THIS WORKS
>                size[0]  = src_img_in.GetDimensionSize( 0 );
>                size[1]  = src_img_in.GetDimensionSize( 1 );
> //
>                ImageType::IndexType start;
>                start.Fill(0);
>
>                ImageType::RegionType region;
>                region.SetIndex( start );
>                region.SetSize( size );
>
>                sumimage->SetLargestPossibleRegion( region );
>                sumimage->SetBufferedRegion( region );
>                sumimage->SetRequestedRegion( region );
>                sumimage->Allocate();
>
>
>                sumimage1->SetLargestPossibleRegion( region );
>                sumimage1->SetBufferedRegion( region );
>                sumimage1->SetRequestedRegion( region );
>                sumimage1->Allocate();
>                typedef itk::Statistics::ScalarImageToHistogramGenerator<ImageType >
> ScalarImageToHistogramGeneratorType;
>                typedef
> itk::OtsuMultipleThresholdsCalculator<ScalarImageToHistogramGeneratorType::HistogramType
>> CalculatorType;
>                typedef itk::BinaryThresholdImageFilter< ImageType, ImageType >
> FilterType;
>                typedef itk::AddImageFilter< ImageType, ImageType ,ImageType>
> AddFilterType;
>
>                ScalarImageToHistogramGeneratorType::Pointer
> scalarImageToHistogramGenerator =
> ScalarImageToHistogramGeneratorType::New();
>                CalculatorType::Pointer calculator = CalculatorType::New();
>                FilterType::Pointer filter = FilterType::New();
>                AddFilterType::Pointer addFilter = AddFilterType::New();
>
>                scalarImageToHistogramGenerator->SetNumberOfBins(n_bins);
>                calculator->SetNumberOfThresholds(n_trsh);
> //THIS WORKS
>                if(flag) scalarImageToHistogramGenerator->SetInput(dmtoitk2(src_img_in));
>                else scalarImageToHistogramGenerator->SetInput(dmtoitk2(src_img));
>        //
>
> calculator->SetInputHistogram(scalarImageToHistogramGenerator->GetOutput());
>                filter->SetInput(dmtoitk2(src_img_in) );
>
>                scalarImageToHistogramGenerator->Compute();
>                calculator->Update();
>
>                const CalculatorType::OutputType &thresholdVector =
> calculator->GetOutput();
>                CalculatorType::OutputType::const_iterator itNum =
> thresholdVector.begin();
>
> //THIS WORKS
>                DM::ImageCalculateMinMax( src_img_in,1, 1, &min, &max );
> //
>                const float outsideValue = 0;
>
>
>                float lowerThreshold = min;
>                float upperThreshold;
>
>                filter->SetOutsideValue( outsideValue );
>
>                for(; itNum < thresholdVector.end(); itNum++)
>                {
>
>                const float insideValue = count;
>                filter->SetInsideValue( insideValue );
>
>            upperThreshold = static_cast<float>(*itNum);
>
>                filter->SetLowerThreshold( lowerThreshold );
>                filter->SetUpperThreshold( upperThreshold );
>                filter->Update();
>
>                lowerThreshold = upperThreshold;
>
>                addFilter->SetInput1( filter->GetOutput() );
>                addFilter->SetInput2(  sumimage );
>                //sumimage = addFilter->GetOutput( );
>                addFilter->Update();
>                sumimage1 = addFilter->GetOutput() ;
>                sumimage = sumimage1;
>
>                count++;
>                }
>                const float insideValue = count;
>
>
>                filter->SetInsideValue( count  );
>                filter->SetLowerThreshold( lowerThreshold );
>                filter->SetUpperThreshold( max );
>                filter->Update();
>
>
>                addFilter->SetInput1( filter->GetOutput() );
>                addFilter->SetInput2(  sumimage );
>                //sumimage = addFilter->GetOutput( );
>                addFilter->Update();
>                sumimage1 = addFilter->GetOutput() ;
>        //THIS WORKS
>                l_deriv_img_out = itktodm2( sumimage1);
>                l_deriv_img_out.SetName(( std::string("Segmentated ") + name ).c_str());
>
> --

This is a perfect use case for the Wish List section of the Examples Wiki.

I went ahead and copied what you posted here:
http://www.itk.org/Wiki/ITK/Examples/WishList/Segmentation/OtsuMultipleThresholdsCalculator

I would suggest you programatically generate an input image and
explain what it is and what you would expect the resulting thresholds
to be. At least get the code to compile so we can reproduce the bogus
output you are seeing. Then maybe someone can fix the wiki page
directly, or explain the problem to you so you can fix it. In either
case, the result is an easy to find, easy to maintain explanation of
how to use this class.

David


More information about the Insight-users mailing list