FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Coherent-Line-Drawing/src/ETF.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
/
ETF.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
145 lines (117 loc) · 3.99 KB
Breadcrumbs
Coherent-Line-Drawing
/
src
/
ETF.cpp
Copy path
File metadata and controls
145 lines (117 loc) · 3.99 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
#defin
e
_USE_MATH_DEFINE
S
#
include
<
cmath
>
#
include
<
opencv2/opencv.hpp
>
#
include
"
ETF.h
"
ETF
::
ETF
()
: s{
cv::Size
(
300
,
300
)}
, flowField{
cv::Mat::zeros
(s,
CV_32FC3
)}
, refinedETF{
cv::Mat::zeros
(s,
CV_32FC3
)}
, gradientMag{
cv::Mat::zeros
(s,
CV_32FC3
)}
{
;
}
ETF
::
ETF
(
const
cv::Size s)
: s{s}
, flowField{
cv::Mat::zeros
(s,
CV_32FC3
)}
, refinedETF{
cv::Mat::zeros
(s,
CV_32FC3
)}
, gradientMag{
cv::Mat::zeros
(s,
CV_32FC3
)}
{
;
}
/*
*
* Generate initial ETF
* by taking perpendicular vectors(counter-clockwise) from gradient map
*/
void
ETF::initial_ETF
(
const
std::string file,
const
cv::Size s)
{
//
Resizing Mat
cv::resize
(flowField, flowField, s,
0
,
0
,
CV_INTER_LINEAR
);
cv::resize
(refinedETF, refinedETF, s,
0
,
0
,
CV_INTER_LINEAR
);
cv::resize
(gradientMag, gradientMag, s,
0
,
0
,
CV_INTER_LINEAR
);
cv::Mat src =
cv::imread
(file,
1
);
cv::Mat src_n;
cv::Mat grad;
normalize
(src, src_n,
0.0
,
1.0
, cv::
NORM_MINMAX
,
CV_32FC1
);
//
GaussianBlur(src_n, src_n, cv::Size(51, 51), 0, 0);
//
Generate grad_x and grad_y
cv::Mat grad_x, grad_y, abs_grad_x, abs_grad_y;
Sobel
(src_n, grad_x,
CV_32FC1
,
1
,
0
,
5
);
Sobel
(src_n, grad_y,
CV_32FC1
,
0
,
1
,
5
);
//
Compute gradient
magnitude
(grad_x, grad_y, gradientMag);
normalize
(gradientMag, gradientMag,
0.0
,
1.0
, cv::
NORM_MINMAX
);
flowField =
cv::Mat::zeros
(src.
size
(),
CV_32FC3
);
#
pragma
omp parallel for
for
(
int
i =
0
; i < src.
rows
; ++i) {
for
(
int
j =
0
; j < src.
cols
; ++j) {
cv::Vec3f u = grad_x.
at
<cv::Vec3f>(i, j);
cv::Vec3f v = grad_y.
at
<cv::Vec3f>(i, j);
flowField.
at
<cv::Vec3f>(i, j) =
normalize
(
cv::Vec3f
(v.
val
[
0
], u.
val
[
0
],
0
));
}
}
flowField =
rotate
(flowField,
90
);
}
void
ETF::refine_ETF
(
int
kernel)
{
#
pragma
omp parallel for
for
(
int
r =
0
; r < flowField.
rows
; ++r) {
for
(
int
c =
0
; c < flowField.
cols
; ++c) {
refinedETF.
at
<cv::Vec3f>(r, c) =
computeNewVector
(c, r, kernel);
}
}
flowField = refinedETF.
clone
();
}
cv::Mat
ETF::rotate
(
const
cv::Mat &src,
const
double
degree)
{
const
double
theta{degree /
180.0
*
M_PI
};
cv::Mat dst{
cv::Mat::zeros
(src.
size
(),
CV_32FC3
)};
for
(
int
i =
0
; i < src.
rows
; ++i) {
for
(
int
j =
0
; j < src.
cols
; ++j) {
cv::Vec3f v = src.
at
<cv::Vec3f>(i, j);
const
float
rx = v[
0
] *
cos
(theta) - v[
1
] *
sin
(theta);
const
float
ry = v[
1
] *
cos
(theta) + v[
0
] *
sin
(theta);
dst.
at
<cv::Vec3f>(i, j) =
cv::Vec3f
(rx, ry,
0.0
);
}
}
return
dst;
}
/*
* Paper's Eq(1)
*/
cv::Vec3f
ETF::computeNewVector
(
const
int
x,
const
int
y,
const
int
kernel)
{
const
cv::Vec3f t_cur_x = flowField.
at
<cv::Vec3f>(y, x);
cv::Vec3f t_new =
cv::Vec3f
(
0
,
0
,
0
);
for
(
int
r = y - kernel; r <= y + kernel; ++r) {
for
(
int
c = x - kernel; c <= x + kernel; ++c) {
if
(r <
0
|| r >= refinedETF.
rows
|| c <
0
|| c >= refinedETF.
cols
)
continue
;
const
cv::Vec3f t_cur_y = flowField.
at
<cv::Vec3f>(r, c);
const
float
phi =
computePhi
(t_cur_x, t_cur_y);
const
float
w_s =
computeWs
(
cv::Point2f
(x, y),
cv::Point2f
(c, r), kernel);
const
float
w_m =
computeWm
(
norm
(gradientMag.
at
<cv::Vec3f>(y, x)),
cv::norm
(gradientMag.
at
<
float
>(r, c)));
const
float
w_d =
computeWd
(t_cur_x, t_cur_y);
t_new += phi * t_cur_y * w_s * w_m * w_d;
}
}
return
cv::normalize
(t_new);
}
/*
* Paper's Eq(5)
*/
float
ETF::computePhi
(
const
cv::Vec3f &x,
const
cv::Vec3f &y) {
return
x.
dot
(y) >
0
?
1
: -
1
; }
/*
* Paper's Eq(2)
*/
float
ETF::computeWs
(
const
cv::Point2f &x,
const
cv::Point2f &y,
const
int
r) {
return
norm
(x - y) < r ?
1
:
0
; }
/*
* Paper's Eq(3)
*/
float
ETF::computeWm
(
const
float
gradmag_x,
const
float
gradmag_y)
{
const
float
wm = (
1
+
tanh
(gradmag_y - gradmag_x)) /
2
;
return
wm;
}
/*
* Paper's Eq(4)
*/
float
ETF::computeWd
(
const
cv::Vec3f &x,
const
cv::Vec3f &y) {
return
abs
(x.
dot
(y)); }
Back
|
FazBrowse Home
|
New Git URL