-
Notifications
You must be signed in to change notification settings - Fork 3
/
lgn_dog.go
201 lines (163 loc) · 5.55 KB
/
lgn_dog.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
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"image"
"log"
"github.com/anthonynsimon/bild/transform"
"github.com/emer/etable/etable"
"github.com/emer/etable/etensor"
_ "github.com/emer/etable/etview" // include to get gui views
"github.com/emer/vision/dog"
"github.com/emer/vision/vfilter"
"github.com/goki/gi/gi"
"github.com/goki/gi/gimain"
"github.com/goki/gi/giv"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
)
// this is the stub main for gogi that calls our actual
// mainrun function, at end of file
func main() {
gimain.Main(func() {
mainrun()
})
}
// Vis encapsulates specific visual processing pipeline in
// use in a given case -- can add / modify this as needed
type Vis struct {
// name of image file to operate on
ImageFile gi.FileName `desc:"name of image file to operate on"`
// LGN DoG filter parameters
DoG dog.Filter `desc:"LGN DoG filter parameters"`
// [view: inline] geometry of input, output
Geom vfilter.Geom `inactive:"+" view:"inline" desc:"geometry of input, output"`
// target image size to use -- images will be rescaled to this size
ImgSize image.Point `desc:"target image size to use -- images will be rescaled to this size"`
// [view: no-inline] DoG filter tensor -- has 3 filters (on, off, net)
DoGTsr etensor.Float32 `view:"no-inline" desc:"DoG filter tensor -- has 3 filters (on, off, net)"`
// [view: no-inline] DoG filter table (view only)
DoGTab etable.Table `view:"no-inline" desc:"DoG filter table (view only)"`
// [view: -] current input image
Img image.Image `view:"-" desc:"current input image"`
// [view: no-inline] input image as tensor
ImgTsr etensor.Float32 `view:"no-inline" desc:"input image as tensor"`
// [view: no-inline] DoG filter output tensor
OutTsr etensor.Float32 `view:"no-inline" desc:"DoG filter output tensor"`
}
var KiT_Vis = kit.Types.AddType(&Vis{}, VisProps)
func (vi *Vis) Defaults() {
vi.ImageFile = gi.FileName("side-tee-128.png")
vi.DoG.Defaults()
sz := 12 // V1mF16 typically = 12, no border
spc := 4
vi.DoG.SetSize(sz, spc)
// note: first arg is border -- we are relying on Geom
// to set border to .5 * filter size
// any further border sizes on same image need to add Geom.FiltRt!
vi.Geom.Set(image.Point{0, 0}, image.Point{spc, spc}, image.Point{sz, sz})
vi.ImgSize = image.Point{128, 128}
// vi.ImgSize = image.Point{64, 64}
vi.DoG.ToTensor(&vi.DoGTsr)
vi.DoG.ToTable(&vi.DoGTab) // note: view only, testing
vi.DoGTab.Cols[1].SetMetaData("max", "0.2")
vi.DoGTab.Cols[1].SetMetaData("min", "-0.2")
}
// OpenImage opens given filename as current image Img
// and converts to a float32 tensor for processing
func (vi *Vis) OpenImage(filepath string) error {
var err error
vi.Img, err = gi.OpenImage(filepath)
if err != nil {
log.Println(err)
return err
}
isz := vi.Img.Bounds().Size()
if isz != vi.ImgSize {
vi.Img = transform.Resize(vi.Img, vi.ImgSize.X, vi.ImgSize.Y, transform.Linear)
}
vfilter.RGBToGrey(vi.Img, &vi.ImgTsr, vi.Geom.FiltRt.X, false) // pad for filt, bot zero
vfilter.WrapPad(&vi.ImgTsr, vi.Geom.FiltRt.X)
vi.ImgTsr.SetMetaData("image", "+")
return nil
}
// LGNDoG runs DoG filtering on input image
// must have valid Img in place to start.
func (vi *Vis) LGNDoG() {
flt := vi.DoG.FilterTensor(&vi.DoGTsr, dog.Net)
vfilter.Conv1(&vi.Geom, flt, &vi.ImgTsr, &vi.OutTsr, vi.DoG.Gain)
// log norm is generally good it seems for dogs
vfilter.TensorLogNorm32(&vi.OutTsr, 0) // 0 = renorm all, 1 = renorm within each on / off separately
}
// Filter is overall method to run filters on current image file name
// loads the image from ImageFile and then runs filters
func (vi *Vis) Filter() error {
err := vi.OpenImage(string(vi.ImageFile))
if err != nil {
log.Println(err)
return err
}
vi.LGNDoG()
return nil
}
////////////////////////////////////////////////////////////////////////////////////////////
// Gui
// ConfigGui configures the GoGi gui interface for this Vis
func (vi *Vis) ConfigGui() *gi.Window {
width := 1600
height := 1200
gi.SetAppName("v1gabor")
gi.SetAppAbout(`This demonstrates basic V1 Gabor Filtering. See <a href="https://github.com/emer/vision/v1">V1 on GitHub</a>.</p>`)
win := gi.NewMainWindow("v1gabor", "V1 Gabor Filtering", width, height)
// vi.Win = win
vp := win.WinViewport2D()
updt := vp.UpdateStart()
mfr := win.SetMainFrame()
tbar := gi.AddNewToolBar(mfr, "tbar")
tbar.SetStretchMaxWidth()
// vi.ToolBar = tbar
split := gi.AddNewSplitView(mfr, "split")
split.Dim = gi.X
split.SetStretchMax()
sv := giv.AddNewStructView(split, "sv")
sv.Viewport = vp
sv.SetStruct(vi)
split.SetSplits(1)
// main menu
appnm := gi.AppName()
mmen := win.MainMenu
mmen.ConfigMenus([]string{appnm, "File", "Edit", "Window"})
amen := win.MainMenu.ChildByName(appnm, 0).(*gi.Action)
amen.Menu.AddAppMenu(win)
emen := win.MainMenu.ChildByName("Edit", 1).(*gi.Action)
emen.Menu.AddCopyCutPaste(win)
gi.SetQuitReqFunc(func() {
gi.Quit()
})
win.SetCloseReqFunc(func(w *gi.Window) {
gi.Quit()
})
win.SetCloseCleanFunc(func(w *gi.Window) {
go gi.Quit() // once main window is closed, quit
})
vp.UpdateEndNoSig(updt)
win.MainMenuUpdated()
return win
}
// These props create interactive toolbar for GUI
var VisProps = ki.Props{
"ToolBar": ki.PropSlice{
{"Filter", ki.Props{
"desc": "run filter methods on current ImageFile image",
"icon": "updt",
}},
},
}
var TheVis Vis
func mainrun() {
TheVis.Defaults()
TheVis.Filter()
win := TheVis.ConfigGui()
win.StartEventLoop()
}