目录
1、itkBinaryMorphologicalOpeningImageFilter 开操作
该Filter的功能:用于去除图像内部或边界处的小(即小于结构元素)结构。
开操作的形态学定义为:先对图像进行腐蚀,再对图像进行膨胀
假定输入数据由二进制值(0或1)组成,结构元素中只有值 > 0的元素才可以影响目标像素。
常用的成员函数:
SetInput()
:设置输入的图像数据SetKernel()
:设置作用于图像的结构元素Set/GetBackgroundValue()
:设置/获取图像腐蚀部分的值, 默认为零Set/GetForegroundValue()
:设置/获取图像中的“前景”的值, 默认为InputPixelType的最大值Update()
:更新
2、itkBinaryMorphologicalClosingImageFilter 闭操作
该Filter的功能:去除图像内部或边界处的小孔(即小于结构元素)和管状结构。
开操作的形态学定义为:先对图像进行膨胀,再对图像进行腐蚀
假定结构元素由二进制值(0或1)组成,结构元素中只有值 > 0的元素才可以影响目标像素。
常用的成员函数:
SetInput()
:设置输入的图像数据SetKernel()
:设置作用于图像的结构元素Set/GetSafeBorder()
:设置/获取输入图像中的安全边框(安全边框:避免边框效果),并在闭操作完成后删除Set/GetForegroundValue()
:设置/获取图像中的“前景”的值。 默认为InputPixelType的最大值Update()
:更新
3、扩展:itkBinaryBallStructuringElement
表示具有二进制元素(0或1)的球结构(椭圆体)的邻域。
此类定义了一个 Neighborhood,其元素关闭或打开取决于它们是在半径与 Neighborhood半径匹配的椭球体外部还是内部,该类可用作形态图像过滤器的结构元素。
BinaryBallStructuringElement 具有N维半径。 每个维度的半径分别定义为邻域从中心像素向外延伸的像素数,例如,半径为 2x3 的 二维2D BinaryBallStructuringElement 对象的边长为 5x7。
BinaryBallStructuringElement对象始终具有明确的中心,因此它的边长始终是奇数,边长=半径*2+1;
在内部,最好使用FlatStructuringElement执行其所有计算,因为 FlatStructuringElement 更灵活。
SetRadius()
:设置结构体半径CreateStructuringElement()
:构建结构元素
4、扩展:itkSubtractImageFilter
两个图像的逐像素减法,即将image2中的每个像素从image1中对应的像素中减去:
Output = Input1 - Input2
由于昨晚二值形态学的图像像素值只有0或1,要恢复原图灰度可以用该类进行处理
5、代码示例:
#include "itkImage.h"
#include "itkBinaryMorphologicalOpeningImageFilter.h"
#include "itkBinaryMorphologicalClosingImageFilter.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkSubtractImageFilter.h"
const unsigned int Dimension= 3; //数据的Dimension
typedef signed short shortPixelType;
typedef itk::Image<shortPixelType, Dimension> ShortImageType;
bool morphologyFilter(ShortImageType* image, int radius)
{
typedef BinaryBallStructuringElement<short, Dimension> BSEType;
typedef SubtractImageFilter<ShortImageType> SubImgFilter;
//开操作
typedef BinaryMorphologicalOpeningImageFilter<ShortImageType, ShortImageType, SEType> BOpenFilterType;
//闭操作
typedef BinaryMorphologicalClosingImageFilter<ShortImageType, ShortImageType, SEType> BCloseFilteType;
BSEType ballStrEle;
//unsigned int radius = 3; //半径为3,直径为7
ballStrEle.SetRadius(radius);
ballStrEle.CreateStructuringElement();
//开操作代码
typename BOpenFilterType::Pointer openFilter = BOpenFilterType::New();
//auto openFilter = BOpenFilterType::New();
openFilter->SetInput(image);
openFilter->SetKernel(ballStrEle);
try
{
openFilter->Update();
}
catch(itk::ExceptionObject& ex)
{
//读取过程发生错误
std::cerr << "Error: " << ex << std::endl;
return false;
}
//闭操作代码
typename BCloseFilteType::Pointer closeFilter = BCloseFilteType::New();
//auto closeFilter = BOpenFilterType::New();
closeFilter->SetInput(image);
closeFilter->SetKernel(ballStrEle);
try
{
closeFilter->Update();
}
catch(itk::ExceptionObject& ex)
{
//读取过程发生错误
std::cerr << "Error: " << ex << std::endl;
return false;
}
SubImgFilter::Pointer diffImg = SubImgFilter::New();
//auto diffImg = SubImgFilter::New();
diffImg->SetInput1(image);
diffImg->SetInput2(openFilter->GetOutput());
//diffImg->SetInput2(closeFilter->GetOutput());
return true;
}