-
Notifications
You must be signed in to change notification settings - Fork 11
/
input.go
40 lines (35 loc) · 986 Bytes
/
input.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
package gomxnet
import (
"fmt"
"image"
)
type ImageMean struct {
R, G, B float32
}
func InputFrom(imgs []image.Image, mean ImageMean) ([]float32, error) {
if len(imgs) == 0 {
return nil, fmt.Errorf("No image")
}
height := imgs[0].Bounds().Max.Y - imgs[0].Bounds().Min.Y
width := imgs[0].Bounds().Max.X - imgs[0].Bounds().Min.X
out := make([]float32, height*width*3*len(imgs))
for i := 0; i < len(imgs); i++ {
m := imgs[i]
bounds := m.Bounds()
h := bounds.Max.Y - bounds.Min.Y
w := bounds.Max.X - bounds.Min.X
if h != height || w != width {
return nil, fmt.Errorf("Size not matched")
}
start := width * height * 3 * i
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r, g, b, _ := m.At(x+bounds.Min.X, y+bounds.Min.Y).RGBA()
out[start+y*width+x] = float32(r>>8) - mean.R
out[start+width*height+y*width+x] = float32(g>>8) - mean.G
out[start+2*width*height+y*width+x] = float32(b>>8) - mean.B
}
}
}
return out, nil
}