2023-11-13-dist
This commit is contained in:
159
src/tool/GO.ts
159
src/tool/GO.ts
@@ -8,6 +8,7 @@ export default {
|
||||
var max = obj?.max == undefined ? 5 : obj.max;
|
||||
var img = new Image();
|
||||
img.src = url;
|
||||
let arr:any = []
|
||||
img.onload = () => {
|
||||
const width = img.width;
|
||||
const height = img.height;
|
||||
@@ -40,10 +41,168 @@ export default {
|
||||
ratio:parseInt(String((num/ (width * height) * 100).toFixed(6)))
|
||||
}
|
||||
ratio.push(rgb)
|
||||
// console.log(ratio);
|
||||
|
||||
// console.log((num/ (width * height) * 100).toFixed(2));
|
||||
|
||||
}
|
||||
|
||||
|
||||
for (let i = 0; i < data.data.length; i+=4) {
|
||||
var r = data.data[i];
|
||||
var g = data.data[i+1];
|
||||
var b = data.data[i+2];
|
||||
var a = data.data[i+3];
|
||||
arr.push([r,g,b,a])
|
||||
}
|
||||
|
||||
|
||||
// interface Point {
|
||||
// x: number;
|
||||
// y: number;
|
||||
// z: number;
|
||||
// }
|
||||
|
||||
// interface Centroid extends Point {
|
||||
// points: Point[];
|
||||
// }
|
||||
|
||||
// // 计算两个点之间的欧氏距离
|
||||
// function euclideanDistance(a: Point, b: Point): number {
|
||||
// const dx = a.x - b.x;
|
||||
// const dy = a.y - b.y;
|
||||
// const dz = a.z - b.z;
|
||||
// return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
||||
// }
|
||||
|
||||
// // 使用 K-means 算法获取主要的 RGB 值
|
||||
// function getMainRGBValues(rgbData: number[][], k: number): number[][] {
|
||||
// const points: Point[] = rgbData.map(rgb => ({
|
||||
// x: rgb[0],
|
||||
// y: rgb[1],
|
||||
// z: rgb[2]
|
||||
// }));
|
||||
|
||||
// // 随机选择初始聚类中心
|
||||
// const centroids: Centroid[] = [];
|
||||
// for (let i = 0; i < k; i++) {
|
||||
// const randomPoint = points[Math.floor(Math.random() * points.length)];
|
||||
// centroids.push({ ...randomPoint, points: [] });
|
||||
// }
|
||||
|
||||
// let converged = false;
|
||||
|
||||
// while (!converged) {
|
||||
// // 将每个点分配到最近的聚类中心
|
||||
// points.forEach(point => {
|
||||
// let minDistance = Infinity;
|
||||
// let closestCentroid: Centroid | undefined;
|
||||
|
||||
// centroids.forEach(centroid => {
|
||||
// const distance = euclideanDistance(point, centroid);
|
||||
// if (distance < minDistance) {
|
||||
// minDistance = distance;
|
||||
// closestCentroid = centroid;
|
||||
// }
|
||||
// });
|
||||
|
||||
// if (closestCentroid) {
|
||||
// closestCentroid.points.push(point);
|
||||
// }
|
||||
// });
|
||||
|
||||
// // 更新聚类中心的位置
|
||||
// const newCentroids: Centroid[] = [];
|
||||
// centroids.forEach(centroid => {
|
||||
// const { points } = centroid;
|
||||
|
||||
// if (points.length > 0) {
|
||||
// const sumX = points.reduce((acc, point) => acc + point.x, 0);
|
||||
// const sumY = points.reduce((acc, point) => acc + point.y, 0);
|
||||
// const sumZ = points.reduce((acc, point) => acc + point.z, 0);
|
||||
|
||||
// const newX = sumX / points.length;
|
||||
// const newY = sumY / points.length;
|
||||
// const newZ = sumZ / points.length;
|
||||
|
||||
// newCentroids.push({ x: newX, y: newY, z: newZ, points: [] });
|
||||
// } else {
|
||||
// newCentroids.push(centroid);
|
||||
// }
|
||||
// });
|
||||
|
||||
// // 检查聚类中心是否发生变化
|
||||
// converged = JSON.stringify(centroids) === JSON.stringify(newCentroids);
|
||||
// centroids.splice(0, centroids.length, ...newCentroids);
|
||||
// }
|
||||
|
||||
|
||||
// // 找到每个聚类的平均颜色值
|
||||
// const mainRGBValues = centroids.map(centroid => [
|
||||
// Math.round(centroid.x),
|
||||
// Math.round(centroid.y),
|
||||
// Math.round(centroid.z)
|
||||
// ]);
|
||||
|
||||
// return mainRGBValues;
|
||||
// }
|
||||
|
||||
// // 示例使用
|
||||
// const rgbData: number[][] = [
|
||||
// [255, 0, 0], // 红色
|
||||
// [0, 255, 0], // 绿色
|
||||
// [0, 0, 255], // 蓝色
|
||||
// // 更多 RGB 数据...
|
||||
// ];
|
||||
|
||||
// const k = 1; // 假设我们想要找到三个主要的 RGB 值
|
||||
// const mainRGBValues = getMainRGBValues(rgbData, k);
|
||||
// console.log(mainRGBValues); // 输出主要的 RGB 值
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//获取颜色做多的值
|
||||
// function findMostFrequent(arr:any) {
|
||||
// let map:any = {};
|
||||
// let maxCount = 0;
|
||||
// let mostFrequent;
|
||||
|
||||
// for (let item of arr) {
|
||||
// if (map[item] === undefined) {
|
||||
// map[item] = 1;
|
||||
// } else {
|
||||
// map[item]++;
|
||||
// }
|
||||
// if (map[item] > maxCount) {
|
||||
// maxCount = map[item];
|
||||
// mostFrequent = item;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return mostFrequent;
|
||||
// }
|
||||
// console.log(arr);
|
||||
|
||||
// console.log(findMostFrequent(arr));
|
||||
|
||||
resolve({
|
||||
width,
|
||||
height,
|
||||
|
||||
Reference in New Issue
Block a user