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
|
-
!
-
-
|
|
|
!
|
-
-
!
|
|
-
!
|
|
|
|
-
|
|
|
!
|
|
!
|
-
|
|
|
!
|
-
|
|
|
|
|
!
|
-
|
|
|
|
|
|
|
!
|
|
|
|
-
-
!
|
|
-
!
|
-
!
|
|
|
|
|
|
-
|
!
|
|
!
|
-
|
|
|
|
!
|
-
-
|
|
|
!
|
|
-
!
|
|
|
!
|
-
|
|
|
|
|
|
!
|
|
|
|
-
-
!
|
-
!
-
|
!
|
-
!
|
|
-
!
-
|
|
|
!
|
-
!
|
-
!
|
-
!
|
-
!
|
-
!
!
!
-
!
-
-
!
-
|
!
|
|
!
| #include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <algorithm>
#include <vector>
#include <cstdint>
#ifdef _MSC_VER
#ifdef NDEBUG
#define OPENCV_LIB(name) "opencv_" #name ".lib"
#else
#define OPENCV_LIB(name) "opencv_" #name "d.lib"
#endif #pragma comment(lib, OPENCV_LIB(core248))
#pragma comment(lib, OPENCV_LIB(imgproc248))
#pragma comment(lib, OPENCV_LIB(highgui248))
#endif
namespace
{
std::vector<cv::Mat> to_32f_channels(const cv::Mat& src)
{
std::vector<cv::Mat> planes;
cv::split(src, planes);
std::transform(
planes.begin(),
planes.end(),
planes.begin(),
[](const cv::Mat& p)
{
cv::Mat dest;
p.convertTo(dest, CV_32FC1);
return dest;
});
return planes;
}
cv::Mat to_8u_image(const std::vector<cv::Mat>& src_planes)
{
cv::Mat temp, dest;
cv::merge(src_planes, temp);
temp.convertTo(dest, CV_MAKETYPE(CV_8U, src_planes.size()));
return dest;
}
std::vector<cv::Mat> blend_for_channels(
const std::vector<cv::Mat>& back_planes,
const std::vector<cv::Mat>& front_planes,
const cv::Mat& front_alpha)
{
const auto scale_alpha = front_alpha * (1.0 / 255);
const auto scale_alpha_inv = cv::Scalar::all(1) - scale_alpha;
const auto plane_count = std::min(back_planes.size(), front_planes.size());
std::vector<cv::Mat> dest_planes(plane_count);
std::transform(
back_planes.begin(),
back_planes.begin() + plane_count,
front_planes.begin(),
dest_planes.begin(),
[&](const cv::Mat& back, const cv::Mat& front)
{
return back.mul(scale_alpha_inv) + front.mul(scale_alpha);
});
return dest_planes;
}
cv::Mat dilate_image(const cv::Mat& src, int size = 10)
{
const cv::Mat elem =
(cv::Mat_<std::uint8_t>(3, 3) << 0, 1, 0, 1, 1, 1, 0, 1, 0);
cv::Mat dest;
cv::dilate(src, dest, elem, cv::Point(-1, -1), size);
return dest;
}
bool hem_alpha_image_file(
const char* filename,
const cv::Scalar& hem_color = cv::Scalar::all(255),
int hem_size = 10)
{
const auto src_img = cv::imread(filename, -1);
if (src_img.empty() || src_img.elemSize1() != 1 || src_img.channels() != 4)
{
return false;
}
const auto img_planes = to_32f_channels(src_img);
const auto img_alpha = img_planes[3];
const std::vector<cv::Mat> hem_planes =
{
cv::Mat(src_img.rows, src_img.cols, CV_32FC1, hem_color[0]),
cv::Mat(src_img.rows, src_img.cols, CV_32FC1, hem_color[1]),
cv::Mat(src_img.rows, src_img.cols, CV_32FC1, hem_color[2]),
};
auto dest_planes = blend_for_channels(hem_planes, img_planes, img_alpha);
const cv::Mat dilate_alpha = dilate_image(img_alpha, hem_size);
dest_planes.push_back(dilate_alpha);
const cv::Mat dest_img = to_8u_image(dest_planes);
return cv::imwrite(filename, dest_img);
}
}
int main(int argc, char* argv[])
{
for (int i = 1; i < argc; ++i)
{
hem_alpha_image_file(argv[i]);
}
return 0;
}
|