R语言怎么实现二值化分割

发布时间:2022-06-01 11:36:03 作者:iii
来源:亿速云 阅读:254

这篇“R语言怎么实现二值化分割”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“R语言怎么实现二值化分割”文章吧。

原理讲解

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

R语言怎么实现二值化分割  

 

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

R语言怎么实现二值化分割
只要图像像素值在这者之间,则该像素值将改变为 Insidevalue;否则将改为 Outsidevalue;最终图像的像素值只有两种:Insidevalue 或者是 Outsidevalue;

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

代码实现

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

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

#include<itkBinaryThresholdImageFilter.h>
#include<itkImage.h>
#include<itkImageFileReader.h>
#include<itkImageFileWriter.h>
#include<itkPNGImageIOFactory.h>
#include<string.h>

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::Image<InputPixelType, 2>;
    using OutputImageType = itk::Image<OutputPixelType, 2>;

    using FilterType = itk::BinaryThresholdImageFilter<InputImageType, OutputImageType>;

    using ReaderType = itk::ImageFileReader<InputImageType>;
    using WriterType = itk::ImageFileWriter<OutputImageType>;

    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 << e.what() << endl;

        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;

}

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

R语言怎么实现二值化分割

以上就是关于“R语言怎么实现二值化分割”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. opencv python实现图像二值化的示例
  2. python如何实现图片二值化及灰度处理方式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

r语言

上一篇:Python如何实现线性规划

下一篇:R语言怎么实现图像分割

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》