Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Mat uneq = imread("images/uneq.jpg", CV_LOAD_IMAGE_COLOR);
- if (uneq.empty()) printf("Unable to read input file (%s, %d).", __FILE__, __LINE__);
- cvtColor(uneq, uneq, CV_BGR2GRAY);
- const int L = 256;
- int p[L];
- for (int l = 0; l < L; l++)
- p[l] = 0;
- for (int j = 0; j < uneq.rows; j++)
- for (int i = 0; i < uneq.cols; i++)
- for (int l = 0; l < L; l++)
- if (uneq.at<uchar>(j, i) == l)
- p[l]++;
- int cdf[L];
- for (int l = 0; l < L; l++)
- cdf[l] = 0;
- int ipom,index;
- int max=0;
- ipom = p[0];
- cdf[0] = p[0];
- for (int l = 1; l < L; l++)
- {
- if (ipom == 0)
- {
- ipom = p[l];
- index = l;
- }
- if (p[l] > max)
- max = p[l];
- cdf[l] = cdf[l - 1] + p[l];
- }
- /*
- int soucet = 0;
- for (int l = 0; l < L; l++)
- {
- soucet += p[l];
- printf("%d: %d\n", l, p[l]);
- }
- printf("soucet: %d\n", soucet);
- for (int l = 0; l < L; l++)
- {
- printf("%d: %d\n", l, cdf[l]);
- }*/
- Mat eq(uneq.rows, uneq.cols, CV_8UC1);
- double pom;
- for (int j = 0; j < eq.rows; j++)
- for (int i = 0; i < eq.cols; i++)
- eq.at<uchar>(j, i) = round(((float)(cdf[uneq.at<uchar>(j, i)] - cdf[index]) / (float)(eq.rows*eq.cols - cdf[index])) * (L - 1));
- int pp[L];
- for (int l = 0; l < L; l++)
- pp[l] = 0;
- for (int j = 0; j < eq.rows; j++)
- for (int i = 0; i < eq.cols; i++)
- for (int l = 0; l < L; l++)
- if (eq.at<uchar>(j, i) == l)
- pp[l]++;
- int cdff[L];
- for (int l = 0; l < L; l++)
- cdff[l] = 0;
- for (int l = 1; l < L; l++)
- cdff[l] = cdff[l - 1] + pp[l];
- Mat hist(max, L, CV_8UC1);
- Mat hist2(max, L, CV_8UC1);
- for (int j = 0; j < hist.rows; j++)
- for (int i = 0; i < hist.cols; i++)
- hist.at<uchar>(j, i) = 0;
- for (int i = 0; i < hist.cols; i++)
- for (int j = hist.rows - p[i]; j < hist.rows; j++)
- hist.at<uchar>(j, i) = 255;
- for (int j = 0; j < hist2.rows; j++)
- for (int i = 0; i < hist2.cols; i++)
- hist2.at<uchar>(j, i) = 0;
- for (int i = 0; i < hist2.cols; i++)
- for (int j = hist2.rows - pp[i]; j < hist2.rows; j++)
- hist2.at<uchar>(j, i) = 255;
- Mat chist(uneq.rows*uneq.cols, L, CV_8UC1);
- Mat chist2(uneq.rows*uneq.cols, L, CV_8UC1);
- for (int j = 0; j < chist.rows; j++)
- for (int i = 0; i < chist.cols; i++)
- chist.at<uchar>(j, i) = 0;
- for (int i = 0; i < chist.cols; i++)
- for (int j = chist.rows - cdf[i]; j < chist.rows; j++)
- chist.at<uchar>(j, i) = 255;
- for (int j = 0; j < chist2.rows; j++)
- for (int i = 0; i < chist2.cols; i++)
- chist2.at<uchar>(j, i) = 0;
- for (int i = 0; i < chist2.cols; i++)
- for (int j = chist2.rows - cdff[i]; j < chist2.rows; j++)
- chist2.at<uchar>(j, i) = 255;
- namedWindow("Uneq", 0);
- namedWindow("Eq", 0);
- imshow("Uneq", uneq);
- imshow("Eq", eq);
- namedWindow("Input Histogram", 0);
- imshow("Input Histogram", hist);
- namedWindow("Output Histogram 2", 0);
- imshow("Output Histogram 2", hist2);
- namedWindow("Input Cumulative Histogram", 0);
- imshow("Input Cumulative Histogram", chist);
- namedWindow("Output Cumulative Histogram 2", 0);
- imshow("Output Cumulative Histogram 2", chist2);
- waitKey(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement