revid: add transfrom matrix variable

This commit is contained in:
Russell Stanley 2022-04-11 09:23:40 +09:30
parent ee6fc3ca44
commit 3047704ca0
7 changed files with 112 additions and 78 deletions

View File

@ -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())
}

View File

@ -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 {

View File

@ -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")
}

View File

@ -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

View File

@ -99,6 +99,7 @@ const (
KeyVBRQuality = "VBRQuality"
KeyVerticalFlip = "VerticalFlip"
KeyWidth = "Width"
KeyTransformMatrix = "TransformMatrix"
)
// Config map parameter types.
@ -144,6 +145,23 @@ var Variables = []struct {
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",

View File

@ -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
}

View File

@ -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)
}