-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdilate.ts
51 lines (40 loc) · 1.49 KB
/
dilate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import StructuralElement from './structural.ts';
import Bitmap from './bitmap.ts';
import { inImage } from './erode.ts';
/**
* Wykonuje dylację obrazu
* @param bitmap Obraz wejściowy
* @param se Element strukturalny
* @param distance (Przydatne w mapie odległości) Gdy zostanie podana wartość funkcja wpiszę ją zamiast wyniku dylacji
* @returns Obraz po dylacji
*/
const dilate = (bitmap: Bitmap, se: StructuralElement, distance?: number) => {
let cx = Math.floor(se.width / 2);
let cy = Math.floor(se.height / 2);
let result = new Bitmap(bitmap.width, bitmap.height);
for(let i = 0; i < bitmap.width; i++) {
for(let j = 0; j < bitmap.height; j++) {
let max = 0;
for(let si = 0; si < se.width; si++) {
for(let sj = 0; sj < se.height; sj++) {
const px = i - (cx - si);
const py = j - (cy - sj);
if(inImage(px, py, bitmap) && se[si][sj]) {
const pixel = bitmap.getMono(px, py);
if(pixel > max)
max = pixel;
}
}
}
if(distance && max != 0) {
if(!bitmap.getLogic(i, j))
result.setMono(i, j, distance);
else
result.setMono(i, j, bitmap.getMono(i, j));
} else
result.setMono(i, j, max);
}
}
return result;
}
export default dilate;