2022-04-12 07:02:34 +03:00
|
|
|
//go:build !nocv
|
|
|
|
// +build !nocv
|
|
|
|
|
|
|
|
/*
|
|
|
|
DESCRIPTION
|
2022-04-22 08:39:23 +03:00
|
|
|
Provides a function which can extract the transformation matrix.
|
2022-04-12 07:02:34 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Russell Stanley <russell@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2021-2022 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 turbidity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"image"
|
|
|
|
|
|
|
|
"gocv.io/x/gocv"
|
|
|
|
)
|
|
|
|
|
2022-04-28 07:13:49 +03:00
|
|
|
// Perspective transformation constants.
|
2022-04-12 07:02:34 +03:00
|
|
|
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.
|
|
|
|
)
|
|
|
|
|
2022-04-28 07:13:49 +03:00
|
|
|
// FindTransform, given a template and standard image the perspetive transformation matrix will be determined.
|
|
|
|
// the matrix will be returned and logged for use in vidgrind.
|
2022-04-12 07:02:34 +03:00
|
|
|
func FindTransform(standardPath, templatePath string) (gocv.Mat, error) {
|
|
|
|
mask := gocv.NewMat()
|
2022-04-28 07:13:49 +03:00
|
|
|
std := gocv.IMRead(standardPath, gocv.IMReadColor)
|
|
|
|
stdCorners := gocv.NewMat()
|
2022-04-12 07:02:34 +03:00
|
|
|
|
|
|
|
template := gocv.IMRead(templatePath, gocv.IMReadGrayScale)
|
|
|
|
templateCorners := gocv.NewMat()
|
2022-04-28 07:13:49 +03:00
|
|
|
transformMatrix := gocv.NewMat()
|
2022-04-12 07:02:34 +03:00
|
|
|
|
|
|
|
// Validate template image is not empty and has valid corners.
|
|
|
|
if template.Empty() {
|
2022-04-28 07:13:49 +03:00
|
|
|
return transformMatrix, errors.New("template image is empty")
|
2022-04-12 07:02:34 +03:00
|
|
|
}
|
|
|
|
if !gocv.FindChessboardCorners(template, image.Pt(3, 3), &templateCorners, gocv.CalibCBNormalizeImage) {
|
2022-04-28 07:13:49 +03:00
|
|
|
return transformMatrix, errors.New("could not find corners in template image")
|
2022-04-12 07:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate standard image is not empty and has valid corners.
|
2022-04-28 07:13:49 +03:00
|
|
|
if std.Empty() {
|
|
|
|
return transformMatrix, errors.New("standard image is empty")
|
2022-04-12 07:02:34 +03:00
|
|
|
}
|
2022-04-28 07:13:49 +03:00
|
|
|
if !gocv.FindChessboardCorners(std, image.Pt(3, 3), &stdCorners, gocv.CalibCBNormalizeImage) {
|
|
|
|
return transformMatrix, errors.New("could not find corners in standard image")
|
2022-04-12 07:02:34 +03:00
|
|
|
}
|
|
|
|
|
2022-04-28 07:13:49 +03:00
|
|
|
transformMatrix = gocv.FindHomography(stdCorners, &templateCorners, gocv.HomograpyMethodRANSAC, ransacThreshold, &mask, maxIter, confidence)
|
|
|
|
return transformMatrix, nil
|
2022-04-12 07:02:34 +03:00
|
|
|
}
|