|
| 1 | +package edu.wpi.grip.core.operations.composite; |
| 2 | + |
| 3 | +import com.google.common.eventbus.EventBus; |
| 4 | +import edu.wpi.grip.core.*; |
| 5 | + |
| 6 | +import java.io.InputStream; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import static org.bytedeco.javacpp.opencv_core.*; |
| 10 | +import static org.bytedeco.javacpp.opencv_imgproc.*; |
| 11 | + |
| 12 | +/** |
| 13 | + * An {@link Operation} that takes in a list of contours and outputs a list of any contours in the input that match |
| 14 | + * all of several criteria. Right now, the user can specify a minimum area, minimum perimeter, and ranges for width |
| 15 | + * and height. |
| 16 | + * <p> |
| 17 | + * This is useful because running a Find Contours on a real-life image typically leads to many small undesirable |
| 18 | + * contours from noise and small objects, as well as contours that do not meet the expected characteristics of the |
| 19 | + * feature we're actually looking for. So, this operation can help narrow them down. |
| 20 | + */ |
| 21 | +public class FilterContoursOperation implements Operation { |
| 22 | + |
| 23 | + private final SocketHint<ContoursReport> contoursHint = new SocketHint.Builder<>(ContoursReport.class) |
| 24 | + .identifier("Contours").initialValueSupplier(ContoursReport::new).build(); |
| 25 | + |
| 26 | + private final SocketHint<Number> minAreaHint = |
| 27 | + SocketHints.Inputs.createNumberSpinnerSocketHint("Min Area", 0, 0, Integer.MAX_VALUE); |
| 28 | + |
| 29 | + private final SocketHint<Number> minPerimeterHint = |
| 30 | + SocketHints.Inputs.createNumberSpinnerSocketHint("Min Perimeter", 0, 0, Integer.MAX_VALUE); |
| 31 | + |
| 32 | + private final SocketHint<Number> minWidthHint = |
| 33 | + SocketHints.Inputs.createNumberSpinnerSocketHint("Min Width", 0, 0, Integer.MAX_VALUE); |
| 34 | + |
| 35 | + private final SocketHint<Number> maxWidthHint = |
| 36 | + SocketHints.Inputs.createNumberSpinnerSocketHint("Max Width", 0, 0, Integer.MAX_VALUE); |
| 37 | + |
| 38 | + private final SocketHint<Number> minHeightHint = |
| 39 | + SocketHints.Inputs.createNumberSpinnerSocketHint("Min Height", 0, 0, Integer.MAX_VALUE); |
| 40 | + |
| 41 | + private final SocketHint<Number> maxHeightHint = |
| 42 | + SocketHints.Inputs.createNumberSpinnerSocketHint("Max Height", 0, 0, Integer.MAX_VALUE); |
| 43 | + |
| 44 | + @Override |
| 45 | + public String getName() { |
| 46 | + return "Filter Contours"; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getDescription() { |
| 51 | + return "Find contours matching certain criteria."; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Optional<InputStream> getIcon() { |
| 56 | + return Optional.of(getClass().getResourceAsStream("/edu/wpi/grip/ui/icons/find-contours.png")); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public InputSocket<?>[] createInputSockets(EventBus eventBus) { |
| 61 | + return new InputSocket<?>[]{ |
| 62 | + new InputSocket<>(eventBus, contoursHint), |
| 63 | + new InputSocket<>(eventBus, minAreaHint), |
| 64 | + new InputSocket<>(eventBus, minPerimeterHint), |
| 65 | + new InputSocket<>(eventBus, minWidthHint), |
| 66 | + new InputSocket<>(eventBus, maxWidthHint), |
| 67 | + new InputSocket<>(eventBus, minHeightHint), |
| 68 | + new InputSocket<>(eventBus, maxHeightHint), |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public OutputSocket<?>[] createOutputSockets(EventBus eventBus) { |
| 74 | + return new OutputSocket<?>[]{new OutputSocket<>(eventBus, contoursHint)}; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs) { |
| 80 | + final InputSocket<ContoursReport> inputSocket = (InputSocket<ContoursReport>) inputs[0]; |
| 81 | + final double minArea = ((Number) inputs[1].getValue().get()).doubleValue(); |
| 82 | + final double minPerimeter = ((Number) inputs[2].getValue().get()).doubleValue(); |
| 83 | + final double minWidth = ((Number) inputs[3].getValue().get()).doubleValue(); |
| 84 | + final double maxWidth = ((Number) inputs[4].getValue().get()).doubleValue(); |
| 85 | + final double minHeight = ((Number) inputs[5].getValue().get()).doubleValue(); |
| 86 | + final double maxHeight = ((Number) inputs[6].getValue().get()).doubleValue(); |
| 87 | + |
| 88 | + final MatVector inputContours = inputSocket.getValue().get().getContours(); |
| 89 | + final MatVector outputContours = new MatVector(inputContours.size()); |
| 90 | + |
| 91 | + // Add contours from the input vector to the output vector only if they pass all of the criteria (minimum |
| 92 | + // area, minimum perimeter, width, and height) |
| 93 | + int filteredContourCount = 0; |
| 94 | + for (int i = 0; i < inputContours.size(); i++) { |
| 95 | + final Mat contour = inputContours.get(i); |
| 96 | + final Rect bb = boundingRect(contour); |
| 97 | + |
| 98 | + if (contourArea(contour) < minArea) continue; |
| 99 | + if (arcLength(contour, true) < minPerimeter) continue; |
| 100 | + if (bb.width() < minWidth || bb.width() > maxWidth) continue; |
| 101 | + if (bb.height() < minHeight || bb.height() > maxHeight) continue; |
| 102 | + |
| 103 | + outputContours.put(filteredContourCount++, contour); |
| 104 | + } |
| 105 | + |
| 106 | + outputContours.resize(filteredContourCount); |
| 107 | + |
| 108 | + final OutputSocket<ContoursReport> outputSocket = (OutputSocket<ContoursReport>) outputs[0]; |
| 109 | + outputSocket.getValue().get().setRows(inputSocket.getValue().get().getRows()); |
| 110 | + outputSocket.getValue().get().setCols(inputSocket.getValue().get().getCols()); |
| 111 | + outputSocket.getValue().get().setContours(outputContours); |
| 112 | + outputSocket.setValueOptional(outputSocket.getValue()); |
| 113 | + } |
| 114 | +} |
0 commit comments