This commit is contained in:
gobbo_a
2016-11-02 10:16:36 +01:00
parent 3784b3737b
commit 67ebb276df
2 changed files with 115 additions and 109 deletions

View File

@@ -27,11 +27,13 @@ import ch.psi.pshell.imaging.ColormapSource;
import ch.psi.pshell.imaging.ColormapSource.ColormapSourceConfig;
import ch.psi.pshell.ui.App;
import ch.psi.pshell.imaging.Data;
import ch.psi.pshell.imaging.DimensionDouble;
import ch.psi.pshell.imaging.Histogram;
import ch.psi.pshell.imaging.Overlay;
import ch.psi.pshell.imaging.Overlays;
import ch.psi.pshell.imaging.Overlays.Text;
import ch.psi.pshell.imaging.Pen;
import ch.psi.pshell.imaging.PointDouble;
import ch.psi.pshell.imaging.Renderer;
import ch.psi.pshell.imaging.RendererListener;
import ch.psi.pshell.imaging.RendererMode;
@@ -118,8 +120,9 @@ public class ScreenPanel extends Panel {
}
}
renderer.setProfileNormalized(true);
renderer.setShowProfileLimits(true);
renderer.setProfileNormalized(true);
renderer.setShowProfileLimits(false);
showFit = buttonFit.isSelected();
if (imageBufferLenght>1){
@@ -260,7 +263,6 @@ public class ScreenPanel extends Panel {
ArrayList<Integer> calibration = getCalibration();
double width = Math.abs(calibration.get(2) - calibration.get(0));
return getCalibrationWidth() / width;
//return getCalibrationWidth()/1000;
}
public double getScaleY() {
@@ -345,8 +347,7 @@ public class ScreenPanel extends Panel {
try {
Path configFile = Paths.get(configFolder, cameraName + ".json");
cameraConfigJson = new String(Files.readAllBytes(configFile));
this.cameraName = cameraName;
//SwingUtils.showMessage(null, "", json);
this.cameraName = cameraName;
try {
if (buttonCamtool.isSelected()) {
camera = new Camtool("CurrentCamera", cameraName, false);
@@ -371,7 +372,7 @@ public class ScreenPanel extends Panel {
try {
camera.getConfig().spatialCalOffsetX = config.getCalOffsetX();
camera.getConfig().spatialCalOffsetY = config.getCalOffsetY();
camera.getConfig().spatialCalScaleX = config.getScaleX();
camera.getConfig().spatialCalScaleX = - config.getScaleX();
camera.getConfig().spatialCalScaleY = - config.getScaleY();
} catch (Exception ex) {
camera.getConfig().spatialCalOffsetX = Double.NaN;
@@ -389,7 +390,7 @@ public class ScreenPanel extends Panel {
try {
camera.getConfig().spatialCalOffsetX = ((Camtool) camera).calOffX.read();
camera.getConfig().spatialCalOffsetY = ((Camtool) camera).calOffY.read();
camera.getConfig().spatialCalScaleX = ((Camtool) camera).calScaleX.read();
camera.getConfig().spatialCalScaleX = - ((Camtool) camera).calScaleX.read();
camera.getConfig().spatialCalScaleY = - ((Camtool) camera).calScaleY.read();
} catch (Exception ex) {
System.err.println(ex.getMessage());
@@ -416,23 +417,26 @@ public class ScreenPanel extends Panel {
camera.addListener(new ImageListener() {
@Override
public void onImage(Object o, BufferedImage bi, Data data) {
if (firstImage && (bi!=null)){
if ((renderer.getMode()==RendererMode.Zoom) || (renderer.getMode()==RendererMode.Fixed)){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
centralizeRenderer();
}
});
if (bi!=null){
if (firstImage){
if ((renderer.getMode()==RendererMode.Zoom) || (renderer.getMode()==RendererMode.Fixed)){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
centralizeRenderer();
}
});
}
firstImage = false;
}
firstImage = false;
renderer.setProfileSize(Math.min(bi.getWidth(), bi.getHeight()));
}
if ((bi == null) || (!showFit)) {
renderer.removeOverlays(fitOv);
fitOv = null;
} else {
if (!renderer.isPaused()) {
Overlay[] fo = getFitOverlays(data);
Overlay[] fo = getFitOverlays(data);
renderer.updateOverlays(fo, fitOv);
fitOv = fo;
}
@@ -621,94 +625,101 @@ public class ScreenPanel extends Panel {
Overlay[] getFitOverlays(Data data) {
Overlays.Polyline hpoly = null;
Overlays.Polyline vpoly = null;
Double xMean = null;
Double xSigma = null;
Double yMean = null;
Double ySigma = null;
Double xMean = null, xSigma = null, xNorm = null;
Double yMean = null, ySigma = null, yNorm = null;
if (data != null) {
//img = Utils.grayscale(img);
int profileSize = Math.min(data.getWidth(), data.getHeight())/4;
//int profileSize = 50;
try {
double[] sum = (double[]) Convert.toDouble(data.integrateVertically(true));
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) {
if ((gaussian[2] < sum.length * 0.45) && (gaussian[0] > min * 0.03)) {
double maxPlot = gaussian[0];
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) (data.getHeight() - 1 - ((fit[i] / maxPlot) * profileSize));
// y[i] = (int) (- 1 - ((fit[i] / maxPlot) * profileSize));
}
vpoly = new Overlays.Polyline(fitPen, x, y);
int profileSize = renderer.getProfileSize();
ArrayProperties properties = data.getProperties();
double maxPlot = properties.max;
double minPlot = properties.min;
double rangePlot = maxPlot - minPlot;
if (rangePlot>0){
try {
double[] sum = (double[]) Convert.toDouble(data.integrateVertically(true));
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;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
double[] sum = (double[]) Convert.toDouble(data.integrateHorizontally(true));
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)) {
double maxPlot = gaussian[0];
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) ((fit[i] / maxPlot) * profileSize);
double[] gaussian = fitGaussian(sum, x);
if (gaussian != null) {
if ((gaussian[2] < sum.length * 0.45)
&& (gaussian[2] > 2)
&& (gaussian[0] > min * 0.03)
) {
//gaussian[0] += min;
xNorm = gaussian[0];
xMean = gaussian[1];
xSigma = gaussian[2];
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));
}
vpoly = new Overlays.Polyline(fitPen, x, y);
}
hpoly = new Overlays.Polyline(fitPen, y, x);
}
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
double[] sum = (double[]) Convert.toDouble(data.integrateHorizontally(true));
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;
}
Overlays.Crosshairs cross = null;
Overlays.Text text = 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()));
text = new Overlays.Text(fitPen,
String.format("x = %1.3f " + "\u03C3 = %1.3f \ny = %1.3f " + "\u03C3 = %1.3f" ,
data.getX((int) Math.round(xMean)), xSigma, data.getY((int) Math.round(yMean)), ySigma),
new Font(Font.MONOSPACED, 0, 14), new Point(20,20));
text.setFixed(true);
text.setAnchor(Overlay.ANCHOR_VIEWPORT_TOP_LEFT);
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[2] > 2)
&& (gaussian[0] > min * 0.03)
) {
//gaussian[0] += min;
yNorm = gaussian[0];
yMean = gaussian[1];
ySigma = gaussian[2];
double[] fit = getFitFunction(gaussian, x);
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);
}
hpoly = new Overlays.Polyline(fitPen, y, x);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
Overlays.Crosshairs cross = null;
Overlays.Text text = 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()));
if (renderer.getCalibration()!=null){
xSigma *= renderer.getCalibration().getScaleX();
ySigma *= renderer.getCalibration().getScaleY();
}
text = new Overlays.Text(fitPen,
String.format("x = %1.3f " + "\u03C3 = %1.3f \ny = %1.3f " + "\u03C3 = %1.3f" ,
data.getX((int) Math.round(xMean)), xSigma, data.getY((int) Math.round(yMean)), ySigma),
new Font(Font.MONOSPACED, 0, 14), new Point(20,20));
text.setFixed(true);
text.setAnchor(Overlay.ANCHOR_VIEWPORT_TOP_LEFT);
}
return new Overlay[]{hpoly, vpoly, cross, text};
}
//hpoly.setPassive(false);
//vpoly.setPassive(false);
//hpoly.setAnchor(Overlay.ANCHOR_VIEWPORT_LEFT);
//vpoly.setAnchor(Overlay.ANCHOR_VIEWPORT_BOTTOM);
return new Overlay[]{hpoly, vpoly, cross, text};
}
return null;
}
@@ -1783,15 +1794,11 @@ public class ScreenPanel extends Panel {
private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
try {
//renderer.setSnapshotDialogVisible(buttonSave.isSelected());
String snapshotFile=getController().getSetup().expandPath("{images}/snapshot.png");
renderer.saveSnapshot(snapshotFile, "png", true);
getController().setExecutionContext("snapshot", null, null, null, null, null, null, null);
getController().getDataManager().setDataset("/data", renderer.getData().getMatrix(), renderer.getData().isUnsigned());
getController().getDataManager().closeOutput();
//byte[] arr = (byte[]) (((DataBufferByte)(Utils.grayscale(renderer.getImage()).getRaster().getDataBuffer())).getData());
//Object image = Convert.toBidimensional(arr, renderer.getImage().getWidth(), renderer.getImage().getHeight());
//getController().getDataManager().setDataset("/image", image);
StringBuilder message = new StringBuilder();
message.append("Camera: ").append(cameraName).append(" (").
append((camera instanceof Camtool) ? "camtool" : "direct").append(")").append("\n");
@@ -1801,6 +1808,7 @@ public class ScreenPanel extends Panel {
message.append(text.getText()).append("\n");
}
elog("ScreenPanel Snapshot", message.toString(), new String[]{snapshotFile});
SwingUtils.showMessage(getTopLevel(), "Success", "Generated data file:\n" + getController().getExecutionContext().getPath(), 5000);
} catch (Exception ex) {
ex.printStackTrace();
@@ -1817,12 +1825,13 @@ public class ScreenPanel extends Panel {
setLaserState(false);
}
try{
((Camtool) camera).captureBackground(5);
((Camtool) camera).captureBackground(5);
} finally{
if (laserOn){
setLaserState(true);
}
}
SwingUtils.showMessage(getTopLevel(), "Success", "Success capturing background", 5000);
}
}
} catch (Exception ex) {
@@ -1849,7 +1858,6 @@ public class ScreenPanel extends Panel {
private void buttonZoomNormalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoomNormalActionPerformed
try {
renderer.setMode(RendererMode.Fixed);
//renderer.setViewPosition(new Point(0, 0));
centralizeRenderer();
} catch (Exception ex) {
showException(ex);
@@ -1860,7 +1868,7 @@ public class ScreenPanel extends Panel {
try {
if ((camera != null) && (camera instanceof ColormapSource)) {
ColormapSource source = (ColormapSource) camera;
Color colorReticule = new Color(64,64,64);
Color colorReticule = new Color(16,16,16);
Color colorMarker = new Color(128,128,128);
if (buttonGrayscale.isSelected()) {
colorReticule = new Color(0, 192, 0);
@@ -1900,14 +1908,12 @@ public class ScreenPanel extends Panel {
private void buttonZoom025ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoom025ActionPerformed
renderer.setZoom(0.25);
renderer.setMode(RendererMode.Zoom);
//renderer.setViewPosition(new Point(0, 0));
centralizeRenderer();
}//GEN-LAST:event_buttonZoom025ActionPerformed
private void buttonZoom05ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoom05ActionPerformed
renderer.setZoom(0.5);
renderer.setMode(RendererMode.Zoom);
//renderer.setViewPosition(new Point(0, 0));
centralizeRenderer();
}//GEN-LAST:event_buttonZoom05ActionPerformed