Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. int* computeHistogramGrayscale(Mat img)
  2. {
  3.     int *hist= new int[256]();
  4.     int rows = img.rows, cols = img.cols;
  5.     for (int r = 0; r < rows; r++)
  6.     {
  7.         for (int c = 0; c < cols; c++)
  8.         {
  9.             hist[img.at<uchar>(r, c)]++;
  10.         }
  11.     }
  12.     return hist;
  13. }
  14. int* computeHistogramGrayscaleCumulative(int *hist)
  15. {
  16.     int *histC = new int[256]();
  17.     int current = 0;
  18.     for (int i = 0; i < 256; i++)
  19.     {
  20.         current += hist[i];
  21.         histC[i]= current; 
  22.     }
  23.     return histC;
  24. }
  25. //////////////////////////////////////////////////////////////////////////////////////////
  26. float* computeDeviation(int *histC,Mat src)
  27. {
  28.     float *histf = new float[256]();
  29.     int current = 0;
  30.     float pixels = src.rows*src.cols;
  31.     for (int i = 0; i < 256; i++)
  32.     {
  33.         histf[i] = ((float)255 * histC[i])/ pixels;
  34.     }
  35.     return histf;
  36. }
  37. Mat proc_img_eq(Mat &img)
  38. {
  39.     Mat dst = img.clone();
  40.     int *hist = computeHistogramGrayscale(img);
  41.     int *histC = computeHistogramGrayscaleCumulative(hist);
  42.     float *t = computeDeviation(histC, img);
  43.     int rows = img.rows, cols = img.cols;
  44.     for (int r = 0; r < rows; r++)
  45.         for (int c = 0; c < cols; c++)
  46.             dst.at<uchar>(r, c) = t[img.at<uchar>(r, c)];
  47.     return dst;
  48. }
  49. void lab7_histogram_all() {
  50.     char fname[MAX_PATH];
  51.     while (openFileDlg(fname))
  52.     {
  53.         double t = (double)getTickCount(); // Get the current time [s]
  54.         Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  55.         int *hist = computeHistogramGrayscale(src);
  56.         int *histC = computeHistogramGrayscaleCumulative(hist);
  57.         showHistogram("Histogram", hist, 256, 500);
  58.         showHistogram("HistogramCummulative", histC, 256, 500);
  59.         Mat dst = proc_img_eq(src);
  60.         int *histd = computeHistogramGrayscale(dst);
  61.         int *histCd = computeHistogramGrayscaleCumulative(histd);
  62.         showHistogram("Histogram dst", histd, 256, 500);
  63.         showHistogram("HistogramCummulative dst", histCd, 256, 500);
  64.         imshow("src", src);
  65.         imshow("dst", dst);
  66.         // Get the current time again and compute the time difference [s]
  67.         t = ((double)getTickCount() - t) / getTickFrequency();
  68.  
  69.  
  70.  
  71.         // Print (in the console window) the processing time in [ms]
  72.         printf("Time = %.3f [ms]\n", t * 1000);
  73.         waitKey();
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement