|
| 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