-
Notifications
You must be signed in to change notification settings - Fork 464
/
DilationAndErosion.pde
50 lines (41 loc) · 1.24 KB
/
DilationAndErosion.pde
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
import gab.opencv.*;
PImage src, dilated, eroded, both;
OpenCV opencv;
void setup() {
src = loadImage("pen_sketch.jpg");
src.resize(src.width/2, 0);
size(800, 786);
opencv = new OpenCV(this, src);
// Dilate and Erode both need a binary image
// So, we'll make it gray and threshold it.
opencv.gray();
opencv.threshold(100);
// We'll also invert so that erosion eats away the lines
// and dilation expands them (rather than vice-versa)
opencv.invert();
// save a snapshot to use in both operations
src = opencv.getSnapshot();
// erode and save snapshot for display
opencv.erode();
eroded = opencv.getSnapshot();
// reload un-eroded image and dilate it
opencv.loadImage(src);
opencv.dilate();
// save dilated version for display
dilated = opencv.getSnapshot();
// now erode on top of dilated version to close holes
opencv.erode();
both = opencv.getSnapshot();
noLoop();
}
void draw() {
image(src, 0, 0);
image(eroded, src.width, 0);
image(dilated, 0, src.height);
image(both, src.width, src.height);
fill(0, 255, 0);
text("original", 20, 20);
text("erode", src.width + 20, 20);
text("dilate", 20, src.height+20);
text("dilate then erode\n(close holes)", src.width+20, src.height+20);
}