The full name of ITK is Insight Toolkit. It is an open source, cross platform and image analysis toolkit. Its development follows extreme programming. The mainstream language is C + +. However, at present, the development team has provided an interface for Python.
ITK encapsulates many excellent algorithms. ITK can be used in image processing, registration, segmentation and other fields. The processed image dimension is oriented to two-dimensional, three-dimensional or higher dimensions
Principle explanation
This is the first article in the ITK series of tutorials, which mainly introduces the implementation of binary segmentation function in the toolkit; The purpose of image segmentation is to extract the region we want by changing the pixel value of the image, which is generally the premise of image processing;
The binary segmentation in ITK mainly uses the itk::BinaryThresholdImageFilter filter, and its segmentation schematic diagram is as follows:
Binary segmentation is the most basic of segmentation methods. By defining two pixel critical points: Lower and Upper
$$
P = \left{
\begin{aligned}
Inside value & &{LowerThreshold<= P <= UpperThreshold}\
Outside value & & {lowerThreshold>P; P> UpperThreshold}\
\end{aligned}
\right.
$$
As long as the image pixel value is between, the pixel value will be adapted to Insidevalue; Otherwise, it will be changed to Outsidevalue; There are only two kinds of pixel values in the final image: inside value or outside value;
Note: the above four parameters: Insidevalue, Outsidevalue, Lowervalue and Uppervalue are set by the user.
code implementation
As mentioned above, the header file mainly used in binary segmentation is itkBinaryThresholdImageFilter, which mainly completes the segmentation effect by setting four parameters.
The following code part is about the function realization of binary segmentation. In the code, a series of steps are carried out successively, such as image reading, parameter setting, binary processing, image writing and so on
#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; }
Here, the inside value is set to 0 (black), and the outside value is set to 255 (white); The threshold segmentation interval is set to (150180); The selected segmentation image is the PNG image of brain slice officially provided by ITK. The final segmentation results are as follows