From 669e72e86dff35fea8669f4adc5a8a9dc3fd7c17 Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 19 Dec 2019 15:06:32 +1030 Subject: [PATCH] PR fixes --- filter/{filter.go => mog.go} | 45 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 21 deletions(-) rename filter/{filter.go => mog.go} (67%) diff --git a/filter/filter.go b/filter/mog.go similarity index 67% rename from filter/filter.go rename to filter/mog.go index ed4f0d01..be0f20fb 100644 --- a/filter/filter.go +++ b/filter/mog.go @@ -1,13 +1,14 @@ /* -NAME - filter.go +DESCRIPTION + 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 - Ella Pietraroia Scott Barnard 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 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. */ -// Package filter provides filters that modify video streams. package filter import ( @@ -34,31 +34,33 @@ import ( "gocv.io/x/gocv" ) -// MogFilter is a filter that provides basic motion detection. -type MogFilter struct { +// MOGFilter is a filter that provides basic motion detection. MoG is short for +// Mixture of Gaussians method. +type MOGFilter struct { dst io.WriteCloser - minArea float64 - mog2 *gocv.BackgroundSubtractorMOG2 - kernel gocv.Mat + area float64 + bs *gocv.BackgroundSubtractorMOG2 + knl gocv.Mat debug bool windows []*gocv.Window } -// NewMogFilter returns a pointer to a new MogFilter -func NewMogFilter(dst io.WriteCloser, minArea, threshold float64, history, kernelSize int, debug bool) *MogFilter { - mog2 := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false) - kernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(kernelSize, kernelSize)) +// NewMOGFilter returns a pointer to a new MOGFilter. +func NewMOGFilter(dst io.WriteCloser, area, threshold float64, history, kernelSize int, debug bool) *MogFilter { + bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false) + k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(kernelSize, kernelSize)) var windows []*gocv.Window 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 // it using c-go. -func (m *MogFilter) Close() error { - m.mog2.Close() +func (m *MOGFilter) Close() error { + m.bs.Close() m.kernel.Close() for _, window := range m.windows { window.Close() @@ -66,9 +68,10 @@ func (m *MogFilter) Close() error { return m.dst.Close() } +// Implements io.Writer. // Write applies the motion filter to the video stream. Only frames with motion // 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) defer img.Close() @@ -76,7 +79,7 @@ func (m *MogFilter) Write(f []byte) (int, error) { defer imgDelta.Close() // Seperate foreground and background. - m.mog2.Apply(img, &imgDelta) + m.bs.Apply(img, &imgDelta) // Threshold imgDelta. gocv.Threshold(imgDelta, &imgDelta, 25, 255, gocv.ThresholdBinary) @@ -93,7 +96,7 @@ func (m *MogFilter) Write(f []byte) (int, error) { var contours [][]image.Point allContours := gocv.FindContours(imgDelta, gocv.RetrievalExternal, gocv.ChainApproxSimple) for _, c := range allContours { - if gocv.ContourArea(c) > m.minArea { + if gocv.ContourArea(c) > m.area { contours = append(contours, c) } }