-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.go
81 lines (65 loc) · 1.57 KB
/
image.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
package io
import (
"image"
"io/ioutil"
"log"
"os"
_ "image/jpeg"
"github.com/cvley/gocaffe/blob"
"github.com/golang/protobuf/proto"
"github.com/nfnt/resize"
pb "github.com/cvley/gocaffe/proto"
)
type Image struct {
data []float32
channels int
width int
height int
}
func ReadImageFile(file string, width int, height int, mean string) (*blob.Blob, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
meanBlob, err := readMean(mean)
if err != nil {
return nil, err
}
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
m := resize.Resize(uint(meanBlob.Width()), uint(meanBlob.Height()), img, resize.Lanczos3)
// TODO hard code
//shape := []int64{1, 3, meanBlob.Height(), meanBlob.Width()}
shape := []int64{1, 3, int64(height), int64(width)}
datum, err := blob.New(shape)
if err != nil {
return nil, err
}
for x := 0; x < int(meanBlob.Width()); x++ {
for y := 0; y < int(meanBlob.Height()); y++ {
r, g, b, _ := m.At(x, y).RGBA()
datum.Set([]int{0, 0, y, x}, float64(b>>8)-meanBlob.Get([]int{0, 0, y, x}))
datum.Set([]int{0, 1, y, x}, float64(g>>8)-meanBlob.Get([]int{0, 1, y, x}))
datum.Set([]int{0, 2, y, x}, float64(r>>8)-meanBlob.Get([]int{0, 2, y, x}))
}
}
return datum, nil
}
func readMean(file string) (*blob.Blob, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
b := &pb.BlobProto{}
if err := proto.Unmarshal(buf, b); err != nil {
return nil, err
}
return blob.FromProto(b)
}