1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <vector> using namespace cv; using namespace std;
vector<float> computeNormalizedHistogram(const Mat& image) { vector<float> histogram(256, 0); for (int r = 0; r < image.rows; r++) { for (int c = 0; c < image.cols; c++) { histogram[image.at<uchar>(r, c)]++; } } for (int i = 0; i < 256; i++) { histogram[i] /= (image.rows * image.cols); } return histogram; }
void showHistogram(const vector<float>& histogram, string name) { Mat histImage(256, 256, CV_8UC3, Scalar(0, 0, 0)); int histWidth = histImage.cols / 256; for (int i = 0; i < 256; i++) { int histHeight = static_cast<int>(histogram[i] * histImage.rows*10); rectangle(histImage, Point(i * histWidth, histImage.rows), Point((i + 1) * histWidth, histImage.rows - histHeight), Scalar(0, 0, 255), -1); }
namedWindow(name, WINDOW_AUTOSIZE); imshow(name, histImage); imwrite(name + ".jpg", histImage); }
vector<float> computecumulativeHistogram(const vector<float>& histogram) { vector<float> equalizedHistogram(256, 0); vector<float> cumulativeHistogram(256, 0); cumulativeHistogram[0] = histogram[0]; for (int i = 1; i < 256; i++) { cumulativeHistogram[i] = cumulativeHistogram[i - 1] + histogram[i]; } return cumulativeHistogram; }
Mat equalizeHistogram(const Mat& image) { vector<float> hist = computeNormalizedHistogram(image); vector<float> cumulativeHist = computecumulativeHistogram(hist); Mat equalizedImage = image.clone(); for (int y = 0; y < image.rows; y++) { for (int x = 0; x < image.cols; x++) { int intensity = image.at<uchar>(y, x); equalizedImage.at<uchar>(y, x) = cvRound(255 * cumulativeHist[intensity]); } } return equalizedImage; }
Mat equalizedColorHistogram(const Mat& image) { Mat bgrChannels[3]; Mat equalizedBGRChannels[3]; split(image, bgrChannels); for (int i = 0; i < 3; i++) { equalizedBGRChannels[i] = equalizeHistogram(bgrChannels[i]); } Mat equalizedColorImage; merge(equalizedBGRChannels, 3, equalizedColorImage); return equalizedColorImage; }
int main() { Mat myImage = imread("wallhaven.jpg"); imshow("MyImage", myImage); Mat grayImage; cvtColor(myImage, grayImage, COLOR_BGR2GRAY); imshow("GrayImage", grayImage); imwrite("GrayImage.jpg", grayImage);
vector<float> hist = computeNormalizedHistogram(grayImage); showHistogram(hist,"Hist");
Mat equalizedImage = equalizeHistogram(grayImage); imshow("EqualizedImage", equalizedImage); imwrite("EqualizedImage.jpg", equalizedImage); vector<float> equalizedHist = computeNormalizedHistogram(equalizedImage); showHistogram(equalizedHist, "equalizedHist");
Mat equalizedColorImage = equalizedColorHistogram(myImage); imshow("EqualizedColorImage", equalizedColorImage); imwrite("EqualizedColorImage.jpg", equalizedColorImage);
waitKey(0); return 0; }
|