-
Notifications
You must be signed in to change notification settings - Fork 15
/
canny.min.js
1 lines (1 loc) · 4.31 KB
/
canny.min.js
1
(function(){var t,r,a;a={},a.generateMatrix=function(t,r,a){var n,i,e,o,h,u,s;for(n=[],i=o=0,u=t-1;u>=0?u>=o:o>=u;i=u>=0?++o:--o)for(n[i]=[],e=h=0,s=r-1;s>=0?s>=h:h>=s;e=s>=0?++h:--h)n[i][e]=a;return n},r=function(){function t(t,r){this.width=t,this.height=r,this.data=a.generateMatrix(this.width,this.height,0)}return t.prototype.loadCanvas=function(t){var r,a,n,i,e,o,h,u,s,f,d;for(a=t.getContext("2d"),h=a.getImageData(0,0,t.width,t.height).data,u=0,s=0,e=f=0,d=h.length;d>f;e=f+=4)n=h[e],o=h[e],i=h[e+1],r=h[e+2],this.data[u][s]=Math.round(.298*o+.586*i+.114*r),u===this.width-1?(u=0,s+=1):u+=1;return this},t.prototype.getNeighbors=function(t,r,n){var i,e,o,h,u,s,f,d,c;for(o=a.generateMatrix(n,n,0),i=s=0,d=n-1;d>=0?d>=s:s>=d;i=d>=0?++s:--s)for(o[i]=[],e=f=0,c=n-1;c>=0?c>=f:f>=c;e=c>=0?++f:--f)h=t-(n-1)/2+i,u=r-(n-1)/2+e,o[i][e]=this.data[h]&&this.data[h][u]?this.data[h][u]:0;return o},t.prototype.eachPixel=function(t,r){var a,n,i,e,o,h,u,s;for(i=o=0,u=this.width-1;u>=0?u>=o:o>=u;i=u>=0?++o:--o)for(e=h=0,s=this.height-1;s>=0?s>=h:h>=s;e=s>=0?++h:--h)a=this.data[i][e],n=this.getNeighbors(i,e,t),r(i,e,a,n);return this},t.prototype.toImageDataArray=function(){var t,r,a,n,i,e,o,h,u;for(t=[],n=i=0,h=this.height-1;h>=0?h>=i:i>=h;n=h>=0?++i:--i)for(a=e=0,u=this.width-1;u>=0?u>=e:e>=u;a=u>=0?++e:--e){for(r=o=0;2>=o;r=++o)t.push(this.data[a][n]);t.push(255)}return t},t.prototype.copy=function(){var r,a,n,i,e,o,h;for(r=new t(this.width,this.height),a=i=0,o=this.width-1;o>=0?o>=i:i>=o;a=o>=0?++i:--i)for(n=e=0,h=this.height-1;h>=0?h>=e:e>=h;n=h>=0?++e:--e)r.data[a][n]=this.data[a][n];return r.width=this.width,r.height=this.height,r},t.prototype.drawOn=function(t){var r,a,n,i,e,o,h;for(a=t.getContext("2d"),i=a.createImageData(t.width,t.height),h=this.toImageDataArray(),n=e=0,o=h.length;o>e;n=++e)r=h[n],i.data[n]=r;return a.putImageData(i,0,0)},t.prototype.fill=function(t){var r,a,n,i,e;for(e=[],a=n=0,i=this.height-1;i>=0?i>=n:n>=i;a=i>=0?++n:--n)e.push(function(){var n,i,e;for(e=[],r=n=0,i=this.width-1;i>=0?i>=n:n>=i;r=i>=0?++n:--n)e.push(this.data[r][a]=t);return e}.call(this));return e},t}(),t={},t.gaussianBlur=function(r,a,n){var i,e;return null==a&&(a=1.4),null==n&&(n=3),e=t.generateKernel(a,n),i=r.copy(),i.fill(0),r.eachPixel(n,function(t,r,a,o){var h,u,s;for(h=0,s=[];n-1>=h;){for(u=0;n-1>=u;)i.data[t][r]+=o[h][u]*e[h][u],u++;s.push(h++)}return s}),i},t.generateKernel=function(t,r){var n,i,e,o,h,u,s,f,d,c,l,g,p,v,w,y,x;for(u=t,n=2.718,h=a.generateMatrix(r,r,0),s=0,e=c=0,v=r-1;v>=0?v>=c:c>=v;e=v>=0?++c:--c)for(f=-(r-1)/2+e,o=l=0,w=r-1;w>=0?w>=l:l>=w;o=w>=0?++l:--l)d=-(r-1)/2+o,i=1/(2*Math.PI*u*u)*Math.pow(n,-(f*f+d*d)/(2*u*u)),h[e][o]=i,s+=i;for(e=g=0,y=r-1;y>=0?y>=g:g>=y;e=y>=0?++g:--g)for(o=p=0,x=r-1;x>=0?x>=p:p>=x;o=x>=0?++p:--p)h[e][o]=(h[e][o]/s).toFixed(3);return console.log("kernel",h),h},t.sobel=function(t){var r,a,n;return n=[[-1,0,1],[-2,0,2],[-1,0,1]],a=[[-1,-2,-1],[0,0,0],[1,2,1]],r=t.copy(),r.fill(0),t.eachPixel(3,function(t,i,e,o){var h,u,s,f,d,c;for(h=0,u=0,s=d=0;2>=d;s=++d)for(f=c=0;2>=c;f=++c)h+=n[s][f]*o[s][f],u+=a[s][f]*o[s][f];return r.data[t][i]=Math.sqrt(h*h+u*u)}),r},t.nonMaximumSuppression=function(t){var r;return r=t.copy(),r.fill(0),t.eachPixel(3,function(t,a,n,i){return r.data[t][a]=i[1][1]>i[0][1]&&i[1][1]>i[2][1]?i[1][1]:0,r.data[t][a]=i[1][1]>i[0][2]&&i[1][1]>i[2][0]?i[1][1]:0,r.data[t][a]=i[1][1]>i[1][0]&&i[1][1]>i[1][2]?i[1][1]:0,r.data[t][a]=i[1][1]>i[0][0]&&i[1][1]>i[2][2]?i[1][1]:0}),r},t.hysteresis=function(t,r,a){var n,i,e,o,h;return n=t.copy(),e=function(t){return t>r},i=function(t){return r>=t&&t>=a},o=function(t){return a>t},t.eachPixel(3,function(t,r,a){return e(a)?n.data[t][r]=255:o(a)||i(a)?n.data[t][r]=0:void 0}),h=function(r,a){var o,u,s,f,d;if(0!==r&&0!==a&&r!==t.width-1&&a!==t.height-1&&e(n.data[r][a])){for(s=n.getNeighbors(r,a,3),d=[],o=f=0;2>=f;o=++f)d.push(function(){var t,e;for(e=[],u=t=0;2>=t;u=++t)i(s[o][u])?(n.data[r-1+o][a-1+u]=255,e.push(h(r-1+o,a-1+u))):e.push(void 0);return e}());return d}},n.eachPixel(3,function(t,r){return h(t,r)}),n.eachPixel(1,function(t,r,a){return e(a)?void 0:n.data[t][r]=0}),n},t.canny=function(a,n,i,e,o){var h,u,s,f;return null==n&&(n=100),null==i&&(i=50),null==e&&(e=1.4),null==o&&(o=3),u=new r(a.width,a.height),u.loadCanvas(a),h=t.gaussianBlur(u,e,o),f=t.sobel(h),s=t.nonMaximumSuppression(f),t.hysteresis(s,n,i)},window.CannyJS=t,window.GrayImageData=r}).call(this);