Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ImageProcessing.h"
- #include "Timer.h"
- #include <array>
- #include <cassert>
- #include <iostream>
- #include <future>
- enum RGB {R = 0, G = 1, B = 2};
- constexpr auto MAX_VALUE = std::numeric_limits<uint16_t>::max();
- using Histogram = std::array<size_t, MAX_VALUE>;
- /*
- We need to pass an image through reference
- */
- void calculateHistogram(Image const & image, Rect roi, int channel, Histogram & histogram)
- {
- for (size_t x = roi.x; x < roi.x + roi.width; ++x) {
- for (size_t y = roi.y; y < roi.y + roi.height; ++y) {
- auto pixel = image.getPixel(x, y);
- ++histogram[(*(reinterpret_cast<double*>(&pixel) + channel) * MAX_VALUE)];
- }
- }
- }
- void calculateCFH(Histogram const & histogram, Histogram& cfh)
- {
- size_t sum = 0;
- for(size_t i = 0; i < histogram.size(); ++i) {
- cfh[i] = sum += histogram[i];
- }
- }
- void equalizeChannel(Image & image, Rect roi, int channel, Histogram const & cfh)
- {
- // std::cout << "roi" << std::endl;
- // std::cout << roi.height << " " << roi.width << std::endl;
- std::cout << "I'm here" << std::endl;
- for (size_t x = roi.x; x < roi.x + roi.width; ++x) {
- for (size_t y = roi.y; y < roi.y + roi.height; ++y) {
- auto pixel = image.getPixel(x, y);
- auto currentChannel = (reinterpret_cast<double*>(&pixel) + channel);
- *currentChannel = cfh[*currentChannel * (MAX_VALUE - 1)] * static_cast<double>(MAX_VALUE) / image.area() / MAX_VALUE;
- image.setPixel(x, y, pixel);
- }
- }
- }
- void doHistogramEqualization(Image &image, Rect roi)
- {
- // Image include ROI ?
- assert(roi.x >= 0);
- assert(roi.x + roi.width <= image.width());
- assert(roi.y >= 0);
- assert(roi.y + roi.height <= image.height());
- int numberOfThreads = 5;
- for (int ch = 0; ch < 3; ++ch) {
- Histogram hist{}, cfh{};
- {
- ScopeTimer timer("calculateHistogram");
- std::vector <std::future <void >> futures;
- for(int numberOfThread = 0; numberOfThread < numberOfThreads; ++numberOfThread){
- int _x = roi.x + numberOfThread * roi.width / numberOfThreads;
- int _xr = (numberOfThread + 1 != numberOfThreads) ? (roi.x + (numberOfThread + 1) * roi.width / numberOfThreads) : (roi.x + roi.width);
- int _width = _xr - _x;
- Rect roi1{_x, roi.y, _width, roi.height};
- futures.push_back(std::async([&image, roi1, ch, &hist] {
- calculateHistogram(image, roi1, ch, hist);
- }));
- }
- // calculateHistogram(image, ch, hist);
- }
- {
- ScopeTimer timer("calculateCFH");
- calculateCFH(hist, cfh);
- }
- {
- ScopeTimer timer("equalizeChannel");
- std::vector <std::future <void >> futures;
- for(int numberOfThread = 0; numberOfThread < numberOfThreads; ++numberOfThread){
- int _x = roi.x + numberOfThread * roi.width / numberOfThreads;
- int _xr = (numberOfThread + 1 != numberOfThreads) ? (roi.x + (numberOfThread + 1) * roi.width / numberOfThreads) : (roi.x + roi.width);
- int _width = _xr - _x;
- Rect roi1{_x, roi.y, _width, roi.height};
- futures.push_back(std::async([&image, roi1, ch, &cfh] {
- equalizeChannel(image, roi1, ch, cfh);
- }));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement