ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/Convolution/ConvolveImageWithKernel/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
*
* https://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"
#ifdef ENABLE_QUICKVIEW
# include "QuickView.h"
#endif
using ImageType = itk::Image<float, 2>;
static void
CreateKernel(ImageType::Pointer kernel, unsigned int width);
int
main(int argc, char * argv[])
{
// Verify command line arguments
if (argc < 2)
{
std::cerr << "Usage: ";
std::cerr << argv[0] << "inputImageFile [width]" << std::endl;
return EXIT_FAILURE;
}
// Parse command line arguments
unsigned int width = 3;
if (argc > 2)
{
width = std::stoi(argv[2]);
}
auto kernel = ImageType::New();
CreateKernel(kernel, width);
const auto input = itk::ReadImage<ImageType>(argv[1]);
// Convolve image with kernel.
auto convolutionFilter = FilterType::New();
convolutionFilter->SetInput(input);
#if ITK_VERSION_MAJOR >= 4
convolutionFilter->SetKernelImage(kernel);
#else
convolutionFilter->SetImageKernelInput(kernel);
#endif
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddImage<ImageType>(input, true, itksys::SystemTools::GetFilenameName(argv[1]));
std::stringstream desc;
desc << "ConvolutionFilter\n"
<< "Kernel Witdh = " << width;
viewer.AddImage<ImageType>(convolutionFilter->GetOutput(), true, desc.str());
viewer.Visualize();
#endif
return EXIT_SUCCESS;
}
void
CreateKernel(ImageType::Pointer kernel, unsigned int width)
{
start.Fill(0);
size.Fill(width);
region.SetSize(size);
region.SetIndex(start);
kernel->SetRegions(region);
kernel->Allocate();
itk::ImageRegionIterator<ImageType> imageIterator(kernel, region);
while (!imageIterator.IsAtEnd())
{
// imageIterator.Set(255);
imageIterator.Set(1);
++imageIterator;
}
}
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itkImageFileReader.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itkImageRegionIterator.h
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:274
itk::Size::Fill
void Fill(SizeValueType value)
Definition: itkSize.h:213
itkConvolutionImageFilter.h
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::ConvolutionImageFilter
Convolve a given image with an arbitrary image kernel.
Definition: itkConvolutionImageFilter.h:65
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
New
static Pointer New()
itk::ImageRegion::SetSize
void SetSize(const SizeType &size)
Definition: itkImageRegion.h:202
QuickView::Visualize
void Visualize(bool interact=true)