av/filter/mog.go

128 lines
3.5 KiB
Go
Raw Normal View History

/*
2019-12-19 07:36:32 +03:00
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
Scott Barnard <scott@ausocean.org>
LICENSE
2019-12-19 07:36:32 +03:00
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
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
import (
"image"
"image/color"
"io"
"gocv.io/x/gocv"
)
2019-12-19 07:36:32 +03:00
// MOGFilter is a filter that provides basic motion detection. MoG is short for
// Mixture of Gaussians method.
type MOGFilter struct {
dst io.WriteCloser
2019-12-19 07:36:32 +03:00
area float64
bs *gocv.BackgroundSubtractorMOG2
knl gocv.Mat
debug bool
windows []*gocv.Window
}
2019-12-19 07:36:32 +03:00
// 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 {
2019-12-19 07:36:32 +03:00
windows = []*gocv.Window{gocv.NewWindow("Debug: Bounding boxes"), gocv.NewWindow("Debug: Motion")}
}
2019-12-19 07:36:32 +03:00
return &MOGFilter{dst, area, &bs, k, debug, windows}
}
2019-12-19 07:36:32 +03:00
// Implements io.Closer.
// Close frees resources used by gocv, because it has to be done manually, due to
// it using c-go.
2019-12-19 07:36:32 +03:00
func (m *MOGFilter) Close() error {
m.bs.Close()
m.kernel.Close()
for _, window := range m.windows {
window.Close()
}
return m.dst.Close()
}
2019-12-19 07:36:32 +03:00
// 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.
2019-12-19 07:36:32 +03:00
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.
2019-12-19 07:36:32 +03:00
m.bs.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 {
2019-12-19 07:36:32 +03:00
if gocv.ContourArea(c) > m.area {
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)
}
2019-12-19 07:43:53 +03:00
if len(contours) > 0 {
gocv.PutText(&img, "Motion", image.Pt(32, 32), gocv.FontHersheyPlain, 2.0, color.RGBA{255, 0, 0, 0}, 2)
}
2019-12-19 07:43:53 +03:00
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)
}