温馨提示×

温馨提示×

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

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

R语言怎么实现图像分割

发布时间:2022-06-01 11:36:59 来源:亿速云 阅读:520 作者:iii 栏目:大数据

这篇文章主要介绍“R语言怎么实现图像分割”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“R语言怎么实现图像分割”文章能帮助大家解决问题。

原理讲解

General Threshold 中用到了三种分割方式:

第一种,原理图如下,该方法需要设置一个低临界阈值 Lower Threshold,图像中像素值若低于这个值,其值变为 Outsidevalue,否则像素值不变;

该方法中需要设置二个参数,低像素值设定用到的是 ThresholdBelow() 函数;用户指定  Outsidevalue;

R语言怎么实现图像分割   

第二种,方法中需要设置一个高临界阈值 Upper Threshold,图像中像素值若高于这个值,像素值将变成 Outsidevalue,否则像素值不变;

该方法中也需要设置二个参数,高阈值设定用到 ThresholdAbove() 函数,用户指定 Outsidevalue;

R语言怎么实现图像分割    

第三种,结合了前两种,该方法需要设置两个阈值临界 Lower Threshold 和 Upper Threshold 两个值,若像素值介于两者之间则不变,否则设为 Outsidevalue;

方法中需要设置三个参数,低阈值设定用到  ThresholdBelow() 函数,高阈值设定用到 ThresholdAbove() 函数,用户指定 Outsidevalue;

R语言怎么实现图像分割    
代码实现

General Threshold 分割方法用到主要头文件为  itk::ThresholdImageFilter ;该 Filter 中阈值的设置用到两个函数:

  • ThresholdAbove() ;

  • ThresholdBelow() ;

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

using namespace std;

int main()
{
    itk::PNGImageIOFactory::RegisterOneFactory();
    string input_name = "D:/ceshi1/ITK/Filter/General_Seg/input.png";
    string output_name1 = "D:/ceshi1/ITK/Filter/General_Seg/output1.png";
    string output_name2 = "D:/ceshi1/ITK/Filter/General_Seg/output2.png";
    string output_name3 = "D:/ceshi1/ITK/Filter/General_Seg/output3.png";

    using PixelType = unsigned char;
    using ImageType = itk::Image<PixelType, 2>;
    using FilterType = itk::ThresholdImageFilter<ImageType>;
    using ReaderType = itk::ImageFileReader<ImageType>;
    using WriterType = itk::ImageFileWriter<ImageType>;

    ReaderType::Pointer reader = ReaderType::New();
    WriterType::Pointer writer = WriterType::New();
    FilterType::Pointer filter = FilterType::New();


    reader->SetFileName(input_name);
    writer->SetFileName(output_name1);
    writer->SetInput(filter->GetOutput());

    filter->SetInput(reader->GetOutput());

    //first Threshold method;
    filter->SetOutsideValue(0);
    filter->ThresholdBelow(170);

    try
    {
        filter->Update();
        writer->Update();

    }
    catch (exception & e)
    {
        cout << "Caught Error" << endl;
        cout << e.what() << endl;
        return EXIT_FAILURE;
    }


    filter->ThresholdAbove(190);
    writer->SetFileName(output_name2);

    try
    {
        filter->Update();
        writer->Update();

    }
    catch (exception & e)
    {
        cout << "Caught Error" << endl;
        cout << e.what() << endl;
        return EXIT_FAILURE;
    }


    //Third Threshold Seg;
    filter->ThresholdOutside(170, 190);
    writer->SetFileName(output_name3);


    try
    {
        filter->Update();
        writer->Update();

    }
    catch (exception & e)
    {
        cout << "Caught Error" << endl;
        cout << e.what() << endl;
        return EXIT_FAILURE;
    }


    return EXIT_SUCCESS;


}

该例子中,用到的输入图像为 ITK 官网提供的大脑切片  PNG  图像,参数设定情况如下:Lower Threshold 设为170,Upper Threshold 设为190,Outsidevalue 设为 0,

最后生成的结果图如下,第 2-4 张图片分别为 第一至第三种方法生成得到的结果。

R语言怎么实现图像分割

关于“R语言怎么实现图像分割”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

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

AI