ITK 图像分割(一):阈值ThresholdImageFilter

效果:

Video:

区域增加分割

1、itkThresholdImageFilter

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

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

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

常用的成员函数:

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


 

 Example:

#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 = 200;      //设置下阈值
	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

2、itkBinaryThresholdImageFilter

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

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

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

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

常用的成员函数

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

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

Example:

#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 是否已设置且不为空
 

Code

#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
 

Code:

#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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/388142.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

山西电力市场日前价格预测【2024-02-10】

日前价格预测 预测说明&#xff1a; 如上图所示&#xff0c;预测明日&#xff08;2024-02-10&#xff09;山西电力市场全天平均日前电价为126.73元/MWh。其中&#xff0c;最高日前电价为302.95元/MWh&#xff0c;预计出现在08:15。最低日前电价为0.00元/MWh&#xff0c;预计出…

Stable Diffusion之最全详解图解

Stable Diffusion之最全详解图解 1. Stable Diffusion介绍1.1 研究背景1.2 学术名词 2.Stable Diffusion原理解析2.1 技术架构2.2 原理介绍扩散过程 3.1 Diffusion前向过程3.2 Diffusion逆向&#xff08;推断&#xff09;过程 1. Stable Diffusion介绍 Stable Diffusion是2022…

分布式文件系统 SpringBoot+FastDFS+Vue.js【一】

分布式文件系统 SpringBootFastDFSVue.js【一】 一、分布式文件系统1.1.文件系统1.2.什么是分布式文件系统1.3.分布式文件系统的出现1.3.主流的分布式文件系统1.4.分布式文件服务提供商1.4.1.阿里OSS1.4.2.七牛云存储1.4.3.百度云存储 二、fastDFS2.1.fastDSF介绍2.2.为什么要使…

跟着pink老师前端入门教程-day26

一、计算机编程基础 &#xff08;一&#xff09;编程语言 1、编程 编程&#xff1a;就是让计算机为解决某个问题而使用某种程序设计语言编写程序代码&#xff0c;并最终得到结果的过程。 计算机程序&#xff1a;就是计算机所执行的一系列的指令集合&#xff0c;而程序全部…

MySQL 基础知识(三)之数据库操作

目录 1 显示当前时间、用户名、数据库版本 2 查看已有数据库 3 创建数据库 4 使用数据库 5 查看当前使用的数据库 6 查看当前数据库信息 7 查看数据库编码 8 修改数据库信息 9 删除数据库 10 查看最大连接数 11 查看数据库当前连接数&#xff0c;并发数 12 查看数据…

2022年12月电子学会青少年软件编程 中小学生Python编程等级考试二级真题解析(判断题)

2022年12月Python编程等级考试二级真题解析 判断题&#xff08;共10题&#xff0c;每题2分&#xff0c;共20分&#xff09; 26、字典的元素可以通过键来访问&#xff0c;也可以通过索引(下标)来访问 答案&#xff1a;错 考点分析&#xff1a;考查字典相关知识&#xff0c;字…

c语言操作符(上)

目录 ​编辑 原码、反码、补码 1、正数 2、负数 3、二进制计算1-1 移位操作符 1、<<左移操作符 2、>>右移操作符 位操作符&、|、^、~ 1、&按位与 2、|按位或 3、^按位异或 特点 4、~按位取反 原码、反码、补码 1、正数 原码 反码 补码相同…

属性/成员变量

一、属性/成员变量 二、注意事项 三、创建对象

预算紧缩下创新创业者应采取哪3个策略来保持创新?

在今天越来越饱和的消费市场中&#xff0c;品牌零售通过复杂、过度的的促销、折扣、优惠券和忠诚度奖励来吸引消费者&#xff0c;但这种做法可能削弱消费者的忠诚度&#xff0c;损害品牌声誉&#xff0c;并抑制新的收入机会。相反&#xff0c;零售商应采取更简化、以客户为中心…

thinkphp6入门(20)-- 如何上传图片、文件

1. 配置文件 设置上传的路径 对应文件夹 2. 前端 <div class"card-body"><h1 class"card-title">用户头像</h1><img src"../../../uploads/{$user.avatar_photo_path}" alt"avatar" height"100"/&g…

Linux makefile 大型多文件的处理

最简单的例子是 main.cpp test.cpp test.h 首先将这三个写好 然后的话 test.cpp 上面输出 helloworld 首先我们在同一个目录下创建一个makefile 文件 然后用vim 编辑它 如下图&#xff08;使用的c&#xff09; mybin 是我们的可执行程序 gcc是编译的命令 gcc 前面必…

贪心算法练习day1

练习1--翻硬币 1&#xff09;题目及要求 2&#xff09;解题思路 输入的是字符串&#xff0c;要想将两组字符串进行一一对比&#xff0c;需要将字符串转换成字符数组&#xff0c;再使用for循环依次遍历字符数组&#xff0c;进行比对。 输入两行字符串&#xff0c;转换成两个字…

云备份项目:在云端保护您的数据【一、初识】

桃李不言&#xff0c;下自成蹊 文章目录 项目简介项目设计方案服务端功能划分客户端功能划分 项目环境搭建环境准备第三方库JsonCppbundle数据压缩库httplib 总结 ☘️项目源代码&#xff1a;云备份 ☘️云备份专栏&#xff1a;云备份 项目简介 云备份系统是一个自动化的备份解…

双非本科准备秋招(19.1)—— Synchronized优化

轻量级锁 流程 一个对象虽然有多线程加锁&#xff0c;但是加锁时间是错开的&#xff0c;那么可以用轻量级锁优化。 语法还是synchronized&#xff0c;只是对使用者是透明的。 static final Object obj new Object(); public static void method1() {synchronized( obj ) {//…

从零开始学howtoheap:解题西湖论剑Storm_note

how2heap是由shellphish团队制作的堆利用教程&#xff0c;介绍了多种堆利用技术&#xff0c;后续系列实验我们就通过这个教程来学习。环境可参见从零开始配置pwn环境&#xff1a;从零开始配置pwn环境&#xff1a;从零开始配置pwn环境&#xff1a;优化pwn虚拟机配置支持libc等指…

DS:二叉树的顺序结构及堆的实现

创作不易&#xff0c;兄弟们给个三连&#xff01;&#xff01; 一、二叉树的顺序存储 顺序结构指的是利用数组来存储&#xff0c;一般只适用于表示完全二叉树&#xff0c;原因如上图&#xff0c;存储不完全二叉树会造成空间上的浪费&#xff0c;有的人又会问&#xff0c;为什么…

数据接收程序

#include<reg51.h> //包含单片机寄存器的头文件 sbit pPSW^0; /***************************************************** 函数功能&#xff1a;接收一个字节数据 ***************************************************/ unsigned char Receive(void) { unsigned…

【教3妹学编程-算法题】子集中元素的最大数量

2哥 : 3妹&#xff0c;今年过年收到压岁钱了没呢。 3妹&#xff1a;切&#xff0c;我都多大了啊&#xff0c;肯定没收了啊 2哥 : 俺也一样&#xff0c;不仅没收到&#xff0c;小侄子小外甥都得给&#xff0c;还倒贴好几千 3妹&#xff1a;哈哈哈哈&#xff0c;2叔叔&#xff0c…

Activation of network connection failed(ubuntu连不上网)

ubuntu连不上网&#xff0c;看了好几个方法找到个有用的记录一下 1. 还原默认设置 2. 更改适配器&#xff1a;加上vmware bridge protocol

新机Word/PowerPoint新建空白文档后闪退问题

首先可以尝试一下常规的修复&#xff1a; 设置-应用-安装的应用-搜索office-点击Micros Office Home and Student...右侧三个点-选择修改-点击是-快速修复-修复 再不行就按上面的选择联机修复&#xff0c;这个会卸载现有Office然后自动帮你重新下载 我做了以上两个都没有解决问…
最新文章