From 9c7de50b9d721d313655344d75760b26bb6b1147 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 16 Dec 2019 14:57:25 +1030 Subject: [PATCH 1/5] Created a motion detection filter to be used with the filter interface --- filter/filter.go | 128 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 filter/filter.go diff --git a/filter/filter.go b/filter/filter.go new file mode 100644 index 00000000..22672fed --- /dev/null +++ b/filter/filter.go @@ -0,0 +1,128 @@ +/* +NAME + filter.go + +AUTHORS + Ella Pietraroia + Scott Barnard + +LICENSE + filter.go is Copyright (C) 2017-2020 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 + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + It is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + in gpl.txt. If not, see http://www.gnu.org/licenses. +*/ + +// Package filter provides filters that modify video streams. +package filter + +import ( + "image" + "image/color" + "io" + + "gocv.io/x/gocv" +) + +// Filter is an interface shared by all filters. It is the same as an io.WriteCloser +// interface, however more functions may be added as needed. +type Filter interface { + io.WriteCloser +} + +// MogFilter is a filter that provides basic motion detection. +type MogFilter struct { + dst io.WriteCloser + minArea float64 + mog2 *gocv.BackgroundSubtractorMOG2 + kernel 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)) + var windows []*gocv.Window + if debug { + windows = append(windows, gocv.NewWindow("Debug: Bounding boxes"), gocv.NewWindow("Debug: Motion")) + } + return &MogFilter{dst, minArea, &mog2, kernel, debug, windows} +} + +// 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() + m.kernel.Close() + for _, window := range m.windows { + window.Close() + } + return m.dst.Close() +} + +// 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) { + img, _ := gocv.IMDecode(f, gocv.IMReadColor) + defer img.Close() + + imgDelta := gocv.NewMat() + defer imgDelta.Close() + + // Seperate foreground and background. + m.mog2.Apply(img, &imgDelta) + + // Threshold imgDelta. + gocv.Threshold(imgDelta, &imgDelta, 25, 255, gocv.ThresholdBinary) + + // Remove noise. + gocv.Erode(imgDelta, &imgDelta, m.kernel) + gocv.Dilate(imgDelta, &imgDelta, m.kernel) + + // Fill small holes. + gocv.Dilate(imgDelta, &imgDelta, m.kernel) + gocv.Erode(imgDelta, &imgDelta, m.kernel) + + // Find contours and reject ones with a small area. + var contours [][]image.Point + allContours := gocv.FindContours(imgDelta, gocv.RetrievalExternal, gocv.ChainApproxSimple) + for _, c := range allContours { + if gocv.ContourArea(c) > m.minArea { + contours = append(contours, c) + } + } + + // Draw debug information. + if m.debug { + for _, c := range contours { + rect := gocv.BoundingRect(c) + gocv.Rectangle(&img, rect, color.RGBA{0, 0, 255, 0}, 1) + } + if len(contours) > 0 { + gocv.PutText(&img, "Motion", image.Pt(32, 32), gocv.FontHersheyPlain, 2.0, color.RGBA{255, 0, 0, 0}, 2) + } + m.windows[0].IMShow(img) + m.windows[1].IMShow(imgDelta) + m.windows[0].WaitKey(1) + } + + // Don't write to destination if there is no motion. + if len(contours) == 0 { + return -1, nil + } + + // Write to destination. + return m.dst.Write(f) +} From 7e62e17a83281c56ad2753beb753010a6a03804c Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 19 Dec 2019 14:25:31 +1030 Subject: [PATCH 2/5] Cleaned up branch --- filter/filter.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/filter/filter.go b/filter/filter.go index 22672fed..05047e66 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -34,12 +34,6 @@ import ( "gocv.io/x/gocv" ) -// Filter is an interface shared by all filters. It is the same as an io.WriteCloser -// interface, however more functions may be added as needed. -type Filter interface { - io.WriteCloser -} - // MogFilter is a filter that provides basic motion detection. type MogFilter struct { dst io.WriteCloser From 9fce612585d41515b646f1a4eb9a4a3fcf8f1647 Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 19 Dec 2019 14:27:43 +1030 Subject: [PATCH 3/5] Fixed copyright --- filter/filter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filter/filter.go b/filter/filter.go index 05047e66..ed4f0d01 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -7,7 +7,7 @@ AUTHORS Scott Barnard LICENSE - filter.go is Copyright (C) 2017-2020 the Australian Ocean Lab (AusOcean) + filter.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 From 669e72e86dff35fea8669f4adc5a8a9dc3fd7c17 Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 19 Dec 2019 15:06:32 +1030 Subject: [PATCH 4/5] 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) } } From 8086cac80291f2dacbeffbdf2dfc0f056416a30e Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 19 Dec 2019 15:13:53 +1030 Subject: [PATCH 5/5] PR fixes --- filter/mog.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/filter/mog.go b/filter/mog.go index be0f20fb..7cfa8356 100644 --- a/filter/mog.go +++ b/filter/mog.go @@ -107,9 +107,11 @@ func (m *MOGFilter) Write(f []byte) (int, error) { rect := gocv.BoundingRect(c) gocv.Rectangle(&img, rect, color.RGBA{0, 0, 255, 0}, 1) } + if len(contours) > 0 { gocv.PutText(&img, "Motion", image.Pt(32, 32), gocv.FontHersheyPlain, 2.0, color.RGBA{255, 0, 0, 0}, 2) } + m.windows[0].IMShow(img) m.windows[1].IMShow(imgDelta) m.windows[0].WaitKey(1)