This commit is contained in:
nuknal
2024-05-29 10:20:21 +08:00
parent 5eb5ae6a72
commit e15ae9247b
17 changed files with 276 additions and 45 deletions

167
producer/scenes.go Normal file
View File

@@ -0,0 +1,167 @@
package imageproc
import (
"fmt"
"image"
"os"
"path/filepath"
"strings"
"github.com/airbusgeo/godal"
log "github.com/sirupsen/logrus"
"gocv.io/x/gocv"
)
type Scene struct {
Width int
Height int
X int // coordinate of the left top corner of the scene
Y int
Mat []gocv.Mat
Tiff string
}
func (s *Scene) Cleanup() {
for i := range s.Mat {
s.Mat[i].Close()
}
}
// 对 PAN 和 配准后的MSS 在 Y 方向进行分景景之间有25%的重叠
// 默认分景大小:
// MSS 2336 * 2336
// PAN 9344 * 9344
func (r *Registrator) SubScenes() (panScenes []*Scene, mssScenes []*Scene, err error) {
hPAN := (9344 - 2336)
hPANOverlap := 2336
panScenesCnt := r.PanHeight / hPAN
for i := 0; i < panScenesCnt; i++ {
y1 := (i+1)*hPAN + hPANOverlap
if y1 > r.PanHeight {
y1 = r.PanHeight
}
scene := &Scene{
Width: 9344,
Height: y1 - i*hPAN,
X: 0,
Y: i * hPAN,
}
mat := r.PanImage.Region(image.Rect(0, i*hPAN, 9344, y1))
scene.Mat = append(scene.Mat, mat)
panScenes = append(panScenes, scene)
}
hMSS := hPAN / 4
hMSSOverlap := hPANOverlap / 4
mssScenesCnt := r.MssHeight / hMSS
for i := 0; i < mssScenesCnt; i++ {
y1 := (i+1)*hMSS + hMSSOverlap
if y1 > r.MssHeight {
y1 = r.MssHeight
}
scene := &Scene{
Width: 2336,
Height: y1 - i*hMSS,
X: 0,
Y: i * hMSS,
}
for band := 0; band < 4; band++ {
mat := r.registeredMssImages[band].Region(image.Rect(0, i*hMSS, 2336, y1))
scene.Mat = append(scene.Mat, mat)
}
mssScenes = append(mssScenes, scene)
}
if len(panScenes) != len(mssScenes) {
log.Error("pan and mss scenes count not match")
err = fmt.Errorf("pan and mss scenes count not match")
}
return panScenes, mssScenes, err
}
func (r *Registrator) SaveScenesToTiff(panScenes []*Scene, mssScenes []*Scene) error {
for i, scene := range panScenes {
dir := filepath.Join(r.Params.OutputDir, fmt.Sprintf("%03d", i+1))
os.MkdirAll(dir, 0755)
tiff := filepath.Base(r.Params.PanTiffFile)
tiff = strings.TrimSuffix(tiff, ".tiff")
filename := fmt.Sprintf("%s_%03d.tiff", tiff, i+1)
scene.Tiff = filepath.Join(dir, filename)
err := savePanToGDALGTiff(scene.Mat[0], scene.Tiff)
if err != nil {
log.Errorf("save pan scene %d to tiff failed: %v", i+1, err)
return err
}
}
for i, scene := range mssScenes {
dir := filepath.Join(r.Params.OutputDir, fmt.Sprintf("%03d", i+1))
os.MkdirAll(dir, 0755)
tiff := filepath.Base(r.Params.MssTiffFile)
tiff = strings.TrimSuffix(tiff, ".tiff")
filename := fmt.Sprintf("%s_%03d.tiff", tiff, i+1)
scene.Tiff = filepath.Join(dir, filename)
rgbirImage, _ := r.MergeMSSToBGRNIR(scene.Mat)
err := SaveBGRToGDALGTiff(rgbirImage,
4, 5,
[]godal.ColorInterp{godal.CIBlue, godal.CIGreen, godal.CIRed, godal.CIAlpha},
scene.Tiff)
if err != nil {
log.Errorf("save mss scene %d to tiff failed: %v", i+1, err)
return err
}
}
return nil
}
func (r *Registrator) DoScenePansharpen(panScenes []*Scene, mssScenes []*Scene) error {
for i := 0; i < len(panScenes); i++ {
// rgbirImg, _ := r.MergeMSSToBGRNIR(mssScenes[i].Mat)
// ihsImage := PansharpenIHS(panScenes[i].Mat[0], rgbirImg)
// path := strings.TrimSuffix(r.Params.MssTiffFile, ".tiff")
// filename := fmt.Sprintf("%s_%03d_FUS.tiff", path, i+1)
// SaveBGRToGDALGTiff(ihsImage, 3, 1.25, filename)
GDALPansharpen(panScenes[i].Tiff, mssScenes[i].Tiff)
}
return nil
}
func (r *Registrator) MergeMSSToBGRNIR(channels []gocv.Mat) (gocv.Mat, error) {
var rgbirImage gocv.Mat
if len(channels) != 4 {
return rgbirImage, fmt.Errorf("mss channels count not match")
}
width := channels[0].Cols()
height := channels[0].Rows()
// 创建合并后的图像RGBIR
rgbirImage = gocv.NewMatWithSize(height, width, gocv.MatTypeCV16UC4) // 4通道16位
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
blue := channels[0].GetShortAt(y, x)
green := channels[1].GetShortAt(y, x)
red := channels[2].GetShortAt(y, x)
ir := channels[3].GetShortAt(y, x)
rgbirImage.SetShortAt(y, x*4+0, blue)
rgbirImage.SetShortAt(y, x*4+1, green)
rgbirImage.SetShortAt(y, x*4+2, red)
rgbirImage.SetShortAt(y, x*4+3, ir)
}
}
return rgbirImage, nil
}