• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5<html xmlns="http://www.w3.org/1999/xhtml">
6  <head>
7    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
9    <title>Tutorial: Histogram - Boost.GIL  documentation</title>
10    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
11    <link rel="stylesheet" href="../_static/style.css" type="text/css" />
12    <script type="text/javascript">
13      var DOCUMENTATION_OPTIONS = {
14          URL_ROOT:    '../',
15          VERSION:     '',
16          COLLAPSE_MODINDEX: false,
17          FILE_SUFFIX: '.html'
18      };
19    </script>
20    <script type="text/javascript" src="../_static/jquery.js"></script>
21    <script type="text/javascript" src="../_static/underscore.js"></script>
22    <script type="text/javascript" src="../_static/doctools.js"></script>
23    <link rel="index" title="Index" href="../genindex.html" />
24    <link rel="search" title="Search" href="../search.html" />
25    <link rel="top" title="Boost.GIL  documentation" href="../index.html" />
26    <link rel="next" title="Tutorial: Image Gradient" href="gradient.html" />
27    <link rel="prev" title="Tutorial: Video Lecture" href="video.html" />
28  </head>
29  <body>
30    <div class="header">
31    <table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
32    "header">
33      <tr>
34        <td valign="top" width="300">
35          <h3><a href="../index.html"><img
36          alt="C++ Boost" src="../_static/gil.png" border="0"></a></h3>
37        </td>
38
39        <td >
40          <h1 align="center"><a href="../index.html"></a></h1>
41        </td>
42	<td>
43      <div id="searchbox" style="display: none">
44        <form class="search" action="../search.html" method="get">
45          <input type="text" name="q" size="18" />
46          <input type="submit" value="Search" />
47          <input type="hidden" name="check_keywords" value="yes" />
48          <input type="hidden" name="area" value="default" />
49        </form>
50      </div>
51      <script type="text/javascript">$('#searchbox').show(0);</script>
52	</td>
53      </tr>
54    </table>
55    </div>
56    <hr/>
57    <div class="content">
58    <div class="navbar" style="text-align:right;">
59
60
61      <a class="prev" title="Tutorial: Video Lecture" href="video.html"><img src="../_static/prev.png" alt="prev"/></a>
62      <a class="next" title="Tutorial: Image Gradient" href="gradient.html"><img src="../_static/next.png" alt="next"/></a>
63
64    </div>
65
66  <div class="section" id="tutorial-histogram">
67<h1>Tutorial: Histogram</h1>
68<div class="contents local topic" id="contents">
69<ul class="simple">
70<li><a class="reference internal" href="#original-implementation" id="id1">Original implementation</a></li>
71<li><a class="reference internal" href="#gil-implementation" id="id2">GIL implementation</a></li>
72</ul>
73</div>
74<p>This is a short tutorial presenting an example of a very simple sample of code
75from an existing code base that calculates histogram of an image.
76Next, the program is rewritten using GIL featres.</p>
77<div class="section" id="original-implementation">
78<h2><a class="toc-backref" href="#id1">Original implementation</a></h2>
79<p>Actual code from a commercial software product that computes the luminosity
80histogram (variable names have been changed and unrelated parts removed):</p>
81<div class="highlight-cpp"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">luminosity_hist</span><span class="p">(</span>
82    <span class="n">std</span><span class="o">::</span><span class="kt">uint8_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">r</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="kt">uint8_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">g</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="kt">uint8_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">b</span><span class="p">,</span>
83    <span class="kt">int</span> <span class="n">rows</span><span class="p">,</span> <span class="kt">int</span> <span class="n">cols</span><span class="p">,</span> <span class="kt">int</span> <span class="n">sRowBytes</span><span class="p">,</span> <span class="n">Histogram</span><span class="o">*</span> <span class="n">hist</span><span class="p">)</span>
84<span class="p">{</span>
85    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">r</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">r</span> <span class="o">&lt;</span> <span class="n">rows</span><span class="p">;</span> <span class="n">r</span><span class="o">++</span><span class="p">)</span>
86    <span class="p">{</span>
87        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">c</span> <span class="o">&lt;</span> <span class="n">cols</span><span class="p">;</span> <span class="n">c</span><span class="o">++</span><span class="p">)</span>
88        <span class="p">{</span>
89            <span class="kt">int</span> <span class="n">v</span> <span class="o">=</span> <span class="n">RGBToGray</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="n">c</span><span class="p">],</span> <span class="n">g</span><span class="p">[</span><span class="n">c</span><span class="p">],</span> <span class="n">b</span><span class="p">[</span><span class="n">c</span><span class="p">]);</span> <span class="c1">// call internal function or macro</span>
90            <span class="p">(</span><span class="o">*</span><span class="n">hist</span><span class="p">)[</span><span class="n">v</span><span class="p">]</span><span class="o">++</span><span class="p">;</span>
91        <span class="p">}</span>
92        <span class="n">r</span> <span class="o">+=</span> <span class="n">sRowBytes</span><span class="p">;</span>
93        <span class="n">g</span> <span class="o">+=</span> <span class="n">sRowBytes</span><span class="p">;</span>
94        <span class="n">b</span> <span class="o">+=</span> <span class="n">sRowBytes</span><span class="p">;</span>
95    <span class="p">}</span>
96<span class="p">}</span>
97</pre></div>
98</div>
99<p>Let&#8217;s consider the following issues of the implementation above:</p>
100<ul class="simple">
101<li>Works only for RGB (duplicate versions exist for other color spaces)</li>
102<li>Works only for 8-bit images (duplicate versions exist)</li>
103<li>Works only for planar images</li>
104</ul>
105</div>
106<div class="section" id="gil-implementation">
107<h2><a class="toc-backref" href="#id2">GIL implementation</a></h2>
108<div class="highlight-cpp"><div class="highlight"><pre><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">GrayView</span><span class="p">,</span> <span class="k">typename</span> <span class="n">R</span><span class="o">&gt;</span>
109<span class="kt">void</span> <span class="n">grayimage_histogram</span><span class="p">(</span><span class="n">GrayView</span><span class="o">&amp;</span> <span class="n">img</span><span class="p">,</span> <span class="n">R</span><span class="o">&amp;</span> <span class="n">hist</span><span class="p">)</span>
110<span class="p">{</span>
111    <span class="k">for</span> <span class="p">(</span><span class="k">typename</span> <span class="n">GrayView</span><span class="o">::</span><span class="n">iterator</span> <span class="n">it</span><span class="o">=</span><span class="n">img</span><span class="p">.</span><span class="n">begin</span><span class="p">();</span> <span class="n">it</span><span class="o">!=</span><span class="n">img</span><span class="p">.</span><span class="n">end</span><span class="p">();</span> <span class="o">++</span><span class="n">it</span><span class="p">)</span>
112        <span class="o">++</span><span class="n">hist</span><span class="p">[</span><span class="o">*</span><span class="n">it</span><span class="p">];</span>
113<span class="p">}</span>
114
115<span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">View</span><span class="p">,</span> <span class="k">typename</span> <span class="n">R</span><span class="o">&gt;</span>
116<span class="kt">void</span> <span class="n">luminosity8bit_hist</span><span class="p">(</span><span class="n">View</span><span class="o">&amp;</span> <span class="n">img</span><span class="p">,</span> <span class="n">R</span><span class="o">&amp;</span> <span class="n">hist</span><span class="p">)</span>
117<span class="p">{</span>
118    <span class="n">grayimage_histogram</span><span class="p">(</span><span class="n">color_converted_view</span><span class="o">&lt;</span><span class="n">gray8_pixel_t</span><span class="o">&gt;</span><span class="p">(</span><span class="n">img</span><span class="p">),</span><span class="n">hist</span><span class="p">);</span>
119<span class="p">}</span>
120</pre></div>
121</div>
122<p>Using the Boost.Lambda library (or C++11 lambda) features it can written
123even simpler:</p>
124<div class="highlight-cpp"><div class="highlight"><pre><span class="k">using</span> <span class="n">boost</span><span class="o">::</span><span class="n">lambda</span><span class="p">;</span>
125
126<span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">GrayView</span><span class="p">,</span> <span class="k">typename</span> <span class="n">R</span><span class="o">&gt;</span>
127<span class="kt">void</span> <span class="n">grayimage_histogram</span><span class="p">(</span><span class="n">GrayView</span><span class="o">&amp;</span> <span class="n">img</span><span class="p">,</span> <span class="n">R</span><span class="o">&amp;</span> <span class="n">hist</span><span class="p">)</span>
128<span class="p">{</span>
129    <span class="n">for_each_pixel</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="o">++</span><span class="n">var</span><span class="p">(</span><span class="n">hist</span><span class="p">)[</span><span class="n">_1</span><span class="p">]);</span>
130<span class="p">}</span>
131</pre></div>
132</div>
133<p>Let&#8217;s consider the following advantages of the GIL version:</p>
134<ul class="simple">
135<li>Works with any supported channel depth, color space, channel ordering
136(RGB vs BGR), and row alignment policy.</li>
137<li>Works for both planar and interleaved images.</li>
138<li>Works with new color spaces, channel depths and image types that can be
139provided in future extensions of GIL</li>
140<li>The second version is as efficient as the hand-coded version</li>
141</ul>
142<p>Shortly, it is also very flexible.</p>
143<p>For example, to compute the histogram of the second channel of the top left
144quadrant of the image, taking every other row and column, call:</p>
145<div class="highlight-cpp"><div class="highlight"><pre><span class="n">grayimage_histogram</span><span class="p">(</span>
146    <span class="n">nth_channel_view</span><span class="p">(</span>
147        <span class="n">subsampled_view</span><span class="p">(</span>
148            <span class="n">subimage_view</span><span class="p">(</span><span class="n">img</span><span class="p">,</span>
149                <span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span> <span class="n">img</span><span class="p">.</span><span class="n">width</span><span class="p">()</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">img</span><span class="p">.</span><span class="n">height</span><span class="p">()</span> <span class="o">/</span> <span class="mi">2</span><span class="p">),</span> <span class="c1">// upper left quadrant</span>
150                <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span>                                     <span class="c1">// skip every other row and column</span>
151            <span class="p">),</span>
152        <span class="mi">1</span>   <span class="c1">// index of the second channel (for example, green for RGB)</span>
153    <span class="p">),</span>
154    <span class="n">hist</span>
155<span class="p">);</span>
156</pre></div>
157</div>
158<p>Since GIL operates on the source pixels of <code class="docutils literal"><span class="pre">img</span></code> object directly, no extra
159memory is allocated and no images are copied.</p>
160</div>
161</div>
162
163
164    <div class="navbar" style="text-align:right;">
165
166
167      <a class="prev" title="Tutorial: Video Lecture" href="video.html"><img src="../_static/prev.png" alt="prev"/></a>
168      <a class="next" title="Tutorial: Image Gradient" href="gradient.html"><img src="../_static/next.png" alt="next"/></a>
169
170    </div>
171    </div>
172    <div class="footer" role="contentinfo">
173      Last updated on 2020-08-11 15:08:48.
174      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
175    </div>
176  </body>
177</html>