This commit is contained in:
sfop
2017-03-24 09:54:38 +01:00
parent c1e99edf8b
commit ba32753be7
2 changed files with 85 additions and 39 deletions

View File

@@ -38,6 +38,7 @@ import ch.psi.pshell.imaging.Renderer;
import ch.psi.pshell.imaging.RendererListener;
import ch.psi.pshell.imaging.RendererMode;
import ch.psi.pshell.imaging.Source;
import ch.psi.pshell.imaging.Utils;
import ch.psi.pshell.scripting.InterpreterResult;
import ch.psi.pshell.scripting.ScriptManager;
import ch.psi.pshell.swing.ValueSelection;
@@ -50,6 +51,7 @@ import ch.psi.utils.swing.Editor.EditorDialog;
import ch.psi.utils.swing.MainFrame;
import ch.psi.utils.swing.SwingUtils.OptionResult;
import ch.psi.utils.swing.SwingUtils.OptionType;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
@@ -57,7 +59,6 @@ import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
@@ -69,12 +70,16 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
@@ -206,7 +211,7 @@ public class ScreenPanel extends Panel {
if (App.hasArgument("calc")) {
useCamtoolStats = false;
}
renderer.setProfileNormalized(true);
renderer.setShowProfileLimits(false);
@@ -1040,6 +1045,8 @@ public class ScreenPanel extends Panel {
Double xMean = null, xSigma = null, xNorm = null;
Double yMean = null, ySigma = null, yNorm = null;
double[] pX = null, pY = null, gX = null, gY = null;
int height = data.getHeight();
int width = data.getWidth();
//Double xCom=null, yCom=null;
if (data != null) {
int profileSize = renderer.getProfileSize();
@@ -1083,9 +1090,9 @@ public class ScreenPanel extends Panel {
for (int i = 0; i < x.length; i++) {
if (gX != null) {
y[i] = (int) (data.getHeight() - 1 - (((gX[i] - minPlot) / rangePlot) * profileSize));
y[i] = (int) (height - 1 - (((gX[i] - minPlot) / rangePlot) * profileSize));
}
p[i] = (int) (data.getHeight() - 1 - (((pX[i] - minPlot) / rangePlot) * profileSize));
p[i] = (int) (height - 1 - (((pX[i] - minPlot) / rangePlot) * profileSize));
}
vgaussian = new Overlays.Polyline(penFit, x, y);
vprofile = new Overlays.Polyline(renderer.getPenProfile(), x, p);
@@ -1147,9 +1154,9 @@ public class ScreenPanel extends Panel {
double[] fit = getFitFunction(gaussian, x);
int[] y = new int[x.length];
for (int i = 0; i < x.length; i++) {
y[i] = (int) (data.getHeight() - 1 - ((((fit[i] + min) / data.getHeight() - minPlot) / rangePlot) * profileSize));
p[i] = (int) (data.getHeight() - 1 - (((sum[i] / data.getHeight() - minPlot) / rangePlot) * profileSize));
}
y[i] = (int) (height - 1 - ((((fit[i] + min) / height - minPlot) / rangePlot) * profileSize));
p[i] = (int) (height - 1 - (((sum[i] / height- minPlot) / rangePlot) * profileSize));
}
vgaussian = new Overlays.Polyline(penFit, x, y);
vprofile = new Overlays.Polyline(renderer.getPenProfile(), x, p);
}
@@ -1183,8 +1190,8 @@ public class ScreenPanel extends Panel {
int[] y = new int[x.length];
for (int i = 0; i < x.length; i++) {
y[i] = (int) ((((fit[i] + min) / data.getWidth() - minPlot) / rangePlot) * profileSize);
p[i] = (int) (((sum[i] / data.getWidth() - minPlot) / rangePlot) * profileSize);
y[i] = (int) ((((fit[i] + min) / width - minPlot) / rangePlot) * profileSize);
p[i] = (int) (((sum[i] / width - minPlot) / rangePlot) * profileSize);
}
hgaussian = new Overlays.Polyline(penFit, y, x);
hprofile = new Overlays.Polyline(renderer.getPenProfile(), p, x);
@@ -1207,8 +1214,6 @@ public class ScreenPanel extends Panel {
if (yMean != null) {
yMean = data.getY((int) Math.round(yMean));
}
//if (xCom!=null) xCom = data.getX((int) Math.round(xCom));
//if (yCom!=null) yCom = data.getY((int) Math.round(yCom));
}
}
@@ -1216,9 +1221,6 @@ public class ScreenPanel extends Panel {
Overlays.Text text = null;
if ((xMean != null) && (yMean != null)) {
text = new Overlays.Text(penFit,
//String.format("x: m=%1.3f \u03C3=%1.3f c=%1.3f\ny: m=%1.3f \u03C3=%1.3f c=%1.3f",
// xMean, xSigma, xCom,
// yMean, ySigma, yCom),
String.format("x: m=%1.3f \u03C3=%1.3f\ny: m=%1.3f \u03C3=%1.3f",
xMean, xSigma,
yMean, ySigma),
@@ -1561,7 +1563,9 @@ public class ScreenPanel extends Panel {
JDialog calibrationDialolg;
Overlay[] calibrationOverlays;
void calibrate() throws IOException{
if ( (camera instanceof Camtool) && (calibrationOverlays==null)) {
if ( (camera instanceof Camtool) && (calibrationOverlays==null)) {
Map<String, Object> calibration = ((Camtool) camera).getCalibration(cameraName);
//Map<String, Object> calibration = (Map<String, Object>) ((Camtool) camera).getConfig(cameraName).get("calibration");
((Camtool) camera).resetRoi();
renderer.setMode(RendererMode.Fit);
Pen pen = new Pen(new Color(128, 0, 128), 1, Pen.LineStyle.solid);
@@ -1573,21 +1577,56 @@ public class ScreenPanel extends Panel {
for (Overlay ov : calibrationOverlays){
ov.setMovable(true);
}
renderer.addOverlays(calibrationOverlays);
SwingUtils.invokeDelayed(() -> {
Dimension size = renderer.getImageSize();
top.update(new Point(0, size.height/8));
bottom.update(new Point(0, 7*size.height/8));
left.update(new Point(size.width/8, 0));
right.update(new Point(7*size.width/8, 0));
}, 500);
JOptionPane calibrationMessage = new JOptionPane("Move the lines to the reference marks \n" +
"and press 'OK' to update the calibration.",
JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null,null, null);
renderer.addOverlays(calibrationOverlays);
try{
List refMarker = ((List)calibration.get("reference_marker"));
Rectangle r = new Rectangle();
left.update(new Point(Math.max((Integer)refMarker.get(0), 0), 0));
top.update(new Point(0, Math.max((Integer)refMarker.get(1), 0)));
right.update(new Point(Math.max((Integer)refMarker.get(2), 0), 0));
bottom.update(new Point(0, Math.max((Integer)refMarker.get(3),0)));
} catch (Exception ex){
SwingUtils.invokeDelayed(() -> {
Dimension size = renderer.getImageSize();
top.update(new Point(0, size.height/8));
bottom.update(new Point(0, 7*size.height/8));
left.update(new Point(size.width/8, 0));
right.update(new Point(7*size.width/8, 0));
}, 500);
}
JPanel panel = new JPanel(new BorderLayout());
JTextField textWidth = new JTextField();
JTextField textHeight= new JTextField();
if (calibration!=null){
textWidth.setText(String.valueOf(calibration.get("reference_marker_width")));
textHeight.setText(String.valueOf(calibration.get("reference_marker_height")));
}
((BorderLayout) panel.getLayout()).setVgap(10);
JPanel p1 = new JPanel(new BorderLayout());
((BorderLayout) p1.getLayout()).setHgap(5);
p1.add(new JLabel("Move the lines to the reference marks"), BorderLayout.NORTH);
p1.add(new JLabel("and press 'OK' to update the calibration."), BorderLayout.SOUTH);
panel.add(p1, BorderLayout.NORTH);
JPanel p2 = new JPanel(new BorderLayout());
((BorderLayout) p2.getLayout()).setHgap(5);
p2.add(new JLabel("Reference marker width: "), BorderLayout.WEST);
p2.add(textWidth, BorderLayout.EAST);
panel.add(p2, BorderLayout.CENTER);
JPanel p3 = new JPanel(new BorderLayout());
((BorderLayout) p3.getLayout()).setHgap(5);
p3.add(new JLabel("Reference marker height: "), BorderLayout.WEST);
p3.add(textHeight, BorderLayout.EAST);
panel.add(p3, BorderLayout.SOUTH);
textWidth.setPreferredSize(new Dimension(70, textWidth.getPreferredSize().height));
textHeight.setPreferredSize(new Dimension(70, textHeight.getPreferredSize().height));
JOptionPane calibrationMessage = new JOptionPane(panel,JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null,null, null);
calibrationDialolg = calibrationMessage.createDialog(getTopLevel(), "Calibration");
calibrationDialolg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
calibrationDialolg.setModal(false);
calibrationDialolg.addWindowListener(new WindowAdapter() {
calibrationDialolg.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
if (calibrationOverlays!=null){
@@ -1601,10 +1640,17 @@ public class ScreenPanel extends Panel {
int y1 = Math.min(top.getPosition().y, bottom.getPosition().y);
int y2 = Math.max(top.getPosition().y, bottom.getPosition().y);
if ((x1!=-1) && (x2!=-1) && (y1!=-1) && (y2!=-1)){
System.out.println("Updating camera calibration: " + x1 + ", "+ + x2 + ", " + + y1 + ", " + + y2);
System.out.println("Updating " + cameraName + " calibration: " + x1 + ", "+ x2 + ", " + y1 + ", " + y2);
HashMap c = new HashMap();
c.put("reference_marker", Arrays.asList(new Integer[]{x1, y1, x2, y2}));
if (textWidth.getText().trim().length() > 0){
c.put("reference_marker_width", Integer.valueOf(textWidth.getText()));
}
if (textHeight.getText().trim().length() > 0){
c.put("reference_marker_height", Integer.valueOf(textHeight.getText()));
}
((Camtool) camera).setCalibration(cameraName, c);
Logger.getLogger(ScreenPanel.class.getName()).warning("Updated " + cameraName + " calibration: " + x1 + ", "+ x2 + ", " + y1 + ", " + y2);
}
}
} catch (IOException ex) {