[Insight-users] classification of a region defined by a PolyLine

edoardo.belletti at alice.it edoardo.belletti at alice.it
Wed Apr 7 10:07:37 EDT 2010


Hi 
I am interested in the classification of a region of an image with kmeans. My problem is that:
I want to classify only the pixel of a region that it isn't rectangular, for example a region represented by a polylineparametricpath.
How can I do this? is there a filter that can transform a itkPolyLineParametricPath to a RegionType?
I write this code that works only with rectangular region:
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkScalarImageKmeansImageFilter.h"

int main( int argc, char * argv [] )
{
  if( argc < 5 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << " inputScalarImage outputLabeledImage contiguousLabels";
    std::cerr << " numberOfClasses mean1 mean2... meanN " << std::endl;
    return EXIT_FAILURE;
    }

  const char * inputImageFileName = argv[1];

  typedef signed short       PixelType;
  const unsigned int          Dimension = 2;

  typedef itk::Image<PixelType, Dimension > ImageType;

  typedef itk::ImageFileReader< ImageType > ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( inputImageFileName );

  typedef itk::ScalarImageKmeansImageFilter< ImageType > KMeansFilterType;

  KMeansFilterType::Pointer kmeansFilter = KMeansFilterType::New();

  kmeansFilter->SetInput( reader->GetOutput() );

  const unsigned int numberOfInitialClasses = atoi( argv[4] );

  const unsigned int useNonContiguousLabels = atoi( argv[3] );

  kmeansFilter->UseNonContiguousLabelsOn ();

// to constrain classfication to a certain region 

		ImageType::IndexType start;
		start[0] = 40;
		start[1] = 40;
		
		ImageType::SizeType size;
		size[0] = 100;
		size[1] = 100;
		
		ImageType::RegionType desiredRegion;
		desiredRegion.SetSize( size );
		desiredRegion.SetIndex( start );

		kmeansFilter->SetImageRegion( desiredRegion );


  const unsigned int argoffset = 5;

  if( static_cast<unsigned int>(argc) < numberOfInitialClasses + argoffset )
    {
    std::cerr << "Error: " << std::endl;
    std::cerr << numberOfInitialClasses << " classes has been specified ";
    std::cerr << "but no enough means have been provided in the command ";
    std::cerr << "line arguments " << std::endl;
    return EXIT_FAILURE;
    }

  for( unsigned k=0; k < numberOfInitialClasses; k++ )
    {
    const double userProvidedInitialMean = atof( argv[k+argoffset] );
    kmeansFilter->AddClassWithInitialMean( userProvidedInitialMean );
    }

  const char * outputImageFileName = argv[2];

  typedef KMeansFilterType::OutputImageType  OutputImageType;

  typedef itk::ImageFileWriter< OutputImageType > WriterType;

  WriterType::Pointer writer = WriterType::New();
  
  writer->SetInput( kmeansFilter->GetOutput() );

  writer->SetFileName( outputImageFileName );

  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Problem encountered while writing ";
    std::cerr << " image file : " << argv[2] << std::endl;
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }

  KMeansFilterType::ParametersType estimatedMeans = kmeansFilter->GetFinalMeans();

  const unsigned int numberOfClasses = estimatedMeans.Size();

  for ( unsigned int i = 0 ; i < numberOfClasses ; ++i )
    {
    std::cout << "cluster[" << i << "] ";
    std::cout << "    estimated mean : " << estimatedMeans[i] << std::endl;
    }

  return EXIT_SUCCESS;
  
}



Thank you very much
Edoardo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100407/c7c44a29/attachment-0001.htm>


More information about the Insight-users mailing list