From f4d6c1e9c5f6c3036110c0d067cbcb1bd85d141b Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 31 Mar 2016 16:42:21 -0400 Subject: [PATCH] Adds an operation to threshold the moving parts of an image --- .../wpi/grip/core/operations/Operations.java | 3 +- .../operations/composite/ThresholdMoving.java | 70 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/edu/wpi/grip/core/operations/composite/ThresholdMoving.java diff --git a/core/src/main/java/edu/wpi/grip/core/operations/Operations.java b/core/src/main/java/edu/wpi/grip/core/operations/Operations.java index 69d65ecbef..859bd2d58f 100644 --- a/core/src/main/java/edu/wpi/grip/core/operations/Operations.java +++ b/core/src/main/java/edu/wpi/grip/core/operations/Operations.java @@ -88,7 +88,8 @@ public class Operations { NormalizeOperation::new, WatershedOperation::new, SwitchOperation::new, - ValveOperation::new + ValveOperation::new, + ThresholdMoving::new ); } diff --git a/core/src/main/java/edu/wpi/grip/core/operations/composite/ThresholdMoving.java b/core/src/main/java/edu/wpi/grip/core/operations/composite/ThresholdMoving.java new file mode 100644 index 0000000000..2eca25d5a1 --- /dev/null +++ b/core/src/main/java/edu/wpi/grip/core/operations/composite/ThresholdMoving.java @@ -0,0 +1,70 @@ +package edu.wpi.grip.core.operations.composite; + +import com.google.common.eventbus.EventBus; +import edu.wpi.grip.core.Operation; +import edu.wpi.grip.core.sockets.InputSocket; +import edu.wpi.grip.core.sockets.OutputSocket; +import edu.wpi.grip.core.sockets.SocketHints; +import org.bytedeco.javacpp.opencv_core; +import org.bytedeco.javacpp.opencv_core.Mat; +import org.bytedeco.javacpp.opencv_core.Size; + +import java.util.Optional; + +import static edu.wpi.grip.core.Operation.Category.IMAGE_PROCESSING; + +/** + * Finds the absolute difference between the current image and the previous image. + */ +public class ThresholdMoving implements Operation { + + + @Override + public String getName() { + return "Threshold Moving"; + } + + @Override + public String getDescription() { + return "Thresholds off parts of the image that have moved or changed between the previous and next image."; + } + + @Override + public InputSocket[] createInputSockets(EventBus eventBus) { + return new InputSocket[]{ + new InputSocket<>(eventBus, SocketHints.Inputs.createMatSocketHint("image", false)) + }; + } + + @Override + public OutputSocket[] createOutputSockets(EventBus eventBus) { + return new OutputSocket[]{ + new OutputSocket<>(eventBus, SocketHints.Outputs.createMatSocketHint("moved")) + }; + } + + @Override + public Category getCategory() { + return IMAGE_PROCESSING; + } + + @Override + public Optional createData() { + return Optional.of(new Mat()); + } + + @Override + public void perform(InputSocket[] inputs, OutputSocket[] outputs, Optional data) { + final Mat input = ((InputSocket) inputs[0]).getValue().get(); + final OutputSocket outputSocket = (OutputSocket) outputs[0]; + final Mat lastImage = (Mat) data.get(); + + final Size lastSize = lastImage.size(); + final Size inputSize = input.size(); + if (!lastImage.empty() && lastSize.height() == inputSize.height() && lastSize.width() == inputSize.width()) { + opencv_core.absdiff(input, lastImage, outputSocket.getValue().get()); + } + input.copyTo(lastImage); + outputSocket.setValue(outputSocket.getValue().get()); + } +}