美文网首页
ITK 基础(二) — 图像分割 General Thresho

ITK 基础(二) — 图像分割 General Thresho

作者: 小张Python | 来源:发表于2020-08-11 12:54 被阅读0次

    General Threshold 介绍

    上篇文章介绍了 ITK 中的二值化分割,最终得到的是 二值图像(图像中只有两种像素值),

    但有时我们会遇到另外一种需求,只改变某一阈值范围的像素值,其他部分保留;这时二值化分割已经满足不了我们的基本需求了,需要寻求另外一种方法。

    本篇教程介绍 ITK 中的 General Threshold ,是二值化的改进版,可以只改变某一范围内的像素值,并且其它范围内像素值得到保留。

    原理讲解

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

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

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

    First.png

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

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

    Second.png

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

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

    Third.png

    代码实现

    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 张图片分别为 第一至第三种方法生成得到的结果。

    Snipaste_2020-05-03_17-15-15.png

    相关文章

      网友评论

          本文标题:ITK 基础(二) — 图像分割 General Thresho

          本文链接:https://www.haomeiwen.com/subject/ronedktx.html