Skip to content

Commit da7fff9

Browse files
committed
Update HistograEqualization.cpp
1 parent 080a031 commit da7fff9

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "opencv2/opencv.hpp"
2+
#include "iostream"
3+
#include "algorithm"
4+
#include "vector"
5+
#include "stdio.h"
6+
using namespace std;
7+
using namespace cv;
8+
9+
//直方图均衡化
10+
Mat Histogramequalization(Mat src) {
11+
int R[256] = {0};
12+
int G[256] = {0};
13+
int B[256] = {0};
14+
int rows = src.rows;
15+
int cols = src.cols;
16+
int sum = rows * cols;
17+
//统计直方图的RGB分布
18+
for (int i = 0; i < rows; i++) {
19+
for (int j = 0; j < cols; j++) {
20+
B[src.at<Vec3b>(i, j)[0]]++;
21+
G[src.at<Vec3b>(i, j)[1]]++;
22+
R[src.at<Vec3b>(i, j)[2]]++;
23+
}
24+
}
25+
//构建直方图的累计分布方程,用于直方图均衡化
26+
double val[3] = {0};
27+
for (int i = 0; i < 256; i++) {
28+
val[0] += B[i];
29+
val[1] += G[i];
30+
val[2] += R[i];
31+
B[i] = val[0] * 255 / sum;
32+
G[i] = val[1] * 255 / sum;
33+
R[i] = val[2] * 255 / sum;
34+
}
35+
//归一化直方图
36+
Mat dst(rows, cols, CV_8UC3);
37+
for(int i = 0; i < rows; i++){
38+
for(int j = 0; j < cols; j++){
39+
dst.at<Vec3b>(i, j)[0] = B[src.at<Vec3b>(i, j)[0]];
40+
dst.at<Vec3b>(i, j)[1] = B[src.at<Vec3b>(i, j)[1]];
41+
dst.at<Vec3b>(i, j)[2] = B[src.at<Vec3b>(i, j)[2]];
42+
}
43+
}
44+
return dst;
45+
}
46+
47+
int main(){
48+
Mat src = imread("../1.jpg");
49+
Mat dst = Histogramequalization(src);
50+
imshow("origin", src);
51+
imshow("result", dst);
52+
imwrite("../result.jpg", dst);
53+
waitKey(0);
54+
return 0;
55+
}

Correction algorithm/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# OpenCV 和 C++ 实现一些矫正算法
22

3-
- GammaCorrection.cpp Gamma矫正C++实现,原理附在了代码中
3+
- GammaCorrection.cpp Gamma 矫正C++实现。原理附在了代码中
4+
5+
- HistograEqualization.cpp 直方图均衡化C++实现。原理请看:https://blog.csdn.net/just_sort/article/details/85013803
46

0 commit comments

Comments
 (0)