contours.ts
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -76,2 +76,6 @@ import { ImageDataLike } from './types/ImageDataLike'; | ||
extract(): Point[][]; | ||
/** | ||
* Applies threshold to image data | ||
*/ | ||
threshold(): ContourFinder; | ||
} |
@@ -5,2 +5,6 @@ 'use strict'; | ||
function approximate() { | ||
console.log('yay'); | ||
} | ||
function _extends() { | ||
@@ -248,2 +252,10 @@ _extends = Object.assign || function (target) { | ||
return contours; | ||
} | ||
/** | ||
* Applies threshold to image data | ||
*/ | ||
; | ||
_proto.threshold = function threshold() { | ||
return this; | ||
}; | ||
@@ -254,3 +266,29 @@ | ||
// https://rosettacode.org/wiki/Ramer-Douglas-Peucker_line_simplification#JavaScript | ||
function RDP(contour, eps) { | ||
var lastIndex = contour.length - 1; | ||
var firstPoint = contour[0]; | ||
var lastPoint = contour[lastIndex]; | ||
var x21 = lastPoint.x - firstPoint.x; | ||
var y21 = lastPoint.y - firstPoint.y; | ||
var _contour$slice$map$re = contour.slice(1, lastIndex).map(function (p) { | ||
return Math.abs(y21 * p.x - x21 * p.y + lastPoint.x * firstPoint.y - lastPoint.y * firstPoint.x); | ||
}).reduce(function (p, c, i) { | ||
var v = Math.max(p[0], c); | ||
return [v, v === p[0] ? p[1] : i + 1]; | ||
}, [-1, 0]), | ||
dMax = _contour$slice$map$re[0], | ||
x = _contour$slice$map$re[1]; | ||
if (dMax > eps) { | ||
return [].concat(RDP(contour.slice(0, x + 1), eps), RDP(contour.slice(x), eps).slice(1)); | ||
} | ||
return [contour[0], contour[lastIndex]]; | ||
} | ||
exports.ContourFinder = ContourFinder; | ||
exports.RDP = RDP; | ||
exports.approximate = approximate; | ||
//# sourceMappingURL=contours.ts.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";function t(){return(t=Object.assign||function(t){for(var i=1;i<arguments.length;i++){var r=arguments[i];for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])}return t}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});var i={"1,0":{x:1,y:1},"1,1":{x:0,y:1},"0,1":{x:-1,y:1},"-1,1":{x:-1,y:0},"-1,0":{x:-1,y:-1},"-1,-1":{x:0,y:-1},"0,-1":{x:1,y:-1},"1,-1":{x:1,y:0}};exports.ContourFinder=function(){function r(t){this.visited={},this.data=t.data,this.width=t.width,this.height=t.height}var e=r.prototype;return e.pointToIndex=function(t){return t.y*this.width+t.x},e.nextClockwise=function(t){var r=t.previous,e=t.boundary,o=t.start,s=void 0===o?r:o,n=i[r.x-e.x+","+(r.y-e.y)],h={x:e.x+n.x,y:e.y+n.y};return h.x<0||h.y<0||h.x>=this.width||h.y>=this.height?this.nextClockwise({previous:h,boundary:e,start:s}):h.x===s.x&&h.y===s.y?{previous:h,boundary:e}:0===this.data[this.pointToIndex(h)]?{previous:r,boundary:h}:this.nextClockwise({previous:h,boundary:e,start:s})},e.traceContour=function(i){var r=[t({},i)],e={x:i.x,y:i.y+1},o=t({},i),s=t({},e);do{var n=this.nextClockwise({previous:s,boundary:o});s=n.previous;var h=this.pointToIndex(o=n.boundary);this.visited[h]||(r.push(o),this.visited[h]=!0)}while(s.x!==e.x||s.y!==e.y||o.x!==i.x||o.y!==i.y);return r},e.extract=function(){for(var t=[],i=!1,r=0;r<this.width;r+=1)for(var e=this.height-1;e>=0;e-=1){var o=this.pointToIndex({x:r,y:e});0===this.data[o]?this.visited[o]||i?i=!0:(this.visited[o]=!0,t.push(this.traceContour({x:r,y:e}))):i=!1}return t},r}(); | ||
"use strict";function t(){return(t=Object.assign||function(t){for(var i=1;i<arguments.length;i++){var r=arguments[i];for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])}return t}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});var i={"1,0":{x:1,y:1},"1,1":{x:0,y:1},"0,1":{x:-1,y:1},"-1,1":{x:-1,y:0},"-1,0":{x:-1,y:-1},"-1,-1":{x:0,y:-1},"0,-1":{x:1,y:-1},"1,-1":{x:1,y:0}};exports.ContourFinder=function(){function r(t){this.visited={},this.data=t.data,this.width=t.width,this.height=t.height}var e=r.prototype;return e.pointToIndex=function(t){return t.y*this.width+t.x},e.nextClockwise=function(t){var r=t.previous,e=t.boundary,n=t.start,o=void 0===n?r:n,s=i[r.x-e.x+","+(r.y-e.y)],a={x:e.x+s.x,y:e.y+s.y};return a.x<0||a.y<0||a.x>=this.width||a.y>=this.height?this.nextClockwise({previous:a,boundary:e,start:o}):a.x===o.x&&a.y===o.y?{previous:a,boundary:e}:0===this.data[this.pointToIndex(a)]?{previous:r,boundary:a}:this.nextClockwise({previous:a,boundary:e,start:o})},e.traceContour=function(i){var r=[t({},i)],e={x:i.x,y:i.y+1},n=t({},i),o=t({},e);do{var s=this.nextClockwise({previous:o,boundary:n});o=s.previous;var a=this.pointToIndex(n=s.boundary);this.visited[a]||(r.push(n),this.visited[a]=!0)}while(o.x!==e.x||o.y!==e.y||n.x!==i.x||n.y!==i.y);return r},e.extract=function(){for(var t=[],i=!1,r=0;r<this.width;r+=1)for(var e=this.height-1;e>=0;e-=1){var n=this.pointToIndex({x:r,y:e});0===this.data[n]?this.visited[n]||i?i=!0:(this.visited[n]=!0,t.push(this.traceContour({x:r,y:e}))):i=!1}return t},e.threshold=function(){return this},r}(),exports.RDP=function t(i,r){var e=i.length-1,n=i[0],o=i[e],s=o.x-n.x,a=o.y-n.y,h=i.slice(1,e).map((function(t){return Math.abs(a*t.x-s*t.y+o.x*n.y-o.y*n.x)})).reduce((function(t,i,r){var e=Math.max(t[0],i);return[e,e===t[0]?t[1]:r+1]}),[-1,0]),u=h[1];return h[0]>r?[].concat(t(i.slice(0,u+1),r),t(i.slice(u),r).slice(1)):[i[0],i[e]]},exports.approximate=function(){console.log("yay")}; | ||
//# sourceMappingURL=contours.ts.cjs.production.min.js.map |
@@ -0,1 +1,5 @@ | ||
function approximate() { | ||
console.log('yay'); | ||
} | ||
function _extends() { | ||
@@ -243,2 +247,10 @@ _extends = Object.assign || function (target) { | ||
return contours; | ||
} | ||
/** | ||
* Applies threshold to image data | ||
*/ | ||
; | ||
_proto.threshold = function threshold() { | ||
return this; | ||
}; | ||
@@ -249,3 +261,27 @@ | ||
export { ContourFinder }; | ||
// https://rosettacode.org/wiki/Ramer-Douglas-Peucker_line_simplification#JavaScript | ||
function RDP(contour, eps) { | ||
var lastIndex = contour.length - 1; | ||
var firstPoint = contour[0]; | ||
var lastPoint = contour[lastIndex]; | ||
var x21 = lastPoint.x - firstPoint.x; | ||
var y21 = lastPoint.y - firstPoint.y; | ||
var _contour$slice$map$re = contour.slice(1, lastIndex).map(function (p) { | ||
return Math.abs(y21 * p.x - x21 * p.y + lastPoint.x * firstPoint.y - lastPoint.y * firstPoint.x); | ||
}).reduce(function (p, c, i) { | ||
var v = Math.max(p[0], c); | ||
return [v, v === p[0] ? p[1] : i + 1]; | ||
}, [-1, 0]), | ||
dMax = _contour$slice$map$re[0], | ||
x = _contour$slice$map$re[1]; | ||
if (dMax > eps) { | ||
return [].concat(RDP(contour.slice(0, x + 1), eps), RDP(contour.slice(x), eps).slice(1)); | ||
} | ||
return [contour[0], contour[lastIndex]]; | ||
} | ||
export { ContourFinder, RDP, approximate }; | ||
//# sourceMappingURL=contours.ts.esm.js.map |
@@ -0,1 +1,3 @@ | ||
export * from './approximate'; | ||
export * from './contours'; | ||
export * from './rdp'; |
@@ -14,9 +14,14 @@ { | ||
"eslint-config-prettier": "^6.15.0", | ||
"eslint-config-react-app": "^6.0.0", | ||
"eslint-config-standard": "^16.0.2", | ||
"eslint-config-standard-with-typescript": "^19.0.1", | ||
"eslint-plugin-flowtype": "^5.2.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-jest": "^24.1.3", | ||
"eslint-plugin-jsx-a11y": "^6.4.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-react": "^7.21.5", | ||
"eslint-plugin-react-hooks": "^4.2.0", | ||
"eslint-plugin-standard": "^4.1.0", | ||
@@ -69,3 +74,3 @@ "husky": "^4.3.0", | ||
"build": "tsdx build", | ||
"lint": "tsdx lint", | ||
"lint": "tsdx lint --fix", | ||
"prepare": "tsdx build", | ||
@@ -88,3 +93,3 @@ "size": "size-limit", | ||
"typings": "dist/index.d.ts", | ||
"version": "0.1.2" | ||
"version": "0.1.3" | ||
} |
@@ -1,8 +0,8 @@ | ||
# contours.ts | ||
# Contours.ts | ||
Extract shapes & contours in an image, for browsers and node.js. | ||
[](https://travis-ci.org/mubaidr/contours.ts) | ||
[](https://codebeat.co/projects/github-com-mubaidr-contours.ts-master) | ||
[](https://codecov.io/gh/mubaidr/contours.ts) | ||
 | ||
[](https://codebeat.co/projects/github-com-mubaidr-contours-ts-master) | ||
[](https://codecov.io/gh/mubaidr/contours.ts) | ||
[](#contributors) | ||
@@ -25,5 +25,25 @@ | ||
const contours = new ContourFinder(data).extract() | ||
/* | ||
if `imageData` is like `{ | ||
data: number[] | Uint8ClampedArray | ||
width: number | ||
height: number | ||
}` | ||
*/ | ||
console.log(contours) // array of points | ||
const contours = new ContourFinder(imageData).threshold().extract() | ||
console.log(contours) | ||
/* | ||
- `contours` is collection of contours found in image | ||
- each contour is collection of points. | ||
e.g. | ||
[ | ||
[{x: 0, y: 0}, {x: 1, y: 0}], //contour | ||
[{x: 0, y: 1}, {x: 0, y: 0}, {x: 1, y: 0}, {x: 1, y: 1}] // another contour | ||
] | ||
*/ | ||
``` | ||
@@ -30,0 +50,0 @@ |
@@ -1,1 +0,3 @@ | ||
// export function approximate() {} | ||
export function approximate(): void { | ||
console.log('yay') | ||
} |
@@ -222,2 +222,10 @@ import { ImageDataLike } from './types/ImageDataLike' | ||
} | ||
/** | ||
* Applies threshold to image data | ||
*/ | ||
public threshold(): ContourFinder { | ||
return this | ||
} | ||
} |
@@ -1,2 +0,3 @@ | ||
// export * from './approximate' | ||
export * from './approximate' | ||
export * from './contours' | ||
export * from './rdp' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
70351
24
882
71
28