8 #ifndef META_OCEAN_CV_ADVANCED_ADVANCED_ZERO_MEAN_SUM_SQUARE_DIFFERENCES_BASE_H
9 #define META_OCEAN_CV_ADVANCED_ADVANCED_ZERO_MEAN_SUM_SQUARE_DIFFERENCES_BASE_H
50 template <
unsigned int tChannels,
unsigned int tPatchSize>
51 static uint32_t
patch8BitPerChannelTemplate(
const uint8_t* image0,
const uint8_t* image1,
const unsigned int width0,
const unsigned int width1,
const Scalar centerX0,
const Scalar centerY0,
const Scalar centerX1,
const Scalar centerY1,
const unsigned int image0PaddingElements,
const unsigned int image1PaddingElements);
69 template <
unsigned int tChannels,
unsigned int tPatchSize>
70 static uint32_t
patch8BitPerChannelTemplate(
const uint8_t* image0,
const uint8_t* image1,
const unsigned int width0,
const unsigned int width1,
const unsigned int centerX0,
const unsigned int centerY0,
const Scalar centerX1,
const Scalar centerY1,
const unsigned int image0PaddingElements,
const unsigned int image1PaddingElements);
94 template <
unsigned int tChannels>
95 static inline IndexPair32 determine8BitPerChannelPartialTemplate(
const uint8_t* frame0,
const uint8_t* frame1,
const uint8_t* mask0,
const uint8_t* mask1,
const unsigned int width0,
const unsigned int height0,
const unsigned int width1,
const unsigned int height1,
unsigned int sizeX,
unsigned int sizeY,
const int left0,
const int top0,
const int left1,
const int top1);
113 template <
unsigned int tChannels>
114 static inline unsigned int sum8BitPerChannelPartialTemplate(
const uint8_t* frame,
const uint8_t* mask,
const unsigned int width,
const unsigned int height,
const unsigned int sizeX,
const unsigned int sizeY,
const int left,
const int top,
unsigned int sums[tChannels]);
137 template <
unsigned int tChannels>
138 static inline unsigned int sum8BitPerChannelPartialTemplate(
const uint8_t* frame0,
const uint8_t* frame1,
const uint8_t* mask0,
const uint8_t* mask1,
const unsigned int width0,
const unsigned int height0,
const unsigned int width1,
const unsigned int height1,
unsigned int sizeX,
unsigned int sizeY,
const int left0,
const int top0,
const int left1,
const int top1,
unsigned int sums0[tChannels],
unsigned int sums1[tChannels]);
141 template <
unsigned int tChannels,
unsigned int tPatchSize>
144 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
145 static_assert(tPatchSize >= 1u && tPatchSize % 2u == 1u,
"Invalid image patch size, must be odd!");
147 ocean_assert(image0 !=
nullptr && image1 !=
nullptr);
149 ocean_assert(width0 >= tPatchSize + 1u);
150 ocean_assert(width1 >= tPatchSize + 1u);
152 constexpr
unsigned int tPatchSize_2 = tPatchSize / 2u;
154 ocean_assert(centerX0 >=
Scalar(tPatchSize_2) && centerX0 <
Scalar(width0 - tPatchSize_2 - 1u));
155 ocean_assert_and_suppress_unused(centerY0 >=
Scalar(tPatchSize_2), tPatchSize_2);
157 ocean_assert(centerX1 >=
Scalar(tPatchSize_2) && centerX1 <
Scalar(width1 - tPatchSize_2 - 1u));
158 ocean_assert(centerY1 >=
Scalar(tPatchSize_2));
160 constexpr
unsigned int tPatchPixels = tPatchSize * tPatchSize;
161 constexpr
unsigned int tPatchElements = tPatchPixels * tChannels;
163 uint8_t target[tPatchElements * 2u];
165 AdvancedFrameInterpolatorBilinear::interpolateSquarePatch8BitPerChannel<tChannels, tPatchSize>(image0, width0, image0PaddingElements, target,
Vector2(centerX0, centerY0));
166 AdvancedFrameInterpolatorBilinear::interpolateSquarePatch8BitPerChannel<tChannels, tPatchSize>(image1, width1, image1PaddingElements, target + tPatchElements,
Vector2(centerX1, centerY1));
168 return ZeroMeanSumSquareDifferences::buffer8BitPerChannel<tChannels, tPatchPixels>(target, target + tPatchElements);
171 template <
unsigned int tChannels,
unsigned int tPatchSize>
174 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
175 static_assert(tPatchSize >= 1u && tPatchSize % 2u == 1u,
"Invalid image patch size, must be odd!");
177 ocean_assert(image0 !=
nullptr && image1 !=
nullptr);
179 ocean_assert(width0 >= tPatchSize);
180 ocean_assert(width1 >= tPatchSize + 1u);
182 constexpr
unsigned int tPatchSize_2 = tPatchSize / 2u;
184 ocean_assert(centerX0 >= tPatchSize_2 && centerX0 < width0 - tPatchSize_2);
185 ocean_assert_and_suppress_unused(centerY0 >= tPatchSize_2, tPatchSize_2);
187 ocean_assert(centerX1 >=
Scalar(tPatchSize_2) && centerX1 <
Scalar(width1 - tPatchSize_2 - 1u));
188 ocean_assert(centerY1 >=
Scalar(tPatchSize_2));
190 constexpr
unsigned int tPatchPixels = tPatchSize * tPatchSize;
191 constexpr
unsigned int tPatchElements = tPatchPixels * tChannels;
193 uint8_t target[tPatchElements * 2u];
195 constexpr
unsigned int targetPaddingElements = 0u;
198 AdvancedFrameInterpolatorBilinear::interpolateSquarePatch8BitPerChannel<tChannels, tPatchSize>(image1, width1, image1PaddingElements, target + tPatchElements,
Vector2(centerX1, centerY1));
200 return ZeroMeanSumSquareDifferences::buffer8BitPerChannel<tChannels, tPatchPixels>(target, target + tPatchElements);
203 template <
unsigned int tChannels>
204 inline IndexPair32 AdvancedZeroMeanSumSquareDifferencesBase::determine8BitPerChannelPartialTemplate(
const uint8_t* frame0,
const uint8_t* frame1,
const uint8_t* mask0,
const uint8_t* mask1,
const unsigned int width0,
const unsigned int height0,
const unsigned int width1,
const unsigned int height1,
const unsigned int sizeX,
const unsigned int sizeY,
const int left0,
const int top0,
const int left1,
const int top1)
206 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
208 ocean_assert(frame0 && frame1);
209 ocean_assert(mask0 && mask1);
211 ocean_assert(sizeX >= 1u && sizeY >= 1u);
212 ocean_assert(width0 >= sizeX && width1 >= sizeX);
213 ocean_assert(height0 >= sizeY && height1 >= sizeY);
215 ocean_assert(left0 >= -
int(sizeX) + 1 && left0 <
int(width0));
216 ocean_assert(top0 >= -
int(sizeY) + 1 && top0 <
int(height0));
217 ocean_assert(left1 >= -
int(sizeX) + 1 && left1 <
int(width1));
218 ocean_assert(top1 >= -
int(sizeY) + 1 && top1 <
int(height1));
220 const int patchLeftBorder = max(0, max(-left0, -left1));
221 const int patchTopBorder = max(0, max(-top0, -top1));
223 const int patchRightBorder = max(0, max(left0 +
int(sizeX) -
int(width0), left1 +
int(sizeX) -
int(width1)));
224 const int patchBottomBorder = max(0, max(top0 +
int(sizeY) -
int(height0), top1 +
int(sizeY) -
int(height1)));
226 ocean_assert(patchLeftBorder >= 0 && patchLeftBorder <
int(sizeX));
227 ocean_assert(patchTopBorder >= 0 && patchTopBorder <
int(sizeY));
228 ocean_assert(patchRightBorder >= 0 && patchRightBorder <
int(sizeX));
229 ocean_assert(patchBottomBorder >= 0 && patchBottomBorder <
int(sizeY));
231 const int pLeft0 = left0 + patchLeftBorder;
232 const int pLeft1 = left1 + patchLeftBorder;
234 const int pTop0 = top0 + patchTopBorder;
235 const int pTop1 = top1 + patchTopBorder;
237 const unsigned int pSizeX = sizeX - patchLeftBorder - patchRightBorder;
238 const unsigned int pSizeY = sizeY - patchTopBorder - patchBottomBorder;
240 if (pSizeX > sizeX || pSizeY > sizeY)
245 unsigned int means0[tChannels] = {0u};
246 unsigned int means1[tChannels] = {0u};
248 const unsigned int pixels = sum8BitPerChannelPartialTemplate<tChannels>(frame0, frame1, mask0, mask1, width0, height0, width1, height1, sizeX, sizeY, left0, top0, left1, top1, means0, means1);
255 int means1_0[tChannels];
257 for (
unsigned int n = 0u; n < tChannels; ++n)
258 means1_0[n] =
int((means1[n] + (pixels / 2u)) / pixels) - int((means0[n] + (pixels / 2u)) / pixels);
260 unsigned int ssd = 0u;
262 frame0 += (pTop0 * int(width0) + pLeft0) *
int(tChannels);
263 frame1 += (pTop1 * int(width1) + pLeft1) *
int(tChannels);
265 mask0 += (pTop0 * int(width0) + pLeft0);
266 mask1 += (pTop1 * int(width1) + pLeft1);
268 const uint8_t*
const mask0End = mask0 + pSizeY * width0;
270 while (mask0 != mask0End)
272 ocean_assert(mask0 < mask0End);
274 const uint8_t*
const mask0RowEnd = mask0 + pSizeX;
276 while (mask0 != mask0RowEnd)
278 ocean_assert(mask0 < mask0End);
279 ocean_assert(mask0 < mask0RowEnd);
281 const unsigned int factor = (*mask0 * *mask1) != 0u;
283 for (
unsigned int n = 0u; n < tChannels; ++n)
284 ssd +=
sqr(
int(frame0[n]) - int(frame1[n]) + means1_0[n]) * factor;
293 frame0 += (width0 - pSizeX) * tChannels;
294 frame1 += (width1 - pSizeX) * tChannels;
296 mask0 += (width0 - pSizeX);
297 mask1 += (width1 - pSizeX);
300 return std::make_pair(ssd, pixels);
303 template <
unsigned int tChannels>
306 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
308 ocean_assert(frame && mask);
309 ocean_assert(width >= 1u && height >= 1u);
310 ocean_assert(sizeX >= 1u && sizeY >= 1u);
312 ocean_assert(width >= sizeX && height >= sizeY);
313 ocean_assert(left >= -
int(sizeX) + 1 && left <
int(width));
314 ocean_assert(top >= -
int(sizeY) + 1 && top <
int(height));
316 const int patchLeftBorder = max(0, -left);
317 const int patchTopBorder = max(0, -top);
319 const int patchRightBorder = max(0, left +
int(sizeX) -
int(width));
320 const int patchBottomBorder = max(0, top +
int(sizeY) -
int(height));
322 ocean_assert(patchLeftBorder >= 0 && patchLeftBorder <
int(sizeX));
323 ocean_assert(patchTopBorder >= 0 && patchTopBorder <
int(sizeY));
324 ocean_assert(patchRightBorder >= 0 && patchRightBorder <
int(sizeX));
325 ocean_assert(patchBottomBorder >= 0 && patchBottomBorder <
int(sizeY));
327 const int pLeft = left + patchLeftBorder;
328 const int pTop = top + patchTopBorder;
330 const unsigned int pSizeX = sizeX - patchLeftBorder - patchRightBorder;
331 const unsigned int pSizeY = sizeY - patchTopBorder - patchBottomBorder;
333 if (pSizeX > sizeX || pSizeY > sizeY)
336 unsigned int pixels = 0u;
338 for (
unsigned int n = 0u; n < tChannels; ++n)
341 frame += (pTop * int(width) + pLeft) *
int(tChannels);
342 mask += (pTop * int(width) + pLeft);
344 const uint8_t*
const maskEnd = mask + pSizeY * width;
346 while (mask != maskEnd)
348 ocean_assert(mask < maskEnd);
350 const uint8_t*
const maskRowEnd = mask + pSizeX;
352 while (mask != maskRowEnd)
354 ocean_assert(mask < maskEnd);
355 ocean_assert(mask < maskRowEnd);
357 const unsigned int factor = *mask != 0u;
359 for (
unsigned int n = 0u; n < tChannels; ++n)
360 sums[n] += frame[n] * factor;
368 frame += int(width - pSizeX) * int(tChannels);
369 mask += int(width - pSizeX);
375 template <
unsigned int tChannels>
376 inline unsigned int AdvancedZeroMeanSumSquareDifferencesBase::sum8BitPerChannelPartialTemplate(
const uint8_t* frame0,
const uint8_t* frame1,
const uint8_t* mask0,
const uint8_t* mask1,
const unsigned int width0,
const unsigned int height0,
const unsigned int width1,
const unsigned int height1,
unsigned int sizeX,
unsigned int sizeY,
const int left0,
const int top0,
const int left1,
const int top1,
unsigned int sums0[tChannels],
unsigned int sums1[tChannels])
378 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
380 ocean_assert(frame0 && frame1);
381 ocean_assert(mask0 && mask1);
383 ocean_assert(sizeX >= 1u && sizeY >= 1u);
384 ocean_assert(width0 >= sizeX && width1 >= sizeX);
385 ocean_assert(height0 >= sizeY && height1 >= sizeY);
387 ocean_assert(left0 >= -
int(sizeX) + 1 && left0 <
int(width0));
388 ocean_assert(top0 >= -
int(sizeY) + 1 && top0 <
int(height0));
389 ocean_assert(left1 >= -
int(sizeX) + 1 && left1 <
int(width1));
390 ocean_assert(top1 >= -
int(sizeY) + 1 && top1 <
int(height1));
392 const int patchLeftBorder = max(0, max(-left0, -left1));
393 const int patchTopBorder = max(0, max(-top0, -top1));
395 const int patchRightBorder = max(0, max(left0 +
int(sizeX) -
int(width0), left1 +
int(sizeX) -
int(width1)));
396 const int patchBottomBorder = max(0, max(top0 +
int(sizeY) -
int(height0), top1 +
int(sizeY) -
int(height1)));
398 ocean_assert(patchLeftBorder >= 0 && patchLeftBorder <
int(sizeX));
399 ocean_assert(patchTopBorder >= 0 && patchTopBorder <
int(sizeY));
400 ocean_assert(patchRightBorder >= 0 && patchRightBorder <
int(sizeX));
401 ocean_assert(patchBottomBorder >= 0 && patchBottomBorder <
int(sizeY));
403 const int pLeft0 = left0 + patchLeftBorder;
404 const int pLeft1 = left1 + patchLeftBorder;
406 const int pTop0 = top0 + patchTopBorder;
407 const int pTop1 = top1 + patchTopBorder;
409 const unsigned int pSizeX = sizeX - patchLeftBorder - patchRightBorder;
410 const unsigned int pSizeY = sizeY - patchTopBorder - patchBottomBorder;
412 if (pSizeX > sizeX || pSizeY > sizeY)
415 for (
unsigned int n = 0u; n < tChannels; ++n)
417 for (
unsigned int n = 0u; n < tChannels; ++n)
420 unsigned int pixels = 0u;
422 frame0 += (pTop0 * int(width0) + pLeft0) *
int(tChannels);
423 frame1 += (pTop1 * int(width1) + pLeft1) *
int(tChannels);
425 mask0 += (pTop0 * int(width0) + pLeft0);
426 mask1 += (pTop1 * int(width1) + pLeft1);
428 const uint8_t*
const mask0End = mask0 + pSizeY * width0;
430 while (mask0 != mask0End)
432 ocean_assert(mask0 < mask0End);
434 const uint8_t*
const mask0RowEnd = mask0 + pSizeX;
436 while (mask0 != mask0RowEnd)
438 ocean_assert(mask0 < mask0End);
439 ocean_assert(mask0 < mask0RowEnd);
441 const unsigned int factor = (*mask0 * *mask1) != 0u;
443 for (
unsigned int n = 0u; n < tChannels; ++n)
444 sums0[n] += frame0[n] * factor;
446 for (
unsigned int n = 0u; n < tChannels; ++n)
447 sums1[n] += frame1[n] * factor;
458 frame0 += int(width0 - pSizeX) * int(tChannels);
459 frame1 += int(width1 - pSizeX) * int(tChannels);
461 mask0 += int(width0 - pSizeX);
462 mask1 += int(width1 - pSizeX);
This class implements functions calculating the zero-mean sum of square differences.
Definition: AdvancedZeroMeanSumSquareDifferencesBase.h:31
static IndexPair32 determine8BitPerChannelPartialTemplate(const uint8_t *frame0, const uint8_t *frame1, const uint8_t *mask0, const uint8_t *mask1, const unsigned int width0, const unsigned int height0, const unsigned int width1, const unsigned int height1, unsigned int sizeX, unsigned int sizeY, const int left0, const int top0, const int left1, const int top1)
Determines the partial zero-mean ssd of two image patches between two frames while for each frame a b...
Definition: AdvancedZeroMeanSumSquareDifferencesBase.h:204
static unsigned int sum8BitPerChannelPartialTemplate(const uint8_t *frame, const uint8_t *mask, const unsigned int width, const unsigned int height, const unsigned int sizeX, const unsigned int sizeY, const int left, const int top, unsigned int sums[tChannels])
Determines the mean values of given image patch with arbitrary sizes while a binary mask specifies wh...
Definition: AdvancedZeroMeanSumSquareDifferencesBase.h:304
static uint32_t patch8BitPerChannelTemplate(const uint8_t *image0, const uint8_t *image1, const unsigned int width0, const unsigned int width1, const Scalar centerX0, const Scalar centerY0, const Scalar centerX1, const Scalar centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
Returns the sum of square differences for an image patch determined between two individual images.
Definition: AdvancedZeroMeanSumSquareDifferencesBase.h:142
static void patchFrame(const T *source, T *buffer, const unsigned int width, const unsigned int channels, const unsigned int x, const unsigned int y, const unsigned int patchSize, const unsigned int sourcePaddingElements, const unsigned int bufferPaddingElements)
Copies a small patch area of a given frame into a buffer holding only the entire patch.
Definition: FrameConverter.h:3127
unsigned int sqr(const char value)
Returns the square value of a given value.
Definition: base/Utilities.h:1029
std::pair< Index32, Index32 > IndexPair32
Definition of a pair holding 32 bit indices.
Definition: Base.h:138
float Scalar
Definition of a scalar type.
Definition: Math.h:128
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition: Vector2.h:21
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15