forked from BBuf/Image-processing-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnevenLightCompensate.cpp
71 lines (67 loc) · 2.49 KB
/
UnevenLightCompensate.cpp
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
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <immintrin.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat speed_rgb2gray(Mat src) {
Mat dst(src.rows, src.cols, CV_8UC1);
#pragma omp parallel for num_threads(12)
for (int i = 0; i < src.rows; i++) {
for (int j = 0; j < src.cols; j++) {
dst.at<uchar>(i, j) = ((src.at<Vec3b>(i, j)[0] << 18) + (src.at<Vec3b>(i, j)[0] << 15) + (src.at<Vec3b>(i, j)[0] << 14) +
(src.at<Vec3b>(i, j)[0] << 11) + (src.at<Vec3b>(i, j)[0] << 7) + (src.at<Vec3b>(i, j)[0] << 7) + (src.at<Vec3b>(i, j)[0] << 5) +
(src.at<Vec3b>(i, j)[0] << 4) + (src.at<Vec3b>(i, j)[0] << 2) +
(src.at<Vec3b>(i, j)[1] << 19) + (src.at<Vec3b>(i, j)[1] << 16) + (src.at<Vec3b>(i, j)[1] << 14) + (src.at<Vec3b>(i, j)[1] << 13) +
(src.at<Vec3b>(i, j)[1] << 10) + (src.at<Vec3b>(i, j)[1] << 8) + (src.at<Vec3b>(i, j)[1] << 4) + (src.at<Vec3b>(i, j)[1] << 3) + (src.at<Vec3b>(i, j)[1] << 1) +
(src.at<Vec3b>(i, j)[2] << 16) + (src.at<Vec3b>(i, j)[2] << 15) + (src.at<Vec3b>(i, j)[2] << 14) + (src.at<Vec3b>(i, j)[2] << 12) +
(src.at<Vec3b>(i, j)[2] << 9) + (src.at<Vec3b>(i, j)[2] << 7) + (src.at<Vec3b>(i, j)[2] << 6) + (src.at<Vec3b>(i, j)[2] << 5) + (src.at<Vec3b>(i, j)[2] << 4) + (src.at<Vec3b>(i, j)[2] << 1) >> 20);
}
}
return dst;
}
Mat unevenLightCompensate(Mat src, int block_Size) {
int row = src.rows;
int col = src.cols;
Mat gray(row, col, CV_8UC1);
if (src.channels() == 3) {
gray = speed_rgb2gray(src);
}
else {
gray = src;
}
float average = mean(gray)[0];
int new_row = ceil(1.0 * row / block_Size);
int new_col = ceil(1.0 * col / block_Size);
Mat new_img(new_row, new_col, CV_32FC1);
for (int i = 0; i < new_row; i++) {
for (int j = 0; j < new_col; j++) {
int rowx = i * block_Size;
int rowy = (i + 1) * block_Size;
int colx = j * block_Size;
int coly = (j + 1) * block_Size;
if (rowy > row) rowy = row;
if (coly > col) coly = col;
Mat ROI = src(Range(rowx, rowy), Range(colx, coly));
float block_average = mean(ROI)[0];
new_img.at<float>(i, j) = block_average;
}
}
new_img = new_img - average;
Mat new_img2;
resize(new_img, new_img2, Size(col, row), (0, 0), (0, 0), INTER_CUBIC);
Mat new_src;
gray.convertTo(new_src, CV_32FC1);
Mat dst = new_src - new_img2;
dst.convertTo(dst, CV_8UC1);
return dst;
}
int main() {
Mat src = cv::imread("F:\\car.jpg");
Mat dst = unevenLightCompensate(src, 64);
cv::imshow("result", dst);
cv::imwrite("F:\\res.jpg", dst);
cv::waitKey(0);
return 0;
}