2020-01-22 09:02:47 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
A filter that detects motion and discards frames without motion. The
|
2020-01-31 05:25:49 +03:00
|
|
|
filter uses a difference method looking at each individual pixel to
|
2020-01-31 08:39:13 +03:00
|
|
|
determine what is background and what is foreground.
|
2020-01-22 09:02:47 +03:00
|
|
|
|
|
|
|
AUTHORS
|
2020-01-24 09:31:28 +03:00
|
|
|
Ella Pietraroia <ella@ausocean.org>
|
2020-01-22 09:02:47 +03:00
|
|
|
|
|
|
|
LICENSE
|
2020-01-31 05:25:49 +03:00
|
|
|
basic.go is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
2020-01-22 09:02:47 +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-01-24 09:31:28 +03:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
2020-02-07 04:12:27 +03:00
|
|
|
"image/color"
|
2020-01-24 09:31:28 +03:00
|
|
|
"image/jpeg"
|
2020-01-22 09:02:47 +03:00
|
|
|
"io"
|
2020-01-30 08:01:01 +03:00
|
|
|
"sync"
|
2020-02-06 02:12:12 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/revid/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultBasicThreshold = 45000
|
|
|
|
defaultBasicPixels = 1000
|
2020-01-22 09:02:47 +03:00
|
|
|
)
|
|
|
|
|
2020-01-31 06:48:54 +03:00
|
|
|
type pixel struct{ r, g, b uint32 }
|
2020-01-28 08:34:18 +03:00
|
|
|
|
2020-01-31 06:55:26 +03:00
|
|
|
// Basic is a filter that provides basic motion detection via a difference
|
2020-01-31 05:25:49 +03:00
|
|
|
// method.
|
|
|
|
type Basic struct {
|
2020-02-07 04:12:27 +03:00
|
|
|
debugging debugWindows
|
2020-02-05 09:06:09 +03:00
|
|
|
dst io.WriteCloser
|
|
|
|
img image.Image
|
|
|
|
bg [][]pixel
|
2020-02-07 04:12:27 +03:00
|
|
|
bwImg *image.Gray
|
2020-02-05 09:06:09 +03:00
|
|
|
thresh int
|
|
|
|
pix int
|
|
|
|
w int
|
|
|
|
h int
|
|
|
|
motion int
|
2020-01-22 09:02:47 +03:00
|
|
|
}
|
|
|
|
|
2020-01-31 06:55:26 +03:00
|
|
|
// NewBasic returns a pointer to a new Basic filter struct.
|
2020-02-06 02:12:12 +03:00
|
|
|
func NewBasic(dst io.WriteCloser, c config.Config) *Basic {
|
|
|
|
// Validate parameters.
|
|
|
|
if c.MotionThreshold <= 0 {
|
|
|
|
c.LogInvalidField("MotionThreshold", defaultBasicThreshold)
|
|
|
|
c.MotionThreshold = defaultBasicThreshold
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MotionPixels <= 0 {
|
|
|
|
c.LogInvalidField("MotionPixels", defaultBasicPixels)
|
|
|
|
c.MotionPixels = defaultBasicPixels
|
|
|
|
}
|
|
|
|
|
2020-02-05 09:06:09 +03:00
|
|
|
return &Basic{
|
|
|
|
dst: dst,
|
2020-02-06 02:12:12 +03:00
|
|
|
thresh: int(c.MotionThreshold),
|
|
|
|
pix: c.MotionPixels,
|
2020-02-07 04:12:27 +03:00
|
|
|
debugging: newWindows("BASIC"),
|
2020-01-24 09:31:28 +03:00
|
|
|
}
|
2020-01-22 09:02:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements io.Closer.
|
2020-01-31 05:25:49 +03:00
|
|
|
func (bf *Basic) Close() error {
|
2020-02-05 09:06:09 +03:00
|
|
|
return bf.debugging.close()
|
2020-01-22 09:02:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-01-31 05:25:49 +03:00
|
|
|
func (bf *Basic) Write(f []byte) (int, error) {
|
2020-01-31 03:23:25 +03:00
|
|
|
// Decode MJPEG.
|
2020-01-30 08:01:01 +03:00
|
|
|
var err error
|
2020-01-31 05:25:49 +03:00
|
|
|
bf.img, err = jpeg.Decode(bytes.NewReader(f))
|
2020-01-24 09:31:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("image can't be decoded: %w", err)
|
|
|
|
}
|
2020-01-30 08:01:01 +03:00
|
|
|
|
2020-02-03 03:41:38 +03:00
|
|
|
// First frame must be set as the first background image.
|
2020-01-31 05:25:49 +03:00
|
|
|
if bf.bg == nil {
|
|
|
|
bounds := bf.img.Bounds()
|
|
|
|
bf.w = bounds.Max.X
|
|
|
|
bf.h = bounds.Max.Y
|
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
bf.bwImg = image.NewGray(image.Rect(0, 0, bf.w, bf.h))
|
2020-01-31 05:25:49 +03:00
|
|
|
|
|
|
|
bf.bg = make([][]pixel, bf.h)
|
2020-01-31 07:53:18 +03:00
|
|
|
for j, _ := range bf.bg {
|
|
|
|
bf.bg[j] = make([]pixel, bf.w)
|
|
|
|
for i, _ := range bf.bg[j] {
|
2020-01-31 05:25:49 +03:00
|
|
|
p := bf.img.At(i, j)
|
|
|
|
r, g, b, _ := p.RGBA()
|
2020-01-31 07:53:18 +03:00
|
|
|
bf.bg[j][i].r = r
|
|
|
|
bf.bg[j][i].b = b
|
|
|
|
bf.bg[j][i].g = g
|
2020-01-24 09:31:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(f), nil
|
|
|
|
}
|
|
|
|
|
2020-01-31 05:25:49 +03:00
|
|
|
// Use 4x goroutines to each process one row of pixels.
|
2020-01-31 06:53:33 +03:00
|
|
|
var j int
|
|
|
|
j = 0
|
2020-01-31 07:53:18 +03:00
|
|
|
var wg sync.WaitGroup
|
2020-01-30 08:01:01 +03:00
|
|
|
|
2020-01-31 05:25:49 +03:00
|
|
|
for j < bf.h {
|
2020-01-30 08:01:01 +03:00
|
|
|
wg.Add(4)
|
2020-01-31 07:53:18 +03:00
|
|
|
go bf.process(j, &wg)
|
|
|
|
go bf.process(j+1, &wg)
|
|
|
|
go bf.process(j+2, &wg)
|
|
|
|
go bf.process(j+3, &wg)
|
2020-01-30 08:01:01 +03:00
|
|
|
j = j + 4
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2020-01-28 08:34:18 +03:00
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
// Draw debug information.
|
|
|
|
bf.debugging.show(bf.img, bf.bwImg, bf.motion > bf.pix, nil, fmt.Sprintf("Motion: %d", bf.motion), fmt.Sprintf("Pix: %d", bf.pix))
|
2020-01-22 09:02:47 +03:00
|
|
|
|
2020-01-31 03:23:25 +03:00
|
|
|
// If there are not enough motion pixels then discard the frame.
|
2020-02-03 03:22:04 +03:00
|
|
|
if bf.motion < bf.pix {
|
2020-01-28 08:34:18 +03:00
|
|
|
return len(f), nil
|
|
|
|
}
|
2020-01-22 09:02:47 +03:00
|
|
|
|
2020-01-31 03:23:25 +03:00
|
|
|
// Write all motion frames.
|
2020-01-31 05:25:49 +03:00
|
|
|
return bf.dst.Write(f)
|
|
|
|
}
|
|
|
|
|
2020-02-03 03:41:38 +03:00
|
|
|
// Go routine for one row of the image to be processed.
|
2020-01-31 05:25:49 +03:00
|
|
|
func (bf *Basic) process(j int, wg *sync.WaitGroup) {
|
2020-01-31 08:26:36 +03:00
|
|
|
for i, _ := range bf.bg[j] {
|
2020-01-31 05:25:49 +03:00
|
|
|
n := bf.img.At(i, j)
|
|
|
|
r, b, g, _ := n.RGBA()
|
|
|
|
|
|
|
|
// Compare the difference of the RGB values of each pixel to the background image.
|
|
|
|
diffR := absDiff(r, bf.bg[j][i].r)
|
|
|
|
diffG := absDiff(g, bf.bg[j][i].g)
|
|
|
|
diffB := absDiff(g, bf.bg[j][i].b)
|
|
|
|
|
|
|
|
diff := diffR + diffG + diffB
|
|
|
|
|
2020-02-03 03:22:04 +03:00
|
|
|
if diff > bf.thresh {
|
2020-01-31 05:25:49 +03:00
|
|
|
bf.motion++
|
2020-02-07 04:12:27 +03:00
|
|
|
bf.bwImg.SetGray(i, j, color.Gray{0xff})
|
2020-02-04 03:44:09 +03:00
|
|
|
} else {
|
2020-02-07 04:12:27 +03:00
|
|
|
bf.bwImg.SetGray(i, j, color.Gray{0x00})
|
2020-01-31 05:25:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update backgound image.
|
2020-01-31 07:53:18 +03:00
|
|
|
bf.bg[j][i].r = r
|
|
|
|
bf.bg[j][i].b = b
|
|
|
|
bf.bg[j][i].g = g
|
2020-01-31 05:25:49 +03:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
2020-02-03 03:41:38 +03:00
|
|
|
// Returns the absolute value of the difference of two uint32 numbers.
|
2020-01-31 08:26:36 +03:00
|
|
|
func absDiff(a, b uint32) int {
|
|
|
|
c := int(a) - int(b)
|
|
|
|
if c < 0 {
|
|
|
|
return -c
|
|
|
|
} else {
|
|
|
|
return c
|
|
|
|
}
|
2020-01-22 09:02:47 +03:00
|
|
|
}
|