aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJinweiClarkChao <[email protected]>2015-08-25 12:27:20 +0800
committerJinweiClarkChao <[email protected]>2015-08-25 16:49:49 +0800
commitf07885c1eb4c98dc52704517623b2e7df1ed616d (patch)
treeacc14792523ff5bbb464882e548b96752c2d6886
parentde74f5fa499b82390bce432251609d18d9ce5fd8 (diff)
downloadDehaze-f07885c1eb4c98dc52704517623b2e7df1ed616d.tar.gz
initial complete
-rw-r--r--DCP/DCP.vcxproj1
-rw-r--r--DCP/DCP.vcxproj.filters3
-rw-r--r--DCP/airlight.cpp134
-rw-r--r--DCP/darkchannel.cpp34
-rw-r--r--DCP/dcp.cpp15
-rw-r--r--DCP/dcp_core.h5
-rw-r--r--DCP/guidedfilter.cpp3
-rw-r--r--DCP/recover.cpp28
-rw-r--r--DCP/test.cpp14
-rw-r--r--DCP/transmission.cpp57
-rw-r--r--README.md2
11 files changed, 159 insertions, 137 deletions
diff --git a/DCP/DCP.vcxproj b/DCP/DCP.vcxproj
index eba571d..8d88b2c 100644
--- a/DCP/DCP.vcxproj
+++ b/DCP/DCP.vcxproj
@@ -88,6 +88,7 @@
88 <ClCompile Include="dcp.cpp" /> 88 <ClCompile Include="dcp.cpp" />
89 <ClCompile Include="guidedfilter.cpp" /> 89 <ClCompile Include="guidedfilter.cpp" />
90 <ClCompile Include="recover.cpp" /> 90 <ClCompile Include="recover.cpp" />
91 <ClCompile Include="test.cpp" />
91 <ClCompile Include="transmission.cpp" /> 92 <ClCompile Include="transmission.cpp" />
92 </ItemGroup> 93 </ItemGroup>
93 <ItemGroup> 94 <ItemGroup>
diff --git a/DCP/DCP.vcxproj.filters b/DCP/DCP.vcxproj.filters
index b301b89..0992c1b 100644
--- a/DCP/DCP.vcxproj.filters
+++ b/DCP/DCP.vcxproj.filters
@@ -33,6 +33,9 @@
33 <ClCompile Include="guidedfilter.cpp"> 33 <ClCompile Include="guidedfilter.cpp">
34 <Filter>Source Files</Filter> 34 <Filter>Source Files</Filter>
35 </ClCompile> 35 </ClCompile>
36 <ClCompile Include="test.cpp">
37 <Filter>Source Files</Filter>
38 </ClCompile>
36 </ItemGroup> 39 </ItemGroup>
37 <ItemGroup> 40 <ItemGroup>
38 <None Include="packages.config" /> 41 <None Include="packages.config" />
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
3void Estimate_A(IplImage *DarkChannel, IplImage *InputImage, struct Max_Pixel_Selector *MaxPixel, double A[]) 7using namespace std;
8
9struct 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); 15void 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
diff --git a/DCP/darkchannel.cpp b/DCP/darkchannel.cpp
index a6cd530..ab5a365 100644
--- a/DCP/darkchannel.cpp
+++ b/DCP/darkchannel.cpp
@@ -18,34 +18,38 @@ void CalcDarkChannel(IplImage *darkchannel, IplImage *input, int radius)
18 int st_row, ed_row; 18 int st_row, ed_row;
19 int st_col, ed_col; 19 int st_col, ed_col;
20 20
21 for (int i = 1; i <= height; i++) 21 for (int i = 0; i < height; i++)
22 { 22 {
23 for (int j = 1; j <= width; j++) 23 for (int j = 0; j < width; j++)
24 { 24 {
25 st_row = i - radius, ed_row = i + radius; 25 st_row = i - radius, ed_row = i + radius;
26 st_col = j - radius, ed_col = j + radius; 26 st_col = j - radius, ed_col = j + radius;
27 27
28 if (st_row <= 0) 28 if (st_row < 0)
29 st_row = 1; 29 st_row = 0;
30 if (ed_row > height) 30 if (ed_row >= height)
31 ed_row = height; 31 ed_row = height - 1;
32 if (st_col <= 0) 32 if (st_col < 0)
33 st_col = 1; 33 st_col = 0;
34 if (ed_col > width) 34 if (ed_col >= width)
35 ed_col = width; 35 ed_col = width - 1;
36
37 int cur = 0;
38 int min = 300;
36 39
37 int min = 0;
38 int tmp[3];
39 for (int m = st_row; m <= ed_row; m++) 40 for (int m = st_row; m <= ed_row; m++)
40 { 41 {
41 for (int n = st_col; n <= ed_col; n++) 42 for (int n = st_col; n <= ed_col; n++)
42 { 43 {
43 for (int k = 0; k < 3; k++) 44 for (int k = 0; k < 3; k++)
44 tmp[k] = *(uchar *)(input->imageData + (i - 1) * widthstep + (j - 1) * nch + k); 45 {
45 min = minbgr(tmp[0], tmp[1], tmp[2]); 46 cur = *(uchar *)(input->imageData + m * widthstep + n * nch + k);
47 if (cur < min)
48 min = cur;
49 }
46 } 50 }
47 } 51 }
48 *(uchar *)(darkchannel->imageData + (i - 1) * gwidthstep + (j - 1)) = min; 52 *(uchar *)(darkchannel->imageData + i * gwidthstep + j) = min;
49 } 53 }
50 } 54 }
51} 55}
diff --git a/DCP/dcp.cpp b/DCP/dcp.cpp
index df4aad9..4e100c6 100644
--- a/DCP/dcp.cpp
+++ b/DCP/dcp.cpp
@@ -1,5 +1,8 @@
1#include "dcp.h" 1#include "dcp.h"
2 2
3#include <iostream>
4using namespace std;
5
3void dehaze(IplImage *recover, IplImage *input) 6void dehaze(IplImage *recover, IplImage *input)
4{ 7{
5 int height = input->height; 8 int height = input->height;
@@ -11,8 +14,16 @@ void dehaze(IplImage *recover, IplImage *input)
11 14
12 int darkchannelradius = MIN(width, height) * 0.02; 15 int darkchannelradius = MIN(width, height) * 0.02;
13 16
14 CalcDarkChannel(darkchannel, input, darkchannelradius); 17
15
16 18
19 double Airlight[3] = { 0.0, 0.0, 0.0 };
20 CalcDarkChannel(darkchannel, input, darkchannelradius);
21
22 CalcAirlight(darkchannel, input, Airlight);
23
24 CalcTransmission(transmission, input, Airlight, darkchannelradius);
25 GuidedFilterColor(refine_transmission, input, transmission, 1e-6, 60);
26 CalcRecover(recover, input, refine_transmission, Airlight);
17 27
28
18} 29}
diff --git a/DCP/dcp_core.h b/DCP/dcp_core.h
index 3fb8c7c..85e4d59 100644
--- a/DCP/dcp_core.h
+++ b/DCP/dcp_core.h
@@ -5,5 +5,10 @@
5#include <opencv\highgui.h> 5#include <opencv\highgui.h>
6 6
7void CalcDarkChannel(IplImage *darkchannel, IplImage *input, int radius); 7void CalcDarkChannel(IplImage *darkchannel, IplImage *input, int radius);
8void CalcAirlight(IplImage *darkchannel, IplImage *input, double A[]);
9void CalcTransmission(IplImage *transmission, IplImage *input, double A[], int radius);
10void CalcRecover(IplImage *result, IplImage *input, IplImage *transmission, double A[]);
11void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r);
12
8 13
9#endif \ No newline at end of file 14#endif \ No newline at end of file
diff --git a/DCP/guidedfilter.cpp b/DCP/guidedfilter.cpp
index 46040ad..bfdf956 100644
--- a/DCP/guidedfilter.cpp
+++ b/DCP/guidedfilter.cpp
@@ -42,9 +42,6 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r
42 42
43 for (i = 1; i <= height; i++) 43 for (i = 1; i <= height; i++)
44 { 44 {
45 if (i % 10 == 0)
46 printf("%d\n", i);
47
48 for (j = 1; j <= width; j++) 45 for (j = 1; j <= width; j++)
49 { 46 {
50 st_row = i - r, ed_row = i + r; 47 st_row = i - r, ed_row = i + r;
diff --git a/DCP/recover.cpp b/DCP/recover.cpp
index a99b0f2..5dbd75e 100644
--- a/DCP/recover.cpp
+++ b/DCP/recover.cpp
@@ -1,7 +1,13 @@
1#include "dcp_core.h" 1#include "dcp_core.h"
2 2
3void Recover(IplImage *Result, IplImage *InputImage, double A[]) 3void CalcRecover(IplImage *result, IplImage *input, IplImage *transmission, double A[])
4{ 4{
5 int height = input->height;
6 int width = input->width;
7 int widthstep = input->widthStep;
8 int gwidthstep = transmission->widthStep;
9 int nch = input->nChannels;
10
5 double t0 = 0.1; 11 double t0 = 0.1;
6 double t, tmp_res; 12 double t, tmp_res;
7 13
@@ -9,27 +15,29 @@ void Recover(IplImage *Result, IplImage *InputImage, double A[])
9 int e = 0, number = 0; 15 int e = 0, number = 0;
10 double a = 0; 16 double a = 0;
11 17
12 for (i = 1; i <= height; i++) 18 A[0] /= 255.0;
19 A[1] /= 255.0;
20 A[2] /= 255.0;
21
22 for (i = 0; i < height; i++)
13 { 23 {
14 for (j = 1; j <= width; j++) 24 for (j = 0; j < width; j++)
15 { 25 {
16 t = real_trans[e++]; 26 t = *(uchar *)(transmission->imageData + i * gwidthstep + j);
17 t /= 255.0; 27 t /= 255.0;
18 for (k = 0; k < 3; k++) 28 for (k = 0; k < 3; k++)
19 { 29 {
20 a = *(uchar *)(InputImage->imageData + (i - 1) * widthstep + (j - 1) * nch + k); 30 a = *(uchar *)(input->imageData + (i) * widthstep + (j) * nch + k);
21 a /= 255; 31 a /= 255.0;
22
23 tmp_res = ((a - A[k]) / MaxTwo(t, t0)) + A[k];
24 32
25 real_recover[number++] = tmp_res; 33 tmp_res = ((a - A[k]) / MAX(t, t0)) + A[k];
26 34
27 if (tmp_res > 1) 35 if (tmp_res > 1)
28 tmp_res = 1; 36 tmp_res = 1;
29 else if (tmp_res < 0) 37 else if (tmp_res < 0)
30 tmp_res = 0; 38 tmp_res = 0;
31 39
32 *(uchar *)(Result->imageData + (i - 1) * widthstep + (j - 1) * nch + k) = tmp_res * 255.0; 40 *(uchar *)(result->imageData + (i) * widthstep + (j) * nch + k) = tmp_res * 255.0;
33 } 41 }
34 } 42 }
35 } 43 }
diff --git a/DCP/test.cpp b/DCP/test.cpp
new file mode 100644
index 0000000..7efeeb7
--- /dev/null
+++ b/DCP/test.cpp
@@ -0,0 +1,14 @@
1#include "dcp.h"
2
3int main()
4{
5 IplImage *input = cvLoadImage("input.png");
6 IplImage *result = cvCreateImage(cvGetSize(input), IPL_DEPTH_8U, 3);
7 dehaze(result, input);
8
9 cvNamedWindow("Result");
10 cvShowImage("Result", result);
11 cvWaitKey();
12
13 return 0;
14} \ No newline at end of file
diff --git a/DCP/transmission.cpp b/DCP/transmission.cpp
index 5bb2ece..48a8299 100644
--- a/DCP/transmission.cpp
+++ b/DCP/transmission.cpp
@@ -1,51 +1,46 @@
1#include "dcp_core.h" 1#include "dcp_core.h"
2 2
3void Estimate_transmission(IplImage *Transmission_maps, IplImage *InputImage, double A[], double heap[], double real_trans[]) 3void CalcTransmission(IplImage *transmission, IplImage *input, double A[], int radius)
4{ 4{
5 double w = 0.95; 5 int width = input->width;
6 double tmp; 6 int height = input->height;
7 double tran; 7 int widthstep = input->widthStep;
8 int i, j, k; 8 int gwidthstep = transmission->widthStep;
9 int e = 0; 9 int nch = input->nChannels;
10 10
11 IplImage *tmp_Trans = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3); 11 double w = 0.95;
12
13 IplImage *normalized_input = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
12 14
13 for (k = 0; k < 3; k++) 15 for (int k = 0; k < 3; k++)
14 { 16 {
15 for (i = 1; i <= height; i++) 17 for (int i = 0; i < height; i++)
16 { 18 {
17 for (j = 1; j <= width; j++) 19 for (int j = 0; j < width; j++)
18 { 20 {
19 tmp = *(uchar *)(InputImage->imageData + (i - 1) * widthstep + (j - 1) * nch + k); 21 double tmp = *(uchar *)(input->imageData + i * widthstep + j * nch + k);
20 tmp /= A[k]; 22 tmp = tmp / A[k] * 255.0;
21 23
22 if (tmp > 255) 24 tmp = tmp > 255 ? 255 : tmp;
23 tmp = 255; 25 *(uchar *)(normalized_input->imageData + i * widthstep + j * nch + k) = tmp;
24 *(uchar *)(tmp_Trans->imageData + (i - 1) * widthstep + (j - 1) * nch + k) = tmp;
25 } 26 }
26 } 27 }
27 } 28 }
28 Cal_DarkChannel(Transmission_maps, tmp_Trans, nInteger, heap); 29 CalcDarkChannel(transmission, normalized_input, radius);
29 30
30 for (i = 1; i <= height; i++) 31 for (int i = 0; i < height; i++)
31 { 32 {
32 for (j = 1; j <= width; j++) 33 for (int j = 0; j < width; j++)
33 { 34 {
34 tran = *(uchar *)(Transmission_maps->imageData + (i - 1) * gwidthstep + (j - 1)); 35 double tran = *(uchar *)(transmission->imageData + i * gwidthstep + j);
35
36 tran /= 255.0;
37 tran = 1 - w * tran;
38 36
39 real_trans[e++] = tran * 255.0; 37 tran = 1 - w * (tran / 255.0);
40 38
41 if (tran > 1) 39 tran = tran > 1 ? 1 : tran;
42 tran = 1; 40 tran = tran < 0 ? 0 : tran;
43 else if (tran < 0)
44 tran = 0;
45 41
46 *(uchar *)(Transmission_maps->imageData + (i - 1) * gwidthstep + (j - 1)) = tran * 255; 42 *(uchar *)(transmission->imageData + i * gwidthstep + j) = tran * 255;
47 } 43 }
48 } 44 }
49 //cvSaveImage("Estimated-Trans.bmp", Transmission_maps); 45 cvReleaseImage(&normalized_input);
50 cvReleaseImage(&tmp_Trans);
51} 46}
diff --git a/README.md b/README.md
index 4f6f576..5f1946b 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,6 @@
1Implementation of Dark Channel Prior 1Implementation of Dark Channel Prior
2 2
3This repo is still under development.
4
3He K, Sun J, Tang X. Single image haze removal using dark channel prior[J]. Pattern Analysis and 5He K, Sun J, Tang X. Single image haze removal using dark channel prior[J]. Pattern Analysis and
4Machine Intelligence, IEEE Transactions on, 2011, 33(12): 2341-2353. 6Machine Intelligence, IEEE Transactions on, 2011, 33(12): 2341-2353.
Powered by cgit v1.2.3 (git 2.41.0)