![]()
| |
Tutorial : Getting Started. Let's start to write our first program to get the idea. This will demonstrate how to load and create images, as well as handle image display and mouse events. Assume we want to load a color image lena.jpg , smooth it, display it in a windows, and enter an event loop so that clicking a point in the image will draw the (R,G,B) intensity profiles of the corresponding image line (in another window). Yes, that sounds quite complex for a first code, but don't worry, it will be very simple using the CImg library ! Well, just look at the code below, it does the task :#include "CImg.h"
using namespace cimg_library;
int main() {
CImg<unsigned char> image("lena.jpg"), visu(500,400,1,3,0);
const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 };
image.blur(2.5);
while (!main_disp.is_closed() && !draw_disp.is_closed()) {
main_disp.wait();
if (main_disp.button() && main_disp.mouse_y()>=0) {
const int y = main_disp.mouse_y();
visu.fill(0).draw_graph(image.get_crop(0,y,0,0,image.width()-1,y,0,0),red,1,1,0,255,0);
visu.draw_graph(image.get_crop(0,y,0,1,image.width()-1,y,0,1),green,1,1,0,255,0);
visu.draw_graph(image.get_crop(0,y,0,2,image.width()-1,y,0,2),blue,1,1,0,255,0).display(draw_disp);
}
}
return 0;
}
![]() #include "CImg.h"
Include the main and only header file of the CImg library. using namespace cimg_library;
Use the library namespace to ease the declarations afterward. int main() {
Definition of the main function. CImg<unsigned char> image("lena.jpg"), visu(500,400,1,3,0);
Creation of two instances of images of const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 };
Definition of three different colors as array of unsigned char. This will be used to draw plots with different colors. image.blur(2.5);
Blur the image, with a gaussian blur and a standard variation of 2.5. Note that most of the CImg functions have two versions : one that acts in-place (which is the case of blur), and one that returns the result as a new image (the name of the function begins then with CImgDisplay main_disp(image,"Click a point"), draw_disp(visu,"Intensity profile");
Creation of two display windows, one for the input image image, and one for the image visu which will be display intensity profiles. By default, CImg displays handles events (mouse,keyboard,..). On Windows, there is a way to create fullscreen displays. while (!main_disp.is_closed() && !draw_disp.is_closed()) {
Enter the event loop, the code will exit when one of the two display windows is closed. main_disp.wait();
Wait for an event (mouse, keyboard,..) in the display window if (main_disp.button() && main_disp.mouse_y()>=0) {
Test if the mouse button has been clicked on the image area. One may distinguish between the 3 different mouse buttons, but in this case it is not necessary const int y = main_disp.mouse_y();
Get the image line y-coordinate that has been clicked. visu.fill(0).draw_graph(image.get_crop(0,y,0,0,image.width()-1,y,0,0),red,1,0,256,0);
This line illustrates the pipeline property of most of the CImg class functions. The first function visu.draw_graph(image.get_crop(0,y,0,1,image.width()-1,y,0,1),green,1,0,256,0);
Plot the intensity profile for the green channel of the clicked line. visu.draw_graph(image.get_crop(0,y,0,2,image.width()-1,y,0,2),blue,1,0,256,0).display(draw_disp);
Same thing for the blue channel. Note how the function (which return a reference to ...till the end
I don't think you need more explanations !As you have noticed, the CImg library allows to write very small and intuitive code. Note also that this source will perfectly work on Unix and Windows systems. Take also a look to the examples provided in the CImg package ( directory |