pansharpen by ESRI method

This commit is contained in:
nuknal
2024-09-12 17:25:40 +08:00
parent 7bd0896f05
commit 8a2d339d11
5 changed files with 182 additions and 7 deletions

View File

@@ -1,6 +1,8 @@
package utils
import (
"os"
"path/filepath"
"time"
"github.com/airbusgeo/godal"
@@ -12,7 +14,7 @@ import (
func SavePanToGDALGTiff(pan gocv.Mat, topLeftX, topLeftY float64, tiffFile string, resolution float64) error {
// log.Println("Saving PAN image to TIFF file:", tiffFile)
os.MkdirAll(filepath.Dir(tiffFile), 0755)
width := pan.Cols()
height := pan.Rows()
@@ -60,6 +62,8 @@ func SaveBGRToGDALGTiff(bgr gocv.Mat,
width := bgr.Cols()
height := bgr.Rows()
os.MkdirAll(filepath.Dir(tiffFile), 0755)
// 创建一个二维切片来存储图像数据
data := make([][]uint16, bands)
for i := range data {
@@ -174,3 +178,46 @@ func ReadTiff(tifFile string) ([][][]float32, error) {
}
return data, nil
}
func ReadTifftoMat(ftiff string) (*gocv.Mat, error) {
ds, err := godal.Open(ftiff)
if err != nil {
log.Printf("Error opening TIFF file %s: %v", ftiff, err)
return nil, err
}
defer ds.Close()
bands := ds.Bands()
// 获取图像大小
width := bands[0].Structure().SizeX
height := bands[0].Structure().SizeY
bandsCnt := len(bands)
var img gocv.Mat
if bandsCnt == 1 {
img = gocv.NewMatWithSize(height, width, gocv.MatTypeCV16UC1)
} else {
img = gocv.NewMatWithSize(height, width, gocv.MatTypeCV16UC4)
}
// 读取每个波段并存储到图像矩阵中
for i := 0; i < bandsCnt; i++ {
band := bands[i]
data := make([]uint16, width*height)
err = band.Read(0, 0, data, width, height)
if err != nil {
return nil, err
}
// 将 16 位数据存储到图像的相应通道
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
value := data[y*width+x]
img.SetShortAt(y, x*bandsCnt+i, int16(value))
}
}
}
return &img, nil
}