optimization
This commit is contained in:
3
pkg/producer/ecc_align.go
Normal file
3
pkg/producer/ecc_align.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package producer
|
||||
|
||||
// align the image via Enhanced Correlation Coefficient (ECC) algorithm
|
||||
22
pkg/producer/edge_phase_correlation.go
Normal file
22
pkg/producer/edge_phase_correlation.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package producer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gocv.io/x/gocv"
|
||||
)
|
||||
|
||||
// https://medium.com/radix-ai-blog/banishing-the-jitters-stabilizing-satellite-imagery-with-opencvs-phase-correlation-3ba2dc6ac096
|
||||
|
||||
// tackle the brightness and contrast problem
|
||||
// by performing phase correlation on detected
|
||||
// edges instead of the raw image
|
||||
|
||||
func FindEdges(img gocv.Mat) gocv.Mat {
|
||||
img0 := gocv.IMRead("/Users/lan/workspace/sjy01/image-proc/data/052022/010/PAN/SJY01_PAN_20240520_115428_052022_103_010_L1A.tiff", gocv.IMReadUnchanged)
|
||||
fmt.Println(img0.Cols(), img0.Rows(), img0.Type().String())
|
||||
img0.ConvertTo(&img0, gocv.MatTypeCV8U)
|
||||
dst := gocv.NewMat()
|
||||
gocv.Canny(img0, &dst, 100, 200)
|
||||
return dst
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package producer
|
||||
|
||||
import (
|
||||
"image"
|
||||
"math"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gocv.io/x/gocv"
|
||||
@@ -41,69 +40,3 @@ func (r *Registrator) calculateBlockPhaseShift(panBlock, mssBlock gocv.Mat) (goc
|
||||
|
||||
return shift, response
|
||||
}
|
||||
|
||||
func (r *Registrator) DoMssPhaseShift() error {
|
||||
// 使用平均偏移量来做平移变换
|
||||
for band := 0; band < MssBands; band++ {
|
||||
var efficientShiftM int
|
||||
var xTotal, yTotal float32
|
||||
for _, shift := range r.phaseShifts[band] {
|
||||
if math.IsNaN(float64(shift.dx)) || math.IsNaN(float64(shift.dy)) {
|
||||
continue
|
||||
}
|
||||
efficientShiftM += 1
|
||||
xTotal += shift.dx
|
||||
yTotal += shift.dy
|
||||
}
|
||||
dx := xTotal / float32(efficientShiftM)
|
||||
dy := yTotal / float32(efficientShiftM)
|
||||
log.Println("Band", band+1, "average shift:", dx, dy, "efficientShiftM:", efficientShiftM)
|
||||
|
||||
translationMat := gocv.NewMatWithSize(2, 3, gocv.MatTypeCV32F)
|
||||
defer translationMat.Close()
|
||||
|
||||
translationMat.SetFloatAt(0, 0, 1)
|
||||
translationMat.SetFloatAt(0, 1, 0)
|
||||
translationMat.SetFloatAt(0, 2, dx)
|
||||
translationMat.SetFloatAt(1, 0, 0)
|
||||
translationMat.SetFloatAt(1, 1, 1)
|
||||
translationMat.SetFloatAt(1, 2, dy)
|
||||
|
||||
alignedMss := gocv.NewMatWithSize(r.MssHeight, r.MssWidth, gocv.MatTypeCV32FC1)
|
||||
defer alignedMss.Close()
|
||||
|
||||
cvtMss := gocv.NewMat()
|
||||
defer cvtMss.Close()
|
||||
|
||||
r.MssImages[band].ConvertTo(&cvtMss, gocv.MatTypeCV32FC1)
|
||||
// 手动平移像素
|
||||
outY := math.MaxInt
|
||||
for y := 0; y < r.MssHeight; y++ {
|
||||
for x := 0; x < r.MssWidth; x++ {
|
||||
// 计算新的坐标
|
||||
newX := x + int(dx)
|
||||
newY := y + int(dy)
|
||||
|
||||
// 如果新坐标在图像范围内,进行像素值赋值
|
||||
if newX >= 0 && newX < r.MssWidth && newY >= 0 && newY < r.MssHeight {
|
||||
alignedMss.SetFloatAt(y, x, cvtMss.GetFloatAt(newY, newX))
|
||||
} else {
|
||||
// 如果新坐标不在图像范围内,设置为黑色
|
||||
alignedMss.SetFloatAt(y, x, 0)
|
||||
if outY > y {
|
||||
outY = y
|
||||
log.Println("Warning: pixel out of range", x, y)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// gocv.WarpAffine(cvtMss, &alignedMss, translationMat, image.Pt(cvtMss.Size()[1], cvtMss.Size()[0]))
|
||||
r.registeredMssImages[band] = gocv.NewMat()
|
||||
alignedMss.ConvertTo(&r.registeredMssImages[band], gocv.MatTypeCV16U)
|
||||
|
||||
log.Println("Band", band+1, "registeredMssImages size:", r.registeredMssImages[band].Size())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user