Advertisement
Bibodui

Сохранил говнокод

Nov 16th, 2023
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <opencv2/opencv.hpp>
  3.  
  4. using namespace std;
  5. using namespace cv;
  6.  
  7. void calculateHistogram(const Mat& image, vector<int>& histogram) {
  8.     // Инициализация гистограммы
  9.     histogram.assign(256, 0);
  10.  
  11.     // Подсчет значений гистограммы
  12.     for (int i = 0; i < image.rows; ++i) {
  13.         for (int j = 0; j < image.cols; ++j) {
  14.             int pixelValue = static_cast<int>(image.at<uchar>(i, j));
  15.             histogram[pixelValue]++;
  16.         }
  17.     }
  18. }
  19.  
  20. //void equalizeHistogram(Mat& image, const vector<int>& histogram) {
  21. //    // Вычисление кумулятивной функции распределения
  22. //    vector<int> cumulativeHistogram(256, 0);
  23. //    cumulativeHistogram[0] = histogram[0];
  24. //    for (int i = 1; i < 256; ++i) {
  25. //        cumulativeHistogram[i] = cumulativeHistogram[i - 1] + histogram[i];
  26. //    }
  27. //
  28. //    // Эквализация изображения
  29. //    for (int i = 0; i < image.rows; ++i) {
  30. //        for (int j = 0; j < image.cols; ++j) {
  31. //            int pixelValue = static_cast<int>(image.at<uchar>(i, j));
  32. //            int equalizedValue = static_cast<int>(255 * cumulativeHistogram[pixelValue] / (image.rows * image.cols));
  33. //            image.at<uchar>(i, j) = static_cast<uchar>(equalizedValue);
  34. //        }
  35. //    }
  36. //}
  37.  
  38. void hz(Mat image, vector<int> gist)
  39. {
  40.         int min = gist[0];
  41.         int max = gist[255];
  42.         for (int i = 0; i < 256; i++)
  43.             gist[i] -= min;
  44.  
  45.         for (int i = 0; i < 256; i++)
  46.             gist[i] /= max;
  47.  
  48.         for (int i = 0; i < 256; i++)
  49.             gist[i] *= 255;
  50. }
  51.  
  52. void equalizeHistogram(Mat& image, const vector<int>& histogram)
  53. {
  54.     // Вычисление кумулятивной функции распределения
  55.     int k = 256;
  56.     vector<int> hist(k, 0);
  57.        
  58.     for (int i = 0; i < image.rows; ++i)
  59.         for (int j = 0; j < image.cols; ++j)
  60.         {
  61.             int pixelValue = static_cast<int>(image.at<uchar>(i, j));
  62.             hist[pixelValue]++;
  63.         }
  64.  
  65.     for (int i = 1; i < k; ++i)
  66.         hist[i] = hist[i - 1] + hist[i];
  67.  
  68.     for (int i = 0; i < k; i++)
  69.     {
  70.         hist[i] *= 255;
  71.         hist[i] /= (image.rows * image.cols);
  72.     }
  73.  
  74.     for (int i = 0; i < image.rows; ++i)
  75.         for (int j = 0; j < image.cols; ++j)
  76.         {
  77.             int pixelValue = static_cast<int>(image.at<uchar>(i, j));
  78.             image.at<uchar>(i, j) = hist[pixelValue];
  79.         }
  80.  
  81.  
  82. }
  83.  
  84. int main() {
  85.     // Загрузка изображения
  86.     Mat image = imread("your_image.jpg", IMREAD_GRAYSCALE);
  87.  
  88.     if (image.empty()) {
  89.         cerr << "Could not open or find the image." << endl;
  90.         return -1;
  91.     }
  92.  
  93.     //// Вычисление и вывод исходной гистограммы
  94.     vector<int> histogram;
  95.     //calculateHistogram(image, histogram);
  96.     //cout << "Original Histogram:" << endl;
  97.     //for (int i = 0; i < 256; ++i) {
  98.     //    cout << i << ": " << histogram[i] << endl;
  99.     //}
  100.  
  101.     // Эквализация гистограммы
  102.     equalizeHistogram(image, histogram);
  103.  
  104.     // Вычисление и вывод эквализированной гистограммы
  105.     calculateHistogram(image, histogram);
  106.     cout << "\nEqualized Histogram:" << endl;
  107.     for (int i = 0; i < 256; ++i) {
  108.         cout << i << ": " << histogram[i] << endl;
  109.     }
  110.  
  111.     //hz(image, histogram);
  112.  
  113.     // Отображение изображения с эквализированной гистограммой
  114.     imshow("Equalized Image", image);
  115.     waitKey(0);
  116.  
  117.     return 0;
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement