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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/*=========================================================================
*
* 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.
*
*=========================================================================*/
#ifndef itkEigenAnalysis2DImageFilter_hxx
#define itkEigenAnalysis2DImageFilter_hxx
#include "itkImageRegionIteratorWithIndex.h"
#include "itkProgressReporter.h"
namespace itk
{
template <typename TInputImage, typename TEigenValueImage, typename TEigenVectorImage>
EigenAnalysis2DImageFilter<TInputImage, TEigenValueImage, TEigenVectorImage>::EigenAnalysis2DImageFilter()
{
this->SetNumberOfRequiredInputs(3);
this->SetNumberOfRequiredOutputs(3);
this->SetNthOutput(0, this->MakeOutput(0));
this->SetNthOutput(1, this->MakeOutput(1));
this->SetNthOutput(2, this->MakeOutput(2));
static_assert(EigenVectorType::Dimension == 2, "Error: PixelType of EigenVector Image must have exactly 2 elements!");
}
template <typename TInputImage, typename TEigenValueImage, typename TEigenVectorImage>
void
EigenAnalysis2DImageFilter<TInputImage, TEigenValueImage, TEigenVectorImage>::SetInput1(TInputImage * image)
{
this->SetNthInput(0, image);
}
template <typename TInputImage, typename TEigenValueImage, typename TEigenVectorImage>
void
EigenAnalysis2DImageFilter<TInputImage, TEigenValueImage, TEigenVectorImage>::SetInput2(TInputImage * image)
{
this->SetNthInput(1, image);
}
template <typename TInputImage, typename TEigenValueImage, typename TEigenVectorImage>
void
EigenAnalysis2DImageFilter<TInputImage, TEigenValueImage, TEigenVectorImage>::SetInput3(TInputImage * image)
{
this->SetNthInput(2, image);
}
template <typename TInputImage, typename TEigenValueImage, typename TEigenVectorImage>
auto
EigenAnalysis2DImageFilter<TInputImage, TEigenValueImage, TEigenVectorImage>::GetMaxEigenValue()
-> EigenValueImageType *
{
return dynamic_cast<EigenValueImageType *>(this->ProcessObject::GetOutput(0));
}
template <typename TInputImage, typename TEigenValueImage, typename TEigenVectorImage>
auto
EigenAnalysis2DImageFilter<TInputImage, TEigenValueImage, TEigenVectorImage>::GetMinEigenValue()
-> EigenValueImageType *
{
return dynamic_cast<EigenValueImageType *>(this->ProcessObject::GetOutput(1));
}
template <typename TInputImage, typename TEigenValueImage, typename TEigenVectorImage>
auto
EigenAnalysis2DImageFilter<TInputImage, TEigenValueImage, TEigenVectorImage>::GetMaxEigenVector()
-> EigenVectorImageType *
{
auto * eigenVector = dynamic_cast<EigenVectorImageType *>(this->ProcessObject::GetOutput(2));
if (eigenVector)
{
return eigenVector;
}
else
{
itkWarningMacro(
<< "EigenAnalysis2DImageFilter::GetMaxEigenVector(): dynamic_cast has failed. A reinterpret_cast is being "
"attempted."
<< std::endl
<< "Type name is: " << typeid(*this->GetOutput(2)).name());
return reinterpret_cast<EigenVectorImageType *>(this->ProcessObject::GetOutput(2));
}
}
template <typename TInputImage, typename TEigenValueImage, typename TEigenVectorImage>
DataObject::Pointer
EigenAnalysis2DImageFilter<TInputImage, TEigenValueImage, TEigenVectorImage>::MakeOutput(
DataObjectPointerArraySizeType idx)
{
DataObject::Pointer output;
switch (idx)
{
case 0:
output = (EigenValueImageType::New()).GetPointer();
break;
case 1:
output = (EigenValueImageType::New()).GetPointer();
break;
case 2:
output = (EigenVectorImageType::New()).GetPointer();
break;
}
return output.GetPointer();
}
template <typename TInputImage, typename TEigenValueImage, typename TEigenVectorImage>
void
EigenAnalysis2DImageFilter<TInputImage, TEigenValueImage, TEigenVectorImage>::GenerateData()
{
typename TInputImage::ConstPointer inputPtr1(dynamic_cast<const TInputImage *>((ProcessObject::GetInput(0))));
typename TInputImage::ConstPointer inputPtr2(dynamic_cast<const TInputImage *>((ProcessObject::GetInput(1))));
typename TInputImage::ConstPointer inputPtr3(dynamic_cast<const TInputImage *>((ProcessObject::GetInput(2))));
EigenValueImagePointer outputPtr1 = this->GetMaxEigenValue();
EigenValueImagePointer outputPtr2 = this->GetMinEigenValue();
EigenVectorImagePointer outputPtr3 = this->GetMaxEigenVector();
outputPtr1->SetBufferedRegion(inputPtr1->GetBufferedRegion());
outputPtr2->SetBufferedRegion(inputPtr1->GetBufferedRegion());
outputPtr3->SetBufferedRegion(inputPtr1->GetBufferedRegion());
outputPtr1->Allocate();
outputPtr2->Allocate();
outputPtr3->Allocate();
EigenValueImageRegionType region = outputPtr1->GetRequestedRegion();
ImageRegionConstIteratorWithIndex<TInputImage> inputIt1(inputPtr1, region);
ImageRegionConstIteratorWithIndex<TInputImage> inputIt2(inputPtr2, region);
ImageRegionConstIteratorWithIndex<TInputImage> inputIt3(inputPtr3, region);
ImageRegionIteratorWithIndex<EigenValueImageType> outputIt1(outputPtr1, region);
ImageRegionIteratorWithIndex<EigenValueImageType> outputIt2(outputPtr2, region);
ImageRegionIteratorWithIndex<EigenVectorImageType> outputIt3(outputPtr3, region);
EigenVectorType nullVector;
nullVector.Fill(0.0);
// support progress methods/callbacks
ProgressReporter progress(this, 0, region.GetNumberOfPixels());
inputIt1.GoToBegin();
inputIt2.GoToBegin();
inputIt3.GoToBegin();
outputIt1.GoToBegin();
outputIt2.GoToBegin();
outputIt3.GoToBegin();
EigenVectorType eigenVector;
while (!inputIt1.IsAtEnd())
{
const auto xx = static_cast<double>(inputIt1.Get());
const auto xy = static_cast<double>(inputIt2.Get());
const auto yy = static_cast<double>(inputIt3.Get());
const double dxy = xx - yy;
const double sxy = xx + yy;
const double S = std::sqrt(dxy * dxy + 4.0 * xy * xy);
const double pp = (sxy + S) / 2.0;
const double qq = (sxy - S) / 2.0;
outputIt1.Set(pp);
outputIt2.Set(qq);
eigenVector[0] = static_cast<VectorComponentType>((-dxy - S) / 2.0);
eigenVector[1] = static_cast<VectorComponentType>(-xy);
const VectorComponentType norm = eigenVector.GetNorm();
if (norm > 1e-30)
{
outputIt3.Set(eigenVector / norm);
}
else
{
outputIt3.Set(nullVector);
}
++inputIt1;
++inputIt2;
++inputIt3;
++outputIt1;
++outputIt2;
++outputIt3;
progress.CompletedPixel();
}
}
} // end namespace itk
#endif
|