2021-05-17 04:21:50 +03:00
|
|
|
// +build !nocv
|
2020-01-22 04:36:14 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
A filter that detects motion and discards frames without motion. The
|
|
|
|
algorithm calculates the absolute difference for each pixel between
|
|
|
|
two frames, then finds the mean. If the mean is above a given threshold,
|
|
|
|
then it is considered motion.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Scott Barnard <scott@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2020-02-12 05:40:24 +03:00
|
|
|
diff.go is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
2020-01-22 04:36:14 +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 (
|
2020-02-07 04:12:27 +03:00
|
|
|
"fmt"
|
2020-01-22 04:36:14 +03:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"gocv.io/x/gocv"
|
2021-05-17 04:16:53 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/revid/config"
|
2020-01-22 04:36:14 +03:00
|
|
|
)
|
|
|
|
|
2020-02-06 02:12:12 +03:00
|
|
|
const defaultDiffThreshold = 3
|
|
|
|
|
2020-02-12 05:40:24 +03:00
|
|
|
// NewDiff returns a pointer to a new difference motion filter.
|
|
|
|
func NewDiff(dst io.WriteCloser, c config.Config) *Motion {
|
2020-01-22 04:36:14 +03:00
|
|
|
|
2020-02-06 02:12:12 +03:00
|
|
|
// Validate parameters.
|
|
|
|
if c.MotionThreshold <= 0 {
|
|
|
|
c.LogInvalidField("MotionThreshold", defaultDiffThreshold)
|
|
|
|
c.MotionThreshold = defaultDiffThreshold
|
|
|
|
}
|
|
|
|
|
2020-02-12 05:40:24 +03:00
|
|
|
alg := &Diff{
|
2020-02-06 02:12:12 +03:00
|
|
|
thresh: c.MotionThreshold,
|
2020-02-05 09:06:09 +03:00
|
|
|
prev: gocv.NewMat(),
|
|
|
|
debugging: newWindows("DIFF"),
|
2020-01-22 04:36:14 +03:00
|
|
|
}
|
2020-02-12 05:40:24 +03:00
|
|
|
|
|
|
|
return NewMotion(dst, alg, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diff is a motion detection algorithm. It calculates the absolute
|
|
|
|
// difference for each pixel between two frames, then finds the mean.
|
|
|
|
// If the mean is above a given threshold, it is considered motion.
|
|
|
|
type Diff struct {
|
|
|
|
debugging debugWindows
|
|
|
|
thresh float64
|
|
|
|
prev gocv.Mat
|
2020-01-22 04:36:14 +03:00
|
|
|
}
|
|
|
|
|
2020-02-12 05:40:24 +03:00
|
|
|
// Close frees resources used by gocv. It has to be done manually,
|
|
|
|
// due to gocv using c-go.
|
2020-02-10 07:57:43 +03:00
|
|
|
func (d *Diff) Close() error {
|
2020-02-05 09:06:09 +03:00
|
|
|
d.debugging.close()
|
2020-02-12 05:40:24 +03:00
|
|
|
d.prev.Close()
|
2020-01-22 04:36:14 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-12 05:40:24 +03:00
|
|
|
// Detect performs the motion detection on a frame. It returns true
|
|
|
|
// if motion is detected.
|
|
|
|
func (d *Diff) Detect(img *gocv.Mat) bool {
|
2020-01-22 04:36:14 +03:00
|
|
|
if d.prev.Empty() {
|
2020-02-12 05:40:24 +03:00
|
|
|
d.prev = img.Clone()
|
|
|
|
return false
|
2020-01-24 08:40:08 +03:00
|
|
|
}
|
2020-01-22 04:36:14 +03:00
|
|
|
|
|
|
|
imgDelta := gocv.NewMat()
|
|
|
|
defer imgDelta.Close()
|
|
|
|
|
|
|
|
// Seperate foreground and background.
|
2020-02-12 05:40:24 +03:00
|
|
|
gocv.AbsDiff(*img, d.prev, &imgDelta)
|
2020-01-22 04:36:14 +03:00
|
|
|
gocv.CvtColor(imgDelta, &imgDelta, gocv.ColorBGRToGray)
|
|
|
|
|
|
|
|
mean := imgDelta.Mean().Val1
|
|
|
|
|
2020-01-27 03:53:56 +03:00
|
|
|
// Update History.
|
2020-01-22 04:36:14 +03:00
|
|
|
d.prev = img.Clone()
|
|
|
|
|
|
|
|
// Draw debug information.
|
2020-02-12 05:40:24 +03:00
|
|
|
d.debugging.show(*img, imgDelta, mean > d.thresh, nil, fmt.Sprintf("Mean: %f", mean), fmt.Sprintf("Threshold: %f", d.thresh))
|
2020-01-22 04:36:14 +03:00
|
|
|
|
2020-02-12 05:40:24 +03:00
|
|
|
// Return if there is motion.
|
|
|
|
return mean > d.thresh
|
2020-01-22 04:36:14 +03:00
|
|
|
}
|