同时支持3通道和4通道的图像的局部的双线性插值的c++实现

// 模板实现,支持动态通道数,同时编译期优化
      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]);
                      }
                  }
              }
          }
      };

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小帆别吃糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值