SHARE
TWEET

Untitled

a guest Jan 29th, 2020 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ImageProcessing.h"
  2. #include "Timer.h"
  3. #include <array>
  4. #include <cassert>
  5. #include <iostream>
  6. #include <future>
  7.  
  8. enum RGB {R = 0, G = 1, B = 2};
  9. constexpr auto MAX_VALUE = std::numeric_limits<uint16_t>::max();
  10. using Histogram = std::array<size_t, MAX_VALUE>;
  11.  
  12. /*
  13.     We need to pass an image through reference
  14.  */
  15. void calculateHistogram(Image const & image, Rect roi, int channel, Histogram & histogram)
  16. {
  17.     for (size_t x = roi.x; x < roi.x + roi.width; ++x) {
  18.         for (size_t y = roi.y; y < roi.y + roi.height; ++y) {
  19.             auto pixel = image.getPixel(x, y);
  20.             ++histogram[(*(reinterpret_cast<double*>(&pixel) + channel) * MAX_VALUE)];
  21.         }
  22.     }
  23. }
  24.  
  25. void calculateCFH(Histogram const & histogram, Histogram& cfh)
  26. {
  27.     size_t sum = 0;
  28.     for(size_t i = 0; i < histogram.size(); ++i) {
  29.         cfh[i] = sum += histogram[i];
  30.     }
  31. }
  32.  
  33. void equalizeChannel(Image & image, Rect roi, int channel, Histogram const & cfh)
  34. {
  35. //    std::cout << "roi" << std::endl;
  36. //    std::cout << roi.height << " " << roi.width << std::endl;
  37.     std::cout << "I'm here" << std::endl;
  38.     for (size_t x = roi.x; x < roi.x + roi.width; ++x) {
  39.         for (size_t y = roi.y; y < roi.y + roi.height; ++y) {
  40.             auto pixel = image.getPixel(x, y);
  41.             auto currentChannel = (reinterpret_cast<double*>(&pixel) + channel);
  42.             *currentChannel = cfh[*currentChannel * (MAX_VALUE - 1)] * static_cast<double>(MAX_VALUE) / image.area() / MAX_VALUE;
  43.             image.setPixel(x, y, pixel);
  44.         }
  45.     }
  46. }
  47.  
  48. void doHistogramEqualization(Image &image, Rect roi)
  49. {
  50.     // Image include ROI ?
  51.     assert(roi.x >= 0);
  52.     assert(roi.x + roi.width <= image.width());
  53.     assert(roi.y >= 0);
  54.     assert(roi.y + roi.height <= image.height());
  55.     int numberOfThreads = 5;
  56.     for (int ch = 0; ch < 3; ++ch) {
  57.         Histogram hist{}, cfh{};
  58.         {
  59.             ScopeTimer timer("calculateHistogram");
  60.             std::vector <std::future <void >> futures;
  61.             for(int numberOfThread = 0; numberOfThread < numberOfThreads; ++numberOfThread){
  62.                 int _x = roi.x + numberOfThread * roi.width / numberOfThreads;
  63.                 int _xr = (numberOfThread + 1 != numberOfThreads) ? (roi.x + (numberOfThread + 1) * roi.width / numberOfThreads) : (roi.x + roi.width);
  64.                 int _width = _xr - _x;
  65.                 Rect roi1{_x, roi.y, _width, roi.height};
  66.                 futures.push_back(std::async([&image, roi1, ch, &hist] {
  67.                     calculateHistogram(image, roi1, ch, hist);
  68.                 }));
  69.             }
  70. //            calculateHistogram(image, ch, hist);
  71.         }
  72.         {
  73.             ScopeTimer timer("calculateCFH");
  74.             calculateCFH(hist, cfh);
  75.         }
  76.         {
  77.             ScopeTimer timer("equalizeChannel");
  78.             std::vector <std::future <void >> futures;
  79.             for(int numberOfThread = 0; numberOfThread < numberOfThreads; ++numberOfThread){
  80.                 int _x = roi.x + numberOfThread * roi.width / numberOfThreads;
  81.                 int _xr = (numberOfThread + 1 != numberOfThreads) ? (roi.x + (numberOfThread + 1) * roi.width / numberOfThreads) : (roi.x + roi.width);
  82.                 int _width = _xr - _x;
  83.                 Rect roi1{_x, roi.y, _width, roi.height};
  84.                 futures.push_back(std::async([&image, roi1, ch, &cfh] {
  85.                     equalizeChannel(image, roi1, ch, cfh);
  86.                 }));
  87.             }
  88.         }
  89.     }
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top