This commit is contained in:
Scott 2019-12-19 15:06:32 +10:30
parent 9fce612585
commit 669e72e86d
1 changed files with 24 additions and 21 deletions

View File

@ -1,13 +1,14 @@
/* /*
NAME DESCRIPTION
filter.go A filter that detects motion and discards frames without motion. The
filter uses a Mixture of Gaussians method (MoG) to determine what is
background and what is foreground.
AUTHORS AUTHORS
Ella Pietraroia <ella@ausocean.org>
Scott Barnard <scott@ausocean.org> Scott Barnard <scott@ausocean.org>
LICENSE LICENSE
filter.go is Copyright (C) 2019 the Australian Ocean Lab (AusOcean) mog.go is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the under the terms of the GNU General Public License as published by the
@ -23,7 +24,6 @@ LICENSE
in gpl.txt. If not, see http://www.gnu.org/licenses. in gpl.txt. If not, see http://www.gnu.org/licenses.
*/ */
// Package filter provides filters that modify video streams.
package filter package filter
import ( import (
@ -34,31 +34,33 @@ import (
"gocv.io/x/gocv" "gocv.io/x/gocv"
) )
// MogFilter is a filter that provides basic motion detection. // MOGFilter is a filter that provides basic motion detection. MoG is short for
type MogFilter struct { // Mixture of Gaussians method.
type MOGFilter struct {
dst io.WriteCloser dst io.WriteCloser
minArea float64 area float64
mog2 *gocv.BackgroundSubtractorMOG2 bs *gocv.BackgroundSubtractorMOG2
kernel gocv.Mat knl gocv.Mat
debug bool debug bool
windows []*gocv.Window windows []*gocv.Window
} }
// NewMogFilter returns a pointer to a new MogFilter // NewMOGFilter returns a pointer to a new MOGFilter.
func NewMogFilter(dst io.WriteCloser, minArea, threshold float64, history, kernelSize int, debug bool) *MogFilter { func NewMOGFilter(dst io.WriteCloser, area, threshold float64, history, kernelSize int, debug bool) *MogFilter {
mog2 := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false) bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false)
kernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(kernelSize, kernelSize)) k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(kernelSize, kernelSize))
var windows []*gocv.Window var windows []*gocv.Window
if debug { if debug {
windows = append(windows, gocv.NewWindow("Debug: Bounding boxes"), gocv.NewWindow("Debug: Motion")) windows = []*gocv.Window{gocv.NewWindow("Debug: Bounding boxes"), gocv.NewWindow("Debug: Motion")}
} }
return &MogFilter{dst, minArea, &mog2, kernel, debug, windows} return &MOGFilter{dst, area, &bs, k, debug, windows}
} }
// Implements io.Closer.
// Close frees resources used by gocv, because it has to be done manually, due to // Close frees resources used by gocv, because it has to be done manually, due to
// it using c-go. // it using c-go.
func (m *MogFilter) Close() error { func (m *MOGFilter) Close() error {
m.mog2.Close() m.bs.Close()
m.kernel.Close() m.kernel.Close()
for _, window := range m.windows { for _, window := range m.windows {
window.Close() window.Close()
@ -66,9 +68,10 @@ func (m *MogFilter) Close() error {
return m.dst.Close() return m.dst.Close()
} }
// Implements io.Writer.
// Write applies the motion filter to the video stream. Only frames with motion // Write applies the motion filter to the video stream. Only frames with motion
// are written to the destination encoder, frames without are discarded. // are written to the destination encoder, frames without are discarded.
func (m *MogFilter) Write(f []byte) (int, error) { func (m *MOGFilter) Write(f []byte) (int, error) {
img, _ := gocv.IMDecode(f, gocv.IMReadColor) img, _ := gocv.IMDecode(f, gocv.IMReadColor)
defer img.Close() defer img.Close()
@ -76,7 +79,7 @@ func (m *MogFilter) Write(f []byte) (int, error) {
defer imgDelta.Close() defer imgDelta.Close()
// Seperate foreground and background. // Seperate foreground and background.
m.mog2.Apply(img, &imgDelta) m.bs.Apply(img, &imgDelta)
// Threshold imgDelta. // Threshold imgDelta.
gocv.Threshold(imgDelta, &imgDelta, 25, 255, gocv.ThresholdBinary) gocv.Threshold(imgDelta, &imgDelta, 25, 255, gocv.ThresholdBinary)
@ -93,7 +96,7 @@ func (m *MogFilter) Write(f []byte) (int, error) {
var contours [][]image.Point var contours [][]image.Point
allContours := gocv.FindContours(imgDelta, gocv.RetrievalExternal, gocv.ChainApproxSimple) allContours := gocv.FindContours(imgDelta, gocv.RetrievalExternal, gocv.ChainApproxSimple)
for _, c := range allContours { for _, c := range allContours {
if gocv.ContourArea(c) > m.minArea { if gocv.ContourArea(c) > m.area {
contours = append(contours, c) contours = append(contours, c)
} }
} }