Reference
The CImg Library provides different macros that define useful iterative loops over an image. Basically, it can be used to replace one or several for(..)
instructions, but it also proposes interesting extensions to classical loops. Below is a list of all existing loop macros, classified in four different categories :
Loops over the pixel buffer are really basic loops that iterate a pointer on the pixel data buffer of a cimg_library::CImg
image. Two macros are defined for this purpose :
img
, using a pointer T* ptr
, starting from the beginning of the buffer (first pixel) till the end of the buffer (last pixel).img
must be a (non empty) cimg_library::CImg
image of pixels T
.ptr
is a pointer of type T*
. This kind of loop should not appear a lot in your own source code, since this is a low-level loop and many functions of the CImg class may be used instead. Here is an example of use : cimg_for()
but from the end to the beginning of the pixel buffer.img
, using an offset
, starting from the beginning of the buffer (first pixel, off=0
) till the end of the buffer (last pixel value, off = img.size()-1
).img
must be a (non empty) cimg_library::CImg<T> image of pixels T
.off
is an inner-loop variable, only defined inside the scope of the loop.Here is an example of use :
The following loops are probably the most used loops in image processing programs. They allow to loop over the image along one or several dimensions, along a raster scan course. Here is the list of such loop macros for a single dimension :
for (int x = 0; x<img.width(); ++x)
.for (int y = 0; y<img.height(); ++y)
.for (int z = 0; z<img.depth(); ++z)
.for (int c = 0; c<img.spectrum(); ++c)
.Combinations of these macros are also defined as other loop macros, allowing to loop directly over 2D, 3D or 4D images :
cimg_forY(img,y)
cimg_forX(img,x)
.cimg_forZ(img,z)
cimg_forX(img,x)
.cimg_forZ(img,z)
cimg_forY(img,y)
.cimg_forC(img,c)
cimg_forX(img,x)
.cimg_forC(img,c)
cimg_forY(img,y)
.cimg_forC(img,c)
cimg_forZ(img,z)
.cimg_forZ(img,z)
cimg_forXY(img,x,y)
.cimg_forC(img,c)
cimg_forXY(img,x,y)
.cimg_forC(img,c)
cimg_forXZ(img,x,z)
.cimg_forC(img,c)
cimg_forYZ(img,y,z)
.cimg_forC(img,c)
cimg_forXYZ(img,x,y,z)
.x
,y
,z
and v
are inner-defined variables only visible inside the scope of the loop. They don't have to be defined before the call of the macro.img
must be a (non empty) cimg_library::CImg image.Here is an example of use that creates an image with a smooth color gradient :
Similar macros are also defined to loop only on the border of an image, or inside the image (excluding the border). The border may be several pixel wide :
n
pixels wide.n
pixels wide.n
pixels wide.n
pixels wide.n
pixels wide.n
pixels wide.And also :
n
pixels wide.n
pixels wide.n
pixels wide.n
pixels wide.n
pixels wide.n
pixels wide.x
,y
,z
and c
are inner-defined variables only visible inside the scope of the loop. They don't have to be defined before the call of the macro.img
must be a (non empty) cimg_library::CImg image.n
stands for the size of the border.Here is an example of use, to create a 2d grayscale image with two different intensity gradients :
Inside an image loop, it is often useful to get values of neighborhood pixels of the current pixel at the loop location. The CImg Library provides a very smart and fast mechanism for this purpose, with the definition of several loop macros that remember the neighborhood values of the pixels. The use of these macros can highly optimize your code, and also simplify your program.
For 2D images, the neighborhood-based loop macros are :
For all these loops, x
and y
are inner-defined variables only visible inside the scope of the loop. They don't have to be defined before the call of the macro. img
is a non empty CImg<T> image. z
and c
are constants that define on which image slice and vector channel the loop must apply (usually both 0 for grayscale 2D images). Finally, I
is the 2x2, 3x3, 4x4 or 5x5 neighborhood of type T
that will be updated with the correct pixel values during the loop (see Defining neighborhoods).
For 3D images, the neighborhood-based loop macros are :
For all these loops, x
, y
and z
are inner-defined variables only visible inside the scope of the loop. They don't have to be defined before the call of the macro. img
is a non empty CImg<T> image. c
is a constant that defines on which image channel the loop must apply (usually 0 for grayscale 3D images). Finally, I
is the 2x2x2 or 3x3x3 neighborhood of type T
that will be updated with the correct pixel values during the loop (see Defining neighborhoods).
A neighborhood is defined as an instance of a class having operator[] defined. This particularly includes classical C-array, as well as CImg<T> objects.
For instance, a 3x3 neighborhood can be defined either as a 'float[9]' or a 'CImg<float>(3,3)' variable.
There are also some useful macros that can be used to define variables that reference the neighborhood elements. There are :
I
, of type type
.I
, of type type
.I
, of type type
.I
, of type type
.I
, of type type
.I
, of type type
.Actually, I
is a generic name for the neighborhood. In fact, these macros declare a set of new variables. For instance, defining a 3x3 neighborhood CImg_3x3(I,float)
declares 9 different float variables Ipp
,Icp
,Inp
,Ipc
,Icc
,Inc
,Ipn
,Icn
,Inn
which correspond to each pixel value of a 3x3 neighborhood. Variable indices are p
,c
or n
, and stand respectively for 'previous', 'current' and 'next'. First indice denotes the x-axis
, second indice denotes the y-axis
. Then, the names of the variables are directly related to the position of the corresponding pixels in the neighborhood. For 3D neighborhoods, a third indice denotes the z-axis
. Then, inside a neighborhood loop, you will have the following equivalence :
Ipp = img(x-1,y-1)
Icn = img(x,y+1)
Inp = img(x+1,y-1)
Inpc = img(x+1,y-1,z)
Ippn = img(x-1,y-1,z+1)
For bigger neighborhoods, such as 4x4 or 5x5 neighborhoods, two additionnal indices are introduced : a
(stands for 'after') and b
(stands for 'before'), so that :
Ibb = img(x-2,y-2)
Ina = img(x+1,y+2)
The value of a neighborhood pixel outside the image range (image border problem) is automatically set to the same values as the nearest valid pixel in the image (this is also called the Neumann border condition).
More than a long discussion, the above example will demonstrate how to compute the gradient norm of a 3D volume using the cimg_for3x3x3()
loop macro :
And the following example shows how to deal with neighborhood references to blur a color image by averaging pixel values on a 5x5 neighborhood.
As you can see, explaining the use of the CImg neighborhood macros is actually more difficult than using them !
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.