c++ - 使用CImg使用线性插值填充颜色

标签 c++ colors interpolation cimg

“使用 CImg,编写一个程序来构造一个正方形图像并为每个角分配一种颜色。使用 线性插值,对正方形中的每个像素进行填充和着色,使整个着色平滑 广场的整个区域"

我很难理解如何为每个角选择颜色,并为正方形填充颜色。下面是我的代码,但似乎随机放置颜色,我无法选择自己的颜色。在我现在拥有的代码下方。

#include <iostream>
#include "CImg/CImg.h"

using namespace cimg_library;
using namespace std;

int main() {
  //Create an empty image
  CImg<unsigned char> square(255, 255, 1, 3, 0);

  for (int i = 0; i < 255; i++){
      for(int j = 0; j < 255; j++){
          //Red
          square(i,j,0,0) = i; 
          //Green
          square(i,j,0,1) = j;
          //Blue
          square(i,j,0,2) = i;                
      }
  }

  //Display and save filtered image
  CImgDisplay disp(square); 
  while (!disp.is_closed())
      disp.wait();   
  square.save("squareInterpolation.bmp");  
}

我的代码的结果:
image

我想要的结果(忽略白色边框):
image

非常感谢!

最佳答案

目前,您的代码没有进行任何插值——它只是在赋值。插值是指您知道图像极限的某些值并计算这些已知值之间的值(使用公式)以填充未知值。

实际上,CImg 会为您进行插值,恕我直言,优秀的工程师会重复使用和回收经过良好测试的代码,因为它可以降低成本。因此,最简单的解决方案是创建红色、绿色、蓝色和白色角点(插值中的已知值)的 2x2 图像,然后让 CImg 使用插值将图像的大小调整为 255x255。

它可能看起来像这样:

#include <iostream>
#include <cstdlib>
#define cimg_display 0
#include "CImg.h"

using namespace cimg_library;
using namespace std;

#define INTERPOLATION_LINEAR            3
#define INTERPOLATION_CUBIC             5

#define BOUNDARY_DIRICHLET              0
#define BOUNDARY_NEUMANN                1

int main() {
   // Create 2x2 image with Red, Green, Blue and White pixels
   CImg<unsigned char> image(2,2,1,3,0);
   image(0,0,0,0)=255;  // top-left is Red
   image(1,0,0,1)=255;  // top-right is Green
   image(0,1,0,2)=255;  // bottom-left is Blue
   image(1,1,0,0)=255;  // bottom-right is white
   image(1,1,0,1)=255;  // bottom-right is white
   image(1,1,0,2)=255;  // bottom-right is white

   // Let CImg do the interpolation for us - because smart engineers re-use well tested code
   CImg<unsigned char> result=image.resize(255,255,-100,-100,INTERPOLATION_LINEAR,BOUNDARY_DIRICHLET);

   // Save result image as NetPBM PNM - no libraries required
   result.save_pnm("result.pnm");
}

enter image description here

我想这不是您期望的方式,所以我会将该解决方案与我自己实现插值的另一个解决方案结合起来。这是您可能会使用的 Wikipedia article on Bilinear Interpolation 中的公式.

enter image description here

可以这样实现:

#include <iostream>
#include <cstdlib>
#define cimg_display 0
#include "CImg.h"

using namespace cimg_library;
using namespace std;

int main() {
   // Create 256x256 image with Red, Green, Blue and White pixels
   CImg<unsigned char> image(256,256,1,3,0);
   image(0,0,0,0)=255;      // top-left is Red
   image(255,0,0,1)=255;    // top-right is Green
   image(0,255,0,2)=255;    // bottom-left is Blue
   image(255,255,0,0)=255;  // bottom-right is white
   image(255,255,0,1)=255;  // bottom-right is white
   image(255,255,0,2)=255;  // bottom-right is white

   // Fill inner area by interpolation
   for(int r=0;r<256;r++){                   // "r" for "row" 
      for(int c=0;c<256;c++){                // "c" for "column"
         for(int b=0;b<3;b++){               // "b" for "band" in my head
            float oneMinusX=(255-r)/255.0;
            float oneMinusY=(255-c)/255.0;
            image(r,c,0,b)=image(0,0,0,b)*oneMinusX*oneMinusY +
                     image(255,0,0,b)*oneMinusY*r/255.0 +
                     image(0,255,0,b)*oneMinusX*c/255.0 +
                     image(255,255,0,b)*r*c/(255.0*255.0);
         }
      }
   }

   // Save result image as NetPBM PNM - no libraries required
   image.save_pnm("result.pnm");
}

关于c++ - 使用CImg使用线性插值填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48605392/

相关文章:

需要 C++ 数组练习帮助

c++ - 在矩形qt中生成随机坐标

c++ - 保存更改后,如何使 XCode 4 自动构建和运行代码?

python - Learning Object Detection 检测结果显示变色

c++ - 从 boost::spirit 解析器中检索 AST

swift - 如何在 [Int] Swift 中存储颜色代码

colors - "intelligent"为何是 PDF (ISO) 标准 : Capable of detecting a BW-printer and adapting accordingly?

python - Matplotlib:等高线图的数据三次插值(或 FIT)

function - Julia :如何最佳地声明包含插值的向量?

python - Pandas 线性插值按另一列分组