<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7655.8">
<TITLE>classification of a region defined by a PolyLine</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
<BR>
<P><FONT SIZE=2>Hi<BR>
I am interested in the classification of a region of an image with kmeans. My problem is that:<BR>
I want to classify only the pixel of a region that it isn't rectangular, for example a region represented by a polylineparametricpath.<BR>
How can I do this? is there a filter that can transform a itkPolyLineParametricPath to a RegionType?<BR>
I write this code that works only with rectangular region:<BR>
#include "itkImage.h"<BR>
#include "itkImageFileReader.h"<BR>
#include "itkImageFileWriter.h"<BR>
#include "itkScalarImageKmeansImageFilter.h"<BR>
<BR>
int main( int argc, char * argv [] )<BR>
{<BR>
if( argc < 5 )<BR>
{<BR>
std::cerr << "Usage: " << std::endl;<BR>
std::cerr << argv[0];<BR>
std::cerr << " inputScalarImage outputLabeledImage contiguousLabels";<BR>
std::cerr << " numberOfClasses mean1 mean2... meanN " << std::endl;<BR>
return EXIT_FAILURE;<BR>
}<BR>
<BR>
const char * inputImageFileName = argv[1];<BR>
<BR>
typedef signed short PixelType;<BR>
const unsigned int Dimension = 2;<BR>
<BR>
typedef itk::Image<PixelType, Dimension > ImageType;<BR>
<BR>
typedef itk::ImageFileReader< ImageType > ReaderType;<BR>
ReaderType::Pointer reader = ReaderType::New();<BR>
reader->SetFileName( inputImageFileName );<BR>
<BR>
typedef itk::ScalarImageKmeansImageFilter< ImageType > KMeansFilterType;<BR>
<BR>
KMeansFilterType::Pointer kmeansFilter = KMeansFilterType::New();<BR>
<BR>
kmeansFilter->SetInput( reader->GetOutput() );<BR>
<BR>
const unsigned int numberOfInitialClasses = atoi( argv[4] );<BR>
<BR>
const unsigned int useNonContiguousLabels = atoi( argv[3] );<BR>
<BR>
kmeansFilter->UseNonContiguousLabelsOn ();<BR>
<BR>
// to constrain classfication to a certain region<BR>
<BR>
ImageType::IndexType start;<BR>
start[0] = 40;<BR>
start[1] = 40;<BR>
<BR>
ImageType::SizeType size;<BR>
size[0] = 100;<BR>
size[1] = 100;<BR>
<BR>
ImageType::RegionType desiredRegion;<BR>
desiredRegion.SetSize( size );<BR>
desiredRegion.SetIndex( start );<BR>
<BR>
kmeansFilter->SetImageRegion( desiredRegion );<BR>
<BR>
<BR>
const unsigned int argoffset = 5;<BR>
<BR>
if( static_cast<unsigned int>(argc) < numberOfInitialClasses + argoffset )<BR>
{<BR>
std::cerr << "Error: " << std::endl;<BR>
std::cerr << numberOfInitialClasses << " classes has been specified ";<BR>
std::cerr << "but no enough means have been provided in the command ";<BR>
std::cerr << "line arguments " << std::endl;<BR>
return EXIT_FAILURE;<BR>
}<BR>
<BR>
for( unsigned k=0; k < numberOfInitialClasses; k++ )<BR>
{<BR>
const double userProvidedInitialMean = atof( argv[k+argoffset] );<BR>
kmeansFilter->AddClassWithInitialMean( userProvidedInitialMean );<BR>
}<BR>
<BR>
const char * outputImageFileName = argv[2];<BR>
<BR>
typedef KMeansFilterType::OutputImageType OutputImageType;<BR>
<BR>
typedef itk::ImageFileWriter< OutputImageType > WriterType;<BR>
<BR>
WriterType::Pointer writer = WriterType::New();<BR>
<BR>
writer->SetInput( kmeansFilter->GetOutput() );<BR>
<BR>
writer->SetFileName( outputImageFileName );<BR>
<BR>
try<BR>
{<BR>
writer->Update();<BR>
}<BR>
catch( itk::ExceptionObject & excp )<BR>
{<BR>
std::cerr << "Problem encountered while writing ";<BR>
std::cerr << " image file : " << argv[2] << std::endl;<BR>
std::cerr << excp << std::endl;<BR>
return EXIT_FAILURE;<BR>
}<BR>
<BR>
KMeansFilterType::ParametersType estimatedMeans = kmeansFilter->GetFinalMeans();<BR>
<BR>
const unsigned int numberOfClasses = estimatedMeans.Size();<BR>
<BR>
for ( unsigned int i = 0 ; i < numberOfClasses ; ++i )<BR>
{<BR>
std::cout << "cluster[" << i << "] ";<BR>
std::cout << " estimated mean : " << estimatedMeans[i] << std::endl;<BR>
}<BR>
<BR>
return EXIT_SUCCESS;<BR>
<BR>
}<BR>
<BR>
<BR>
<BR>
Thank you very much<BR>
Edoardo<BR>
</FONT>
</P>
</BODY>
</HTML>