]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | \page BeforeAfterExample Histogram Example | |
3 | ||
4 | Actual commercial code that computes the luminosity histogram (variable names have been changed and unrelated parts removed): | |
5 | ||
6 | \code | |
7 | void luminosity_hist(const uint8 *r, const uint8 *g, const uint8 *b, int rows, int cols, int sRowBytes, Histogram *hist) | |
8 | { | |
9 | for (int r=0; r<rows; r++) | |
10 | { | |
11 | for (int c=0; c<cols; c++) | |
12 | { | |
13 | int v=RGBToGray(r[c],g[c],b[c]); | |
14 | (*hist)[v]++; | |
15 | } | |
16 | r+=sRowBytes; | |
17 | g+=sRowBytes; | |
18 | b+=sRowBytes; | |
19 | } | |
20 | } | |
21 | \endcode | |
22 | ||
23 | - Works only for RGB (duplicate versions exist for other color spaces) | |
24 | - Works only for 8-bit images (duplicate versions exist) | |
25 | - Works only for planar images | |
26 | ||
27 | <p> Histogram using GIL: | |
28 | ||
29 | ||
30 | \code | |
31 | template <typename GrayView, typename R> | |
32 | void grayimage_histogram(GrayView& img, R& hist) { | |
33 | for (typename GrayView::iterator it=img.begin(); it!=img.end(); ++it) | |
34 | ++hist[*it]; | |
35 | } | |
36 | ||
37 | template <typename View, typename R> | |
38 | void luminosity8bit_hist(View& img, R& hist) | |
39 | { | |
40 | grayimage_histogram(color_converted_view<gray8_pixel_t>(img),hist); | |
41 | } | |
42 | \endcode | |
43 | ||
44 | using \p boost::lambda the GIL version can be written even simpler: | |
45 | \code | |
46 | using boost::lambda; | |
47 | ||
48 | template <typename GrayView, typename R> | |
49 | void grayimage_histogram(GrayView& img, R& hist) | |
50 | { | |
51 | for_each_pixel(img, ++var(hist)[_1]); | |
52 | } | |
53 | \endcode | |
54 | ||
55 | The GIL version: | |
56 | - Works with any supported channel depth, color space, channel ordering (RGB vs BGR), and row alignment policy. | |
57 | - Works for both planar and interleaved images. | |
58 | - Works with new color spaces, channel depths and image types that can be provided in future extensions of GIL | |
59 | - The second version is as efficient as the hand-coded version | |
60 | ||
61 | It is also very flexible. For example, to compute the histogram of the second channel of the top left quadrant of the image, | |
62 | taking every other row and column, call: | |
63 | ||
64 | \code | |
65 | grayimage_histogram( | |
66 | nth_channel_view( | |
67 | subsampled_view( | |
68 | subimage_view(img, 0,0, img.width()/2,img.height()/2), // upper left quadrant | |
69 | 2, 2 // skip every other row and column | |
70 | ), | |
71 | 1 // index of the second channel (for example, green for RGB) | |
72 | ), | |
73 | hist | |
74 | ); | |
75 | \endcode | |
76 | ||
77 | Note that no extra memory is allocated and no images are copied - GIL operates on the source pixels of \p img directly. | |
78 | */ |