vtk c++ 图像分割_ITK基础(一) — 二值化分割

本文介绍使用ITK实现图像二值化分割的方法。通过设置像素阈值,将图像分割成两部分,实现目标区域的提取。代码示例展示了如何应用itk::BinaryThresholdImageFilter过滤器,并设置关键参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ITK  全称为 Insight Toolkit ,是一款开源、跨平台、用于图像分析工具包,开发遵循极限编程,主流使用语言为 C++,但目前开发团队已经提供了面向 Python 的接口。

ITK 内部封装了许多优秀算法。ITK 可用于图像处理、配准、分割等领域,处理图像维度面向二维、三维或者更高维度

原理讲解

本文为 ITK 系列教程的第一篇文章,主要介绍该工具包中二值化分割功能的实现;图像分割的目的通过改变图像像素值,来提取我们想要的区域,一般是图像处理的大前提;

ITK 中的二值化分割主要用到 itk::BinaryThresholdImageFilter 过滤器,其分割原理图如下:

e24d7941c5fa1add9da43ba3c8d58176.png

二值化分割是分割方法中最基础的,通过定义 Lower 和 Upper 两个像素临界点

1ab4f0b27a6fcfa2e59ec3feac813db8.png
只要图像像素值在这者之间,则该像素值将改变为 Insidevalue;否则将改为 Outsidevalue;最终图像的像素值只有两种:Insidevalue 或者是 Outsidevalue;

注:上面的 Insidevalue、Outsidevalue、Lowervalue、Uppervalue 四个参数是用户自己设定的。

代码实现

上文已经提到了,二值化分割主要用到的头文件为 itkBinaryThresholdImageFilter ,该过滤器主要通过设置四个参数来完成分割效果。

下面的代码部分就是关于二值分割的功能实现,代码中,依次进行图像读取、参数设定、二值化处理、图像写出等一系列步骤

#include
#include
#include
#include
#include
#include

using namespace std;

int Binary_Threshold(){

    itk::PNGImageIOFactory::RegisterOneFactory();

    string input_name = "D:/ceshi1/ITK/Filter/Threshold_Seg/input.png";
    string output_name = "D:/ceshi1/ITK/Filter/Threshold_Seg/output.png";


    using InputPixelType = unsigned char;
    using OutputPixelType = unsigned char;

    using InputImageType = itk::Image2>;using OutputImageType = itk::Image2>;using FilterType = itk::BinaryThresholdImageFilter;using ReaderType = itk::ImageFileReader;using WriterType = itk::ImageFileWriter;
    ReaderType::Pointer reader = ReaderType::New();
    WriterType::Pointer writer = WriterType::New();
    FilterType::Pointer filter = FilterType::New();
    reader->SetFileName(input_name);
    filter->SetInput(reader->GetOutput());
    writer->SetInput(filter->GetOutput());
    writer->SetFileName(output_name);const OutputPixelType  outsidevalue = 0;const InputPixelType  insidevalue = 255;
    filter->SetOutsideValue(outsidevalue);
    filter->SetInsideValue(insidevalue);const InputPixelType lowerThreshold = 150;const OutputPixelType upperThreshold = 180;
    filter->SetUpperThreshold(upperThreshold);
    filter->SetLowerThreshold(lowerThreshold);try
    {
        filter->Update();// Running Filter;
        writer->Update();//Runing Writer;
    }catch(exception &e)
    {cout <"Caught Error!" <endl;cout <endl;return EXIT_FAILURE;
    }return EXIT_SUCCESS;
}

这里 Insidevalue 设置为 0 (黑色),Outsidevalue 设置为 255(白色);阈值分割区间设为 (150,180 );选取的分割图像为 ITK 官方提供的脑部切片 PNG 图片,最终的分割结果如下

0f627bcd4bab3edd34a620d30a96bfe0.png
推荐阅读
分别用 VTK 体绘制和面绘制来实现医学图像三维重建;VTK 基础(一)  —  常用控件介绍及实现圆锥体绘制VTK 基础(二)  —  单窗口实现多对象绘制!vtk基础(三) — 文本注释 vtkTextActor 和 vtkFollower 的使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值