av/filter/knn.go

155 lines
4.9 KiB
Go
Raw Permalink Normal View History

// +build !circleci
2019-12-31 08:10:47 +03:00
/*
2019-12-23 07:29:48 +03:00
DESCRIPTION
A filter that detects motion and discards frames without motion. The
2019-12-31 06:07:47 +03:00
filter uses a K-Nearest Neighbours (KNN) to determine what is
2019-12-23 07:29:48 +03:00
background and what is foreground.
AUTHORS
2019-12-31 06:07:47 +03:00
Ella Pietraroia <ella@ausocean.org>
2019-12-23 07:29:48 +03:00
LICENSE
KNN.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
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 (
2019-12-31 07:59:58 +03:00
"fmt"
2019-12-23 07:29:48 +03:00
"image"
"image/color"
"io"
"gocv.io/x/gocv"
)
2020-02-03 09:09:25 +03:00
// KNN is a filter that provides basic motion detection. KNN is short for
2019-12-31 06:07:47 +03:00
// K-Nearest Neighbours method.
type KNN struct {
dst io.WriteCloser // Destination to which motion containing frames go.
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.
knl gocv.Mat // Matrix that is used for calculations.
debug bool // If true then debug windows with the bounding boxes and difference will be shown on the screen.
windows []*gocv.Window // Holds debug windows.
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.
hfCount int // Counter for the hold array.
scale float64 // The factor that frames will be downscaled by for motion detection.
2019-12-23 07:29:48 +03:00
}
2020-02-03 09:14:38 +03:00
// NewKNN returns a pointer to a new KNN filter struct.
func NewKNN(dst io.WriteCloser, area, threshold float64, history, kernelSize int, debug bool, hf int, scaleFactor int) *KNN {
2019-12-23 07:29:48 +03:00
bs := gocv.NewBackgroundSubtractorKNNWithParams(history, threshold, false)
k := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(kernelSize, kernelSize))
var windows []*gocv.Window
if debug {
windows = []*gocv.Window{gocv.NewWindow("KNN: Bounding boxes"), gocv.NewWindow("KNN: Motion")}
2019-12-23 07:29:48 +03:00
}
return &KNN{dst, area, &bs, k, debug, windows, make([][]byte, hf-1), hf, 0, 1 / float64(scaleFactor)}
2019-12-23 07:29:48 +03:00
}
// Implements io.Closer.
// Close frees resources used by gocv, because it has to be done manually, due to
// it using c-go.
func (m *KNN) Close() error {
2019-12-23 07:29:48 +03:00
m.bs.Close()
m.knl.Close()
for _, window := range m.windows {
window.Close()
}
return nil
}
// 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 *KNN) Write(f []byte) (int, error) {
if m.hfCount < (m.hf - 1) {
m.hold[m.hfCount] = f
m.hfCount++
return len(f), nil
}
m.hfCount = 0
2019-12-31 07:59:58 +03:00
img, err := gocv.IMDecode(f, gocv.IMReadColor)
if err != nil {
return 0, fmt.Errorf("can't decode image: %w", err)
}
2019-12-23 07:29:48 +03:00
defer img.Close()
imgDelta := gocv.NewMat()
defer imgDelta.Close()
// Downsize image to speed up calculations.
gocv.Resize(img, &img, image.Point{}, m.scale, m.scale, gocv.InterpolationNearestNeighbor)
2019-12-23 07:29:48 +03:00
// Seperate foreground and background.
m.bs.Apply(img, &imgDelta)
// Threshold imgDelta.
gocv.Threshold(imgDelta, &imgDelta, 25, 255, gocv.ThresholdBinary)
// Remove noise.
gocv.Erode(imgDelta, &imgDelta, m.knl)
gocv.Dilate(imgDelta, &imgDelta, m.knl)
// Fill small holes.
gocv.Dilate(imgDelta, &imgDelta, m.knl)
gocv.Erode(imgDelta, &imgDelta, m.knl)
// Find contours and reject ones with a small area.
var contours [][]image.Point
allContours := gocv.FindContours(imgDelta, gocv.RetrievalExternal, gocv.ChainApproxSimple)
for _, c := range allContours {
if gocv.ContourArea(c) > m.area {
contours = append(contours, c)
}
}
// Draw debug information.
if m.debug {
for _, c := range contours {
rect := gocv.BoundingRect(c)
gocv.Rectangle(&img, rect, color.RGBA{0, 0, 255, 0}, 1)
}
if len(contours) > 0 {
gocv.PutText(&img, "Motion", image.Pt(32, 32), gocv.FontHersheyPlain, 2.0, color.RGBA{255, 0, 0, 0}, 2)
}
m.windows[0].IMShow(img)
m.windows[1].IMShow(imgDelta)
m.windows[0].WaitKey(1)
}
// Don't write to destination if there is no motion.
if len(contours) == 0 {
return len(f), nil
2019-12-23 07:29:48 +03:00
}
// Write to destination, past 4 frames then current frame.
for i, h := range m.hold {
_, err := m.dst.Write(h)
m.hold[i] = nil
if err != nil {
return len(f), fmt.Errorf("could not write previous frames: %w", err)
}
}
2019-12-23 07:29:48 +03:00
return m.dst.Write(f)
}