histogram.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #include <boost/gil.hpp>
  9. #include <boost/gil/extension/io/jpeg.hpp>
  10. #include <algorithm>
  11. #include <fstream>
  12. // Example file to demonstrate a way to compute histogram
  13. using namespace boost::gil;
  14. template <typename GrayView, typename R>
  15. void gray_image_hist(GrayView const& img_view, R& hist)
  16. {
  17. for (auto it = img_view.begin(); it != img_view.end(); ++it)
  18. ++hist[*it];
  19. // Alternatively, prefer the algorithm with lambda
  20. // for_each_pixel(img_view, [&hist](gray8_pixel_t const& pixel) {
  21. // ++hist[pixel];
  22. // });
  23. }
  24. template <typename V, typename R>
  25. void get_hist(const V& img_view, R& hist) {
  26. gray_image_hist(color_converted_view<gray8_pixel_t>(img_view), hist);
  27. }
  28. int main() {
  29. rgb8_image_t img;
  30. read_image("test.jpg", img, jpeg_tag());
  31. int histogram[256];
  32. std::fill(histogram,histogram + 256, 0);
  33. get_hist(const_view(img), histogram);
  34. std::fstream histo_file("out-histogram.txt", std::ios::out);
  35. for(std::size_t ii = 0; ii < 256; ++ii)
  36. histo_file << histogram[ii] << std::endl;
  37. histo_file.close();
  38. return 0;
  39. }