博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
颜色分类函数
阅读量:4577 次
发布时间:2019-06-08

本文共 2511 字,大约阅读时间需要 8 分钟。

逻辑原理:

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]
parseInt(item)); let running=false; mat1.rowMap(function (item,r,c) {
if(typeof item==="number"){return item;} if(maxTagNum-1<=num||isNearColor(item,bgColor)){
return num; }else{
running=true; return item; } }) if(running){
return sortColor(num+1); } return num+1; } return sortColor(0); } async function init() {
const pixels=await getPixelsSync('1.jpg'); console.log(pixels) const [w,h]=pixels.shape; //1定义矩阵 const mat1=new Matrix([],h,w); mat1.rowMap(function (item,r,c) {
return getColor(c,r,pixels); }) console.log(sortMatColor(mat1,3)) //生产0、1、2种颜色类别 console.log(mat1.toString()) } init() // scanRound(0,0,100,100,function (x,y) {
// return 1; // },function (x,y) {
// console.log(x,y); // })

 

转载于:https://www.cnblogs.com/caoke/p/11097220.html

你可能感兴趣的文章
PCA(主分量分析)-转载
查看>>
Hadoop 2.0命令手册
查看>>
SVN 提交项目.a文件缺失 cornerstone设置方法
查看>>
git push不用重复输入用户名和密码(解决方案)
查看>>
委托与事件
查看>>
实现POST服务器
查看>>
Linux下的tar压缩解压缩命令详解
查看>>
大数据项目测试<二>项目的测试工作
查看>>
linux网路IP.设定主机名.ssh .bash命令&通配符
查看>>
mysql 行列动态转换(列联表,交叉表)
查看>>
软件开发模式
查看>>
装机散记--某鱼矿卡和某宝矿卡
查看>>
(二)辗转相除法求最大公约数
查看>>
服务器负载均衡的基本功能和实现原理
查看>>
Android中导入Unity项目,界面点击事件失去焦点问题
查看>>
day16——oracle灾备1
查看>>
ngnix简单使用
查看>>
Ajax的四种请求方式
查看>>
【转】提升你的Java应用性能:改善数据处理
查看>>
ES6之class 中 constructor 方法 和 super 的作用
查看>>