From 3047704ca08ad60ac8d486832838a2a166456f04 Mon Sep 17 00:00:00 2001 From: Russell Stanley Date: Mon, 11 Apr 2022 09:23:40 +0930 Subject: [PATCH] revid: add transfrom matrix variable --- cmd/rv/main.go | 2 +- cmd/rv/probe.go | 5 +- cmd/rv/probe_test.go | 3 +- revid/config/config.go | 2 + revid/config/variables.go | 140 ++++++++++++++++++++---------------- turbidity/turbidity.go | 21 +++--- turbidity/turbidity_test.go | 17 +++-- 7 files changed, 112 insertions(+), 78 deletions(-) diff --git a/cmd/rv/main.go b/cmd/rv/main.go index bd22e8b9..c430faed 100644 --- a/cmd/rv/main.go +++ b/cmd/rv/main.go @@ -146,7 +146,7 @@ func main() { p *turbidityProbe ) - p, err := NewTurbidityProbe(*log, 60*time.Second) + p, err := NewTurbidityProbe(*log, 60*time.Second, rv.Config().TransformMatrix) if err != nil { log.Log(logger.Fatal, "could not create new turbidity probe", "error", err.Error()) } diff --git a/cmd/rv/probe.go b/cmd/rv/probe.go index 9bda6931..5e1ca408 100644 --- a/cmd/rv/probe.go +++ b/cmd/rv/probe.go @@ -68,16 +68,18 @@ type turbidityProbe struct { ts *turbidity.TurbiditySensor log logger.Logger buffer *bytes.Buffer + transform []float64 trimCounter int } // NewTurbidityProbe returns a new turbidity probe. -func NewTurbidityProbe(log logger.Logger, delay time.Duration) (*turbidityProbe, error) { +func NewTurbidityProbe(log logger.Logger, delay time.Duration, transformMatrix []float64) (*turbidityProbe, error) { tp := new(turbidityProbe) tp.log = log tp.delay = delay tp.ticker = *time.NewTicker(delay) tp.buffer = bytes.NewBuffer(*new([]byte)) + tp.transform = transformMatrix // Create the turbidity sensor. standard := gocv.IMRead("../../turbidity/images/default.jpg", gocv.IMReadGrayScale) @@ -125,6 +127,7 @@ func (tp *turbidityProbe) Write(p []byte) (int, error) { select { case <-tp.ticker.C: tp.log.Log(logger.Debug, "beginning turbidity calculation") + tp.log.Log(logger.Debug, "transformation matrix", tp.transform[0]) startTime := time.Now() err := tp.turbidityCalculation() if err != nil { diff --git a/cmd/rv/probe_test.go b/cmd/rv/probe_test.go index 4f40c702..f04d6b10 100644 --- a/cmd/rv/probe_test.go +++ b/cmd/rv/probe_test.go @@ -44,8 +44,9 @@ func TestProbe(t *testing.T) { MaxAge: logMaxAge, } log := logger.New(logVerbosity, io.MultiWriter(fileLog), logSuppress) + transformMatrix := []float64{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0} - ts, err := NewTurbidityProbe(*log, time.Microsecond) + ts, err := NewTurbidityProbe(*log, time.Microsecond, transformMatrix) if err != nil { t.Fatalf("failed to create turbidity probe") } diff --git a/revid/config/config.go b/revid/config/config.go index 7cf9864a..2e665f72 100644 --- a/revid/config/config.go +++ b/revid/config/config.go @@ -273,6 +273,8 @@ type Config struct { VerticalFlip bool // VerticalFlip flips video vertically for Raspivid input. Width uint // Width defines the input video width Raspivid input. + + TransformMatrix []float64 // Describes the transformation matrix to extract the target. } // Validate checks for any errors in the config fields and defaults settings diff --git a/revid/config/variables.go b/revid/config/variables.go index db16aa33..8098eb68 100644 --- a/revid/config/variables.go +++ b/revid/config/variables.go @@ -99,6 +99,7 @@ const ( KeyVBRQuality = "VBRQuality" KeyVerticalFlip = "VerticalFlip" KeyWidth = "Width" + KeyTransformMatrix = "TransformMatrix" ) // Config map parameter types. @@ -140,38 +141,55 @@ const ( // this variable in a Config, and a function for validating the value of the variable. var Variables = []struct { Name string - Type string + Type string Update func(*Config, string) Validate func(*Config) }{ + { + Name: KeyTransformMatrix, + Type: typeString, + Update: func(c *Config, v string) { + v = strings.Replace(v, " ", "", 0) + elements := strings.Split(v, ",") + vals := make([]float64, len(elements)) + for _, i := range elements { + vFloat, err := strconv.ParseFloat(i, 64) + if err != nil { + c.Logger.Log(logger.Warning, "invalid TransformMatrix param", "value", i) + } + vals = append(vals, vFloat) + } + c.TransformMatrix = vals + }, + }, { Name: KeyAutoWhiteBalance, - Type: "enum:off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon", + Type: "enum:off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon", Update: func(c *Config, v string) { c.AutoWhiteBalance = v }, }, { Name: KeyAWBGains, - Type: typeString, + Type: typeString, Update: func(c *Config, v string) { c.AWBGains = v }, }, { Name: KeyBitDepth, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.BitDepth = parseUint(KeyBitDepth, v, c) }, }, { Name: KeyBitrate, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.Bitrate = parseUint(KeyBitrate, v, c) }, }, { Name: KeyBrightness, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.Brightness = parseUint(KeyBrightness, v, c) }, }, { Name: KeyBurstPeriod, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.BurstPeriod = parseUint(KeyBurstPeriod, v, c) }, Validate: func(c *Config) { if c.BurstPeriod <= 0 { @@ -182,12 +200,12 @@ var Variables = []struct { }, { Name: KeyCameraChan, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.CameraChan = uint8(parseUint(KeyCameraChan, v, c)) }, }, { Name: KeyCameraIP, - Type: typeString, + Type: typeString, Update: func(c *Config, v string) { c.CameraIP = v }, Validate: func(c *Config) { if c.CameraIP == "" { @@ -198,11 +216,11 @@ var Variables = []struct { }, { Name: KeyCBR, - Type: typeBool, + Type: typeBool, Update: func(c *Config, v string) { c.CBR = parseBool(KeyCBR, v, c) }, }, { - Name: KeyClipDuration, + Name: KeyClipDuration, Type: typeUint, Update: func(c *Config, v string) { _v, err := strconv.Atoi(v) @@ -220,27 +238,27 @@ var Variables = []struct { }, { Name: KeyChannels, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.Channels = parseUint(KeyChannels, v, c) }, }, { Name: KeyContrast, - Type: typeInt, + Type: typeInt, Update: func(c *Config, v string) { c.Contrast = parseInt(KeyContrast, v, c) }, }, { Name: KeyEV, - Type: typeInt, + Type: typeInt, Update: func(c *Config, v string) { c.EV = parseInt(KeyEV, v, c) }, }, { Name: KeyExposure, - Type: "enum:auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks", + Type: "enum:auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks", Update: func(c *Config, v string) { c.Exposure = v }, }, { Name: KeyFileFPS, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.FileFPS = parseUint(KeyFileFPS, v, c) }, Validate: func(c *Config) { if c.FileFPS <= 0 || (c.FileFPS > 0 && c.Input != InputFile) { @@ -250,7 +268,7 @@ var Variables = []struct { }, }, { - Name: KeyFilters, + Name: KeyFilters, Type: "enums:NoOp,MOG,VariableFPS,KNN,Difference,Basic", Update: func(c *Config, v string) { filters := strings.Split(v, ",") @@ -267,7 +285,7 @@ var Variables = []struct { }, { Name: KeyFrameRate, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.FrameRate = parseUint(KeyFrameRate, v, c) }, Validate: func(c *Config) { if c.FrameRate <= 0 || c.FrameRate > 60 { @@ -278,21 +296,21 @@ var Variables = []struct { }, { Name: KeyHeight, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.Height = parseUint(KeyHeight, v, c) }, }, { Name: KeyHorizontalFlip, - Type: typeBool, + Type: typeBool, Update: func(c *Config, v string) { c.HorizontalFlip = parseBool(KeyHorizontalFlip, v, c) }, }, { Name: KeyHTTPAddress, - Type: typeString, + Type: typeString, Update: func(c *Config, v string) { c.HTTPAddress = v }, }, { - Name: KeyInput, + Name: KeyInput, Type: "enum:raspivid,raspistill,rtsp,v4l,file,audio", Update: func(c *Config, v string) { c.Input = parseEnum( @@ -319,7 +337,7 @@ var Variables = []struct { }, }, { - Name: KeyInputCodec, + Name: KeyInputCodec, Type: "enum:h264,h265,mjpeg,jpeg,pcm,adpcm", Update: func(c *Config, v string) { c.InputCodec = v @@ -333,16 +351,16 @@ var Variables = []struct { }, { Name: KeyInputPath, - Type: typeString, + Type: typeString, Update: func(c *Config, v string) { c.InputPath = v }, }, { Name: KeyISO, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.ISO = parseUint(KeyISO, v, c) }, }, { - Name: KeyLogging, + Name: KeyLogging, Type: "enum:Debug,Info,Warning,Error,Fatal", Update: func(c *Config, v string) { switch v { @@ -371,18 +389,18 @@ var Variables = []struct { }, { Name: KeyLoop, - Type: typeBool, + Type: typeBool, Update: func(c *Config, v string) { c.Loop = parseBool(KeyLoop, v, c) }, }, { Name: KeyMinFPS, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.MinFPS = parseUint(KeyMinFPS, v, c) }, Validate: func(c *Config) { c.MinFPS = lessThanOrEqual(KeyMinFPS, c.MinFPS, 0, c, defaultMinFPS) }, }, { Name: KeyMinFrames, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.MinFrames = parseUint(KeyMinFrames, v, c) }, Validate: func(c *Config) { const maxMinFrames = 1000 @@ -393,7 +411,7 @@ var Variables = []struct { }, }, { - Name: KeyMode, + Name: KeyMode, Type: "enum:Normal,Paused,Burst,Loop", Update: func(c *Config, v string) { c.Loop = false @@ -404,26 +422,26 @@ var Variables = []struct { }, { Name: KeyMotionDownscaling, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.MotionDownscaling = parseUint(KeyMotionDownscaling, v, c) }, }, { Name: KeyMotionHistory, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.MotionHistory = parseUint(KeyMotionHistory, v, c) }, }, { Name: KeyMotionInterval, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.MotionInterval = parseUint(KeyMotionInterval, v, c) }, }, { Name: KeyMotionKernel, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.MotionKernel = parseUint(KeyMotionKernel, v, c) }, }, { - Name: KeyMotionMinArea, + Name: KeyMotionMinArea, Type: typeFloat, Update: func(c *Config, v string) { f, err := strconv.ParseFloat(v, 64) @@ -435,16 +453,16 @@ var Variables = []struct { }, { Name: KeyMotionPadding, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.MotionPadding = parseUint(KeyMotionPadding, v, c) }, }, { Name: KeyMotionPixels, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.MotionPixels = parseUint(KeyMotionPixels, v, c) }, }, { - Name: KeyMotionThreshold, + Name: KeyMotionThreshold, Type: typeFloat, Update: func(c *Config, v string) { f, err := strconv.ParseFloat(v, 64) @@ -455,7 +473,7 @@ var Variables = []struct { }, }, { - Name: KeyOutput, + Name: KeyOutput, Type: "enum:File,HTTP,RTMP,RTP", Update: func(c *Config, v string) { c.Outputs = make([]uint8, 1) @@ -477,11 +495,11 @@ var Variables = []struct { }, { Name: KeyOutputPath, - Type: typeString, + Type: typeString, Update: func(c *Config, v string) { c.OutputPath = v }, }, { - Name: KeyOutputs, + Name: KeyOutputs, Type: "enums:File,HTTP,RTMP,RTP", Update: func(c *Config, v string) { outputs := strings.Split(v, ",") @@ -512,18 +530,18 @@ var Variables = []struct { }, { Name: KeyPSITime, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.PSITime = parseUint(KeyPSITime, v, c) }, Validate: func(c *Config) { c.PSITime = lessThanOrEqual(KeyPSITime, c.PSITime, 0, c, defaultPSITime) }, }, { Name: KeyQuantization, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.Quantization = parseUint(KeyQuantization, v, c) }, }, { Name: KeyPoolCapacity, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.PoolCapacity = parseUint(KeyPoolCapacity, v, c) }, Validate: func(c *Config) { c.PoolCapacity = lessThanOrEqual(KeyPoolCapacity, c.PoolCapacity, 0, c, defaultPoolCapacity) @@ -531,7 +549,7 @@ var Variables = []struct { }, { Name: KeyPoolStartElementSize, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.PoolStartElementSize = parseUint("PoolStartElementSize", v, c) }, Validate: func(c *Config) { c.PoolStartElementSize = lessThanOrEqual("PoolStartElementSize", c.PoolStartElementSize, 0, c, defaultPoolStartElementSize) @@ -539,14 +557,14 @@ var Variables = []struct { }, { Name: KeyPoolWriteTimeout, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.PoolWriteTimeout = parseUint(KeyPoolWriteTimeout, v, c) }, Validate: func(c *Config) { c.PoolWriteTimeout = lessThanOrEqual(KeyPoolWriteTimeout, c.PoolWriteTimeout, 0, c, defaultPoolWriteTimeout) }, }, { - Name: KeyRecPeriod, + Name: KeyRecPeriod, Type: typeFloat, Update: func(c *Config, v string) { _v, err := strconv.ParseFloat(v, 64) @@ -558,17 +576,17 @@ var Variables = []struct { }, { Name: KeyRotation, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.Rotation = parseUint(KeyRotation, v, c) }, }, { Name: KeyRTMPURL, - Type: typeString, + Type: typeString, Update: func(c *Config, v string) { c.RTMPURL = v }, }, { Name: KeyRTPAddress, - Type: typeString, + Type: typeString, Update: func(c *Config, v string) { c.RTPAddress = v }, Validate: func(c *Config) { if c.RTPAddress == "" { @@ -579,21 +597,21 @@ var Variables = []struct { }, { Name: KeySampleRate, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.SampleRate = parseUint(KeySampleRate, v, c) }, }, { Name: KeySaturation, - Type: typeInt, + Type: typeInt, Update: func(c *Config, v string) { c.Saturation = parseInt(KeySaturation, v, c) }, }, { Name: KeySharpness, - Type: typeInt, + Type: typeInt, Update: func(c *Config, v string) { c.Sharpness = parseInt(KeySharpness, v, c) }, }, { - Name: KeyJPEGQuality, + Name: KeyJPEGQuality, Type: typeUint, Update: func(c *Config, v string) { _v, err := strconv.Atoi(v) @@ -604,7 +622,7 @@ var Variables = []struct { }, }, { - Name: KeySuppress, + Name: KeySuppress, Type: typeBool, Update: func(c *Config, v string) { c.Suppress = parseBool(KeySuppress, v, c) @@ -612,7 +630,7 @@ var Variables = []struct { }, }, { - Name: KeyTimelapseInterval, + Name: KeyTimelapseInterval, Type: typeUint, Update: func(c *Config, v string) { _v, err := strconv.Atoi(v) @@ -623,7 +641,7 @@ var Variables = []struct { }, }, { - Name: KeyTimelapseDuration, + Name: KeyTimelapseDuration, Type: typeUint, Update: func(c *Config, v string) { _v, err := strconv.Atoi(v) @@ -635,11 +653,11 @@ var Variables = []struct { }, { Name: KeyVBRBitrate, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.VBRBitrate = parseUint(KeyVBRBitrate, v, c) }, }, { - Name: KeyVBRQuality, + Name: KeyVBRQuality, Type: "enum:standard,fair,good,great,excellent", Update: func(c *Config, v string) { c.VBRQuality = Quality(parseEnum( @@ -658,12 +676,12 @@ var Variables = []struct { }, { Name: KeyVerticalFlip, - Type: typeBool, + Type: typeBool, Update: func(c *Config, v string) { c.VerticalFlip = parseBool(KeyVerticalFlip, v, c) }, }, { Name: KeyWidth, - Type: typeUint, + Type: typeUint, Update: func(c *Config, v string) { c.Width = parseUint(KeyWidth, v, c) }, }, } diff --git a/turbidity/turbidity.go b/turbidity/turbidity.go index 8bbc4d6e..cc8578c2 100644 --- a/turbidity/turbidity.go +++ b/turbidity/turbidity.go @@ -41,11 +41,19 @@ import ( "gocv.io/x/gocv" ) +// Homography constants +const ( + ransacThreshold = 3.0 // Maximum allowed reprojection error to treat a point pair as an inlier. + maxIter = 2000 // The maximum number of RANSAC iterations. + confidence = 0.995 // Confidence level, between 0 and 1. +) + // TurbiditySensor is a software based turbidity sensor that uses CV to determine sharpness and constrast level // of a chessboard-like target submerged in water that can be correlated to turbidity/visibility values. type TurbiditySensor struct { template, templateCorners gocv.Mat standard, standardCorners gocv.Mat + H gocv.Mat k1, k2, sobelFilterSize int scale, alpha float64 log logger.Logger @@ -56,6 +64,7 @@ func NewTurbiditySensor(template, standard gocv.Mat, k1, k2, sobelFilterSize int ts := new(TurbiditySensor) templateCorners := gocv.NewMat() standardCorners := gocv.NewMat() + mask := gocv.NewMat() // Validate template image is not empty and has valid corners. if template.Empty() { @@ -74,8 +83,10 @@ func NewTurbiditySensor(template, standard gocv.Mat, k1, k2, sobelFilterSize int if !gocv.FindChessboardCorners(standard, image.Pt(3, 3), &standardCorners, gocv.CalibCBNormalizeImage) { return nil, errors.New("could not find corners in standard image") } + ts.standard = standard ts.standardCorners = standardCorners + ts.H = gocv.FindHomography(ts.standardCorners, &ts.templateCorners, gocv.HomograpyMethodRANSAC, ransacThreshold, &mask, maxIter, confidence) ts.k1, ts.k2, ts.sobelFilterSize = k1, k2, sobelFilterSize ts.alpha, ts.scale = alpha, scale @@ -191,13 +202,6 @@ func (ts TurbiditySensor) evaluateBlockAMEE(img gocv.Mat, xStart, yStart, xEnd, // transform will search img for matching template. Returns the transformed image which best match the template. func (ts TurbiditySensor) transform(img gocv.Mat) (gocv.Mat, error) { out := gocv.NewMat() - mask := gocv.NewMat() - imgCorners := ts.standardCorners - const ( - ransacThreshold = 3.0 // Maximum allowed reprojection error to treat a point pair as an inlier. - maxIter = 2000 // The maximum number of RANSAC iterations. - confidence = 0.995 // Confidence level, between 0 and 1. - ) if img.Empty() { return out, errors.New("image is empty, cannot transform") @@ -206,8 +210,7 @@ func (ts TurbiditySensor) transform(img gocv.Mat) (gocv.Mat, error) { // if !gocv.FindChessboardCorners(img, image.Pt(3, 3), &imgCorners, gocv.CalibCBFastCheck) {} // Find and apply transformation. - H := gocv.FindHomography(imgCorners, &ts.templateCorners, gocv.HomograpyMethodRANSAC, ransacThreshold, &mask, maxIter, confidence) - gocv.WarpPerspective(img, &out, H, image.Pt(ts.template.Rows(), ts.template.Cols())) + gocv.WarpPerspective(img, &out, ts.H, image.Pt(ts.template.Rows(), ts.template.Cols())) gocv.CvtColor(out, &out, gocv.ColorRGBToGray) return out, nil } diff --git a/turbidity/turbidity_test.go b/turbidity/turbidity_test.go index 4c97b65a..72f4e5bf 100644 --- a/turbidity/turbidity_test.go +++ b/turbidity/turbidity_test.go @@ -42,8 +42,8 @@ import ( ) const ( - nImages = 10 // Number of images to test. (Max 13) - nSamples = 1 // Number of samples for each image. (Max 10) + nImages = 12 // Number of images to test. (Max 13) + nSamples = 10 // Number of samples for each image. (Max 10) increment = 2.5 // Increment of the turbidity level. ) @@ -62,7 +62,7 @@ const ( func TestImages(t *testing.T) { const ( - k1, k2 = 8, 8 + k1, k2 = 4, 4 filterSize = 3 scale, alpha = 1.0, 1.0 ) @@ -85,7 +85,7 @@ func TestImages(t *testing.T) { for i := range imgs { imgs[i] = make([]gocv.Mat, nSamples) for j := range imgs[i] { - imgs[i][j] = gocv.IMRead(fmt.Sprintf("images/t-%v/000%v.jpg", i, j), gocv.IMReadGrayScale) + imgs[i][j] = gocv.IMRead(fmt.Sprintf("images/t-%v/000%v.jpg", i, j), gocv.IMReadColor) } } @@ -94,6 +94,13 @@ func TestImages(t *testing.T) { t.Fatalf("could not create turbidity sensor: %v", err) } + for i := 0; i < ts.H.Rows(); i++ { + for j := 0; j < ts.H.Cols(); j++ { + fmt.Printf(" %v,\t", ts.H.GetDoubleAt(i, j)) + } + fmt.Println() + } + results, err := NewResults(nImages) if err != nil { t.Fatalf("could not create results: %v", err) @@ -111,7 +118,7 @@ func TestImages(t *testing.T) { results.Update(stat.Mean(sample_result.Sharpness, nil), stat.Mean(sample_result.Contrast, nil), float64(i)*increment, i) } - err = plotResults(results.Turbidity, results.Sharpness, results.Contrast) + err = plotResults(results.Turbidity, normalize(results.Sharpness), normalize(results.Contrast)) if err != nil { t.Fatalf("plotting Failed: %v", err) }