ITK:直方图创建和Bin访问
该示例介绍了如何在C++中使用ITK库创建直方图及其Bin对象,并展示了如何访问和配置这些Bin对象。直方图与itk::Statistics::ListSample、itk::Statistics::ImageToListSampleAdaptor或itk::Statistics::PointSetToListSampleAdaptor不同之处在于其测量向量可以具有可变数量的值(类型),而其他类别的所有测量向量具有固定值(一个)。此外,在该示例中使用了密集频率容器(itk::Statistics::DenseFrequencyContainer2),因为预期没有零频率测量值;如果预期有大量零箱,则应选择稀疏频率容器。
代码部分展示了如何配置测量向量大小为2个分量,并初始化了一个3x3的直方图,默认区间设置为[1.1, 7.1]和[2.6, 8.6]。通过SetFrequency方法设置了各个Bin的频率值,并通过GetFrequency和GetInstanceIdentifier方法验证了某个特定Bin(索引[0, 2])的频率和实例标识符为5和6。
运行该程序后会输出以下结果:
Frequency of the bin at index [0, 2] is 5 and the bin's instance identifier is 6
ITK:直方图创建和Bin访问
- 内容提要
- C++实现代码
内容提要
本示例阐述了构建 Histogram 的方法及其应用。
在 Histogram 类中,默认情况下每个实例被称为 bin。然而 Histogram 与其他三个类别的测量向量类型存在显著差异:直方图的每个测量向量可变数量(类型),而其他三个类别所有测量向量具有固定值(一个)。同样地,在这些类别中还存在另一种情况:数组类型的容器能够容纳多个数据元素。但在 Histogram 对象中,默认情况下任何给定的测量向量都有一个唯一的实例。
在这里我们将密集频率容器用于 Histogram 的构建,在 Python 环境中无法自定义设置其稀疏性参数。此外,在这些类别中还存在另一种情况:在这种情况下默认情况下任何给定的测量向量都有一个唯一的实例。在这里我们将密集频率容器用于 Histogram 的构建,在 Python 环境中无法自定义设置其稀疏性参数。
Frequency of the bin at index [0, 2] is 5 and the bin's instance identifier is 6
C++实现代码
#include "itkHistogram.h"
#include "itkDenseFrequencyContainer2.h"
int main()
{
using MeasurementType = float;
using FrequencyContainerType = itk::Statistics::DenseFrequencyContainer2;
constexpr unsigned int numberOfComponents = 2;
using HistogramType = itk::Statistics::Histogram<MeasurementType, FrequencyContainerType>;
HistogramType::Pointer histogram = HistogramType::New();
histogram->SetMeasurementVectorSize(numberOfComponents);
// We initialize it as a 3x3 histogram with equal size intervals.
HistogramType::SizeType size(numberOfComponents);
size.Fill(3);
HistogramType::MeasurementVectorType lowerBound(numberOfComponents);
HistogramType::MeasurementVectorType upperBound(numberOfComponents);
lowerBound[0] = 1.1;
lowerBound[1] = 2.6;
upperBound[0] = 7.1;
upperBound[1] = 8.6;
histogram->Initialize(size, lowerBound, upperBound);
histogram->SetFrequency(0, 0);
histogram->SetFrequency(1, 2);
histogram->SetFrequency(2, 3);
histogram->SetFrequency(3, 2);
histogram->SetFrequency(4, 1);
histogram->SetFrequency(5, 1);
histogram->SetFrequency(6, 5);
histogram->SetFrequency(7, 4);
histogram->SetFrequency(8, 0);
// Let us examine if the frequency is set correctly by calling the
// GetFrequency(index) method. We can use the
// GetFrequency(instance identifier) method for the same purpose.
HistogramType::IndexType index(numberOfComponents);
index[0] = 0;
index[1] = 2;
std::cout << "Frequency of the bin at index " << index << " is " << histogram->GetFrequency(index)
<< " and the bin's instance identifier is " << histogram->GetInstanceIdentifier(index) << std::endl;
return EXIT_SUCCESS;
}
