iteration error fix

This commit is contained in:
Ella Pietraroia 2020-01-31 15:23:18 +10:30
parent 0189fad0e2
commit 2c947edc2b
3 changed files with 53 additions and 17 deletions

View File

@ -58,6 +58,8 @@ type Basic struct {
img image.Image img image.Image
bg [][]pixel bg [][]pixel
bwImg *image.RGBA bwImg *image.RGBA
thresh int
pix int
w int w int
h int h int
file io.WriteCloser file io.WriteCloser
@ -66,7 +68,7 @@ type Basic struct {
} }
// NewBasic returns a pointer to a new Basic filter struct. // NewBasic returns a pointer to a new Basic filter struct.
func NewBasic(dst io.WriteCloser, debug bool) *Basic { func NewBasic(dst io.WriteCloser, debug bool, t, p int) *Basic {
bwImg := image.NewRGBA(image.Rect(0, 0, 0, 0)) bwImg := image.NewRGBA(image.Rect(0, 0, 0, 0))
var file io.WriteCloser var file io.WriteCloser
var err error var err error
@ -77,7 +79,7 @@ func NewBasic(dst io.WriteCloser, debug bool) *Basic {
panic(fmt.Sprintf("could not create debug file: %v", err)) panic(fmt.Sprintf("could not create debug file: %v", err))
} }
} }
return &Basic{dst, nil, nil, bwImg, 0, 0, file, 0, debug} return &Basic{dst, nil, nil, bwImg, t, p, 0, 0, file, 0, debug}
} }
// Implements io.Closer. // Implements io.Closer.
@ -108,14 +110,14 @@ func (bf *Basic) Write(f []byte) (int, error) {
bf.bwImg = image.NewRGBA(image.Rect(0, 0, bf.w, bf.h)) bf.bwImg = image.NewRGBA(image.Rect(0, 0, bf.w, bf.h))
bf.bg = make([][]pixel, bf.h) bf.bg = make([][]pixel, bf.h)
for i, _ := range bf.bg { for j, _ := range bf.bg {
bf.bg[i] = make([]pixel, bf.w) bf.bg[j] = make([]pixel, bf.w)
for j, _ := range bf.bg[i] { for i, _ := range bf.bg[j] {
p := bf.img.At(i, j) p := bf.img.At(i, j)
r, g, b, _ := p.RGBA() r, g, b, _ := p.RGBA()
bf.bg[i][j].r = r bf.bg[j][i].r = r
bf.bg[i][j].b = b bf.bg[j][i].b = b
bf.bg[i][j].g = g bf.bg[j][i].g = g
} }
} }
return len(f), nil return len(f), nil
@ -124,14 +126,14 @@ func (bf *Basic) Write(f []byte) (int, error) {
// Use 4x goroutines to each process one row of pixels. // Use 4x goroutines to each process one row of pixels.
var j int var j int
j = 0 j = 0
var wg *sync.WaitGroup var wg sync.WaitGroup
for j < bf.h { for j < bf.h {
wg.Add(4) wg.Add(4)
go bf.process(j, wg) go bf.process(j, &wg)
go bf.process(j+1, wg) go bf.process(j+1, &wg)
go bf.process(j+2, wg) go bf.process(j+2, &wg)
go bf.process(j+3, wg) go bf.process(j+3, &wg)
j = j + 4 j = j + 4
wg.Wait() wg.Wait()
} }
@ -189,9 +191,9 @@ func (bf *Basic) process(j int, wg *sync.WaitGroup) {
} }
// Update backgound image. // Update backgound image.
bf.bg[i][j].r = r bf.bg[j][i].r = r
bf.bg[i][j].b = b bf.bg[j][i].b = b
bf.bg[i][j].g = g bf.bg[j][i].g = g
} }
wg.Done() wg.Done()
} }

View File

@ -106,6 +106,10 @@ const (
defaultMOGMinArea = 25.0 defaultMOGMinArea = 25.0
defaultMOGThreshold = 20.0 defaultMOGThreshold = 20.0
defaultMOGHistory = 500 defaultMOGHistory = 500
// Basic filter parameter defaults
defaultBasicThreshold = 45000
defaultBasicPixels = 1000
) )
// Quality represents video quality. // Quality represents video quality.
@ -301,6 +305,9 @@ type Config struct {
MOGThreshold float64 // Intensity value from the KNN motion detection algorithm that is considered motion. MOGThreshold float64 // Intensity value from the KNN motion detection algorithm that is considered motion.
MOGHistory uint // Length of MOG filter's history MOGHistory uint // Length of MOG filter's history
BasicThreshold int
BasicPixels int
// If true will restart reading of input after an io.EOF. // If true will restart reading of input after an io.EOF.
Loop bool Loop bool
@ -314,6 +321,8 @@ type Config struct {
// can be set over the web. It is a psuedo const. // can be set over the web. It is a psuedo const.
var TypeData = map[string]string{ var TypeData = map[string]string{
"AutoWhiteBalance": "enum:off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon", "AutoWhiteBalance": "enum:off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon",
"BasicPixels": "int",
"BasicThreshold": "int",
"BitDepth": "int", "BitDepth": "int",
"Brightness": "uint", "Brightness": "uint",
"BurstPeriod": "uint", "BurstPeriod": "uint",
@ -525,6 +534,16 @@ func (c *Config) Validate() error {
c.MOGHistory = defaultMOGHistory c.MOGHistory = defaultMOGHistory
} }
if c.BasicThreshold <= 0 {
c.logInvalidField("BasicThreshold", defaultBasicThreshold)
c.BasicThreshold = defaultBasicThreshold
}
if c.BasicPixels <= 0 {
c.logInvalidField("BasicPixels", defaultBasicPixels)
c.BasicPixels = defaultBasicPixels
}
if c.ShowWindows { if c.ShowWindows {
os, err := osName() os, err := osName()
if err != nil { if err != nil {

View File

@ -349,7 +349,7 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io.
case config.FilterDifference: case config.FilterDifference:
r.filters[i] = filter.NewDifference(dst, r.cfg.ShowWindows, r.cfg.DiffThreshold) r.filters[i] = filter.NewDifference(dst, r.cfg.ShowWindows, r.cfg.DiffThreshold)
case config.FilterBasic: case config.FilterBasic:
r.filters[i] = filter.NewBasic(dst, r.cfg.ShowWindows) r.filters[i] = filter.NewBasic(dst, r.cfg.ShowWindows, r.cfg.BasicThreshold, r.cfg.BasicPixels)
default: default:
panic("Undefined Filter") panic("Undefined Filter")
} }
@ -860,6 +860,21 @@ func (r *Revid) Update(vars map[string]string) error {
break break
} }
r.cfg.MOGHistory = uint(v) r.cfg.MOGHistory = uint(v)
case "BasicThreshold":
v, err := strconv.Atoi(value)
if err != nil {
r.cfg.Logger.Log(logger.Warning, pkg+"invalid BasicThreshold var", "value", value)
break
}
r.cfg.BasicThreshold = v
case "BasicPixels":
v, err := strconv.Atoi(value)
if err != nil {
r.cfg.Logger.Log(logger.Warning, pkg+"invalid BasicPixels var", "value", value)
break
}
r.cfg.BasicPixels = v
case "FileFPS": case "FileFPS":
v, err := strconv.Atoi(value) v, err := strconv.Atoi(value)
if err != nil { if err != nil {