ITK/Examples/Broken/Statistics/ImageKmeansModelEstimator: Difference between revisions
Daviddoria (talk | contribs) (Created page with "==KmeansModelEstimator.cxx== <source lang="cpp"> #include "itkImage.h" #include "itkListSample.h" #include "itkVector.h" #include "itkImageKmeansModelEstimator.h" #include "itkIm...") |
|||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
== | ==ImageKmeansModelEstimator.cxx== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
#include "itkImage.h" | #include "itkImage.h" | ||
Line 5: | Line 5: | ||
#include "itkVector.h" | #include "itkVector.h" | ||
#include "itkImageKmeansModelEstimator.h" | #include "itkImageKmeansModelEstimator.h" | ||
#include "itkImageRegionIteratorWithIndex.h" | |||
#include "itkImageToListSampleAdaptor.h" | #include "itkImageToListSampleAdaptor.h" | ||
#include "itkDistanceToCentroidMembershipFunction.h" | #include "itkDistanceToCentroidMembershipFunction.h" | ||
Line 25: | Line 26: | ||
void ITKImagetoVTKImageColor(ColorImageType::Pointer image, vtkImageData* outputImage); | void ITKImagetoVTKImageColor(ColorImageType::Pointer image, vtkImageData* outputImage); | ||
void ITKImagetoVTKImageGrayscale(GrayscaleImageType::Pointer image, vtkImageData* outputImage); | void ITKImagetoVTKImageGrayscale(GrayscaleImageType::Pointer image, vtkImageData* outputImage); | ||
void CreateBlankImage(GrayscaleImageType::Pointer image, ColorImageType::Pointer inputImage); | |||
int main(int, char* [] ) | int main(int, char* [] ) | ||
{ | { | ||
// Create a demo image | |||
ColorImageType::Pointer image = ColorImageType::New(); | ColorImageType::Pointer image = ColorImageType::New(); | ||
CreateImage(image); | CreateImage(image); | ||
typedef itk::Statistics::DistanceToCentroidMembershipFunction< itk::Vector<unsigned char,3> > | // Compute pixel clusters using KMeans | ||
typedef itk::Statistics::DistanceToCentroidMembershipFunction< itk::Vector<unsigned char,3> > MembershipFunctionType ; | |||
typedef MembershipFunctionType::Pointer MembershipFunctionPointer ; | typedef MembershipFunctionType::Pointer MembershipFunctionPointer ; | ||
typedef std::vector< MembershipFunctionPointer > MembershipFunctionPointerVector; | |||
typedef itk::ImageKmeansModelEstimator<ColorImageType, MembershipFunctionType> ImageKmeansModelEstimatorType; | |||
typedef itk::ImageKmeansModelEstimator<ColorImageType, MembershipFunctionType> | |||
ImageKmeansModelEstimatorType::Pointer | ImageKmeansModelEstimatorType::Pointer | ||
Line 50: | Line 50: | ||
kmeansEstimator->SetMaxSplitAttempts( 10 ); | kmeansEstimator->SetMaxSplitAttempts( 10 ); | ||
kmeansEstimator->Update(); | kmeansEstimator->Update(); | ||
// Classify each pixel | |||
typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType ; | typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType ; | ||
typedef itk::Statistics::SampleClassifierFilter< SampleType > ClassifierType; | typedef itk::Statistics::SampleClassifierFilter< SampleType > ClassifierType; | ||
Line 58: | Line 60: | ||
classifier->SetDecisionRule(decisionRule); | classifier->SetDecisionRule(decisionRule); | ||
classifier->SetNumberOfClasses(3); | classifier->SetNumberOfClasses(3); | ||
Line 66: | Line 67: | ||
typedef ClassifierType::MembershipFunctionVectorType MembershipFunctionVectorType; | typedef ClassifierType::MembershipFunctionVectorType MembershipFunctionVectorType; | ||
MembershipFunctionPointerVector | // Setup membership functions | ||
MembershipFunctionPointerVector kmeansMembershipFunctions = | |||
kmeansEstimator->GetMembershipFunctions(); | |||
MembershipFunctionVectorObjectType::Pointer membershipFunctionsVectorObject = MembershipFunctionVectorObjectType::New(); | |||
classifier->SetMembershipFunctions(membershipFunctionsVectorObject); | |||
MembershipFunctionVectorType & membershipFunctionsVector = membershipFunctionsVectorObject->Get(); | |||
for(unsigned int i = 0; i < kmeansMembershipFunctions.size(); i++) | |||
{ | |||
membershipFunctionsVector.push_back(kmeansMembershipFunctions[i].GetPointer()); | |||
} | |||
// Setup class labels | |||
ClassLabelVectorObjectType::Pointer classLabelsObject = ClassLabelVectorObjectType::New(); | ClassLabelVectorObjectType::Pointer classLabelsObject = ClassLabelVectorObjectType::New(); | ||
classifier->SetClassLabels( classLabelsObject ); | classifier->SetClassLabels( classLabelsObject ); | ||
Line 77: | Line 90: | ||
classLabelsVector.push_back( 250 ); | classLabelsVector.push_back( 250 ); | ||
// Perform the classification | |||
typedef itk::Statistics::ImageToListSampleAdaptor< ColorImageType > SampleAdaptorType; | |||
SampleAdaptorType::Pointer sample = SampleAdaptorType::New(); | |||
sample->SetImage(image); | |||
classifier->SetInput(sample); | |||
classifier->Update(); | |||
// Prepare the output image | |||
GrayscaleImageType::Pointer outputImage = GrayscaleImageType::New(); | GrayscaleImageType::Pointer outputImage = GrayscaleImageType::New(); | ||
outputImage | CreateBlankImage(outputImage, image); | ||
// Setup the membership iterator | |||
itk:: | const ClassifierType::MembershipSampleType* membershipSample = classifier->GetOutput(); | ||
ClassifierType::MembershipSampleType::ConstIterator membershipIterator = membershipSample->Begin(); | |||
// Setup the output image iterator - this is automatically synchronized with the membership iterator since the sample is an adaptor | |||
itk::ImageRegionIteratorWithIndex<GrayscaleImageType> outputIterator(outputImage,outputImage->GetLargestPossibleRegion()); | |||
outputIterator.GoToBegin(); | |||
while(! | while(membershipIterator != membershipSample->End()) | ||
{ | { | ||
int classLabel = membershipIterator.GetClassLabel(); | |||
std::cout << "Class label: " << classLabel << std::endl; | |||
outputIterator.Set(static_cast<unsigned char>(classLabel)); | |||
++membershipIterator; | |||
++outputIterator; | |||
++ | |||
} | } | ||
// Visualize | // Visualize | ||
vtkSmartPointer<vtkImageData> | // Original image | ||
vtkSmartPointer<vtkImageData> originalVTKImage = | |||
vtkSmartPointer<vtkImageData>::New(); | vtkSmartPointer<vtkImageData>::New(); | ||
ITKImagetoVTKImageColor(image, originalVTKImage); | |||
ITKImagetoVTKImageColor(image, | |||
vtkSmartPointer<vtkImageActor> | vtkSmartPointer<vtkImageActor> originalActor = | ||
vtkSmartPointer<vtkImageActor>::New(); | vtkSmartPointer<vtkImageActor>::New(); | ||
originalActor->SetInput(originalVTKImage); | |||
// Kmeans image | |||
vtkSmartPointer<vtkImageData> kmeansVTKImage = | |||
vtkSmartPointer<vtkImageData>::New(); | |||
ITKImagetoVTKImageGrayscale(outputImage, kmeansVTKImage); | |||
vtkSmartPointer<vtkImageActor> kmeansActor = | |||
vtkSmartPointer<vtkImageActor>::New(); | |||
kmeansActor->SetInput(kmeansVTKImage); | |||
// There will be one render window | // There will be one render window | ||
vtkSmartPointer<vtkRenderWindow> renderWindow = | vtkSmartPointer<vtkRenderWindow> renderWindow = | ||
vtkSmartPointer<vtkRenderWindow>::New(); | vtkSmartPointer<vtkRenderWindow>::New(); | ||
renderWindow->SetSize( | renderWindow->SetSize(600, 300); | ||
// Define viewport ranges | |||
// (xmin, ymin, xmax, ymax) | |||
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0}; | |||
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0}; | |||
vtkSmartPointer<vtkRenderWindowInteractor> interactor = | vtkSmartPointer<vtkRenderWindowInteractor> interactor = | ||
vtkSmartPointer<vtkRenderWindowInteractor>::New(); | vtkSmartPointer<vtkRenderWindowInteractor>::New(); | ||
interactor->SetRenderWindow(renderWindow); | interactor->SetRenderWindow(renderWindow); | ||
vtkSmartPointer<vtkRenderer> | vtkSmartPointer<vtkRenderer> leftRenderer = | ||
vtkSmartPointer<vtkRenderer>::New(); | vtkSmartPointer<vtkRenderer>::New(); | ||
renderWindow->AddRenderer( | renderWindow->AddRenderer(leftRenderer); | ||
leftRenderer->SetViewport(leftViewport); | |||
leftRenderer->SetBackground(.6, .5, .4); | |||
leftRenderer->AddActor(originalActor); | |||
leftRenderer->ResetCamera(); | |||
vtkSmartPointer<vtkRenderer> rightRenderer = | |||
vtkSmartPointer<vtkRenderer>::New(); | |||
renderWindow->AddRenderer(rightRenderer); | |||
rightRenderer->SetViewport(rightViewport); | |||
rightRenderer->SetBackground(.7, .4, .4); | |||
rightRenderer->AddActor(kmeansActor); | |||
rightRenderer->ResetCamera(); | |||
renderWindow->Render(); | renderWindow->Render(); | ||
Line 131: | Line 177: | ||
interactor->Start(); | interactor->Start(); | ||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||
Line 140: | Line 183: | ||
void CreateImage(ColorImageType::Pointer image) | void CreateImage(ColorImageType::Pointer image) | ||
{ | { | ||
// Create | // Create a black image with a red square and a green square | ||
ColorImageType::RegionType region; | ColorImageType::RegionType region; | ||
ColorImageType::IndexType start; | ColorImageType::IndexType start; | ||
Line 255: | Line 298: | ||
} | } | ||
} | } | ||
void CreateBlankImage(GrayscaleImageType::Pointer image, ColorImageType::Pointer inputImage) | |||
< | { | ||
image->SetRegions(inputImage->GetLargestPossibleRegion()); | |||
image->Allocate(); | |||
itk::ImageRegionIterator<GrayscaleImageType> imageIterator(image,image->GetLargestPossibleRegion()); | |||
while(!imageIterator.IsAtEnd()) | |||
{ | |||
imageIterator.Set(0); | |||
++imageIterator; | |||
} | |||
} | |||
</source> | |||
{{ITKVTKCMakeLists|{{SUBPAGENAME}}}} |
Latest revision as of 21:27, 23 December 2012
ImageKmeansModelEstimator.cxx
<source lang="cpp">
- include "itkImage.h"
- include "itkListSample.h"
- include "itkVector.h"
- include "itkImageKmeansModelEstimator.h"
- include "itkImageRegionIteratorWithIndex.h"
- include "itkImageToListSampleAdaptor.h"
- include "itkDistanceToCentroidMembershipFunction.h"
- include "itkSampleClassifierFilter.h"
- include "itkMinimumDecisionRule2.h"
- include "vtkSmartPointer.h"
- include "vtkImageActor.h"
- include "vtkImageData.h"
- include "vtkInteractorStyleImage.h"
- include "vtkRenderer.h"
- include "vtkRenderWindow.h"
- include "vtkRenderWindowInteractor.h"
typedef itk::Vector<unsigned char,3> MeasurementVectorType; typedef itk::Image<MeasurementVectorType,2> ColorImageType; typedef itk::Image<unsigned char,2> GrayscaleImageType;
void CreateImage(ColorImageType::Pointer image); void ITKImagetoVTKImageColor(ColorImageType::Pointer image, vtkImageData* outputImage); void ITKImagetoVTKImageGrayscale(GrayscaleImageType::Pointer image, vtkImageData* outputImage); void CreateBlankImage(GrayscaleImageType::Pointer image, ColorImageType::Pointer inputImage);
int main(int, char* [] ) {
// Create a demo image ColorImageType::Pointer image = ColorImageType::New(); CreateImage(image);
// Compute pixel clusters using KMeans typedef itk::Statistics::DistanceToCentroidMembershipFunction< itk::Vector<unsigned char,3> > MembershipFunctionType ; typedef MembershipFunctionType::Pointer MembershipFunctionPointer ; typedef std::vector< MembershipFunctionPointer > MembershipFunctionPointerVector;
typedef itk::ImageKmeansModelEstimator<ColorImageType, MembershipFunctionType> ImageKmeansModelEstimatorType;
ImageKmeansModelEstimatorType::Pointer kmeansEstimator = ImageKmeansModelEstimatorType::New(); kmeansEstimator->SetInputImage(image); kmeansEstimator->SetNumberOfModels(3); kmeansEstimator->SetThreshold(0.01 ); kmeansEstimator->SetOffsetAdd( 0.01 ); kmeansEstimator->SetOffsetMultiply( 0.01 ); kmeansEstimator->SetMaxSplitAttempts( 10 ); kmeansEstimator->Update();
// Classify each pixel typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType ; typedef itk::Statistics::SampleClassifierFilter< SampleType > ClassifierType; ClassifierType::Pointer classifier = ClassifierType::New();
typedef itk::Statistics::MinimumDecisionRule2 DecisionRuleType; DecisionRuleType::Pointer decisionRule = DecisionRuleType::New(); classifier->SetDecisionRule(decisionRule); classifier->SetNumberOfClasses(3);
typedef ClassifierType::ClassLabelVectorObjectType ClassLabelVectorObjectType; typedef ClassifierType::ClassLabelVectorType ClassLabelVectorType; typedef ClassifierType::MembershipFunctionVectorObjectType MembershipFunctionVectorObjectType; typedef ClassifierType::MembershipFunctionVectorType MembershipFunctionVectorType;
// Setup membership functions MembershipFunctionPointerVector kmeansMembershipFunctions = kmeansEstimator->GetMembershipFunctions();
MembershipFunctionVectorObjectType::Pointer membershipFunctionsVectorObject = MembershipFunctionVectorObjectType::New(); classifier->SetMembershipFunctions(membershipFunctionsVectorObject);
MembershipFunctionVectorType & membershipFunctionsVector = membershipFunctionsVectorObject->Get();
for(unsigned int i = 0; i < kmeansMembershipFunctions.size(); i++) { membershipFunctionsVector.push_back(kmeansMembershipFunctions[i].GetPointer()); }
// Setup class labels ClassLabelVectorObjectType::Pointer classLabelsObject = ClassLabelVectorObjectType::New(); classifier->SetClassLabels( classLabelsObject );
ClassLabelVectorType & classLabelsVector = classLabelsObject->Get(); classLabelsVector.push_back( 50 ); classLabelsVector.push_back( 150 ); classLabelsVector.push_back( 250 );
// Perform the classification typedef itk::Statistics::ImageToListSampleAdaptor< ColorImageType > SampleAdaptorType; SampleAdaptorType::Pointer sample = SampleAdaptorType::New(); sample->SetImage(image);
classifier->SetInput(sample); classifier->Update(); // Prepare the output image GrayscaleImageType::Pointer outputImage = GrayscaleImageType::New(); CreateBlankImage(outputImage, image);
// Setup the membership iterator const ClassifierType::MembershipSampleType* membershipSample = classifier->GetOutput(); ClassifierType::MembershipSampleType::ConstIterator membershipIterator = membershipSample->Begin();
// Setup the output image iterator - this is automatically synchronized with the membership iterator since the sample is an adaptor itk::ImageRegionIteratorWithIndex<GrayscaleImageType> outputIterator(outputImage,outputImage->GetLargestPossibleRegion()); outputIterator.GoToBegin(); while(membershipIterator != membershipSample->End()) { int classLabel = membershipIterator.GetClassLabel(); std::cout << "Class label: " << classLabel << std::endl; outputIterator.Set(static_cast<unsigned char>(classLabel)); ++membershipIterator; ++outputIterator; }
// Visualize // Original image vtkSmartPointer<vtkImageData> originalVTKImage = vtkSmartPointer<vtkImageData>::New(); ITKImagetoVTKImageColor(image, originalVTKImage);
vtkSmartPointer<vtkImageActor> originalActor = vtkSmartPointer<vtkImageActor>::New(); originalActor->SetInput(originalVTKImage);
// Kmeans image vtkSmartPointer<vtkImageData> kmeansVTKImage = vtkSmartPointer<vtkImageData>::New(); ITKImagetoVTKImageGrayscale(outputImage, kmeansVTKImage);
vtkSmartPointer<vtkImageActor> kmeansActor = vtkSmartPointer<vtkImageActor>::New(); kmeansActor->SetInput(kmeansVTKImage); // There will be one render window vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->SetSize(600, 300);
// Define viewport ranges // (xmin, ymin, xmax, ymax) double leftViewport[4] = {0.0, 0.0, 0.5, 1.0}; double rightViewport[4] = {0.5, 0.0, 1.0, 1.0}; vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); interactor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkRenderer> leftRenderer = vtkSmartPointer<vtkRenderer>::New(); renderWindow->AddRenderer(leftRenderer); leftRenderer->SetViewport(leftViewport); leftRenderer->SetBackground(.6, .5, .4);
leftRenderer->AddActor(originalActor); leftRenderer->ResetCamera();
vtkSmartPointer<vtkRenderer> rightRenderer = vtkSmartPointer<vtkRenderer>::New(); renderWindow->AddRenderer(rightRenderer); rightRenderer->SetViewport(rightViewport); rightRenderer->SetBackground(.7, .4, .4);
rightRenderer->AddActor(kmeansActor); rightRenderer->ResetCamera();
renderWindow->Render();
vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New(); interactor->SetInteractorStyle(style);
interactor->Start();
return EXIT_SUCCESS;
}
void CreateImage(ColorImageType::Pointer image) {
// Create a black image with a red square and a green square ColorImageType::RegionType region; ColorImageType::IndexType start; start[0] = 0; start[1] = 0;
ColorImageType::SizeType size; size[0] = 200; size[1] = 300;
region.SetSize(size); region.SetIndex(start);
image->SetRegions(region); image->Allocate();
itk::ImageRegionIterator<ColorImageType> imageIterator(image,region);
itk::Vector<unsigned char, 3> redPixel; redPixel[0] = 255; redPixel[1] = 0; redPixel[2] = 0;
itk::Vector<unsigned char, 3> greenPixel; greenPixel[0] = 0; greenPixel[1] = 255; greenPixel[2] = 0; itk::Vector<unsigned char, 3> blackPixel; blackPixel[0] = 0; blackPixel[1] = 0; blackPixel[2] = 0; while(!imageIterator.IsAtEnd()) { if(imageIterator.GetIndex()[0] > 100 && imageIterator.GetIndex()[0] < 150 && imageIterator.GetIndex()[1] > 100 && imageIterator.GetIndex()[1] < 150) { imageIterator.Set(redPixel); } else if(imageIterator.GetIndex()[0] > 50 && imageIterator.GetIndex()[0] < 70 && imageIterator.GetIndex()[1] > 50 && imageIterator.GetIndex()[1] < 70) { imageIterator.Set(greenPixel); } else { imageIterator.Set(blackPixel); }
++imageIterator; }
}
void ITKImagetoVTKImageColor(ColorImageType::Pointer image, vtkImageData* outputImage)
{
outputImage->SetNumberOfScalarComponents(3); outputImage->SetScalarTypeToUnsignedChar();
outputImage->SetDimensions(image->GetLargestPossibleRegion().GetSize()[0], image->GetLargestPossibleRegion().GetSize()[1], 1);
outputImage->AllocateScalars();
int* dims = outputImage->GetDimensions();
for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { unsigned char* pixel = static_cast<unsigned char*>(outputImage->GetScalarPointer(x,y,0)); ColorImageType::IndexType index;
index[0] = x; index[1] = y; pixel[0] = image->GetPixel(index)[0]; pixel[1] = image->GetPixel(index)[1]; pixel[2] = image->GetPixel(index)[2]; } }
}
void ITKImagetoVTKImageGrayscale(GrayscaleImageType::Pointer image, vtkImageData* outputImage)
{
outputImage->SetNumberOfScalarComponents(1); outputImage->SetScalarTypeToUnsignedChar();
outputImage->SetDimensions(image->GetLargestPossibleRegion().GetSize()[0], image->GetLargestPossibleRegion().GetSize()[1], 1);
outputImage->AllocateScalars();
int* dims = outputImage->GetDimensions();
for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { unsigned char* pixel = static_cast<unsigned char*>(outputImage->GetScalarPointer(x,y,0)); GrayscaleImageType::IndexType index;
index[0] = x; index[1] = y; pixel[0] = image->GetPixel(index); } }
}
void CreateBlankImage(GrayscaleImageType::Pointer image, ColorImageType::Pointer inputImage) {
image->SetRegions(inputImage->GetLargestPossibleRegion()); image->Allocate();
itk::ImageRegionIterator<GrayscaleImageType> imageIterator(image,image->GetLargestPossibleRegion());
while(!imageIterator.IsAtEnd()) {
imageIterator.Set(0);
++imageIterator; }
} </source>
CMakeLists.txt
<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)
project(ImageKmeansModelEstimator)
find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED) include(${VTK_USE_FILE})
else()
find_package(ItkVtkGlue REQUIRED) include(${ItkVtkGlue_USE_FILE}) set(Glue ItkVtkGlue)
endif()
add_executable(ImageKmeansModelEstimator MACOSX_BUNDLE ImageKmeansModelEstimator.cxx) target_link_libraries(ImageKmeansModelEstimator
${Glue} ${VTK_LIBRARIES} ${ITK_LIBRARIES})
</syntaxhighlight>
Download and Build ImageKmeansModelEstimator
Click here to download ImageKmeansModelEstimator. and its CMakeLists.txt file. Once the tarball ImageKmeansModelEstimator.tar has been downloaded and extracted,
cd ImageKmeansModelEstimator/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:
./ImageKmeansModelEstimator
WINDOWS USERS PLEASE NOTE: Be sure to add the VTK and ITK bin directories to your path. This will resolve the VTK and 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.