From f041d6a6c4076180f718e8e72fe2736cba6b7611 Mon Sep 17 00:00:00 2001 From: sfop Date: Mon, 29 May 2017 10:03:02 +0200 Subject: [PATCH] Startup --- devices/cam3.properties | 28 ++++++ plugins/SfCamera.java | 209 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 devices/cam3.properties create mode 100644 plugins/SfCamera.java diff --git a/devices/cam3.properties b/devices/cam3.properties new file mode 100644 index 0000000..763c782 --- /dev/null +++ b/devices/cam3.properties @@ -0,0 +1,28 @@ +#Mon May 29 10:03:01 CEST 2017 +colormap=Grayscale +colormapAutomatic=false +colormapMax=NaN +colormapMin=NaN +flipHorizontally=false +flipVertically=false +grayscale=false +imageHeight=1024 +imageWidth=1280 +invert=false +regionStartX=1 +regionStartY=1 +rescaleFactor=1.0 +rescaleOffset=0.0 +roiHeight=-1 +roiWidth=-1 +roiX=0 +roiY=0 +rotation=0.0 +rotationCrop=false +scale=1.0 +spatialCalOffsetX=NaN +spatialCalOffsetY=NaN +spatialCalScaleX=NaN +spatialCalScaleY=NaN +spatialCalUnits=mm +transpose=false diff --git a/plugins/SfCamera.java b/plugins/SfCamera.java new file mode 100644 index 0000000..0d84a80 --- /dev/null +++ b/plugins/SfCamera.java @@ -0,0 +1,209 @@ + +import ch.psi.pshell.core.JsonSerializer; +import ch.psi.pshell.epics.PsiCamera; +import ch.psi.utils.IO; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import javax.swing.DefaultComboBoxModel; + +public class SfCamera extends PsiCamera{ + + final static String CONFIG_FOLDER = "/afs/psi.ch/intranet/SF/Applications/config/camtool_n"; + + public static DefaultComboBoxModel getCameras() { + File[] cameraConfigFiles = new File[0]; + cameraConfigFiles = IO.listFiles(CONFIG_FOLDER, 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("#") && !prefix.endsWith("_parameters")) { + model.addElement(prefix); + } + } + return model; + } + + public static class CameraConfig { + + public HashMap camera; + + public HashMap getCalibration() { + return (HashMap) camera.get("calibration"); + } + + public ArrayList getCalibrationRefMarker() { + return (ArrayList) getCalibration().get("reference_marker"); + } + + public double getCalOffsetX() { + ArrayList calibration = getCalibrationRefMarker(); + double ret = -(calibration.get(0) + calibration.get(2)) / 2; + return ret; + } + + public double getCalOffsetY() { + ArrayList calibration = getCalibrationRefMarker(); + double ret = -(calibration.get(1) + calibration.get(3)) / 2; + return ret; + } + + public double getScaleX() { + ArrayList calibration = getCalibrationRefMarker(); + double width = Math.abs(calibration.get(2) - calibration.get(0)); + return getCalibrationWidth() / width; + } + + public double getScaleY() { + ArrayList calibration = getCalibrationRefMarker(); + double height = Math.abs(calibration.get(3) - calibration.get(1)); + return getCalibrationHeight() / height; + } + + public Double getCalibrationHeight() { + return (Double) getCalibration().get("reference_marker_height"); + } + + public Double getCalibrationWidth() { + return (Double) getCalibration().get("reference_marker_width"); + } + + public Double getCalibrationHorizontalAngle() { + return (Double) getCalibration().get("horizontal_camera_angle"); + } + + public Double getCalibrationVerticalAngle() { + return (Double) getCalibration().get("vertical_camera_angle"); + } + + public boolean getMirrorX() { + Boolean ret = (Boolean) camera.get("mirror_x"); + return (ret == null) ? false : ret; + } + + public boolean getMirrorY() { + Boolean ret = (Boolean) camera.get("mirror_y"); + return (ret == null) ? false : ret; + } + + public int getRotate() { + Integer ret = (Integer) camera.get("rotate"); + return (ret == null) ? 0 : ret; + } + + public ArrayList getRoi() { + return (ArrayList) camera.get("roi"); + } + + public Boolean getRoiEnable() { + if ((camera.get("roi_enable") == null) || (camera.get("roi") == null)) { + return false; + } + return (Boolean) camera.get("roi_enable"); + } + } + + public static class CameraPars { + + public HashMap parameter; + + public Boolean getBackgroundSubtraction() { + if (parameter.get("background_subtration") == null){ + return false; + } + return (Boolean) parameter.get("background_subtration"); + } + + public ArrayList getRoi() { + return (ArrayList) parameter.get("region_of_interest"); + } + + public Boolean getRoiEnable() { + ArrayList roi = getRoi(); + return ((roi != null) && (roi.size()>=4)); + } + + public HashMap getGoodRegion() { + return (HashMap) parameter.get("good_region"); + } + + public Double getThresholde() { + return (Double) parameter.get("threshold"); + } + + + } + + + public final CameraConfig cameraConfig; + public final String cameraJson; + + public SfCamera(String name, String prefix) throws Exception { + super(name, prefix); + Path configFile = Paths.get(CONFIG_FOLDER, prefix + ".json"); + cameraJson = configFile.toFile().exists() ? new String(Files.readAllBytes(configFile)) : null; + cameraConfig = (CameraConfig) JsonSerializer.decode(cameraJson, CameraConfig.class); + + CameraPars cameraPars = null; + getConfig().roiX = 0; getConfig().roiY = 0; + getConfig().roiWidth = -1; getConfig().roiHeight =-1; + + try{ + Path parsFile = Paths.get(CONFIG_FOLDER, prefix + "_parameters.json"); + if (parsFile.toFile().exists()){ + String cameraParsJson = new String(Files.readAllBytes(parsFile)); + cameraPars = (CameraPars) JsonSerializer.decode(cameraParsJson, CameraPars.class); + if (cameraPars.getRoiEnable()){ + getConfig().roiX = cameraPars.getRoi().get(0); + getConfig().roiY = cameraPars.getRoi().get(2); + getConfig().roiWidth = cameraPars.getRoi().get(1); + getConfig().roiHeight = cameraPars.getRoi().get(3); + } + + } + } catch (Exception ex){ + ex.printStackTrace(); + } + + getConfig().flipHorizontally = cameraConfig.getMirrorX(); + getConfig().flipVertically = cameraConfig.getMirrorY(); + //getConfig().rotation = config.getRotate(); + //getConfig().rotationCrop = true; + switch (cameraConfig.getRotate()) { + case 1: + getConfig().rotation = 270; + break; + case 2: + getConfig().rotation = 180; + break; + case 3: + getConfig().rotation = 90; + break; + } + getConfig().rotationCrop = false; + try { + getConfig().spatialCalOffsetX = cameraConfig.getCalOffsetX(); + getConfig().spatialCalOffsetY = cameraConfig.getCalOffsetY(); + getConfig().spatialCalScaleX = -cameraConfig.getScaleX(); + getConfig().spatialCalScaleY = -cameraConfig.getScaleY(); + } catch (Exception ex) { + getConfig().spatialCalOffsetX = Double.NaN; + getConfig().spatialCalOffsetY = Double.NaN; + getConfig().spatialCalScaleX = Double.NaN; + getConfig().spatialCalScaleY = Double.NaN; + } + getConfig().save(); + } + + @Override + protected void doInitialize() throws IOException, InterruptedException{ + super.doInitialize(); + } + +}