-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcld.go
402 lines (331 loc) · 9.8 KB
/
cld.go
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// MIT License
//
// Copyright (c) 2019 Endre Simo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package function
import (
"fmt"
"image"
"math"
"os"
"sync"
"gocv.io/x/gocv"
)
// Cld is the main entry struct for the Coherent Line Drawing operations.
type Cld struct {
image gocv.Mat
result gocv.Mat
dog gocv.Mat
fDog gocv.Mat
etf *Etf
wg sync.WaitGroup
options
}
// Options struct contains all the options currently supported by Cld,
// exposed by the main CLI application.
type options struct {
sigmaR float64
sigmaM float64
sigmaC float64
rho float64
tau float32
blurSize int
etfKernel int
etfIteration int
fDogIteration int
antiAlias bool
visEtf bool
visResult bool
}
// position is a basic struct for vector type operations
type position struct {
x, y float64
}
// NewCLD is the constructor method which require the source image and the CLD options as parameters.
func NewCLD(imgFile string, cldOpts options) (*Cld, error) {
f, err := os.Stat(imgFile)
if os.IsNotExist(err) {
return nil, err
}
if f.IsDir() {
return nil, fmt.Errorf("missing file name")
}
srcImage := gocv.IMRead(imgFile, gocv.IMReadGrayScale)
rows, cols := srcImage.Rows(), srcImage.Cols()
result := gocv.NewMatWithSize(rows, cols, gocv.MatTypeCV8UC1)
dog := gocv.NewMatWithSize(rows, cols, gocv.MatTypeCV32F)
fDog := gocv.NewMatWithSize(rows, cols, gocv.MatTypeCV32F)
var wg sync.WaitGroup
etf := NewETF()
etf.Init(cols, rows)
err = etf.InitDefaultEtf(imgFile, image.Point{X: cols, Y: rows})
if err != nil {
return nil, fmt.Errorf("unable to initialize edge tangent flow: %s", err)
}
if cldOpts.etfIteration > 0 {
for i := 0; i < cldOpts.etfIteration; i++ {
etf.RefineEtf(cldOpts.etfKernel)
}
}
return &Cld{
srcImage, result, dog, fDog, etf, wg, cldOpts,
}, nil
}
// GenerateCld is the entry method for generating the coherent line drawing output.
// It triggers the generate method in iterative manner and returns the resulting byte array.
func (c *Cld) GenerateCld() []byte {
c.generate()
if c.fDogIteration > 0 {
for i := 0; i < c.fDogIteration; i++ {
c.combineImage()
c.generate()
}
}
pp := NewPostProcessing(c.blurSize)
if c.antiAlias {
pp.AntiAlias(c.result, c.result)
}
return c.result.ToBytes()
}
// generate is a helper method which enclose all the requested operation for the CLD computation.
func (c *Cld) generate() {
srcImg32FC1 := gocv.NewMatWithSize(c.image.Rows(), c.image.Cols(), gocv.MatTypeCV32F)
c.image.ConvertTo(&srcImg32FC1, gocv.MatTypeCV32F, 1.0/255.0)
c.gradientDoG(&srcImg32FC1, &c.dog, c.rho, c.sigmaC)
c.flowDoG(&c.dog, &c.fDog, c.sigmaM)
c.binaryThreshold(&c.fDog, &c.result, c.tau)
}
// gradientDoG computes the gradient difference-of-Gaussians (DoG)
func (c *Cld) gradientDoG(src, dst *gocv.Mat, rho, sigmaC float64) {
var sigmaS = c.sigmaR * sigmaC
gvc := makeGaussianVector(sigmaC)
gvs := makeGaussianVector(sigmaS)
kernel := len(gvs) - 1
width, height := dst.Cols(), dst.Rows()
c.wg.Add(width * height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
go func(y, x int) {
var (
gauCAcc, gauSAcc float64
gauCWeightAcc, gauSWeightAcc float64
)
c.etf.mu.Lock()
defer c.etf.mu.Unlock()
tmp := c.etf.flowField.GetVecfAt(y, x)
gradient := position{x: float64(-tmp[0]), y: float64(tmp[1])}
for step := -kernel; step <= kernel; step++ {
row := float64(y) + gradient.y*float64(step)
col := float64(x) + gradient.x*float64(step)
if row > float64(dst.Rows()-1) || row < 0.0 || col > float64(dst.Cols()-1) || col < 0.0 {
continue
}
val := src.GetFloatAt(int(round(row)), int(round(col)))
gauIdx := absInt(step)
gauCWeight := func(gauIdx int) float64 {
if gauIdx >= len(gvc) {
return 0.0
}
return gvc[gauIdx]
}(gauIdx)
gauSWeight := gvs[gauIdx]
gauCAcc += float64(val) * gauCWeight
gauSAcc += float64(val) * gauSWeight
gauCWeightAcc += gauCWeight
gauSWeightAcc += gauSWeight
}
vc := gauCAcc / gauCWeightAcc
vs := gauSAcc / gauSWeightAcc
res := vc - rho*vs
dst.SetFloatAt(y, x, float32(res))
c.wg.Done()
}(y, x)
}
}
c.wg.Wait()
}
// flowDoG computes the flow difference-of-Gaussians (DoG)
func (c *Cld) flowDoG(src, dst *gocv.Mat, sigmaM float64) {
var (
gauAcc float64
gauWeightAcc float64
)
gausVec := makeGaussianVector(sigmaM)
width, height := src.Cols(), src.Rows()
kernelHalf := len(gausVec) - 1
c.wg.Add(width * height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
go func(y, x int) {
c.etf.mu.Lock()
defer c.etf.mu.Unlock()
gauAcc = -gausVec[0] * float64(src.GetFloatAt(y, x))
gauWeightAcc = -gausVec[0]
// Integral alone ETF
pos := &position{x: float64(x), y: float64(y)}
for step := 0; step < kernelHalf; step++ {
tmp := c.etf.flowField.GetVecfAt(int(pos.y), int(pos.x))
direction := &position{x: float64(tmp[1]), y: float64(tmp[0])}
if direction.x == 0 && direction.y == 0 {
break
}
if pos.x > float64(width-1) || pos.x < 0.0 ||
pos.y > float64(height-1) || pos.y < 0.0 {
break
}
value := src.GetFloatAt(int(pos.y), int(pos.x))
weight := gausVec[step]
gauAcc += float64(value) * weight
gauWeightAcc += weight
// move along ETF direction
pos.x += direction.x
pos.y += direction.y
if int(round(pos.x)) < 0 || int(round(pos.x)) > width-1 ||
int(round(pos.y)) < 0 || int(round(pos.y)) > height-1 {
break
}
}
// Integral alone inverse ETF
pos = &position{x: float64(x), y: float64(y)}
for step := 0; step < kernelHalf; step++ {
tmp := c.etf.flowField.GetVecfAt(int(pos.y), int(pos.x))
direction := &position{x: float64(-tmp[1]), y: float64(-tmp[0])}
if direction.x == 0 && direction.y == 0 {
break
}
if pos.x > float64(width-1) || pos.x < 0.0 ||
pos.y > float64(height-1) || pos.y < 0.0 {
break
}
value := src.GetFloatAt(int(pos.y), int(pos.x))
weight := gausVec[step]
gauAcc += float64(value) * weight
gauWeightAcc += weight
// move along ETF direction
pos.x += direction.x
pos.y += direction.y
if int(round(pos.x)) < 0 || int(round(pos.x)) > width-1 ||
int(round(pos.y)) < 0 || int(round(pos.y)) > height-1 {
break
}
}
newVal := func(gauAcc, gauWeightAcc float64) float64 {
var res float64
if gauAcc/gauWeightAcc > 0 {
res = 1.0
} else {
res = 1.0 + math.Tanh(gauAcc/gauWeightAcc)
}
return res
}
// Update pixel value in the destination matrix.
dst.SetFloatAt(y, x, float32(newVal(gauAcc, gauWeightAcc)))
c.wg.Done()
}(y, x)
}
}
gocv.Normalize(*dst, dst, 0.0, 1.0, gocv.NormMinMax)
c.wg.Wait()
}
// binaryThreshold threshold an image as black and white.
func (c *Cld) binaryThreshold(src, dst *gocv.Mat, tau float32) []byte {
width, height := dst.Cols(), dst.Rows()
c.wg.Add(width * height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
go func(y, x int) {
c.etf.mu.Lock()
defer c.etf.mu.Unlock()
h := src.GetFloatAt(y, x)
v := func(h float32) uint8 {
if h < tau {
return 0
}
return 255
}(h)
dst.SetUCharAt(y, x, v)
c.wg.Done()
}(y, x)
}
}
c.wg.Wait()
return dst.ToBytes()
}
func (c *Cld) combineImage() {
for y := 0; y < c.image.Rows(); y++ {
for x := 0; x < c.image.Cols(); x++ {
c.wg.Add(1)
go func(y, x int) {
c.etf.mu.Lock()
defer c.etf.mu.Unlock()
h := c.result.GetUCharAt(y, x)
if h == 0 {
c.image.SetUCharAt(y, x, 0)
}
c.wg.Done()
}(y, x)
}
}
// Apply a gaussian blur to let it more smooth
gocv.GaussianBlur(c.image, &c.image, image.Point{c.blurSize, c.blurSize}, 0.0, 0.0, gocv.BorderConstant)
c.wg.Wait()
}
// gauss computes gaussian function of variance
func gauss(x, mean, sigma float64) float64 {
return math.Exp((-(x-mean)*(x-mean))/(2*sigma*sigma)) / math.Sqrt(math.Pi*2.0*sigma*sigma)
}
// makeGaussianVector constructs a gaussian vector field of floats
func makeGaussianVector(sigma float64) []float64 {
var (
gau []float64
threshold = 0.001
i int
)
for {
i++
if gauss(float64(i), 0.0, sigma) < threshold {
break
}
}
// clear slice
gau = gau[:0]
// extend slice
gau = append(gau, make([]float64, i+1)...)
gau[0] = gauss(0.0, 0.0, sigma)
for j := 1; j < len(gau); j++ {
gau[j] = gauss(float64(j), 0.0, sigma)
}
return gau
}
// absInt return the absolute value of x
func absInt(x int) int {
if x < 0 {
return -x
}
return x
}
// round returns the nearest integer, rounding ties away from zero.
func round(x float64) float64 {
t := math.Trunc(x)
if math.Abs(x-t) >= 0.5 {
return t + math.Copysign(1, x)
}
return t
}