Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int* computeHistogramGrayscale(Mat img)
- {
- int *hist= new int[256]();
- int rows = img.rows, cols = img.cols;
- for (int r = 0; r < rows; r++)
- {
- for (int c = 0; c < cols; c++)
- {
- hist[img.at<uchar>(r, c)]++;
- }
- }
- return hist;
- }
- int* computeHistogramGrayscaleCumulative(int *hist)
- {
- int *histC = new int[256]();
- int current = 0;
- for (int i = 0; i < 256; i++)
- {
- current += hist[i];
- histC[i]= current;
- }
- return histC;
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- float* computeDeviation(int *histC,Mat src)
- {
- float *histf = new float[256]();
- int current = 0;
- float pixels = src.rows*src.cols;
- for (int i = 0; i < 256; i++)
- {
- histf[i] = ((float)255 * histC[i])/ pixels;
- }
- return histf;
- }
- Mat proc_img_eq(Mat &img)
- {
- Mat dst = img.clone();
- int *hist = computeHistogramGrayscale(img);
- int *histC = computeHistogramGrayscaleCumulative(hist);
- float *t = computeDeviation(histC, img);
- int rows = img.rows, cols = img.cols;
- for (int r = 0; r < rows; r++)
- for (int c = 0; c < cols; c++)
- dst.at<uchar>(r, c) = t[img.at<uchar>(r, c)];
- return dst;
- }
- void lab7_histogram_all() {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- double t = (double)getTickCount(); // Get the current time [s]
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int *hist = computeHistogramGrayscale(src);
- int *histC = computeHistogramGrayscaleCumulative(hist);
- showHistogram("Histogram", hist, 256, 500);
- showHistogram("HistogramCummulative", histC, 256, 500);
- Mat dst = proc_img_eq(src);
- int *histd = computeHistogramGrayscale(dst);
- int *histCd = computeHistogramGrayscaleCumulative(histd);
- showHistogram("Histogram dst", histd, 256, 500);
- showHistogram("HistogramCummulative dst", histCd, 256, 500);
- imshow("src", src);
- imshow("dst", dst);
- // Get the current time again and compute the time difference [s]
- t = ((double)getTickCount() - t) / getTickFrequency();
- // Print (in the console window) the processing time in [ms]
- printf("Time = %.3f [ms]\n", t * 1000);
- waitKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement