【发布时间】:2014-09-17 06:26:33
【问题描述】:
我一直在尝试了解如何使用 CImg 的绘图功能,但文档对我来说不是很清楚。我只想绘制一个像素,但我不明白 draw_point 是如何工作的。有人可以举一些 draw_point 以及如何声明图像的例子吗?另外,C++ 有更好的选择吗?我只想要最简单的 C++ 成像库。我想逐个像素地操作一个空图像。有没有更好的选择?
【问题讨论】:
标签: c++ image-processing pixel imaging cimg
我一直在尝试了解如何使用 CImg 的绘图功能,但文档对我来说不是很清楚。我只想绘制一个像素,但我不明白 draw_point 是如何工作的。有人可以举一些 draw_point 以及如何声明图像的例子吗?另外,C++ 有更好的选择吗?我只想要最简单的 C++ 成像库。我想逐个像素地操作一个空图像。有没有更好的选择?
【问题讨论】:
标签: c++ image-processing pixel imaging cimg
我已经修改了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 的文档。我在一些项目中使用过它,对我来说它非常方便。
【讨论】: