2 \page BeforeAfterExample Histogram Example
4 Actual commercial code that computes the luminosity histogram (variable names have been changed and unrelated parts removed):
7 void luminosity_hist(const uint8 *r, const uint8 *g, const uint8 *b, int rows, int cols, int sRowBytes, Histogram *hist)
9 for (int r=0; r<rows; r++)
11 for (int c=0; c<cols; c++)
13 int v=RGBToGray(r[c],g[c],b[c]);
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
27 <p> Histogram using GIL:
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)
37 template <typename View, typename R>
38 void luminosity8bit_hist(View& img, R& hist)
40 grayimage_histogram(color_converted_view<gray8_pixel_t>(img),hist);
44 using \p boost::lambda the GIL version can be written even simpler:
48 template <typename GrayView, typename R>
49 void grayimage_histogram(GrayView& img, R& hist)
51 for_each_pixel(img, ++var(hist)[_1]);
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
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:
68 subimage_view(img, 0,0, img.width()/2,img.height()/2), // upper left quadrant
69 2, 2 // skip every other row and column
71 1 // index of the second channel (for example, green for RGB)
77 Note that no extra memory is allocated and no images are copied - GIL operates on the source pixels of \p img directly.