filter: changed embedded struct to a field within struct

This commit is contained in:
Scott 2020-02-05 16:36:09 +10:30
parent 86ec511697
commit 6bad1706ac
8 changed files with 112 additions and 108 deletions

View File

@ -40,34 +40,30 @@ type pixel struct{ r, g, b uint32 }
// Basic is a filter that provides basic motion detection via a difference // Basic is a filter that provides basic motion detection via a difference
// method. // method.
type Basic struct { type Basic struct {
debugFile debugging debugFile
dst io.WriteCloser dst io.WriteCloser
img image.Image img image.Image
bg [][]pixel bg [][]pixel
thresh int thresh int
pix int pix int
w int w int
h int h int
motion int motion int
} }
// NewBasic returns a pointer to a new Basic filter struct. // NewBasic returns a pointer to a new Basic filter struct.
func NewBasic(dst io.WriteCloser, t, p int) *Basic { func NewBasic(dst io.WriteCloser, t, p int) *Basic {
bf := &Basic{ return &Basic{
dst: dst, dst: dst,
thresh: t, thresh: t,
pix: p, pix: p,
debugging: newDebugFile(),
} }
err := bf.newDebugFile()
if err != nil {
panic(fmt.Sprintf("could not create debug file: %w", err))
}
return bf
} }
// Implements io.Closer. // Implements io.Closer.
func (bf *Basic) Close() error { func (bf *Basic) Close() error {
return bf.closeDebugFile() return bf.debugging.close()
} }
// Implements io.Writer. // Implements io.Writer.
@ -87,7 +83,7 @@ func (bf *Basic) Write(f []byte) (int, error) {
bf.w = bounds.Max.X bf.w = bounds.Max.X
bf.h = bounds.Max.Y bf.h = bounds.Max.Y
bf.resetDebugFileFrame(bf.w, bf.h) bf.debugging.resetFrame(bf.w, bf.h)
bf.bg = make([][]pixel, bf.h) bf.bg = make([][]pixel, bf.h)
for j, _ := range bf.bg { for j, _ := range bf.bg {
@ -119,7 +115,7 @@ func (bf *Basic) Write(f []byte) (int, error) {
} }
// Will save a video of where motion is detected in motion.mjpeg (in the current folder). // Will save a video of where motion is detected in motion.mjpeg (in the current folder).
err = bf.saveFrame(bf.motion, bf.pix) err = bf.debugging.saveFrame(bf.motion, bf.pix)
if err != nil { if err != nil {
return len(f), fmt.Errorf("image can't be encoded for debug video: %w", err) return len(f), fmt.Errorf("image can't be encoded for debug video: %w", err)
} }
@ -148,9 +144,9 @@ func (bf *Basic) process(j int, wg *sync.WaitGroup) {
if diff > bf.thresh { if diff > bf.thresh {
bf.motion++ bf.motion++
bf.drawToDebugFile(i, j, 0xff) bf.debugging.draw(i, j, 0xff)
} else { } else {
bf.drawToDebugFile(i, j, 0x00) bf.debugging.draw(i, j, 0x00)
} }
// Update backgound image. // Update backgound image.

View File

@ -47,16 +47,22 @@ type debugFile struct {
} }
// newDebugFile creates a file for saving debugging frames to. // newDebugFile creates a file for saving debugging frames to.
func (d *debugFile) newDebugFile() error { func newDebugFile() debugFile {
const debugfile = "motion.mjpeg" const debugfile = "motion.mjpeg"
d.bwImg = image.NewRGBA(image.Rect(0, 0, 0, 0))
var err error file, err := os.Create(debugfile)
d.file, err = os.Create(debugfile) if err != nil {
return err panic(fmt.Sprintf("could not create debug file: %w", err))
}
return debugFile{
bwImg: image.NewRGBA(image.Rect(0, 0, 0, 0)),
file: file,
}
} }
// closeDebugFile closes files used for debugging purposes. // close closes files used for debugging purposes.
func (d *debugFile) closeDebugFile() error { func (d *debugFile) close() error {
err := d.file.Close() err := d.file.Close()
if err != nil { if err != nil {
return fmt.Errorf("file cannot be closed: %w", err) return fmt.Errorf("file cannot be closed: %w", err)
@ -64,8 +70,8 @@ func (d *debugFile) closeDebugFile() error {
return nil return nil
} }
// drawToDebugFile draws debugging information to a frame. // draw draws debugging information to a frame.
func (d *debugFile) drawToDebugFile(i, j int, val uint8) { func (d *debugFile) draw(i, j int, val uint8) {
d.bwImg.SetRGBA(i, j, color.RGBA{val, val, val, 0xff}) d.bwImg.SetRGBA(i, j, color.RGBA{val, val, val, 0xff})
} }
@ -90,7 +96,7 @@ func (d *debugFile) saveFrame(motion, pix int) error {
return jpeg.Encode(d.file, d.bwImg, nil) return jpeg.Encode(d.file, d.bwImg, nil)
} }
// resetDebugFileFrame makes an image of given dimensions. // resetFrame makes an image of given dimensions.
func (d *debugFile) resetDebugFileFrame(w, h int) { func (d *debugFile) resetFrame(w, h int) {
d.bwImg = image.NewRGBA(image.Rect(0, 0, w, h)) d.bwImg = image.NewRGBA(image.Rect(0, 0, w, h))
} }

View File

@ -39,20 +39,25 @@ type debugWindows struct {
windows []*gocv.Window windows []*gocv.Window
} }
// closeWindows frees resources used by gocv. // close frees resources used by gocv.
func (d *debugWindows) closeWindows() { func (d *debugWindows) close() {
for _, window := range d.windows { for _, window := range d.windows {
window.Close() window.Close()
} }
} }
// newWindows creates debugging windows for the motion filter. // newWindows creates debugging windows for the motion filter.
func (d *debugWindows) newWindows(name string) { func newWindows(name string) debugWindows {
d.windows = []*gocv.Window{gocv.NewWindow(name + ": Bounding boxes"), gocv.NewWindow(name + ": Motion")} return debugWindows{
windows: []*gocv.Window{
gocv.NewWindow(name + ": Bounding boxes"),
gocv.NewWindow(name + ": Motion"),
},
}
} }
// showDebug displays debug information for the motion filters. // show displays debug information for the motion filters.
func (d *debugWindows) showDebug(img, imgDelta gocv.Mat, motion bool, contours ...[][]image.Point) { func (d *debugWindows) show(img, imgDelta gocv.Mat, motion bool, contours ...[][]image.Point) {
if len(contours) > 0 { if len(contours) > 0 {
for _, c := range contours[0] { for _, c := range contours[0] {
rect := gocv.BoundingRect(c) rect := gocv.BoundingRect(c)

View File

@ -39,21 +39,20 @@ import (
// 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 Difference struct {
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. // NewDifference returns a pointer to a new Difference struct.
func NewDifference(dst io.WriteCloser, threshold float64) *Difference { func NewDifference(dst io.WriteCloser, threshold float64) *Difference {
d := &Difference{ return &Difference{
dst: dst, dst: dst,
thresh: threshold, thresh: threshold,
prev: gocv.NewMat(), prev: gocv.NewMat(),
debugging: newWindows("DIFF"),
} }
d.newWindows("DIFF")
return d
} }
// Implements io.Closer. // Implements io.Closer.
@ -61,7 +60,7 @@ func NewDifference(dst io.WriteCloser, threshold float64) *Difference {
// it using c-go. // it using c-go.
func (d *Difference) Close() error { func (d *Difference) Close() error {
d.prev.Close() d.prev.Close()
d.closeWindows() d.debugging.close()
return nil return nil
} }
@ -97,7 +96,7 @@ func (d *Difference) Write(f []byte) (int, error) {
d.prev = img.Clone() d.prev = img.Clone()
// Draw debug information. // Draw debug information.
d.showDebug(img, imgDelta, mean > d.thresh) d.debugging.show(img, imgDelta, mean > d.thresh)
// Don't write to destination if there is no motion. // Don't write to destination if there is no motion.
if mean < d.thresh { if mean < d.thresh {

View File

@ -39,32 +39,31 @@ import (
// KNN is a filter that provides basic motion detection. KNN is short for // KNN is a filter that provides basic motion detection. KNN is short for
// K-Nearest Neighbours method. // K-Nearest Neighbours method.
type KNN struct { type KNN struct {
debugWindows debugging debugWindows
dst io.WriteCloser // Destination to which motion containing frames go. dst io.WriteCloser // Destination to which motion containing frames go.
area float64 // The minimum area that a contour can be found in. area float64 // The minimum area that a contour can be found in.
bs *gocv.BackgroundSubtractorKNN // Uses the KNN algorithm to find the difference between the current and background frame. bs *gocv.BackgroundSubtractorKNN // Uses the KNN algorithm to find the difference between the current and background frame.
knl gocv.Mat // Matrix that is used for calculations. knl gocv.Mat // Matrix that is used for calculations.
hold [][]byte // Will hold all frames up to hf (so only every hf frame is motion detected). hold [][]byte // Will hold all frames up to hf (so only every hf frame is motion detected).
hf int // The number of frames to be held. hf int // The number of frames to be held.
hfCount int // Counter for the hold array. hfCount int // Counter for the hold array.
scale float64 // The factor that frames will be downscaled by for motion detection. scale float64 // The factor that frames will be downscaled by for motion detection.
} }
// NewKNN returns a pointer to a new KNN filter struct. // NewKNN returns a pointer to a new KNN filter struct.
func NewKNN(dst io.WriteCloser, area, threshold float64, history, kernelSize int, hf int, scaleFactor int) *KNN { func NewKNN(dst io.WriteCloser, area, threshold float64, history, kernelSize int, hf int, scaleFactor int) *KNN {
bs := gocv.NewBackgroundSubtractorKNNWithParams(history, threshold, false) bs := gocv.NewBackgroundSubtractorKNNWithParams(history, threshold, false)
k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(kernelSize, kernelSize)) k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(kernelSize, kernelSize))
m := &KNN{ return &KNN{
dst: dst, dst: dst,
area: area, area: area,
bs: &bs, bs: &bs,
knl: k, knl: k,
hold: make([][]byte, hf-1), hold: make([][]byte, hf-1),
hf: hf, hf: hf,
scale: 1 / float64(scaleFactor), scale: 1 / float64(scaleFactor),
debugging: newWindows("KNN"),
} }
m.newWindows("KNN")
return m
} }
// Implements io.Closer. // Implements io.Closer.
@ -73,7 +72,7 @@ func NewKNN(dst io.WriteCloser, area, threshold float64, history, kernelSize int
func (m *KNN) Close() error { func (m *KNN) Close() error {
m.bs.Close() m.bs.Close()
m.knl.Close() m.knl.Close()
m.closeWindows() m.debugging.close()
return nil return nil
} }
@ -124,7 +123,7 @@ func (m *KNN) Write(f []byte) (int, error) {
} }
// Draw debug information. // Draw debug information.
m.showDebug(img, imgDelta, len(contours) > 0, contours) m.debugging.show(img, imgDelta, len(contours) > 0, contours)
// Don't write to destination if there is no motion. // Don't write to destination if there is no motion.
if len(contours) == 0 { if len(contours) == 0 {

View File

@ -39,32 +39,31 @@ import (
// MOG is a filter that provides basic motion detection. MoG is short for // MOG is a filter that provides basic motion detection. MoG is short for
// Mixture of Gaussians method. // Mixture of Gaussians method.
type MOG struct { type MOG struct {
debugWindows debugging debugWindows
dst io.WriteCloser // Destination to which motion containing frames go. dst io.WriteCloser // Destination to which motion containing frames go.
area float64 // The minimum area that a contour can be found in. 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. 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. knl gocv.Mat // Matrix that is used for calculations.
hold [][]byte // Will hold all frames up to hf (so only every hf frame is motion detected). hold [][]byte // Will hold all frames up to hf (so only every hf frame is motion detected).
hf int // The number of frames to be held. hf int // The number of frames to be held.
hfCount int // Counter for the hold array. hfCount int // Counter for the hold array.
scale float64 // The factor that frames will be downscaled by for motion detection. scale float64 // The factor that frames will be downscaled by for motion detection.
} }
// NewMOG returns a pointer to a new MOG filter struct. // NewMOG returns a pointer to a new MOG filter struct.
func NewMOG(dst io.WriteCloser, area, threshold float64, history int, hf int, scaleFactor int) *MOG { func NewMOG(dst io.WriteCloser, area, threshold float64, history int, hf int, scaleFactor int) *MOG {
bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false) bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false)
k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(3, 3)) k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(3, 3))
m := &MOG{ return &MOG{
dst: dst, dst: dst,
area: area, area: area,
bs: &bs, bs: &bs,
knl: k, knl: k,
hold: make([][]byte, hf-1), hold: make([][]byte, hf-1),
hf: hf, hf: hf,
scale: 1 / float64(scaleFactor), scale: 1 / float64(scaleFactor),
debugging: newWindows("MOG"),
} }
m.newWindows("MOG")
return m
} }
// Implements io.Closer. // Implements io.Closer.
@ -73,7 +72,7 @@ func NewMOG(dst io.WriteCloser, area, threshold float64, history int, hf int, sc
func (m *MOG) Close() error { func (m *MOG) Close() error {
m.bs.Close() m.bs.Close()
m.knl.Close() m.knl.Close()
m.closeWindows() m.debugging.close()
return nil return nil
} }
@ -124,7 +123,7 @@ func (m *MOG) Write(f []byte) (int, error) {
} }
// Draw debug information. // Draw debug information.
m.showDebug(img, imgDelta, len(contours) > 0, contours) m.debugging.show(img, imgDelta, len(contours) > 0, contours)
// Don't write to destination if there is no motion. // Don't write to destination if there is no motion.
if len(contours) == 0 { if len(contours) == 0 {

View File

@ -2,7 +2,7 @@
/* /*
DESCRIPTION DESCRIPTION
Displays debug information for the motion filters. Doesn't display debug information for the motion filters.
AUTHORS AUTHORS
Scott Barnard <scott@ausocean.org> Scott Barnard <scott@ausocean.org>
@ -30,16 +30,16 @@ package filter
type debugFile struct{} type debugFile struct{}
// newDebugFile creates a file for saving debugging frames to. // newDebugFile creates a file for saving debugging frames to.
func (d *debugFile) newDebugFile() error { return nil } func newDebugFile() debugFile { return debugFile{} }
// closeDebugFile closes files used for debugging purposes. // close closes files used for debugging purposes.
func (d *debugFile) closeDebugFile() error { return nil } func (d *debugFile) close() error { return nil }
// drawToDebugFile draws debugging information to a frame. // draw draws debugging information to a frame.
func (d *debugFile) drawToDebugFile(i, j int, val uint8) {} func (d *debugFile) draw(i, j int, val uint8) {}
// saveFrame writes a frame showing motion to a file. // saveFrame writes a frame showing motion to a file.
func (d *debugFile) saveFrame(motion, pix int) error { return nil } func (d *debugFile) saveFrame(motion, pix int) error { return nil }
// resetDebugFileFrame makes an image of given dimensions. // resetFrame makes an image of given dimensions.
func (d *debugFile) resetDebugFileFrame(w, h int) {} func (d *debugFile) resetFrame(w, h int) {}

View File

@ -3,7 +3,7 @@
/* /*
DESCRIPTION DESCRIPTION
Displays debug information for the motion filters. Doesn't display debug information for the motion filters.
AUTHORS AUTHORS
Scott Barnard <scott@ausocean.org> Scott Barnard <scott@ausocean.org>
@ -36,11 +36,11 @@ import (
// debugWindows is used for displaying debug information for the motion filters. // debugWindows is used for displaying debug information for the motion filters.
type debugWindows struct{} type debugWindows struct{}
// closeWindows frees resources used by gocv. // close frees resources used by gocv.
func (d *debugWindows) closeWindows() {} func (d *debugWindows) close() {}
// newWindows creates debugging windows for the motion filter. // newWindows creates debugging windows for the motion filter.
func (d *debugWindows) newWindows(name string) {} func newWindows(name string) debugWindows { return debugWindows{} }
// showDebug displays debug information for the motion filters. // show displays debug information for the motion filters.
func (d *debugWindows) showDebug(img, imgDelta gocv.Mat, motion bool, contours ...[][]image.Point) {} func (d *debugWindows) show(img, imgDelta gocv.Mat, motion bool, contours ...[][]image.Point) {}