简体   繁体   English

无法使用cimg库将图像正确保存到文件中

[英]Can't save image to file properly with cimg library

Here is the entirety of my code: 这是我的全部代码:

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

using namespace cimg_library;
int main() {

CImg<float> image(100,100,1,3,0);
const float color[] = {1.0,1.0,0.0};
image.draw_point(50,50,color);
image.save("file.bmp");
CImgDisplay local(image, "Hah");
while (true) {
    local.wait();
    }   

}

This successfully displays what I expect in a window, namely, a completely black square with a white pixel at 50,50. 这成功地显示了我在窗口中所期望的内容,即一个白色像素为50,50的完全黑色正方形。 However, file.bmp is simply a black square, without that pixel (and saving a cimg Image that has been modified with repeated calls to draw_point in a larger program that does something useful also fails). 但是,file.bmp只是一个黑色正方形,没有那个像素(并且保存了一个cimg图像,该图像已经通过在较大的程序中重复调用draw_point而修改,该程序执行一些有用的操作也会失败)。 What's going on here? 这里发生了什么?

The problem is that you create CImgDisplay with normalization enabled. 问题是您在启用规范化的情况下创建CImgDisplay Thus, your pixel of {1,1,0} is normalized to {255,255,0} and is visible on your screen. 因此, {1,1,0}像素标准化为{255,255,0}并在屏幕上显示。

CImg.save does not perform normalization, so the the pixel is saved to disk as a very dark pixel. CImg.save不执行规范化,因此像素以非常暗的像素保存到磁盘。

You can fix the problem by changing your white pixel color: 您可以通过更改白色像素颜色来解决问题:

const float color[] = {255.,255.,255.};

And, optionally, by disabling normalization: 并且,可选地,通过禁用标准化:

CImgDisplay local(image, "Hah", 0);

In the alternative, you could normalize the original image before saving or displaying it: 或者,您可以在保存或显示原始图像之前将其标准化:

image.draw_point(50,50,color);
image.normalize(0, 255);
image.save("file.bmp");

References: 参考文献:

I'm not familiar with this library but this behavior would allude that CImgDisplay is interpreting the image differently when rendering. 我不熟悉这个库,但是这种行为会暗示CImgDisplay在渲染时会以不同的方式解释图像。

I would expect the color white to be defined as const unsigned char color[] = {255,255,255}; 我希望将白色的颜色定义为const unsigned char color[] = {255,255,255}; to represent RGB values for the bitmap. 表示位图的RGB值。

I would check the documentation for CImg as a first step. 我会检查CImg的文档作为第一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM