8 #ifndef META_OCEAN_CV_SUM_SQUARE_DIFFERENCES_BASE_H
9 #define META_OCEAN_CV_SUM_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>
85 static uint32_t patchBuffer8BitPerChannelTemplate(
const uint8_t* patch0,
const uint8_t* buffer1,
const unsigned int patch0StrideElements);
94 template <
unsigned int tBufferSize>
95 static inline uint32_t buffer8BitPerChannelTemplate(
const uint8_t*
const buffer0,
const uint8_t*
const buffer1);
115 template <
unsigned int tChannels,
unsigned int tPatchSize>
116 static inline IndexPair32 patchAtBorder8BitPerChannelTemplate(
const uint8_t* image0,
const uint8_t* image1,
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);
136 template <
unsigned int tChannels>
137 static uint32_t patchMirroredBorder8BitPerChannelTemplate(
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);
155 static OCEAN_FORCE_INLINE uint32_t patch8BitPerChannel(
const uint8_t*
const image0,
const uint8_t*
const image1,
const unsigned int channels,
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);
167 static uint32_t
patch8BitPerChannel(
const uint8_t* patch0,
const uint8_t* patch1,
const unsigned int channels,
const unsigned int patchSize,
const unsigned int patch0StrideElements,
const unsigned int patch1StrideElements);
181 static OCEAN_FORCE_INLINE uint32_t patchBuffer8BitPerChannel(
const uint8_t* image0,
unsigned int channels,
unsigned int patchSize,
const unsigned int width0,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int image0PaddingElements,
const uint8_t* buffer1);
192 static inline uint32_t patchBuffer8BitPerChannel(
const uint8_t* patch0,
const uint8_t* buffer1,
unsigned int channels,
unsigned int patchSize,
const unsigned int patch0StrideElements);
201 static uint32_t
buffer8BitPerChannel(
const uint8_t*
const buffer0,
const uint8_t*
const buffer1,
const unsigned int bufferSize);
221 static IndexPair32 patchAtBorder8BitPerChannel(
const uint8_t* image0,
const uint8_t* image1,
const unsigned int channels,
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);
224 template <
unsigned int tChannels,
unsigned int tPatchSize>
225 OCEAN_FORCE_INLINE uint32_t
SumSquareDifferencesBase::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)
227 static_assert(tPatchSize % 2u == 1u,
"Invalid image patch size, need an odd value!");
228 static_assert(tChannels > 0u,
"Invalid number of frame channels!");
230 ocean_assert(image0 !=
nullptr && image1 !=
nullptr);
232 constexpr
unsigned int tPatchSize_2 = tPatchSize / 2u;
234 ocean_assert(centerX0 >= tPatchSize_2 && centerY0 >= tPatchSize_2 && centerX0 < width0 - tPatchSize_2);
235 ocean_assert(centerX1 >= tPatchSize_2 && centerY1 >= tPatchSize_2 && centerX1 < width1 - tPatchSize_2);
237 ocean_assert(width0 >= tPatchSize);
238 ocean_assert(width1 >= tPatchSize);
240 const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
241 const unsigned int image1StrideElements = width1 * tChannels + image1PaddingElements;
243 return patch8BitPerChannelTemplate<tChannels, tPatchSize>(image0 + (centerY0 - tPatchSize_2) * image0StrideElements + (centerX0 - tPatchSize_2) * tChannels, image1 + (centerY1 - tPatchSize_2) * image1StrideElements + (centerX1 - tPatchSize_2) * tChannels, image0StrideElements, image1StrideElements);
246 template <
unsigned int tChannels,
unsigned int tPatchSize>
249 static_assert(tPatchSize != 0u,
"Invalid image patch size, need an odd value!");
250 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
252 ocean_assert(patch0 !=
nullptr && patch1 !=
nullptr);
254 ocean_assert(patch0StrideElements >= tPatchSize * tChannels);
255 ocean_assert(patch1StrideElements >= tPatchSize * tChannels);
257 uint32_t result = 0u;
260 for (
unsigned int y = 0u; y < tPatchSize; ++y)
262 for (
unsigned int n = 0u; n < tPatchSize * tChannels; ++n)
264 value = int16_t(patch0[n]) - int16_t(patch1[n]);
265 result += value * value;
268 patch0 += patch0StrideElements;
269 patch1 += patch1StrideElements;
275 template <
unsigned int tChannels,
unsigned int tPatchSize>
278 static_assert(tChannels >= 1u,
"Invalid number of frame channels!");
279 static_assert(tPatchSize % 2u == 1u,
"Invalid patch size!");
281 ocean_assert(image0 !=
nullptr && buffer1 !=
nullptr);
283 constexpr
unsigned int tPatchSize_2 = tPatchSize / 2u;
285 ocean_assert(centerX0 >= tPatchSize_2 && centerY0 >= tPatchSize_2 && centerX0 < width0 - tPatchSize_2);
287 ocean_assert(width0 >= tPatchSize);
289 const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
291 return patchBuffer8BitPerChannelTemplate<tChannels, tPatchSize>(image0 + (centerY0 - tPatchSize_2) * image0StrideElements + (centerX0 - tPatchSize_2) * tChannels, buffer1, image0StrideElements);
294 template <
unsigned int tChannels,
unsigned int tPatchSize>
297 return patch8BitPerChannelTemplate<tChannels, tPatchSize>(patch0, buffer1, patch0StrideElements, tChannels * tPatchSize);
300 template <
unsigned int tBufferSize>
303 static_assert(tBufferSize != 0u,
"Invalid number of frame channels!");
305 ocean_assert(buffer0 !=
nullptr && buffer1 !=
nullptr);
310 for (
unsigned int n = 0u; n < tBufferSize; ++n)
312 difference = int16_t(buffer0[n]) - int16_t(buffer1[n]);
313 ssd += uint32_t(difference * difference);
319 template <
unsigned int tChannels,
unsigned int tPatchSize>
320 inline IndexPair32 SumSquareDifferencesBase::patchAtBorder8BitPerChannelTemplate(
const uint8_t* image0,
const uint8_t* image1,
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)
322 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
323 static_assert(tPatchSize % 2u == 1u,
"Invalid image patch size, must be odd!");
325 ocean_assert(image0 !=
nullptr && image1 !=
nullptr);
327 const unsigned int tPatchSize_2 = tPatchSize / 2u;
329 ocean_assert(width0 >= tPatchSize_2 + 1u && height0 >= tPatchSize_2 + 1u);
330 ocean_assert(width1 >= tPatchSize_2 + 1u && height1 >= tPatchSize_2 + 1u);
332 ocean_assert(centerX0 < width0 && centerY0 < height0);
333 ocean_assert(centerX1 < width1 && centerY1 < height1);
335 ocean_assert(centerX0 - tPatchSize_2 < width0 - (tPatchSize - 1u) == (centerX0 >= tPatchSize_2 && centerX0 < width0 - tPatchSize_2));
336 ocean_assert(centerY0 - tPatchSize_2 < height0 - (tPatchSize - 1u) == (centerY0 >= tPatchSize_2 && centerY0 < height0 - tPatchSize_2));
337 ocean_assert(centerX1 - tPatchSize_2 < width1 - (tPatchSize - 1u) == (centerX1 >= tPatchSize_2 && centerX1 < width1 - tPatchSize_2));
338 ocean_assert(centerY1 - tPatchSize_2 < height1 - (tPatchSize - 1u) == (centerY1 >= tPatchSize_2 && centerY1 < height1 - tPatchSize_2));
340 if (centerX0 - tPatchSize_2 < width0 - (tPatchSize - 1u) && centerY0 - tPatchSize_2 < height0 - (tPatchSize - 1u) && centerX1 - tPatchSize_2 < width1 - (tPatchSize - 1u) && centerY1 - tPatchSize_2 < height1 - (tPatchSize - 1u))
342 const uint32_t ssd = patch8BitPerChannelTemplate<tChannels, tPatchSize>(image0, image1, width0, width1, centerX0, centerY0, centerX1, centerY1, image0PaddingElements, image1PaddingElements);
347 const unsigned int offsetLeft0 = min(centerX0, tPatchSize_2);
348 const unsigned int offsetTop0 = min(centerY0, tPatchSize_2);
349 const unsigned int offsetRight0 = min(width0 - centerX0 - 1u, tPatchSize_2);
350 const unsigned int offsetBottom0 = min(height0 - centerY0 - 1u, tPatchSize_2);
352 const unsigned int offsetLeft1 = min(centerX1, tPatchSize_2);
353 const unsigned int offsetTop1 = min(centerY1, tPatchSize_2);
354 const unsigned int offsetRight1 = min(width1 - centerX1 - 1u, tPatchSize_2);
355 const unsigned int offsetBottom1 = min(height1 - centerY1 - 1u, tPatchSize_2);
357 if (offsetLeft1 < offsetLeft0 || offsetRight1 < offsetRight0 || offsetTop1 < offsetTop0 || offsetBottom1 < offsetBottom0)
363 const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
364 const unsigned int image1StrideElements = width1 * tChannels + image1PaddingElements;
366 const unsigned int columns = offsetRight0 + 1u + offsetLeft0;
367 const unsigned int rows = offsetBottom0 + 1u + offsetTop0;
369 const uint8_t* patch0 = image0 + (centerY0 - offsetTop0) * image0StrideElements + (centerX0 - offsetLeft0) * tChannels;
370 const uint8_t* patch1 = image1 + (centerY1 - offsetTop0) * image1StrideElements + (centerX1 - offsetLeft0) * tChannels;
372 unsigned int ssd = 0u;
374 for (
unsigned int y = 0u; y < rows; ++y)
376 for (
unsigned int x = 0u; x < columns; ++x)
378 for (
unsigned int n = 0u; n < tChannels; ++n)
380 ssd +=
sqrDistance(patch0[x * tChannels + n], patch1[x * tChannels + n]);
384 patch0 += image0StrideElements;
385 patch1 += image1StrideElements;
391 template <
unsigned int tChannels>
392 uint32_t
SumSquareDifferencesBase::patchMirroredBorder8BitPerChannelTemplate(
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)
394 static_assert(tChannels != 0u,
"Invalid number of data channels!");
396 ocean_assert(image0 !=
nullptr && image1 !=
nullptr);
397 ocean_assert(patchSize % 2u == 1u);
399 const unsigned int patchSize_2 = patchSize / 2u;
401 ocean_assert(width0 >= patchSize_2);
402 ocean_assert(width1 >= patchSize_2);
404 ocean_assert(centerX0 < width0 && centerY0 < height0);
405 ocean_assert(centerX1 < width1 && centerY1 < height1);
407 const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
408 const unsigned int image1StrideElements = width1 * tChannels + image1PaddingElements;
410 const int left0 = int(centerX0 - patchSize_2);
411 const int top0 = int(centerY0 - patchSize_2);
413 const int left1 = int(centerX1 - patchSize_2);
414 const int top1 = int(centerY1 - patchSize_2);
416 const uint8_t* i0 = image0 + top0 * int(image0StrideElements) + int(left0) * int(tChannels);
417 const uint8_t* i1 = image1 + top1 * int(image1StrideElements) + int(left1) * int(tChannels);
422 unsigned int y0 = centerY0 - patchSize_2;
423 unsigned int y1 = centerY1 - patchSize_2;
425 const uint8_t*
const i0End = i0 + patchSize * image0StrideElements;
429 ocean_assert(i0 < i0End);
434 unsigned int x0 = centerX0 - patchSize_2;
435 unsigned int x1 = centerX1 - patchSize_2;
437 const uint8_t*
const c0RowEnd = c0 + patchSize * tChannels;
439 while (c0 != c0RowEnd)
441 ocean_assert(c0 < c0RowEnd);
443 if (x0 < width0 && x1 < width1)
447 for (
unsigned int n = 0u; n < tChannels; ++n)
449 value = *c0++ - *c1++;
450 ssd += value * value;
453 else if (x0 < width0)
457 ocean_assert(x1 >= width1);
461 for (
unsigned int n = 0u; n < tChannels; ++n)
463 value = *c0++ - *m1++;
464 ssd += value * value;
469 else if (x1 < width1)
473 ocean_assert(x0 >= width0);
477 for (
unsigned int n = 0u; n < tChannels; ++n)
479 value = *m0++ - *c1++;
480 ssd += value * value;
489 ocean_assert(x0 >= width0 && x1 >= width1);
494 for (
unsigned int n = 0u; n < tChannels; ++n)
496 value = *m0++ - *m1++;
497 ssd += value * value;
508 i0 += image0StrideElements;
509 i1 += image1StrideElements;
518 OCEAN_FORCE_INLINE uint32_t
SumSquareDifferencesBase::patch8BitPerChannel(
const uint8_t* image0,
const uint8_t* image1,
const unsigned int channels,
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)
520 ocean_assert(image0 !=
nullptr && image1 !=
nullptr);
522 ocean_assert(patchSize % 2u == 1u);
523 ocean_assert(channels > 0u);
525 const unsigned int patchSize_2 = patchSize / 2u;
527 ocean_assert(centerX0 >= patchSize_2 && centerY0 >= patchSize_2 && centerX0 < width0 - patchSize_2);
528 ocean_assert(centerX1 >= patchSize_2 && centerY1 >= patchSize_2 && centerX1 < width1 - patchSize_2);
530 ocean_assert(width0 >= patchSize_2);
531 ocean_assert(width1 >= patchSize_2);
533 const unsigned int image0StrideElements = width0 * channels + image0PaddingElements;
534 const unsigned int image1StrideElements = width1 * channels + image1PaddingElements;
536 return patch8BitPerChannel(image0 + (centerY0 - patchSize_2) * image0StrideElements + (centerX0 - patchSize_2) * channels, image1 + (centerY1 - patchSize_2) * image1StrideElements + (centerX1 - patchSize_2) * channels, channels, patchSize, image0StrideElements, image1StrideElements);
539 OCEAN_FORCE_INLINE uint32_t
SumSquareDifferencesBase::patchBuffer8BitPerChannel(
const uint8_t* image0,
unsigned int channels,
unsigned int patchSize,
const unsigned int width0,
const unsigned int centerX0,
const unsigned int centerY0,
const unsigned int image0PaddingElements,
const uint8_t* buffer1)
541 ocean_assert(image0 !=
nullptr && buffer1 !=
nullptr);
543 ocean_assert(channels >= 1u);
544 ocean_assert(patchSize % 2u == 1u);
546 const unsigned int patchSize_2 = patchSize / 2u;
548 ocean_assert(centerX0 >= patchSize_2 && centerY0 >= patchSize_2 && centerX0 < width0 - patchSize_2);
550 ocean_assert(width0 >= patchSize);
552 const unsigned int image0StrideElements = width0 * channels + image0PaddingElements;
554 return patchBuffer8BitPerChannel(image0 + (centerY0 - patchSize_2) * image0StrideElements + (centerX0 - patchSize_2) * channels, buffer1, channels, patchSize, image0StrideElements);
559 return patch8BitPerChannel(patch0, buffer1, channels, patchSize, patch0StrideElements, channels * patchSize);
static int mirrorOffset(const unsigned int index, const unsigned int elements)
Deprecated.
Definition: CVUtilities.h:446
This class implements several sum square differences functions based e.g., on template parameters.
Definition: SumSquareDifferencesBase.h:25
static OCEAN_FORCE_INLINE uint32_t patch8BitPerChannel(const uint8_t *const image0, const uint8_t *const image1, const unsigned int channels, 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 sum of square differences between two square patches.
Definition: SumSquareDifferencesBase.h:518
static IndexPair32 patchAtBorder8BitPerChannelTemplate(const uint8_t *image0, const uint8_t *image1, 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 sum of square differences between two square image patches which can be partially outside...
Definition: SumSquareDifferencesBase.h:320
static uint32_t patch8BitPerChannel(const uint8_t *patch0, const uint8_t *patch1, const unsigned int channels, const unsigned int patchSize, const unsigned int patch0StrideElements, const unsigned int patch1StrideElements)
Returns the sum of square differences between two square patches.
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 sum of square differences between a square image patch and a buffer.
Definition: SumSquareDifferencesBase.h:276
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 sum of square differences between two square patches.
Definition: SumSquareDifferencesBase.h:225
static OCEAN_FORCE_INLINE uint32_t patchBuffer8BitPerChannel(const uint8_t *image0, unsigned int channels, 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: SumSquareDifferencesBase.h:539
static uint32_t buffer8BitPerChannel(const uint8_t *const buffer0, const uint8_t *const buffer1, const unsigned int bufferSize)
Returns the sum of square differences between two individual buffers.
static IndexPair32 patchAtBorder8BitPerChannel(const uint8_t *image0, const uint8_t *image1, const unsigned int channels, 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 sum of square differences between two square image patches which can be partially outside...
static uint32_t buffer8BitPerChannelTemplate(const uint8_t *const buffer0, const uint8_t *const buffer1)
Returns the sum of square differences between two individual buffers.
Definition: SumSquareDifferencesBase.h:301
static uint32_t patchMirroredBorder8BitPerChannelTemplate(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 sum of square differences between two patches within an image, patch pixels outside the i...
Definition: SumSquareDifferencesBase.h:392
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition: base/Utilities.h:1089
std::pair< Index32, Index32 > IndexPair32
Definition of a pair holding 32 bit indices.
Definition: Base.h:138
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15