From 66efed8b335610f9eae59fd3f52eccaffd7a6e0b Mon Sep 17 00:00:00 2001 From: JinweiClarkChao Date: Tue, 25 Aug 2015 16:03:05 +0800 Subject: fix --- DCP/DCP.vcxproj | 85 +++++++++++++++++++++++++++++++------- DCP/DCP.vcxproj.filters | 6 +-- DCP/airlight.cpp | 12 ++---- DCP/dcp.cpp | 39 ++++++++++++------ DCP/dcp_core.h | 8 +++- DCP/guidedfilter.cpp | 106 +++++++++++++++++++++--------------------------- DCP/packages.config | 4 -- DCP/recover.cpp | 11 ++--- DCP/test.cpp | 11 +++-- DCP/transmission.cpp | 9 ++-- DCP/utility.h | 29 +++++++++++++ 11 files changed, 199 insertions(+), 121 deletions(-) delete mode 100644 DCP/packages.config create mode 100644 DCP/utility.h (limited to 'DCP') diff --git a/DCP/DCP.vcxproj b/DCP/DCP.vcxproj index 8d88b2c..b2ef313 100644 --- a/DCP/DCP.vcxproj +++ b/DCP/DCP.vcxproj @@ -1,15 +1,22 @@  - Debug Win32 + + Debug + x64 + Release Win32 + + Release + x64 + {1543533F-481B-40EC-BEFC-90E46456A5F8} @@ -23,6 +30,12 @@ v120 Unicode + + Application + true + v120 + Unicode + Application false @@ -30,24 +43,47 @@ true Unicode + + Application + false + v120 + true + Unicode + + + + + + + + + + + - f521f52d + ed7df352 true + + true + false + + false + @@ -61,6 +97,19 @@ true + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + + + Console + true + + Level3 @@ -78,9 +127,27 @@ true + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + + + Console + true + true + true + + + @@ -91,18 +158,6 @@ - - - - - - - - - This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - + \ No newline at end of file diff --git a/DCP/DCP.vcxproj.filters b/DCP/DCP.vcxproj.filters index 0992c1b..23bfa2a 100644 --- a/DCP/DCP.vcxproj.filters +++ b/DCP/DCP.vcxproj.filters @@ -37,9 +37,6 @@ Source Files - - - Header Files @@ -47,5 +44,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/DCP/airlight.cpp b/DCP/airlight.cpp index 08f18d0..78ec9a9 100644 --- a/DCP/airlight.cpp +++ b/DCP/airlight.cpp @@ -1,10 +1,7 @@ #include "dcp_core.h" -#include -#include -#include -#include -using namespace std; +#include +using std::sort; struct Pixel { @@ -33,7 +30,6 @@ void CalcAirlight(IplImage *darkchannel, IplImage *input, double A[]) } sort(v_darkchannel, v_darkchannel + count, [](struct Pixel &a, struct Pixel &b){ return a.value > b.value; }); - IplImage *mask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1); cvZero(mask); @@ -65,12 +61,12 @@ void CalcAirlight(IplImage *darkchannel, IplImage *input, double A[]) sort(v_channel, v_channel + count, [](struct Pixel &a, struct Pixel &b){ return a.value > b.value; }); - int channel_airlight = 0; + double channel_airlight = 0; for (int i = 0; i < count * 0.01; i++) { channel_airlight += v_channel[i].value; } - channel_airlight /= (count * 0.01); + channel_airlight = channel_airlight / (count * 0.01); A[k] = channel_airlight; free(v_channel); diff --git a/DCP/dcp.cpp b/DCP/dcp.cpp index 4e100c6..716759c 100644 --- a/DCP/dcp.cpp +++ b/DCP/dcp.cpp @@ -1,7 +1,4 @@ -#include "dcp.h" - -#include -using namespace std; +#include "dcp_core.h" void dehaze(IplImage *recover, IplImage *input) { @@ -12,18 +9,36 @@ void dehaze(IplImage *recover, IplImage *input) IplImage *transmission = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1); IplImage *refine_transmission = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1); - int darkchannelradius = MIN(width, height) * 0.02; - - - + int darkchannelradius = cvRound(MIN(width, height) * 0.02); double Airlight[3] = { 0.0, 0.0, 0.0 }; + + TicToc t; + t.tic(); + printf("CalcDarkChannel..."); CalcDarkChannel(darkchannel, input, darkchannelradius); - + t.toc(); + + t.tic(); + printf("CalcAirlight..."); CalcAirlight(darkchannel, input, Airlight); - + t.toc(); + + t.tic(); + printf("CalcTransmission..."); CalcTransmission(transmission, input, Airlight, darkchannelradius); + t.toc(); + + t.tic(); + printf("GuidedFilterColor..."); GuidedFilterColor(refine_transmission, input, transmission, 1e-6, 60); - CalcRecover(recover, input, refine_transmission, Airlight); + t.toc(); - + t.tic(); + printf("CalcRecover..."); + CalcRecover(recover, input, refine_transmission, Airlight); + t.toc(); + + cvReleaseImage(&refine_transmission); + cvReleaseImage(&transmission); + cvReleaseImage(&darkchannel); } diff --git a/DCP/dcp_core.h b/DCP/dcp_core.h index 85e4d59..658e461 100644 --- a/DCP/dcp_core.h +++ b/DCP/dcp_core.h @@ -4,11 +4,17 @@ #include #include +#include + +#include +#include + +#include "utility.h" + void CalcDarkChannel(IplImage *darkchannel, IplImage *input, int radius); void CalcAirlight(IplImage *darkchannel, IplImage *input, double A[]); void CalcTransmission(IplImage *transmission, IplImage *input, double A[], int radius); void CalcRecover(IplImage *result, IplImage *input, IplImage *transmission, double A[]); void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r); - #endif \ No newline at end of file diff --git a/DCP/guidedfilter.cpp b/DCP/guidedfilter.cpp index bfdf956..34d255a 100644 --- a/DCP/guidedfilter.cpp +++ b/DCP/guidedfilter.cpp @@ -1,10 +1,5 @@ #include "dcp_core.h" -#include -#include - -using namespace std; - void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r) { int height = q->height; @@ -35,26 +30,24 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r double tmp_Ir, tmp_Ig, tmp_Ib; double tmp_p, tmp_q; - vector v_ak_r; - vector v_ak_g; - vector v_ak_b; - vector v_bk; + double *v_ak_r = (double *)malloc(sizeof(double) * height * width); + double *v_ak_g = (double *)malloc(sizeof(double) * height * width); + double *v_ak_b = (double *)malloc(sizeof(double) * height * width); + double *v_bk = (double *)malloc(sizeof(double) * height * width); + + int count = 0; - for (i = 1; i <= height; i++) + for (i = 0; i < height; i++) { - for (j = 1; j <= width; j++) + for (j = 0; j < width; j++) { st_row = i - r, ed_row = i + r; st_col = j - r, ed_col = j + r; - if (st_row <= 0) - st_row = 1; - if (ed_row > height) - ed_row = height; - if (st_col <= 0) - st_col = 1; - if (ed_col > width) - ed_col = width; + st_row = st_row < 0 ? 0 : st_row; + ed_row = ed_row >= height ? (height - 1) : ed_row; + st_col = st_col < 0 ? 0 : st_col; + ed_col = ed_col >= width ? (width - 1) : ed_col; sum_Ir = sum_Ig = sum_Ib = 0; sum_Ir_square = sum_Ig_square = sum_Ib_square = 0; @@ -67,11 +60,11 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r { for (n = st_col; n <= ed_col; n++) { - tmp_Ib = *(uchar *)(II->imageData + (m - 1) * widthstep + (n - 1) * nch); - tmp_Ig = *(uchar *)(II->imageData + (m - 1) * widthstep + (n - 1) * nch + 1); - tmp_Ir = *(uchar *)(II->imageData + (m - 1) * widthstep + (n - 1) * nch + 2); + tmp_Ib = *(uchar *)(II->imageData + (m) * widthstep + (n) * nch); + tmp_Ig = *(uchar *)(II->imageData + (m) * widthstep + (n) * nch + 1); + tmp_Ir = *(uchar *)(II->imageData + (m) * widthstep + (n) * nch + 2); - tmp_p = *(uchar *)(p->imageData + (m - 1) * gwidthstep + (n - 1)); + tmp_p = *(uchar *)(p->imageData + (m) * gwidthstep + (n)); sum_Ib += tmp_Ib; sum_Ig += tmp_Ig; @@ -118,41 +111,34 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r bk = (sum_PiIg - sum_IrIg * ak_r - (sum_Ig_square + w * eps) * ak_g - sum_IgIb * ak_b) / sum_Ig; - tmp_Ib = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch); - tmp_Ig = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 1); - tmp_Ir = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 2); + tmp_Ib = *(uchar *)(II->imageData + i * widthstep + j * nch); + tmp_Ig = *(uchar *)(II->imageData + i * widthstep + j * nch + 1); + tmp_Ir = *(uchar *)(II->imageData + i * widthstep + j * nch + 2); tmp_q = ak_b * tmp_Ib + ak_g * tmp_Ig + ak_r * tmp_Ir + bk; + tmp_q = tmp_q > 255 ? 255 : (tmp_q < 0 ? 0 : tmp_q); - if (tmp_q > 255) - tmp_q = 255; - else if (tmp_q < 0) - tmp_q = 0; + *(uchar *)(q->imageData + i * gwidthstep + j) = cvRound(tmp_q); - *(uchar *)(q->imageData + (i - 1) * gwidthstep + (j - 1)) = tmp_q; - - v_ak_b.push_back(ak_b); - v_ak_g.push_back(ak_g); - v_ak_r.push_back(ak_r); - v_bk.push_back(bk); + v_ak_b[count] = ak_b; + v_ak_g[count] = ak_g; + v_ak_r[count] = ak_r; + v_bk[count] = bk; + count++; } } - for (int i = 1; i <= height; i++) + for (int i = 0; i < height; i++) { - for (int j = 1; j <= width; j++) + for (int j = 0; j < width; j++) { st_row = i - r, ed_row = i + r; st_col = j - r, ed_col = j + r; - if (st_row <= 0) - st_row = 1; - if (ed_row > height) - ed_row = height; - if (st_col <= 0) - st_col = 1; - if (ed_col > width) - ed_col = width; + st_row = st_row < 0 ? 0 : st_row; + ed_row = ed_row >= height ? (height - 1) : ed_row; + st_col = st_col < 0 ? 0 : st_col; + ed_col = ed_col >= width ? (width - 1) : ed_col; double ak_r, ak_g, ak_b, bk; ak_r = ak_g = ak_b = bk = 0; @@ -162,10 +148,10 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r { for (int n = st_col; n <= ed_col; n++) { - ak_r += v_ak_r[(m - 1) * width + n - 1]; - ak_g += v_ak_g[(m - 1) * width + n - 1]; - ak_b += v_ak_b[(m - 1) * width + n - 1]; - bk += v_bk[(m - 1) * width + n - 1]; + ak_r += v_ak_r[(m) * width + n]; + ak_g += v_ak_g[(m) * width + n]; + ak_b += v_ak_b[(m) * width + n]; + bk += v_bk[(m) * width + n]; number++; } } @@ -175,18 +161,18 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r ak_b /= number; bk /= number; - tmp_Ib = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch); - tmp_Ig = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 1); - tmp_Ir = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 2); + tmp_Ib = *(uchar *)(II->imageData + i * widthstep + j * nch); + tmp_Ig = *(uchar *)(II->imageData + i * widthstep + j * nch + 1); + tmp_Ir = *(uchar *)(II->imageData + i * widthstep + j * nch + 2); tmp_q = ak_b * tmp_Ib + ak_g * tmp_Ig + ak_r * tmp_Ir + bk; - - if (tmp_q > 255) - tmp_q = 255; - else if (tmp_q < 0) - tmp_q = 0; - - *(uchar *)(q->imageData + (i - 1) * gwidthstep + (j - 1)) = tmp_q; + tmp_q = tmp_q > 255 ? 255 : (tmp_q < 0 ? 0 : tmp_q); + + *(uchar *)(q->imageData + i * gwidthstep + j) = cvRound(tmp_q); } } + free(v_ak_b); + free(v_ak_g); + free(v_ak_r); + free(v_bk); } diff --git a/DCP/packages.config b/DCP/packages.config deleted file mode 100644 index b2057a2..0000000 --- a/DCP/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/DCP/recover.cpp b/DCP/recover.cpp index 5dbd75e..b4609d7 100644 --- a/DCP/recover.cpp +++ b/DCP/recover.cpp @@ -27,17 +27,14 @@ void CalcRecover(IplImage *result, IplImage *input, IplImage *transmission, doub t /= 255.0; for (k = 0; k < 3; k++) { - a = *(uchar *)(input->imageData + (i) * widthstep + (j) * nch + k); + a = *(uchar *)(input->imageData + i * widthstep + j * nch + k); a /= 255.0; tmp_res = ((a - A[k]) / MAX(t, t0)) + A[k]; + tmp_res *= 255.0; + tmp_res = tmp_res > 255 ? 255 : (tmp_res < 0 ? 0 : tmp_res); - if (tmp_res > 1) - tmp_res = 1; - else if (tmp_res < 0) - tmp_res = 0; - - *(uchar *)(result->imageData + (i) * widthstep + (j) * nch + k) = tmp_res * 255.0; + *(uchar *)(result->imageData + i * widthstep + j * nch + k) = cvRound(tmp_res); } } } diff --git a/DCP/test.cpp b/DCP/test.cpp index 7efeeb7..2af8719 100644 --- a/DCP/test.cpp +++ b/DCP/test.cpp @@ -2,13 +2,12 @@ int main() { - IplImage *input = cvLoadImage("input.png"); + IplImage *input = cvLoadImage("house-input.bmp"); IplImage *result = cvCreateImage(cvGetSize(input), IPL_DEPTH_8U, 3); dehaze(result, input); - cvNamedWindow("Result"); - cvShowImage("Result", result); - cvWaitKey(); - + cvSaveImage("result.bmp", result); + cvReleaseImage(&result); + cvReleaseImage(&input); return 0; -} \ No newline at end of file +} diff --git a/DCP/transmission.cpp b/DCP/transmission.cpp index 48a8299..0079ef7 100644 --- a/DCP/transmission.cpp +++ b/DCP/transmission.cpp @@ -22,7 +22,7 @@ void CalcTransmission(IplImage *transmission, IplImage *input, double A[], int r tmp = tmp / A[k] * 255.0; tmp = tmp > 255 ? 255 : tmp; - *(uchar *)(normalized_input->imageData + i * widthstep + j * nch + k) = tmp; + *(uchar *)(normalized_input->imageData + i * widthstep + j * nch + k) = cvRound(tmp); } } } @@ -35,11 +35,10 @@ void CalcTransmission(IplImage *transmission, IplImage *input, double A[], int r double tran = *(uchar *)(transmission->imageData + i * gwidthstep + j); tran = 1 - w * (tran / 255.0); + tran *= 255.0; + tran = tran > 255 ? 255 : (tran < 0 ? 0 : tran); - tran = tran > 1 ? 1 : tran; - tran = tran < 0 ? 0 : tran; - - *(uchar *)(transmission->imageData + i * gwidthstep + j) = tran * 255; + *(uchar *)(transmission->imageData + i * gwidthstep + j) = cvRound(tran); } } cvReleaseImage(&normalized_input); diff --git a/DCP/utility.h b/DCP/utility.h new file mode 100644 index 0000000..55cc100 --- /dev/null +++ b/DCP/utility.h @@ -0,0 +1,29 @@ +#ifndef UTILITY_H +#define UTILITY_H + +#include +#include + +class TicToc +{ +private: + typedef std::chrono::high_resolution_clock clock; + typedef std::chrono::microseconds res; + clock::time_point t1, t2; + +public: + void tic() + { + t1 = clock::now(); + } + + void toc() + { + t2 = clock::now(); + std::cout << "Elapsed time: " + << std::chrono::duration_cast(t2 - t1).count() / 1e6 + << " seconds." << std::endl; + } +}; + +#endif \ No newline at end of file -- cgit v1.2.3