【ITK库学习】使用itk库对图像进行Binary与Gray形态学处理:膨胀、腐蚀

1、itkBinaryDilateImageFilter 二值膨胀

该类功能:图像中单个强度值的快速二值膨胀。

该类是对图像前景进行Binary膨胀形态操作,只有强度值“SetForegroundValue()”(别名为SetDilateValue())指定的值被视为前景,其他强度值被视为背景。

通过选择“ForegroundValue”(别名“DilateValue”),可以将灰度图像处理为二值图像,与扩张值匹配的像素值被视为“前景”,所有其他像素被视为“背景”。
这在处理分段图像时很有用,其中分段#1中的所有像素都具有值1,分段#2中的像素具有值2等,可以处理特定的“分段号”。 ForegroundValue 默认为 PixelType 的最大值。

假定结构元素由二进制值(0或1)组成,结构元素中只有值 > 0的元素才可以影响目标像素。

常用的成员函数

  • SetInput():设置输入的图像数据
  • SetKernel():设置作用于图像的结构元素
  • Set/GetDilateValue():设置/获取图像中被视为“前景”的值, 默认为 PixelType 的最大值,父类中 Set/GetForegroundValue()的函数别名

2、itkBinaryErodeImageFilter 二值腐蚀

该类功能:图像中单个强度值的快速二值腐蚀。

该类是对图像前景进行二值腐蚀形态操作,只有强度值“SetForegroundValue()”(别名为SetErodeValue())指定的值被视为前景,其他强度值被视为背景。

通过选择“ForegroundValue”(别名“ErodeValue”),可以将灰度图像处理为二值图像,与侵蚀值匹配的像素值被视为“前景”,所有其他像素被视为“背景”。
这在处理分段图像时很有用,其中分段#1中的所有像素都具有值1并且分段#2中的像素具有值2等。可以处理特定的“分段号”。

假定结构元素由二进制值(0或1)组成,结构元素中只有值 > 0的元素才可以影响目标像素。

常用的成员函数

  • SetInput():设置输入的图像数据
  • SetKernel():设置作用于图像的结构元素
  • Set/GetErodeValue():设置/获取图像中被视为“前景”的值,默认为PixelType的最大值,父类中 Set/GetForegroundValue()的函数别名

3、itkGrayscaleDilateImageFilter 灰度膨胀

该类功能:图像的灰度膨胀。

使用灰度形态学扩张图像,膨胀取结构元素标识的所有像素中的最大值。

假定结构元素由二进制值(0或1)组成,结构元素中只有值 > 0的元素才可以影响目标像素。

常用的成员函数

  • SetInput():设置输入的图像数据
  • SetKernel():设置作用于图像的结构元素
  • Set/GetBoundary():设置/获取边界值
  • Set/GetAlgorithm():设置/获取后端过滤器类
  • Modified():需要将其内部过滤器设置为已修改

4、itkGrayscaleErodeImageFilter 灰度腐蚀

该类功能:图像的灰度腐蚀。

该类使用灰度形态学腐蚀图像,腐蚀取结构元素识别的所有像素中的最大值。

假定结构元素由二进制值(0或1)组成,结构元素中只有值 > 0的元素才可以影响目标像素。

常用的成员函数

  • SetInput():设置输入的图像数据
  • SetKernel():设置作用于图像的结构元素
  • Set/GetBoundary():设置/获取边界值
  • Set/GetAlgorithm():设置/获取后端过滤器类
  • Modified():需要将其内部过滤器设置为已修改

5、Binary与Gray函数的区别

Binary类是用于二值图像的腐蚀操作,而Gray类是用于灰度图像的腐蚀操作。因此,它们主要区别在于它们适用的图像类型和像素更新的逻辑。
其中:
itkGrayscaleErodeImageFilter是将图像中的每个像素点与其周围的像素进行比较,并将像素值更新为邻域中最小的像素值,这有助于消除图像中的小尺寸噪声或细小区域

itkBinaryErodeImageFilter也是比较像素与其周围像素的值,但只有在邻域中的像素值全为1时,像素才会更新为1,否则更新为0,这有助于减小图像中的物体大小或填充物体中的小孔洞

灰度腐蚀滤波器适用于灰度图像,并根据最小像素值更新像素,而二值腐蚀滤波器适用于二值图像,并根据邻域中的像素值更新像素。

itkBinaryDilateImageFilter是对图像中的前景像素进行扩张,将其周围的背景像素标记为前景像素。

itkGrayscaleDilateImageFilter是对图像中的像素进行扩张,将其周围的像素的最大值作为新的像素值。

6、扩展:itkBinaryThresholdImageFilter 图像二值化

该类通过阈值化对输入图像进行二值化。

此过滤器生成一个输出图像,其像素是两个值(OutsideValue或InsideValue )之一,具体取决于相应的输入图像像素是否位于两个阈值(LowerThreshold和UpperThreshold)之间,等于任一阈值的值被视为在阈值之间。

该过滤器根据输入图像类型和输出图像类型进行模板化,过滤器期望两个图像具有相同的维度数。

LowerThreshold 和 UpperThreshold 的默认值为: LowerThreshold = NumericTraits::NonpositiveMin(); UpperThreshold = NumericTraits::max();
因此,通常仅需要设置其中之一,具体取决于用户是否希望阈值高于或低于期望阈值。
常用的成员函数

  • SetInput():设置输入的图像数据
  • Set/GetOutsideValue():设置/获取“外部”像素值
  • Set/GetInsideValue():设置/获取“内部”像素值
  • Set/GetLowerThreshold():设置/获取最低阈值,默认值:NumericTraits::NonpositiveMin()
  • Set/GetUpperThreshold():设置/获取最高阈值,默认值:NumericTraits::max()

7、代码示例:


#include "itkImage.h"
#include "itkBinaryDilateImageFilter.h"
#include "itkBinaryErodeImageFilter.h"
#include "itkGrayscaleDilateImageFilter.h"
#include "itkGrayscaleErodeImageFilter.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkSubtractImageFilter.h"
#include "itkBinaryThresholdImageFilter.h";

using namespace itk;

const unsigned int  Dimension= 3;       //数据的Dimension
typedef signed short shortPixelType;   
typedef itk::Image<shortPixelType, Dimension> ShortImageType;

//二值膨胀腐蚀形态学操作	
bool binaryMorphologyFilter(ShortImageType* image, int radius)
{
    //如果输入图像不为二值图像,先进行二值化处理---------------------
    const short lowerThr = 0;      //设置二值化的上下阈值
    const short upperThr = 1000;
    short backGround = 0;    //设置前景背景值
    short foreGround = 255;
    typedef BinaryThresholdImageFilter<ShortImageType,ShortImageType> BThresholdFilterType;
    typename BThresholdFilterType::Pointer thresholder= BThresholdFilterType::New();
    thresholder->SetInput(image);
    thresholder->SetOutsideValue(backGround);
    thresholder->SetInsideValue(foreGround);
    thresholder->SetLowerThreshold(lowerThr);
    thresholder->SetUpperThreshold(upperThr);
    thresholder->Update();
    //------------------------------------------------------------
    
    typedef BinaryBallStructuringElement<short, Dimension> BSEType;
	
    BSEType ballStrEle;
    //unsigned int radius = 3;      //半径为3,直径为7
    ballStrEle.SetRadius(radius);
    ballStrEle.CreateStructuringElement();
    
    //二值膨胀
    typedef BinaryDilateImageFilter<ShortImageType, ShortImageType, BSEType> BDilateFilterType;
    //typename BDilateFilterType::Pointer binaryDilate = BDilateFilterType::::New();
    auto binaryDilate = BDilateFilterType::New();
    binaryDilate->SetInput(image);
    //binaryDilate->SetInput(hresholder);
    binaryDilate->SetKernel(ballStrEle);
    try
	{
	    binaryDilate->Update();
	}
	catch(itk::ExceptionObject& ex)
	{
	    //读取过程发生错误
        std::cerr << "Error: " << ex << std::endl;
        return false;
	}
	
    //二值腐蚀
    typedef BinaryErodeImageFilter<ShortImageType, ShortImageType, BSEType> BErodeFilteType;
    //typename BErodeFilteType::Pointer binaryErode = BErodeFilteType::New();
    auto binaryErode = BErodeFilteType::New(); 
    binaryErode->SetInput(image);
    //binaryErode->SetInput(hresholder);
    binaryErode->SetKernel(ballStrEle);
    try
	{
	    binaryErode->Update();
	}
	catch(itk::ExceptionObject& ex)
	{
	    //读取过程发生错误
        std::cerr << "Error: " << ex << std::endl;
        return false;
	}  
    return true;
}

//灰度膨胀腐蚀形态学操作	
bool grayMorphologyFilter(ShortImageType* image, int radius)
{
	typedef BinaryBallStructuringElement<short, Dimension> BSEType;
	
    BSEType ballStrEle;
    //unsigned int radius = 3;      //半径为3,直径为7
    ballStrEle.SetRadius(radius);
    ballStrEle.CreateStructuringElement();
	
    //灰度膨胀
    typedef GrayscaleDilateImageFilter<ShortImageType, ShortImageType, BSEType> GDilateFilterType;
    auto grayscaleDilate = GDilateFilterType::New();
    grayscaleDilate->SetInput(image);
    grayscaleDilate->SetKernel(ballStrEle);
    try
	{
	    grayscaleDilate->Update();
	}
	catch(itk::ExceptionObject& ex)
	{
	    //读取过程发生错误
        std::cerr << "Error: " << ex << std::endl;
        return false;
	}
	
    //灰度腐蚀
    typedef GrayscaleErodeImageFilter<ShortImageType, ShortImageType, BSEType> GErodeFilteType;
    auto grayscaleErode = GErodeFilteType::New();
    grayscaleErode->SetInput(image);
    grayscaleErode->SetKernel(ballStrEle);
    try
	{
	    grayscaleErode->Update();
	}
	catch(itk::ExceptionObject& ex)
	{
	    //读取过程发生错误
        std::cerr << "Error: " << ex << std::endl;
        return false;
	}
	return true;
}



  • 31
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值