GrayscaleFillholeImageFilter
填充孔洞,不对边缘造成影响(无平滑边缘效果)
itk提供了两个的补洞类:itkGrayscaleFillholeImageFilter(针对灰度图像), itkBinaryFillholeImageFilter(针对二值图像)
//孔洞填充(灰度图像)
typedef itk::GrayscaleFillholeImageFilter<ImageType, ImageType> FillHoleType;
FillHoleType::Pointer fillHoleFilter = FillHoleType::New();
fillHoleFilter->SetInput(thresholder1->GetOutput());
fillHoleFilter->Update();
![](https://i-blog.csdnimg.cn/blog_migrate/e28eb21c1bf5104bd1816a1eef5fbf9a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/dfd4629454ccb4cf3ef984fcf26c64de.png)
//孔洞填充
//008
//reader-intensityWindowing-thresholder-(StructuringElement)-Dilate-Erode-writer
#include "itkImageFileReader.h"//读取头文件
#include "itkImageFileWriter.h"//写入头文件
#include "itkGDCMImageIO.h"//ImageIo头文件
#include "itkIntensityWindowingImageFilter.h"//调窗处理头文件
#include "itkBinaryThresholdImageFilter.h"//二值门限处理头文件
#include "itkBinaryBallStructuringElement.h"//基本球形
#include "itkBinaryErodeImageFilter.h"
#include "itkBinaryDilateImageFilter.h"
#include "itkGrayscaleFillholeImageFilter.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();
//用于二值图像的构造成员
typedef itk::BinaryBallStructuringElement< PixelType, 2 > StructuringElementType;
StructuringElementType structuringElement;
structuringElement.SetRadius(1); //领域大小为3*3
structuringElement.CreateStructuringElement();
//开运算:先膨胀再腐蚀
typedef itk::BinaryErodeImageFilter <ImageType, ImageType, StructuringElementType > ErodeFilterType;// 腐蚀,需要用输入、输出图像类型和构造成员实例化
typedef itk::BinaryDilateImageFilter <ImageType, ImageType, StructuringElementType > DilateFilterType;// 膨胀,需要用输入、输出图像类型和构造成员实例化
DilateFilterType::Pointer binaryDilate = DilateFilterType::New();
binaryDilate->SetInput(thresholder->GetOutput());
binaryDilate->SetKernel(structuringElement);
binaryDilate->SetDilateValue(255);
binaryDilate->Update();
ErodeFilterType::Pointer binaryErode = ErodeFilterType::New();
binaryErode->SetInput(binaryDilate->GetOutput());
binaryErode->SetKernel(structuringElement);
binaryErode->SetErodeValue(255);
binaryErode->Update();
//闭运算:先腐蚀再膨胀
ErodeFilterType::Pointer binaryErode1 = ErodeFilterType::New();
binaryErode1->SetInput(binaryErode->GetOutput());
binaryErode1->SetKernel(structuringElement);
binaryErode1->SetErodeValue(255);
binaryErode1->Update();
DilateFilterType::Pointer binaryDilate1 = DilateFilterType::New();
binaryDilate1->SetInput(binaryErode1->GetOutput());
binaryDilate1->SetKernel(structuringElement);
binaryDilate1->SetDilateValue(255);
binaryDilate1->Update();
//二值门限处理
typedef itk::BinaryThresholdImageFilter<ImageType, ImageType > ThresholdingFilterType;
ThresholdingFilterType::Pointer thresholder1 = ThresholdingFilterType::New();
thresholder1->SetInput(binaryDilate1->GetOutput());
thresholder1->SetUpperThreshold(255);//上下阈值参数设定
thresholder1->SetLowerThreshold(250);
thresholder1->SetOutsideValue(0);//输出值设定,背景为0,前景为255
thresholder1->SetInsideValue(255);
thresholder1->Update();
//孔洞填充(灰度图像)
typedef itk::GrayscaleFillholeImageFilter<ImageType, ImageType> FillHoleType;
FillHoleType::Pointer fillHoleFilter = FillHoleType::New();
fillHoleFilter->SetInput(thresholder1->GetOutput());
fillHoleFilter->Update();
writer->SetInput(fillHoleFilter->GetOutput());
writer->SetFileName("D:/008.dcm");
writer->SetImageIO(gdcmImageIO);
writer->Update();
return EXIT_SUCCESS;
}