forked from PetteriAimonen/focus-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_merge.cc
More file actions
182 lines (159 loc) · 5.18 KB
/
task_merge.cc
File metadata and controls
182 lines (159 loc) · 5.18 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "task_merge.hh"
#include "task_wavelet.hh"
using namespace focusstack;
Task_Merge::Task_Merge(std::shared_ptr<Task_Merge> prev_merge,
const std::vector<std::shared_ptr<ImgTask> > &images,
int consistency):
m_prev_merge(prev_merge), m_images(images), m_consistency(consistency)
{
m_filename = "merge_result.jpg";
m_name = "Merge " + std::to_string(m_images.size()) + " images";
if (prev_merge)
m_depends_on.push_back(prev_merge);
m_depends_on.insert(m_depends_on.begin(), images.begin(), images.end());
}
void Task_Merge::task()
{
int rows = m_images.front()->img().rows;
int cols = m_images.front()->img().cols;
cv::Mat max_absval(rows, cols, CV_32F);
if (m_prev_merge)
{
m_result = m_prev_merge->img().clone();
m_depthmap = m_prev_merge->depthmap();
get_sq_absval(m_result, max_absval);
}
else
{
m_result.create(rows, cols, CV_32FC2);
m_depthmap.create(rows, cols, CV_16U);
m_depthmap = 0;
max_absval = -1.0f;
}
// Most of the pixel copying is done in loop below using masks, as it is faster.
// Minor touch-ups are done per-pixel in denoise loops.
// Keep a map from image index to image pointer for the denoise step.
m_index_map.clear();
m_index_map.reserve(m_images.size());
// For each pixel in the wavelet image, select the wavelet with highest
// absolute value.
for (int i = 0; i < m_images.size(); i++)
{
const cv::Mat &wavelet = m_images.at(i)->img();
cv::Mat absval(rows, cols, CV_32F);
get_sq_absval(wavelet, absval);
cv::Mat mask = (absval > max_absval);
absval.copyTo(max_absval, mask);
wavelet.copyTo(m_result, mask);
m_depthmap.setTo(m_images.at(i)->index(), mask);
m_index_map[m_images.at(i)->index()] = m_images.at(i);
}
if (m_consistency >= 1)
{
denoise_subbands();
}
if (m_consistency >= 2)
{
denoise_neighbours();
}
// Find out the intersection of input image valid areas.
m_valid_area = m_images.at(0)->valid_area();
for (int i = 1; i < m_images.size(); i++)
{
limit_valid_area(m_images.at(i)->valid_area());
}
m_images.clear();
m_index_map.clear();
m_prev_merge.reset();
}
void Task_Merge::get_sq_absval(const cv::Mat& complex_mat, cv::Mat& absval)
{
for (int y = 0; y < complex_mat.rows; y++)
{
for (int x = 0; x < complex_mat.cols; x++)
{
cv::Vec2f v = complex_mat.at<cv::Vec2f>(y, x);
absval.at<float>(y, x) = v[0] * v[0] + v[1] * v[1];
}
}
}
cv::Mat Task_Merge::get_source_img(int index)
{
auto iter = m_index_map.find(index);
if (iter != m_index_map.end())
return iter->second->img();
else
return m_prev_merge->img();
}
// Compare the horizontal / vertical / diagonal subbands at each level
// and perform two-out-of-three voting filter.
void Task_Merge::denoise_subbands()
{
int levels = Task_Wavelet::levels_for_size(m_result.size());
for (int level = 0; level < levels; level++)
{
int w = m_result.cols >> level;
int h = m_result.rows >> level;
int w2 = w / 2;
int h2 = h / 2;
cv::Mat sub1 = m_depthmap(cv::Rect(w2, 0, w2, h2));
cv::Mat sub2 = m_depthmap(cv::Rect(w2, h2, w2, h2));
cv::Mat sub3 = m_depthmap(cv::Rect(0, h2, w2, h2));
for (int y = 0; y < h2; y++)
{
for (int x = 0; x < w2; x++)
{
uint16_t v1 = sub1.at<uint16_t>(y, x);
uint16_t v2 = sub2.at<uint16_t>(y, x);
uint16_t v3 = sub3.at<uint16_t>(y, x);
// If two out of three subbands match, update the third one to match also.
if (v1 == v2 && v2 == v3)
{
// Nothing to do
}
else if (v2 == v3)
{
// Update sub1
sub1.at<uint16_t>(y, x) = v2;
m_result.at<cv::Vec2f>(y, w2 + x) = get_source_img(v2).at<cv::Vec2f>(y, w2 + x);
}
else if (v1 == v3)
{
// Update sub2
sub2.at<uint16_t>(y, x) = v1;
m_result.at<cv::Vec2f>(h2 + y, w2 + x) = get_source_img(v1).at<cv::Vec2f>(h2 + y, w2 + x);
}
else if (v1 == v2)
{
// Update sub3
sub3.at<uint16_t>(y, x) = v1;
m_result.at<cv::Vec2f>(h2 + y, x) = get_source_img(v1).at<cv::Vec2f>(h2 + y, x);
}
}
}
}
}
// Compare the four neighbours of each pixel and if they all
// are above/below, eliminate the center outlier.
void Task_Merge::denoise_neighbours()
{
for (int y = 1; y < m_depthmap.rows - 1; y++)
{
for (int x = 1; x < m_depthmap.cols - 1; x++)
{
uint16_t left = m_depthmap.at<uint16_t>(y, x - 1);
uint16_t right = m_depthmap.at<uint16_t>(y, x + 1);
uint16_t top = m_depthmap.at<uint16_t>(y - 1, x);
uint16_t bottom = m_depthmap.at<uint16_t>(y + 1, x);
uint16_t center = m_depthmap.at<uint16_t>(y, x);
if ((center > top && center > bottom && center > left && center > right) ||
(center < top && center < bottom && center < left && center < right))
{
// Center pixel is an outlier, average the side pixels to get a better value.
int avg = (top + bottom + left + right + 2) / 4;
m_depthmap.at<uint16_t>(y, x) = avg;
m_result.at<cv::Vec2f>(y, x) = get_source_img(avg).at<cv::Vec2f>(y, x);
}
}
}
}