2021-05-17 04:21:50 +03:00
|
|
|
// +build debug,!nocv
|
2020-02-03 06:58:06 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
Displays debug information for the motion filters.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Scott Barnard <scott@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This file is Copyright (C) 2020 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 (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
2020-02-05 09:06:09 +03:00
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
"gocv.io/x/gocv"
|
|
|
|
)
|
2020-02-05 09:06:09 +03:00
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
// debugWindows is used for displaying debug information for the motion filters.
|
|
|
|
type debugWindows struct {
|
|
|
|
windows []*gocv.Window
|
2020-02-04 03:44:09 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
// close frees resources used by gocv.
|
|
|
|
func (d *debugWindows) close() error {
|
|
|
|
for _, window := range d.windows {
|
|
|
|
err := window.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|
2020-02-04 03:44:09 +03:00
|
|
|
return nil
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
// newWindows creates debugging windows for the motion filter.
|
|
|
|
func newWindows(name string) debugWindows {
|
|
|
|
return debugWindows{
|
|
|
|
windows: []*gocv.Window{
|
|
|
|
gocv.NewWindow(name + ": Video"),
|
|
|
|
gocv.NewWindow(name + ": Motion Detection"),
|
|
|
|
},
|
|
|
|
}
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
// show displays debug information for the motion filters.
|
|
|
|
func (d *debugWindows) show(img, imgDelta interface{}, motion bool, contours *[][]image.Point, text ...string) {
|
|
|
|
var im, imD gocv.Mat
|
|
|
|
const errMsg = "cannot show frame in window: wrong type"
|
|
|
|
var drkRed = color.RGBA{191, 0, 0, 0}
|
|
|
|
var lhtRed = color.RGBA{191, 31, 31, 0}
|
|
|
|
|
|
|
|
// Type conversions.
|
|
|
|
switch img.(type) {
|
|
|
|
case image.Image:
|
|
|
|
im, _ = gocv.ImageToMatRGB(img.(image.Image))
|
|
|
|
case gocv.Mat:
|
|
|
|
im = img.(gocv.Mat)
|
|
|
|
default:
|
|
|
|
panic(errMsg)
|
|
|
|
}
|
|
|
|
switch imgDelta.(type) {
|
|
|
|
case image.Image:
|
|
|
|
imD, _ = gocv.ImageToMatRGB(imgDelta.(image.Image))
|
|
|
|
case gocv.Mat:
|
|
|
|
imD = imgDelta.(gocv.Mat)
|
|
|
|
default:
|
|
|
|
panic(errMsg)
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
// Draw contours.
|
|
|
|
if contours != nil {
|
|
|
|
for _, c := range *contours {
|
|
|
|
rect := gocv.BoundingRect(c)
|
|
|
|
gocv.Rectangle(&im, rect, lhtRed, 1)
|
|
|
|
}
|
|
|
|
}
|
2020-02-04 03:44:09 +03:00
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
// Draw debugging text.
|
|
|
|
if motion {
|
|
|
|
text = append(text, "Motion Detected")
|
|
|
|
}
|
|
|
|
for i, str := range text {
|
|
|
|
gocv.PutText(&im, str, image.Pt(32, 32*(i+1)), gocv.FontHersheyPlain, 2.0, drkRed, 2)
|
|
|
|
}
|
2020-02-04 03:44:09 +03:00
|
|
|
|
2020-02-07 04:12:27 +03:00
|
|
|
// Display windows.
|
|
|
|
d.windows[0].IMShow(im)
|
|
|
|
d.windows[1].IMShow(imD)
|
|
|
|
d.windows[0].WaitKey(1)
|
2020-02-03 06:58:06 +03:00
|
|
|
}
|