Create List of Samples With Associated ID’s#
Synopsis#
Create a list of samples with associated class IDs.
Results#
Output:
instance identifier = 0 measurement vector = [1, 2, 4] frequency = 1 class label = 0
instance identifier = 1 measurement vector = [2, 4, 5] frequency = 1 class label = 0
instance identifier = 2 measurement vector = [3, 8, 6] frequency = 1 class label = 1
instance identifier = 0 measurement vector = [1, 2, 4] frequency = 1
instance identifier = 1 measurement vector = [2, 4, 5] frequency = 1
Code#
C++#
#include "itkListSample.h"
#include "itkMembershipSample.h"
#include "itkVector.h"
int
main()
{
using MeasurementVectorType = itk::Vector<float, 3>;
using SampleType = itk::Statistics::ListSample<MeasurementVectorType>;
auto sample = SampleType::New();
MeasurementVectorType mv;
mv[0] = 1.0;
mv[1] = 2.0;
mv[2] = 4.0;
sample->PushBack(mv);
mv[0] = 2.0;
mv[1] = 4.0;
mv[2] = 5.0;
sample->PushBack(mv);
mv[0] = 3.0;
mv[1] = 8.0;
mv[2] = 6.0;
sample->PushBack(mv);
using MembershipSampleType = itk::Statistics::MembershipSample<SampleType>;
auto membershipSample = MembershipSampleType::New();
membershipSample->SetSample(sample);
membershipSample->SetNumberOfClasses(2);
membershipSample->AddInstance(0U, 0UL);
membershipSample->AddInstance(0U, 1UL);
membershipSample->AddInstance(1U, 2UL);
MembershipSampleType::ConstIterator iter = membershipSample->Begin();
while (iter != membershipSample->End())
{
std::cout << "instance identifier = " << iter.GetInstanceIdentifier()
<< "\t measurement vector = " << iter.GetMeasurementVector() << "\t frequency = " << iter.GetFrequency()
<< "\t class label = " << iter.GetClassLabel() << std::endl;
++iter;
}
MembershipSampleType::ClassSampleType::ConstPointer classSample = membershipSample->GetClassSample(0);
MembershipSampleType::ClassSampleType::ConstIterator c_iter = classSample->Begin();
while (c_iter != classSample->End())
{
std::cout << "instance identifier = " << c_iter.GetInstanceIdentifier()
<< "\t measurement vector = " << c_iter.GetMeasurementVector()
<< "\t frequency = " << c_iter.GetFrequency() << std::endl;
++c_iter;
}
return EXIT_SUCCESS;
}
Classes demonstrated#
-
template<typename TSample>
class MembershipSample : public itk::DataObject Container for storing the instance-identifiers of other sample with their associated class labels.
This class does not store any measurement data. In a sense, you can think it as an additional information to basic samples (such as Histogram, PointSetListSampleAdaptor, and ImageToListSampleAdaptor). The additional information is a class label for a measurement vector. Obviously without such basic types of sample, this one is meaningless. You can call any basic methods that has been defined in the Sample class such as GetMeasurementVector and GetFrequency. You can query the class label for an instance using an instance-identifier. Another new and important method is the GetClassSample method. With a given class label, it returns a pointer to the Subsample object that has all the instance-identifiers of instances that belong to the class.
This class is templated over the type of the basic sample. To use all the method, you should first plug in a basic type sample using the SetSample method
- ITK Sphinx Examples: