-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontours_padding.go
46 lines (43 loc) · 1.56 KB
/
contours_padding.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
package cnns
import (
"gonum.org/v1/gonum/mat"
)
// ContoursPadding Apply edge padding to source matrix
/*
matrix - source matrix
num - number of columns to add and fill with edge values
*/
func ContoursPadding(matrix *mat.Dense, num int) *mat.Dense {
r, c := matrix.Dims()
outRows := r + num*2
outCols := c + num*2
flattenMatrix := make([]float64, outRows*outCols)
for y := 0; y < outRows; y++ {
contoursPadding(matrix, flattenMatrix, r, c, outCols, num, num-1, y)
}
return mat.NewDense(outRows, outCols, flattenMatrix)
}
// contoursPadding See ContoursPadding()
func contoursPadding(matrix *mat.Dense, newFlattenMatrix []float64, rows, cols, outCols int, w, wprev, y int) {
for x := 0; x < outCols; x++ {
if y < w && x < w {
newFlattenMatrix[y*outCols+x] = matrix.At(0, 0)
} else if y < w && x > wprev && x < cols+w {
newFlattenMatrix[y*outCols+x] = matrix.At(0, x-w)
} else if y < w && x > cols+wprev {
newFlattenMatrix[y*outCols+x] = matrix.At(0, cols-1)
} else if y > wprev && y < rows+w && x < w {
newFlattenMatrix[y*outCols+x] = matrix.At(y-w, 0)
} else if y > wprev && y < rows+w && x > cols+wprev {
newFlattenMatrix[y*outCols+x] = matrix.At(y-w, cols-1)
} else if y > rows+wprev && x < w {
newFlattenMatrix[y*outCols+x] = matrix.At(rows-1, 0)
} else if y > rows+wprev && x > wprev && x < cols+w {
newFlattenMatrix[y*outCols+x] = matrix.At(rows-1, x-w)
} else if y > rows+wprev && x > cols+wprev {
newFlattenMatrix[y*outCols+x] = matrix.At(rows-1, cols-1)
} else {
newFlattenMatrix[y*outCols+x] = matrix.At(y-w, x-w)
}
}
}