Assume you want to read an image file in a common file format from the hard drive, change the color of one pixel, and display the resulting image to the screen, in C++.
Which (open-source) libraries would you recommend to accomplish the above with the least amount of code?
Alternatively, which libraries would do the above in the most elegant way possible?
A bit of background: I have been reading a lot of computer graphics literature recently, and there are lots of relatively easy, pixel-based algorithms which I'd like to imple开发者_开发知识库ment. However, while the algorithm itself would usually be straightforward to implement, the necessary amount of frame-work to manipulate an image on a per-pixel basis and display the result stopped me from doing it.
The CImg library is easy to use.
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
It can also be used as a rather powerful image processing library. See the examples here.
You should look into the OpenCV library, especially if you're doing theoretical research into computer graphics.
If target application is windows-only, you could use GDI+, described here and here. And here is one of many proofs of concept for image manipulation using GDI+.
EDIT: Library is not open sourced, included in OS since Windows XP (can be installed on 98 and 2000), API is publicly available, and it gives you many image processing functions at hand which could be used in more complicated algorithms.
精彩评论