blob: 70a8a1fbf70cc41ce23c10e9b616c63af91d460d [file] [log] [blame]
<!-- Copyright 2008 Lubomir Bourdev and Hailin Jin
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
-->
<!--
Copyright 2005-2007 Adobe Systems Incorporated
Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
or a copy at http://stlab.adobe.com/licenses.html)
Some files are held under additional license.
Please see "http://stlab.adobe.com/licenses.html" for more information.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<TITLE>Generic Image Library: Histogram Example</TITLE>
<META HTTP-EQUIV="content-type" CONTENT="text/html;charset=ISO-8859-1"/>
<LINK TYPE="text/css" REL="stylesheet" HREF="adobe_source.css"/>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" style='width: 100%; margin: 0; padding: 0'><tr>
<td width="100%" valign="top" style='padding-left: 10px; padding-right: 10px; padding-bottom: 10px'>
<div class="qindex"><a class="qindex" href="index.html">Modules</a>
| <a class="qindex" href="classes.html">Alphabetical List</a>
| <a class="qindex" href="annotated.html">Class List</a>
| <a class="qindex" href="dirs.html">Directories</a>
| <a class="qindex" href="files.html">File List</a>
| <a class="qindex" href="../index.html">GIL Home Page</a>
</div>
<!-- End Header -->
<!-- Generated by Doxygen 1.5.6 -->
<div class="contents">
<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>
<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)
{
<span class="keywordflow">for</span> (<span class="keywordtype">int</span> r=0; r&lt;rows; r++)
{
<span class="keywordflow">for</span> (<span class="keywordtype">int</span> c=0; c&lt;cols; c++)
{
<span class="keywordtype">int</span> v=RGBToGray(r[c],g[c],b[c]);
(*hist)[v]++;
}
r+=sRowBytes;
g+=sRowBytes;
b+=sRowBytes;
}
}
</pre></div><p>
<ul>
<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>
<p>
Histogram using GIL:<p>
<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;
<span class="keywordtype">void</span> grayimage_histogram(GrayView&amp; img, R&amp; hist) {
<span class="keywordflow">for</span> (<span class="keyword">typename</span> GrayView::iterator it=img.begin(); it!=img.end(); ++it)
++hist[*it];
}
<span class="keyword">template</span> &lt;<span class="keyword">typename</span> View, <span class="keyword">typename</span> R&gt;
<span class="keywordtype">void</span> luminosity8bit_hist(View&amp; img, R&amp; hist)
{
grayimage_histogram(color_converted_view&lt;gray8_pixel_t&gt;(img),hist);
}
</pre></div><p>
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;
<span class="keyword">template</span> &lt;<span class="keyword">typename</span> GrayView, <span class="keyword">typename</span> R&gt;
<span class="keywordtype">void</span> grayimage_histogram(GrayView&amp; img, R&amp; hist)
{
for_each_pixel(img, ++var(hist)[_1]);
}
</pre></div><p>
The GIL version:<ul>
<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>
<p>
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>
<div class="fragment"><pre class="fragment">grayimage_histogram(
nth_channel_view(
subsampled_view(
subimage_view(img, 0,0, img.width()/2,img.height()/2), <span class="comment">// upper left quadrant</span>
2, 2 <span class="comment">// skip every other row and column</span>
),
1 <span class="comment">// index of the second channel (for example, green for RGB)</span>
),
hist
);
</pre></div><p>
Note that no extra memory is allocated and no images are copied - GIL operates on the source pixels of <code>img</code> directly. </div>
<hr size="1"><address style="text-align: right;"><small>Generated on Sat May 2 13:50:16 2009 for Generic Image Library by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>