Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRisenPhoenix committed Nov 8, 2023
1 parent f946588 commit 02a2750
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
38 changes: 23 additions & 15 deletions src/main/java/controller/AutoTrackController.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class AutoTrackController implements Controller {

private final ObservableList<Point3> clicked_image_points = FXCollections.observableArrayList();
private final ObservableList<Point3> clicked_tracker_points = FXCollections.observableArrayList();
private Mat cachedTransformMatrix = null;
private boolean useVerticalFieldGenerator = false;

@Override
public void initialize(URL location, ResourceBundle resources) {
Expand All @@ -125,6 +127,8 @@ public void initialize(URL location, ResourceBundle resources) {
videoImagePlot.setData(dataSeries);
videoImagePlot.registerImageClickedHandler(this::onImageClicked);

useVerticalFieldGenerator = userPreferencesGlobal.getBoolean("verticalFieldGenerator", false);

loadAvailableVideoDevicesAsync();
}

Expand Down Expand Up @@ -288,6 +292,7 @@ private void updateTrackingData(){
var x_normalized = shifted_points[0] / currentShowingImage.getWidth();
var y_normalized = shifted_points[1] / currentShowingImage.getHeight();
lastTrackingData.add(new ExportMeasurement(tool.getName(), point.getX(), point.getY(), point.getZ(), shifted_points[0], shifted_points[1], shifted_points[2], x_normalized, y_normalized));
System.out.println("x: " + shifted_points[0] + " y: " + shifted_points[1] + " z: " + shifted_points[2]);

data.add(new XYChart.Data<>(shifted_points[0], shifted_points[1]));

Expand Down Expand Up @@ -412,6 +417,7 @@ public void on_importMatrix() {
transformationMatrix = TransformationMatrix.loadFromJSON(path);
roiDirty = true;
regMatrixStatusBox.setSelected(true);
cachedTransformMatrix = null;
userPreferences.put("matrixDirectory", inputFile.getAbsoluteFile().getParent());
}catch (FileNotFoundException e) {
logger.log(java.util.logging.Level.SEVERE, "Error loading matrix", e);
Expand All @@ -429,6 +435,7 @@ public void on_reloadMatrix(){
transformationMatrix = TransformationMatrix.loadFromJSON(lastMatrixPath);
roiDirty = true;
regMatrixStatusBox.setSelected(true);
cachedTransformMatrix = null;
}catch (FileNotFoundException e) {
logger.log(java.util.logging.Level.SEVERE, "Error loading matrix", e);
e.printStackTrace();
Expand All @@ -443,12 +450,7 @@ public void on_generateMatrix(){
transformationMatrix.trackingPoints = new float[4][];
for(int i = 0;i<clicked_image_points.size();i++){
transformationMatrix.imagePoints[i] = new float[]{(float) clicked_image_points.get(i).x, (float) clicked_image_points.get(i).y, (float) clicked_image_points.get(i).z};

if(userPreferencesGlobal.getBoolean("verticalFieldGenerator", false)) {
transformationMatrix.trackingPoints[i] = new float[]{(float) clicked_tracker_points.get(i).x, (float) clicked_tracker_points.get(i).z, (float) clicked_tracker_points.get(i).y};
}else {
transformationMatrix.trackingPoints[i] = new float[]{(float) clicked_tracker_points.get(i).x, (float) clicked_tracker_points.get(i).y, (float) clicked_tracker_points.get(i).z};
}
transformationMatrix.trackingPoints[i] = new float[]{(float) clicked_tracker_points.get(i).x, (float) clicked_tracker_points.get(i).y, (float) clicked_tracker_points.get(i).z};
}

FileChooser fileChooser = new FileChooser();
Expand All @@ -460,6 +462,9 @@ public void on_generateMatrix(){
if(saveFile != null){
try {
transformationMatrix.saveToJSON(saveFile);
this.transformationMatrix = transformationMatrix;
regMatrixStatusBox.setSelected(true);
cachedTransformMatrix = null;
userPreferences.put("matrixDirectory", saveFile.getAbsoluteFile().getParent());
} catch (IOException e) {
logger.log(java.util.logging.Level.SEVERE, "Error saving matrix", e);
Expand All @@ -484,9 +489,9 @@ private File getLastKnownFileLocation(String key, String defaultLocation){
* @return The transformed image
*/
private Mat applyImageTransformations(Mat mat){
Imgproc.warpAffine(mat, mat, transformationMatrix.getTranslationMat(), mat.size());
Imgproc.warpAffine(mat, mat, transformationMatrix.getRotationMat(), mat.size());
Imgproc.warpAffine(mat, mat, transformationMatrix.getScaleMat(), mat.size());
// Imgproc.warpAffine(mat, mat, transformationMatrix.getTranslationMat(), mat.size());
// Imgproc.warpAffine(mat, mat, transformationMatrix.getRotationMat(), mat.size());
// Imgproc.warpAffine(mat, mat, transformationMatrix.getScaleMat(), mat.size());

/*
var imagePoints = transformationMatrix.getImagePoints();
Expand Down Expand Up @@ -524,23 +529,26 @@ private Mat applyImageTransformations(Mat mat){
* @param z Z-Coordinate of the point - Ignored in the 2d version
* @return The transformed point as array of length 3 (xyz)
*/
private double[] applyTrackingTransformation2d(double x, double y, double z){
var matrix = transformationMatrix.getTransformMatOpenCvEstimated2d();
private double[] applyTrackingTransformation2d(double x, double y, double z) {
// TODO: Cache matrix
if (cachedTransformMatrix == null){
cachedTransformMatrix = transformationMatrix.getTransformMatOpenCvEstimated2d();
}
var vector = new Mat(3,1, CvType.CV_64F);
vector.put(0,0,x);
if(userPreferencesGlobal.getBoolean("verticalFieldGenerator", false)){
if(useVerticalFieldGenerator){
vector.put(1,0,z);
}else{
vector.put(1, 0, y);
}
vector.put(2,0,1);

var pos_star = new Mat(2,1,CvType.CV_64F);
Core.gemm(matrix, vector,1, new Mat(),1,pos_star);
Core.gemm(cachedTransformMatrix, vector,1, new Mat(),1,pos_star);
double[] out = new double[3];
out[0] = pos_star.get(0,0)[0];
out[1] = pos_star.get(1,0)[0];
out[2] = z;
out[2] = 0;
return out;
}

Expand Down Expand Up @@ -585,7 +593,7 @@ private void onImageClicked(double x, double y){
}

clicked_image_points.add(new Point3(x, y, 0.0));
if(userPreferencesGlobal.getBoolean("verticalFieldGenerator", false)) {
if(useVerticalFieldGenerator) {
clicked_tracker_points.add(new Point3(trackingData.get(0).x_raw, trackingData.get(0).z_raw, trackingData.get(0).y_raw));
}else {
clicked_tracker_points.add(new Point3(trackingData.get(0).x_raw, trackingData.get(0).y_raw, trackingData.get(0).z_raw));
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/view/SettingsView.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<CheckBox fx:id="searchForMoreVideos" mnemonicParsing="false" onAction="#onSearchForMoreVideosClicked"
text="Search for more video devices"/>

<CheckBox fx:id="Vertical Fieldgenerator" mnemonicParsing="false" onAction="#onVerticalFGClicked"
<CheckBox fx:id="verticalFG" mnemonicParsing="false" onAction="#onVerticalFGClicked"
text="2d transform: replace y by z"/>
</VBox>
</TitledPane>
Expand Down

0 comments on commit 02a2750

Please sign in to comment.