1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkImageKernelOperator_h
19 #define itkImageKernelOperator_h
20 
21 #include "itkNeighborhoodOperator.h"
22 
23 #include "itkImage.h"
24 
25 namespace itk
26 {
27 /**
28  * \class ImageKernelOperator
29  * \brief A NeighborhoodOperator whose coefficients are from an image.
30  *
31  * This code was contributed in the Insight Journal paper:
32  *
33  * "Image Kernel Convolution"
34  * by Tustison N., Gee J.
35  * https://hdl.handle.net/1926/1323
36  * http://www.insight-journal.org/browse/publication/208
37  *
38  *
39  * \sa NeighborhoodOperator
40  * \sa NeighborhoodIterator
41  * \sa Neighborhood
42  *
43  * \ingroup Operators
44  * \ingroup ITKCommon
45  */
46 template< typename TPixel, unsigned int VDimension = 2,
47           typename TAllocator = NeighborhoodAllocator< TPixel > >
48 class ITK_TEMPLATE_EXPORT ImageKernelOperator :
49   public NeighborhoodOperator< TPixel, VDimension, TAllocator >
50 {
51 public:
52   /** Standard class type aliases. */
53   using Self = ImageKernelOperator;
54   using Superclass = NeighborhoodOperator< TPixel, VDimension, TAllocator >;
55 
56   using ImageType = Image< TPixel, VDimension >;
57   using SizeType = typename Superclass::SizeType;
58   using CoefficientVector = typename Superclass::CoefficientVector;
59 
60   itkTypeMacro(ImageKernelOperator, NeighborhoodOperator);
61 
62   /** Constructor. */
63   ImageKernelOperator() = default;
64 
65   /** Copy constructor */
ImageKernelOperator(const Self & orig)66   ImageKernelOperator(const Self & orig):
67     Neighborhood< TPixel, VDimension, TAllocator >(orig)
68   {}
69 
70   /** Assignment operator. */
71   Self & operator=(const Self & orig)
72   {
73     Superclass::operator=(orig);
74     return *this;
75   }
76 
77   /** Set the image kernel. Only images with odd size in all
78    * dimensions are allowed. If an image with an even size is passed
79    * as an argument, an exception will be thrown. */
80   void SetImageKernel(const ImageType *kernel);
81 
82   /** Get the image kernel. */
83   const ImageType * GetImageKernel() const;
84 
85   /** Prints information about the object. */
PrintSelf(std::ostream & os,Indent i)86   void PrintSelf(std::ostream & os, Indent i) const override
87   {
88     os << i << "ImageKernelOperator { this=" << this
89        << "} "  << std::endl;
90     Superclass::PrintSelf( os, i.GetNextIndent() );
91   }
92 
93 protected:
94   /** Calculates operator coefficients. */
95   CoefficientVector GenerateCoefficients() override;
96 
97   /** Arranges coefficients spatially in the memory buffer. */
98   void Fill(const CoefficientVector & coeff) override;
99 
100 private:
101   typename ImageType::ConstPointer m_ImageKernel;
102 
103 };
104 } // namespace itk
105 
106 #ifndef ITK_MANUAL_INSTANTIATION
107 #include "itkImageKernelOperator.hxx"
108 #endif
109 
110 #endif
111