// 模板实现,支持动态通道数,同时编译期优化
template<int Channels>
std::array<float, Channels> optimized_bilinear_interpolation(
float src_x, float src_y, int width, int height, int line_size, uint8_t fill_value, uint8_t *src) {
std::array<float, Channels> result;
result.fill(fill_value);
if (src_x < 0 || src_x >= width || src_y < 0 || src_y >= height) {
return result; // 越界时返回填充值
}
int x_low = std::max(0, std::min(static_cast<int>(src_x), width - 1));
int y_low = std::max(0, std::min(static_cast<int>(src_y), height - 1));
int x_high = std::min(x_low + 1, width - 1);
int y_high = std::min(y_low + 1, height - 1);
float lx = src_x - x_low;
float ly = src_y - y_low;
float hx = 1 - lx;
float hy = 1 - ly;
uint8_t *v1 = src + y_low * line_size + x_low * Channels;
uint8_t *v2 = src + y_low * line_size + x_high * Channels;
uint8_t *v3 = src + y_high * line_size + x_low * Channels;
uint8_t *v4 = src + y_high * line_size + x_high * Channels;
for (int c = 0; c < Channels; ++c) {
result[c] = hx * hy * v1[c] + lx * hy * v2[c] + hx * ly * v3[c] + lx * ly * v4[c];
}
return result;
};
template<int Channels>
void process_block(
const cv::Mat &img_pre, cv::Mat &img_fusion, const cv::Mat &img_now, const cv::Mat &weight, int w, int h,
const float *affine_now2pre, int start_row, int end_row) {
int line_size = w * Channels;
uint8_t fill_value = 0;
for (int j = start_row; j < end_row; ++j) {
const uint8_t* weight_row = weight.ptr<uint8_t>(j);
uint8_t* fusion_row = img_fusion.ptr<uint8_t>(j);
const uint8_t* now_row = img_now.ptr<uint8_t>(j);
for (int i = 0; i < w; ++i) {
uint8_t weight_ij = weight_row[i];
uint8_t* fusion_pixel = fusion_row + i * Channels;
const uint8_t* now_pixel = now_row + i * Channels;
float r = weight_ij / 255.0f;
float img_pre_x = affine_now2pre[0] * i + affine_now2pre[1] * j + affine_now2pre[2];
float img_pre_y = affine_now2pre[3] * i + affine_now2pre[4] * j + affine_now2pre[5];
std::array<float, Channels> c;
if (img_pre_x >= 0 && img_pre_x < w && img_pre_y >= 0 && img_pre_y < h) {
c = optimized_bilinear_interpolation<Channels>(img_pre_x, img_pre_y, w, h, line_size, fill_value, img_pre.data);
} else {
c.fill(fill_value);
}
for (int c_idx = 0; c_idx < Channels; ++c_idx) {
fusion_pixel[c_idx] = static_cast<uint8_t>(c[c_idx]);
}
if (weight_ij != 0) {
for (int c_idx = 0; c_idx < Channels; ++c_idx) {
fusion_pixel[c_idx] = static_cast<uint8_t>((1 - r) * fusion_pixel[c_idx] + r * now_pixel[c_idx]);
}
}
}
}
};

339

被折叠的 条评论
为什么被折叠?



