修正色调排序
This commit is contained in:
		@@ -7,6 +7,7 @@ import (
 | 
				
			|||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
 | 
						"sort"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@@ -141,14 +142,41 @@ func NewSchema(config Config) (graphql.Schema, error) {
 | 
				
			|||||||
					continue
 | 
										continue
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				centers, _ := KMeans(extractColors(img), 8)
 | 
									k := 8
 | 
				
			||||||
 | 
									centers, labels := KMeans(extractColors(img), k)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// 将聚类中心和颜色数量结合,并按颜色数量降序排序
 | 
				
			||||||
 | 
									type cluster struct {
 | 
				
			||||||
 | 
										center RGB
 | 
				
			||||||
 | 
										count  int
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									clusters := make([]cluster, k)
 | 
				
			||||||
 | 
									for i := 0; i < k; i++ {
 | 
				
			||||||
 | 
										clusters[i] = cluster{center: centers[i], count: 0}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									// 统计每个聚类的颜色数量
 | 
				
			||||||
 | 
									for _, label := range labels {
 | 
				
			||||||
 | 
										clusters[label].count++
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									// 按颜色数量降序排序
 | 
				
			||||||
 | 
									sort.Slice(clusters, func(i, j int) bool {
 | 
				
			||||||
 | 
										return clusters[i].count > clusters[j].count
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
									// 返回排序后的聚类中心
 | 
				
			||||||
 | 
									sortedCenters := make([]RGB, k)
 | 
				
			||||||
 | 
									for i, c := range clusters {
 | 
				
			||||||
 | 
										sortedCenters[i] = c.center
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									fmt.Println("聚类后的颜色数量:", clusters)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if err := db.Table("web_images").Where("id = ?", item.ID).Updates(map[string]interface{}{
 | 
									if err := db.Table("web_images").Where("id = ?", item.ID).Updates(map[string]interface{}{
 | 
				
			||||||
					"color_0_r": centers[0].R,
 | 
										"color_0_r": sortedCenters[0].R,
 | 
				
			||||||
					"color_0_g": centers[0].G,
 | 
										"color_0_g": sortedCenters[0].G,
 | 
				
			||||||
					"color_0_b": centers[0].B,
 | 
										"color_0_b": sortedCenters[0].B,
 | 
				
			||||||
					"color_1_r": centers[1].R,
 | 
										"color_1_r": sortedCenters[1].R,
 | 
				
			||||||
					"color_1_g": centers[1].G,
 | 
										"color_1_g": sortedCenters[1].G,
 | 
				
			||||||
					"color_1_b": centers[1].B,
 | 
										"color_1_b": sortedCenters[1].B,
 | 
				
			||||||
				}).Error; err != nil {
 | 
									}).Error; err != nil {
 | 
				
			||||||
					fmt.Println("更新颜色字段失败", index, item.ID, item.Content, err)
 | 
										fmt.Println("更新颜色字段失败", index, item.ID, item.Content, err)
 | 
				
			||||||
					continue
 | 
										continue
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,6 +13,12 @@ type RGB struct {
 | 
				
			|||||||
	R, G, B int
 | 
						R, G, B int
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 根据每个聚类中心包含的颜色数量进行排序
 | 
				
			||||||
 | 
					type cluster struct {
 | 
				
			||||||
 | 
						center RGB
 | 
				
			||||||
 | 
						count  int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 计算两个 RGB 颜色的欧氏距离
 | 
					// 计算两个 RGB 颜色的欧氏距离
 | 
				
			||||||
func distance(c1, c2 RGB) float64 {
 | 
					func distance(c1, c2 RGB) float64 {
 | 
				
			||||||
	return math.Sqrt(float64((c1.R-c2.R)*(c1.R-c2.R) + (c1.G-c2.G)*(c1.G-c2.G) + (c1.B-c2.B)*(c1.B-c2.B)))
 | 
						return math.Sqrt(float64((c1.R-c2.R)*(c1.R-c2.R) + (c1.G-c2.G)*(c1.G-c2.G) + (c1.B-c2.B)*(c1.B-c2.B)))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user