cloud cover by kmeans

This commit is contained in:
nuknal
2024-11-11 17:55:57 +08:00
parent b5430d9035
commit 98f12606b6
2 changed files with 21 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ import (
"time"
log "github.com/sirupsen/logrus"
"gocv.io/x/gocv"
)
const (
@@ -36,6 +37,20 @@ func CloudPercentByKMeans(imagePath string) float64 {
return 0.0
}
return computeCloudCoverByKMeans(img)
}
func CloudPercentByKMeans2(imgMat gocv.Mat) float64 {
img, err := imgMat.ToImage()
if err != nil {
log.Errorf("无法将Mat转换为Image: %v", err)
return 0.0
}
return computeCloudCoverByKMeans(img)
}
func computeCloudCoverByKMeans(img image.Image) float64 {
log.Info("cloud cover computing...")
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
pixels := make([]Pixel, 0, width*height)
@@ -62,7 +77,7 @@ func CloudPercentByKMeans(imagePath string) float64 {
newCentroids := updateCentroids(pixels, assignments)
if hasConverged(centroids, newCentroids) {
log.Printf("在第 %d 次迭代后收敛\n", iter+1)
log.Debugf("Convergence after the %d iteration", iter+1)
break
}
centroids = newCentroids

View File

@@ -14,6 +14,7 @@ import (
"github.com/paulmach/orb/geojson"
log "github.com/sirupsen/logrus"
"gocv.io/x/gocv"
cloudcover "starwiz.cn/sjy01/image-proc/pkg/cloud-cover"
"starwiz.cn/sjy01/image-proc/pkg/config"
"starwiz.cn/sjy01/image-proc/pkg/fusion"
"starwiz.cn/sjy01/image-proc/pkg/payload"
@@ -231,11 +232,13 @@ func (r *ImgProc) OutputL1A(panScenes []*Scene, mssScenes []*Scene) error {
return err
}
jpg := strings.Replace(scene.Tiff, ".tiff", ".jpg", 1)
GTiffToJPG(scene.Tiff, jpg, "MSS", false)
scene.Meta.CloudRate = cloudcover.CloudPercentByKMeans(jpg)
scene.Meta.DataSize = sizeOfFile(scene.Tiff)
metaFile := strings.Replace(scene.Tiff, ".tiff", ".meta.xml", 1)
writeProductMeta(scene.Meta, metaFile)
jpg := strings.Replace(scene.Tiff, ".tiff", ".jpg", 1)
GTiffToJPG(scene.Tiff, jpg, "MSS", false)
r.report.Scenes = append(r.report.Scenes, ReportScene{
Name: scene.SceneId,
TiffData: scene.Tiff,