【ITK库学习】使用itk库进行图像分割(一):区域生长--阈值ThresholdImageFilter

1、itkThresholdImageFilter

该类的主要功能是通过设置低阈值、高阈值或介于高低阈值之间,则将图像值输出为用户指定的值。

如果图像值低于、高于或介于设置的阈值之间,该类就将图像值设置为用户指定的“外部”值(默认情况下为“黑色”)。

常用的成员函数

  • Set/GetLower():设置/获取下限阈值
  • Set/GetUpper():设置/获取上限阈值
  • Set/GetOutsideValue():设置/获取“外部”像素值
  • ThresholdAbove():将大于或等于该阈值的值设置为OutsideValue
  • ThresholdBelow():将小于或等于该阈值的值设置为OutsideValue
  • ThresholdOutside():将超出上下限阈值范围的值设置为 OutsideValue

该类并不对像素进行二值化处理,输出图像中的像素值可以是浮点型或整型。

示例代码:

#include "itkImage.h"
#include "itkThresholdImageFilter.h";

using namespace itk;

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

//图像进行阈值分割处理
bool thresholdImage(ShortImageType* image, ShortImageType* outImage)
{
	const short lowerThr = 0;      //设置下阈值
	const short upperThr = 1000;   //设置上阈值
	short outsideValue = 0; 
    typedef ThresholdImageFilter<ShortImageType> thresholdFilterType;
	typename thresholdFilterType::Pointer thresholder = thresholdFilterType::New();
	thresholder->SetInput(image);
	thresholder->SetOutsideValue(outsideValue);
	设置上下阈值
	//thresholder->SetLower(lowerThr);
	//thresholder->SetUpper(upperThr);
	
	//<下阈值的值均设为outsideValue
	thresholder->ThresholdBelow(lowerThr);
	try
	{
		thresholder->Update();
	}
	catch (itk::ExceptionObject& ex)
	{
		//读取过程发生错误
		std::cerr << "Error: " << ex << std::endl;
		return false;
	}
	//>上阈值的值均设为outsideValue
	thresholder->ThresholdAbove(upperThr);
	try
	{
		thresholder->Update();
	}
	catch (itk::ExceptionObject& ex)
	{
		//读取过程发生错误
		std::cerr << "Error: " << ex << std::endl;
		return false;
	}
    //介于阈值之外的值均设为outsideValue
	thresholder->ThresholdOutside(lowerThr, upperThr);
	try
	{
		thresholder->Update();
	}
	catch (itk::ExceptionObject& ex)
	{
		//读取过程发生错误
		std::cerr << "Error: " << ex << std::endl;
		return false;
	}

	outImage = thresholder->GetOutput();
	return true;
}

2、itkBinaryThresholdImageFilter

该类的主要功能是通过阈值处理,将输入图像进行二值化。

输出的图像像素只有两个值:OutsideValue或者InsideValue,具体取决于相应的输入图像像素是否位于高低阈值LowerThreshold和UpperThreshold之间,其中当像素值等于任一阈值时,被认为是在阈值之间。

根据输入图像类型和输出图像类型进行模板化:TInputImage输入图像类型,TOutputImage输出图像类型

itk::BinaryThresholdImageFilter<TInputImage, TOutputImage>

LowerThreshold 和 UpperThreshold 的默认值为:

LowerThreshold = NumericTraits<TInput>::NonpositiveMin(); 
UpperThreshold = NumericTraits<TInput>::max(); 

注意:LowerThreshold不得大于UpperThreshold ,否则会引发异常。

因此,通常仅需要设置其中之一,具体取决于用户是否希望阈值高于或低于期望阈值。

常用的成员函数

  • Set/GetInsideValue():设置/获取“内部”像素值
  • Set/GetOutsideValue():设置/获取“外部”像素值
  • Set/GetLowerThreshold():设置/获取低阈值
  • Set/GetUpperThreshold():设置/获取高阈值

与itkThresholdImageFilter相比较,itkBinaryThresholdImageFilter更适用于将图像根据两个阈值进行二值化处理,而itkThresholdImageFilter适用于将图像中符合条件的像素值映射为特定的数值。

示例代码:

#include "itkImage.h"
#include "itkBinaryThresholdImageFilter.h";

using namespace itk;

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

//图像进行二值化阈值分割处理
bool binaryThresholdImage(ShortImageType* image, ShortImageType* outImage)
{
	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);
	try
	{
		thresholder->Update();
	}
	catch (itk::ExceptionObject& ex)
	{
		//读取过程发生错误
		std::cerr << "Error: " << ex << std::endl;
		return false;
	}

	outImage = thresholder->GetOutput();
	return true;
}

3、itkOtsuThresholdImageFilter

该类的功能是使用最大类间方差法Otsu阈值设置图像阈值。

最大类间方差法:是由日本学者大津(Nobuyuki Otsu)于1979年提出的,是一种自适合于双峰情况的自动求取阈值的方法,又叫大津法,简称Otsu。是一种基于全局的二值化算法。

它是按图像的灰度特性,将图像分成背景和目标两部分。背景和目标之间的类间方差越大,说明构成图像的2部分的差别越大,当部分目标错分为背景或部分背景错分为目标都会导致2部分差别变小。因此,使类间方差最大的分割意味着错分概率最小。

常用的成员函数

  • Set/GetInsideValue():设置/获取“内部”像素值
  • Set/GetOutsideValue():设置/获取“外部”像素值
  • Set/GetReturnBinMidpoint():设置/获取阈值是bin的中点还是最大值? 默认值是 bin 最大值
  • ReturnBinMidpointOn():设置/获取阈值是bin的中点还是最大值? 默认值是 bin 最大值
  • VerifyPreconditions():验证先决条件,验证过程对象是否已正确配置、所有必需的输入是否已设置以及所需的参数是否已正确设置, 如果无效,将抛出异常,在将 UpdateOutputInformation() 传播到输入之前调用此方法,ProcessObject 的实现验证 m_NumberOfRequiredInputs 是否已设置且不为空

示例代码:

#include "itkImage.h"
#include "itkOtsuThresholdImageFilter.h"

using namespace itk;

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

bool OtsuThresholdImage(ShortImageType* image, ShortImageType* outImage)
{
	short outsideValue = 0;    //设置前景背景值
	short insideValue = 255;
	
    typedef OtsuThresholdImageFilter<ShortImageType, ShortImageType> OtsuThresholdFilterType;
	typename OtsuThresholdFilterType::Pointer thresholder = OtsuThresholdFilterType::New();
	thresholder->SetInput(image);
	thresholder->SetOutsideValue(outsideValue);
	thresholder->SetInsideValue(insideValue);
	try
	{
		thresholder->Update();
	}
	catch (itk::ExceptionObject& ex)
	{
		//读取过程发生错误
		std::cerr << "Error: " << ex << std::endl;
		return false;
	}

	outImage = thresholder->GetOutput();
	return true;
}

4、itkConnectedThresholdImageFilter

该类的功能是标记连接到种子并位于值范围内的像素。

该类使用ReplaceValue标记连接到初始种子且位于阈值下限和上限范围内的像素。

与itkThresholdImageFilter等前几个类相比,它们虽然都是根据不同像素的灰度值或像素特征将图像分割成不同的区域,但是此类不同的是基于连接像素的原理,通过选择与种子像素相连的像素来进行分割,分割的结果连通区域,可以灵活地选择不同的种子点连接条件,得到不同的连通区域

itkConnectedThresholdImageFilter适用于分割具有明显边界的目标,可以得到分割结果中目标区域的边界比较平滑。而itkThresholdImageFilter/itkBinaryThresholdImageFilter适用于分割目标灰度值较高或较低的区域,可以得到目标区域与背景的清晰分割。

常用的成员函数

  • AddSeed():增加种子点
  • ClearSeeds():清除种子列表
  • SetSeed():设置种子点
  • GetSeeds():获取种子容器
  • Set/GetLower():设置/获取下限阈值
  • Set/GetUpper():设置/获取上限阈值
  • Set/GetUpperInput():设置/获取连接到管道的上阈值输入
  • Set/GetLowerInput():设置/获取连接到管道的下阈值输入
  • SetConnectivity():要使用的连接类型(完全连接或 4(2D)、6(3D)、2*N(ND) 连接)
  • Set/GetReplaceValue():设置/获取值以替换阈值像素, 介于Lower和Upper(含)内的像素将被替换为该值, 默认值为 1

示例代码:

#include "itkImage.h"
#include "itkConnectedThresholdImageFilter.h"

using namespace itk;

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

bool connectedThresholdImage(ShortImageType* image, ShortImageType* outImage)
{
	const short lowerThr = 0;      //设置二值化的上下阈值
	const short upperThr = 1000;
	const short replaceValue = 255;

	ShortImageType::IndexType seed;
	seed[0] = 100;     //该值必须在图像的三维大小范围内
	seed[1] = 100;
	seed[2] = 25;
	
    typedef ConnectedThresholdImageFilter<ShortImageType, ShortImageType> ConnectedThresholdFilterType;
	typename ConnectedThresholdFilterType::Pointer thresholder = ConnectedThresholdFilterType::New();
	thresholder->SetInput(image);
	thresholder->SetLower(lowerThr);
	thresholder->SetUpper(upperThr);
	thresholder->SetReplaceValue(replaceValue);
	thresholder->SetSeed(seed);
	try
	{
		thresholder->Update();
	}
	catch (itk::ExceptionObject& ex)
	{
		//读取过程发生错误
		std::cerr << "Error: " << ex << std::endl;
		return false;
	}

	outImage = thresholder->GetOutput();
	return true;
}
  • 29
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ITK-SNAP是一种功能强大的开源软件,可用于查看和分析医学图像,包括多标签分割结果。多标签分割是医学图像处理中的一项重要任务,它能够将图像中的不同组织或区域通过不同的标签进行区分和分割。 使用ITK-SNAP查看多标签分割结果非常简单。首先,将多标签分割结果保存为标准的NIfTI或DICOM格式的图像文件。然后,打开ITK-SNAP软件,并导入所保存的分割结果图像文件。 一旦图像被加载到ITK-SNAP中,你可以通过使用工具栏上的不同工具和选项来查看和分析分割结果。例如,你可以使用放大/缩小功能来调整图像的显示比例,以便更好地观察分割结果。你还可以使用移动工具来在分割结果之间进行切换或比较不同的分割结果。 此外,ITK-SNAP还提供了一些强大的可视化工具,例如绘制线条或3D表面模型来显示分割结果。你可以使用这些工具来绘制轮廓线条,标记感兴趣的区域,或者生成3D模型以更好地展示分割结果。 ITK-SNAP还支持对分割结果进行编辑和修改的功能。你可以使用编辑工具来增加或删除特定的标签,调整分割结果的边界,或者进行区域合并和分割。这些编辑功能可以帮助你改进分割结果的准确性和精度。 总的来说,使用ITK-SNAP来查看多标签分割结果非常方便和直观。它提供了丰富的工具和功能,使医学图像分析变得更加容易和高效。无论是医学专业人士还是研究人员,都可以通过使用ITK-SNAP来更好地理解和研究分割结果。 ### 回答2: ITK-SNAP是一种用于医学图像处理和分析的开源软件。它可以用于查看多标签分割结果。 多标签分割是一种图像处理技术,用于将图像中的每个像素分配给不同的类别或标签。通过将不同的结构或组织分配给不同的标签,我们可以更好地理解和分析医学图像,例如CT扫描、MRI或PET图像。 当我们使用ITK-SNAP查看多标签分割结果时,我们可以打开标签图像并显示每个标签的不同类别。每个标签都用不同的颜色来表示,以便我们可以更清楚地区分不同的结构。 使用ITK-SNAP,我们可以进行以下操作来查看多标签分割结果: 1. 打开ITK-SNAP软件,导入包含多标签分割结果的图像文件。 2. 在软件界面中,选择"打开图像"选项,并选择多标签分割的图像文件进行打开。 3. 一旦图像被加载,我们可以通过选择"显示图像"选项来显示图像。 4. 在显示的图像上,我们可以使用不同的工具来查看不同的标签。例如,我们可以使用"显示标签轮廓"来仅显示标签的边界轮廓,或者我们可以使用"显示标签区域"来仅显示特定标签的区域。 5. 此外,我们还可以使用颜色映射工具来将不同的标签分配给不同的颜色。这样,我们可以更好地区分不同的结构。 6. 在ITK-SNAP中,我们还可以进行一些进一步的图像处理和分析操作,例如量化每个标签的体积或计算不同标签之间的距离。 总之,使用ITK-SNAP可以方便地查看和分析多标签分割结果,提供更准确、详细的医学图像处理和分析。 ### 回答3: ITK-SNAP是一种开源的医学图像处理软件,可以用于三维医学图像的分割和可视化。它支持多标签分割结果的查看和分析。 在ITK-SNAP中,多标签分割结果可以通过以下步骤进行查看: 1. 打开ITK-SNAP软件并加载需要分割的医学图像文件。可以使用“文件”菜单中的“打开图像”选项或拖放图像文件到软件界面。 2. 在左侧的“Segmentation”面板中,选择“Multi-label”选项。这将允许我们进行多标签分割。 3. 单击“Add New Segment”按钮以添加一个新的标签。您可以为每个标签选择不同的颜色,并为其命名。 4. 使用鼠标工具(例如刷子、剪刀等)在图像上画出不同的区域,并将其分配给不同的标签。您可以根据需要逐渐添加和修改分割区域。 5. 在“Viewer Control”面板中,选择“Pan”或“Scroll”工具来导航和放大/缩小图像视图。您可以使用“Window/Level”工具调整图像的窗口和水平。 6. 您还可以使用“Volume Render”功能查看三维分割结果。选择“Volume Render”面板,调整光线和阻尼设置,以获得更好的可视化效果。 除了查看多标签分割结果,ITK-SNAP还提供了其他功能,如分割评估、图像配准等。利用这些功能,我们可以定量地评估分割结果的准确性,并进行后续的分析和研究。 总之,ITK-SNAP是一款功能强大的医学图像处理软件,可用于多标签分割结果的可视化和分析。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值