c++ - 'CImg'的使用方法及基本功能

标签 c++ image-processing pixel imaging cimg

<分区>

我一直在努力了解如何使用 CImg 的绘图功能,但文档对我来说不是很清楚。我只想画一个像素,但我不明白 draw_point 是如何工作的。有人可以举一些 draw_point 的例子以及如何声明图像吗?另外,C++ 有更好的替代方案吗?我只想要最简单的 C++ 成像库。我想逐个像素地操作一个空图像。有没有更好的选择?

最佳答案

我修改了 CImg tutorial为了展示如何使用 draw_point,代码如下:

#include "CImg.h"

using namespace cimg_library;

int main() 
{

    int size_x = 640;
    int size_y = 480;
    int size_z = 1;
    int numberOfColorChannels = 3; // R G B
    unsigned char initialValue = 0;

    CImg<unsigned char> image(size_x, size_y, size_z, numberOfColorChannels, initialValue);

    CImgDisplay display(image, "Click a point");

    while (!display.is_closed())
    {
        display.wait();
        if (display.button() && display.mouse_y() >= 0 && display.mouse_x() >= 0)
        {
            const int y = display.mouse_y();
            const int x = display.mouse_x();

            unsigned char randomColor[3];
            randomColor[0] = rand() % 256;
            randomColor[1] = rand() % 256;
            randomColor[2] = rand() % 256;

            image.draw_point(x, y, randomColor);
        }
        image.display(display);
    }
    return 0;
}

draw_point 方法有三个重载,这可能会混淆使用。我使用了以下一个:

template<typename tc>
CImg<T>& draw_point(const int x0, const int y0,
                    const tc *const color, const float opacity=1)

参见 this 了解详情。

至于替代方案,如果您只想逐个像素地修改数据,也许您可​​以像处理原始数据一样使用 libpng 和 libjpeg 进行输入/输出来处理图像。

无论如何,我建议您阅读更多关于 CImg 的文档。我在我的一些项目中使用过它,对我来说它非常方便。

关于c++ - 'CImg'的使用方法及基本功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25883703/

相关文章:

c++ - 从位数组重建值

c++ - 在 clang 中强制使用 `const char[]` 字符串文字

python - OpenCV-Python : How Do I split an Image in a grid?

javascript - 在 A4 svg 绘图中绘制 1 像素笔画宽度图形

c++ - 一种在 C++ 中实现惰性求值的方法

c++ - 不能使用 auto 和 decltype() 时如何避免重复封闭类类型

image-processing - 在 AForge.NET 中使用霍夫变换检测图像中的同心圆

java - OpenCV - Java - 如何删除集群周围的一些像素

javascript - 创建具有不同颜色比例的双色 Canvas

image - HTML5如何在canvas中绘制透明像素图片