Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The easiest way to draw an image?

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 implement. 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.

like image 257
Benno Avatar asked Jan 01 '11 22:01

Benno


People also ask

What is the easiest form of drawing?

Doodle drawing might be one of the easiest possible ways to draw a picture. The fact that it is such a free-form method of drawing also takes away any fear of failure: doodling is the place where you can do no artistic wrong. Everyone can doodle, whether it's rows of hearts and stars, or more involved scenes or shapes.


2 Answers

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.

like image 184
moinudin Avatar answered Sep 30 '22 15:09

moinudin


You should look into the OpenCV library, especially if you're doing theoretical research into computer graphics.

like image 31
Reinderien Avatar answered Sep 30 '22 14:09

Reinderien