ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/Thresholding/SeparateGroundUsingOtsu/Code.cxx
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkImage.h"
#include "itksys/SystemTools.hxx"
#include <sstream>
#ifdef ENABLE_QUICKVIEW
# include "QuickView.h"
#endif
namespace
{
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType, 2>;
} // namespace
static void
CreateImage(ImageType::Pointer image);
int
main(int argc, char * argv[])
{
ImageType::Pointer image;
if (argc < 2)
{
image = ImageType::New();
CreateImage(image.GetPointer());
}
else
{
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
image = reader->GetOutput();
}
FilterType::Pointer otsuFilter = FilterType::New();
otsuFilter->SetInput(image);
otsuFilter->Update(); // To compute threshold
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddImage(
image.GetPointer(), true, argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated image");
std::stringstream desc;
desc << "Otsu Threshold: " << itk::NumericTraits<FilterType::InputPixelType>::PrintType(otsuFilter->GetThreshold());
viewer.AddImage(otsuFilter->GetOutput(), true, desc.str());
viewer.Visualize();
#endif
return EXIT_SUCCESS;
}
void
CreateImage(ImageType::Pointer image)
{
// Create an image
start.Fill(0);
size.Fill(100);
region.SetSize(size);
region.SetIndex(start);
image->SetRegions(region);
image->Allocate();
// Make the whole image white
itk::ImageRegionIterator<ImageType> iterator(image, image->GetLargestPossibleRegion());
/*
//Create a square
while(!iterator.IsAtEnd())
{
iterator.Set(255);
++iterator;
}
*/
}
itkImageFileReader.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itkImageRegionIterator.h
itk::OtsuThresholdImageFilter
Threshold an image using the Otsu Threshold.
Definition: itkOtsuThresholdImageFilter.h:56
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:270
itk::Size::Fill
void Fill(SizeValueType value)
Definition: itkSize.h:211
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:80
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
QuickView.h
QuickView::AddImage
void AddImage(TImage *, bool FlipVertical=true, std::string Description="")
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::NumericTraits::PrintType
T PrintType
Definition: itkNumericTraits.h:68
itkOtsuThresholdImageFilter.h
QuickView
A convenient class to render itk images with vtk.
Definition: QuickView.h:111
itkNumericTraits.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::ImageRegion::SetSize
void SetSize(const SizeType &size)
Definition: itkImageRegion.h:174
QuickView::Visualize
void Visualize(bool interact=true)