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 the main and only header file of the CImg library.
Use the library namespace to ease the declarations afterward.
Definition of the main function.
Creation of two instances of images of
char pixels. The first image
image is initialized by reading an image file from the disk. Here,
lena.jpg must be in the same directory as the current program. Note that you must also have installed the ImageMagick package in order to be able to read JPG images. The second image
visu is initialized as a black color image with dimension
dz=1 (here, it is a 2D image, not a 3D one), and
dv=3 (each pixel has 3 'vector' channels R,G,B). The last argument in the constructor defines the default value of the pixel values (here
0, which means that
visu will be initially black).
Definition of three different colors as array of unsigned char. This will be used to draw plots with different colors.
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
get_ ). In this case, one could have also written
image = image.get_blur(2.5); (more expensive, since it needs an additional copy operation).
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.
Enter the event loop, the code will exit when one of the two display windows is closed.
Wait for an event (mouse, keyboard,..) in the display window
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
Get the image line y-coordinate that has been clicked.
This line illustrates the pipeline property of most of the CImg class functions. The first function
fill(0) simply sets all pixel values with 0 (i.e. clear the image
visu). The interesting thing is that it returns a reference to
visu and then, can be pipelined with the function
draw_graph() which draws a plot in the image
visu. The plot data are given by another image (the first argument of
draw_graph()). In this case, the given image is the red-component of the line y of the original image, retrieved by the function
get_crop() which returns a sub-image of the image
image. Remember that images coordinates are 4D (x,y,z,c) and for color images, the R,G,B channels are respectively given by
v=0, v=1 and
Plot the intensity profile for the green channel of the clicked line.
Same thing for the blue channel. Note how the function (which return a reference to
visu) is pipelined with the function
display() that just paints the image visu in the corresponding display window.
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
examples/ ). It will show you how CImg-based code can be surprisingly small. Moreover, there is surely one example close to what you want to do. A good start will be to look at the file
CImg_demo.cpp which contains small and various examples of what you can do with the CImg Library. All CImg classes are used in this source, and the code can be easily modified to see what happens.
Copyrights (C) From october 2004, David Tschumperlé - GREYC UMR CNRS 6072, Image team.
Copyrights (C) January->September 2004, David Tschumperlé.
Copyrights (C) 2000->2003, David Tschumperlé - INRIA Sophia-Antipolis. Odyssée group.