-
Notifications
You must be signed in to change notification settings - Fork 1
/
blobcounter.min.js
3 lines (2 loc) · 965 Bytes
/
blobcounter.min.js
1
2
3
/*! blobcounter.js v1.0 | (C) 2015 Amir Keshavarz | MIT licensed | https://github.com/satrobit/blobcounter.js */
function blobcounter(r){this.canvas=r}blobcounter.prototype.detect=function(r){for(var h=this.canvas,o=h.getContext("2d"),n=o.getImageData(0,0,h.width,h.height),f=h.height,l=h.width,g=[],y=0;f>y;y++)for(var i=0;l>i;i++){var v=l*y+i<<2,p=l*y+i;if(0==n.data[v]){for(var u=[],c=0;c<g.length;c++){var d=y-r,b=y+1,x=i-r,M=i+r+1;t:for(var w=d;b>w;w++)for(var k=x;M>k;k++){var v=l*w+k,A=g[c].indexOf(v);if(-1!=A){u.push(c);break t}}}if(0==u.length)g.push([p]);else if(1==u.length)g[u[0]].push(p);else if(u.length>1){for(var C=1;C<u.length;C++)Array.prototype.push.apply(g[u[0]],g[u[C]]),g.splice(u[C],1);g[u[0]].push(p)}}}for(var D=[],I=0;I<g.length;I++){for(var O=0,j=0,q=0;q<g[I].length;q++){var y=Math.floor(g[I][q]/l),i=g[I][q]-y*l;O+=i,j+=y}var z=Math.round(O/g[I].length),B=Math.round(j/g[I].length);D.push({cog:{x:z,y:B},mess:g[I].length})}return D};