使用 CImg 镜像图像?
Mirror an image using the CImg?
标题很漂亮 self-explanatory,但我要强调的是我是 c++ 的初学者,尤其是涉及到 CImg 库时。这是我到目前为止的代码,我尝试使用 flipped_idx 进行试验(当合并到代码中时它不起作用)但我认为我没有朝着正确的方向前进。或者也许我是,任何帮助将不胜感激!
我正在输入下面的前两张图片并将它们合并为一张图片。
这是我尝试在 y-axis 上镜像的合并图像。
#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");
}
如果我正在编码,我可能会选择更像这样的东西:
#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");
}
仅供参考,面具看起来像这样:
标题很漂亮 self-explanatory,但我要强调的是我是 c++ 的初学者,尤其是涉及到 CImg 库时。这是我到目前为止的代码,我尝试使用 flipped_idx 进行试验(当合并到代码中时它不起作用)但我认为我没有朝着正确的方向前进。或者也许我是,任何帮助将不胜感激!
我正在输入下面的前两张图片并将它们合并为一张图片。
这是我尝试在 y-axis 上镜像的合并图像。
#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");
}
如果我正在编码,我可能会选择更像这样的东西:
#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");
}
仅供参考,面具看起来像这样: