假设您想从硬盘中读取一个常见的图像文件格式,更改一个像素的颜色,并在屏幕上显示结果图像。使用C++,以下是建议使用最少的代码完成以上操作的(开源)库:
另外,哪个库可以以最优雅的方式实现上述操作?
一些背景信息:我最近一直在阅读计算机图形学的文献,发现有很多相对简单的基于像素的算法我想要实现。然而,虽然算法本身通常很容易实现,但必须按每个像素的基础来操作图像并显示结果所需的框架却阻止了我的行动。另外,哪个库可以以最优雅的方式实现上述操作?
一些背景信息:我最近一直在阅读计算机图形学的文献,发现有很多相对简单的基于像素的算法我想要实现。然而,虽然算法本身通常很容易实现,但必须按每个像素的基础来操作图像并显示结果所需的框架却阻止了我的行动。CImg库易于使用。
CImg<unsigned char> img("lena.png"); // Read in the image lena.png
const unsigned char valR = img(10,10,0,0); // Read the red component at coordinates (10,10)
const unsigned char valG = img(10,10,0,1); // Read the green component at coordinates (10,10)
const unsigned char valB = img(10,10,2); // Read the blue component at coordinates (10,10) (Z-coordinate omitted here).
const unsigned char avg = (valR + valG + valB)/3; // Compute average pixel value.
img(10,10,0) = img(10,10,1) = img(10,10,2) = avg; // Replace the pixel (10,10) by the average grey value.
CImgDisplay main_disp(img, "Modified Lena"); // Display the modified image on the screen
img.save("lena_mod.png"); // Save the modified image to lena_mod.png
它也可以被用作相当强大的图像处理库。在这里看例子。