mirror of https://bitbucket.org/ausocean/av.git
PR fixes
This commit is contained in:
parent
9fce612585
commit
669e72e86d
|
@ -1,13 +1,14 @@
|
|||
/*
|
||||
NAME
|
||||
filter.go
|
||||
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
|
||||
Ella Pietraroia <ella@ausocean.org>
|
||||
Scott Barnard <scott@ausocean.org>
|
||||
|
||||
LICENSE
|
||||
filter.go is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
||||
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
|
||||
|
@ -23,7 +24,6 @@ LICENSE
|
|||
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
||||
*/
|
||||
|
||||
// Package filter provides filters that modify video streams.
|
||||
package filter
|
||||
|
||||
import (
|
||||
|
@ -34,31 +34,33 @@ import (
|
|||
"gocv.io/x/gocv"
|
||||
)
|
||||
|
||||
// MogFilter is a filter that provides basic motion detection.
|
||||
type MogFilter struct {
|
||||
// MOGFilter is a filter that provides basic motion detection. MoG is short for
|
||||
// Mixture of Gaussians method.
|
||||
type MOGFilter struct {
|
||||
dst io.WriteCloser
|
||||
minArea float64
|
||||
mog2 *gocv.BackgroundSubtractorMOG2
|
||||
kernel gocv.Mat
|
||||
area float64
|
||||
bs *gocv.BackgroundSubtractorMOG2
|
||||
knl gocv.Mat
|
||||
debug bool
|
||||
windows []*gocv.Window
|
||||
}
|
||||
|
||||
// NewMogFilter returns a pointer to a new MogFilter
|
||||
func NewMogFilter(dst io.WriteCloser, minArea, threshold float64, history, kernelSize int, debug bool) *MogFilter {
|
||||
mog2 := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false)
|
||||
kernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(kernelSize, kernelSize))
|
||||
// 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 {
|
||||
windows = append(windows, gocv.NewWindow("Debug: Bounding boxes"), gocv.NewWindow("Debug: Motion"))
|
||||
windows = []*gocv.Window{gocv.NewWindow("Debug: Bounding boxes"), gocv.NewWindow("Debug: Motion")}
|
||||
}
|
||||
return &MogFilter{dst, minArea, &mog2, kernel, debug, windows}
|
||||
return &MOGFilter{dst, area, &bs, k, debug, windows}
|
||||
}
|
||||
|
||||
// Implements io.Closer.
|
||||
// Close frees resources used by gocv, because it has to be done manually, due to
|
||||
// it using c-go.
|
||||
func (m *MogFilter) Close() error {
|
||||
m.mog2.Close()
|
||||
func (m *MOGFilter) Close() error {
|
||||
m.bs.Close()
|
||||
m.kernel.Close()
|
||||
for _, window := range m.windows {
|
||||
window.Close()
|
||||
|
@ -66,9 +68,10 @@ func (m *MogFilter) Close() error {
|
|||
return m.dst.Close()
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (m *MogFilter) Write(f []byte) (int, error) {
|
||||
func (m *MOGFilter) Write(f []byte) (int, error) {
|
||||
img, _ := gocv.IMDecode(f, gocv.IMReadColor)
|
||||
defer img.Close()
|
||||
|
||||
|
@ -76,7 +79,7 @@ func (m *MogFilter) Write(f []byte) (int, error) {
|
|||
defer imgDelta.Close()
|
||||
|
||||
// Seperate foreground and background.
|
||||
m.mog2.Apply(img, &imgDelta)
|
||||
m.bs.Apply(img, &imgDelta)
|
||||
|
||||
// Threshold imgDelta.
|
||||
gocv.Threshold(imgDelta, &imgDelta, 25, 255, gocv.ThresholdBinary)
|
||||
|
@ -93,7 +96,7 @@ func (m *MogFilter) Write(f []byte) (int, error) {
|
|||
var contours [][]image.Point
|
||||
allContours := gocv.FindContours(imgDelta, gocv.RetrievalExternal, gocv.ChainApproxSimple)
|
||||
for _, c := range allContours {
|
||||
if gocv.ContourArea(c) > m.minArea {
|
||||
if gocv.ContourArea(c) > m.area {
|
||||
contours = append(contours, c)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue