c++ - C++ 中不同 channel 的 LUT,opencv2

标签 c++ opencv lookup-tables

几天来,我一直在研究用 C++ 实现的 opencv2,并注意到查找表是将更改应用到图像的最快方式。但是,我在将它们用于我的目的时遇到了一些麻烦。

下面的代码显示了一个反转像素值的例子:

bool apply(Image& img) {
   int dim(256);
   Mat lut(1, &dim, CV_8U);
   for (int i=0; i<256; i++)
      lut.at<uchar>(i)= 255-i;
   LUT(img.final,lut,img.final);
   return true;
}

class Image {
public:
   const Mat& original;
   Mat final;
...
};

由于它非常高效,比一个像素一个像素地变化(通过我自己的测试验证)更有效,我想将此方法用于其他操作。但是要做到这一点,我必须分别访问每一层(每种颜色,图片在 BGR 中)。例如,我想将蓝色更改为 255-i,将绿色更改为 255-i/2,将红色更改为 255-i/3。

我在网上搜索了一段时间,但找不到正确的解决方案。据我所知,这是可能的 ( documentation ),但我找不到实现它的方法。

最佳答案

关键是文档中的这段话:

the table should either have a single channel (in this case the same table is used for all channels) or the same number of channels as in the source array

因此,您必须创建一个多 channel LUT:

bool apply(Image& img) {
   int dim(256);

   Mat lut(1, &dim, CV_8UC(img.final.channels()));

   if( img.final.channels() == 1)
   {
      for (int i=0; i<256; i++)
         lut.at<uchar>(i)= 255-i;
   }
   else // stupid idea that all the images are either mono either multichannel
   {
      for (int i=0; i<256; i++)
      {
         lut.at<Vec3b>(i)[0]= 255-i;   // first channel  (B)
         lut.at<Vec3b>(i)[1]= 255-i/2; // second channel (G)
         lut.at<Vec3b>(i)[2]= 255-i/3; // ...            (R)
      }
   }

   LUT(img.final,lut,img.final); // are you sure you are doing final->final? 
   // if yes, correct the LUT allocation part

   return true;
}

关于c++ - C++ 中不同 channel 的 LUT,opencv2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11503219/

相关文章:

C++0x printf() 为 double 打印错误的值

c++ - 使用预处理器检测 MSVC,而不检测 clang-cl、icl、

java - 如何使用Java OpenCV

python - 如何从 RGB 图像中提取单个 channel

domain-driven-design - 查找值是否应该建模为聚合根?

c++ - 当 cin.get(a, size) 填充时,char 数组如何过大

c++ - std::forward_list::remove_if 谓词的要求

opencv - 在单个平面上计算多个标记的单应性

javascript - 根据邮政编码返回城市和州

c - 使用查找表进行优化