diff options
Diffstat (limited to 'DCP/airlight.cpp')
-rw-r--r-- | DCP/airlight.cpp | 134 |
1 files changed, 58 insertions, 76 deletions
diff --git a/DCP/airlight.cpp b/DCP/airlight.cpp index c6df18f..08f18d0 100644 --- a/DCP/airlight.cpp +++ b/DCP/airlight.cpp | |||
@@ -1,97 +1,79 @@ | |||
1 | #include "dcp_core.h" | 1 | #include "dcp_core.h" |
2 | #include <malloc.h> | ||
3 | #include <vector> | ||
4 | #include <iostream> | ||
5 | #include <algorithm> | ||
2 | 6 | ||
3 | void Estimate_A(IplImage *DarkChannel, IplImage *InputImage, struct Max_Pixel_Selector *MaxPixel, double A[]) | 7 | using namespace std; |
8 | |||
9 | struct Pixel | ||
4 | { | 10 | { |
5 | double num = height * width * 0.001; | 11 | int value; |
6 | double num1per = num * 0.01; //num1per is the number of 1 percent of top 0.1 pixels | 12 | int i, j; |
7 | //k is the index of the color channel | 13 | }; |
8 | int k = 0; | ||
9 | int patchsize; | ||
10 | int dark_i, dark_j; | ||
11 | uchar Max = 0; | ||
12 | 14 | ||
13 | IplImage *mask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1); | 15 | void CalcAirlight(IplImage *darkchannel, IplImage *input, double A[]) |
14 | cvZero(mask); | 16 | { |
17 | int height = input->height; | ||
18 | int width = input->width; | ||
19 | int widthstep = input->widthStep; | ||
20 | int gwidthstep = darkchannel->widthStep; | ||
21 | int nch = input->nChannels; | ||
15 | 22 | ||
16 | int e = 0, i, j; | 23 | struct Pixel *v_darkchannel = (struct Pixel *)malloc(sizeof(struct Pixel) * height * width); |
17 | for (i = 1; i <= height; i++) | 24 | int count = 0; |
25 | for (int i = 0; i < height; i++) | ||
18 | { | 26 | { |
19 | for (j = 1; j <= width; j++) | 27 | for (int j = 0; j < width; j++) |
20 | { | 28 | { |
21 | MaxPixel[e].Pixel = *(uchar *)(DarkChannel->imageData + (i - 1) * gwidthstep + (j - 1)); | 29 | int value = *(uchar *)(darkchannel->imageData + i * gwidthstep + j); |
22 | MaxPixel[e].i = i; | 30 | struct Pixel p = { value, i, j }; |
23 | MaxPixel[e].j = j; | 31 | v_darkchannel[count++] = p; |
24 | e++; | ||
25 | } | 32 | } |
26 | } | 33 | } |
27 | sort(MaxPixel, MaxPixel + e, comp1); | 34 | sort(v_darkchannel, v_darkchannel + count, [](struct Pixel &a, struct Pixel &b){ return a.value > b.value; }); |
35 | |||
36 | |||
37 | IplImage *mask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1); | ||
38 | cvZero(mask); | ||
28 | 39 | ||
29 | for (i = 0; i < num; i++) | 40 | for (int i = 0; i < count * 0.001; i++) |
30 | { | 41 | { |
31 | dark_i = MaxPixel[i].i; | 42 | struct Pixel p = v_darkchannel[i]; |
32 | dark_j = MaxPixel[i].j; | 43 | *(uchar *)(mask->imageData + p.i * gwidthstep + p.j) = 255; |
33 | *(uchar *)(mask->imageData + (dark_i - 1) * gwidthstep + (dark_j - 1)) = 255; | ||
34 | //ֻ�е�mask���Ϊ255֮��˵���ÿ���������ֵ���ڰ�ͨ����ǰ0.1%֮�ڵ� | ||
35 | } | 44 | } |
45 | |||
36 | 46 | ||
37 | if (num1per < 9) | 47 | for (int k = 0; k < 3; k++) |
38 | patchsize = 3; | ||
39 | else | ||
40 | patchsize = sqrt(num1per); | ||
41 | |||
42 | for (k = 0; k < 3; k++) | ||
43 | { | 48 | { |
44 | int e, x, y; | 49 | struct Pixel *v_channel = (struct Pixel *)malloc(sizeof(struct Pixel) * height * width); |
45 | int tmp = (patchsize - 1) / 2; | 50 | int count = 0; |
46 | int st_row, ed_row; | ||
47 | int st_col, ed_col; | ||
48 | 51 | ||
49 | double A_tmp = 0; | 52 | for (int i = 0; i < height; i++) |
50 | double aver = 0; // aver Ϊָ����Сpatch��InputImageֵ��ƽ��ֵ | ||
51 | int flag; //flag������ȡmask�ı�� | ||
52 | |||
53 | for (i = 1; i <= num; i++) | ||
54 | { | 53 | { |
55 | aver = 0; | 54 | for (int j = 0; j < width; j++) |
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 | { | 55 | { |
74 | for (y = st_col; y <= ed_col; y++) | 56 | int flag = *(uchar *)(mask->imageData + i * gwidthstep + j); |
75 | { | 57 | if (flag == 0) |
76 | flag = *(uchar *)(mask->imageData + (x - 1) * gwidthstep + (y - 1)); | 58 | continue; |
77 | if (flag == 0) //δ�����˵���õ㲻��ǰ0.1%��Ӧ�������ڣ����� | 59 | |
78 | { | 60 | int value = *(uchar *)(input->imageData + i * widthstep + j * nch + k); |
79 | continue; | 61 | struct Pixel p = { value, i, j }; |
80 | } | 62 | v_channel[count++] = p; |
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 | } | 63 | } |
93 | } | 64 | } |
94 | A[k] = A_tmp / 255.0; | 65 | |
66 | sort(v_channel, v_channel + count, [](struct Pixel &a, struct Pixel &b){ return a.value > b.value; }); | ||
67 | |||
68 | int channel_airlight = 0; | ||
69 | for (int i = 0; i < count * 0.01; i++) | ||
70 | { | ||
71 | channel_airlight += v_channel[i].value; | ||
72 | } | ||
73 | channel_airlight /= (count * 0.01); | ||
74 | A[k] = channel_airlight; | ||
75 | |||
76 | free(v_channel); | ||
95 | } | 77 | } |
96 | cvReleaseImage(&mask); | 78 | free(v_darkchannel); |
97 | } \ No newline at end of file | 79 | } \ No newline at end of file |