ITK/Examples/WishList/Segmentation/kMeansClustering: Difference between revisions
No edit summary |
Daviddoria (talk | contribs) (Match filename to wiki page name.) |
||
Line 1: | Line 1: | ||
== | ==kMeansClustering.cxx== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
#include <itkImage.h> | #include <itkImage.h> | ||
Line 9: | Line 9: | ||
{ | { | ||
//sample usage | //sample usage | ||
//./ | //./kMeansClustering input.jpg output.jpg 1 3 0 100 200 | ||
//verify command line arguments | //verify command line arguments | ||
Line 119: | Line 119: | ||
</source> | </source> | ||
{{ITKCMakeLists|kMeansClustering|}} | {{ITKCMakeLists|kMeansClustering|}} |
Revision as of 14:56, 28 October 2012
kMeansClustering.cxx
<source lang="cpp">
- include <itkImage.h>
- include <itkImageFileReader.h>
- include <itkImageFileWriter.h>
- include <itkScalarImageKmeansImageFilter.h>
int main( int argc, char * argv [] ) {
//sample usage //./kMeansClustering input.jpg output.jpg 1 3 0 100 200 //verify command line arguments 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; }
//parse command line arguments const char * inputImageFileName = argv[1]; const char * outputImageFileName = argv[2]; const unsigned int useNonContiguousLabels = atoi( argv[3] ); const unsigned int numberOfInitialClasses = atoi( argv[4] ); 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; } std::vector<double> userMeans; for( unsigned k = 0; k < numberOfInitialClasses; k++ ) { const double userProvidedInitialMean = atof( argv[k+argoffset] ); userMeans.push_back(userProvidedInitialMean); } // Define the pixel type and dimension of the image that we intend to // classify. typedef signed short PixelType; const unsigned int Dimension = 2;
typedef itk::Image<PixelType, Dimension > ImageType;
// create a reader typedef itk::ImageFileReader< ImageType > ReaderType; ReaderType::Pointer reader = ReaderType::New(); reader->SetFileName( inputImageFileName );
// Instantiate the ScalarImageKmeansImageFilter typedef itk::ScalarImageKmeansImageFilter< ImageType > KMeansFilterType;
KMeansFilterType::Pointer kmeansFilter = KMeansFilterType::New();
kmeansFilter->SetInput( reader->GetOutput() );
// Make the output image intellegable by expanding the range of output image values, if desired kmeansFilter->SetUseNonContiguousLabels( useNonContiguousLabels );
// initialize using the user input means for( unsigned k = 0; k < numberOfInitialClasses; k++ ) { kmeansFilter->AddClassWithInitialMean( userMeans[k] ); }
// Create and setup a writer typedef KMeansFilterType::OutputImageType OutputImageType;
typedef itk::ImageFileWriter< OutputImageType > WriterType;
WriterType::Pointer writer = WriterType::New(); writer->SetInput( kmeansFilter->GetOutput() );
writer->SetFileName( outputImageFileName );
// execut the pipeline try { writer->Update(); } catch( itk::ExceptionObject & excp ) { std::cerr << "Problem encountered while writing "; std::cerr << " image file : " << outputImageFileName << std::endl; std::cerr << excp << std::endl; return EXIT_FAILURE; }
// inspect the means 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;
}
</source>
CMakeLists.txt
<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)
project(kMeansClustering)
find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED) include(${VTK_USE_FILE})
endif()
add_executable(kMeansClustering MACOSX_BUNDLE kMeansClustering.cxx)
if( "${ITK_VERSION_MAJOR}" LESS 4 )
target_link_libraries(kMeansClustering ITKReview ${ITK_LIBRARIES})
else( "${ITK_VERSION_MAJOR}" LESS 4 )
target_link_libraries(kMeansClustering ${ITK_LIBRARIES})
endif( "${ITK_VERSION_MAJOR}" LESS 4 )
</syntaxhighlight>
Download and Build kMeansClustering
Click here to download kMeansClustering and its CMakeLists.txt file. Once the tarball kMeansClustering.tar has been downloaded and extracted,
cd kMeansClustering/build
- If ITK is installed:
cmake ..
- If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..
Build the project:
make
and run it:
./kMeansClustering
WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the ITK dll's at run time.
Building All of the Examples
Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.
ItkVtkGlue
ITK >= 4
For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.
ITK < 4
Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.