1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* 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.
*
*=========================================================================*/
#ifndef itkDifferenceOfGaussiansGradientImageFilter_hxx
#define itkDifferenceOfGaussiansGradientImageFilter_hxx
#include <cmath>
#include "itkProgressReporter.h"
#include "itkDifferenceOfGaussiansGradientImageFilter.h"
#include "itkImageRegionIterator.h"
namespace itk
{
template< typename TInputImage, typename TDataType >
DifferenceOfGaussiansGradientImageFilter< TInputImage, TDataType >
::DifferenceOfGaussiansGradientImageFilter()
{
itkDebugMacro(<< "DifferenceOfGaussiansGradientImageFilter::DifferenceOfGaussiansGradientImageFilter() called");
m_Width = 2;
}
template< typename TInputImage, typename TDataType >
void
DifferenceOfGaussiansGradientImageFilter< TInputImage, TDataType >
::GenerateData()
{
itkDebugMacro(<< "DifferenceOfGaussiansGradientImageFilter::GenerateData() called");
// Get the input and output pointers
typename Superclass::InputImagePointer inputPtr =
const_cast< TInputImage * >( this->GetInput(0) );
typename Superclass::OutputImagePointer outputPtr = this->GetOutput(0);
// Make sure we're getting everything
inputPtr->SetRequestedRegionToLargestPossibleRegion();
// How big is the input image?
typename TInputImage::SizeType size = inputPtr->GetLargestPossibleRegion().GetSize();
// Create a region object native to the output image type
OutputImageRegionType outputRegion;
// Resize the output region
outputRegion.SetSize(size);
// Set the largest legal region size (i.e. the size of the whole image)
// to what we just defined
outputPtr->SetBufferedRegion(outputRegion);
outputPtr->Allocate();
// Create a progress reporter
ProgressReporter progress( this, 0, outputPtr->GetRequestedRegion().GetNumberOfPixels() );
// Create an iterator that will walk the output region
typedef ImageRegionIterator< TOutputImage > OutputIterator;
OutputIterator outIt = OutputIterator( outputPtr,
outputPtr->GetRequestedRegion() );
// Define a few indices that will be used to translate from an input pixel
// to an output pixel
typename TOutputImage::IndexType outputIndex;
typename TOutputImage::IndexType upperIndex;
typename TOutputImage::IndexType lowerIndex;
// walk the output image, and sample the input image
for (; !outIt.IsAtEnd(); ++outIt )
{
// determine the index of the output pixel
outputIndex = outIt.GetIndex();
// is the current index an acceptable distance from the edges
// of the image?
bool isValidGrad = true;
for ( unsigned int i = 0; i < NDimensions; ++i )
{
const int width = static_cast< int >( m_Width );
const int sizeDifference =
static_cast< int >( size.m_Size[i] ) - width;
if ( !( ( outputIndex[i] < sizeDifference )
&& ( outputIndex[i] >= width ) ) )
{
isValidGrad = false;
}
}
if ( isValidGrad )
{
// We're in a safe position, so calculate the gradient for
// each dimension
for ( unsigned int i = 0; i < NDimensions; i++ )
{
// Build the indices for each pixel
for ( unsigned int j = 0; j < NDimensions; j++ )
{
if ( j == i )
{
upperIndex[j] = outputIndex[j] + static_cast< IndexValueType >( m_Width );
lowerIndex[j] = outputIndex[j] - static_cast< IndexValueType >( m_Width );
}
else
{
upperIndex[j] = outputIndex[j];
lowerIndex[j] = outputIndex[j];
}
}
// Remember, output type is a covariant vector of TDataType
outputPtr->GetPixel(outputIndex)[i] =
inputPtr->GetPixel(upperIndex) - inputPtr->GetPixel(lowerIndex);
}
}
else // We're not in a safe position, gradient is zero
{
for ( unsigned int i = 0; i < NDimensions; ++i )
{
outputPtr->GetPixel(outputIndex)[i] = 0.0;
}
}
progress.CompletedPixel();
}
itkDebugMacro(<< "DifferenceOfGaussiansGradientImageFilter::GenerateData() finished");
}
template< typename TInputImage, typename TDataType >
void
DifferenceOfGaussiansGradientImageFilter< TInputImage, TDataType >
::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Width is " << m_Width << std::endl;
}
} // end namespace
#endif
|