diff options
author | JinweiClarkChao <[email protected]> | 2015-08-24 10:15:00 +0800 |
---|---|---|
committer | JinweiClarkChao <[email protected]> | 2015-08-24 10:15:00 +0800 |
commit | 17fbf4355747b1ceb9caca120aec9830a44621cf (patch) | |
tree | 6ec188fe59b0ae8c65a812765ef8f621ecf87ec3 /DCP/airlight.cpp | |
download | Dehaze-17fbf4355747b1ceb9caca120aec9830a44621cf.tar.gz |
init
Diffstat (limited to 'DCP/airlight.cpp')
-rw-r--r-- | DCP/airlight.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/DCP/airlight.cpp b/DCP/airlight.cpp new file mode 100644 index 0000000..c6df18f --- /dev/null +++ b/DCP/airlight.cpp | |||
@@ -0,0 +1,97 @@ | |||
1 | #include "dcp_core.h" | ||
2 | |||
3 | void Estimate_A(IplImage *DarkChannel, IplImage *InputImage, struct Max_Pixel_Selector *MaxPixel, double A[]) | ||
4 | { | ||
5 | double num = height * width * 0.001; | ||
6 | double num1per = num * 0.01; //num1per is the number of 1 percent of top 0.1 pixels | ||
7 | //k is the index of the color channel | ||
8 | int k = 0; | ||
9 | int patchsize; | ||
10 | int dark_i, dark_j; | ||
11 | uchar Max = 0; | ||
12 | |||
13 | IplImage *mask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1); | ||
14 | cvZero(mask); | ||
15 | |||
16 | int e = 0, i, j; | ||
17 | for (i = 1; i <= height; i++) | ||
18 | { | ||
19 | for (j = 1; j <= width; j++) | ||
20 | { | ||
21 | MaxPixel[e].Pixel = *(uchar *)(DarkChannel->imageData + (i - 1) * gwidthstep + (j - 1)); | ||
22 | MaxPixel[e].i = i; | ||
23 | MaxPixel[e].j = j; | ||
24 | e++; | ||
25 | } | ||
26 | } | ||
27 | sort(MaxPixel, MaxPixel + e, comp1); | ||
28 | |||
29 | for (i = 0; i < num; i++) | ||
30 | { | ||
31 | dark_i = MaxPixel[i].i; | ||
32 | dark_j = MaxPixel[i].j; | ||
33 | *(uchar *)(mask->imageData + (dark_i - 1) * gwidthstep + (dark_j - 1)) = 255; | ||
34 | //ֻ�е�mask���Ϊ255֮��˵���ÿ���������ֵ���ڰ�ͨ����ǰ0.1%֮�ڵ� | ||
35 | } | ||
36 | |||
37 | if (num1per < 9) | ||
38 | patchsize = 3; | ||
39 | else | ||
40 | patchsize = sqrt(num1per); | ||
41 | |||
42 | for (k = 0; k < 3; k++) | ||
43 | { | ||
44 | int e, x, y; | ||
45 | int tmp = (patchsize - 1) / 2; | ||
46 | int st_row, ed_row; | ||
47 | int st_col, ed_col; | ||
48 | |||
49 | double A_tmp = 0; | ||
50 | double aver = 0; // aver Ϊָ����Сpatch��InputImageֵ��ƽ��ֵ | ||
51 | int flag; //flag������ȡmask�ı�� | ||
52 | |||
53 | for (i = 1; i <= num; i++) | ||
54 | { | ||
55 | aver = 0; | ||
56 | dark_i = MaxPixel[i].i; | ||
57 | dark_j = MaxPixel[i].j; | ||
58 | |||
59 | st_row = dark_i - tmp, ed_row = dark_i + tmp; | ||
60 | st_col = dark_j - tmp, ed_col = dark_j + tmp; | ||
61 | |||
62 | if (st_row <= 0) | ||
63 | st_row = 1; | ||
64 | if (ed_row>height) | ||
65 | ed_row = height; | ||
66 | if (st_col <= 0) | ||
67 | st_col = 1; | ||
68 | if (ed_col>width) | ||
69 | ed_col = width; | ||
70 | |||
71 | e = 0; | ||
72 | for (x = st_row; x <= ed_row; x++) | ||
73 | { | ||
74 | for (y = st_col; y <= ed_col; y++) | ||
75 | { | ||
76 | flag = *(uchar *)(mask->imageData + (x - 1) * gwidthstep + (y - 1)); | ||
77 | if (flag == 0) //δ�����˵���õ㲻��ǰ0.1%��Ӧ�������ڣ����� | ||
78 | { | ||
79 | continue; | ||
80 | } | ||
81 | else | ||
82 | { | ||
83 | aver += *(uchar *)(InputImage->imageData + (x - 1) * widthstep + (y - 1) *nch + k); | ||
84 | e++; | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | aver /= e; | ||
89 | if (A_tmp < aver) | ||
90 | { | ||
91 | A_tmp = aver; | ||
92 | } | ||
93 | } | ||
94 | A[k] = A_tmp / 255.0; | ||
95 | } | ||
96 | cvReleaseImage(&mask); | ||
97 | } \ No newline at end of file | ||