2019-12-31 04:56:35 +03:00
|
|
|
// +build !circleci
|
|
|
|
|
2019-12-16 07:27:25 +03:00
|
|
|
/*
|
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.
|
2019-12-16 07:27:25 +03:00
|
|
|
|
|
|
|
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)
|
2019-12-16 07:27:25 +03:00
|
|
|
|
|
|
|
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 (
|
2019-12-31 08:00:51 +03:00
|
|
|
"fmt"
|
2019-12-16 07:27:25 +03:00
|
|
|
"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 {
|
2019-12-16 07:27:25 +03:00
|
|
|
dst io.WriteCloser
|
2019-12-19 07:36:32 +03:00
|
|
|
area float64
|
|
|
|
bs *gocv.BackgroundSubtractorMOG2
|
|
|
|
knl gocv.Mat
|
2019-12-16 07:27:25 +03:00
|
|
|
debug bool
|
|
|
|
windows []*gocv.Window
|
2020-01-02 06:07:06 +03:00
|
|
|
hold [][]byte
|
|
|
|
hf int
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
var hfCount int = 0
|
|
|
|
|
2019-12-31 04:56:35 +03:00
|
|
|
// NewMOGFilter returns a pointer to a new MOGFilter struct.
|
2020-01-02 06:07:06 +03:00
|
|
|
func NewMOGFilter(dst io.WriteCloser, area, threshold float64, history int, debug bool, hf int) *MOGFilter {
|
2019-12-19 07:36:32 +03:00
|
|
|
bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false)
|
2019-12-31 07:34:19 +03:00
|
|
|
k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(3, 3))
|
2019-12-16 07:27:25 +03:00
|
|
|
var windows []*gocv.Window
|
|
|
|
if debug {
|
2020-01-02 08:16:32 +03:00
|
|
|
windows = []*gocv.Window{gocv.NewWindow("MOG: Bounding boxes"), gocv.NewWindow("MOG: Motion")}
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
2020-01-02 06:07:06 +03:00
|
|
|
hold := make([][]byte, hf-1)
|
|
|
|
return &MOGFilter{dst, area, &bs, k, debug, windows, hold, hf}
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 07:36:32 +03:00
|
|
|
// Implements io.Closer.
|
2019-12-16 07:27:25 +03:00
|
|
|
// 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()
|
2019-12-19 09:21:49 +03:00
|
|
|
m.knl.Close()
|
2019-12-16 07:27:25 +03:00
|
|
|
for _, window := range m.windows {
|
|
|
|
window.Close()
|
|
|
|
}
|
2019-12-20 08:07:49 +03:00
|
|
|
return nil
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 07:36:32 +03:00
|
|
|
// Implements io.Writer.
|
2019-12-16 07:27:25 +03:00
|
|
|
// 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) {
|
2020-01-02 06:07:06 +03:00
|
|
|
if hfCount < (m.hf - 1) {
|
|
|
|
m.hold[hfCount] = f
|
|
|
|
hfCount++
|
|
|
|
return -1, nil
|
|
|
|
} else {
|
|
|
|
img, err := gocv.IMDecode(f, gocv.IMReadColor)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("image can't be decoded: %w", err)
|
|
|
|
}
|
|
|
|
defer img.Close()
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
imgDelta := gocv.NewMat()
|
|
|
|
defer imgDelta.Close()
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
// Seperate foreground and background.
|
|
|
|
m.bs.Apply(img, &imgDelta)
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
// Threshold imgDelta.
|
|
|
|
gocv.Threshold(imgDelta, &imgDelta, 25, 255, gocv.ThresholdBinary)
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
// Remove noise.
|
|
|
|
gocv.Erode(imgDelta, &imgDelta, m.knl)
|
|
|
|
gocv.Dilate(imgDelta, &imgDelta, m.knl)
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
// Fill small holes.
|
|
|
|
gocv.Dilate(imgDelta, &imgDelta, m.knl)
|
|
|
|
gocv.Erode(imgDelta, &imgDelta, m.knl)
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
// 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.area {
|
|
|
|
contours = append(contours, c)
|
|
|
|
}
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
// 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)
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
2019-12-19 07:43:53 +03:00
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
// Don't write to destination if there is no motion.
|
|
|
|
if len(contours) == 0 {
|
|
|
|
return 0, nil
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
2019-12-19 07:43:53 +03:00
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
// Write to destination, past 4 frames then current frame.
|
|
|
|
for _, h := range m.hold {
|
|
|
|
_, err := m.dst.Write(h)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-02 06:07:06 +03:00
|
|
|
hfCount = 0
|
|
|
|
return m.dst.Write(f)
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|