标签:child push idt function turn fun data code and
用四叉树对图像分类,获取tag和key
function getMid(num) { if(num%2===0){ return num/2 }else{ return (num+1)/2 } } function getFourTree(rect,[x1,y1,x2,y2],deep) { if(deep===3){ let num=0 for(let y=y1;y<y2;y++){ for(let x=x1;x<x2;x++){ num+=rect.data[y*rect.width+x] } } return num; } if(x2-x1===1&&y2-y1===1){ return { val:rect.data[y1*rect.width+x1] } } const mx=getMid(x1+x2) const my=getMid(y1+y2) const tree={ val:0, child:[] } if(mx-x1>0&&my-y1>0){ tree.child[0]=getFourTree(rect,[x1,y1,mx,my],deep+1) if(deep===2){ tree.val+=tree.child[0]; }else{ tree.val+=tree.child[0].val; } } if(x2-mx>0&&my-y1>0){ tree.child[1]=getFourTree(rect,[mx,y1,x2,my],deep+1) if(deep===2){ tree.val+=tree.child[1]; }else{ tree.val+=tree.child[1].val; } } if(mx-x1>0&&y2-my>0){ tree.child[2]=getFourTree(rect,[x1,my,mx,y2],deep+1) if(deep===2){ tree.val+=tree.child[2]; }else{ tree.val+=tree.child[2].val; } } if(x2-mx>0&&y2-my>0){ tree.child[3]=getFourTree(rect,[mx,my,x2,y2],deep+1) if(deep===2){ tree.val+=tree.child[3]; }else{ tree.val+=tree.child[3].val; } } return tree } function getTagAndKey(rect1) { const tree=getFourTree(rect1,[0,0,rect1.width,rect1.height],0); console.log(tree) let tagArr=[]; let keyArr=[]; tree.child.forEach(function (child) { child.child.forEach(function (child2) { tagArr.push(child2.val * 4 >= child.val ? 1 : 0); child2.child.forEach(function (val) { keyArr.push(val * 4 >= child2.val ? 1 : 0); }) }) }) return [tagArr.join(‘‘),keyArr.join(‘‘)] }
const tree=getTagAndKey(rect1)
console.log(tree)
[ ‘1100110001110011‘,
‘1100110111110101110011001111111111110101001101111111111100110011‘ ]
标签:child push idt function turn fun data code and
原文地址:https://www.cnblogs.com/caoke/p/13132087.html