溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

R語言怎么實現(xiàn)二值化分割

發(fā)布時間:2022-06-01 11:36:03 來源:億速云 閱讀:251 作者:iii 欄目:大數(shù)據(jù)

這篇“R語言怎么實現(xiàn)二值化分割”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“R語言怎么實現(xiàn)二值化分割”文章吧。

原理講解

ITK 中的二值化分割主要用到 itk::BinaryThresholdImageFilter 過濾器,其分割原理圖如下:

R語言怎么實現(xiàn)二值化分割  

 

二值化分割是分割方法中最基礎的,通過定義 Lower 和 Upper 兩個像素臨界點

R語言怎么實現(xiàn)二值化分割
只要圖像像素值在這者之間,則該像素值將改變?yōu)?Insidevalue;否則將改為 Outsidevalue;最終圖像的像素值只有兩種:Insidevalue 或者是 Outsidevalue;

注:上面的 Insidevalue、Outsidevalue、Lowervalue、Uppervalue 四個參數(shù)是用戶自己設定的。

代碼實現(xiàn)

上文已經(jīng)提到了,二值化分割主要用到的頭文件為 itkBinaryThresholdImageFilter ,該過濾器主要通過設置四個參數(shù)來完成分割效果。

下面的代碼部分就是關于二值分割的功能實現(xiàn),代碼中,依次進行圖像讀取、參數(shù)設定、二值化處理、圖像寫出等一系列步驟

#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(白色);閾值分割區(qū)間設為 (150,180 );選取的分割圖像為 ITK 官方提供的腦部切片 PNG 圖片,最終的分割結果如下

R語言怎么實現(xiàn)二值化分割

以上就是關于“R語言怎么實現(xiàn)二值化分割”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI