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