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);
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);
204inline 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);
261 unsigned int ssd = 0u;
263 frame0 += (pTop0 * int(width0) + pLeft0) *
int(tChannels);
264 frame1 += (pTop1 * int(width1) + pLeft1) *
int(tChannels);
266 mask0 += (pTop0 * int(width0) + pLeft0);
267 mask1 += (pTop1 * int(width1) + pLeft1);
269 const uint8_t*
const mask0End = mask0 + pSizeY * width0;
271 while (mask0 != mask0End)
273 ocean_assert(mask0 < mask0End);
275 const uint8_t*
const mask0RowEnd = mask0 + pSizeX;
277 while (mask0 != mask0RowEnd)
279 ocean_assert(mask0 < mask0End);
280 ocean_assert(mask0 < mask0RowEnd);
282 const unsigned int factor = (*mask0 * *mask1) != 0u;
284 for (
unsigned int n = 0u; n < tChannels; ++n) {
285 ssd +=
sqr(
int(frame0[n]) -
int(frame1[n]) + means1_0[n]) * factor;
295 frame0 += (width0 - pSizeX) * tChannels;
296 frame1 += (width1 - pSizeX) * tChannels;
298 mask0 += (width0 - pSizeX);
299 mask1 += (width1 - pSizeX);
302 return std::make_pair(ssd, pixels);
308 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
310 ocean_assert(frame && mask);
311 ocean_assert(width >= 1u && height >= 1u);
312 ocean_assert(sizeX >= 1u && sizeY >= 1u);
314 ocean_assert(width >= sizeX && height >= sizeY);
315 ocean_assert(left >= -
int(sizeX) + 1 && left <
int(width));
316 ocean_assert(top >= -
int(sizeY) + 1 && top <
int(height));
318 const int patchLeftBorder = max(0, -left);
319 const int patchTopBorder = max(0, -top);
321 const int patchRightBorder = max(0, left +
int(sizeX) -
int(width));
322 const int patchBottomBorder = max(0, top +
int(sizeY) -
int(height));
324 ocean_assert(patchLeftBorder >= 0 && patchLeftBorder <
int(sizeX));
325 ocean_assert(patchTopBorder >= 0 && patchTopBorder <
int(sizeY));
326 ocean_assert(patchRightBorder >= 0 && patchRightBorder <
int(sizeX));
327 ocean_assert(patchBottomBorder >= 0 && patchBottomBorder <
int(sizeY));
329 const int pLeft = left + patchLeftBorder;
330 const int pTop = top + patchTopBorder;
332 const unsigned int pSizeX = sizeX - patchLeftBorder - patchRightBorder;
333 const unsigned int pSizeY = sizeY - patchTopBorder - patchBottomBorder;
335 if (pSizeX > sizeX || pSizeY > sizeY) {
339 unsigned int pixels = 0u;
341 for (
unsigned int n = 0u; n < tChannels; ++n) {
345 frame += (pTop * int(width) + pLeft) *
int(tChannels);
346 mask += (pTop * int(width) + pLeft);
348 const uint8_t*
const maskEnd = mask + pSizeY * width;
350 while (mask != maskEnd)
352 ocean_assert(mask < maskEnd);
354 const uint8_t*
const maskRowEnd = mask + pSizeX;
356 while (mask != maskRowEnd)
358 ocean_assert(mask < maskEnd);
359 ocean_assert(mask < maskRowEnd);
361 const unsigned int factor = *mask != 0u;
363 for (
unsigned int n = 0u; n < tChannels; ++n) {
364 sums[n] += frame[n] * factor;
373 frame += int(width - pSizeX) * int(tChannels);
374 mask += int(width - pSizeX);
381inline 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])
383 static_assert(tChannels != 0u,
"Invalid number of frame channels!");
385 ocean_assert(frame0 && frame1);
386 ocean_assert(mask0 && mask1);
388 ocean_assert(sizeX >= 1u && sizeY >= 1u);
389 ocean_assert(width0 >= sizeX && width1 >= sizeX);
390 ocean_assert(height0 >= sizeY && height1 >= sizeY);
392 ocean_assert(left0 >= -
int(sizeX) + 1 && left0 <
int(width0));
393 ocean_assert(top0 >= -
int(sizeY) + 1 && top0 <
int(height0));
394 ocean_assert(left1 >= -
int(sizeX) + 1 && left1 <
int(width1));
395 ocean_assert(top1 >= -
int(sizeY) + 1 && top1 <
int(height1));
397 const int patchLeftBorder = max(0, max(-left0, -left1));
398 const int patchTopBorder = max(0, max(-top0, -top1));
400 const int patchRightBorder = max(0, max(left0 +
int(sizeX) -
int(width0), left1 +
int(sizeX) -
int(width1)));
401 const int patchBottomBorder = max(0, max(top0 +
int(sizeY) -
int(height0), top1 +
int(sizeY) -
int(height1)));
403 ocean_assert(patchLeftBorder >= 0 && patchLeftBorder <
int(sizeX));
404 ocean_assert(patchTopBorder >= 0 && patchTopBorder <
int(sizeY));
405 ocean_assert(patchRightBorder >= 0 && patchRightBorder <
int(sizeX));
406 ocean_assert(patchBottomBorder >= 0 && patchBottomBorder <
int(sizeY));
408 const int pLeft0 = left0 + patchLeftBorder;
409 const int pLeft1 = left1 + patchLeftBorder;
411 const int pTop0 = top0 + patchTopBorder;
412 const int pTop1 = top1 + patchTopBorder;
414 const unsigned int pSizeX = sizeX - patchLeftBorder - patchRightBorder;
415 const unsigned int pSizeY = sizeY - patchTopBorder - patchBottomBorder;
417 if (pSizeX > sizeX || pSizeY > sizeY) {
421 for (
unsigned int n = 0u; n < tChannels; ++n) {
424 for (
unsigned int n = 0u; n < tChannels; ++n) {
428 unsigned int pixels = 0u;
430 frame0 += (pTop0 * int(width0) + pLeft0) *
int(tChannels);
431 frame1 += (pTop1 * int(width1) + pLeft1) *
int(tChannels);
433 mask0 += (pTop0 * int(width0) + pLeft0);
434 mask1 += (pTop1 * int(width1) + pLeft1);
436 const uint8_t*
const mask0End = mask0 + pSizeY * width0;
438 while (mask0 != mask0End)
440 ocean_assert(mask0 < mask0End);
442 const uint8_t*
const mask0RowEnd = mask0 + pSizeX;
444 while (mask0 != mask0RowEnd)
446 ocean_assert(mask0 < mask0End);
447 ocean_assert(mask0 < mask0RowEnd);
449 const unsigned int factor = (*mask0 * *mask1) != 0u;
451 for (
unsigned int n = 0u; n < tChannels; ++n) {
452 sums0[n] += frame0[n] * factor;
455 for (
unsigned int n = 0u; n < tChannels; ++n) {
456 sums1[n] += frame1[n] * factor;
468 frame0 += int(width0 - pSizeX) * int(tChannels);
469 frame1 += int(width1 - pSizeX) * int(tChannels);
471 mask0 += int(width0 - pSizeX);
472 mask1 += int(width1 - pSizeX);
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:306
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:3141