ITK  5.3.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageGrid/RunImageFilterOnRegionOfImage/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
int
main(int argc, char * argv[])
{
// Verify command line arguments
if (argc < 2)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " InputImageFile [radius]" << std::endl;
return EXIT_FAILURE;
}
std::string inputFileName = argv[1];
// Setup types
using ImageType = itk::Image<float, 2>;
const auto input = itk::ReadImage<ImageType>(inputFileName);
// Create and setup a median filter
FilterType::Pointer medianFilter = FilterType::New();
FilterType::InputSizeType radius;
radius.Fill(2);
if (argc > 2)
{
radius.Fill(atoi(argv[2]));
}
itk::Size<2> processSize;
processSize[0] = input->GetLargestPossibleRegion().GetSize()[0] / 2;
processSize[1] = input->GetLargestPossibleRegion().GetSize()[1] / 2;
itk::Index<2> processIndex;
processIndex[0] = processSize[0] / 2;
processIndex[1] = processSize[1] / 2;
itk::ImageRegion<2> processRegion(processIndex, processSize);
medianFilter->SetRadius(radius);
medianFilter->SetInput(input);
medianFilter->GetOutput()->SetRequestedRegion(processRegion);
PasteType::Pointer pasteFilter = PasteType::New();
pasteFilter->SetSourceImage(medianFilter->GetOutput());
pasteFilter->SetSourceRegion(medianFilter->GetOutput()->GetRequestedRegion());
pasteFilter->SetDestinationImage(input);
pasteFilter->SetDestinationIndex(processIndex);
SubtractType::Pointer diff = SubtractType::New();
diff->SetInput1(input);
diff->SetInput2(pasteFilter->GetOutput());
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddImage(input, true, itksys::SystemTools::GetFilenameName(inputFileName));
std::stringstream desc;
desc << "Median/PasteImageFilter, radius = " << radius;
viewer.AddImage(pasteFilter->GetOutput(), true, desc.str());
std::stringstream desc2;
desc2 << "Original - Median/Paste";
viewer.AddImage(diff->GetOutput(), true, desc2.str());
viewer.Visualize();
#endif
return EXIT_SUCCESS;
}
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:67
itk::Size< 2 >
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:69
itkMedianImageFilter.h
itkImageFileReader.h
itkImage.h
itk::PasteImageFilter
Paste an image (or a constant value) into another image.
Definition: itkPasteImageFilter.h:57
QuickView.h
QuickView::AddImage
void AddImage(TImage *, bool FlipVertical=true, std::string Description="")
itkSubtractImageFilter.h
itk::SubtractImageFilter
Pixel-wise subtraction of two images.
Definition: itkSubtractImageFilter.h:68
itkPasteImageFilter.h
QuickView
A convenient class to render itk images with vtk.
Definition: QuickView.h:111
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itk::MedianImageFilter
Applies a median filter to an image.
Definition: itkMedianImageFilter.h:53
QuickView::Visualize
void Visualize(bool interact=true)
itk::Size::GetSize
const SizeValueType * GetSize() const
Definition: itkSize.h:169