将每个通道像素乘以给定的向量

Multiply each channel pixel with given vector

本文关键字:向量 通道 像素      更新时间:2023-10-16

基本上我想将图像的每个通道值与向量相乘。例如,vec4f(1,2,3,4(1 *图像2的红色通道 *绿色频道等等,这是我的代码(不是完整的代码,有一些错误(,但我的老板说必须有更好,更简单的方法使用OpenCV,但我找不到。预先感谢。

      void scaleImage(TextureData& dst, TextureData const& src, cv::Vec4f 
      const& scale)
      {

auto size = src.info.size;
dst=src;
cv::Mat bgr[4];
cv::split(src.levels[0].images[0], bgr);
for (int y = 0; y < size.height; ++y)
{
    for (int x = 0; x < size.width; ++x)
    {
        src.levels[0].images[0].channels();
        if (src.levels[0].images[0].channels() == 4)
        {
            auto& imgRGB = dst.levels[0].images[0].at<cv::Vec4f>(x, y);
            imgRGB[3] = static_cast<uint8_t>(scale.w()*bgr[3].at[x, y]);
            imgRGB[2] = static_cast<uint8_t>(scale.x()*bgr[2].at[x, y]);
            imgRGB[1] = static_cast<uint8_t>(scale.y()*bgr[1].at[x, y]);
            imgRGB[0] = static_cast<uint8_t>(scale.z()*bgr[0].at[x, y]);
        }
        if (src.levels[0].images[0].channels() == 3)
        {
            auto& imgRGB = dst.levels[0].images[0].at<cv::Vec3f>(x, y);
            imgRGB[2] = static_cast<uint8_t>(scale.x()*bgr[2].at[x, y]);
            imgRGB[1] = static_cast<uint8_t>(scale.y()*bgr[1].at[x, y]);
            imgRGB[0] = static_cast<uint8_t>(scale.z()*bgr[0].at[x, y]);
        }
        if (src.levels[0].images[0].channels() == 2)
        {
            auto& imgRGB = dst.levels[0].images[0].at<cv::Vec2f>(x, y);
            imgRGB[1] = static_cast<uint8_t>(scale.x()*bgr[2].at[x, y]);
            imgRGB[0] = static_cast<uint8_t>(scale.y()*bgr[1].at[x, y]);
        }
        if (src.levels[0].images[0].channels() == 2)
        {
            auto& imgRGB = dst.levels[0].images[0].at<float>(x, y);
            imgRGB[0] = static_cast<uint8_t>(scale.x()*bgr[2].at[x, y]);
        }
    }
}

好吧,这有点奇怪。我认为没有人需要此功能,但如果...因为……因为它花了我2-3天(只有25行(

  void scaleImage(TextureData& dst, TextureData const& src, Vec4f const& 
  scale)
  {    
   std::vector<cv::Mat> bgr(3);
   std::vector<cv::Mat> resultMask(3);
   src.info;
  cv::split(src.levels[0].images[0], bgr);
  //dst = src;
  //auto size = src.info.size;
   cv::Point anchor = cv::Point(-1, -1);
        float a = scale.x();
        float b = scale.y();
        float c = scale.z();
        float d = scale.w();
        cv::Mat kern = (cv::Mat_<float>(3, 3) << 0, 0, 0,
                                                 0, a, 0,
                                                 0, 0, 0);
        cv::Mat kern1 = (cv::Mat_<float>(3, 3) << 0, 0, 0,
                                                 0, b, 0,
                                                 0, 0, 0);
        cv::Mat kern2 = (cv::Mat_<float>(3, 3) << 0, 0, 0,
                                                 0, c, 0,
                                                 0, 0, 0);
        cv::Mat kern3 = (cv::Mat_<float>(3, 3) << 0, 0, 0,
                                                 0, d, 0,
                                                 0, 0, 0);
        cv::filter2D(bgr[0], resultMask[0], bgr[0].depth(), kern,  anchor, 0, 1);
        cv::filter2D(bgr[1], resultMask[1], bgr[1].depth(), kern1, anchor, 0, 1);
        cv::filter2D(bgr[2], resultMask[2], bgr[2].depth(), kern2, anchor, 0, 1);
        //cv::filter2D(bgr[3], resultMask[3], bgr[3].depth(), kern3, anchor, 0, 1);
        cv::merge(resultMask, dst.levels[0].images[0]);

我不确定您想做什么,但是考虑了cv ::倍数?

  • 您使用的openCV版本吗?
  • 它是因素的常量因素还是const矩阵?