This commit is contained in:
sfop
2016-09-06 06:44:37 +02:00
parent 4787cb4173
commit cf02e9d0a7
3 changed files with 108 additions and 41 deletions

View File

@@ -35,6 +35,7 @@ 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;
@@ -47,11 +48,15 @@ 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 java.util.stream.IntStream;
import javax.script.ScriptException;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
import org.apache.commons.math3.analysis.function.Gaussian;
import org.apache.commons.math3.fitting.GaussianCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoint;
/**
*
*/
@@ -62,6 +67,9 @@ public class Cameras extends Panel {
renderer.setPersistenceFile(Paths.get(getController().getSetup().getContextPath(), "Renderer_Cameras.bin"));
setPersistedComponents(new Component[]{checkCamtool});
comboCameras.setEnabled(false);
if (App.hasArgument("ct")) {
checkCamtool.setSelected(! App.getArgumentValue("ct").equals("0"));
}
}
final String configFolder = "/afs/psi.ch/intranet/SF/Applications/config/camtool";
@@ -97,7 +105,7 @@ public class Cameras extends Panel {
ex.printStackTrace();
}
}
startTimer(1000);
startTimer(2000);
}
@Override
@@ -238,7 +246,7 @@ public class Cameras extends Panel {
}
camera.getConfig().save();
camera.setPolling(-1000);
camera.setPolling(-2000);
camera.setMonitored(false);
renderer.setDevice(camera);
renderer.setShowReticle(true);
@@ -286,44 +294,100 @@ public class Cameras extends Panel {
Profile profile = renderer.getProfile();
if ((profile != Profile.None) && (img != null)) {
img = Utils.grayscale(img);
if (profile.hasVertical()) {
int[] sum = Utils.integrateVertically(img);
int[] x = Arr.indexesInt(img.getWidth());
int[] y = new int[img.getWidth()];
if (profile.hasVertical()) {
try {
ScriptManager sm = Controller.getInstance().getScriptManager();
sm.setVar("y", Convert.toDouble(sum));
sm.setVar("x", Convert.toDouble(x));
InterpreterResult r = sm.eval("r = fit(y, x)");
if (r.exception != null){
r.exception .printStackTrace();
return null;
} else {
Object ret = sm.getVar("r");
System.out.println(ret);
int[] sum = Utils.integrateVertically(img);
int[] x = Arr.indexesInt(sum.length);
double[] gaussian = fitGaussian(sum, x);
if (gaussian!=null){
System.out.println("Norm: " + gaussian[0] + " Mean: " + gaussian[1] + " Sigma: " + gaussian[2]);
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 / factor)/2);
}
vpoly = new Overlays.Polyline(fitPen, x, y);
}
} catch (Exception ex) {
ex.printStackTrace();
}
for (int i = 0; i < img.getWidth(); i++) {
y[i] = (int) (img.getHeight() - 1 - (((double) sum[i]) / 255 / factor)/2);
}
vpoly = new Overlays.Polyline(fitPen, Arr.indexesInt(img.getWidth()), y);
}
if (profile.hasHorizontal()) {
int[] integration = Utils.integrateHorizontally(img);
int[] x = new int[img.getHeight()];
int[] y = Arr.indexesInt(img.getHeight());
for (int i = 0; i < img.getHeight(); i++) {
x[i] = (int) (((double) integration[i]) / 255 / factor)/2;
}
hpoly = new Overlays.Polyline(fitPen, x, y);
try {
int[] sum = Utils.integrateHorizontally(img);
int[] x = Arr.indexesInt(sum.length);
double[] gaussian = fitGaussian(sum, x);
if (gaussian!=null){
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 / factor)/2;
}
hpoly = new Overlays.Polyline(fitPen, y, x);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return new Overlay[]{hpoly, vpoly};
}
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(y, x)");
System.out.println("-> " + pY.maxIndex);
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");
System.out.println(ret.size());
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);
System.out.println("! " + norm + " " + mean + " " + sigma) ;
return new double[]{norm, mean, sigma};
}
}
return null;
}
double[]fitGaussian(int[] y, int[] x){
try{
ScriptManager sm = Controller.getInstance().getScriptManager();
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;
}
/////////
public class Camtool extends ArraySource {