]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <!-- Copyright 2008 Lubomir Bourdev and Hailin Jin |
2 | ||
3 | Distributed under the Boost Software License, Version 1.0. | |
4 | (See accompanying file LICENSE_1_0.txt or copy at | |
5 | http://www.boost.org/LICENSE_1_0.txt) | |
6 | --> | |
7 | ||
8 | <!-- | |
9 | Copyright 2005-2007 Adobe Systems Incorporated | |
10 | Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt | |
11 | or a copy at http://stlab.adobe.com/licenses.html) | |
12 | ||
13 | Some files are held under additional license. | |
14 | Please see "http://stlab.adobe.com/licenses.html" for more information. | |
15 | --> | |
16 | ||
17 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
18 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
19 | <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> | |
20 | ||
21 | <head> | |
22 | <TITLE>Generic Image Library: Histogram Example</TITLE> | |
23 | <META HTTP-EQUIV="content-type" CONTENT="text/html;charset=ISO-8859-1"/> | |
24 | <LINK TYPE="text/css" REL="stylesheet" HREF="adobe_source.css"/> | |
25 | </head> | |
26 | <body> | |
27 | <table border="0" cellspacing="0" cellpadding="0" style='width: 100%; margin: 0; padding: 0'><tr> | |
28 | <td width="100%" valign="top" style='padding-left: 10px; padding-right: 10px; padding-bottom: 10px'> | |
29 | <div class="qindex"><a class="qindex" href="index.html">Modules</a> | |
30 | | <a class="qindex" href="classes.html">Alphabetical List</a> | |
31 | | <a class="qindex" href="annotated.html">Class List</a> | |
32 | | <a class="qindex" href="dirs.html">Directories</a> | |
33 | | <a class="qindex" href="files.html">File List</a> | |
34 | | <a class="qindex" href="../index.html">GIL Home Page</a> | |
35 | </div> | |
36 | <!-- End Header --> | |
37 | <!-- Generated by Doxygen 1.5.6 --> | |
38 | <div class="contents"> | |
39 | <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> | |
40 | <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) | |
41 | { | |
42 | <span class="keywordflow">for</span> (<span class="keywordtype">int</span> r=0; r<rows; r++) | |
43 | { | |
44 | <span class="keywordflow">for</span> (<span class="keywordtype">int</span> c=0; c<cols; c++) | |
45 | { | |
46 | <span class="keywordtype">int</span> v=RGBToGray(r[c],g[c],b[c]); | |
47 | (*hist)[v]++; | |
48 | } | |
49 | r+=sRowBytes; | |
50 | g+=sRowBytes; | |
51 | b+=sRowBytes; | |
52 | } | |
53 | } | |
54 | </pre></div><p> | |
55 | <ul> | |
56 | <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> | |
57 | <p> | |
58 | Histogram using GIL:<p> | |
59 | <div class="fragment"><pre class="fragment"><span class="keyword">template</span> <<span class="keyword">typename</span> GrayView, <span class="keyword">typename</span> R> | |
60 | <span class="keywordtype">void</span> grayimage_histogram(GrayView& img, R& hist) { | |
61 | <span class="keywordflow">for</span> (<span class="keyword">typename</span> GrayView::iterator it=img.begin(); it!=img.end(); ++it) | |
62 | ++hist[*it]; | |
63 | } | |
64 | ||
65 | <span class="keyword">template</span> <<span class="keyword">typename</span> View, <span class="keyword">typename</span> R> | |
66 | <span class="keywordtype">void</span> luminosity8bit_hist(View& img, R& hist) | |
67 | { | |
68 | grayimage_histogram(color_converted_view<gray8_pixel_t>(img),hist); | |
69 | } | |
70 | </pre></div><p> | |
71 | 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; | |
72 | ||
73 | <span class="keyword">template</span> <<span class="keyword">typename</span> GrayView, <span class="keyword">typename</span> R> | |
74 | <span class="keywordtype">void</span> grayimage_histogram(GrayView& img, R& hist) | |
75 | { | |
76 | for_each_pixel(img, ++var(hist)[_1]); | |
77 | } | |
78 | </pre></div><p> | |
79 | The GIL version:<ul> | |
80 | <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> | |
81 | <p> | |
82 | 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> | |
83 | <div class="fragment"><pre class="fragment">grayimage_histogram( | |
84 | nth_channel_view( | |
85 | subsampled_view( | |
86 | subimage_view(img, 0,0, img.width()/2,img.height()/2), <span class="comment">// upper left quadrant</span> | |
87 | 2, 2 <span class="comment">// skip every other row and column</span> | |
88 | ), | |
89 | 1 <span class="comment">// index of the second channel (for example, green for RGB)</span> | |
90 | ), | |
91 | hist | |
92 | ); | |
93 | </pre></div><p> | |
94 | Note that no extra memory is allocated and no images are copied - GIL operates on the source pixels of <code>img</code> directly. </div> | |
95 | <hr size="1"><address style="text-align: right;"><small>Generated on Sat May 2 13:50:16 2009 for Generic Image Library by | |
96 | <a href="http://www.doxygen.org/index.html"> | |
97 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address> | |
98 | </body> | |
99 | </html> |