/usr/include/OTB-5.8/otbHistogramOfOrientedGradientCovariantImageFunction.txx is in libotb-dev 5.8.0+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | /*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbHistogramOfOrientedGradientCovariantImageFunction_txx
#define otbHistogramOfOrientedGradientCovariantImageFunction_txx
#include "otbHistogramOfOrientedGradientCovariantImageFunction.h"
#include "itkConstNeighborhoodIterator.h"
#include "itkNumericTraits.h"
#include "otbMath.h"
namespace otb
{
/**
* Constructor
*/
template <class TInputImage, class TOutputPrecision, class TCoordRep>
HistogramOfOrientedGradientCovariantImageFunction<TInputImage, TOutputPrecision, TCoordRep>
::HistogramOfOrientedGradientCovariantImageFunction() : m_NeighborhoodRadius(8),
m_NumberOfOrientationBins(18)
{}
template <class TInputImage, class TOutputPrecision, class TCoordRep>
void
HistogramOfOrientedGradientCovariantImageFunction<TInputImage, TOutputPrecision, TCoordRep>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
this->Superclass::PrintSelf(os, indent);
os << indent << " Neighborhood radius value : " << m_NeighborhoodRadius << std::endl;
}
template <class TInputImage, class TOutputPrecision, class TCoordRep>
typename HistogramOfOrientedGradientCovariantImageFunction<TInputImage, TOutputPrecision, TCoordRep>::OutputType
HistogramOfOrientedGradientCovariantImageFunction<TInputImage, TOutputPrecision, TCoordRep>
::EvaluateAtIndex(const IndexType& index) const
{
// Build output vector
OutputType hog;
// Check for input image
if( !this->GetInputImage() )
{
return hog;
}
// Check for out of buffer
if ( !this->IsInsideBuffer( index ) )
{
return hog;
}
// Create an N-d neighborhood kernel, using a zeroflux boundary condition
typename InputImageType::SizeType kernelSize;
kernelSize.Fill( m_NeighborhoodRadius );
itk::ConstNeighborhoodIterator<InputImageType>
it(kernelSize, this->GetInputImage(), this->GetInputImage()->GetBufferedRegion());
// Set the iterator at the desired location
it.SetLocation(index);
// Offset to be used in the loops
typename InputImageType::OffsetType offset;
// Compute the center bin radius
double centerBinRadius = static_cast<double>(m_NeighborhoodRadius)/2;
// Define a gaussian kernel around the center location
double squaredRadius = m_NeighborhoodRadius * m_NeighborhoodRadius;
double squaredCenterBinRadius = centerBinRadius * centerBinRadius;
double squaredSigma = 0.25 * squaredRadius;
// Build a global orientation histogram
std::vector<TOutputPrecision> globalOrientationHistogram(m_NumberOfOrientationBins, 0.);
// Compute the orientation bin width
double orientationBinWidth = otb::CONST_2PI / m_NumberOfOrientationBins;
// Build the global orientation histogram
for(int i = -(int)m_NeighborhoodRadius; i< (int)m_NeighborhoodRadius; ++i)
{
for(int j = -(int)m_NeighborhoodRadius; j< (int)m_NeighborhoodRadius; ++j)
{
// Check if the current pixel lies within a disc of radius m_NeighborhoodRadius
double currentSquaredRadius = i*i+j*j;
if(currentSquaredRadius < squaredRadius)
{
// If so, compute the gaussian weighting (this could be
// computed once for all for the sake of optimisation)
double gWeight = (1/vcl_sqrt(otb::CONST_2PI*squaredSigma)) * vcl_exp(- currentSquaredRadius/(2*squaredSigma));
// Compute pixel location
offset[0]=i;
offset[1]=j;
// Get the current gradient covariant value
InputPixelType gradient = it.GetPixel(offset);
// Then, compute the gradient orientation
double angle = vcl_atan2(gradient[1], gradient[0]);
// Also compute its magnitude
TOutputPrecision magnitude = vcl_sqrt(gradient[0]*gradient[0]+gradient[1]*gradient[1]);
// Determine the bin index (shift of otb::CONST_PI since atan2 values
// lies in [-pi, pi]
unsigned int binIndex = vcl_floor((otb::CONST_PI + angle)/orientationBinWidth);
// Handle special case where angle = pi, and binIndex is out-of-bound
if(binIndex == m_NumberOfOrientationBins)
binIndex=m_NumberOfOrientationBins-1;
// Cumulate values
globalOrientationHistogram[binIndex]+= magnitude * gWeight;
}
}
}
// Compute principal orientation
// TODO: Replace this with a stl algorithm
double maxOrientationHistogramValue = globalOrientationHistogram[0];
unsigned int maxOrientationHistogramBin = 0;
for(unsigned int i = 1; i < m_NumberOfOrientationBins; ++i)
{
// Retrieve current value
double currentValue = globalOrientationHistogram[i];
// Look for new maximum
if(maxOrientationHistogramValue<currentValue)
{
maxOrientationHistogramValue = currentValue;
maxOrientationHistogramBin = i;
}
}
// Derive principal orientation
double principalOrientation = maxOrientationHistogramBin * orientationBinWidth - otb::CONST_PI;
// Initialize the five spatial bins
std::vector<TOutputPrecision> centerHistogram(m_NumberOfOrientationBins, 0.);
std::vector<TOutputPrecision> upperLeftHistogram(m_NumberOfOrientationBins, 0.);
std::vector<TOutputPrecision> upperRightHistogram(m_NumberOfOrientationBins, 0.);
std::vector<TOutputPrecision> lowerLeftHistogram(m_NumberOfOrientationBins, 0.);
std::vector<TOutputPrecision> lowerRightHistogram(m_NumberOfOrientationBins, 0.);
// Walk the image once more to fill the spatial bins
for(int i = -(int)m_NeighborhoodRadius; i< (int)m_NeighborhoodRadius; ++i)
{
for(int j = -(int)m_NeighborhoodRadius; j< (int)m_NeighborhoodRadius; ++j)
{
// Check if the current pixel lies within a disc of radius m_NeighborhoodRadius
double currentSquaredRadius = i*i+j*j;
if(currentSquaredRadius < squaredRadius)
{
// If so, compute the gaussian weighting (this could be
// computed once for all for the sake of optimisation)
double gWeight = (1/vcl_sqrt(otb::CONST_2PI * squaredSigma)) * vcl_exp(- currentSquaredRadius/(2*squaredSigma));
// Compute pixel location
offset[0]=i;
offset[1]=j;
// Get the current gradient covariant value
InputPixelType gradient = it.GetPixel(offset);
// Then, compute the compensated gradient orientation
double angle = vcl_atan2(gradient[1], gradient[0]) - principalOrientation;
// Angle is supposed to lie with [-pi, pi], so we ensure that
// compenstation did not introduce out-of-range values
if(angle > otb::CONST_PI)
{
angle -= otb::CONST_2PI;
}
else if(angle < -otb::CONST_PI)
{
angle += otb::CONST_2PI;
}
// Also compute its magnitude
TOutputPrecision magnitude = vcl_sqrt(gradient[0]*gradient[0]+gradient[1]*gradient[1]);
// Determine the bin index (shift of otb::CONST_PI since atan2 values
// lies in [-pi, pi]
unsigned int binIndex = vcl_floor((otb::CONST_PI + angle)/orientationBinWidth);
if(binIndex == m_NumberOfOrientationBins)
binIndex=m_NumberOfOrientationBins-1;
// Compute the angular position
double angularPosition = vcl_atan2((double)j, (double)i) - principalOrientation;
// Angle is supposed to lie within [-pi, pi], so we ensure that
// the compensation did not introduce out-of-range values
if(angularPosition > otb::CONST_PI)
{
angularPosition -= otb::CONST_2PI;
}
else if(angularPosition < -otb::CONST_PI)
{
angularPosition += otb::CONST_2PI;
}
// Check if we lie in center bin
if(currentSquaredRadius < squaredCenterBinRadius)
{
centerHistogram[binIndex]+= magnitude * gWeight;
}
else if(angularPosition > 0)
{
if(angularPosition < otb::CONST_PI_2)
{
upperRightHistogram[binIndex]+= magnitude * gWeight;
}
else
{
upperLeftHistogram[binIndex]+= magnitude * gWeight;
}
}
else
{
if(angularPosition > -otb::CONST_PI_2)
{
lowerRightHistogram[binIndex]+= magnitude * gWeight;
}
else
{
lowerLeftHistogram[binIndex]+= magnitude * gWeight;
}
}
// Cumulate values
globalOrientationHistogram[binIndex]+= magnitude * gWeight;
}
}
}
// Build the final output
hog.push_back(centerHistogram);
hog.push_back(upperLeftHistogram);
hog.push_back(upperRightHistogram);
hog.push_back(lowerRightHistogram);
hog.push_back(lowerLeftHistogram);
// Normalize each histogram
for(typename OutputType::iterator oIt = hog.begin(); oIt!=hog.end(); ++oIt)
{
// Compute L2 norm
double squaredCumul = 1e-10;
for(typename std::vector<TOutputPrecision>::const_iterator vIt = oIt->begin();
vIt!=oIt->end(); ++vIt)
{
squaredCumul += (*vIt)*(*vIt);
}
double scale = 1/vcl_sqrt(squaredCumul);
// Apply normalisation factor
for(typename std::vector<TOutputPrecision>::iterator vIt = oIt->begin();
vIt!=oIt->end(); ++vIt)
{
(*vIt)*=scale;
}
}
// Return result
return hog;
}
} // namespace otb
#endif
|