PTdecode/CImg-1.3.0/html/reference/group__cimg__loops.html

Mon, 03 Aug 2009 23:41:04 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 03 Aug 2009 23:41:04 +0100
changeset 11
69416826d18c
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

added dep/*.d and obj/*.o to hgignore

     1  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     2 <head>
     3 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta>
     4 <title>The CImg Library - C++ Template Image Processing Toolkit</title>
     5 <meta content="David Tschumperle" name="author"></meta>
     6 <link rel="shortcut icon" type="image/x-icon" href="http://cimg.sourceforge.net/favicon.ico"></link>
     7 <link rel="icon" type="image/png" href="favicon.png"></link>
     8 <link href="doxygen.css" rel="stylesheet" type="text/css">
     9 <link href="tabs.css" rel="stylesheet" type="text/css">
    10 <style type="text/css">
    11 <!--
    12 body {background-color:white; font-family:sans-serif; }
    13 a:active{text-decoration:none; color:#303090}
    14 a:link{text-decoration:none; color:#303090}
    15 a:visited{text-decoration:none; color:#303090}
    16 a:hover{text-decoration:underline; color:#4E9F71}
    17 -->
    18 </style>
    19 <script language="JavaScript" type="text/javascript">
    20   <!-- Original:  Eric King (eric_andrew_king@hotmail.com) is used to display images in popup windows -->
    21   <!-- Web Site:  http://redrival.com/eak/ -->
    22   <!-- This script and many more are available free online at -->
    23   <!-- The JavaScript Source!! http://javascript.internet.com -->
    24   <!-- Begin
    25   function NewWindow(mypage, myname, w, h, scroll) {
    26   var winl = (screen.width - w) / 2;
    27   var wint = (screen.height - h) / 2;
    28   winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
    29   win = window.open(mypage, myname, winprops)
    30   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
    31   }
    32   //  End -->
    33 </script>
    34 </head>
    35 <body>
    36  <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
    37   <tbody><tr><td>
    38    <hr noshade="noshade" size="1" width="90%"></hr>
    39    <center>
    40    <br/>
    41    <a href="http://cimg.sourceforge.net"><img src="http://cimg.sourceforge.net/img/CImgLogo.jpg" alt="" border="0"></img></a><br/>
    42    <br/>
    43    </center>
    44    <hr noshade="noshade" size="1" width="90%"></hr>
    45    <center>
    46    <table bgcolor="#EEEEFF" style="width: 90%; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="3" cellspacing="0">
    47    <tbody><tr><td style="text-align: center; vertical-align: top;">
    48    <font color="#FFFFFF" face="Arial,Helvetica" size="-1">
    49    <b>[</b> <a target="_parent" href="http://cimg.sourceforge.net/index.shtml">Main</a> <b>]</b>
    50    <b>[</b> <a target="_parent" href="http://cimg.sourceforge.net/news.shtml">News</a><b>] </b>
    51    <b>[</b> <a target="_parent" href="http://cimg.sourceforge.net/download.shtml">Download</a> <b>]</b>
    52    <b>[</b> <a target="_parent" href="http://cimg.sourceforge.net/screenshots.shtml">Screenshots</a> <b>]</b>
    53    <b>[</b> <a target="_parent" href="http://cimg.sourceforge.net/reference/group__cimg__faq.html">FAQ</a> <b>]</b>
    54    <b>[</b> <a target="_parent" href="http://cimg.sourceforge.net/reference/group__cimg__tutorial.html">Tutorial</a> <b>]</b>
    55    <b>[</b> <a target="_parent" href="http://cimg.sourceforge.net/reference/index.html">Documentation</a> <b>]</b>
    56    <b>[</b> <a target="_parent" href="http://sourceforge.net/forum/forum.php?forum_id=334630">Forum</a> <b>]</b>
    57    <b>[</b> <a target="_parent" href="http://cimg.sourceforge.net/chat.shtml">Chat</a> <b>]</b>
    58    <b>[</b> <a target="_parent" href="http://cimg.sourceforge.net/links.shtml">Links</a> <b>]</b>
    59    </font>
    60    </td></tr></tbody>
    61    </table>
    62    </center>
    63    <hr noshade="noshade" size="1" width="90%"></hr>
    64   </td></tr></tbody>
    65 </table>
    66 <!-- Generated by Doxygen 1.5.7.1 -->
    67 <script type="text/javascript">
    68 <!--
    69 function changeDisplayState (e){
    70   var num=this.id.replace(/[^[0-9]/g,'');
    71   var button=this.firstChild;
    72   var sectionDiv=document.getElementById('dynsection'+num);
    73   if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){
    74     sectionDiv.style.display='block';
    75     button.src='open.gif';
    76   }else{
    77     sectionDiv.style.display='none';
    78     button.src='closed.gif';
    79   }
    80 }
    81 function initDynSections(){
    82   var divs=document.getElementsByTagName('div');
    83   var sectionCounter=1;
    84   for(var i=0;i<divs.length-1;i++){
    85     if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){
    86       var header=divs[i];
    87       var section=divs[i+1];
    88       var button=header.firstChild;
    89       if (button!='IMG'){
    90         divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild);
    91         button=document.createElement('img');
    92         divs[i].insertBefore(button,divs[i].firstChild);
    93       }
    94       header.style.cursor='pointer';
    95       header.onclick=changeDisplayState;
    96       header.id='dynheader'+sectionCounter;
    97       button.src='closed.gif';
    98       section.id='dynsection'+sectionCounter;
    99       section.style.display='none';
   100       section.style.marginLeft='14px';
   101       sectionCounter++;
   102     }
   103   }
   104 }
   105 window.onload = initDynSections;
   106 -->
   107 </script>
   108 <div class="navigation" id="top">
   109   <div class="tabs">
   110     <ul>
   111       <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
   112       <li><a href="modules.html"><span>Modules</span></a></li>
   113       <li><a href="namespaces.html"><span>Namespaces</span></a></li>
   114       <li><a href="annotated.html"><span>Classes</span></a></li>
   115     </ul>
   116   </div>
   117 </div>
   118 <div class="contents">
   119 <h1>Using Image Loops.</h1><table border="0" cellpadding="0" cellspacing="0">
   120 <tr><td></td></tr>
   121 </table>
   122 The CImg Library provides different macros that define useful iterative loops over an image. Basically, it can be used to replace one or several <code>for(..)</code> instructions, but it also proposes interesting extensions to classical loops. Below is a list of all existing loop macros, classified in four different categories :<ul>
   123 <li><a class="el" href="group__cimg__loops.html#lo1">Loops over the pixel buffer</a></li><li><a class="el" href="group__cimg__loops.html#lo4">Loops over image dimensions</a></li><li><a class="el" href="group__cimg__loops.html#lo5">Loops over interior regions and borders.</a></li><li><a class="el" href="group__cimg__loops.html#lo6">Loops using neighborhoods.</a></li></ul>
   124 <h2><a class="anchor" name="lo1">
   125 Loops over the pixel buffer</a></h2>
   126 Loops over the pixel buffer are really basic loops that iterate a pointer on the pixel data buffer of a <code><a class="el" href="structcimg__library_1_1CImg.html" title="Class representing an image (up to 4 dimensions wide), each pixel being of type T...">cimg_library::CImg</a></code> image. Two macros are defined for this purpose :<p>
   127 <ul>
   128 <li><b>cimg_for(img,ptr,T)</b> : This macro loops over the pixel data buffer of the image <code>img</code>, using a pointer <code>T* ptr</code>, starting from the end of the buffer (last pixel) till the beginning of the buffer (first pixel).<ul>
   129 <li><code>img</code> must be a (non empty) <code><a class="el" href="structcimg__library_1_1CImg.html" title="Class representing an image (up to 4 dimensions wide), each pixel being of type T...">cimg_library::CImg</a></code> image of pixels <code>T</code>.</li><li><code>ptr</code> is a pointer of type <code>T*</code>. 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 : <div class="fragment"><pre class="fragment">  CImg&lt;float&gt; img(320,200);
   130   cimg_for(img,ptr,<span class="keywordtype">float</span>) { *ptr=0; }      <span class="comment">// Equivalent to 'img.fill(0);'</span>
   131 </pre></div></li></ul>
   132 </li></ul>
   133 <p>
   134 <ul>
   135 <li><b>cimg_foroff(img,off)</b> : This macro loops over the pixel data buffer of the image <code>img</code>, using an offset <code></code>, starting from the beginning of the buffer (first pixel, <code>off=0</code>) till the end of the buffer (last pixel value, <code>off = img.size()-1</code>).<ul>
   136 <li><code>img</code> must be a (non empty) cimg_library::CImg&lt;T&gt; image of pixels <code>T</code>.</li><li><code>off</code> is an inner-loop variable, only defined inside the scope of the loop.</li></ul>
   137 </li></ul>
   138 <p>
   139 Here is an example of use : <div class="fragment"><pre class="fragment">  CImg&lt;float&gt; img(320,200);
   140   cimg_foroff(img,off) { img[off]=0; }  <span class="comment">// Equivalent to 'img.fill(0);'</span>
   141 </pre></div><h2><a class="anchor" name="lo4">
   142 Loops over image dimensions</a></h2>
   143 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 :<ul>
   144 <li><b>cimg_forX(img,x)</b> : equivalent to : <code>for (int x=0; x&lt;img.dimx(); x++)</code>.</li><li><b>cimg_forY(img,y)</b> : equivalent to : <code>for (int y=0; y&lt;img.dimy(); y++)</code>.</li><li><b>cimg_forZ(img,z)</b> : equivalent to : <code>for (int z=0; z&lt;img.dimz(); z++)</code>.</li><li><b>cimg_forV(img,v)</b> : equivalent to : <code>for (int v=0; v&lt;img.dimv(); v++)</code>.</li></ul>
   145 <p>
   146 Combinations of these macros are also defined as other loop macros, allowing to loop directly over 2D, 3D or 4D images :<ul>
   147 <li><b>cimg_forXY(img,x,y)</b> : equivalent to : <code>cimg_forY(img,y)</code> <code>cimg_forX(img,x)</code>.</li><li><b>cimg_forXZ(img,x,z)</b> : equivalent to : <code>cimg_forZ(img,z)</code> <code>cimg_forX(img,x)</code>.</li><li><b>cimg_forYZ(img,y,z)</b> : equivalent to : <code>cimg_forZ(img,z)</code> <code>cimg_forY(img,y)</code>.</li><li><b>cimg_forXV(img,x,v)</b> : equivalent to : <code>cimg_forV(img,v)</code> <code>cimg_forX(img,x)</code>.</li><li><b>cimg_forYV(img,y,v)</b> : equivalent to : <code>cimg_forV(img,v)</code> <code>cimg_forY(img,y)</code>.</li><li><b>cimg_forZV(img,z,v)</b> : equivalent to : <code>cimg_forV(img,v)</code> <code>cimg_forZ(img,z)</code>.</li><li><b>cimg_forXYZ(img,x,y,z)</b> : equivalent to : <code>cimg_forZ(img,z)</code> <code>cimg_forXY(img,x,y)</code>.</li><li><b>cimg_forXYV(img,x,y,v)</b> : equivalent to : <code>cimg_forV(img,v)</code> <code>cimg_forXY(img,x,y)</code>.</li><li><b>cimg_forXZV(img,x,z,v)</b> : equivalent to : <code>cimg_forV(img,v)</code> <code>cimg_forXZ(img,x,z)</code>.</li><li><b>cimg_forYZV(img,y,z,v)</b> : equivalent to : <code>cimg_forV(img,v)</code> <code>cimg_forYZ(img,y,z)</code>.</li><li><b>cimg_forXYZV(img,x,y,z,v)</b> : equivalent to : <code>cimg_forV(img,v)</code> <code>cimg_forXYZ(img,x,y,z)</code>.</li></ul>
   148 <p>
   149 <ul>
   150 <li>For all these loops, <code>x</code>,<code>y</code>,<code>z</code> and <code>v</code> 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.</li><li><code>img</code> must be a (non empty) <a class="el" href="structcimg__library_1_1CImg.html" title="Class representing an image (up to 4 dimensions wide), each pixel being of type T...">cimg_library::CImg</a> image.</li></ul>
   151 <p>
   152 Here is an example of use that creates an image with a smooth color gradient : <div class="fragment"><pre class="fragment">  CImg&lt;unsigned char&gt; img(256,256,1,3);       <span class="comment">// Define a 256x256 color image</span>
   153   cimg_forXYV(img,x,y,v) { img(x,y,v) = (x+y)*(v+1)/6; }
   154   img.display(<span class="stringliteral">"Color gradient"</span>);
   155 </pre></div><h2><a class="anchor" name="lo5">
   156 Loops over interior regions and borders.</a></h2>
   157 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 :<p>
   158 <ul>
   159 <li><b>cimg_for_insideX(img,x,n)</b> : Loop along the x-axis, except for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_insideY(img,y,n)</b> : Loop along the y-axis, except for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_insideZ(img,z,n)</b> : Loop along the z-axis, except for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_insideV(img,v,n)</b> : Loop along the v-axis, except for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_insideXY(img,x,y,n)</b> : Loop along the (x,y)-axes, excepted for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_insideXYZ(img,x,y,z,n)</b> : Loop along the (x,y,z)-axes, excepted for pixels inside a border of <code>n</code> pixels wide.</li></ul>
   160 <p>
   161 And also :<p>
   162 <ul>
   163 <li><b>cimg_for_borderX(img,x,n)</b> : Loop along the x-axis, only for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_borderY(img,y,n)</b> : Loop along the y-axis, only for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_borderZ(img,z,n)</b> : Loop along the z-axis, only for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_borderV(img,v,n)</b> : Loop along the z-axis, only for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_borderXY(img,x,y,n)</b> : Loop along the (x,y)-axes, only for pixels inside a border of <code>n</code> pixels wide.</li><li><b>cimg_for_borderXYZ(img,x,y,z,n)</b> : Loop along the (x,y,z)-axes, only for pixels inside a border of <code>n</code> pixels wide.</li></ul>
   164 <p>
   165 <ul>
   166 <li>For all these loops, <code>x</code>,<code>y</code>,<code>z</code> and <code>v</code> 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.</li><li><code>img</code> must be a (non empty) <a class="el" href="structcimg__library_1_1CImg.html" title="Class representing an image (up to 4 dimensions wide), each pixel being of type T...">cimg_library::CImg</a> image.</li><li>The constant <code>n</code> stands for the size of the border.</li></ul>
   167 <p>
   168 Here is an example of use, to create a 2d grayscale image with two different intensity gradients : <div class="fragment"><pre class="fragment">  CImg&lt;&gt; img(256,256);
   169   cimg_for_insideXY(img,x,y,50) img(x,y) = x+y;
   170   cimg_for_borderXY(img,x,y,50) img(x,y) = x-y;
   171   img.display();
   172 </pre></div><h2><a class="anchor" name="lo6">
   173 Loops using neighborhoods.</a></h2>
   174 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.<h3><a class="anchor" name="lo7">
   175 Neighborhood-based loops for 2D images</a></h3>
   176 For 2D images, the neighborhood-based loop macros are :<p>
   177 <ul>
   178 <li><b>cimg_for2x2(img,x,y,z,v,I)</b> : Loop along the (x,y)-axes using a centered 2x2 neighborhood.</li><li><b>cimg_for3x3(img,x,y,z,v,I)</b> : Loop along the (x,y)-axes using a centered 3x3 neighborhood.</li><li><b>cimg_for4x4(img,x,y,z,v,I)</b> : Loop along the (x,y)-axes using a centered 4x4 neighborhood.</li><li><b>cimg_for5x5(img,x,y,z,v,I)</b> : Loop along the (x,y)-axes using a centered 5x5 neighborhood.</li></ul>
   179 <p>
   180 For all these loops, <code>x</code> and <code>y</code> 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. <code>img</code> is a non empty CImg&lt;T&gt; image. <code>z</code> and <code>v</code> are constants that define on which image slice and vector channel the loop must apply (usually both 0 for grayscale 2D images). Finally, <code>I</code> is the 2x2, 3x3, 4x4 or 5x5 neighborhood that will be updated with the correct pixel values during the loop (see <a class="el" href="group__cimg__loops.html#lo9">Defining neighborhoods</a>).<h3><a class="anchor" name="lo8">
   181 Neighborhood-based loops for 3D images</a></h3>
   182 For 3D images, the neighborhood-based loop macros are :<p>
   183 <ul>
   184 <li><b>cimg_for2x2x2(img,x,y,z,v,I)</b> : Loop along the (x,y,z)-axes using a centered 2x2x2 neighborhood.</li><li><b>cimg_for3x3x3(img,x,y,z,v,I)</b> : Loop along the (x,y,z)-axes using a centered 3x3x3 neighborhood.</li></ul>
   185 <p>
   186 For all these loops, <code>x</code>, <code>y</code> and <code>z</code> 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. <code>img</code> is a non empty CImg&lt;T&gt; image. <code>v</code> is a constant that defines on which image channel the loop must apply (usually 0 for grayscale 3D images). Finally, <code>I</code> is the 2x2x2 or 3x3x3 neighborhood that will be updated with the correct pixel values during the loop (see <a class="el" href="group__cimg__loops.html#lo9">Defining neighborhoods</a>).<h3><a class="anchor" name="lo9">
   187 Defining neighborhoods</a></h3>
   188 A neighborhood is defined as an instance of a class having operator[] defined. This particularly includes classical C-array, as well as CImg&lt;T&gt; objects.<p>
   189 For instance, a 3x3 neighborhood can be defined either as a 'float[9]' or a 'CImg&lt;float&gt;(3,3)' variable.<h3><a class="anchor" name="lo10">
   190 Using alternate variable names</a></h3>
   191 There are also some useful macros that can be used to define variables that reference the neighborhood elements. There are :<p>
   192 <ul>
   193 <li><b>CImg_2x2(I,type)</b> : Define a 2x2 neighborhood named <code>I</code>, of type <code>type</code>.</li><li><b>CImg_3x3(I,type)</b> : Define a 3x3 neighborhood named <code>I</code>, of type <code>type</code>.</li><li><b>CImg_4x4(I,type)</b> : Define a 4x4 neighborhood named <code>I</code>, of type <code>type</code>.</li><li><b>CImg_5x5(I,type)</b> : Define a 5x5 neighborhood named <code>I</code>, of type <code>type</code>.</li><li><b>CImg_2x2x2(I,type)</b> : Define a 2x2x2 neighborhood named <code>I</code>, of type <code>type</code>.</li><li><b>CImg_3x3x3(I,type)</b> : Define a 3x3x3 neighborhood named <code>I</code>, of type <code>type</code>.</li></ul>
   194 <p>
   195 Actually, <code>I</code> is a <em>generic</em> <em>name</em> for the neighborhood. In fact, these macros declare a <em>set</em> of new variables. For instance, defining a 3x3 neighborhood <code>CImg_3x3(I,float)</code> declares 9 different float variables <code>Ipp</code>,<code>Icp</code>,<code>Inp</code>,<code>Ipc</code>,<code>Icc</code>,<code>Inc</code>,<code>Ipn</code>,<code>Icn</code>,<code>Inn</code> which correspond to each pixel value of a 3x3 neighborhood. Variable indices are <code>p</code>,<code>c</code> or <code>n</code>, and stand respectively for <em>'previous'</em>, <em>'current'</em> and <em>'next'</em>. First indice denotes the <code>x-axis</code>, second indice denotes the <code>y-axis</code>. 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 <code>z-axis</code>. Then, inside a neighborhood loop, you will have the following equivalence :<ul>
   196 <li><code>Ipp = img(x-1,y-1)</code></li><li><code>Icn = img(x,y+1)</code></li><li><code>Inp = img(x+1,y-1)</code></li><li><code>Inpc = img(x+1,y-1,z)</code></li><li><code>Ippn = img(x-1,y-1,z+1)</code></li><li>and so on...</li></ul>
   197 <p>
   198 For bigger neighborhoods, such as 4x4 or 5x5 neighborhoods, two additionnal indices are introduced : <code>a</code> (stands for <em>'after'</em>) and <code>b</code> (stands for <em>'before'</em>), so that :<ul>
   199 <li><code>Ibb = img(x-2,y-2)</code></li><li><code>Ina = img(x+1,y+2)</code></li><li>and so on...</li></ul>
   200 <p>
   201 The value of a neighborhood pixel outside the image range (image border problem) is automatically set to the same values than the nearest valid pixel in the image (this is also called the <em>Neumann</em> <em>border</em> <em>condition</em>).<h3><a class="anchor" name="lo11">
   202 Example codes</a></h3>
   203 More than a long discussion, the above example will demonstrate how to compute the gradient norm of a 3D volume using the <code>cimg_for3x3x3()</code> loop macro :<p>
   204 <div class="fragment"><pre class="fragment">  CImg&lt;float&gt; volume(<span class="stringliteral">"IRM.hdr"</span>);        <span class="comment">// Load an IRM volume from an Analyze7.5 file</span>
   205   CImg_3x3x3(I,<span class="keywordtype">float</span>);                  <span class="comment">// Define a 3x3x3 neighborhood</span>
   206   CImg&lt;float&gt; gradnorm(volume);         <span class="comment">// Create an image with same size as 'volume'</span>
   207   cimg_for3x3x3(volume,x,y,z,0,I) {     <span class="comment">// Loop over the volume, using the neighborhood I</span>
   208     <span class="keyword">const</span> <span class="keywordtype">float</span> ix = 0.5f*(Incc-Ipcc);  <span class="comment">// Compute the derivative along the x-axis.</span>
   209     <span class="keyword">const</span> <span class="keywordtype">float</span> iy = 0.5f*(Icnc-Icpc);  <span class="comment">// Compute the derivative along the y-axis.</span>
   210     <span class="keyword">const</span> <span class="keywordtype">float</span> iz = 0.5f*(Iccn-Iccp);  <span class="comment">// Compute the derivative along the z-axis.</span>
   211     gradnorm(x,y,z) = std::sqrt(ix*ix+iy*iy+iz*iz);  <span class="comment">// Set the gradient norm in the destination image</span>
   212   }
   213   gradnorm.display(<span class="stringliteral">"Gradient norm"</span>);
   214 </pre></div><p>
   215 And the following example shows how to deal with neighborhood references to blur a color image by averaging pixel values on a 5x5 neighborhood.<p>
   216 <div class="fragment"><pre class="fragment">  CImg&lt;unsigned char&gt; src(<span class="stringliteral">"image_color.jpg"</span>), dest(src,<span class="keyword">false</span>), neighbor(5,5);  <span class="comment">// Image definitions.</span>
   217   <span class="keyword">typedef</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> uchar;             <span class="comment">// Avoid space in the second parameter of the macro CImg_5x5x1 below.</span>
   218   CImg&lt;&gt; N(5,5);                           <span class="comment">// Define a 5x5 neighborhood as a 5x5 image.</span>
   219   cimg_forV(src,k)                         <span class="comment">// Standard loop on color channels</span>
   220      cimg_for5x5(src,x,y,0,k,N)            <span class="comment">// 5x5 neighborhood loop.</span>
   221        dest(x,y,k) = N.sum()/(5*5);        <span class="comment">// Averaging pixels to filter the color image.</span>
   222   CImgList&lt;<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>&gt; visu(src,dest);
   223   visu.display("Original + Filtered");     <span class="comment">// Display both original and filtered image.</span>
   224 </pre></div><p>
   225 As you can see, explaining the use of the CImg neighborhood macros is actually more difficult than using them ! </div>
   226 <hr noshade="noshade" size="1" width="100%">
   227 <a href="http://sourceforge.net">
   228 <img src="http://sourceforge.net/sflogo.php?group_id=96492&amp;type=3" border="0" height="37" width="125"></img>
   229 </a>
   230 <!-- Start of StatCounter Code -->
   231 <script type="text/javascript" language="javascript">
   232 <!--
   233 var sc_project=895001;
   234 var sc_invisible=1;
   235 var sc_partition=7;
   236 var sc_security="5ea85181";
   237 //-->
   238 </script>
   239 <script type="text/javascript" language="javascript" src="http://www.statcounter.com/counter/counter.js"></script><noscript><a href="http://www.statcounter.com/" target="_blank"><img  src="http://c8.statcounter.com/counter.php?sc_project=895001&amp;java=0&amp;security=5ea85181&amp;invisible=1" alt="counter stats" border="0"></a> </noscript>
   240 <!-- End of StatCounter Code -->
   241 </body>
   242 </html>