2022-01-05 06:44:22 +03:00
|
|
|
//go:build !nocv
|
2021-05-17 04:21:50 +03:00
|
|
|
// +build !nocv
|
2019-12-31 04:56:35 +03:00
|
|
|
|
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
|
2020-02-12 05:40:24 +03:00
|
|
|
algorithm uses a Mixture of Gaussians method (MoG) to determine what is
|
2019-12-19 07:36:32 +03:00
|
|
|
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 (
|
|
|
|
"image"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"gocv.io/x/gocv"
|
2021-05-17 04:16:53 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/revid/config"
|
2019-12-16 07:27:25 +03:00
|
|
|
)
|
|
|
|
|
2020-02-06 02:12:12 +03:00
|
|
|
const (
|
2020-02-12 05:40:24 +03:00
|
|
|
defaultMOGMinArea = 25.0
|
|
|
|
defaultMOGThreshold = 20.0
|
|
|
|
defaultMOGHistory = 500
|
|
|
|
defaultMOGKernel = 3
|
2020-02-06 02:12:12 +03:00
|
|
|
)
|
|
|
|
|
2020-02-12 05:40:24 +03:00
|
|
|
// NewMOG returns a pointer to a new MOG motion filter.
|
|
|
|
func NewMOG(dst io.WriteCloser, c config.Config) *Motion {
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-02-06 02:12:12 +03:00
|
|
|
// Validate parameters.
|
|
|
|
if c.MotionMinArea <= 0 {
|
|
|
|
c.LogInvalidField("MotionMinArea", defaultMOGMinArea)
|
|
|
|
c.MotionMinArea = defaultMOGMinArea
|
|
|
|
}
|
|
|
|
if c.MotionThreshold <= 0 {
|
|
|
|
c.LogInvalidField("MotionThreshold", defaultMOGThreshold)
|
|
|
|
c.MotionThreshold = defaultMOGThreshold
|
|
|
|
}
|
|
|
|
if c.MotionHistory == 0 {
|
|
|
|
c.LogInvalidField("MotionHistory", defaultMOGHistory)
|
|
|
|
c.MotionHistory = defaultMOGHistory
|
|
|
|
}
|
|
|
|
if c.MotionKernel <= 0 {
|
|
|
|
c.LogInvalidField("MotionKernel", defaultMOGKernel)
|
|
|
|
c.MotionKernel = defaultMOGKernel
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := gocv.NewBackgroundSubtractorMOG2WithParams(int(c.MotionHistory), c.MotionThreshold, false)
|
2020-02-12 05:40:24 +03:00
|
|
|
alg := &MOG{
|
2020-02-06 02:12:12 +03:00
|
|
|
area: c.MotionMinArea,
|
2020-02-05 09:06:09 +03:00
|
|
|
bs: &bs,
|
2020-02-12 05:40:24 +03:00
|
|
|
knl: gocv.GetStructuringElement(gocv.MorphRect, image.Pt(3, 3)),
|
2020-02-05 09:06:09 +03:00
|
|
|
debugging: newWindows("MOG"),
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
2020-02-12 05:40:24 +03:00
|
|
|
|
|
|
|
return NewMotion(dst, alg, c)
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
|
|
|
|
2020-02-12 05:40:24 +03:00
|
|
|
// MOG is a motion detection algorithm. MoG is short for
|
|
|
|
// Mixture of Gaussians method.
|
|
|
|
type MOG struct {
|
|
|
|
debugging debugWindows
|
|
|
|
area float64 // The minimum area that a contour can be found in.
|
|
|
|
bs *gocv.BackgroundSubtractorMOG2 // Uses the MOG algorithm to find the difference between the current and background frame.
|
|
|
|
knl gocv.Mat // Matrix that is used for calculations.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close frees resources used by gocv. It has to be done manually,
|
|
|
|
// due to gocv using c-go.
|
2020-02-03 02:44:33 +03:00
|
|
|
func (m *MOG) Close() error {
|
2019-12-19 07:36:32 +03:00
|
|
|
m.bs.Close()
|
2019-12-19 09:21:49 +03:00
|
|
|
m.knl.Close()
|
2020-02-05 09:06:09 +03:00
|
|
|
m.debugging.close()
|
2019-12-20 08:07:49 +03:00
|
|
|
return nil
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
|
|
|
|
2020-02-12 05:40:24 +03:00
|
|
|
// Detect performs the motion detection on a frame. It returns true
|
|
|
|
// if motion is detected.
|
|
|
|
func (m *MOG) Detect(img *gocv.Mat) bool {
|
2020-01-08 04:01:09 +03:00
|
|
|
imgDelta := gocv.NewMat()
|
|
|
|
defer imgDelta.Close()
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-08 04:01:09 +03:00
|
|
|
// Seperate foreground and background.
|
2020-02-12 05:40:24 +03:00
|
|
|
m.bs.Apply(*img, &imgDelta)
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-08 04:01:09 +03:00
|
|
|
// Threshold imgDelta.
|
|
|
|
gocv.Threshold(imgDelta, &imgDelta, 25, 255, gocv.ThresholdBinary)
|
2019-12-16 07:27:25 +03:00
|
|
|
|
2020-01-08 04:01:09 +03:00
|
|
|
// Remove noise.
|
|
|
|
gocv.Erode(imgDelta, &imgDelta, m.knl)
|
|
|
|
gocv.Dilate(imgDelta, &imgDelta, m.knl)
|
2020-01-02 06:07:06 +03:00
|
|
|
|
2020-01-08 04:01:09 +03:00
|
|
|
// Fill small holes.
|
|
|
|
gocv.Dilate(imgDelta, &imgDelta, m.knl)
|
|
|
|
gocv.Erode(imgDelta, &imgDelta, m.knl)
|
2020-01-02 06:07:06 +03:00
|
|
|
|
2020-01-08 04:01:09 +03:00
|
|
|
// Find contours and reject ones with a small area.
|
|
|
|
var contours [][]image.Point
|
|
|
|
allContours := gocv.FindContours(imgDelta, gocv.RetrievalExternal, gocv.ChainApproxSimple)
|
2022-01-05 06:44:22 +03:00
|
|
|
|
|
|
|
for i := 0; i < allContours.Size(); i++ {
|
|
|
|
if gocv.ContourArea(allContours.At(i)) > m.area {
|
|
|
|
contours = append(contours, allContours.At(i).ToPoints())
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|
2020-01-08 04:01:09 +03:00
|
|
|
}
|
2019-12-19 07:43:53 +03:00
|
|
|
|
2020-01-08 04:01:09 +03:00
|
|
|
// Draw debug information.
|
2020-02-12 05:40:24 +03:00
|
|
|
m.debugging.show(*img, imgDelta, len(contours) > 0, &contours)
|
2020-01-20 07:01:40 +03:00
|
|
|
|
2020-02-12 05:40:24 +03:00
|
|
|
// Return if there is motion.
|
|
|
|
return len(contours) > 0
|
2019-12-16 07:27:25 +03:00
|
|
|
}
|