Files
sf-op/plugins/Cameras.java
2016-09-08 17:54:48 +02:00

657 lines
29 KiB
Java

/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
import ch.psi.pshell.core.Controller;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import javax.swing.DefaultComboBoxModel;
import ch.psi.pshell.ui.Panel;
import ch.psi.pshell.imaging.ImageListener;
import ch.psi.utils.State;
import ch.psi.utils.IO;
import ch.psi.utils.swing.SwingUtils;
import ch.psi.utils.swing.TextEditor;
import ch.psi.pshell.epics.PsiCamera;
import ch.psi.pshell.epics.Camtool;
import ch.psi.pshell.core.JsonSerializer;
import ch.psi.pshell.epics.ArraySource;
import ch.psi.pshell.ui.App;
import ch.psi.pshell.imaging.Data;
import ch.psi.pshell.imaging.Overlay;
import ch.psi.pshell.imaging.Overlays;
import ch.psi.pshell.imaging.Pen;
import ch.psi.pshell.imaging.PointDouble;
import ch.psi.pshell.imaging.Renderer.Profile;
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.utils.Arr;
import ch.psi.utils.ArrayProperties;
import ch.psi.utils.Convert;
import ch.psi.utils.swing.Editor.EditorDialog;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.math3.analysis.function.Gaussian;
import org.apache.commons.math3.fitting.GaussianCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoint;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
/**
*
*/
public class Cameras extends Panel {
public Cameras() {
initComponents();
renderer.setPersistenceFile(Paths.get(getController().getSetup().getContextPath(), "Renderer_Cameras.bin"));
renderer.setProfileFactor(4);
setPersistedComponents(new Component[]{checkCamtool});
comboCameras.setEnabled(false);
if (App.hasArgument("poll")) {
try{
polling = Integer.valueOf(App.getArgumentValue("poll"));
} catch (Exception ex){
ex.printStackTrace();
}
}
if (App.hasArgument("zoom")) {
try{
renderer.setDefaultZoom(Double.valueOf(App.getArgumentValue("zoom")));
} catch (Exception ex){
ex.printStackTrace();
}
}
}
@Override
public void onStart() {
super.onStart();
if (App.hasArgument("ct")) {
checkCamtool.setSelected(!App.getArgumentValue("ct").equals("0") && !App.getArgumentValue("ct").equalsIgnoreCase("false"));
}
}
final String configFolder = "/afs/psi.ch/intranet/SF/Applications/config/camtool";
File[] cameraConfigFiles = new File[0];
ArraySource camera;
String cameraName;
String cameraConfigJson;
CameraConfig config;
int polling = 1000;
//Overridable callbacks
@Override
public void onInitialize(int runCount) {
comboCameras.setEnabled(false);
if (App.hasArgument("s")){
renderer.setDevice((Source)getDevice("image"));
renderer.setAutoScroll(true);
((Source)getDevice("image")).addListener(new ImageListener() {
@Override
public void onImage(Object o, BufferedImage bi, Data data) {
if (bi == null) {
fitOv = null;
} else {
Overlay[] profile = renderer.getProfileOverlays();
profile = ((profile != null) && (profile.length==4)) ? getFitOverlays(bi) : null;
renderer.updateOverlays(profile, fitOv);
fitOv = profile;
}
}
@Override
public void onError(Object o, Exception excptn) {
}
}
);
} else {
cameraConfigFiles = IO.listFiles(configFolder, new String[]{"json"});
Arrays.sort(cameraConfigFiles, (a, b) -> a.compareTo(b));
DefaultComboBoxModel model = new DefaultComboBoxModel();
for (File file : cameraConfigFiles) {
String prefix = IO.getPrefix(file);
if (!prefix.startsWith("#")) {
model.addElement(prefix);
}
}
comboCameras.setModel(model);
comboCameras.setEnabled(true);
comboCameras.setSelectedItem(-1);
if (model.getSize() > 0) {
try {
//setCamera((String)comboCameras.getSelectedItem());
if (App.hasArgument("cam")) {
comboCameras.setSelectedItem(App.getArgumentValue("cam"));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
startTimer(1000);
}
@Override
public void onStateChange(State state, State former) {
}
@Override
public void onExecutedFile(String fileName, Object result) {
}
//Callback to perform update - in event thread
@Override
protected void doUpdate() {
}
public static class CameraConfig {
public HashMap kwargs;
public ArrayList args;
public ArrayList links;
public String type;
public HashMap<String, Object> state;
public CameraConfig image_source;
public HashMap<String, CameraConfig> subcomponents;
public ArrayList<Integer> getCalibration() {
return (ArrayList<Integer>) state.get("calibration");
}
public Double getCalibrationHeight() {
return (Double) state.get("calibration_height");
}
public Double getCalibrationWidth() {
return (Double) state.get("calibration_width");
}
public Double getCalibrationHorizontalAngle() {
return (Double) state.get("calibration_horizontal_angle");
}
public Double getCalibrationVerticalAngle() {
return (Double) state.get("calibration_vertical_angle");
}
public Boolean getMirrorX() {
return (Boolean) state.get("mirror_x");
}
public Boolean getMirrorY() {
return (Boolean) state.get("mirror_y");
}
public Integer getRotate() {
return (Integer) state.get("rotate");
}
public ArrayList<Integer> getOrigin() {
return (ArrayList<Integer>) state.get("origin");
}
public ArrayList<Integer> getRoi() {
return (ArrayList<Integer>) state.get("roi");
}
public Boolean getRoiEnable() {
if ((state.get("roi_enable") == null) || (state.get("roi") == null)) {
return false;
}
return (Boolean) state.get("roi_enable");
}
public ArrayList<Double> getUnitSize() {
return (ArrayList<Double>) state.get("unit_size");
}
public Integer getRun() {
return (Integer) state.get("run");
}
}
Overlay[] fitOv;
void setCamera(String cameraName) throws IOException, InterruptedException {
System.out.println("Setting camera: " + cameraName);
if (camera != null) {
camera.close();
camera = null;
renderer.setDevice(null);
renderer.setShowReticle(false);
renderer.clear();
}
try {
Path configFile = Paths.get(configFolder, cameraName + ".json");
cameraConfigJson = new String(Files.readAllBytes(configFile));
this.cameraName = cameraName;
//SwingUtils.showMessage(null, "", json);
try {
if (checkCamtool.isSelected()) {
camera = new Camtool("CurrentCamera", cameraName, false, true);
camera.getConfig().flipHorizontally = false;
camera.getConfig().flipVertically = false;
camera.getConfig().rotation = 0.0;
camera.getConfig().roiX = 0;
camera.getConfig().roiY = 0;
camera.getConfig().roiWidth = -1;
camera.getConfig().roiHeight = -1;
} else {
camera = new PsiCamera("CurrentCamera", cameraName);
config = (CameraConfig) JsonSerializer.decode(cameraConfigJson, CameraConfig.class);
camera.getConfig().flipHorizontally = config.getMirrorX();
camera.getConfig().flipVertically = config.getMirrorY();
camera.getConfig().rotation = config.getRotate();
camera.getConfig().rotationCrop = true;
camera.getConfig().roiX = config.getRoiEnable() ? config.getRoi().get(0) : 0;
camera.getConfig().roiY = config.getRoiEnable() ? config.getRoi().get(1) : 0;
camera.getConfig().roiWidth = config.getRoiEnable() ? config.getRoi().get(2) : -1;
camera.getConfig().roiHeight = config.getRoiEnable() ? config.getRoi().get(3) : -1;
//camera.getConfig().spatialCalOffsetX = (config.getOrigin()!=null) ? config.getOrigin().get(0) : Double.NaN;
//camera.getConfig().spatialCalOffsetY = (config.getOrigin()!=null) ? config.getOrigin().get(1) : Double.NaN;
//camera.getConfig().spatialCalScaleX = (config.getUnitSize()!=null) && (config.getOrigin().get(0)!=0) ? 1.0/(config.getOrigin().get(0)*1000) : Double.NaN;
//camera.getConfig().spatialCalScaleY= (config.getUnitSize()!=null) && (config.getOrigin().get(1)!=0) ? 1.0/(config.getOrigin().get(1)*1000) : Double.NaN;
ArrayList<Integer> calibration = config.getCalibration();
if ((calibration != null) && (calibration.size() != 4)) {
calibration = null;
}
camera.getConfig().spatialCalOffsetX = (calibration != null) ? -calibration.get(2) : Double.NaN;
camera.getConfig().spatialCalOffsetY = (calibration != null) ? -calibration.get(3) : Double.NaN;
camera.getConfig().spatialCalScaleX = (calibration != null) && (calibration.get(0) != 0) ? 1.0 / (calibration.get(0)) : Double.NaN;
camera.getConfig().spatialCalScaleY = (calibration != null) && (calibration.get(1) != 0) ? 1.0 / (calibration.get(1)) : Double.NaN;
}
} catch (Exception ex) {
config = null;
showException(ex);
}
camera.initialize();
if (camera instanceof Camtool){
try{
camera.getConfig().spatialCalOffsetX = ((Camtool)camera).calOffX.read();
camera.getConfig().spatialCalOffsetY = ((Camtool)camera).calOffY.read();
camera.getConfig().spatialCalScaleX = ((Camtool)camera).calScaleX.read();
camera.getConfig().spatialCalScaleY = ((Camtool)camera).calScaleY.read();
} catch (Exception ex){
ex.printStackTrace();
camera.getConfig().spatialCalOffsetX = 0.0;
camera.getConfig().spatialCalOffsetY = 0.0;
camera.getConfig().spatialCalScaleX = 1.0;
camera.getConfig().spatialCalScaleY = 1.0;
}
/*
double[] origin = ((Camtool)camera).origin.read();
if (origin.length>=2){
camera.getConfig().spatialCalOffsetX = origin[0];
camera.getConfig().spatialCalOffsetY = origin[1];
}
*/
}
camera.getConfig().save();
if (polling<=0){
camera.setMonitored(true);
} else {
camera.setPolling(-polling);
}
renderer.setDevice(camera);
renderer.setAutoScroll(true);
camera.addListener(new ImageListener() {
@Override
public void onImage(Object o, BufferedImage bi, Data data) {
if (bi == null) {
renderer.removeOverlays(fitOv);
fitOv = null;
} else {
//if (renderer.getReticle() != null) {
// renderer.getReticle().setSize(new Dimension(bi.getWidth(), bi.getHeight()));
//}
//long start = System.currentTimeMillis();
Overlay[] profile = renderer.getProfileOverlays();
//System.out.println(System.currentTimeMillis() - start);
profile = ((profile != null) && (profile.length==4)) ? getFitOverlays(bi) : null;
renderer.updateOverlays(profile, fitOv);
fitOv = profile;
}
//camera.removeListener(this);
}
@Override
public void onError(Object o, Exception excptn) {
}
});
} finally {
if (renderer.getZoom() > 1.0){
renderer.resetZoom();
}
if (camera != null) {
if (camera.getConfig().isCalibrated()){
System.out.println("Calibrated");
renderer.setCalibration(camera.getCalibration());
renderer.configureReticle(new Dimension(800,800), 200);
renderer.setShowReticle(true);
} else{
System.out.println("Not Calibrated");
}
}
onTimer();
}
}
@Override
protected void onTimer() {
textState.setText((camera == null) ? "" : camera.getState().toString());
buttonConfig.setEnabled(camera != null);
if (App.hasArgument("s")){
try {
((Source)getDevice("image")).initialize();
} catch (IOException ex) {
Logger.getLogger(Cameras.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(Cameras.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Pen fitPen = new Pen(new Color(192, 105, 0), 1);
Pen crossPen = new Pen(new Color(192, 105, 0), 1);
Overlay[] getFitOverlays(BufferedImage img){
Overlays.Polyline hpoly = null;
Overlays.Polyline vpoly = null;
Double xMean = null; Double xSigma = null;
Double yMean = null; Double ySigma = null;
Profile profile = renderer.getProfile();
if ((profile != Profile.None) && (img != null)) {
img = Utils.grayscale(img);
if (profile.hasVertical()) {
try {
double[] sum = (double[]) Convert.toDouble(Utils.integrateVertically(img));
int[] x = Arr.indexesInt(sum.length);
DescriptiveStatistics stats = new DescriptiveStatistics(sum);
double min = stats.getMin();
for (int i=0; i< sum.length; i++){
sum[i] = sum[i] - min;
}
double[] gaussian = fitGaussian(sum, x);
if (gaussian!=null){
// System.out.println("Norm: " + gaussian[0] + " Mean: " + gaussian[1] + " Sigma: " + gaussian[2]+ " Min: " + min);
//Only aknowledge beam fully inside the image and peak over 3% of min
if ((gaussian[2] < sum.length * 0.45) && (gaussian[0] > min * 0.03)){
xMean = gaussian[1];
xSigma = gaussian[2];
gaussian[0]+=min;
double[] fit = getFitFunction(gaussian, x);
int[] y = new int[x.length];
for (int i = 0; i < x.length; i++) {
y[i] = (int) (img.getHeight() - 1 - (((double) fit[i]) / 255 / renderer.getProfileFactor()));
}
vpoly = new Overlays.Polyline(fitPen, x, y);
}
} else {
//System.out.println("Fit failure");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (profile.hasHorizontal()) {
try {
double[] sum = (double[]) Convert.toDouble(Utils.integrateHorizontally(img));
int[] x = Arr.indexesInt(sum.length);
DescriptiveStatistics stats = new DescriptiveStatistics(sum);
double min = stats.getMin();
for (int i=0; i< sum.length; i++){
sum[i] = sum[i] - min;
}
double[] gaussian = fitGaussian(sum, x);
if (gaussian!=null){
//Only aknowledge beam fully inside the image and peak over 3% of min
if ((gaussian[2] < sum.length * 0.45) && (gaussian[0] > min * 0.03)){
yMean = gaussian[1];
ySigma = gaussian[2];
gaussian[0]+=min;
double[] fit = getFitFunction(gaussian, x);
int[] y = new int[x.length];
for (int i = 0; i < x.length; i++) {
y[i] = (int) (((double) fit[i]) / 255 / renderer.getProfileFactor());
}
hpoly = new Overlays.Polyline(fitPen, y, x);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Overlays.Crosshairs cross= null;
if ((xMean!=null) && (yMean!=null)){
cross = new Overlays.Crosshairs(crossPen,
new Point(xMean.intValue(), yMean.intValue()),
new Dimension(2*xSigma.intValue(),2*ySigma.intValue()));
}
return new Overlay[]{hpoly, vpoly, cross};
}
return null;
}
double[] fitGaussianScript(int[] y, int[] x){
ScriptManager sm = Controller.getInstance().getScriptManager();
ArrayProperties pY = ArrayProperties.get(y);
sm.setVar("y", y);
sm.setVar("x", x);
InterpreterResult r = sm.eval("r = fit_gaussians(y, x, [" + pY.maxIndex+ ",])");
if (r.exception != null){
r.exception .printStackTrace();
} else {
List ret = (List) sm.getVar("r");
if ((ret!=null) && (ret.size()==1)&& (ret.get(0) instanceof List) && (((List)(ret.get(0))).size()==3)){
double norm = (Double) ((List)ret.get(0)).get(0);
double mean = (Double) ((List)ret.get(0)).get(1);
double sigma = (Double) ((List)ret.get(0)).get(2);
return new double[]{norm, mean, sigma};
}
}
return null;
}
double[]fitGaussian(double[] y, int[] x){
try{
ArrayProperties pY = ArrayProperties.get(y);
GaussianCurveFitter fitter = GaussianCurveFitter.create().withStartPoint(new double[]{(pY.max-pY.min)/2,x[pY.maxIndex],1.0}).withMaxIterations(1000);
ArrayList<WeightedObservedPoint>values = new ArrayList<>();
for (int i=0; i< y.length; i++){
values.add(new WeightedObservedPoint(1.0, x[i], y[i]));
}
return fitter.fit(values);
} catch (Exception ex){
return null;
}
}
double[] getFitFunction(double[] pars, int[] x){
double[] fit = new double[x.length];
Gaussian g = new Gaussian(pars[0], pars[1], pars[2]);
for (int i=0; i< x.length; i++){
fit[i] = g.value(x[i]);
}
return fit;
}
////////
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
renderer = new ch.psi.pshell.imaging.Renderer();
jLabel1 = new javax.swing.JLabel();
comboCameras = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
textState = new javax.swing.JTextField();
buttonConfig = new javax.swing.JButton();
buttonSetup = new javax.swing.JButton();
checkCamtool = new javax.swing.JCheckBox();
jLabel1.setText("Camera:");
comboCameras.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboCamerasActionPerformed(evt);
}
});
jLabel2.setText("State:");
textState.setEditable(false);
textState.setHorizontalAlignment(javax.swing.JTextField.CENTER);
textState.setDisabledTextColor(new java.awt.Color(0, 0, 0));
textState.setEnabled(false);
buttonConfig.setText("Config");
buttonConfig.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonConfigActionPerformed(evt);
}
});
buttonSetup.setText("Setup");
buttonSetup.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonSetupActionPerformed(evt);
}
});
checkCamtool.setSelected(true);
checkCamtool.setText("Camtool");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(renderer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(checkCamtool)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(comboCameras, 0, 218, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonConfig)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonSetup)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textState, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonConfig, buttonSetup});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(comboCameras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(textState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonConfig)
.addComponent(buttonSetup)
.addComponent(checkCamtool))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(renderer, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
.addGap(6, 6, 6))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {comboCameras, textState});
}// </editor-fold>//GEN-END:initComponents
private void comboCamerasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCamerasActionPerformed
try {
comboCameras.setEnabled(false);
new Thread(new Runnable() {
@Override
public void run() {
try{
setCamera((String) comboCameras.getSelectedItem());
} catch(Exception ex){
ex.printStackTrace();
} finally{
comboCameras.setEnabled(true);
}
}
}).start();
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_comboCamerasActionPerformed
private void buttonConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonConfigActionPerformed
try {
if (camera != null) {
TextEditor editor = new TextEditor();
editor.setText(cameraConfigJson);
editor.setReadOnly(true);
editor.setTitle(cameraName);
EditorDialog dlg = editor.getDialog(getTopLevel(), false);
dlg.setSize(480, 640);
dlg.setVisible(true);
SwingUtils.centerComponent(this, dlg);
}
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_buttonConfigActionPerformed
private void buttonSetupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSetupActionPerformed
try {
if (camera != null) {
this.showDeviceConfigDialog(camera, false);
}
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_buttonSetupActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton buttonConfig;
private javax.swing.JButton buttonSetup;
private javax.swing.JCheckBox checkCamtool;
private javax.swing.JComboBox comboCameras;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private ch.psi.pshell.imaging.Renderer renderer;
private javax.swing.JTextField textState;
// End of variables declaration//GEN-END:variables
}