Skip to content

Commit 80469b1

Browse files
committed
Put watershed in a try-finally block
1 parent 5b57e1e commit 80469b1

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

core/src/main/java/edu/wpi/grip/core/operations/composite/WatershedOperation.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public String getDescription() {
3939
@Override
4040
public InputSocket<?>[] createInputSockets(EventBus eventBus) {
4141
return new InputSocket<?>[]{
42-
new InputSocket<>(eventBus, srcHint),
43-
new InputSocket<>(eventBus, contoursHint)
42+
new InputSocket<>(eventBus, srcHint),
43+
new InputSocket<>(eventBus, contoursHint)
4444
};
4545
}
4646

4747
@Override
4848
public OutputSocket<?>[] createOutputSockets(EventBus eventBus) {
4949
return new OutputSocket<?>[]{
50-
new OutputSocket<>(eventBus, outputHint)
50+
new OutputSocket<>(eventBus, outputHint)
5151
};
5252
}
5353

@@ -65,23 +65,25 @@ public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs) {
6565
final Mat markers = new Mat(input.size(), CV_32SC1, new Scalar(0.0));
6666
final Mat output = new Mat(markers.size(), CV_8UC1, new Scalar(0.0));
6767

68-
// draw foreground markers (these have to be different colors)
69-
for (int i = 0; i < contours.size(); i++) {
70-
drawContours(markers, contours, i, Scalar.all((i + 1) * (255 / contours.size())), CV_FILLED, LINE_8, null, 2, null);
71-
}
72-
73-
// draw background marker a different color from the foreground markers
74-
// TODO maybe make this configurable? There may be something in the corner
75-
circle(markers, new Point(5, 5), 3, Scalar.WHITE, -1, LINE_8, 0);
68+
try {
69+
// draw foreground markers (these have to be different colors)
70+
for (int i = 0; i < contours.size(); i++) {
71+
drawContours(markers, contours, i, Scalar.all((i + 1) * (255 / contours.size())), CV_FILLED, LINE_8, null, 2, null);
72+
}
7673

77-
watershed(input, markers);
78-
markers.convertTo(output, CV_8UC1);
79-
bitwise_not(output, output); // watershed inverts colors; invert them back
74+
// draw background marker a different color from the foreground markers
75+
// TODO maybe make this configurable? There may be something in the corner
76+
circle(markers, new Point(5, 5), 3, Scalar.WHITE, -1, LINE_8, 0);
8077

81-
outputSocket.setValue(output);
78+
watershed(input, markers);
79+
markers.convertTo(output, CV_8UC1);
80+
bitwise_not(output, output); // watershed inverts colors; invert them back
8281

83-
// make sure that the working mat is freed to avoid a memory leak
84-
markers.release();
82+
outputSocket.setValue(output);
83+
} finally {
84+
// make sure that the working mat is freed to avoid a memory leak
85+
markers.release();
86+
}
8587
}
8688

8789
}

0 commit comments

Comments
 (0)