Last active
October 14, 2022 12:22
-
-
Save michelerenzullo/8dc5af393aa581c72b6dd7f640c8e406 to your computer and use it in GitHub Desktop.
Reflect 101 a 2D matrix or multi channel image, performant as cv::copyMakeBorder, and it receives as input the pad for top,bottom,left,right margin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T, int C> | |
void Reflect_101(const T* const input, T* output, int pad_top, int pad_bottom, int pad_left, int pad_right, const int* original_size) { | |
// This function padd a 2D matrix or a multichannel image with the specified top,bottom,left,right pad and it applies | |
// a reflect 101 like cv::copyMakeBorder, the main (and only) difference is the following constraint to prevent out of buffer reading | |
pad_top = std::min(pad_top, original_size[0] - 1); | |
pad_bottom = std::min(pad_bottom, original_size[0] - 1); | |
pad_left = std::min(pad_left, original_size[1] - 1); | |
pad_right = std::min(pad_right, original_size[1] - 1); | |
const int stride[2] = { original_size[0], original_size[1] * C }; | |
const int padded[2] = { stride[0] + pad_top + pad_bottom, stride[1] + (pad_left + pad_right) * C }; | |
const int right_offset = (pad_left + original_size[1] - 1) * 2 * C; | |
const int left_offset = pad_left * 2 * C; | |
const int bottom_offset = 2 * (stride[0] - 1) + pad_top; | |
#pragma omp parallel for | |
for (int i = 0; i < padded[0]; ++i) { | |
T* const row = output + i * padded[1]; | |
if (i < padded[0] - pad_bottom) | |
std::copy_n(&input[stride[1] * abs(i - pad_top)], stride[1], &row[pad_left * C]); | |
else | |
std::copy_n(&input[stride[1] * (bottom_offset - i)], stride[1], &row[pad_left * C]); | |
for (int j = 0; j < pad_left * C; j += C) | |
std::copy_n(row + left_offset - j, C, row + j); | |
for (int j = padded[1] - pad_right * C; j < padded[1]; j += C) | |
std::copy_n(row + right_offset - j, C, row + j); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment