c++ - 使用 CImg 镜像图像?

标签 c++ cimg

标题是不言自明的,但我要强调我是 c++ 的初学者,尤其是涉及 CImg 库时。这是我到目前为止的代码,我尝试使用 flipped_idx 进行试验(当合并到代码中时它不起作用)但我认为我没有朝着正确的方向前进。或者也许我是,任何帮助将不胜感激!

我正在输入下面的前两张图片并将它们合并为一张图片。

enter image description here

enter image description here

这是我试图在 y 轴上镜像的合并图像。

enter image description here

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

using namespace std;
using namespace cimg_library;

const int imgscale = 400;

int main() {

CImg<unsigned char> player("input/player.bmp");

CImgDisplay green_disp(player, "Original 'player.bmp'");
green_disp.resize(imgscale, imgscale, true);
green_disp.move(10, 50);


CImg<unsigned char> bg("input/forest.bmp");

CImgDisplay bg_disp(bg, "Original 'forest.bmp'");
bg_disp.resize(imgscale, imgscale, true);
bg_disp.move(20 + imgscale, 50);

CImg<unsigned char> merged(player.width(), player.height(),1,3,0);
int w = player.width();
int h = player.height();
int imglen = w*h;
const CImg<float>
    flipped_idx = merged.get_mirror('y');


for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
        int idx = j + i*w;


        unsigned char colorR = player[idx];
        unsigned char colorG = player[idx + imglen];
        unsigned char colorB = player[idx + imglen * 2];

        if (colorG > 215 && colorR < 255) {
            //Background
            merged[idx] = bg[idx]; //red
            merged[idx + imglen] = bg[idx + imglen]; //green
            merged[idx + imglen * 2] = bg[idx + imglen * 2]; //blue
        }
        else {
            //Foreground
            merged[idx] = player[idx]; //red
            merged[idx + imglen] = player[idx + imglen]; //green
            merged[idx + imglen * 2] = player[idx + imglen * 2]; //blue
        }

    }
}

CImgDisplay merged_disp(merged, "Merged image");
merged_disp.resize(imgscale, imgscale,true); //change size
merged_disp.move(30 + imgscale*2, 50); //place it nicely on screen.
merged.save("output/merged.bmp");

最佳答案

如果我尝试尽可能贴近您的代码,但丢弃所有显示内容,我会得到:

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

using namespace std;
using namespace cimg_library;

const int imgscale = 400;

int main() {

CImg<unsigned char> player("player.bmp");
CImg<unsigned char> bg("forest.bmp");

CImg<unsigned char> merged(player.width(), player.height(),1,3,0);
int w = player.width();
int h = player.height();
int imglen = w*h;
CImg<unsigned char> result;

for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
        int idx = j + i*w;

        unsigned char colorR = player[idx];
        unsigned char colorG = player[idx + imglen];
        unsigned char colorB = player[idx + imglen * 2];

        if (colorG > 215 && colorR < 255) {
            //Background
            merged[idx] = bg[idx]; //red
            merged[idx + imglen] = bg[idx + imglen]; //green
            merged[idx + imglen * 2] = bg[idx + imglen * 2]; //blue
        }
        else {
            //Foreground
            merged[idx] = player[idx]; //red
            merged[idx + imglen] = player[idx + imglen]; //green
            merged[idx + imglen * 2] = player[idx + imglen * 2]; //blue
        }
    }
}

result = merged.get_mirror('y');
result.save("merged.bmp");
}

enter image description here


如果我正在编写代码,我可能会选择更像这样的东西:

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

using namespace std;
using namespace cimg_library;

int main() {

   CImg<unsigned char> player("player.bmp");
   CImg<unsigned char> bg("forest.bmp");
   CImg<unsigned char> merged(player.width(), player.height(),1,3,0);
   CImg<unsigned char> result;

   // Extract Green channel and make into mask by thresholding
   CImg<unsigned char> mask = player.get_channel(1).threshold(215);
   mask.save("mask.bmp");

   // Select background or foreground according to mask
   merged = bg.mul(mask) + player.mul(1-mask);
   merged.save("merged.bmp");

   // Mirror and save
   result = merged.get_mirror('y');
   result.save("result.bmp");
}

仅供引用,面具看起来像这样:

enter image description here

关于c++ - 使用 CImg 镜像图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547403/

相关文章:

c++ - 在调用基类之前需要调用成员的方法

c++ - 跨线程共享读资源

c++ - Mac 中 Electron 中的 native C++ 插件出现问题

c++ - 将未知大小的二维数组传递给 C++ 中的函数

c++ - 在 64 位 Windows 中 Hook 32 位进程

c++ - 调试器忽略动态加载的 DLL 中的错误

c++ - CImg 是否与 Emscripten 兼容

c++ - CImg 错误 : 'gm.exe' is not recognized as an internal or external command,

c++ - 无需在内存中加载大图像的图像即可访问像素值

c++ - CImg 库中的奇怪语法,不知道它叫什么