FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Coherent-Line-Drawing/src/CLD.cpp at cpp11 · SSARCandy/Coherent-Line-Drawing · GitHub
SSARCandy
/
Coherent-Line-Drawing
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
64
Star
568
Code
Issues
1
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
Coherent-Line-Drawing
/
src
/
CLD.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
225 lines (177 loc) · 7.08 KB
Breadcrumbs
Coherent-Line-Drawing
/
src
/
CLD.cpp
Copy path
File metadata and controls
225 lines (177 loc) · 7.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#
include
<
opencv2/opencv.hpp
>
#
include
"
CLD.h
"
//
Eq.(8)
inline
double
gauss
(
double
x,
double
mean,
double
sigma)
{
return
(
exp
((-(x - mean) * (x - mean)) / (
2
* sigma * sigma)) /
sqrt
(
M_PI
*
2.0
* sigma * sigma));
}
void
MakeGaussianVector
(
double
sigma, std::vector<
double
> &
GAU
)
{
constexpr
double
threshold =
0.001
;
int
i =
0
;
while
(
true
) {
i++;
if
(
gauss
((
double
)i,
0.0
, sigma) < threshold)
break
;
}
GAU
.
clear
();
GAU
.
resize
(i +
1
);
GAU
[
0
] =
gauss
(
0.0
,
0.0
, sigma);
for
(
int
j =
1
; j <
GAU
.
size
(); j++) {
GAU
[j] =
gauss
((
double
)j,
0.0
, sigma);
}
}
CLD
::
CLD
()
{
cv::Size
s
(
300
,
300
);
init
(s);
}
CLD
::
CLD
(
const
cv::Size s) {
init
(s); }
void
CLD::init
(
const
cv::Size s)
{
originalImg =
cv::Mat::zeros
(s,
CV_8UC1
);
result =
cv::Mat::zeros
(s,
CV_8UC1
);
DoG =
cv::Mat::zeros
(s,
CV_32FC1
);
FDoG =
cv::Mat::zeros
(s,
CV_32FC1
);
etf =
ETF
(s);
sigma_m =
3.0
;
sigma_c =
1.0
;
rho =
0.997
;
tau =
0.8
;
}
void
CLD::readSrc
(
const
std::string file)
{
originalImg =
cv::imread
(file,
CV_LOAD_IMAGE_GRAYSCALE
);
result =
cv::Mat::zeros
(
cv::Size
(originalImg.
cols
, originalImg.
rows
),
CV_8UC1
);
DoG =
cv::Mat::zeros
(
cv::Size
(originalImg.
cols
, originalImg.
rows
),
CV_32FC1
);
FDoG =
cv::Mat::zeros
(
cv::Size
(originalImg.
cols
, originalImg.
rows
),
CV_32FC1
);
etf.
initial_ETF
(file, originalImg.
size
());
//
genCLD();
}
void
CLD::genCLD
()
{
cv::Mat originalImg_32FC1 =
cv::Mat::zeros
(
cv::Size
(originalImg.
cols
, originalImg.
rows
),
CV_32FC1
);
originalImg.
convertTo
(originalImg_32FC1,
CV_32FC1
,
1.0
/
255.0
);
gradientDoG
(originalImg_32FC1, DoG,
this
->
rho
,
this
->
sigma_c
);
flowDoG
(DoG, FDoG,
this
->
sigma_m
);
//
flowDoG(DoG, FDoG, this->sigma_m);
binaryThresholding
(FDoG, result,
this
->
tau
);
}
/*
*
* Flow-based DoG filtering
*/
void
CLD::flowDoG
(
const
cv::Mat &src, cv::Mat &dst,
const
double
sigma_m)
{
std::vector<
double
> gau_m;
MakeGaussianVector
(sigma_m, gau_m);
const
int
img_w = src.
cols
;
const
int
img_h = src.
rows
;
const
int
kernel_half = gau_m.
size
() -
1
;
#
pragma
omp parallel for
for
(
int
y =
0
; y < img_h; y++) {
for
(
int
x =
0
; x < img_w; x++) {
double
gau_m_acc = -gau_m[
0
] * src.
at
<
float
>(y, x);
double
gau_m_weight_acc = -gau_m[
0
];
//
Intergral alone ETF
cv::Point2f
pos
(x, y);
for
(
int
step =
0
; step < kernel_half; step++) {
cv::Vec3f tmp = etf.
flowField
.
at
<cv::Vec3f>((
int
)
round
(pos.
y
), (
int
)
round
(pos.
x
));
cv::Point2f direction =
cv::Point2f
(tmp[
1
], tmp[
0
]);
if
(direction.
x
==
0
&& direction.
y
==
0
)
break
;
if
(pos.
x
> (
double
)img_w -
1
|| pos.
x
<
0.0
|| pos.
y
> (
double
)img_h -
1
|| pos.
y
<
0.0
)
break
;
float
value = src.
at
<
float
>((
int
)
round
(pos.
y
), (
int
)
round
(pos.
x
));
float
weight = gau_m[step];
gau_m_acc += value * weight;
gau_m_weight_acc += weight;
//
move alone ETF direction
pos += direction;
if
((
int
)
round
(pos.
x
) <
0
|| (
int
)
round
(pos.
x
) > img_w -
1
|| (
int
)
round
(pos.
y
) <
0
||
(
int
)
round
(pos.
y
) > img_h -
1
)
break
;
}
//
Intergral alone inverse ETF
pos =
cv::Point2f
(x, y);
for
(
int
step =
0
; step < kernel_half; step++) {
cv::Vec3f tmp = -etf.
flowField
.
at
<cv::Vec3f>((
int
)
round
(pos.
y
), (
int
)
round
(pos.
x
));
cv::Point2f direction =
cv::Point2f
(tmp[
1
], tmp[
0
]);
if
(direction.
x
==
0
&& direction.
y
==
0
)
break
;
if
(pos.
x
> (
double
)img_w -
1
|| pos.
x
<
0.0
|| pos.
y
> (
double
)img_h -
1
|| pos.
y
<
0.0
)
break
;
float
value = src.
at
<
float
>((
int
)
round
(pos.
y
), (
int
)
round
(pos.
x
));
float
weight = gau_m[step];
gau_m_acc += value * weight;
gau_m_weight_acc += weight;
//
move alone ETF direction
pos += direction;
if
((
int
)
round
(pos.
x
) <
0
|| (
int
)
round
(pos.
x
) > img_w -
1
|| (
int
)
round
(pos.
y
) <
0
||
(
int
)
round
(pos.
y
) > img_h -
1
)
break
;
}
dst.
at
<
float
>(y, x) = (gau_m_acc / gau_m_weight_acc) >
0
?
1.0
:
1
+
tanh
(gau_m_acc / gau_m_weight_acc);
}
}
cv::normalize
(dst, dst,
0
,
1
, cv::
NORM_MINMAX
);
}
void
CLD::gradientDoG
(
const
cv::Mat &src, cv::Mat &dst,
const
double
rho,
const
double
sigma_c)
{
const
double
sigma_s =
SIGMA_RATIO
* sigma_c;
std::vector<
double
> gau_c, gau_s;
MakeGaussianVector
(sigma_c, gau_c);
MakeGaussianVector
(sigma_s, gau_s);
const
int
kernel = gau_s.
size
() -
1
;
#
pragma
omp parallel for
for
(
int
y =
0
; y < dst.
rows
; y++) {
for
(
int
x =
0
; x < dst.
cols
; x++) {
double
gau_c_acc =
0
;
double
gau_s_acc =
0
;
double
gau_c_weight_acc =
0
;
double
gau_s_weight_acc =
0
;
cv::Vec3f tmp = etf.
flowField
.
at
<cv::Vec3f>(y, x);
cv::Point2f gradient =
cv::Point2f
(-tmp[
0
], tmp[
1
]);
if
(gradient.
x
==
0
&& gradient.
y
==
0
)
continue
;
for
(
int
step = -kernel; step <= kernel; step++) {
double
row = y + gradient.
y
* step;
double
col = x + gradient.
x
* step;
if
(col > (
double
)dst.
cols
-
1
|| col <
0.0
|| row > (
double
)dst.
rows
-
1
|| row <
0.0
)
continue
;
float
value = src.
at
<
float
>((
int
)
round
(row), (
int
)
round
(col));
int
gau_idx =
abs
(step);
double
gau_c_weight = (gau_idx >= gau_c.
size
()) ?
0.0
: gau_c[gau_idx];
double
gau_s_weight = gau_s[gau_idx];
gau_c_acc += value * gau_c_weight;
gau_s_acc += value * gau_s_weight;
gau_c_weight_acc += gau_c_weight;
gau_s_weight_acc += gau_s_weight;
}
double
v_c = gau_c_acc / gau_c_weight_acc;
double
v_s = gau_s_acc / gau_s_weight_acc;
dst.
at
<
float
>(y, x) = v_c - rho * v_s;
}
}
}
void
CLD::binaryThresholding
(
const
cv::Mat &src, cv::Mat &dst,
const
double
tau)
{
#
pragma
omp parallel for
for
(
int
y =
0
; y < dst.
rows
; y++) {
for
(
int
x =
0
; x < dst.
cols
; x++) {
const
float
H = src.
at
<
float
>(y, x);
const
int
v = H < tau ?
0
:
255
;
dst.
at
<uchar>(y, x) = v;
}
}
}
/*
*
* re-initialize the filter input
* by superimposing the black edge pixels of the previous binary output upon the original image
*/
void
CLD::combineImage
()
{
#
pragma
omp parallel for
for
(
int
y =
0
; y < originalImg.
rows
; y++) {
for
(
int
x =
0
; x < originalImg.
cols
; x++) {
const
float
H = result.
at
<uchar>(y, x);
if
(H ==
0
) {
originalImg.
at
<uchar>(y, x) =
0
;
}
}
}
//
Blur a little-bit to let image more smooth
cv::GaussianBlur
(originalImg, originalImg,
cv::Size
(
3
,
3
),
0
,
0
);
}
Back
|
FazBrowse Home
|
New Git URL