edge_det 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +100 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +37 -0
- package/dist/wasm_bytes.d.ts +1 -0
- package/dist/wasm_bytes.js +1 -0
- package/package.json +11 -1
- package/.github/workflows/build.yml +0 -36
- package/Cargo.lock +0 -114
- package/Cargo.toml +0 -19
- package/edge_det-0.1.0.tgz +0 -0
- package/pnpm-workspace.yaml +0 -2
- package/scripts/build-wasm.mjs +0 -19
- package/src/lib.rs +0 -327
- package/src_ts/index.ts +0 -68
- package/tests/detection.test.ts +0 -238
- package/tsconfig.json +0 -14
- package/vite.config.ts +0 -10
- package/vitest.config.ts +0 -7
package/package.json
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "edge_det",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"url": "https://github.com/xushengfeng/edge_det"
|
|
14
|
+
},
|
|
5
15
|
"scripts": {
|
|
6
16
|
"build:wasm": "node scripts/build-wasm.mjs",
|
|
7
17
|
"build": "npm run build:wasm && tsc",
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
-
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
3
|
-
|
|
4
|
-
name: Node.js Package
|
|
5
|
-
|
|
6
|
-
on:
|
|
7
|
-
push:
|
|
8
|
-
tags:
|
|
9
|
-
- "*"
|
|
10
|
-
|
|
11
|
-
permissions:
|
|
12
|
-
id-token: write # Required for OIDC
|
|
13
|
-
contents: read
|
|
14
|
-
|
|
15
|
-
jobs:
|
|
16
|
-
publish-npm:
|
|
17
|
-
runs-on: ubuntu-latest
|
|
18
|
-
steps:
|
|
19
|
-
- uses: actions/checkout@v3
|
|
20
|
-
- name: Install Rust
|
|
21
|
-
uses: actions-rs/toolchain@v1
|
|
22
|
-
with:
|
|
23
|
-
toolchain: stable
|
|
24
|
-
profile: minimal
|
|
25
|
-
override: true
|
|
26
|
-
- run: |
|
|
27
|
-
rustup target add wasm32-unknown-unknown
|
|
28
|
-
cargo install wasm-pack
|
|
29
|
-
- uses: actions/setup-node@v3
|
|
30
|
-
with:
|
|
31
|
-
node-version: 24
|
|
32
|
-
registry-url: https://registry.npmjs.org/
|
|
33
|
-
- run: |
|
|
34
|
-
npm i
|
|
35
|
-
npm run build
|
|
36
|
-
- run: npm publish
|
package/Cargo.lock
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
# This file is automatically @generated by Cargo.
|
|
2
|
-
# It is not intended for manual editing.
|
|
3
|
-
version = 4
|
|
4
|
-
|
|
5
|
-
[[package]]
|
|
6
|
-
name = "bumpalo"
|
|
7
|
-
version = "3.20.3"
|
|
8
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
-
checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649"
|
|
10
|
-
|
|
11
|
-
[[package]]
|
|
12
|
-
name = "cfg-if"
|
|
13
|
-
version = "1.0.4"
|
|
14
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
15
|
-
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
|
16
|
-
|
|
17
|
-
[[package]]
|
|
18
|
-
name = "edge_det"
|
|
19
|
-
version = "0.1.0"
|
|
20
|
-
dependencies = [
|
|
21
|
-
"wasm-bindgen",
|
|
22
|
-
]
|
|
23
|
-
|
|
24
|
-
[[package]]
|
|
25
|
-
name = "once_cell"
|
|
26
|
-
version = "1.21.4"
|
|
27
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
28
|
-
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
|
29
|
-
|
|
30
|
-
[[package]]
|
|
31
|
-
name = "proc-macro2"
|
|
32
|
-
version = "1.0.106"
|
|
33
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
34
|
-
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
|
35
|
-
dependencies = [
|
|
36
|
-
"unicode-ident",
|
|
37
|
-
]
|
|
38
|
-
|
|
39
|
-
[[package]]
|
|
40
|
-
name = "quote"
|
|
41
|
-
version = "1.0.46"
|
|
42
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
43
|
-
checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368"
|
|
44
|
-
dependencies = [
|
|
45
|
-
"proc-macro2",
|
|
46
|
-
]
|
|
47
|
-
|
|
48
|
-
[[package]]
|
|
49
|
-
name = "rustversion"
|
|
50
|
-
version = "1.0.22"
|
|
51
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
52
|
-
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
|
53
|
-
|
|
54
|
-
[[package]]
|
|
55
|
-
name = "syn"
|
|
56
|
-
version = "2.0.118"
|
|
57
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
58
|
-
checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422"
|
|
59
|
-
dependencies = [
|
|
60
|
-
"proc-macro2",
|
|
61
|
-
"quote",
|
|
62
|
-
"unicode-ident",
|
|
63
|
-
]
|
|
64
|
-
|
|
65
|
-
[[package]]
|
|
66
|
-
name = "unicode-ident"
|
|
67
|
-
version = "1.0.24"
|
|
68
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
69
|
-
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
|
70
|
-
|
|
71
|
-
[[package]]
|
|
72
|
-
name = "wasm-bindgen"
|
|
73
|
-
version = "0.2.126"
|
|
74
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
75
|
-
checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4"
|
|
76
|
-
dependencies = [
|
|
77
|
-
"cfg-if",
|
|
78
|
-
"once_cell",
|
|
79
|
-
"rustversion",
|
|
80
|
-
"wasm-bindgen-macro",
|
|
81
|
-
"wasm-bindgen-shared",
|
|
82
|
-
]
|
|
83
|
-
|
|
84
|
-
[[package]]
|
|
85
|
-
name = "wasm-bindgen-macro"
|
|
86
|
-
version = "0.2.126"
|
|
87
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
88
|
-
checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1"
|
|
89
|
-
dependencies = [
|
|
90
|
-
"quote",
|
|
91
|
-
"wasm-bindgen-macro-support",
|
|
92
|
-
]
|
|
93
|
-
|
|
94
|
-
[[package]]
|
|
95
|
-
name = "wasm-bindgen-macro-support"
|
|
96
|
-
version = "0.2.126"
|
|
97
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
98
|
-
checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e"
|
|
99
|
-
dependencies = [
|
|
100
|
-
"bumpalo",
|
|
101
|
-
"proc-macro2",
|
|
102
|
-
"quote",
|
|
103
|
-
"syn",
|
|
104
|
-
"wasm-bindgen-shared",
|
|
105
|
-
]
|
|
106
|
-
|
|
107
|
-
[[package]]
|
|
108
|
-
name = "wasm-bindgen-shared"
|
|
109
|
-
version = "0.2.126"
|
|
110
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
111
|
-
checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24"
|
|
112
|
-
dependencies = [
|
|
113
|
-
"unicode-ident",
|
|
114
|
-
]
|
package/Cargo.toml
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
[package]
|
|
2
|
-
name = "edge_det"
|
|
3
|
-
version = "0.1.0"
|
|
4
|
-
edition = "2021"
|
|
5
|
-
|
|
6
|
-
[lib]
|
|
7
|
-
crate-type = ["cdylib"]
|
|
8
|
-
|
|
9
|
-
[dependencies]
|
|
10
|
-
wasm-bindgen = "0.2"
|
|
11
|
-
|
|
12
|
-
[package.metadata.wasm-pack.profile.release]
|
|
13
|
-
wasm-opt = false
|
|
14
|
-
|
|
15
|
-
[profile.release]
|
|
16
|
-
opt-level = "s"
|
|
17
|
-
lto = true
|
|
18
|
-
codegen-units = 1
|
|
19
|
-
strip = true
|
package/edge_det-0.1.0.tgz
DELETED
|
Binary file
|
package/pnpm-workspace.yaml
DELETED
package/scripts/build-wasm.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { execSync } from 'child_process'
|
|
2
|
-
import { readFileSync, writeFileSync } from 'fs'
|
|
3
|
-
import { join, dirname } from 'path'
|
|
4
|
-
import { fileURLToPath } from 'url'
|
|
5
|
-
|
|
6
|
-
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
7
|
-
const root = join(__dirname, '..')
|
|
8
|
-
|
|
9
|
-
console.log('Building WASM...')
|
|
10
|
-
execSync('wasm-pack build --target web --release', { cwd: root, stdio: 'inherit' })
|
|
11
|
-
|
|
12
|
-
const wasmPath = join(root, 'pkg', 'edge_det_bg.wasm')
|
|
13
|
-
const wasmBytes = readFileSync(wasmPath)
|
|
14
|
-
console.log(`WASM size: ${wasmBytes.length} bytes`)
|
|
15
|
-
|
|
16
|
-
const bytesStr = JSON.stringify(Array.from(wasmBytes))
|
|
17
|
-
const outPath = join(root, 'src_ts', 'wasm_bytes.ts')
|
|
18
|
-
writeFileSync(outPath, `export const WASM_BYTES = new Uint8Array(${bytesStr});\n`)
|
|
19
|
-
console.log(`Inlined WASM bytes to ${outPath}`)
|
package/src/lib.rs
DELETED
|
@@ -1,327 +0,0 @@
|
|
|
1
|
-
use wasm_bindgen::prelude::*;
|
|
2
|
-
|
|
3
|
-
const GAUSS: [f32; 5] = [1.0, 4.0, 6.0, 4.0, 1.0];
|
|
4
|
-
const GAUSS_SUM: f32 = 16.0;
|
|
5
|
-
|
|
6
|
-
fn grayscale(data: &[u8], w: usize, h: usize) -> Vec<f32> {
|
|
7
|
-
let n = w * h;
|
|
8
|
-
let mut g = vec![0.0f32; n];
|
|
9
|
-
for i in 0..n {
|
|
10
|
-
let o = i * 4;
|
|
11
|
-
g[i] = 0.299 * data[o] as f32 + 0.587 * data[o + 1] as f32 + 0.114 * data[o + 2] as f32;
|
|
12
|
-
}
|
|
13
|
-
g
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
fn blur(img: &[f32], w: usize, h: usize) -> Vec<f32> {
|
|
17
|
-
let mut tmp = vec![0.0f32; w * h];
|
|
18
|
-
let mut out = vec![0.0f32; w * h];
|
|
19
|
-
for y in 0..h {
|
|
20
|
-
for x in 0..w {
|
|
21
|
-
let mut s = 0.0f32;
|
|
22
|
-
for k in 0usize..5 {
|
|
23
|
-
let xx = ((x as i32) + k as i32 - 2).max(0).min(w as i32 - 1) as usize;
|
|
24
|
-
s += img[y * w + xx] * GAUSS[k];
|
|
25
|
-
}
|
|
26
|
-
tmp[y * w + x] = s / GAUSS_SUM;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
for y in 0..h {
|
|
30
|
-
for x in 0..w {
|
|
31
|
-
let mut s = 0.0f32;
|
|
32
|
-
for k in 0usize..5 {
|
|
33
|
-
let yy = ((y as i32) + k as i32 - 2).max(0).min(h as i32 - 1) as usize;
|
|
34
|
-
s += tmp[yy * w + x] * GAUSS[k];
|
|
35
|
-
}
|
|
36
|
-
out[y * w + x] = s / GAUSS_SUM;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
out
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
struct SobelResult {
|
|
43
|
-
mag: Vec<f32>,
|
|
44
|
-
dx: Vec<f32>,
|
|
45
|
-
dy: Vec<f32>,
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
fn sobel(img: &[f32], w: usize, h: usize) -> SobelResult {
|
|
49
|
-
let n = w * h;
|
|
50
|
-
let mut mag = vec![0.0f32; n];
|
|
51
|
-
let mut dx = vec![0.0f32; n];
|
|
52
|
-
let mut dy = vec![0.0f32; n];
|
|
53
|
-
for y in 1..h - 1 {
|
|
54
|
-
for x in 1..w - 1 {
|
|
55
|
-
let i = y * w + x;
|
|
56
|
-
let sx = -img[i - w - 1] - 2.0 * img[i - 1] - img[i + w - 1]
|
|
57
|
-
+ img[i - w + 1] + 2.0 * img[i + 1] + img[i + w + 1];
|
|
58
|
-
let sy = -img[i - w - 1] - 2.0 * img[i - w] - img[i - w + 1]
|
|
59
|
-
+ img[i + w - 1] + 2.0 * img[i + w] + img[i + w + 1];
|
|
60
|
-
dx[i] = sx;
|
|
61
|
-
dy[i] = sy;
|
|
62
|
-
mag[i] = (sx * sx + sy * sy).sqrt();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
SobelResult { mag, dx, dy }
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
fn nms(result: &SobelResult, w: usize, h: usize) -> Vec<f32> {
|
|
69
|
-
let mut out = vec![0.0f32; w * h];
|
|
70
|
-
for y in 1..h - 1 {
|
|
71
|
-
for x in 1..w - 1 {
|
|
72
|
-
let i = y * w + x;
|
|
73
|
-
let m = result.mag[i];
|
|
74
|
-
if m < 0.5 {
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
let gx = result.dx[i];
|
|
78
|
-
let gy = result.dy[i];
|
|
79
|
-
let abs_gx = gx.abs();
|
|
80
|
-
let abs_gy = gy.abs();
|
|
81
|
-
|
|
82
|
-
let (mut mag1, mut mag2) = (0.0f32, 0.0f32);
|
|
83
|
-
if abs_gx > abs_gy {
|
|
84
|
-
let t = abs_gy / abs_gx;
|
|
85
|
-
if gx * gy > 0.0 {
|
|
86
|
-
mag1 = result.mag[i - w - 1] * (1.0 - t) + result.mag[i - w] * t;
|
|
87
|
-
mag2 = result.mag[i + w + 1] * (1.0 - t) + result.mag[i + w] * t;
|
|
88
|
-
} else {
|
|
89
|
-
mag1 = result.mag[i - w + 1] * (1.0 - t) + result.mag[i - w] * t;
|
|
90
|
-
mag2 = result.mag[i + w - 1] * (1.0 - t) + result.mag[i + w] * t;
|
|
91
|
-
}
|
|
92
|
-
} else if abs_gy > 0.0 {
|
|
93
|
-
let t = abs_gx / abs_gy;
|
|
94
|
-
if gx * gy > 0.0 {
|
|
95
|
-
mag1 = result.mag[i - w - 1] * (1.0 - t) + result.mag[i - 1] * t;
|
|
96
|
-
mag2 = result.mag[i + w + 1] * (1.0 - t) + result.mag[i + 1] * t;
|
|
97
|
-
} else {
|
|
98
|
-
mag1 = result.mag[i - w + 1] * (1.0 - t) + result.mag[i + 1] * t;
|
|
99
|
-
mag2 = result.mag[i + w - 1] * (1.0 - t) + result.mag[i - 1] * t;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
out[i] = if m >= mag1 && m >= mag2 { m } else { 0.0 };
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
out
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
fn hysteresis(edges: &mut [u8], w: usize, h: usize) {
|
|
109
|
-
let mut stack: Vec<(i32, i32)> = Vec::new();
|
|
110
|
-
for y in 0..h {
|
|
111
|
-
for x in 0..w {
|
|
112
|
-
if edges[y * w + x] == 2 {
|
|
113
|
-
stack.push((x as i32, y as i32));
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
while let Some((x, y)) = stack.pop() {
|
|
118
|
-
for dy in -1..=1 {
|
|
119
|
-
for dx in -1..=1 {
|
|
120
|
-
let nx = x + dx;
|
|
121
|
-
let ny = y + dy;
|
|
122
|
-
if nx >= 0 && nx < w as i32 && ny >= 0 && ny < h as i32 {
|
|
123
|
-
let ni = (ny * w as i32 + nx) as usize;
|
|
124
|
-
if edges[ni] == 1 {
|
|
125
|
-
edges[ni] = 2;
|
|
126
|
-
stack.push((nx, ny));
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
for e in edges.iter_mut() {
|
|
133
|
-
if *e == 1 {
|
|
134
|
-
*e = 0;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
struct UF {
|
|
140
|
-
p: Vec<usize>,
|
|
141
|
-
r: Vec<u8>,
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
impl UF {
|
|
145
|
-
fn new(n: usize) -> Self {
|
|
146
|
-
Self {
|
|
147
|
-
p: (0..n).collect(),
|
|
148
|
-
r: vec![0; n],
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
fn find(&mut self, x: usize) -> usize {
|
|
152
|
-
if self.p[x] != x {
|
|
153
|
-
self.p[x] = self.find(self.p[x]);
|
|
154
|
-
}
|
|
155
|
-
self.p[x]
|
|
156
|
-
}
|
|
157
|
-
fn union(&mut self, a: usize, b: usize) {
|
|
158
|
-
let ra = self.find(a);
|
|
159
|
-
let rb = self.find(b);
|
|
160
|
-
if ra == rb {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
if self.r[ra] < self.r[rb] {
|
|
164
|
-
self.p[ra] = rb;
|
|
165
|
-
} else if self.r[ra] > self.r[rb] {
|
|
166
|
-
self.p[rb] = ra;
|
|
167
|
-
} else {
|
|
168
|
-
self.p[rb] = ra;
|
|
169
|
-
self.r[ra] += 1;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
fn bounding_boxes(edges: &[u8], w: usize, h: usize, min_area: usize) -> Vec<[i32; 4]> {
|
|
175
|
-
let n = w * h;
|
|
176
|
-
let mut uf = UF::new(n);
|
|
177
|
-
for y in 0..h {
|
|
178
|
-
for x in 0..w {
|
|
179
|
-
let i = y * w + x;
|
|
180
|
-
if edges[i] == 0 {
|
|
181
|
-
continue;
|
|
182
|
-
}
|
|
183
|
-
if x + 1 < w && edges[i + 1] != 0 {
|
|
184
|
-
uf.union(i, i + 1);
|
|
185
|
-
}
|
|
186
|
-
if y + 1 < h && edges[i + w] != 0 {
|
|
187
|
-
uf.union(i, i + w);
|
|
188
|
-
}
|
|
189
|
-
if x + 1 < w && y + 1 < h && edges[i + w + 1] != 0 {
|
|
190
|
-
uf.union(i, i + w + 1);
|
|
191
|
-
}
|
|
192
|
-
if x > 0 && y + 1 < h && edges[i + w - 1] != 0 {
|
|
193
|
-
uf.union(i, i + w - 1);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
let mut x0 = vec![w as i32; n];
|
|
198
|
-
let mut y0 = vec![h as i32; n];
|
|
199
|
-
let mut x1 = vec![0i32; n];
|
|
200
|
-
let mut y1 = vec![0i32; n];
|
|
201
|
-
let mut cnt = vec![0usize; n];
|
|
202
|
-
for y in 0..h {
|
|
203
|
-
for x in 0..w {
|
|
204
|
-
let i = y * w + x;
|
|
205
|
-
if edges[i] == 0 {
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
let r = uf.find(i);
|
|
209
|
-
x0[r] = x0[r].min(x as i32);
|
|
210
|
-
y0[r] = y0[r].min(y as i32);
|
|
211
|
-
x1[r] = x1[r].max(x as i32);
|
|
212
|
-
y1[r] = y1[r].max(y as i32);
|
|
213
|
-
cnt[r] += 1;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
let mut boxes: Vec<[i32; 4]> = Vec::new();
|
|
217
|
-
for i in 0..n {
|
|
218
|
-
if cnt[i] >= min_area {
|
|
219
|
-
boxes.push([x0[i], y0[i], x1[i] - x0[i] + 1, y1[i] - y0[i] + 1]);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
boxes.sort_by(|a, b| (b[2] * b[3]).cmp(&(a[2] * a[3])));
|
|
223
|
-
boxes
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
fn color_gradient(data: &[u8], w: usize, h: usize) -> SobelResult {
|
|
227
|
-
let n = w * h;
|
|
228
|
-
let mut r_ch = vec![0.0f32; n];
|
|
229
|
-
let mut g_ch = vec![0.0f32; n];
|
|
230
|
-
let mut b_ch = vec![0.0f32; n];
|
|
231
|
-
for i in 0..n {
|
|
232
|
-
let o = i * 4;
|
|
233
|
-
r_ch[i] = data[o] as f32;
|
|
234
|
-
g_ch[i] = data[o + 1] as f32;
|
|
235
|
-
b_ch[i] = data[o + 2] as f32;
|
|
236
|
-
}
|
|
237
|
-
let rb = blur(&r_ch, w, h);
|
|
238
|
-
let gb = blur(&g_ch, w, h);
|
|
239
|
-
let bb = blur(&b_ch, w, h);
|
|
240
|
-
|
|
241
|
-
let mut mag = vec![0.0f32; n];
|
|
242
|
-
let mut dx = vec![0.0f32; n];
|
|
243
|
-
let mut dy = vec![0.0f32; n];
|
|
244
|
-
for y in 1..h - 1 {
|
|
245
|
-
for x in 1..w - 1 {
|
|
246
|
-
let i = y * w + x;
|
|
247
|
-
let dr_x = rb[i + 1] - rb[i - 1];
|
|
248
|
-
let dg_x = gb[i + 1] - gb[i - 1];
|
|
249
|
-
let db_x = bb[i + 1] - bb[i - 1];
|
|
250
|
-
let dr_y = rb[i + w] - rb[i - w];
|
|
251
|
-
let dg_y = gb[i + w] - gb[i - w];
|
|
252
|
-
let db_y = bb[i + w] - bb[i - w];
|
|
253
|
-
let sx = (dr_x * dr_x + dg_x * dg_x + db_x * db_x).sqrt();
|
|
254
|
-
let sy = (dr_y * dr_y + dg_y * dg_y + db_y * db_y).sqrt();
|
|
255
|
-
let sign_x = if dr_x + dg_x + db_x >= 0.0 { 1.0 } else { -1.0 };
|
|
256
|
-
let sign_y = if dr_y + dg_y + db_y >= 0.0 { 1.0 } else { -1.0 };
|
|
257
|
-
dx[i] = sx * sign_x;
|
|
258
|
-
dy[i] = sy * sign_y;
|
|
259
|
-
mag[i] = (sx * sx + sy * sy).sqrt();
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
SobelResult { mag, dx, dy }
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
fn merge_and_threshold(
|
|
266
|
-
gray_sup: &[f32],
|
|
267
|
-
color_sup: &[f32],
|
|
268
|
-
w: usize,
|
|
269
|
-
h: usize,
|
|
270
|
-
lo: f32,
|
|
271
|
-
hi: f32,
|
|
272
|
-
) -> Vec<u8> {
|
|
273
|
-
let n = w * h;
|
|
274
|
-
let mut out = vec![0u8; n];
|
|
275
|
-
for i in 0..n {
|
|
276
|
-
let v = gray_sup[i].max(color_sup[i]);
|
|
277
|
-
if v >= hi {
|
|
278
|
-
out[i] = 2;
|
|
279
|
-
} else if v >= lo {
|
|
280
|
-
out[i] = 1;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
out
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
#[wasm_bindgen]
|
|
287
|
-
pub fn detect_borders(
|
|
288
|
-
data: &[u8],
|
|
289
|
-
width: usize,
|
|
290
|
-
height: usize,
|
|
291
|
-
low_threshold: f32,
|
|
292
|
-
high_threshold: f32,
|
|
293
|
-
min_area: usize,
|
|
294
|
-
) -> Vec<f32> {
|
|
295
|
-
let gray = grayscale(data, width, height);
|
|
296
|
-
let blurred = blur(&gray, width, height);
|
|
297
|
-
let gray_sobel = sobel(&blurred, width, height);
|
|
298
|
-
let gray_sup = nms(&gray_sobel, width, height);
|
|
299
|
-
|
|
300
|
-
let color_sobel = color_gradient(data, width, height);
|
|
301
|
-
let color_sup = nms(&color_sobel, width, height);
|
|
302
|
-
|
|
303
|
-
let mut edges = merge_and_threshold(
|
|
304
|
-
&gray_sup,
|
|
305
|
-
&color_sup,
|
|
306
|
-
width,
|
|
307
|
-
height,
|
|
308
|
-
low_threshold,
|
|
309
|
-
high_threshold,
|
|
310
|
-
);
|
|
311
|
-
hysteresis(&mut edges, width, height);
|
|
312
|
-
|
|
313
|
-
let boxes = bounding_boxes(&edges, width, height, min_area);
|
|
314
|
-
let mut out = Vec::with_capacity(boxes.len() * 4);
|
|
315
|
-
for b in &boxes {
|
|
316
|
-
out.push(b[0] as f32);
|
|
317
|
-
out.push(b[1] as f32);
|
|
318
|
-
out.push(b[2] as f32);
|
|
319
|
-
out.push(b[3] as f32);
|
|
320
|
-
}
|
|
321
|
-
out
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
#[wasm_bindgen]
|
|
325
|
-
pub fn detect_borders_default(data: &[u8], width: usize, height: usize) -> Vec<f32> {
|
|
326
|
-
detect_borders(data, width, height, 20.0, 60.0, 100)
|
|
327
|
-
}
|
package/src_ts/index.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
initSync,
|
|
3
|
-
detect_borders,
|
|
4
|
-
detect_borders_default,
|
|
5
|
-
} from '../pkg/edge_det.js'
|
|
6
|
-
import { WASM_BYTES } from './wasm_bytes'
|
|
7
|
-
|
|
8
|
-
export interface Border {
|
|
9
|
-
x: number
|
|
10
|
-
y: number
|
|
11
|
-
w: number
|
|
12
|
-
h: number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
let _initialized = false
|
|
16
|
-
|
|
17
|
-
function ensureInit() {
|
|
18
|
-
if (!_initialized) {
|
|
19
|
-
initSync({ module: WASM_BYTES })
|
|
20
|
-
_initialized = true
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function detectBorders(
|
|
25
|
-
data: Uint8Array,
|
|
26
|
-
width: number,
|
|
27
|
-
height: number,
|
|
28
|
-
options?: { lowThreshold?: number; highThreshold?: number; minArea?: number }
|
|
29
|
-
): Border[] {
|
|
30
|
-
ensureInit()
|
|
31
|
-
const result = detect_borders(
|
|
32
|
-
data,
|
|
33
|
-
width,
|
|
34
|
-
height,
|
|
35
|
-
options?.lowThreshold ?? 20,
|
|
36
|
-
options?.highThreshold ?? 60,
|
|
37
|
-
options?.minArea ?? 100
|
|
38
|
-
)
|
|
39
|
-
const borders: Border[] = []
|
|
40
|
-
for (let i = 0; i < result.length; i += 4) {
|
|
41
|
-
borders.push({
|
|
42
|
-
x: result[i],
|
|
43
|
-
y: result[i + 1],
|
|
44
|
-
w: result[i + 2],
|
|
45
|
-
h: result[i + 3],
|
|
46
|
-
})
|
|
47
|
-
}
|
|
48
|
-
return borders
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function detectBordersDefault(
|
|
52
|
-
data: Uint8Array,
|
|
53
|
-
width: number,
|
|
54
|
-
height: number
|
|
55
|
-
): Border[] {
|
|
56
|
-
ensureInit()
|
|
57
|
-
const result = detect_borders_default(data, width, height)
|
|
58
|
-
const borders: Border[] = []
|
|
59
|
-
for (let i = 0; i < result.length; i += 4) {
|
|
60
|
-
borders.push({
|
|
61
|
-
x: result[i],
|
|
62
|
-
y: result[i + 1],
|
|
63
|
-
w: result[i + 2],
|
|
64
|
-
h: result[i + 3],
|
|
65
|
-
})
|
|
66
|
-
}
|
|
67
|
-
return borders
|
|
68
|
-
}
|