8 #ifndef META_OCEAN_CV_FRAME_VARIANCE_H
9 #define META_OCEAN_CV_FRAME_VARIANCE_H
42 template <
typename T,
typename TIntegral>
43 static bool deviation1Channel8Bit(
const T* frame,
const TIntegral* integral, uint8_t* deviation,
const unsigned int width,
const unsigned int height,
const unsigned int framePaddingElements,
const unsigned int deviationPaddingElements,
const unsigned int window);
59 static bool deviation1Channel8Bit(
const T* frame, uint8_t* deviation,
const unsigned int width,
const unsigned int height,
const unsigned int framePaddingElements,
const unsigned int deviationPaddingElements,
const unsigned int window);
97 template <
typename TElementType,
typename TSummationType,
typename TMultiplicationType,
unsigned int tChannels>
98 static void imageStatistics(
const TElementType* frame,
const unsigned int width,
const unsigned int height,
const unsigned int framePaddingElements,
double* mean =
nullptr,
double* variance =
nullptr,
double* standardDeviation =
nullptr);
101 template <
typename TElementType,
typename TSummationType,
typename TMultiplicationType,
unsigned int tChannels>
102 void FrameVariance::imageStatistics(
const TElementType* frame,
const unsigned int width,
const unsigned int height,
const unsigned int framePaddingElements,
double* mean,
double* variance,
double* standardDeviation)
104 static_assert(tChannels != 0u,
"Number of channels must be in the range [1, infinity)");
105 ocean_assert(frame !=
nullptr);
106 ocean_assert(width != 0u && height != 0u);
108 const unsigned int pixelCount = width * height;
109 const unsigned int elementsCount = (width * tChannels + framePaddingElements) * height;
111 const TElementType*
const frameEnd = frame + elementsCount;
113 TSummationType sum[tChannels];
114 TSummationType squareSum[tChannels];
115 memset(sum, 0, tChannels *
sizeof(TSummationType));
116 memset(squareSum, 0, tChannels *
sizeof(TSummationType));
118 if (framePaddingElements == 0u)
120 for (
unsigned int i = 0u; i < pixelCount; ++i)
122 for (
unsigned int c = 0u; c < tChannels; ++c)
124 ocean_assert_and_suppress_unused(frame < frameEnd, frameEnd);
126 sum[c] += TSummationType(*frame);
128 ocean_assert(std::is_integral<TSummationType>::value ==
false || (squareSum[c] <=
NumericT<TSummationType>::maxValue() - TSummationType(TMultiplicationType(*frame) * TMultiplicationType(*frame)) &&
"Integer overflow; TSummationType must be a wider type, cf. NextLargerTyper<TSummationType>::Type"));
130 squareSum[c] += TSummationType(TMultiplicationType(*frame) * TMultiplicationType(*frame));
138 for (
unsigned int y = 0u; y < height; ++y)
140 for (
unsigned int x = 0u; x < width; ++x)
142 for (
unsigned int c = 0u; c < tChannels; ++c)
144 ocean_assert(frame < frameEnd);
146 sum[c] += TSummationType(*frame);
148 ocean_assert(std::is_integral<TSummationType>::value ==
false || (squareSum[c] <=
NumericT<TSummationType>::maxValue() - TSummationType(TMultiplicationType(*frame) * TMultiplicationType(*frame)) &&
"Integer overflow; TSummationType must be a wider type, cf. NextLargerTyper<TSummationType>::Type"));
150 squareSum[c] += TSummationType(TMultiplicationType(*frame) * TMultiplicationType(*frame));
156 frame += framePaddingElements;
160 double localMean[tChannels];
161 double localVariance[tChannels];
163 ocean_assert(pixelCount != 0u);
164 const double normalizer = 1.0 / double(pixelCount);
166 for (
unsigned int c = 0u; c < tChannels; ++c)
168 localMean[c] = double(sum[c]) * normalizer;
174 localVariance[c] = std::max(0.0, (
double(squareSum[c]) * normalizer) - (localMean[c] * localMean[c]));
179 for (
unsigned int c = 0u; c < tChannels; ++c)
181 mean[c] = localMean[c];
187 for (
unsigned int c = 0u; c < tChannels; ++c)
189 variance[c] = localVariance[c];
193 if (standardDeviation)
195 for (
unsigned int c = 0u; c < tChannels; ++c)
197 ocean_assert(localVariance[c] >= 0.0);
This class implements functions to determine the frame variance.
Definition: FrameVariance.h:27
static void imageStatistics(const TElementType *frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, double *mean=nullptr, double *variance=nullptr, double *standardDeviation=nullptr)
Compute the per-channel mean, variance, and standard deviation over an image.
Definition: FrameVariance.h:102
static bool deviation1Channel8Bit(const T *frame, uint8_t *deviation, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const unsigned int deviationPaddingElements, const unsigned int window)
This functions determines the deviation within a 1-channel 8 bit frame.
static bool deviation1Channel8Bit(const T *frame, const TIntegral *integral, uint8_t *deviation, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const unsigned int deviationPaddingElements, const unsigned int window)
This functions determines the frame deviation of a 1 channel 8 bit frame.
This class provides basic numeric functionalities.
Definition: Numeric.h:57
static T sqrt(const T value)
Returns the square root of a given value.
Definition: Numeric.h:1533
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15