逻辑原理:
1、找出图片种最多的一种颜色,将它的相似颜色和它分类成0;
2、找出剩下的颜色种的最多一种颜色,将它的相似颜色和它分类成1;
3、找出剩下的颜色种的最多一种颜色,将它的相似颜色和它分类成2;
如果类别限制为maxTagNum,将剩下的颜色都分类成maxTagNum
const getPixels = require("get-pixels") function getPixelsSync(filename){ return new Promise(function (resolve,reject) { getPixels(filename, function(err, pixels) { if(err) { console.log("Bad image path") reject(err) return } resolve(pixels) }) }) } const {Matrix,Fraction,Point,Line} = require("./utils/math"); function getColor(x,y,pixels) { return [ pixels.data[x*4+y*4*pixels.shape[0]], pixels.data[x*4+1+y*4*pixels.shape[0]], pixels.data[x*4+2+y*4*pixels.shape[0]], pixels.data[x*4+3+y*4*pixels.shape[0]] ] } function isNearColor(color1,color2){ if((Math.abs(color1[0]-color2[0])+Math.abs(color1[1]-color2[1])+Math.abs(color1[2]-color2[2])+Math.abs(color1[3]-color2[3]))<75){ return 1; } return 0; } //将颜色分成多少种 maxTagNum是限制最大多少种 function sortMatColor(mat1,maxTagNum){ function sortColor(num) { let map={} let maxKey; mat1.rowEach(function (item,r,c) { if(typeof item==="number"){return;} const key=item.join(',') if(map[key]===undefined){ map[key]=0; } map[key]++; if(!maxKey){ maxKey=key; }else if(map[maxKey]