filter: Difference → Diff

This commit is contained in:
Scott 2020-02-10 15:27:43 +10:30
parent a41033beb3
commit fa15d92388
4 changed files with 13 additions and 13 deletions

View File

@ -39,25 +39,25 @@ import (
const defaultDiffThreshold = 3 const defaultDiffThreshold = 3
// Difference is a filter that provides basic motion detection. Difference calculates // Diff is a filter that provides basic motion detection. Difference calculates
// the absolute difference for each pixel between two frames, then finds the mean. If // 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. // the mean is above a given threshold, then it is considered motion.
type Difference struct { type Diff struct {
debugging debugWindows debugging debugWindows
dst io.WriteCloser dst io.WriteCloser
thresh float64 thresh float64
prev gocv.Mat prev gocv.Mat
} }
// NewDifference returns a pointer to a new Difference struct. // NewDiff returns a pointer to a new Diff struct.
func NewDifference(dst io.WriteCloser, c config.Config) *Difference { func NewDiff(dst io.WriteCloser, c config.Config) *Diff {
// Validate parameters. // Validate parameters.
if c.MotionThreshold <= 0 { if c.MotionThreshold <= 0 {
c.LogInvalidField("MotionThreshold", defaultDiffThreshold) c.LogInvalidField("MotionThreshold", defaultDiffThreshold)
c.MotionThreshold = defaultDiffThreshold c.MotionThreshold = defaultDiffThreshold
} }
return &Difference{ return &Diff{
dst: dst, dst: dst,
thresh: c.MotionThreshold, thresh: c.MotionThreshold,
prev: gocv.NewMat(), prev: gocv.NewMat(),
@ -68,7 +68,7 @@ func NewDifference(dst io.WriteCloser, c config.Config) *Difference {
// Implements io.Closer. // Implements io.Closer.
// Close frees resources used by gocv, because it has to be done manually, due to // Close frees resources used by gocv, because it has to be done manually, due to
// it using c-go. // it using c-go.
func (d *Difference) Close() error { func (d *Diff) Close() error {
d.prev.Close() d.prev.Close()
d.debugging.close() d.debugging.close()
return nil return nil
@ -77,7 +77,7 @@ func (d *Difference) Close() error {
// Implements io.Writer. // Implements io.Writer.
// Write applies the motion filter to the video stream. Only frames with motion // Write applies the motion filter to the video stream. Only frames with motion
// are written to the destination encoder, frames without are discarded. // are written to the destination encoder, frames without are discarded.
func (d *Difference) Write(f []byte) (int, error) { func (d *Diff) Write(f []byte) (int, error) {
if d.prev.Empty() { if d.prev.Empty() {
var err error var err error
d.prev, err = gocv.IMDecode(f, gocv.IMReadColor) d.prev, err = gocv.IMDecode(f, gocv.IMReadColor)

View File

@ -44,8 +44,8 @@ func NewKNN(dst io.WriteCloser, c config.Config) *NoOp {
return &NoOp{dst: dst} return &NoOp{dst: dst}
} }
// NewDiffference returns a pointer to a new NoOp struct for testing purposes only. // NewDiff returns a pointer to a new NoOp struct for testing purposes only.
func NewDifference(dst io.WriteCloser, c config.Config) *NoOp { func NewDiff(dst io.WriteCloser, c config.Config) *NoOp {
return &NoOp{dst: dst} return &NoOp{dst: dst}
} }

View File

@ -113,7 +113,7 @@ const (
FilterMOG FilterMOG
FilterVariableFPS FilterVariableFPS
FilterKNN FilterKNN
FilterDifference FilterDiff
FilterBasic FilterBasic
) )

View File

@ -339,8 +339,8 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io.
r.filters[i] = filter.NewVariableFPS(dst, r.cfg.MinFPS, filter.NewMOG(dst, r.cfg)) r.filters[i] = filter.NewVariableFPS(dst, r.cfg.MinFPS, filter.NewMOG(dst, r.cfg))
case config.FilterKNN: case config.FilterKNN:
r.filters[i] = filter.NewKNN(dst, r.cfg) r.filters[i] = filter.NewKNN(dst, r.cfg)
case config.FilterDifference: case config.FilterDiff:
r.filters[i] = filter.NewDifference(dst, r.cfg) r.filters[i] = filter.NewDiff(dst, r.cfg)
case config.FilterBasic: case config.FilterBasic:
r.filters[i] = filter.NewBasic(dst, r.cfg) r.filters[i] = filter.NewBasic(dst, r.cfg)
default: default:
@ -676,7 +676,7 @@ func (r *Revid) Update(vars map[string]string) error {
} }
case "Filters": case "Filters":
filters := strings.Split(value, ",") filters := strings.Split(value, ",")
m := map[string]int{"NoOp": config.FilterNoOp, "MOG": config.FilterMOG, "VariableFPS": config.FilterVariableFPS, "KNN": config.FilterKNN, "Difference": config.FilterDifference, "Basic": config.FilterBasic} m := map[string]int{"NoOp": config.FilterNoOp, "MOG": config.FilterMOG, "VariableFPS": config.FilterVariableFPS, "KNN": config.FilterKNN, "Difference": config.FilterDiff, "Basic": config.FilterBasic}
r.cfg.Filters = make([]int, len(filters)) r.cfg.Filters = make([]int, len(filters))
for i, filter := range filters { for i, filter := range filters {
v, ok := m[filter] v, ok := m[filter]