67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package producer
|
|
|
|
import (
|
|
"errors"
|
|
"image"
|
|
|
|
"github.com/duke-git/lancet/v2/slice"
|
|
log "github.com/sirupsen/logrus"
|
|
"gocv.io/x/gocv"
|
|
)
|
|
|
|
type PhaseShiftM struct {
|
|
dx float32
|
|
dy float32
|
|
response float64
|
|
Block Block
|
|
}
|
|
|
|
type Block struct {
|
|
width int
|
|
height int
|
|
coord image.Point // top-left corner of the block in the original image
|
|
}
|
|
|
|
func (r *Registrator) calculateBlockPhaseShift(panBlock, mssBlock gocv.Mat) (gocv.Point2f, float64) {
|
|
pan := gocv.NewMat()
|
|
mss := gocv.NewMat()
|
|
|
|
panBlock.ConvertTo(&pan, gocv.MatTypeCV32FC1)
|
|
defer pan.Close()
|
|
mssBlock.ConvertTo(&mss, gocv.MatTypeCV32FC1)
|
|
defer mss.Close()
|
|
|
|
hann := gocv.NewMat()
|
|
defer hann.Close()
|
|
|
|
shift, response := gocv.PhaseCorrelate(pan, mss, hann)
|
|
|
|
dx := shift.X
|
|
dy := shift.Y
|
|
log.Debugf("Block shift: dx = %f, dy = %f. response = %f", dx, dy, response)
|
|
|
|
return shift, response
|
|
}
|
|
|
|
func (r *Registrator) fileterPhaseShift(thredholds []float64, greaterThan bool) error {
|
|
if len(thredholds) > 4 {
|
|
return errors.New("thredholds length should be less than 4")
|
|
}
|
|
|
|
for i := 0; i < len(thredholds); i++ {
|
|
th := thredholds[i]
|
|
r.phaseShifts[i] = slice.Filter(r.phaseShifts[i], func(i int, value PhaseShiftM) bool {
|
|
if value.response > 0.999999 {
|
|
return false
|
|
}
|
|
|
|
if greaterThan {
|
|
return value.dy > float32(th)
|
|
}
|
|
return value.dy < float32(th)
|
|
})
|
|
}
|
|
|
|
return nil
|
|
}
|