8 #ifndef META_OCEAN_CV_SUM_ZERO_MEAN_SQUARE_DIFFERENCES_BASE_H
9 #define META_OCEAN_CV_SUM_ZERO_MEAN_SQUARE_DIFFERENCES_BASE_H
44 template <
unsigned int tChannels,
unsigned int tPatchSize>
45 static OCEAN_FORCE_INLINE uint32_t
patch8BitPerChannelTemplate(
const uint8_t*
const image0,
const uint8_t*
const image1,
const unsigned int width0,
const unsigned int width1,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int centerX1,
const unsigned int centerY1,
const unsigned int image0PaddingElements,
const unsigned int image1PaddingElements);
57 template <
unsigned int tChannels,
unsigned int tPatchSize>
58 static uint32_t
patch8BitPerChannelTemplate(
const uint8_t* patch0,
const uint8_t* patch1,
const unsigned int patch0StrideElements,
const unsigned int patch1StrideElements);
72 template <
unsigned int tChannels,
unsigned int tPatchSize>
73 static OCEAN_FORCE_INLINE uint32_t
patchBuffer8BitPerChannelTemplate(
const uint8_t* image0,
const unsigned int width0,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int image0PaddingElements,
const uint8_t* buffer1);
84 template <
unsigned int tChannels,
unsigned int tPatchSize>
95 template <
unsigned int tChannels,
unsigned int tPixels>
114 template <
unsigned int tChannels>
115 static OCEAN_FORCE_INLINE uint32_t
patch8BitPerChannel(
const uint8_t*
const image0,
const uint8_t*
const image1,
const unsigned int patchSize,
const unsigned int width0,
const unsigned int width1,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int centerX1,
const unsigned int centerY1,
const unsigned int image0PaddingElements,
const unsigned int image1PaddingElements);
127 template <
unsigned int tChannels>
128 static uint32_t
patch8BitPerChannel(
const uint8_t* patch0,
const uint8_t* patch1,
const unsigned int patchSize,
const unsigned int patch0StrideElements,
const unsigned int patch1StrideElements);
142 template <
unsigned int tChannels>
143 static OCEAN_FORCE_INLINE uint32_t
patchBuffer8BitPerChannel(
const uint8_t* image0,
unsigned int patchSize,
const unsigned int width0,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int image0PaddingElements,
const uint8_t* buffer1);
154 template <
unsigned int tChannels>
155 static uint32_t
patchBuffer8BitPerChannel(
const uint8_t* patch0,
const uint8_t* buffer1,
unsigned int patchSize,
const unsigned int patch0StrideElements);
165 template <
unsigned int tChannels>
166 static inline unsigned int buffer8BitPerChannel(
const uint8_t* buffer0,
const uint8_t* buffer1,
const unsigned int pixels);
186 template <
unsigned int tChannels>
187 static uint32_t
patchMirroredBorder8BitPerChannel(
const uint8_t* image0,
const uint8_t* image1,
const unsigned int patchSize,
const unsigned int width0,
const unsigned int height0,
const unsigned int width1,
const unsigned int height1,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int centerX1,
const unsigned int centerY1,
const unsigned int image0PaddingElements,
const unsigned int image1PaddingElements);
190 template <
unsigned int tChannels,
unsigned int tPatchSize>
191 inline uint32_t
ZeroMeanSumSquareDifferencesBase::patch8BitPerChannelTemplate(
const uint8_t*
const image0,
const uint8_t*
const image1,
const unsigned int width0,
const unsigned int width1,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int centerX1,
const unsigned int centerY1,
const unsigned int image0PaddingElements,
const unsigned int image1PaddingElements)
193 static_assert(tChannels >= 1u,
"Invalid number of frame channels!");
194 static_assert(tPatchSize % 2u == 1u,
"Invalid patch size!");
196 ocean_assert(image0 !=
nullptr && image1 !=
nullptr);
198 constexpr
unsigned int tPatchSize_2 = tPatchSize / 2u;
200 ocean_assert(centerX0 >= tPatchSize_2 && centerY0 >= tPatchSize_2 && centerX0 < width0 - tPatchSize_2);
201 ocean_assert(centerX1 >= tPatchSize_2 && centerY1 >= tPatchSize_2 && centerX1 < width1 - tPatchSize_2);
203 ocean_assert(width0 >= tPatchSize);
204 ocean_assert(width1 >= tPatchSize);
206 const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
207 const unsigned int image1StrideElements = width1 * tChannels + image1PaddingElements;
209 return patch8BitPerChannelTemplate<tChannels, tPatchSize>(image0 + (centerY0 - tPatchSize_2) * image0StrideElements + (centerX0 - tPatchSize_2) * tChannels, image1 + (centerY1 - tPatchSize_2) * image1StrideElements + (centerX1 - tPatchSize_2) * tChannels, image0StrideElements, image1StrideElements);
212 template <
unsigned int tChannels,
unsigned int tPatchSize>
215 static_assert(tPatchSize % 2u == 1u,
"Invalid image patch size, need an odd value!");
216 static_assert(tChannels > 0u,
"Invalid number of frame channels!");
218 ocean_assert(patch0 !=
nullptr && patch1 !=
nullptr);
220 ocean_assert(patch0StrideElements >= tPatchSize * tChannels);
221 ocean_assert(patch1StrideElements >= tPatchSize * tChannels);
223 uint32_t sumMean0[tChannels] = {0u};
224 uint32_t sumMean1[tChannels] = {0u};
226 for (
unsigned int y = 0u; y < tPatchSize; ++y)
228 for (
unsigned int x = 0u; x < tPatchSize; ++x)
230 for (
unsigned int c = 0u; c < tChannels; c++)
232 sumMean0[c] += patch0[c];
233 sumMean1[c] += patch1[c];
240 patch0 += patch0StrideElements - tPatchSize * tChannels;
241 patch1 += patch1StrideElements - tPatchSize * tChannels;
244 uint8_t mean0[tChannels];
245 uint8_t mean1[tChannels];
247 for (
unsigned int n = 0u; n < tChannels; ++n)
249 mean0[n] = uint8_t((sumMean0[n] + (tPatchSize * tPatchSize / 2u)) / (tPatchSize * tPatchSize));
250 mean1[n] = uint8_t((sumMean1[n] + (tPatchSize * tPatchSize / 2u)) / (tPatchSize * tPatchSize));
253 patch0 -= patch0StrideElements * tPatchSize;
254 patch1 -= patch1StrideElements * tPatchSize;
259 for (
unsigned int y = 0u; y < tPatchSize; ++y)
261 for (
unsigned int x = 0u; x < tPatchSize; ++x)
263 for (
unsigned int c = 0u; c < tChannels; c++)
265 value = int16_t(patch0[c] - mean0[c]) - int16_t(patch1[c] - mean1[c]);
266 result += value * value;
273 patch0 += patch0StrideElements - tPatchSize * tChannels;
274 patch1 += patch1StrideElements - tPatchSize * tChannels;
280 template <
unsigned int tChannels,
unsigned int tPatchSize>
283 static_assert(tChannels >= 1u,
"Invalid number of frame channels!");
284 static_assert(tPatchSize % 2u == 1u,
"Invalid patch size!");
286 ocean_assert(image0 !=
nullptr && buffer1 !=
nullptr);
288 constexpr
unsigned int tPatchSize_2 = tPatchSize / 2u;
290 ocean_assert(centerX0 >= tPatchSize_2 && centerY0 >= tPatchSize_2 && centerX0 < width0 - tPatchSize_2);
292 ocean_assert(width0 >= tPatchSize);
294 const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
296 return patchBuffer8BitPerChannelTemplate<tChannels, tPatchSize>(image0 + (centerY0 - tPatchSize_2) * image0StrideElements + (centerX0 - tPatchSize_2) * tChannels, buffer1, image0StrideElements);
299 template <
unsigned int tChannels,
unsigned int tPatchSize>
302 return patch8BitPerChannelTemplate<tChannels, tPatchSize>(patch0, buffer1, patch0StrideElements, tChannels * tPatchSize);
305 template <
unsigned int tChannels,
unsigned int tPixels>
308 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
309 static_assert(tPixels != 0u,
"Invalid image buffer size!");
311 ocean_assert(buffer0 !=
nullptr && buffer1 !=
nullptr);
313 uint32_t sumMean0[tChannels] = {0u};
314 uint32_t sumMean1[tChannels] = {0u};
316 for (
unsigned int n = 0u; n < tPixels; ++n)
318 for (
unsigned int c = 0u; c < tChannels; ++c)
320 sumMean0[c] += buffer0[c];
321 sumMean1[c] += buffer1[c];
324 buffer0 += tChannels;
325 buffer1 += tChannels;
328 uint8_t mean0[tChannels];
329 uint8_t mean1[tChannels];
331 for (
unsigned int c = 0u; c < tChannels; ++c)
333 mean0[c] = uint8_t((sumMean0[c] + tPixels / 2u) / tPixels);
334 mean1[c] = uint8_t((sumMean1[c] + tPixels / 2u) / tPixels);
337 buffer0 -= tChannels * tPixels;
338 buffer1 -= tChannels * tPixels;
343 for (
unsigned int n = 0u; n < tPixels; ++n)
345 for (
unsigned int c = 0u; c < tChannels; ++c)
347 value = int16_t(buffer0[c] - mean0[c]) - int16_t(buffer1[c] - mean1[c]);
348 ssd += uint32_t(value * value);
351 buffer0 += tChannels;
352 buffer1 += tChannels;
358 template <
unsigned int tChannels>
359 OCEAN_FORCE_INLINE uint32_t
ZeroMeanSumSquareDifferencesBase::patch8BitPerChannel(
const uint8_t*
const image0,
const uint8_t*
const image1,
const unsigned int patchSize,
const unsigned int width0,
const unsigned int width1,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int centerX1,
const unsigned int centerY1,
const unsigned int image0PaddingElements,
const unsigned int image1PaddingElements)
361 static_assert(tChannels >= 1u,
"Invalid channel number!");
363 ocean_assert(image0 !=
nullptr && image1 !=
nullptr);
364 ocean_assert(patchSize % 2u == 1u);
366 const unsigned int patchSize_2 = patchSize / 2u;
368 ocean_assert(centerX0 >= patchSize_2 && centerY0 >= patchSize_2 && centerX0 < width0 - patchSize_2);
369 ocean_assert(centerX1 >= patchSize_2 && centerY1 >= patchSize_2 && centerX1 < width1 - patchSize_2);
371 ocean_assert(width0 >= patchSize_2);
372 ocean_assert(width1 >= patchSize_2);
374 const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
375 const unsigned int image1StrideElements = width1 * tChannels + image1PaddingElements;
377 return patch8BitPerChannel<tChannels>(image0 + (centerY0 - patchSize_2) * image0StrideElements + (centerX0 - patchSize_2) * tChannels, image1 + (centerY1 - patchSize_2) * image1StrideElements + (centerX1 - patchSize_2) * tChannels, patchSize, image0StrideElements, image1StrideElements);
380 template <
unsigned int tChannels>
383 static_assert(tChannels >= 1u,
"Invalid channel number!");
385 ocean_assert(patch0 !=
nullptr && patch1 !=
nullptr);
386 ocean_assert(patchSize % 2u == 1u);
388 ocean_assert(patch0StrideElements >= patchSize * tChannels);
389 ocean_assert(patch1StrideElements >= patchSize * tChannels);
391 uint32_t sumMean0[tChannels] = {0u};
392 uint32_t sumMean1[tChannels] = {0u};
394 for (
unsigned int y = 0u; y < patchSize; ++y)
396 for (
unsigned int x = 0u; x < patchSize; ++x)
398 for (
unsigned int c = 0u; c < tChannels; c++)
400 sumMean0[c] += patch0[c];
401 sumMean1[c] += patch1[c];
408 patch0 += patch0StrideElements - patchSize * tChannels;
409 patch1 += patch1StrideElements - patchSize * tChannels;
412 uint8_t mean0[tChannels];
413 uint8_t mean1[tChannels];
415 for (
unsigned int n = 0u; n < tChannels; ++n)
417 mean0[n] = uint8_t((sumMean0[n] + (patchSize * patchSize / 2u)) / (patchSize * patchSize));
418 mean1[n] = uint8_t((sumMean1[n] + (patchSize * patchSize / 2u)) / (patchSize * patchSize));
421 patch0 -= patch0StrideElements * patchSize;
422 patch1 -= patch1StrideElements * patchSize;
427 for (
unsigned int y = 0u; y < patchSize; ++y)
429 for (
unsigned int x = 0u; x < patchSize; ++x)
431 for (
unsigned int c = 0u; c < tChannels; c++)
433 value = int16_t(patch0[c] - mean0[c]) - int16_t(patch1[c] - mean1[c]);
434 result += value * value;
441 patch0 += patch0StrideElements - patchSize * tChannels;
442 patch1 += patch1StrideElements - patchSize * tChannels;
448 template <
unsigned int tChannels>
451 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
453 ocean_assert(buffer0 !=
nullptr && buffer1 !=
nullptr);
454 ocean_assert(pixels >= 1u);
456 uint32_t sumMean0[tChannels] = {0u};
457 uint32_t sumMean1[tChannels] = {0u};
459 for (
unsigned int n = 0u; n < pixels; ++n)
461 for (
unsigned int c = 0u; c < tChannels; ++c)
463 sumMean0[c] += buffer0[c];
464 sumMean1[c] += buffer1[c];
467 buffer0 += tChannels;
468 buffer1 += tChannels;
471 uint8_t mean0[tChannels];
472 uint8_t mean1[tChannels];
474 for (
unsigned int c = 0u; c < tChannels; ++c)
476 mean0[c] = uint8_t((sumMean0[c] + pixels / 2u) / pixels);
477 mean1[c] = uint8_t((sumMean1[c] + pixels / 2u) / pixels);
480 buffer0 -= tChannels * pixels;
481 buffer1 -= tChannels * pixels;
486 for (
unsigned int n = 0u; n < pixels; ++n)
488 for (
unsigned int c = 0u; c < tChannels; ++c)
490 value = int16_t(buffer0[c] - mean0[c]) - int16_t(buffer1[c] - mean1[c]);
491 ssd += uint32_t(value * value);
494 buffer0 += tChannels;
495 buffer1 += tChannels;
501 template <
unsigned int tChannels>
504 static_assert(tChannels >= 1u,
"Invalid channel number!");
506 ocean_assert(image0 !=
nullptr && buffer1 !=
nullptr);
508 ocean_assert(patchSize % 2u == 1u);
510 const unsigned int patchSize_2 = patchSize / 2u;
512 ocean_assert(centerX0 >= patchSize_2 && centerY0 >= patchSize_2 && centerX0 < width0 - patchSize_2);
514 ocean_assert(width0 >= patchSize);
516 const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
518 return patchBuffer8BitPerChannel<tChannels>(image0 + (centerY0 - patchSize_2) * image0StrideElements + (centerX0 - patchSize_2) * tChannels, buffer1, patchSize, image0StrideElements);
521 template <
unsigned int tChannels>
524 return patch8BitPerChannel<tChannels>(patch0, buffer1, patchSize, patch0StrideElements, tChannels * patchSize);
527 template <
unsigned int tChannels>
528 uint32_t
ZeroMeanSumSquareDifferencesBase::patchMirroredBorder8BitPerChannel(
const uint8_t* image0,
const uint8_t* image1,
const unsigned int patchSize,
const unsigned int width0,
const unsigned int height0,
const unsigned int width1,
const unsigned int height1,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int centerX1,
const unsigned int centerY1,
const unsigned int image0PaddingElements,
const unsigned int image1PaddingElements)
530 static_assert(tChannels != 0u,
"Invalid number of data channels!");
532 ocean_assert(image0 !=
nullptr && image1 !=
nullptr);
533 ocean_assert(patchSize % 2u == 1u);
535 const unsigned int patchSize_2 = patchSize / 2u;
537 ocean_assert(width0 >= patchSize_2);
538 ocean_assert(width1 >= patchSize_2);
540 ocean_assert(centerX0 < width0 && centerY0 < height0);
541 ocean_assert(centerX1 < width1 && centerY1 < height1);
543 const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
544 const unsigned int image1StrideElements = width1 * tChannels + image1PaddingElements;
546 const int left0 = int(centerX0 - patchSize_2);
547 const int top0 = int(centerY0 - patchSize_2);
549 const int left1 = int(centerX1 - patchSize_2);
550 const int top1 = int(centerY1 - patchSize_2);
552 const uint8_t* i0 = image0 + top0 * int(image0StrideElements) + left0 * int(tChannels);
553 const uint8_t* i1 = image1 + top1 * int(image1StrideElements) + left1 * int(tChannels);
555 unsigned int mean0[tChannels] = {0u};
556 unsigned int mean1[tChannels] = {0u};
558 unsigned int y0 = centerY0 - patchSize_2;
559 unsigned int y1 = centerY1 - patchSize_2;
561 const uint8_t*
const i0End = i0 + patchSize * image0StrideElements;
565 ocean_assert(i0 < i0End);
570 unsigned int x0 = centerX0 - patchSize_2;
571 unsigned int x1 = centerX1 - patchSize_2;
573 const uint8_t*
const c0RowEnd = c0 + patchSize * tChannels;
575 while (c0 != c0RowEnd)
577 ocean_assert(c0 < c0RowEnd);
579 if (x0 < width0 && x1 < width1)
583 for (
unsigned int n = 0u; n < tChannels; ++n)
592 else if (x0 < width0)
596 ocean_assert(x1 >= width1);
600 for (
unsigned int n = 0u; n < tChannels; ++n)
609 else if (x1 < width1)
613 ocean_assert(x0 >= width0);
617 for (
unsigned int n = 0u; n < tChannels; ++n)
630 ocean_assert(x0 >= width0 && x1 >= width1);
635 for (
unsigned int n = 0u; n < tChannels; ++n)
649 i0 += image0StrideElements;
650 i1 += image1StrideElements;
656 for (
unsigned int n = 0u; n < tChannels; ++n)
658 mean0[n] = (mean0[n] + (patchSize * patchSize / 2u)) / (patchSize * patchSize);
659 mean1[n] = (mean1[n] + (patchSize * patchSize / 2u)) / (patchSize * patchSize);
662 y0 = centerY0 - patchSize_2;
663 y1 = centerY1 - patchSize_2;
665 i0 = image0 + top0 * int(image0StrideElements) + left0 * int(tChannels);
666 i1 = image1 + top1 * int(image1StrideElements) + left1 * int(tChannels);
673 ocean_assert(i0 < i0End);
678 unsigned int x0 = centerX0 - patchSize_2;
679 unsigned int x1 = centerX1 - patchSize_2;
681 const uint8_t*
const c0RowEnd = c0 + patchSize * tChannels;
683 while (c0 != c0RowEnd)
685 ocean_assert(c0 < c0RowEnd);
687 if (x0 < width0 && x1 < width1)
691 for (
unsigned int n = 0u; n < tChannels; ++n)
693 value = (c0[n] - mean0[n]) - (c1[n] - mean1[n]);
694 zmssd += value * value;
700 else if (x0 < width0)
704 ocean_assert(x1 >= width1);
708 for (
unsigned int n = 0u; n < tChannels; ++n)
710 value = (c0[n] - mean0[n]) - (m1[n] - mean1[n]);
711 zmssd += value * value;
717 else if (x1 < width1)
721 ocean_assert(x0 >= width0);
725 for (
unsigned int n = 0u; n < tChannels; ++n)
727 value = (m0[n] - mean0[n]) - (c1[n] - mean1[n]);
728 zmssd += value * value;
738 ocean_assert(x0 >= width0 && x1 >= width1);
743 for (
unsigned int n = 0u; n < tChannels; ++n)
745 value = (m0[n] - mean0[n]) - (m1[n] - mean1[n]);
746 zmssd += value * value;
757 i0 += image0StrideElements;
758 i1 += image1StrideElements;
static int mirrorOffset(const unsigned int index, const unsigned int elements)
Deprecated.
Definition: CVUtilities.h:446
This class implements several zero-mean sum square differences functions based e.g....
Definition: ZeroMeanSumSquareDifferencesBase.h:25
static OCEAN_FORCE_INLINE uint32_t patchBuffer8BitPerChannel(const uint8_t *image0, unsigned int patchSize, const unsigned int width0, const unsigned int centerX0, const unsigned int centerY0, const unsigned int image0PaddingElements, const uint8_t *buffer1)
Returns the sum of square differences between a square image patch and a buffer.
Definition: ZeroMeanSumSquareDifferencesBase.h:502
static uint32_t patchMirroredBorder8BitPerChannel(const uint8_t *image0, const uint8_t *image1, const unsigned int patchSize, const unsigned int width0, const unsigned int height0, const unsigned int width1, const unsigned int height1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
Returns the zero-mean sum of square differences between two patches within an image,...
Definition: ZeroMeanSumSquareDifferencesBase.h:528
static uint32_t buffer8BitPerChannelTemplate(const uint8_t *buffer0, const uint8_t *buffer1)
Returns the zero-mean sum of square differences between two memory buffers.
Definition: ZeroMeanSumSquareDifferencesBase.h:306
static OCEAN_FORCE_INLINE uint32_t patch8BitPerChannel(const uint8_t *const image0, const uint8_t *const image1, const unsigned int patchSize, const unsigned int width0, const unsigned int width1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
Returns the zero-mean sum of square differences between two square patches.
Definition: ZeroMeanSumSquareDifferencesBase.h:359
static OCEAN_FORCE_INLINE uint32_t patchBuffer8BitPerChannelTemplate(const uint8_t *image0, const unsigned int width0, const unsigned int centerX0, const unsigned int centerY0, const unsigned int image0PaddingElements, const uint8_t *buffer1)
Returns the zero-mean sum of square differences between a square image patch and a buffer.
Definition: ZeroMeanSumSquareDifferencesBase.h:281
static OCEAN_FORCE_INLINE uint32_t patch8BitPerChannelTemplate(const uint8_t *const image0, const uint8_t *const image1, const unsigned int width0, const unsigned int width1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
Returns the zero-mean sum of square differences between two square patches.
static unsigned int buffer8BitPerChannel(const uint8_t *buffer0, const uint8_t *buffer1, const unsigned int pixels)
Returns the zero-mean sum of square differences between two memory buffers.
Definition: ZeroMeanSumSquareDifferencesBase.h:449
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15