1、ITK库概述
ITK (Insight Segmentation and Registration Toolkit) 是一个开源的跨平台软件开发工具包,主要用于图像处理,特别是生物医学图像处理领域。该工具包提供了一套丰富的图像处理算法,特别是在图像分割和配准方面具有强大的功能。

ITK是一个基于C++的开源图像处理库,专为医学图像处理而设计。它提供了大量用于图像处理、分割和配准的算法,同时也支持图像的输入输出操作。
ITK库的主要特点包括:
- 跨平台支持 (Windows, Linux, macOS) - 基于泛型编程的设计
- 支持多线程处理
- 智能指针内存管理
- 强大的图像处理算法集合。
2、核心模块分类
ITK库按照功能可以分为几个主要模块:
2.1 图像输入输出 (Image IO)
负责各种图像格式的读写操作,包括DICOM、JPEG、PNG、TIFF等常见格式。
2.2 图像处理滤波器 (Image Filters)
提供各种图像处理操作,如滤波、形态学操作、阈值处理等。
2.3 图像配准 (Image Registration)
提供图像配准功能,包括各种变换模型、相似性度量和优化算法。
2.4 图像分割 (Image Segmentation)
提供图像分割算法,如阈值分割、区域生长、水平集等。
2.5 数学运算与变换 (Mathematical Operations and Transforms)
提供数学运算和各种变换操作,如傅里叶变换、小波变换等。
3、各模块功能详解
3.1 图像输入输出模块
3.2 图像处理滤波器模块函数详解
3.3 图像配准模块函数详解
3.4 图像分割模块函数详解
3.4.1 概述
图像分割是图像分析中的关键步骤,旨在将图像划分为多个区域或对象。ITK提供了多种图像分割算法,从简单的阈值分割到复杂的水平集方法,可以满足不同场景下的分割需求。图像分割在医学图像处理中尤为重要,例如器官分割、病变检测等。
3.4.2 阈值分割
阈值分割是最简单的图像分割方法,根据像素值将图像分为不同区域。
BinaryThresholdImageFilter
BinaryThresholdImageFilter 根据设定的阈值范围将图像转换为二值图像。在阈值范围内的像素被设置为一个值(通常为255),范围外的像素被设置为另一个值(通常为0)。
主要函数:
- SetLowerThreshold(InputPixelType lower): 设置较低阈值
- SetUpperThreshold(InputPixelType upper): 设置较高阈值
- SetInsideValue(OutputPixelType value): 设置阈值范围内的像素值
- SetOutsideValue(OutputPixelType value): 设置阈值范围外的像素值
示例代码:
cpp
#include "itkBinaryThresholdImageFilter.h"
using BinaryThresholdFilterType = itk::BinaryThresholdImageFilter<ImageType, ImageType>;
BinaryThresholdFilterType::Pointer thresholdFilter = BinaryThresholdFilterType::New();
thresholdFilter->SetInput(inputImage);
thresholdFilter->SetLowerThreshold(100);
thresholdFilter->SetUpperThreshold(200);
thresholdFilter->SetInsideValue(255);
thresholdFilter->SetOutsideValue(0);
thresholdFilter->Update();
OtsuThresholdImageFilter
OtsuThresholdImageFilter 实现Otsu自动阈值算法,该算法通过最大化类间方差来自动确定最佳阈值。
主要函数:
- SetNumberOfHistogramBins(unsigned long numberOfHistogramBins): 设置直方图箱数
示例代码:
cpp
#include "itkOtsuThresholdImageFilter.h"
using OtsuFilterType = itk::OtsuThresholdImageFilter<ImageType, ImageType>;
OtsuFilterType::Pointer otsuFilter = OtsuFilterType::New();
otsuFilter->SetInput(inputImage);
otsuFilter->SetNumberOfHistogramBins(128);
otsuFilter->SetInsideValue(255);
otsuFilter->SetOutsideValue(0);
otsuFilter->Update();
// 获取计算出的阈值
double threshold = otsuFilter->GetThreshold();
3.4.3 区域生长
区域生长方法从一个或多个种子点开始,根据相似性准则将相邻像素合并到区域中。
ConnectedComponentImageFilter
ConnectedComponentImageFilter 标记图像中的连接组件,为每个连接组件分配唯一的标签。
主要函数:
- SetFullyConnected(bool fullyConnected): 设置邻接关系(4邻接或8邻接)
示例代码:
cpp
#include "itkConnectedComponentImageFilter.h"
using ConnectedComponentFilterType = itk::ConnectedComponentImageFilter<ImageType, ImageType>;
ConnectedComponentFilterType::Pointer connectedComponentFilter = ConnectedComponentFilterType::New();
connectedComponentFilter->SetInput(inputImage);
connectedComponentFilter->SetFullyConnected(false); // 使用4邻接
connectedComponentFilter->Update();
ConfidenceConnectedImageFilter
ConfidenceConnectedImageFilter 基于统计置信度的区域生长算法,从种子点开始,根据像素值的统计特性进行区域生长。
主要函数:
- SetInitialNeighborhoodRadius(unsigned int radius): 设置初始邻域半径
- SetMultiplier(double multiplier): 设置置信度倍数
- SetNumberOfIterations(unsigned int iterations): 设置迭代次数
- SetReplaceValue(OutputImagePixelType value): 设置输出值
- SetSeed(const IndexType &seed): 添加种子点
示例代码:
cpp
#include "itkConfidenceConnectedImageFilter.h"
using ConfidenceConnectedFilterType = itk::ConfidenceConnectedImageFilter<ImageType, ImageType>;
ConfidenceConnectedFilterType::Pointer confidenceConnectedFilter = ConfidenceConnectedFilterType::New();
confidenceConnectedFilter->SetInput(inputImage);
confidenceConnectedFilter->SetInitialNeighborhoodRadius(2);
confidenceConnectedFilter->SetNumberOfIterations(5);
confidenceConnectedFilter->SetMultiplier(2.0);
confidenceConnectedFilter->SetReplaceValue(255);
// 添加种子点
ImageType::IndexType seed;
seed[0] = 100;
seed[1] = 100;
confidenceConnectedFilter->SetSeed(seed);
confidenceConnectedFilter->Update();
3.4.4 水平集方法
水平集方法是一类基于偏微分方程的分割方法,能够处理复杂的拓扑变化。
GeodesicActiveContourLevelSetImageFilter
GeodesicActiveContourLevelSetImageFilter 实现测地活动轮廓水平集算法,该算法结合了边缘信息和区域信息进行分割。
主要函数:
- SetPropagationScaling(double value): 设置传播速度权重
- SetAdvectionScaling(double value): 设置对流项权重
- SetCurvatureScaling(double value): 设置曲率权重
- SetMaximumRMSError(double value): 设置最大RMS误差
- SetNumberOfIterations(unsigned int iterations): 设置迭代次数
示例代码:
cpp
#include "itkGeodesicActiveContourLevelSetImageFilter.h"
using GeodesicActiveContourFilterType = itk::GeodesicActiveContourLevelSetImageFilter<ImageType, ImageType>;
GeodesicActiveContourFilterType::Pointer geodesicActiveContour = GeodesicActiveContourFilterType::New();
geodesicActiveContour->SetInput(levelSetImage); // 初始水平集
geodesicActiveContour->SetFeatureImage(featureImage); // 特征图像(通常是梯度图像)
geodesicActiveContour->SetPropagationScaling(1.0);
geodesicActiveContour->SetAdvectionScaling(1.0);
geodesicActiveContour->SetCurvatureScaling(1.0);
geodesicActiveContour->SetMaximumRMSError(0.02);
geodesicActiveContour->SetNumberOfIterations(100);
geodesicActiveContour->Update();
FastMarchingImageFilter
FastMarchingImageFilter 实现快速行进算法,用于生成初始水平集函数或进行距离变换。
主要函数:
- SetTrialPoints(NodeContainer*): 设置种子点
- SetSpeedConstant(double value): 设置速度常数
- SetStoppingValue(double value): 设置停止值
示例代码:
cpp
#include "itkFastMarchingImageFilter.h"
using FastMarchingFilterType = itk::FastMarchingImageFilter<ImageType, ImageType>;
using NodeContainer = FastMarchingFilterType::NodeContainer;
using NodeType = FastMarchingFilterType::NodeType;
FastMarchingFilterType::Pointer fastMarching = FastMarchingFilterType::New();
// 创建种子点
NodeContainer::Pointer seeds = NodeContainer::New();
seeds->Initialize();
ImageType::IndexType seedPosition;
seedPosition[0] = 100;
seedPosition[1] = 100;
NodeType node;
const double seedValue = 0.0;
node.SetValue(seedValue);
node.SetIndex(seedPosition);
seeds->InsertElement(0, node);
fastMarching->SetTrialPoints(seeds);
fastMarching->SetSpeedConstant(1.0);
fastMarching->SetStoppingValue(100);
fastMarching->SetInput(inputImage);
fastMarching->Update();
3.4.5 形态学分割
形态学分割方法基于数学形态学理论进行图像分割。
MorphologicalWatershedImageFilter
MorphologicalWatershedImageFilter 实现形态学分水岭算法,根据图像的形态学梯度进行分割。
主要函数:
- SetLevel(double level): 设置分水岭级别
- SetMarkWatershedLine(bool mark): 设置是否标记分水岭线
- SetFullyConnected(bool connected): 设置连接性
示例代码:
cpp
#include "itkMorphologicalWatershedImageFilter.h"
using WatershedFilterType = itk::MorphologicalWatershedImageFilter<ImageType, ImageType>;
WatershedFilterType::Pointer watershed = WatershedFilterType::New();
watershed->SetInput(inputImage);
watershed->SetLevel(0.5);
watershed->SetMarkWatershedLine(true);
watershed->SetFullyConnected(false);
watershed->Update();
MorphologicalWatershedFromMarkersImageFilter
MorphologicalWatershedFromMarkersImageFilter 实现基于标记的形态学分水岭算法,通过预定义的标记来控制分割结果。
示例代码:
cpp
#include "itkMorphologicalWatershedFromMarkersImageFilter.h"
using WatershedFromMarkersFilterType = itk::MorphologicalWatershedFromMarkersImageFilter<ImageType, ImageType>;
WatershedFromMarkersFilterType::Pointer watershedFromMarkers = WatershedFromMarkersFilterType::New();
watershedFromMarkers->SetInput(inputImage);
watershedFromMarkers->SetMarkerImage(markerImage);
watershedFromMarkers->Update();
3.4.6 使用示例
以下是一个完整的图像分割示例,结合多种分割方法:
cpp
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkOtsuThresholdImageFilter.h"
#include "itkConnectedComponentImageFilter.h"
#include "itkRelabelComponentImageFilter.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkBinaryMorphologicalClosingImageFilter.h"
int main(int argc, char* argv[])
{
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " inputImage outputImage" << std::endl;
return EXIT_FAILURE;
}
using InputImageType = itk::Image<unsigned char, 2>;
using OutputImageType = itk::Image<unsigned short, 2>;
// 读取输入图像
using ReaderType = itk::ImageFileReader<InputImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
// 使用Otsu算法自动阈值分割
using OtsuFilterType = itk::OtsuThresholdImageFilter<InputImageType, InputImageType>;
OtsuFilterType::Pointer otsuFilter = OtsuFilterType::New();
otsuFilter->SetInput(reader->GetOutput());
otsuFilter->SetNumberOfHistogramBins(128);
otsuFilter->SetInsideValue(255);
otsuFilter->SetOutsideValue(0);
otsuFilter->Update();
std::cout << "Otsu Threshold: " << otsuFilter->GetThreshold() << std::endl;
// 连接组件标记
using ConnectedComponentFilterType = itk::ConnectedComponentImageFilter<InputImageType, OutputImageType>;
ConnectedComponentFilterType::Pointer connectedComponentFilter = ConnectedComponentFilterType::New();
connectedComponentFilter->SetInput(otsuFilter->GetOutput());
connectedComponentFilter->Update();
// 重新标记组件,按大小排序
using RelabelFilterType = itk::RelabelComponentImageFilter<OutputImageType, OutputImageType>;
RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
relabelFilter->SetInput(connectedComponentFilter->GetOutput());
relabelFilter->SetMinimumObjectSize(50); // 移除小对象
relabelFilter->Update();
std::cout << "Number of objects: " << relabelFilter->GetNumberOfObjects() << std::endl;
// 使用形态学操作平滑结果
using StructuringElementType = itk::BinaryBallStructuringElement<OutputImageType::PixelType, 2>;
using ClosingFilterType = itk::BinaryMorphologicalClosingImageFilter<OutputImageType, OutputImageType, StructuringElementType>;
StructuringElementType structuringElement;
structuringElement.SetRadius(1);
structuringElement.CreateStructuringElement();
ClosingFilterType::Pointer closingFilter = ClosingFilterType::New();
closingFilter->SetInput(relabelFilter->GetOutput());
closingFilter->SetKernel(structuringElement);
closingFilter->Update();
// 保存结果
using WriterType = itk::ImageFileWriter<OutputImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(argv[2]);
writer->SetInput(closingFilter->GetOutput());
writer->Update();
return EXIT_SUCCESS;
}