mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-26 01:51:40 +00:00
Move Java backend to properly named folder
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.vision.camera.CameraCapture;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <R> Pipeline result type
|
||||
*/
|
||||
public abstract class CVPipeline<R extends CVPipelineResult, S extends CVPipelineSettings> {
|
||||
protected Mat outputMat = new Mat();
|
||||
CameraCapture cameraCapture;
|
||||
public S settings;
|
||||
|
||||
protected CVPipeline(S settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
protected CVPipeline(String pipelineName, S settings) {
|
||||
this.settings = settings;
|
||||
settings.nickname = pipelineName;
|
||||
}
|
||||
|
||||
public void initPipeline(CameraCapture camera) {
|
||||
cameraCapture = camera;
|
||||
cameraCapture.setExposure((int) settings.exposure);
|
||||
cameraCapture.setBrightness((int) settings.brightness);
|
||||
cameraCapture.setGain((int) settings.gain);
|
||||
}
|
||||
abstract public R runPipeline(Mat inputMat);
|
||||
|
||||
public boolean is3D() {
|
||||
return (this instanceof CVPipeline3d);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.Main;
|
||||
import com.chameleonvision.vision.camera.CameraCapture;
|
||||
import com.chameleonvision.vision.camera.CaptureStaticProperties;
|
||||
import com.chameleonvision.vision.pipeline.pipes.*;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.chameleonvision.vision.pipeline.CVPipeline2d.*;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class CVPipeline2d extends CVPipeline<CVPipeline2dResult, CVPipeline2dSettings> {
|
||||
|
||||
private Mat rawCameraMat = new Mat();
|
||||
|
||||
private RotateFlipPipe rotateFlipPipe;
|
||||
private BlurPipe blurPipe;
|
||||
private ErodeDilatePipe erodeDilatePipe;
|
||||
private HsvPipe hsvPipe;
|
||||
private FindContoursPipe findContoursPipe;
|
||||
private FilterContoursPipe filterContoursPipe;
|
||||
private SpeckleRejectPipe speckleRejectPipe;
|
||||
private GroupContoursPipe groupContoursPipe;
|
||||
private SortContoursPipe sortContoursPipe;
|
||||
private Collect2dTargetsPipe collect2dTargetsPipe;
|
||||
private Draw2dContoursPipe.Draw2dContoursSettings draw2dContoursSettings;
|
||||
private Draw2dContoursPipe draw2dContoursPipe;
|
||||
private OutputMatPipe outputMatPipe;
|
||||
|
||||
private String pipelineTimeString = "";
|
||||
private CaptureStaticProperties camProps;
|
||||
private Scalar hsvLower, hsvUpper;
|
||||
|
||||
public CVPipeline2d() {
|
||||
super(new CVPipeline2dSettings());
|
||||
}
|
||||
|
||||
public CVPipeline2d(String name) {
|
||||
super(name, new CVPipeline2dSettings());
|
||||
}
|
||||
|
||||
public CVPipeline2d(CVPipeline2dSettings settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initPipeline(CameraCapture process) {
|
||||
super.initPipeline(process);
|
||||
|
||||
camProps = cameraCapture.getProperties().getStaticProperties();
|
||||
hsvLower = new Scalar(settings.hue.get(0).intValue(), settings.saturation.get(0).intValue(), settings.value.get(0).intValue());
|
||||
hsvUpper = new Scalar(settings.hue.get(1).intValue(), settings.saturation.get(1).intValue(), settings.value.get(1).intValue());
|
||||
|
||||
rotateFlipPipe = new RotateFlipPipe(settings.rotationMode, settings.flipMode);
|
||||
blurPipe = new BlurPipe(5);
|
||||
erodeDilatePipe = new ErodeDilatePipe(settings.erode, settings.dilate, 7);
|
||||
hsvPipe = new HsvPipe(hsvLower, hsvUpper);
|
||||
findContoursPipe = new FindContoursPipe();
|
||||
filterContoursPipe = new FilterContoursPipe(settings.area, settings.ratio, settings.extent, camProps);
|
||||
speckleRejectPipe = new SpeckleRejectPipe(settings.speckle.doubleValue());
|
||||
groupContoursPipe = new GroupContoursPipe(settings.targetGroup, settings.targetIntersection);
|
||||
sortContoursPipe = new SortContoursPipe(settings.sortMode, camProps, 5);
|
||||
collect2dTargetsPipe = new Collect2dTargetsPipe(settings.calibrationMode, settings.point,
|
||||
settings.dualTargetCalibrationM, settings.dualTargetCalibrationB, camProps);
|
||||
draw2dContoursSettings = new Draw2dContoursPipe.Draw2dContoursSettings();
|
||||
// TODO: make settable from UI? config?
|
||||
draw2dContoursSettings.showCentroid = false;
|
||||
draw2dContoursSettings.showCrosshair = true;
|
||||
draw2dContoursSettings.boxOutlineSize = 2;
|
||||
draw2dContoursSettings.showRotatedBox = true;
|
||||
draw2dContoursSettings.showMaximumBox = true;
|
||||
draw2dContoursSettings.showMultiple = settings.multiple;
|
||||
|
||||
draw2dContoursPipe = new Draw2dContoursPipe(draw2dContoursSettings, camProps);
|
||||
outputMatPipe = new OutputMatPipe(settings.isBinary);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CVPipeline2dResult runPipeline(Mat inputMat) {
|
||||
long totalPipelineTimeNanos = 0;
|
||||
long pipelineStartTimeNanos = System.nanoTime();
|
||||
|
||||
if (cameraCapture == null) {
|
||||
throw new RuntimeException("Pipeline was not initialized before being run!");
|
||||
}
|
||||
|
||||
// TODO (HIGH) find the source of the random NPE
|
||||
if (settings == null) {
|
||||
throw new RuntimeException("settings was not initialized!");
|
||||
}
|
||||
if (inputMat.cols() <= 1) {
|
||||
throw new RuntimeException("Input Mat is empty!");
|
||||
}
|
||||
|
||||
pipelineTimeString = "";
|
||||
|
||||
inputMat.copyTo(rawCameraMat);
|
||||
|
||||
// prepare pipes
|
||||
camProps = cameraCapture.getProperties().getStaticProperties();
|
||||
hsvLower = new Scalar(settings.hue.get(0).intValue(), settings.saturation.get(0).intValue(), settings.value.get(0).intValue());
|
||||
hsvUpper = new Scalar(settings.hue.get(1).intValue(), settings.saturation.get(1).intValue(), settings.value.get(1).intValue());
|
||||
rotateFlipPipe.setConfig(settings.rotationMode, settings.flipMode);
|
||||
blurPipe.setConfig(0);
|
||||
erodeDilatePipe.setConfig(settings.erode, settings.dilate, 7);
|
||||
hsvPipe.setConfig(hsvLower, hsvUpper);
|
||||
filterContoursPipe.setConfig(settings.area, settings.ratio, settings.extent, camProps);
|
||||
speckleRejectPipe.setConfig(settings.speckle.doubleValue());
|
||||
groupContoursPipe.setConfig(settings.targetGroup, settings.targetIntersection);
|
||||
sortContoursPipe.setConfig(settings.sortMode, camProps, 5);
|
||||
collect2dTargetsPipe.setConfig(settings.calibrationMode, settings.point,
|
||||
settings.dualTargetCalibrationM, settings.dualTargetCalibrationB, camProps);
|
||||
draw2dContoursPipe.setConfig(settings.multiple, camProps);
|
||||
outputMatPipe.setConfig(settings.isBinary);
|
||||
|
||||
long pipeInitTimeNanos = System.nanoTime() - pipelineStartTimeNanos;
|
||||
|
||||
// run pipes
|
||||
Pair<Mat, Long> rotateFlipResult = rotateFlipPipe.run(inputMat);
|
||||
totalPipelineTimeNanos += rotateFlipResult.getRight();
|
||||
|
||||
Pair<Mat, Long> blurResult = blurPipe.run(rotateFlipResult.getLeft());
|
||||
totalPipelineTimeNanos += blurResult.getRight();
|
||||
|
||||
Pair<Mat, Long> erodeDilateResult = erodeDilatePipe.run(blurResult.getLeft());
|
||||
totalPipelineTimeNanos += erodeDilateResult.getRight();
|
||||
|
||||
Pair<Mat, Long> hsvResult = hsvPipe.run(erodeDilateResult.getLeft());
|
||||
totalPipelineTimeNanos += hsvResult.getRight();
|
||||
|
||||
Pair<List<MatOfPoint>, Long> findContoursResult = findContoursPipe.run(hsvResult.getLeft());
|
||||
totalPipelineTimeNanos += findContoursResult.getRight();
|
||||
|
||||
Pair<List<MatOfPoint>, Long> filterContoursResult = filterContoursPipe.run(findContoursResult.getLeft());
|
||||
totalPipelineTimeNanos += filterContoursResult.getRight();
|
||||
|
||||
Pair<List<MatOfPoint>, Long> speckleRejectResult = speckleRejectPipe.run(filterContoursResult.getLeft());
|
||||
totalPipelineTimeNanos += speckleRejectResult.getRight();
|
||||
|
||||
Pair<List<RotatedRect>, Long> groupContoursResult = groupContoursPipe.run(speckleRejectResult.getLeft());
|
||||
totalPipelineTimeNanos += groupContoursResult.getRight();
|
||||
|
||||
Pair<List<RotatedRect>, Long> sortContoursResult = sortContoursPipe.run(groupContoursResult.getLeft());
|
||||
totalPipelineTimeNanos += sortContoursResult.getRight();
|
||||
|
||||
Pair<List<Target2d>, Long> collect2dTargetsResult = collect2dTargetsPipe.run(Pair.of(sortContoursResult.getLeft(), camProps));
|
||||
totalPipelineTimeNanos += collect2dTargetsResult.getRight();
|
||||
|
||||
// takes pair of (Mat of original camera image (8UC3), Mat of HSV thresholded image(8UC1))
|
||||
Pair<Mat, Long> outputMatResult = outputMatPipe.run(Pair.of(rotateFlipResult.getLeft(), hsvResult.getLeft()));
|
||||
totalPipelineTimeNanos += outputMatResult.getRight();
|
||||
|
||||
// takes pair of (Mat to draw on, List<RotatedRect> of sorted contours)
|
||||
Pair<Mat, Long> draw2dContoursResult = draw2dContoursPipe.run(Pair.of(outputMatResult.getLeft(), sortContoursResult.getLeft()));
|
||||
totalPipelineTimeNanos += draw2dContoursResult.getRight();
|
||||
|
||||
if (Main.testMode) {
|
||||
pipelineTimeString += String.format("PipeInit: %.2fms, ", pipeInitTimeNanos / 1000000.0);
|
||||
pipelineTimeString += String.format("RotateFlip: %.2fms, ", rotateFlipResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("Blur: %.2fms, ", blurResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("ErodeDilate: %.2fms, ", erodeDilateResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("HSV: %.2fms, ", hsvResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("FindContours: %.2fms, ", findContoursResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("FilterContours: %.2fms, ", filterContoursResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("SpeckleReject: %.2fms, ", speckleRejectResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("GroupContours: %.2fms, ", groupContoursResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("SortContours: %.2fms, ", sortContoursResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("Collect2dTargets: %.2fms, ", collect2dTargetsResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("OutputMat: %.2fms, ", outputMatResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("Draw2dContours: %.2fms, ", draw2dContoursResult.getRight() / 1000000.0);
|
||||
|
||||
System.out.println(pipelineTimeString);
|
||||
double totalPipelineTimeMillis = totalPipelineTimeNanos / 1000000.0;
|
||||
double totalPipelineTimeFPS = 1.0 / (totalPipelineTimeMillis / 1000.0);
|
||||
double truePipelineTimeMillis = (System.nanoTime() - pipelineStartTimeNanos) / 1000000.0;
|
||||
double truePipelineFPS = 1.0 / (truePipelineTimeMillis / 1000.0);
|
||||
System.out.printf("Pipeline processed in %.3fms (%.2fFPS), ", totalPipelineTimeMillis, totalPipelineTimeFPS);
|
||||
System.out.printf("full pipeline run time was %.3fms (%.2fFPS)\n", truePipelineTimeMillis, truePipelineFPS);
|
||||
}
|
||||
|
||||
return new CVPipeline2dResult(collect2dTargetsResult.getLeft(), draw2dContoursResult.getLeft(), totalPipelineTimeNanos);
|
||||
}
|
||||
|
||||
public static class CVPipeline2dResult extends CVPipelineResult<Target2d> {
|
||||
public CVPipeline2dResult(List<Target2d> targets, Mat outputMat, long processTimeNanos) {
|
||||
super(targets, outputMat, processTimeNanos);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Target2d {
|
||||
public double calibratedX = 0.0;
|
||||
public double calibratedY = 0.0;
|
||||
public double pitch = 0.0;
|
||||
public double yaw = 0.0;
|
||||
public double area = 0.0;
|
||||
public RotatedRect rawPoint;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.vision.enums.CalibrationMode;
|
||||
import com.chameleonvision.vision.enums.SortMode;
|
||||
import com.chameleonvision.vision.enums.TargetGroup;
|
||||
import com.chameleonvision.vision.enums.TargetIntersection;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CVPipeline2dSettings extends CVPipelineSettings {
|
||||
public List<Number> hue = Arrays.asList(50, 180);
|
||||
public List<Number> saturation = Arrays.asList(50, 255);
|
||||
public List<Number> value = Arrays.asList(50, 255);
|
||||
public boolean erode = false;
|
||||
public boolean dilate = false;
|
||||
public List<Number> area = Arrays.asList(0.0, 100.0);
|
||||
public List<Number> ratio = Arrays.asList(0.0, 20.0);
|
||||
public List<Number> extent = Arrays.asList(0, 100);
|
||||
public Number speckle = 5;
|
||||
public boolean isBinary = false;
|
||||
public SortMode sortMode = SortMode.Largest;
|
||||
public boolean multiple = false;
|
||||
public TargetGroup targetGroup = TargetGroup.Single;
|
||||
public TargetIntersection targetIntersection = TargetIntersection.Up;
|
||||
public List<Number> point = Arrays.asList(0, 0);
|
||||
public CalibrationMode calibrationMode = CalibrationMode.None;
|
||||
public double dualTargetCalibrationM = 1;
|
||||
public double dualTargetCalibrationB = 0;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.chameleonvision.vision.pipeline.CVPipeline3d.*;
|
||||
|
||||
public class CVPipeline3d extends CVPipeline<CVPipeline3dResult, CVPipeline3dSettings> {
|
||||
|
||||
|
||||
protected CVPipeline3d(CVPipeline3dSettings settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
||||
CVPipeline3d() {
|
||||
super(new CVPipeline3dSettings());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CVPipeline3dResult runPipeline(Mat inputMat) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static class CVPipeline3dResult extends CVPipelineResult<Target3d> {
|
||||
public CVPipeline3dResult(List<Target3d> targets, Mat outputMat, long processTime) {
|
||||
super(targets, outputMat, processTime);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Target3d extends CVPipeline2d.Target2d {
|
||||
// TODO: (2.1) Define 3d-specific target data
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
public class CVPipeline3dSettings extends CVPipeline2dSettings {
|
||||
// TODO: (2.1) define 3d-specific pipeline settings
|
||||
// add 3d-specific property to ensure serializing/deserializing works
|
||||
public boolean placeholder = false;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class CVPipelineResult<T> {
|
||||
public final List<T> targets;
|
||||
public final boolean hasTarget;
|
||||
public final Mat outputMat = new Mat();
|
||||
public final long processTime;
|
||||
public long imageTimestamp = 0;
|
||||
|
||||
public CVPipelineResult(List<T> targets, Mat outputMat, long processTime) {
|
||||
this.targets = targets;
|
||||
hasTarget = targets != null && !targets.isEmpty();
|
||||
outputMat.copyTo(this.outputMat);
|
||||
this.processTime = processTime;
|
||||
}
|
||||
|
||||
public void setTimestamp(long timestamp) {
|
||||
imageTimestamp = timestamp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.vision.enums.ImageFlipMode;
|
||||
import com.chameleonvision.vision.enums.ImageRotationMode;
|
||||
import com.chameleonvision.vision.enums.StreamDivisor;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
public class CVPipelineSettings {
|
||||
public int index = 0;
|
||||
public ImageFlipMode flipMode = ImageFlipMode.NONE;
|
||||
public ImageRotationMode rotationMode = ImageRotationMode.DEG_0;
|
||||
public String nickname = "New Pipeline";
|
||||
public double exposure = 50.0;
|
||||
public double brightness = 50.0;
|
||||
public double gain = 0;
|
||||
public int videoModeIndex = 0;
|
||||
public StreamDivisor streamDivisor = StreamDivisor.NONE;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.vision.camera.CameraCapture;
|
||||
import com.chameleonvision.vision.pipeline.pipes.Draw2dContoursPipe;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.RotatedRect;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.chameleonvision.vision.pipeline.DriverVisionPipeline.DriverPipelineResult;
|
||||
|
||||
public class DriverVisionPipeline extends CVPipeline<DriverPipelineResult, CVPipelineSettings> {
|
||||
|
||||
private Draw2dContoursPipe draw2dContoursPipe;
|
||||
private Draw2dContoursPipe.Draw2dContoursSettings draw2dContoursSettings = new Draw2dContoursPipe.Draw2dContoursSettings();
|
||||
private final List<RotatedRect> blankList = List.of();
|
||||
|
||||
public DriverVisionPipeline(CVPipelineSettings settings) {
|
||||
super(settings);
|
||||
settings.index = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initPipeline(CameraCapture capture) {
|
||||
super.initPipeline(capture);
|
||||
draw2dContoursSettings.showCrosshair = true;
|
||||
draw2dContoursPipe = new Draw2dContoursPipe(draw2dContoursSettings, cameraCapture.getProperties().getStaticProperties());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DriverPipelineResult runPipeline(Mat inputMat) {
|
||||
|
||||
inputMat.copyTo(outputMat);
|
||||
|
||||
draw2dContoursPipe.setConfig(false, cameraCapture.getProperties().getStaticProperties());
|
||||
draw2dContoursPipe.run(Pair.of(outputMat, blankList)).getLeft().copyTo(outputMat);
|
||||
|
||||
return new DriverPipelineResult(null, outputMat, 0);
|
||||
}
|
||||
|
||||
public static class DriverPipelineResult extends CVPipelineResult<Void> {
|
||||
public DriverPipelineResult(List<Void> targets, Mat outputMat, long processTime) {
|
||||
super(targets, outputMat, processTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.config.CameraConfig;
|
||||
import com.chameleonvision.config.ConfigManager;
|
||||
import com.chameleonvision.vision.VisionManager;
|
||||
import com.chameleonvision.vision.VisionProcess;
|
||||
import com.chameleonvision.web.SocketHandler;
|
||||
import edu.wpi.first.networktables.NetworkTableEntry;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class PipelineManager {
|
||||
|
||||
private static final int DRIVERMODE_INDEX = -1;
|
||||
|
||||
public final LinkedList<CVPipeline> pipelines = new LinkedList<>();
|
||||
|
||||
public final CVPipeline driverModePipeline = new DriverVisionPipeline(new CVPipelineSettings());
|
||||
|
||||
private final VisionProcess parentProcess;
|
||||
private int lastPipelineIndex;
|
||||
private int currentPipelineIndex;
|
||||
public NetworkTableEntry ntIndexEntry;
|
||||
|
||||
public PipelineManager(VisionProcess visionProcess, List<CVPipelineSettings> loadedPipelineSettings) {
|
||||
parentProcess = visionProcess;
|
||||
if (loadedPipelineSettings == null || loadedPipelineSettings.size() == 0) {
|
||||
pipelines.add(new CVPipeline2d("New Pipeline"));
|
||||
} else {
|
||||
for (CVPipelineSettings setting : loadedPipelineSettings) {
|
||||
addInternalPipeline(setting);
|
||||
}
|
||||
}
|
||||
driverModePipeline.initPipeline(visionProcess.getCamera());
|
||||
setCurrentPipeline(0);
|
||||
}
|
||||
|
||||
private void reassignIndexes() {
|
||||
pipelines.sort(IndexComparator);
|
||||
for (int i = 0; i < pipelines.size(); i++) {
|
||||
pipelines.get(i).settings.index = i;
|
||||
}
|
||||
}
|
||||
|
||||
private CameraConfig getConfig(VisionProcess process) {
|
||||
return VisionManager.getCameraConfig(process);
|
||||
}
|
||||
|
||||
private CameraConfig getConfig() {
|
||||
return getConfig(parentProcess);
|
||||
}
|
||||
|
||||
private void savePipelineConfig(CVPipelineSettings setting) {
|
||||
getConfig().pipelineConfig.save(setting);
|
||||
}
|
||||
|
||||
private void deletePipelineConfig(CVPipelineSettings setting) {
|
||||
getConfig().pipelineConfig.delete(setting);
|
||||
}
|
||||
|
||||
private void renamePipelineConfig(CVPipelineSettings setting, String newName) {
|
||||
getConfig().pipelineConfig.rename(setting, newName);
|
||||
}
|
||||
|
||||
public void saveAllPipelines() {
|
||||
pipelines.parallelStream().map(pipeline -> pipeline.settings).forEach(this::savePipelineConfig);
|
||||
}
|
||||
|
||||
private void addInternalPipeline(CVPipelineSettings setting) {
|
||||
if (setting instanceof CVPipeline3dSettings) {
|
||||
pipelines.add(new CVPipeline3d((CVPipeline3dSettings) setting));
|
||||
} else if (setting instanceof CVPipeline2dSettings) {
|
||||
pipelines.add(new CVPipeline2d((CVPipeline2dSettings) setting));
|
||||
} else {
|
||||
System.out.println("Non 2D/3D pipelines not supported!");
|
||||
}
|
||||
reassignIndexes();
|
||||
}
|
||||
|
||||
public void setDriverMode(boolean driverMode) {
|
||||
if (driverMode) {
|
||||
setCurrentPipeline(DRIVERMODE_INDEX);
|
||||
} else {
|
||||
setCurrentPipeline(lastPipelineIndex);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getDriverMode() {
|
||||
return currentPipelineIndex == DRIVERMODE_INDEX;
|
||||
}
|
||||
|
||||
public int getCurrentPipelineIndex() {
|
||||
return currentPipelineIndex;
|
||||
}
|
||||
|
||||
public CVPipeline getCurrentPipeline() {
|
||||
if (currentPipelineIndex <= DRIVERMODE_INDEX) {
|
||||
return driverModePipeline;
|
||||
} else {
|
||||
return pipelines.get(currentPipelineIndex);
|
||||
}
|
||||
}
|
||||
|
||||
public void setCurrentPipeline(int index) {
|
||||
CVPipeline newPipeline;
|
||||
if (index == DRIVERMODE_INDEX) {
|
||||
newPipeline = driverModePipeline;
|
||||
|
||||
// if we're changing into driver mode, try to set the nt entry to frue
|
||||
parentProcess.setDriverModeEntry(true);
|
||||
} else {
|
||||
newPipeline = pipelines.get(index);
|
||||
|
||||
// if we're switching out of driver mode, try to set the nt entry to false
|
||||
parentProcess.setDriverModeEntry(false);
|
||||
}
|
||||
if (newPipeline != null) {
|
||||
lastPipelineIndex = currentPipelineIndex;
|
||||
currentPipelineIndex = index;
|
||||
getCurrentPipeline().initPipeline(parentProcess.getCamera());
|
||||
|
||||
if(ConfigManager.settings.currentCamera.equals(parentProcess.getCamera().getProperties().name)) {
|
||||
ConfigManager.settings.currentPipeline = currentPipelineIndex;
|
||||
|
||||
HashMap<String, Object> pipeChange = new HashMap<>();
|
||||
pipeChange.put("currentPipeline", currentPipelineIndex);
|
||||
SocketHandler.broadcastMessage(pipeChange);
|
||||
try {
|
||||
SocketHandler.sendFullSettings();
|
||||
} catch (Exception e) {
|
||||
// avoid NullPointerException when run before threads start
|
||||
}
|
||||
}
|
||||
newPipeline.initPipeline(parentProcess.getCamera());
|
||||
if(ntIndexEntry != null) {
|
||||
ntIndexEntry.setDouble(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addPipeline(CVPipelineSettings setting) {
|
||||
addInternalPipeline(setting);
|
||||
savePipelineConfig(setting);
|
||||
}
|
||||
|
||||
public void addPipeline(CVPipeline pipeline) {
|
||||
pipelines.add(pipeline);
|
||||
reassignIndexes();
|
||||
savePipelineConfig(pipeline.settings);
|
||||
}
|
||||
|
||||
public void addNewPipeline(boolean is3D) {
|
||||
CVPipeline newPipeline;
|
||||
if (!is3D) {
|
||||
newPipeline = new CVPipeline2d();
|
||||
} else {
|
||||
newPipeline = new CVPipeline3d();
|
||||
}
|
||||
newPipeline.settings.index = pipelines.size();
|
||||
addPipeline(newPipeline);
|
||||
}
|
||||
|
||||
public CVPipeline getPipeline(int index) {
|
||||
return pipelines.get(index);
|
||||
}
|
||||
|
||||
public void duplicatePipeline(CVPipelineSettings pipeline) {
|
||||
duplicatePipeline(pipeline, parentProcess);
|
||||
}
|
||||
|
||||
public void duplicatePipeline(CVPipelineSettings pipeline, VisionProcess destinationProcess) {
|
||||
pipeline.index = destinationProcess.pipelineManager.pipelines.size();
|
||||
pipeline.nickname += "(Copy)";
|
||||
destinationProcess.pipelineManager.addPipeline(pipeline);
|
||||
}
|
||||
|
||||
public void renameCurrentPipeline(String newName) {
|
||||
CVPipelineSettings settings = getCurrentPipeline().settings;
|
||||
settings.nickname = newName;
|
||||
renamePipelineConfig(settings, newName);
|
||||
}
|
||||
|
||||
public void deleteCurrentPipeline() {
|
||||
deletePipeline(currentPipelineIndex);
|
||||
}
|
||||
|
||||
private void deletePipeline(int index) {
|
||||
if (index == currentPipelineIndex) {
|
||||
currentPipelineIndex -= 1;
|
||||
}
|
||||
deletePipelineConfig(getPipeline(index).settings);
|
||||
pipelines.remove(index);
|
||||
reassignIndexes();
|
||||
}
|
||||
|
||||
public void saveDriverModeConfig() {
|
||||
getConfig().saveDriverMode(driverModePipeline.settings);
|
||||
}
|
||||
|
||||
private static final Comparator<CVPipeline> IndexComparator = (o1, o2) -> {
|
||||
int o1Index = o1.settings.index;
|
||||
int o2Index = o2.settings.index;
|
||||
|
||||
if (o1Index == o2Index) {
|
||||
return 0;
|
||||
} else if (o1Index < o2Index) {
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.CvException;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class BlurPipe implements Pipe<Mat, Mat> {
|
||||
|
||||
private int blurSize;
|
||||
|
||||
private Mat processBuffer = new Mat();
|
||||
private Mat outputMat = new Mat();
|
||||
|
||||
public BlurPipe(int blurSize) {
|
||||
this.blurSize = blurSize;
|
||||
}
|
||||
|
||||
public void setConfig(int blurSize) {
|
||||
this.blurSize = blurSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Mat, Long> run(Mat input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
if (blurSize > 0) {
|
||||
input.copyTo(processBuffer);
|
||||
try {
|
||||
Imgproc.blur(processBuffer, processBuffer, new Size(blurSize, blurSize));
|
||||
processBuffer.copyTo(outputMat);
|
||||
processBuffer.release();
|
||||
} catch (CvException e) {
|
||||
System.err.println("(BlurPipe) Exception thrown by OpenCV: \n" + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
input.copyTo(outputMat);
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(outputMat, processTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.vision.camera.CaptureStaticProperties;
|
||||
import com.chameleonvision.vision.pipeline.CVPipeline2d;
|
||||
import com.chameleonvision.vision.enums.CalibrationMode;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.RotatedRect;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Collect2dTargetsPipe implements Pipe<Pair<List<RotatedRect>, CaptureStaticProperties>, List<CVPipeline2d.Target2d>> {
|
||||
|
||||
private CalibrationMode calibrationMode;
|
||||
private CaptureStaticProperties camProps;
|
||||
private List<Number> calibrationPoint;
|
||||
private double calibrationM, calibrationB;
|
||||
|
||||
private List<CVPipeline2d.Target2d> targets = new ArrayList<>();
|
||||
|
||||
public Collect2dTargetsPipe(CalibrationMode calibrationMode, List<Number> calibrationPoint,
|
||||
double calibrationM, double calibrationB, CaptureStaticProperties camProps) {
|
||||
this.calibrationMode = calibrationMode;
|
||||
this.camProps = camProps;
|
||||
this.calibrationPoint = calibrationPoint;
|
||||
this.calibrationM = calibrationM;
|
||||
this.calibrationB = calibrationB;
|
||||
}
|
||||
|
||||
public void setConfig(CalibrationMode calibrationMode, List<Number> calibrationPoint,
|
||||
double calibrationM, double calibrationB, CaptureStaticProperties camProps) {
|
||||
this.calibrationMode = calibrationMode;
|
||||
this.camProps = camProps;
|
||||
this.calibrationPoint = calibrationPoint;
|
||||
this.calibrationM = calibrationM;
|
||||
this.calibrationB = calibrationB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<List<CVPipeline2d.Target2d>, Long> run(Pair<List<RotatedRect>, CaptureStaticProperties> inputPair) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
targets.clear();
|
||||
var input = inputPair.getLeft();
|
||||
var imageArea = inputPair.getRight().imageArea;
|
||||
|
||||
if (input.size() > 0) {
|
||||
for (RotatedRect r : input) {
|
||||
CVPipeline2d.Target2d t = new CVPipeline2d.Target2d();
|
||||
t.rawPoint = r;
|
||||
switch (calibrationMode) {
|
||||
case None:
|
||||
t.calibratedX = camProps.centerX;
|
||||
t.calibratedY = camProps.centerY;
|
||||
break;
|
||||
case Single:
|
||||
t.calibratedX = calibrationPoint.get(0).doubleValue();
|
||||
t.calibratedY = calibrationPoint.get(1).doubleValue();
|
||||
break;
|
||||
case Dual:
|
||||
t.calibratedX = (r.center.y - calibrationB) / calibrationM;
|
||||
t.calibratedY = (r.center.x * calibrationM) + calibrationB;
|
||||
break;
|
||||
}
|
||||
|
||||
t.pitch = calculatePitch(r.center.y, t.calibratedY);
|
||||
t.yaw = calculateYaw(r.center.x, t.calibratedX);
|
||||
t.area = r.size.area() / imageArea;
|
||||
|
||||
targets.add(t);
|
||||
}
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(targets, processTime);
|
||||
}
|
||||
|
||||
private double calculatePitch(double pixelY, double centerY) {
|
||||
double pitch = FastMath.toDegrees(FastMath.atan((pixelY - centerY) / camProps.verticalFocalLength));
|
||||
return (pitch * -1);
|
||||
}
|
||||
|
||||
private double calculateYaw(double pixelX, double centerX) {
|
||||
return FastMath.toDegrees(FastMath.atan((pixelX - centerX) / camProps.horizontalFocalLength));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.vision.camera.CaptureStaticProperties;
|
||||
import com.chameleonvision.util.Helpers;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Draw2dContoursPipe implements Pipe<Pair<Mat, List<RotatedRect>>, Mat> {
|
||||
|
||||
private final Draw2dContoursSettings settings;
|
||||
private CaptureStaticProperties camProps;
|
||||
|
||||
private Mat processBuffer = new Mat();
|
||||
private Mat outputMat = new Mat();
|
||||
|
||||
private Point[] vertices = new Point[4];
|
||||
private List<MatOfPoint> drawnContours = new ArrayList<>();
|
||||
@SuppressWarnings("FieldCanBeLocal")
|
||||
private Point xMax = new Point(), xMin = new Point(), yMax = new Point(), yMin = new Point();
|
||||
|
||||
|
||||
public Draw2dContoursPipe(Draw2dContoursSettings settings, CaptureStaticProperties camProps) {
|
||||
this.settings = settings;
|
||||
this.camProps = camProps;
|
||||
}
|
||||
|
||||
public void setConfig(boolean showMultiple,CaptureStaticProperties captureProps) {
|
||||
settings.showMultiple = showMultiple;
|
||||
camProps = captureProps;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Mat, Long> run(Pair<Mat, List<RotatedRect>> input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
if (settings.showCrosshair || settings.showCentroid || settings.showMaximumBox || settings.showRotatedBox) {
|
||||
input.getLeft().copyTo(processBuffer);
|
||||
|
||||
if (input.getRight().size() > 0) {
|
||||
for (int i = 0; i < input.getRight().size(); i++) {
|
||||
if (i != 0 && !settings.showMultiple){
|
||||
break;
|
||||
}
|
||||
RotatedRect r = input.getRight().get(i);
|
||||
if (r == null) continue;
|
||||
|
||||
drawnContours.forEach(Mat::release);
|
||||
drawnContours.clear();
|
||||
|
||||
r.points(vertices);
|
||||
MatOfPoint contour = new MatOfPoint(vertices);
|
||||
drawnContours.add(contour);
|
||||
|
||||
if (settings.showCentroid) {
|
||||
Imgproc.circle(processBuffer, r.center, 3, Helpers.colorToScalar(settings.centroidColor));
|
||||
}
|
||||
|
||||
if (settings.showRotatedBox) {
|
||||
Imgproc.drawContours(processBuffer, drawnContours, 0, Helpers.colorToScalar(settings.rotatedBoxColor), settings.boxOutlineSize);
|
||||
}
|
||||
|
||||
if (settings.showMaximumBox) {
|
||||
Rect box = Imgproc.boundingRect(contour);
|
||||
Imgproc.rectangle(processBuffer, new Point(box.x, box.y), new Point((box.x + box.width), (box.y + box.height)), Helpers.colorToScalar(settings.maximumBoxColor), settings.boxOutlineSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.showCrosshair) {
|
||||
xMax.set(new double[] {camProps.centerX + 10, camProps.centerY});
|
||||
xMin.set(new double[] {camProps.centerX - 10, camProps.centerY});
|
||||
yMax.set(new double[] {camProps.centerX, camProps.centerY + 10});
|
||||
yMin.set(new double[] {camProps.centerX, camProps.centerY - 10});
|
||||
Imgproc.line(processBuffer, xMax, xMin, Helpers.colorToScalar(settings.crosshairColor), 2);
|
||||
Imgproc.line(processBuffer, yMax, yMin, Helpers.colorToScalar(settings.crosshairColor), 2);
|
||||
}
|
||||
|
||||
processBuffer.copyTo(outputMat);
|
||||
processBuffer.release();
|
||||
} else {
|
||||
input.getLeft().copyTo(outputMat);
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(outputMat, processTime);
|
||||
}
|
||||
|
||||
public static class Draw2dContoursSettings {
|
||||
public boolean showCentroid = false;
|
||||
public boolean showCrosshair = false;
|
||||
public boolean showMultiple = false;
|
||||
public int boxOutlineSize = 0;
|
||||
public boolean showRotatedBox = false;
|
||||
public boolean showMaximumBox = false;
|
||||
public Color centroidColor = Color.GREEN;
|
||||
public Color crosshairColor = Color.GREEN;
|
||||
public Color rotatedBoxColor = Color.BLUE;
|
||||
public Color maximumBoxColor = Color.RED;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class ErodeDilatePipe implements Pipe<Mat, Mat> {
|
||||
|
||||
private boolean erode;
|
||||
private boolean dilate;
|
||||
private Mat kernel;
|
||||
|
||||
private Mat processBuffer = new Mat();
|
||||
private Mat outputMat = new Mat();
|
||||
|
||||
public ErodeDilatePipe(boolean erode, boolean dilate, int kernelSize) {
|
||||
this.erode = erode;
|
||||
this.dilate = dilate;
|
||||
kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(kernelSize, kernelSize));
|
||||
}
|
||||
|
||||
public void setConfig(boolean erode, boolean dilate, int kernelSize) {
|
||||
this.erode = erode;
|
||||
this.dilate = dilate;
|
||||
kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(kernelSize, kernelSize));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Mat, Long> run(Mat input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
if (erode || dilate) {
|
||||
input.copyTo(processBuffer);
|
||||
|
||||
if (erode) {
|
||||
Imgproc.erode(processBuffer, processBuffer, kernel);
|
||||
}
|
||||
|
||||
if (dilate) {
|
||||
Imgproc.dilate(processBuffer, processBuffer, kernel);
|
||||
}
|
||||
|
||||
processBuffer.copyTo(outputMat);
|
||||
processBuffer.release();
|
||||
} else {
|
||||
input.copyTo(outputMat);
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(outputMat, processTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.vision.camera.CaptureStaticProperties;
|
||||
import com.chameleonvision.util.MathHandler;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.MatOfPoint;
|
||||
import org.opencv.core.MatOfPoint2f;
|
||||
import org.opencv.core.Rect;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FilterContoursPipe implements Pipe<List<MatOfPoint>, List<MatOfPoint>> {
|
||||
|
||||
private List<Number> area;
|
||||
private List<Number> ratio;
|
||||
private List<Number> extent;
|
||||
private CaptureStaticProperties camProps;
|
||||
|
||||
private List<MatOfPoint> filteredContours = new ArrayList<>();
|
||||
|
||||
public FilterContoursPipe(List<Number> area, List<Number> ratio, List<Number> extent, CaptureStaticProperties camProps) {
|
||||
this.area = area;
|
||||
this.ratio = ratio;
|
||||
this.extent = extent;
|
||||
this.camProps = camProps;
|
||||
}
|
||||
|
||||
public void setConfig(List<Number> area, List<Number> ratio, List<Number> extent, CaptureStaticProperties camProps) {
|
||||
this.area = area;
|
||||
this.ratio = ratio;
|
||||
this.extent = extent;
|
||||
this.camProps = camProps;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<List<MatOfPoint>, Long> run(List<MatOfPoint> input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
filteredContours.clear();
|
||||
|
||||
if (input.size() > 0) {
|
||||
for (MatOfPoint Contour : input) {
|
||||
try {
|
||||
double contourArea = Imgproc.contourArea(Contour);
|
||||
double AreaRatio = (contourArea / camProps.imageArea) * 100;
|
||||
double minArea = (MathHandler.sigmoid(area.get(0)));
|
||||
double maxArea = (MathHandler.sigmoid(area.get(1)));
|
||||
if (AreaRatio < minArea || AreaRatio > maxArea) {
|
||||
continue;
|
||||
}
|
||||
var rect = Imgproc.minAreaRect(new MatOfPoint2f(Contour.toArray()));
|
||||
double minExtent = (extent.get(0).doubleValue() * rect.size.area()) / 100;
|
||||
double maxExtent = (extent.get(1).doubleValue() * rect.size.area()) / 100;
|
||||
if (contourArea <= minExtent || contourArea >= maxExtent) {
|
||||
continue;
|
||||
}
|
||||
Rect bb = Imgproc.boundingRect(Contour);
|
||||
double aspectRatio = ((double)bb.width / bb.height);
|
||||
if (aspectRatio < ratio.get(0).doubleValue() || aspectRatio > ratio.get(1).doubleValue()) {
|
||||
continue;
|
||||
}
|
||||
filteredContours.add(Contour);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error while filtering contours");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(filteredContours, processTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfPoint;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FindContoursPipe implements Pipe<Mat, List<MatOfPoint>> {
|
||||
|
||||
private List<MatOfPoint> foundContours = new ArrayList<>();
|
||||
|
||||
public FindContoursPipe() {}
|
||||
|
||||
@Override
|
||||
public Pair<List<MatOfPoint>, Long> run(Mat input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
foundContours.clear();
|
||||
|
||||
Imgproc.findContours(input, foundContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(foundContours, processTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.util.MathHandler;
|
||||
import com.chameleonvision.vision.enums.TargetGroup;
|
||||
import com.chameleonvision.vision.enums.TargetIntersection;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
import org.opencv.imgproc.Moments;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class GroupContoursPipe implements Pipe<List<MatOfPoint>, List<RotatedRect>> {
|
||||
|
||||
private static final Comparator<MatOfPoint> sortByMomentsX =
|
||||
Comparator.comparingDouble(GroupContoursPipe::calcMomentsX);
|
||||
|
||||
private TargetGroup group;
|
||||
private TargetIntersection intersection;
|
||||
|
||||
private List<RotatedRect> groupedContours = new ArrayList<>();
|
||||
private MatOfPoint2f intersectMatA = new MatOfPoint2f();
|
||||
private MatOfPoint2f intersectMatB = new MatOfPoint2f();
|
||||
|
||||
public GroupContoursPipe(TargetGroup group, TargetIntersection intersection) {
|
||||
this.group = group;
|
||||
this.intersection = intersection;
|
||||
}
|
||||
|
||||
public void setConfig(TargetGroup group, TargetIntersection intersection) {
|
||||
this.group = group;
|
||||
this.intersection = intersection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<List<RotatedRect>, Long> run(List<MatOfPoint> input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
groupedContours.clear();
|
||||
|
||||
if (input.size() > (group.equals(TargetGroup.Single) ? 0 : 1)) {
|
||||
|
||||
List<MatOfPoint> sorted = new ArrayList<>(input);
|
||||
sorted.sort(sortByMomentsX);
|
||||
|
||||
Collections.reverse(sorted);
|
||||
|
||||
switch (group) {
|
||||
case Single: {
|
||||
input.forEach(c -> {
|
||||
MatOfPoint2f contour = new MatOfPoint2f();
|
||||
contour.fromArray(c.toArray());
|
||||
if (contour.cols() != 0 && contour.rows() != 0) {
|
||||
RotatedRect rect = Imgproc.minAreaRect(contour);
|
||||
groupedContours.add(rect);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
case Dual: {
|
||||
for (var i = 0; i < input.size(); i++) {
|
||||
List<Point> finalContourList = new ArrayList<>(input.get(i).toList());
|
||||
|
||||
try {
|
||||
MatOfPoint firstContour = input.get(i);
|
||||
MatOfPoint secondContour = input.get(i + 1);
|
||||
|
||||
if (isIntersecting(firstContour, secondContour)) {
|
||||
finalContourList.addAll(secondContour.toList());
|
||||
} else {
|
||||
finalContourList.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
intersectMatA.release();
|
||||
intersectMatB.release();
|
||||
firstContour.release();
|
||||
secondContour.release();
|
||||
|
||||
MatOfPoint2f contour = new MatOfPoint2f();
|
||||
contour.fromList(finalContourList);
|
||||
|
||||
if (contour.cols() != 0 && contour.rows() != 0) {
|
||||
RotatedRect rect = Imgproc.minAreaRect(contour);
|
||||
groupedContours.add(rect);
|
||||
}
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
finalContourList.clear();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(groupedContours, processTime);
|
||||
}
|
||||
|
||||
private static double calcMomentsX(MatOfPoint c) {
|
||||
Moments m = Imgproc.moments(c);
|
||||
return (m.get_m10() / m.get_m00());
|
||||
}
|
||||
|
||||
private boolean isIntersecting(MatOfPoint contourOne, MatOfPoint contourTwo) {
|
||||
if (intersection.equals(TargetIntersection.None)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
intersectMatA.fromArray(contourOne.toArray());
|
||||
intersectMatB.fromArray(contourTwo.toArray());
|
||||
RotatedRect a = Imgproc.fitEllipse(intersectMatA);
|
||||
RotatedRect b = Imgproc.fitEllipse(intersectMatB);
|
||||
double mA = MathHandler.toSlope(a.angle);
|
||||
double mB = MathHandler.toSlope(b.angle);
|
||||
double x0A = a.center.x;
|
||||
double y0A = a.center.y;
|
||||
double x0B = b.center.x;
|
||||
double y0B = b.center.y;
|
||||
double intersectionX = ((mA * x0A) - y0A - (mB * x0B) + y0B) / (mA - mB);
|
||||
double intersectionY = (mA * (intersectionX - x0A)) + y0A;
|
||||
double massX = (x0A + x0B) / 2;
|
||||
double massY = (y0A + y0B) / 2;
|
||||
switch (intersection) {
|
||||
case Up: {
|
||||
if (intersectionY < massY) {
|
||||
if (mA > 0 && mB < 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Down: {
|
||||
if (intersectionY > massY) {
|
||||
if (mA < 0 && mB > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case Left: {
|
||||
if (intersectionX < massX) {
|
||||
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Right: {
|
||||
if (intersectionX > massX) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.CvException;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class HsvPipe implements Pipe<Mat, Mat> {
|
||||
|
||||
private Scalar hsvLower;
|
||||
private Scalar hsvUpper;
|
||||
|
||||
private Mat processBuffer = new Mat();
|
||||
private Mat outputMat = new Mat();
|
||||
|
||||
public HsvPipe(Scalar hsvLower, Scalar hsvUpper) {
|
||||
this.hsvLower = hsvLower;
|
||||
this.hsvUpper = hsvUpper;
|
||||
}
|
||||
|
||||
public void setConfig(Scalar hsvLower, Scalar hsvUpper) {
|
||||
this.hsvLower = hsvLower;
|
||||
this.hsvUpper = hsvUpper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Mat, Long> run(Mat input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
input.copyTo(processBuffer);
|
||||
|
||||
try {
|
||||
Imgproc.cvtColor(processBuffer, processBuffer, Imgproc.COLOR_BGR2HSV, 3);
|
||||
Core.inRange(processBuffer, hsvLower, hsvUpper, processBuffer);
|
||||
} catch (CvException e) {
|
||||
System.err.println("(HsvPipe) Exception thrown by OpenCV: \n" + e.getMessage());
|
||||
}
|
||||
|
||||
processBuffer.copyTo(outputMat);
|
||||
processBuffer.release();
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(outputMat, processTime);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.CvException;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class OutputMatPipe implements Pipe<Pair<Mat, Mat>, Mat> {
|
||||
|
||||
private boolean showThresholded;
|
||||
|
||||
private Mat processBuffer = new Mat();
|
||||
private Mat outputMat = new Mat();
|
||||
|
||||
public OutputMatPipe(boolean showThresholded) {
|
||||
this.showThresholded = showThresholded;
|
||||
}
|
||||
|
||||
public void setConfig(boolean showThresholded) {
|
||||
this.showThresholded = showThresholded;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param input Input object for pipe
|
||||
* Left is raw camera mat (8UC3), Right is HSV threshold mat (8UC1)
|
||||
* @return Returns desired output Mat, and processing time in nanoseconds
|
||||
*/
|
||||
@Override
|
||||
public Pair<Mat, Long> run(Pair<Mat, Mat> input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
if (showThresholded) {
|
||||
try {
|
||||
input.getRight().copyTo(processBuffer);
|
||||
Imgproc.cvtColor(processBuffer, processBuffer, Imgproc.COLOR_GRAY2BGR, 3);
|
||||
processBuffer.copyTo(outputMat);
|
||||
processBuffer.release();
|
||||
} catch (CvException e) {
|
||||
System.err.println("(OutputMat) Exception thrown by OpenCV: \n" + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
input.getLeft().copyTo(outputMat);
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(outputMat, processTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
public interface Pipe<I, O> {
|
||||
/**
|
||||
*
|
||||
* @param input Input object for pipe
|
||||
* @return Returns a Pair containing the process time in Nanoseconds,
|
||||
* and the output object
|
||||
*/
|
||||
Pair<O, Long> run(I input);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.vision.enums.ImageFlipMode;
|
||||
import com.chameleonvision.vision.enums.ImageRotationMode;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
public class RotateFlipPipe implements Pipe<Mat, Mat> {
|
||||
|
||||
private ImageRotationMode rotation;
|
||||
private ImageFlipMode flip;
|
||||
|
||||
private Mat processBuffer = new Mat();
|
||||
private Mat outputMat = new Mat();
|
||||
|
||||
public RotateFlipPipe(ImageRotationMode rotation, ImageFlipMode flip) {
|
||||
this.rotation = rotation;
|
||||
this.flip = flip;
|
||||
}
|
||||
|
||||
public void setConfig(ImageRotationMode rotation, ImageFlipMode flip) {
|
||||
this.rotation = rotation;
|
||||
this.flip = flip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Mat, Long> run(Mat input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
boolean shouldFlip = !flip.equals(ImageFlipMode.NONE);
|
||||
boolean shouldRotate = !rotation.equals(ImageRotationMode.DEG_0);
|
||||
|
||||
if (shouldFlip || shouldRotate) {
|
||||
input.copyTo(processBuffer);
|
||||
|
||||
if (shouldFlip) {
|
||||
Core.flip(processBuffer, processBuffer, flip.value);
|
||||
}
|
||||
|
||||
if (shouldRotate) {
|
||||
Core.rotate(processBuffer, processBuffer, rotation.value);
|
||||
}
|
||||
|
||||
processBuffer.copyTo(outputMat);
|
||||
processBuffer.release();
|
||||
} else {
|
||||
input.copyTo(outputMat);
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(outputMat, processTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.vision.camera.CaptureStaticProperties;
|
||||
import com.chameleonvision.vision.enums.SortMode;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
import org.opencv.core.RotatedRect;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class SortContoursPipe implements Pipe<List<RotatedRect>, List<RotatedRect>> {
|
||||
|
||||
private final Comparator<RotatedRect> SortByCentermostComparator = Comparator.comparingDouble(this::calcCenterDistance);
|
||||
|
||||
private static final Comparator<RotatedRect> SortByLargestComparator = (rect1, rect2) -> Double.compare(rect2.size.area(), rect1.size.area());
|
||||
private static final Comparator<RotatedRect> SortBySmallestComparator = SortByLargestComparator.reversed();
|
||||
|
||||
private static final Comparator<RotatedRect> SortByHighestComparator = (rect1, rect2) -> Double.compare(rect2.center.y, rect1.center.y);
|
||||
private static final Comparator<RotatedRect> SortByLowestComparator = SortByHighestComparator.reversed();
|
||||
|
||||
private static final Comparator<RotatedRect> SortByLeftmostComparator = Comparator.comparingDouble(rect -> rect.center.x);
|
||||
private static final Comparator<RotatedRect> SortByRightmostComparator = SortByLeftmostComparator.reversed();
|
||||
|
||||
private SortMode sort;
|
||||
private CaptureStaticProperties camProps;
|
||||
private int maxTargets;
|
||||
|
||||
private List<RotatedRect> sortedContours = new ArrayList<>();
|
||||
|
||||
public SortContoursPipe(SortMode sort, CaptureStaticProperties camProps, int maxTargets) {
|
||||
this.sort = sort;
|
||||
this.camProps = camProps;
|
||||
this.maxTargets = maxTargets;
|
||||
}
|
||||
|
||||
public void setConfig(SortMode sort, CaptureStaticProperties camProps, int maxTargets) {
|
||||
this.sort = sort;
|
||||
this.camProps = camProps;
|
||||
this.maxTargets = maxTargets;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<List<RotatedRect>, Long> run(List<RotatedRect> input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
sortedContours.clear();
|
||||
|
||||
if (input.size() > 0) {
|
||||
sortedContours.addAll(input.subList(0, Math.min(input.size(), maxTargets - 1)));
|
||||
|
||||
switch (sort) {
|
||||
case Largest:
|
||||
sortedContours.sort(SortByLargestComparator);
|
||||
break;
|
||||
case Smallest:
|
||||
sortedContours.sort(SortBySmallestComparator);
|
||||
break;
|
||||
case Highest:
|
||||
sortedContours.sort(SortByHighestComparator);
|
||||
break;
|
||||
case Lowest:
|
||||
sortedContours.sort(SortByLowestComparator);
|
||||
break;
|
||||
case Leftmost:
|
||||
sortedContours.sort(SortByLeftmostComparator);
|
||||
break;
|
||||
case Rightmost:
|
||||
sortedContours.sort(SortByRightmostComparator);
|
||||
break;
|
||||
case Centermost:
|
||||
sortedContours.sort(SortByCentermostComparator);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(sortedContours, processTime);
|
||||
}
|
||||
|
||||
private double calcCenterDistance(RotatedRect rect) {
|
||||
return FastMath.sqrt(FastMath.pow(camProps.centerX - rect.center.x, 2) + FastMath.pow(camProps.centerY - rect.center.y, 2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.MatOfPoint;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SpeckleRejectPipe implements Pipe<List<MatOfPoint>, List<MatOfPoint>> {
|
||||
|
||||
private double minPercentOfAvg;
|
||||
|
||||
private List<MatOfPoint> despeckledContours = new ArrayList<>();
|
||||
|
||||
public SpeckleRejectPipe(double minPercentOfAvg) {
|
||||
this.minPercentOfAvg = minPercentOfAvg;
|
||||
}
|
||||
|
||||
public void setConfig(double minPercentOfAvg) {
|
||||
this.minPercentOfAvg = minPercentOfAvg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<List<MatOfPoint>, Long> run(List<MatOfPoint> input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
despeckledContours.clear();
|
||||
|
||||
if (input.size() > 0) {
|
||||
double averageArea = 0.0;
|
||||
|
||||
for (MatOfPoint c : input) {
|
||||
averageArea += Imgproc.contourArea(c);
|
||||
}
|
||||
|
||||
averageArea /= input.size();
|
||||
|
||||
double minAllowedArea = minPercentOfAvg / 100.0 * averageArea;
|
||||
|
||||
for (MatOfPoint c : input) {
|
||||
if (Imgproc.contourArea(c) >= minAllowedArea) {
|
||||
despeckledContours.add(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(despeckledContours, processTime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user