-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdehaze.cpp
More file actions
72 lines (56 loc) · 2.45 KB
/
Copy pathdehaze.cpp
File metadata and controls
72 lines (56 loc) · 2.45 KB
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
72
#include "dehaze.h"
#include <ctime>
// Алгоритм удаления дымки с BGR изображения
cv::Mat dehaze(const cv::Mat& img, int patch_size, int max_iter, double eps, int lambda,
double tmin, double dp, bool log)
{
assert(img.channels() == 3 && "not bgr picture");
cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
gray = toDuble(gray);
cv::Vec3b a = get_atm_light(img);
cv::Scalar a_s(a[0], a[1], a[2]);
cv::Mat i = im2col<uchar>(img, patch_size, patch_size);
cv::Mat tmap = tmap_optimal(i, a_s, patch_size, max_iter, eps, log);
//Удаление блочных артефактов с карты пропускания
tmap = cv::repeat(tmap, patch_size * patch_size, 1);
tmap = col2im<double>(tmap, patch_size, patch_size, ceil((double)img.cols / patch_size), ceil((double)img.rows / patch_size));
tmap = tmap(cv::Range(0, img.rows), cv::Range(0, img.cols));
tmap = guided_filter(gray, tmap, 30, 1e-4);
cv::Mat adapt_light = adaptive_atm_light(tmap, img, gray, a_s, lambda);
cv::Mat res2;
{
cv::Mat dimg = toDuble(img);
cv::Mat temp;
cv::pow(cv::max(tmap, tmin), dp, temp);
cv::Mat res = (dimg - adapt_light);
std::vector<cv::Mat> temp_3ch = { temp, temp, temp };
cv::merge(temp_3ch, temp);
res = res / temp + adapt_light;
cv::threshold(res, res, 0.0, 0.0, cv::THRESH_TOZERO);
cv::threshold(res, res, 1.0, 1.0, cv::THRESH_TRUNC);
res.convertTo(res, CV_8UC3, 255.);
res2 = res.clone();
}
if (false)
{
cv::Mat img2 = cv::imread("/mnt/disk512/work/tools/dehaze/Nuzhny007/rel/orlan/03.45321.jpg");
cv::Mat dimg = toDuble(img2);
cv::Mat temp;
cv::pow(cv::max(tmap, tmin), dp, temp);
cv::Mat res = (dimg - adapt_light);
std::vector<cv::Mat> temp_3ch = { temp, temp, temp };
cv::merge(temp_3ch, temp);
res = res / temp + adapt_light;
cv::threshold(res, res, 0.0, 0.0, cv::THRESH_TOZERO);
cv::threshold(res, res, 1.0, 1.0, cv::THRESH_TRUNC);
res.convertTo(res, CV_8UC3, 255.);
cv::imwrite("res2.jpg", res);
cv::Mat adapt_light2;
adapt_light.convertTo(adapt_light2, CV_8U, 1.);
cv::imwrite("adapt_light_1.png", adapt_light2);
adapt_light.convertTo(adapt_light, CV_8U, 255.);
cv::imwrite("adapt_light_255.png", adapt_light);
}
return res2;
}