分景后配准

This commit is contained in:
nuknal
2024-11-01 09:19:42 +08:00
parent e51d07901d
commit 75e4dd6d90
7 changed files with 191 additions and 58 deletions

View File

@@ -22,8 +22,6 @@ const (
PixelBytes = 2
PanWidth = payload.PAN_PIXEL_WIDTH // 像素宽度
MssWidth = payload.MSS_PIXEL_WIDTH
BlockNH = 8
BlockNW = 4
OverlappedBlockLines = 1000 // 重叠块的行数
DownSampled ResampleMethod = "down_sample_pan"
UpSampled ResampleMethod = "up_sample_mss"
@@ -33,6 +31,7 @@ const (
ReferenceShiftYB2 = 200.0
ReferenceShiftYB3 = 300.0
ReferenceShiftYB4 = 400.0
MaxShiftY = 480
)
type ResampleMethod string
@@ -47,6 +46,8 @@ type ImgProc struct {
MssImages [4]gocv.Mat
MssHeight int
MssWidth int
blockH int
blockW int
shiftMutex sync.Mutex
phaseShifts [4][]PhaseShiftM
@@ -72,6 +73,8 @@ type ImgProc struct {
func NewImgProc(rsmethod ResampleMethod) *ImgProc {
var r ImgProc
r.resampleMethod = rsmethod
r.blockH = 8
r.blockW = 8
return &r
}
@@ -141,7 +144,14 @@ func (r *ImgProc) LoadMssRaw() error {
return nil
}
func (r *ImgProc) DoPhaseCorrelation() error {
func (r *ImgProc) DoPhaseCorrelation(skip bool) error {
if skip {
for i := 0; i < MssBands; i++ {
r.registeredMssImages[i] = r.MssImages[i].Clone()
}
return nil
}
switch r.resampleMethod {
case UpSampled:
return r.CalcUpPhaseCorrelation()
@@ -167,11 +177,13 @@ func (r *ImgProc) CalcDownPhaseCorrelation() error {
log.Println("down sampled PAN images size:", downsampledPanImage.Size())
// 分块高度
blockHeight := r.MssHeight / BlockNH
blockWidth := r.MssWidth / BlockNW
blockHeight := r.MssHeight / r.blockH
blockWidth := r.MssWidth / r.blockW
log.Infof("blockHeight=%d, blockWidth=%d", blockHeight, blockWidth)
// 在 MSS 4 个波段上进行配准
err := r.doPhaseCorrelation(r.MssImages[0],
err := r.doPhaseCorrelation(downsampledPanImage,
[]gocv.Mat{r.MssImages[0], r.MssImages[1], r.MssImages[2], r.MssImages[3]},
r.MssHeight, r.MssWidth, blockHeight, blockWidth)
if err != nil {
@@ -195,22 +207,30 @@ func (r *ImgProc) CalcDownPhaseCorrelation() error {
// r.DoMSSCoRegistration(true)
// 基于 PAN 图像进行配准
err = r.doPhaseCorrelation(downsampledPanImage,
[]gocv.Mat{r.registeredMssImages[0]},
r.MssHeight, r.MssWidth, blockHeight, blockWidth)
if err != nil {
return err
// err = r.doPhaseCorrelation(downsampledPanImage,
// []gocv.Mat{r.registeredMssImages[0]},
// r.MssHeight, r.MssWidth, blockHeight, blockWidth)
// if err != nil {
// return err
// }
// r.fileterPhaseShift([]float64{30.0}, true)
// r.calcMSSDeltaCoeffs(1)
// r.DoPANCoRegistration()
// 裁掉位移部分
r.MssHeight -= MaxShiftY
r.PanHeight -= MaxShiftY * 4
r.PanImage = r.PanImage.Region(image.Rect(0, 0, r.PanWidth, r.PanHeight))
for i := 0; i < MssBands; i++ {
r.registeredMssImages[i] = r.registeredMssImages[i].Region(image.Rect(0, 0, r.MssWidth, r.MssHeight))
}
r.fileterPhaseShift([]float64{30.0}, true)
r.calcMSSDeltaCoeffs(1)
r.DoPANCoRegistration()
return nil
}
// 将MSS升采样采样后计算相位相关的偏移量
func (r *ImgProc) CalcUpPhaseCorrelation() error {
log.Fatal("unsuppotted up-resample method")
log.Fatal("not implemented yet")
// 确保 MSS 高度是 PAN 高度的 1/4
if r.MssHeight*4 != r.PanHeight {
err := fmt.Errorf("MSS height is not 1/4 of PAN height, invalid raw file")
@@ -219,34 +239,36 @@ func (r *ImgProc) CalcUpPhaseCorrelation() error {
}
// 在 MSS 4 个波段上进行配准
err := r.doPhaseCorrelation(r.MssImages[0],
[]gocv.Mat{r.MssImages[0], r.MssImages[1], r.MssImages[2], r.MssImages[3]},
r.MssHeight, r.MssWidth, r.MssHeight/BlockNH, r.MssWidth/BlockNW)
if err != nil {
return err
}
r.DoMSSCoRegistration(false)
// err := r.doPhaseCorrelation(r.MssImages[0],
// []gocv.Mat{r.MssImages[0], r.MssImages[1], r.MssImages[2], r.MssImages[3]},
// r.MssHeight, r.MssWidth, r.MssHeight/r.blockH, r.MssWidth/r.blockW)
// if err != nil {
// return err
// }
// r.DoMSSCoRegistration(false)
upsampledMssImages := make([]gocv.Mat, MssBands)
for i := 0; i < MssBands; i++ {
upsampledMssImages[i] = gocv.NewMat()
gocv.Resize(r.registeredMssImages[i], &upsampledMssImages[i],
gocv.Resize(r.MssImages[i], &upsampledMssImages[i],
image.Point{X: r.PanWidth, Y: r.PanHeight}, 0, 0, gocv.InterpolationCubic)
}
fmt.Println("up sampled MSS images size:", upsampledMssImages[0].Size())
// 分块高度 - BlockNH, BlockNW % 4 == 0
blockHeight := r.PanHeight / BlockNH
blockWidth := r.PanWidth / BlockNW
blockHeight := r.PanHeight / r.blockH
blockWidth := r.PanWidth / r.blockW
log.Infof("blockHeight=%d, blockWidth=%d", blockHeight, blockWidth)
// 基于 PAN 图像进行配准
r.doPhaseCorrelation(r.PanImage,
[]gocv.Mat{upsampledMssImages[0]},
r.MssHeight, r.MssWidth, blockHeight, blockWidth)
return r.DoPANCoRegistration()
r.doPhaseCorrelation(r.PanImage, upsampledMssImages,
r.PanHeight, r.PanWidth, blockHeight, blockWidth)
r.fileterPhaseShift([]float64{64, 64, 64, 64}, true)
r.calcMSSDeltaCoeffs(4)
r.DoMSSCoRegistration(false)
return nil
}
func (r *ImgProc) doPhaseCorrelation(base gocv.Mat,
@@ -261,8 +283,8 @@ func (r *ImgProc) doPhaseCorrelation(base gocv.Mat,
r.deltaYCoeffs[band] = make([]float64, 0)
}
for bh := 0; bh < BlockNH; bh++ {
for bw := 0; bw < BlockNW; bw++ {
for bh := 0; bh < r.blockH; bh++ {
for bw := 0; bw < r.blockW; bw++ {
wg.Add(1)
go func(bh, bw int) {
defer wg.Done()
@@ -449,10 +471,6 @@ func (r *ImgProc) DoMSSCoRegistration(byEdge bool) error {
mapY.Close()
}
// 裁掉末尾的的 MSS 480 行 和 PAN 的 480*4 行
r.PanHeight -= 360 * 4
r.MssHeight -= 360
return nil
}
@@ -498,9 +516,6 @@ func (r *ImgProc) DoPANCoRegistration() error {
r.registeredMssImages[i].Close()
r.registeredMssImages[i] = registeredMSS
}
// 裁掉末尾的的 MSS 480 行 和 PAN 的 480*4 行
r.PanHeight -= 120 * 4
r.MssHeight -= 120
return nil
}