Skip to content

Instantly share code, notes, and snippets.

@SamCarlberg
Created March 30, 2016 18:23
Show Gist options
  • Save SamCarlberg/a0113c4465c13bb289f831e577cf9b8e to your computer and use it in GitHub Desktop.
Save SamCarlberg/a0113c4465c13bb289f831e577cf9b8e to your computer and use it in GitHub Desktop.
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 java.io.InputStream;
import java.util.Optional;
import static org.bytedeco.javacpp.opencv_core.Mat;
import static org.bytedeco.javacpp.opencv_core.Size;
import static org.bytedeco.javacpp.opencv_imgproc.*;
/**
* Scale an image to an exact width and height using one of several interpolation modes. Scaling images down can
* be a useful optimization, and scaling them up might be necessary for combining multiple images that are different
* sizes.
*/
public class ResizeOperation extends Operation {
private final InputSocket<Mat> inputSocket;
private final InputSocket<Number> widthSocket;
private final InputSocket<Number> heightSocket;
private final InputSocket<Interpolation> interpolationSocket;
private final OutputSocket<Mat> outputSocket;
public ResizeOperation(EventBus eventBus) {
super(eventBus);
this.inputSocket = new InputSocket<>(eventBus, SocketHints.Inputs.createMatSocketHint("Input", false));
this.widthSocket = new InputSocket<>(eventBus, SocketHints.Inputs.createNumberSpinnerSocketHint("Width", 640));
this.heightSocket = new InputSocket<>(eventBus, SocketHints.Inputs.createNumberSpinnerSocketHint("Height", 480));
this.interpolationSocket = new InputSocket<>(eventBus, SocketHints.createEnumSocketHint("Interpolation", Interpolation.CUBIC));
this.outputSocket = new OutputSocket<>(eventBus, SocketHints.Outputs.createMatSocketHint("Output"));
}
private enum Interpolation {
NEAREST("None", INTER_NEAREST),
LINEAR("Linear", INTER_LINEAR),
CUBIC("Cubic", INTER_CUBIC),
LANCZOS("Lanczos", INTER_LANCZOS4),
AREA("Area", INTER_AREA);
final String label;
final int value;
Interpolation(String label, int value) {
this.label = label;
this.value = value;
}
@Override
public String toString() {
return label;
}
}
@Override
public String getName() {
return "Resize Image";
}
@Override
public String getDescription() {
return "Scale an image to an exact size";
}
@Override
public Category getCategory() {
return Category.IMAGE_PROCESSING;
}
@Override
public Optional<InputStream> getIcon() {
return Optional.of(getClass().getResourceAsStream("/edu/wpi/grip/ui/icons/resize.png"));
}
@Override
public InputSocket<?>[] getInputSockets() {
return new InputSocket<?>[]{
inputSocket,
widthSocket,
heightSocket,
interpolationSocket
};
}
@Override
public OutputSocket<?>[] getOutputSockets() {
return new OutputSocket<?>[]{
outputSocket
};
}
@Override
public void perform() {
final Mat input = inputSocket.getValue().get();
final Number width = widthSocket.getValue().get();
final Number height = heightSocket.getValue().get();
final Interpolation interpolation = interpolationSocket.getValue().get();
final Mat output = outputSocket.getValue().get();
resize(input, output, new Size(width.intValue(), height.intValue()), 0.0, 0.0, interpolation.value);
outputSocket.setValue(output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment