2019-09-15 15:03:44 -04:00
|
|
|
package com.chameleonvision.vision.process;
|
2019-09-10 23:47:06 +03:00
|
|
|
|
2019-09-14 17:14:49 +03:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
import org.opencv.core.*;
|
|
|
|
|
import org.opencv.imgproc.*;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2019-09-14 20:38:50 +03:00
|
|
|
import java.util.HashMap;
|
2019-09-14 17:14:49 +03:00
|
|
|
import java.util.List;
|
|
|
|
|
|
2019-09-10 23:47:06 +03:00
|
|
|
public class VisionProcess {
|
2019-09-15 19:07:58 -04:00
|
|
|
|
|
|
|
|
private HashMap<String, Integer>TargetGrouping= new HashMap<>() {{
|
|
|
|
|
put("Single", 1);
|
|
|
|
|
put("Dual", 2);
|
|
|
|
|
put("Triple", 3);
|
|
|
|
|
put("Quadruple", 4);
|
|
|
|
|
put("Quintuple", 5);
|
2019-09-14 20:38:50 +03:00
|
|
|
}};
|
2019-09-15 19:07:58 -04:00
|
|
|
|
|
|
|
|
private double CamArea, CenterX, CenterY;
|
|
|
|
|
|
2019-09-14 20:38:50 +03:00
|
|
|
VisionProcess(double CenterX, double CenterY, double CamArea){
|
|
|
|
|
this.CenterX = CenterX;
|
|
|
|
|
this.CenterY = CenterY;
|
|
|
|
|
this.CamArea = CamArea;
|
|
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
|
2019-09-14 17:14:49 +03:00
|
|
|
private Mat Kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5));
|
|
|
|
|
|
2019-09-15 19:07:58 -04:00
|
|
|
private Mat hsvMat = new Mat();
|
|
|
|
|
private Mat hsvThreshMat = new Mat();
|
|
|
|
|
private Scalar hsvLower, hsvUpper;
|
|
|
|
|
|
|
|
|
|
Mat HSVThreshold(@NotNull List<Integer> hue, @NotNull List<Integer> saturation, @NotNull List<Integer> value, Mat image, boolean IsErode, boolean IsDilate){
|
|
|
|
|
Imgproc.cvtColor(image, hsvMat,Imgproc.COLOR_BGR2HSV,3);
|
|
|
|
|
hsvLower = new Scalar(hue.get(0), saturation.get(0), value.get(0));
|
|
|
|
|
hsvUpper = new Scalar(hue.get(1), saturation.get(1), value.get(1));
|
|
|
|
|
Core.inRange(hsvMat, hsvLower, hsvUpper, hsvThreshMat);
|
2019-09-14 17:14:49 +03:00
|
|
|
if (IsErode){
|
2019-09-15 19:07:58 -04:00
|
|
|
Imgproc.erode(hsvThreshMat, hsvThreshMat, Kernel);
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
|
|
|
|
if (IsDilate){
|
2019-09-15 19:07:58 -04:00
|
|
|
Imgproc.dilate(hsvThreshMat, hsvThreshMat, Kernel);
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
return hsvThreshMat;
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
|
|
|
|
|
private List<MatOfPoint> FoundContours = new ArrayList<>();
|
2019-09-14 17:14:49 +03:00
|
|
|
public List<MatOfPoint> FindContours(Mat BinaryImage){
|
2019-09-15 19:07:58 -04:00
|
|
|
FoundContours.clear();
|
|
|
|
|
Imgproc.findContours(BinaryImage, FoundContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
|
|
|
|
BinaryImage.release();
|
|
|
|
|
return FoundContours;
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
|
|
|
|
|
private List<MatOfPoint> FilteredContours = new ArrayList<MatOfPoint>();
|
|
|
|
|
public List<MatOfPoint> FilterContours(List<MatOfPoint> InputContours, List<Integer> area, List<Integer> ratio, List<Integer> extent, String SortMode, String TargetIntersection, String TargetGrouping){
|
2019-09-14 20:38:50 +03:00
|
|
|
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);
|
2019-09-15 19:07:58 -04:00
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
|
2019-09-14 20:38:50 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return FilteredContours;
|
|
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
|
|
|
|
|
private List<MatOfPoint> FinalCountours = new ArrayList<>();
|
|
|
|
|
private List<MatOfPoint> GroupTargets(List<MatOfPoint> InputContours, String IntersectionPoint,String TargetGroup) {
|
|
|
|
|
FinalCountours.clear();
|
2019-09-14 20:38:50 +03:00
|
|
|
if (!TargetGroup.equals("Single")){
|
|
|
|
|
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);
|
2019-09-15 19:07:58 -04:00
|
|
|
MatOfPoint secondContour = InputContours.get(i+c+1);
|
|
|
|
|
if (IsIntersecting(firstContour, secondContour, IntersectionPoint)){
|
2019-09-14 20:38:50 +03:00
|
|
|
System.out.println("");
|
|
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
firstContour.release();
|
|
|
|
|
secondContour.release();
|
2019-09-14 20:38:50 +03:00
|
|
|
} catch (IndexOutOfBoundsException e){
|
|
|
|
|
FinalContour = new MatOfPoint();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return InputContours;
|
|
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
|
|
|
|
|
private Mat intersectMatA = new Mat();
|
|
|
|
|
private Mat intersectMatB = new Mat();
|
|
|
|
|
private boolean IsIntersecting(MatOfPoint ContourOne, MatOfPoint ContourTwo, String IntersectionPoint) {
|
|
|
|
|
Imgproc.fitLine(ContourOne, intersectMatA, Imgproc.CV_DIST_L2,0,0.01,0.01);
|
|
|
|
|
Imgproc.fitLine(ContourTwo, intersectMatB, Imgproc.CV_DIST_L2,0,0.01,0.01);
|
|
|
|
|
// Rect2d =
|
2019-09-14 20:38:50 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
2019-09-10 23:47:06 +03:00
|
|
|
}
|