forked from PetteriAimonen/focus-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_denoise.cc
More file actions
79 lines (65 loc) · 1.53 KB
/
task_denoise.cc
File metadata and controls
79 lines (65 loc) · 1.53 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
73
74
75
76
77
#include "task_denoise.hh"
#include "task_wavelet.hh"
using namespace focusstack;
Task_Denoise::Task_Denoise(std::shared_ptr<ImgTask> input, float level)
{
m_filename = "denoised_" + input->basename();
m_name = "Denoise " + input->basename();
m_index = input->index();
m_input = input;
m_depends_on.push_back(input);
m_level = level;
}
static inline float threshold_filter(float x, float level)
{
if (x < -level)
{
return x + level;
}
else if (x > level)
{
return x - level;
}
else
{
return 0.0f;
}
}
void Task_Denoise::task()
{
cv::Mat src = m_input->img();
m_result.create(src.rows, src.cols, CV_32FC2);
src.copyTo(m_result);
int levels = Task_Wavelet::levels_for_size(src.size());
int lowest_w = src.cols >> levels;
int lowest_h = src.rows >> levels;
for (int y = 0; y < src.rows; y++)
{
for (int x = 0; x < src.cols; x++)
{
if (y < lowest_h && x < lowest_w)
{
// Don't filter the downscaled image
continue;
}
cv::Vec2f v = m_result.at<cv::Vec2f>(y, x);
float absval = v[0] * v[0] + v[1] * v[1];
if (absval <= m_level)
{
// Cut off all wavelets below the threshold
v[0] = 0.0f;
v[1] = 0.0f;
}
else
{
// Diminish other wavelets, but don't change the phase.
float ratio = (absval - m_level) / absval;
v[0] *= ratio;
v[1] *= ratio;
}
m_result.at<cv::Vec2f>(y, x) = v;
}
}
m_valid_area = m_input->valid_area();
m_input.reset();
}