BinaryThresholdImageFilter
这个滤波器通过在如图 6-1 所示的规则下。改变像素值来将一个图像转化成一个二值图像。用户需要指定两个门限:上门限和下门限,也就是两个亮度值:内部和外部。对于输入图像中的每个像素,使用上、下门限来和像素值进行比较。如果像素值在由[下门限,上门限]定义的范围内,则输出像素指定为 InsideValue
;否则输出像素指定为 OutsideValue
。门限处理通常作为一个分割途径的最后一个操作。
SetOutsideValue( )方式定义了指向那些亮度值在上、下门限范围之外的像素的亮度值。
SetInsideValue( )方式定义了指向那些亮度值在上、下门限范围之内的像素的亮度值。
SetLowerThreshold( )和 SetUpperThreshold( )方式定义了将转化为 InsideValue 的输入图
像亮度范围。注意上、下门限值的类型是输入图像像素类型,而 InsideValue
和
OutsideValue 的类型是输出图像的像素类型。
#include "itkImageFileReader.h"//读取头文件
#include "itkImageFileWriter.h"//写入头文件
#include "itkGDCMImageIO.h"//ImageIo头文件
#include "itkIntensityWindowingImageFilter.h"//调窗处理头文件
#include "itkBinaryThresholdImageFilter.h"//二值门限处理头文件
int main(int argc, char* argv[])
{
typedef signed short PixelType; // signed short 数据类型
typedef itk::Image< PixelType, 2 > ImageType;//image类型
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();//reader
WriterType::Pointer writer = WriterType::New();//writer
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();//gdcmImageIO
//读取文件
reader->SetFileName("D:/leg_dcm/IM70.dcm");
reader->SetImageIO(gdcmImageIO);
reader->Update();
//调窗处理
typedef itk::IntensityWindowingImageFilter<ImageType, ImageType > IntensityFilterType;
IntensityFilterType::Pointer intensityWindowing = IntensityFilterType::New();
intensityWindowing->SetWindowMinimum(124);//最小窗值
intensityWindowing->SetWindowMaximum(126);//最大窗值
intensityWindowing->SetOutputMinimum(0);//
intensityWindowing->SetOutputMaximum(255); //
intensityWindowing->SetInput(reader->GetOutput());//
intensityWindowing->Update();
//二值门限
typedef itk::BinaryThresholdImageFilter<ImageType, ImageType > ThresholdingFilterType;
ThresholdingFilterType::Pointer thresholder = ThresholdingFilterType::New();
thresholder->SetInput(intensityWindowing->GetOutput());
thresholder->SetUpperThreshold(255);//上下阈值参数设定
thresholder->SetLowerThreshold(250);
thresholder->SetOutsideValue(0);//输出值设定,背景为0,前景为255
thresholder->SetInsideValue(255);
thresholder->Update();
writer->SetInput(thresholder->GetOutput());
writer->SetFileName("D:/003.dcm");
writer->SetImageIO(gdcmImageIO);
writer->Update();
return EXIT_SUCCESS;
}