2
0
mirror of https://github.com/boostorg/gil.git synced 2026-01-19 16:22:14 +00:00
Files
gil/example/tutorial_histogram.cpp
Nicolas Herry 0b24f4cdbf Ensure all examples build without error (#628)
* Added all missing examples, dodgy Jamfile still
* Fixed attributions and Jamfile indent
* One readme per example: synopsis, build and exec reqs
* Cleaned up example/convolve2d.cpp
* Added example target to root Jamfile

Closes #436
2021-11-10 18:21:02 +01:00

52 lines
1.3 KiB
C++

//
// Copyright 2005-2007 Adobe Systems Incorporated
//
// 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
//
#include <boost/gil.hpp>
#include <boost/gil/extension/io/jpeg.hpp>
#include <algorithm>
#include <fstream>
// Demonstrates a way to compute a histogram
// This example relies on a colour-converting view to work with a greyscale view
using namespace boost::gil;
template <typename GrayView, typename R>
void gray_image_hist(GrayView const& img_view, R& hist)
{
for (auto it = img_view.begin(); it != img_view.end(); ++it)
++hist[*it];
// Alternatively, prefer the algorithm with lambda
// for_each_pixel(img_view, [&hist](gray8_pixel_t const& pixel) {
// ++hist[pixel];
// });
}
template <typename V, typename R>
void get_hist(const V& img_view, R& hist) {
gray_image_hist(color_converted_view<gray8_pixel_t>(img_view), hist);
}
int main() {
rgb8_image_t img;
read_image("test.jpg", img, jpeg_tag());
int histogram[256];
std::fill(histogram,histogram + 256, 0);
get_hist(const_view(img), histogram);
std::fstream histo_file("out-histogram.txt", std::ios::out);
for(std::size_t ii = 0; ii < 256; ++ii)
histo_file << histogram[ii] << std::endl;
histo_file.close();
return 0;
}