Skip to content

Commit 3019fb5

Browse files
committed
Merge pull request #130 from ThomasJClark/preview-gui
Add more preview enhancements
2 parents 6024f89 + 5e98305 commit 3019fb5

File tree

4 files changed

+105
-13
lines changed

4 files changed

+105
-13
lines changed

src/main/java/edu/wpi/grip/ui/preview/BlobsSocketPreviewView.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import edu.wpi.grip.ui.util.ImageConverter;
99
import javafx.application.Platform;
1010
import javafx.geometry.Orientation;
11+
import javafx.scene.control.CheckBox;
1112
import javafx.scene.control.Label;
1213
import javafx.scene.control.Separator;
1314
import javafx.scene.image.Image;
@@ -27,6 +28,7 @@ public class BlobsSocketPreviewView extends SocketPreviewView<BlobsReport> {
2728
private final ImageView imageView = new ImageView();
2829
private final Label infoLabel = new Label();
2930
private final Mat tmp = new Mat();
31+
private boolean showInputImage = false;
3032

3133
/**
3234
* @param eventBus The EventBus used by the application
@@ -35,11 +37,15 @@ public class BlobsSocketPreviewView extends SocketPreviewView<BlobsReport> {
3537
public BlobsSocketPreviewView(EventBus eventBus, OutputSocket<BlobsReport> socket) {
3638
super(eventBus, socket);
3739

38-
final VBox content = new VBox();
40+
final CheckBox show = new CheckBox("Show Input Image");
41+
show.setSelected(this.showInputImage);
42+
show.selectedProperty().addListener(observable -> {
43+
this.showInputImage = show.isSelected();
44+
this.convertImage();
45+
});
46+
47+
final VBox content = new VBox(this.imageView, new Separator(Orientation.HORIZONTAL), this.infoLabel, show);
3948
content.getStyleClass().add("preview-box");
40-
content.getChildren().add(this.imageView);
41-
content.getChildren().add(new Separator(Orientation.HORIZONTAL));
42-
content.getChildren().add(this.infoLabel);
4349
this.setContent(content);
4450

4551
this.convertImage();
@@ -60,10 +66,16 @@ private void convertImage() {
6066
// If there were lines found, draw them on the image before displaying it
6167
if (!blobsReport.getBlobs().isEmpty()) {
6268
if (input.channels() == 3) {
63-
input = input.clone();
69+
input.copyTo(tmp);
6470
} else {
6571
cvtColor(input, tmp, CV_GRAY2BGR);
66-
input = tmp;
72+
}
73+
74+
input = tmp;
75+
76+
// If we don't want to see the background image, set it to black
77+
if (!this.showInputImage) {
78+
bitwise_xor(tmp, tmp, tmp);
6779
}
6880

6981
// For each line in the report, draw a line along with the starting and ending points

src/main/java/edu/wpi/grip/ui/preview/LinesSocketPreviewView.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import edu.wpi.grip.ui.util.ImageConverter;
99
import javafx.application.Platform;
1010
import javafx.geometry.Orientation;
11+
import javafx.scene.control.CheckBox;
1112
import javafx.scene.control.Label;
1213
import javafx.scene.control.Separator;
1314
import javafx.scene.image.Image;
1415
import javafx.scene.image.ImageView;
1516
import javafx.scene.layout.VBox;
16-
import javafx.stage.Screen;
1717
import org.bytedeco.javacpp.opencv_core;
1818

1919
import java.util.List;
@@ -31,6 +31,7 @@ public class LinesSocketPreviewView extends SocketPreviewView<LinesReport> {
3131
private final ImageView imageView = new ImageView();
3232
private final Label infoLabel = new Label();
3333
private final Mat tmp = new Mat();
34+
private boolean showInputImage = false;
3435

3536
/**
3637
* @param eventBus The EventBus used by the application
@@ -39,11 +40,16 @@ public class LinesSocketPreviewView extends SocketPreviewView<LinesReport> {
3940
public LinesSocketPreviewView(EventBus eventBus, OutputSocket<LinesReport> socket) {
4041
super(eventBus, socket);
4142

42-
final VBox content = new VBox();
43+
// Add a checkbox to set if the preview should just show the lines, or also the input image
44+
final CheckBox show = new CheckBox("Show Input Image");
45+
show.setSelected(this.showInputImage);
46+
show.selectedProperty().addListener(observable -> {
47+
this.showInputImage = show.isSelected();
48+
this.convertImage();
49+
});
50+
51+
final VBox content = new VBox(this.imageView, new Separator(Orientation.HORIZONTAL), this.infoLabel, show);
4352
content.getStyleClass().add("preview-box");
44-
content.getChildren().add(this.imageView);
45-
content.getChildren().add(new Separator(Orientation.HORIZONTAL));
46-
content.getChildren().add(this.infoLabel);
4753
this.setContent(content);
4854

4955
this.convertImage();
@@ -65,10 +71,16 @@ private void convertImage() {
6571
// If there were lines found, draw them on the image before displaying it
6672
if (!linesReport.getLines().isEmpty()) {
6773
if (input.channels() == 3) {
68-
input = input.clone();
74+
input.copyTo(tmp);
6975
} else {
7076
cvtColor(input, tmp, CV_GRAY2BGR);
71-
input = tmp;
77+
}
78+
79+
input = tmp;
80+
81+
// If we don't want to see the background image, set it to black
82+
if (!this.showInputImage) {
83+
bitwise_xor(tmp, tmp, tmp);
7284
}
7385

7486
// For each line in the report, draw a line along with the starting and ending points
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package edu.wpi.grip.ui.preview;
2+
3+
import com.google.common.eventbus.EventBus;
4+
import com.google.common.eventbus.Subscribe;
5+
import edu.wpi.grip.core.OutputSocket;
6+
import edu.wpi.grip.core.events.SocketChangedEvent;
7+
import javafx.scene.control.Label;
8+
import javafx.scene.control.TextField;
9+
import javafx.scene.layout.GridPane;
10+
import org.bytedeco.javacpp.IntPointer;
11+
12+
import static org.bytedeco.javacpp.opencv_core.*;
13+
14+
/**
15+
* A {@link SocketPreviewView} for OpenCV points and sizes
16+
*/
17+
public class PointSizeSocketPreviewView extends SocketPreviewView<IntPointer> {
18+
19+
private final TextField x, y;
20+
21+
/**
22+
* @param eventBus The EventBus used by the application
23+
* @param socket An output socket to preview
24+
*/
25+
public PointSizeSocketPreviewView(EventBus eventBus, OutputSocket<IntPointer> socket) {
26+
super(eventBus, socket);
27+
28+
x = new TextField();
29+
x.setEditable(false);
30+
y = new TextField();
31+
y.setEditable(false);
32+
33+
final GridPane gridPane = new GridPane();
34+
gridPane.add(x, 1, 0);
35+
gridPane.add(y, 1, 1);
36+
37+
// The only difference between point and size previews is the labels
38+
if (socket.getSocketHint().getType().equals(Point.class)) {
39+
gridPane.add(new Label("x: "), 0, 0);
40+
gridPane.add(new Label("y: "), 0, 1);
41+
} else if (socket.getSocketHint().getType().equals(Size.class)) {
42+
gridPane.add(new Label("width: "), 0, 0);
43+
gridPane.add(new Label("height: "), 0, 1);
44+
}
45+
46+
this.updateTextFields();
47+
this.setContent(gridPane);
48+
}
49+
50+
@Subscribe
51+
public void onSocketChanged(SocketChangedEvent event) {
52+
if (event.getSocket() == this.getSocket()) {
53+
this.updateTextFields();
54+
}
55+
}
56+
57+
private void updateTextFields() {
58+
this.x.setText("" + this.getSocket().getValue().get(0));
59+
this.y.setText("" + this.getSocket().getValue().get(1));
60+
}
61+
}

src/main/java/edu/wpi/grip/ui/preview/SocketPreviewViewFactory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
import edu.wpi.grip.core.OutputSocket;
55
import edu.wpi.grip.core.operations.composite.BlobsReport;
66
import edu.wpi.grip.core.operations.composite.LinesReport;
7+
import org.bytedeco.javacpp.IntPointer;
78

89
import static org.bytedeco.javacpp.opencv_core.Mat;
910

11+
import static org.bytedeco.javacpp.opencv_core.Point;
12+
import static org.bytedeco.javacpp.opencv_core.Size;
13+
14+
1015
/**
1116
* Factory for constructing {@link SocketPreviewView}s
1217
*/
@@ -21,6 +26,8 @@ public class SocketPreviewViewFactory {
2126
public static <T> SocketPreviewView<T> createPreviewView(EventBus eventBus, OutputSocket<T> socket) {
2227
if (socket.getSocketHint().getType() == Mat.class) {
2328
return (SocketPreviewView) new ImageSocketPreviewView(eventBus, (OutputSocket<Mat>) socket);
29+
} else if (socket.getSocketHint().getType() == Point.class || socket.getSocketHint().getType() == Size.class) {
30+
return (SocketPreviewView) new PointSizeSocketPreviewView(eventBus, (OutputSocket<IntPointer>) socket);
2431
} else if (socket.getSocketHint().getType() == LinesReport.class) {
2532
return (SocketPreviewView) new LinesSocketPreviewView(eventBus, (OutputSocket<LinesReport>) socket);
2633
} else if (socket.getSocketHint().getType() == BlobsReport.class) {

0 commit comments

Comments
 (0)