added target grouping

This commit is contained in:
ori agranat
2019-09-14 20:38:50 +03:00
parent 1b8943eb27
commit d6850f73b1
5 changed files with 87 additions and 9 deletions

View File

@@ -20,15 +20,16 @@ public class CameraProcess implements Runnable{
@Override
public void run() {
//calling all classes
CameraServer cs = CameraServer.getInstance();
NetworkTableInstance networkTableInstance = NetworkTableInstance.getDefault();
// NetworkTableInstance networkTableInstance = NetworkTableInstance.getDefault();
SettingsManager manager = SettingsManager.getInstance();
VisionProcess visionProcess = new VisionProcess();
manager.CamerasCurrentPipeline.put(CameraName,manager.Cameras.get(CameraName).pipelines.keySet().toArray()[0].toString());
//Setting up camera and network table
var Table = networkTableInstance.getTable("/Chameleon-Vision/" + CameraName);
var PipeLineEntry = Table.getEntry("Pipeline");
var DriverModeEntry = Table.getEntry("Driver_Mode");
// var Table = networkTableInstance.getTable("/Chameleon-Vision/" + CameraName);
// var PipeLineEntry = Table.getEntry("Pipeline");
// var DriverModeEntry = Table.getEntry("Driver_Mode");
var cv_sink = cs.getVideo(manager.UsbCameras.get(CameraName));
int Width = manager.Cameras.get(CameraName).camVideoMode.width;
int Height = manager.Cameras.get(CameraName).camVideoMode.heigh;
@@ -42,14 +43,20 @@ public class CameraProcess implements Runnable{
double VerticalView = FastMath.atan(FastMath.tan(DiagonalView/2) * (VerticalRatio / DiagonalView)) * 2;
double H_FOCAL_LENGTH = Width / (2 * FastMath.tan(HorizontalView /2));
double V_FOCAL_LENGTH = Width / (2 * FastMath.tan(VerticalView /2));
double CenterX = ((double) Width / 2) - 0.5;
double CenterY = ((double) Height/2) - 0.5;
double CamArea = (double)(Width * Height);
VisionProcess visionProcess = new VisionProcess(CenterX,CenterY,CamArea);
Mat mat = new Mat();
long time;
while (!Thread.interrupted()){
Pipeline pipeline = manager.Cameras.get(CameraName).pipelines.get(manager.CamerasCurrentPipeline.get(CameraName));
time = cv_sink.grabFrame(mat);
Mat HSVImage = visionProcess.HSVThreshold(pipeline.hue,pipeline.saturation,pipeline.value,mat,pipeline.erode,pipeline.dilate);
List<MatOfPoint> Contours = visionProcess.FindContours(HSVImage);
// Mat HSVImage = visionProcess.HSVThreshold(pipeline.hue,pipeline.saturation,pipeline.value,mat,pipeline.erode,pipeline.dilate);
// List<MatOfPoint> Contours = visionProcess.FindContours(HSVImage);
// List<MatOfPoint> FilterdContours = visionProcess.FilterContours(Contours,pipeline.area,pipeline.ratio,pipeline.extent,pipeline.sort_mode,pipeline.target_intersection,pipeline.target_group);
cv_publish.putFrame(mat);
}
}

View File

@@ -4,10 +4,25 @@ import org.jetbrains.annotations.NotNull;
import org.opencv.core.*;
import org.opencv.imgproc.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class VisionProcess {
private HashMap<String, Integer>TargetGrouping= new HashMap<String, Integer>(){{
put("Single",1);
put("Dual",2);
put("Triple",3);
put("Quadruple",4);
put("Quintuple",5);
}};
private double CamArea,CenterX, CenterY;
VisionProcess(double CenterX, double CenterY, double CamArea){
this.CenterX = CenterX;
this.CenterY = CenterY;
this.CamArea = CamArea;
}
private Mat Kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5));
public Mat HSVThreshold(@NotNull List<Integer> hue, @NotNull List<Integer> saturation , @NotNull List<Integer> value, Mat image, boolean IsErode, boolean IsDilate){
@@ -29,4 +44,59 @@ public class VisionProcess {
Imgproc.findContours(BinaryImage,Contours,new Mat(),Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_TC89_L1);
return Contours;
}
public List<MatOfPoint> FilterContours(List<MatOfPoint> InputContours, List<Integer> area, List<Integer> ratio, List<Integer> extent, String SortMode,String TargetIntersection , String TargetGrouping){
List<MatOfPoint> FilteredContours = new ArrayList<MatOfPoint>();
for (MatOfPoint Contour : InputContours){
try{
var contourArea = Imgproc.contourArea(Contour);
double targetArea = (contourArea / CamArea) * 100;
if (targetArea >= area.get(0) || targetArea <= area.get(1)){
continue;
}
var rect = Imgproc.minAreaRect(new MatOfPoint2f(Contour.toArray()));
var targetFullness = (contourArea/rect.size.area())*100;
if (targetFullness <= extent.get(0) || targetArea >= extent.get(1)){
continue;
}
var aspectRatio = rect.size.width / rect.size.height;
if (aspectRatio <= ratio.get(0) || aspectRatio >= ratio.get(1)){
continue;
}
FilteredContours.add(Contour);
} catch (Exception e){
continue;
}
}
return FilteredContours;
}
private List<MatOfPoint> GroupTargets(List<MatOfPoint> InputContours, String IntersectionPoint,String TargetGroup){
if (!TargetGroup.equals("Single")){
List<MatOfPoint> FinalCountours = new ArrayList<MatOfPoint>();
for (var i = 0; i < InputContours.size(); i++){
var FinalContour = InputContours.get(i);
for (var c = 0; c < (TargetGrouping.get(TargetGroup)-1);c++){
try{
MatOfPoint firstContour = InputContours.get(i + c);
MatOfPoint secoundContour = InputContours.get(i+c+1);
if (IsIntersecting(firstContour,secoundContour, IntersectionPoint)){
System.out.println("");
}
} catch (IndexOutOfBoundsException e){
FinalContour = new MatOfPoint();
break;
}
}
}
}
return InputContours;
}
private boolean IsIntersecting(MatOfPoint ContourOne, MatOfPoint ContourTwo, String IntersectionPoint){
Mat LineA = new Mat();
Imgproc.fitLine(ContourOne,LineA,Imgproc.CV_DIST_L2,0,0.01,0.01);
Mat LineB = new Mat();
Imgproc.fitLine(ContourTwo,LineB,Imgproc.CV_DIST_L2,0,0.01,0.01);
return true;
}
}

View File

@@ -10,9 +10,9 @@ import java.util.Map;
public class Main {
public static void main(String [] args) {
SettingsManager manager = SettingsManager.getInstance();
NetworkTableInstance.getDefault().startClientTeam(SettingsManager.GeneralSettings.team_number);
// NetworkTableInstance.getDefault().startClientTeam(SettingsManager.GeneralSettings.team_number);
for (Map.Entry<String, UsbCamera> entry: SettingsManager.UsbCameras.entrySet()){
new Thread(new CameraProcess(entry.getKey()));
new Thread(new CameraProcess(entry.getKey())).start();
}
Server.main(8888);
}

View File

@@ -16,6 +16,7 @@ public class Pipeline {
public List<Integer> extent = Arrays.asList(0,100);
public boolean is_binary = false;
public String sort_mode = "Largest";
public String target_group = "Single";
public String target_intersection = "Largest";
public double M = 1;
public double B = 0;