/Src/Dependencies/Boost/libs/gil/doc/html/g_i_l_0009.html

http://hadesmem.googlecode.com/ · HTML · 99 lines · 78 code · 5 blank · 16 comment · 0 complexity · 358a539d548853c4b7cd5ae63fe6041d MD5 · raw file

  1. <!-- Copyright 2008 Lubomir Bourdev and Hailin Jin
  2. Distributed under the Boost Software License, Version 1.0.
  3. (See accompanying file LICENSE_1_0.txt or copy at
  4. http://www.boost.org/LICENSE_1_0.txt)
  5. -->
  6. <!--
  7. Copyright 2005-2007 Adobe Systems Incorporated
  8. Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
  9. or a copy at http://stlab.adobe.com/licenses.html)
  10. Some files are held under additional license.
  11. Please see "http://stlab.adobe.com/licenses.html" for more information.
  12. -->
  13. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  14. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  15. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  16. <head>
  17. <TITLE>Generic Image Library: Histogram Example</TITLE>
  18. <META HTTP-EQUIV="content-type" CONTENT="text/html;charset=ISO-8859-1"/>
  19. <LINK TYPE="text/css" REL="stylesheet" HREF="adobe_source.css"/>
  20. </head>
  21. <body>
  22. <table border="0" cellspacing="0" cellpadding="0" style='width: 100%; margin: 0; padding: 0'><tr>
  23. <td width="100%" valign="top" style='padding-left: 10px; padding-right: 10px; padding-bottom: 10px'>
  24. <div class="qindex"><a class="qindex" href="index.html">Modules</a>
  25. | <a class="qindex" href="classes.html">Alphabetical List</a>
  26. | <a class="qindex" href="annotated.html">Class List</a>
  27. | <a class="qindex" href="dirs.html">Directories</a>
  28. | <a class="qindex" href="files.html">File List</a>
  29. | <a class="qindex" href="../index.html">GIL Home Page</a>
  30. </div>
  31. <!-- End Header -->
  32. <!-- Generated by Doxygen 1.5.6 -->
  33. <div class="contents">
  34. <h1><a class="anchor" name="BeforeAfterExample">Histogram Example </a></h1>Actual commercial code that computes the luminosity histogram (variable names have been changed and unrelated parts removed):<p>
  35. <div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> luminosity_hist(<span class="keyword">const</span> uint8 *r, <span class="keyword">const</span> uint8 *g, <span class="keyword">const</span> uint8 *b, <span class="keywordtype">int</span> rows, <span class="keywordtype">int</span> cols, <span class="keywordtype">int</span> sRowBytes, Histogram *hist)
  36. {
  37. <span class="keywordflow">for</span> (<span class="keywordtype">int</span> r=0; r&lt;rows; r++)
  38. {
  39. <span class="keywordflow">for</span> (<span class="keywordtype">int</span> c=0; c&lt;cols; c++)
  40. {
  41. <span class="keywordtype">int</span> v=RGBToGray(r[c],g[c],b[c]);
  42. (*hist)[v]++;
  43. }
  44. r+=sRowBytes;
  45. g+=sRowBytes;
  46. b+=sRowBytes;
  47. }
  48. }
  49. </pre></div><p>
  50. <ul>
  51. <li>Works only for RGB (duplicate versions exist for other color spaces)</li><li>Works only for 8-bit images (duplicate versions exist)</li><li>Works only for planar images</li></ul>
  52. <p>
  53. Histogram using GIL:<p>
  54. <div class="fragment"><pre class="fragment"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> GrayView, <span class="keyword">typename</span> R&gt;
  55. <span class="keywordtype">void</span> grayimage_histogram(GrayView&amp; img, R&amp; hist) {
  56. <span class="keywordflow">for</span> (<span class="keyword">typename</span> GrayView::iterator it=img.begin(); it!=img.end(); ++it)
  57. ++hist[*it];
  58. }
  59. <span class="keyword">template</span> &lt;<span class="keyword">typename</span> View, <span class="keyword">typename</span> R&gt;
  60. <span class="keywordtype">void</span> luminosity8bit_hist(View&amp; img, R&amp; hist)
  61. {
  62. grayimage_histogram(color_converted_view&lt;gray8_pixel_t&gt;(img),hist);
  63. }
  64. </pre></div><p>
  65. using <code>boost::lambda</code> the GIL version can be written even simpler: <div class="fragment"><pre class="fragment"><span class="keyword">using</span> boost::lambda;
  66. <span class="keyword">template</span> &lt;<span class="keyword">typename</span> GrayView, <span class="keyword">typename</span> R&gt;
  67. <span class="keywordtype">void</span> grayimage_histogram(GrayView&amp; img, R&amp; hist)
  68. {
  69. for_each_pixel(img, ++var(hist)[_1]);
  70. }
  71. </pre></div><p>
  72. The GIL version:<ul>
  73. <li>Works with any supported channel depth, color space, channel ordering (RGB vs BGR), and row alignment policy.</li><li>Works for both planar and interleaved images.</li><li>Works with new color spaces, channel depths and image types that can be provided in future extensions of GIL</li><li>The second version is as efficient as the hand-coded version</li></ul>
  74. <p>
  75. It is also very flexible. For example, to compute the histogram of the second channel of the top left quadrant of the image, taking every other row and column, call:<p>
  76. <div class="fragment"><pre class="fragment">grayimage_histogram(
  77. nth_channel_view(
  78. subsampled_view(
  79. subimage_view(img, 0,0, img.width()/2,img.height()/2), <span class="comment">// upper left quadrant</span>
  80. 2, 2 <span class="comment">// skip every other row and column</span>
  81. ),
  82. 1 <span class="comment">// index of the second channel (for example, green for RGB)</span>
  83. ),
  84. hist
  85. );
  86. </pre></div><p>
  87. Note that no extra memory is allocated and no images are copied - GIL operates on the source pixels of <code>img</code> directly. </div>
  88. <hr size="1"><address style="text-align: right;"><small>Generated on Sat May 2 13:50:16 2009 for Generic Image Library by&nbsp;
  89. <a href="http://www.doxygen.org/index.html">
  90. <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
  91. </body>
  92. </html>