-
Notifications
You must be signed in to change notification settings - Fork 0
/
HysteresisFilter.java
178 lines (157 loc) · 5.45 KB
/
HysteresisFilter.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2014vision;
import java.awt.Point;
import java.util.Stack;
import static org.bytedeco.javacpp.opencv_core.CV_8U;
import org.bytedeco.javacpp.opencv_core.CvMat;
import org.bytedeco.javacpp.opencv_core.CvScalar;
import static org.bytedeco.javacpp.opencv_core.cvGet2D;
import static org.bytedeco.javacpp.opencv_core.cvZero;
/**
*
* @author Jared "Jär Bär" Gentner
*/
public class HysteresisFilter implements Filter {
private Threshold initialThreshold;
private Threshold secondaryThreshold;
public HysteresisFilter(Threshold initialThreshold, Threshold secondaryThreshold) {
this.initialThreshold = initialThreshold;
this.secondaryThreshold = secondaryThreshold;
}
@Override
public CvMat runFilter(CvMat image) {
CvMat dest = CvMat.create(image.rows(), image.cols(), CV_8U);
cvZero(dest);
for (int i = 0; i < image.rows(); ++i) {
for (int j = 0; j < image.cols(); ++j) {
CvScalar pixel = cvGet2D(image, i, j);
double h = pixel.getVal(0),
s = pixel.getVal(1),
v = pixel.getVal(2);
if (initialThreshold.filter(h, s, v)) {
thresh(image, dest, i, j);
}
}
}
return dest;
}
private void thresh(CvMat image, CvMat dest, int i, int j) {
Stack<Point> stack = new Stack<>();
stack.add(new Point(i, j));
while (!stack.isEmpty()) {
Point point = stack.pop();
j = (int)point.getY();
i = (int)point.getX();
dest.put(i, j, 0xFF);
CvScalar pixel;
if (!(i + 1 >= image.rows())) {
if (dest.get(i + 1, j) != 0xFF) {
pixel = cvGet2D(image, i + 1, j);
double h = pixel.getVal(0),
s = pixel.getVal(1),
v = pixel.getVal(2);
if (secondaryThreshold.filter(h, s, v)) {
dest.put(i + 1, j, 0xFF);
stack.push(new Point(i + 1, j));
}
}
}
if (!(j + 1 >= image.cols())) {
if (dest.get(i, j + 1) != 0xFF) {
pixel = cvGet2D(image, i, j + 1);
double h = pixel.getVal(0),
s = pixel.getVal(1),
v = pixel.getVal(2);
if (secondaryThreshold.filter(h, s, v)) {
dest.put(i, j + 1, 0xFF);
stack.push(new Point(i, j + 1));
}
}
}
if (i - 1 != -1) {
if (dest.get(i - 1, j) != 0xFF) {
pixel = cvGet2D(image, i - 1, j);
double h = pixel.getVal(0),
s = pixel.getVal(1),
v = pixel.getVal(2);
if (secondaryThreshold.filter(h, s, v)) {
dest.put(i - 1, j, 0xFF);
stack.push(new Point(i - 1, j));
}
}
}
if (j - 1 != -1) {
if (dest.get(i, j - 1) != 0xFF) {
pixel = cvGet2D(image, i, j - 1);
double h = pixel.getVal(0),
s = pixel.getVal(1),
v = pixel.getVal(2);
if (secondaryThreshold.filter(h, s, v)) {
dest.put(i, j - 1, 0xFF);
stack.push(new Point(i, j - 1));
}
}
}
}
}
/*private void recurseThresh(CvMat image, CvMat dest, int i, int j) {
CvScalar pixel;
if (!(i + 1 >= image.rows())) {
if (dest.get(i + 1, j) != 0xFF) {
pixel = cvGet2D(image, i + 1, j);
double h = pixel.getVal(0),
s = pixel.getVal(1),
v = pixel.getVal(2);
if (secondaryThreshold.filter(h, s, v)) {
dest.put(i + 1, j, 0xFF);
recurseThresh(image, dest, i + 1, j);
}
}
}
if (!(j + 1 >= image.cols())) {
if (dest.get(i, j + 1) != 0xFF) {
pixel = cvGet2D(image, i, j + 1);
double h = pixel.getVal(0),
s = pixel.getVal(1),
v = pixel.getVal(2);
if (secondaryThreshold.filter(h, s, v)) {
dest.put(i, j + 1, 0xFF);
recurseThresh(image, dest, i, j + 1);
}
}
}
if (i - 1 != -1) {
if (dest.get(i - 1, j) != 0xFF) {
pixel = cvGet2D(image, i - 1, j);
double h = pixel.getVal(0),
s = pixel.getVal(1),
v = pixel.getVal(2);
if (secondaryThreshold.filter(h, s, v)) {
dest.put(i - 1, j, 0xFF);
recurseThresh(image, dest, i - 1, j);
}
}
}
if (j - 1 != -1) {
if (dest.get(i, j - 1) != 0xFF) {
pixel = cvGet2D(image, i, j - 1);
double h = pixel.getVal(0),
s = pixel.getVal(1),
v = pixel.getVal(2);
if (secondaryThreshold.filter(h, s, v)) {
dest.put(i, j - 1, 0xFF);
recurseThresh(image, dest, i, j - 1);
}
}
}
}*/
@Override
public void setThreshold(Threshold... thresh) {
this.initialThreshold = thresh[0];
this.secondaryThreshold = thresh[1];
}
}