Skip to content

Commit 18a7a4a

Browse files
committed
Merge pull request #560 from JLLeitschuh/feat/thresholdMoving
Adds an operation to threshold the moving parts of an image
2 parents 69bb1bb + f4d6c1e commit 18a7a4a

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

core/src/main/java/edu/wpi/grip/core/operations/Operations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public class Operations {
8888
NormalizeOperation::new,
8989
WatershedOperation::new,
9090
SwitchOperation::new,
91-
ValveOperation::new
91+
ValveOperation::new,
92+
ThresholdMoving::new
9293
);
9394
}
9495

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package edu.wpi.grip.core.operations.composite;
2+
3+
import com.google.common.eventbus.EventBus;
4+
import edu.wpi.grip.core.Operation;
5+
import edu.wpi.grip.core.sockets.InputSocket;
6+
import edu.wpi.grip.core.sockets.OutputSocket;
7+
import edu.wpi.grip.core.sockets.SocketHints;
8+
import org.bytedeco.javacpp.opencv_core;
9+
import org.bytedeco.javacpp.opencv_core.Mat;
10+
import org.bytedeco.javacpp.opencv_core.Size;
11+
12+
import java.util.Optional;
13+
14+
import static edu.wpi.grip.core.Operation.Category.IMAGE_PROCESSING;
15+
16+
/**
17+
* Finds the absolute difference between the current image and the previous image.
18+
*/
19+
public class ThresholdMoving implements Operation {
20+
21+
22+
@Override
23+
public String getName() {
24+
return "Threshold Moving";
25+
}
26+
27+
@Override
28+
public String getDescription() {
29+
return "Thresholds off parts of the image that have moved or changed between the previous and next image.";
30+
}
31+
32+
@Override
33+
public InputSocket<?>[] createInputSockets(EventBus eventBus) {
34+
return new InputSocket<?>[]{
35+
new InputSocket<>(eventBus, SocketHints.Inputs.createMatSocketHint("image", false))
36+
};
37+
}
38+
39+
@Override
40+
public OutputSocket<?>[] createOutputSockets(EventBus eventBus) {
41+
return new OutputSocket<?>[]{
42+
new OutputSocket<>(eventBus, SocketHints.Outputs.createMatSocketHint("moved"))
43+
};
44+
}
45+
46+
@Override
47+
public Category getCategory() {
48+
return IMAGE_PROCESSING;
49+
}
50+
51+
@Override
52+
public Optional<?> createData() {
53+
return Optional.of(new Mat());
54+
}
55+
56+
@Override
57+
public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs, Optional<?> data) {
58+
final Mat input = ((InputSocket<Mat>) inputs[0]).getValue().get();
59+
final OutputSocket<Mat> outputSocket = (OutputSocket<Mat>) outputs[0];
60+
final Mat lastImage = (Mat) data.get();
61+
62+
final Size lastSize = lastImage.size();
63+
final Size inputSize = input.size();
64+
if (!lastImage.empty() && lastSize.height() == inputSize.height() && lastSize.width() == inputSize.width()) {
65+
opencv_core.absdiff(input, lastImage, outputSocket.getValue().get());
66+
}
67+
input.copyTo(lastImage);
68+
outputSocket.setValue(outputSocket.getValue().get());
69+
}
70+
}

0 commit comments

Comments
 (0)