2019-09-15 15:03:44 -04:00
|
|
|
package com.chameleonvision.vision.process;
|
2019-09-10 23:47:06 +03:00
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
import com.chameleonvision.vision.CameraValues;
|
2019-09-17 02:12:53 -04:00
|
|
|
import org.apache.commons.math3.util.FastMath;
|
2019-09-14 17:14:49 +03:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
import org.opencv.core.*;
|
|
|
|
|
import org.opencv.imgproc.*;
|
|
|
|
|
|
2019-09-17 02:12:53 -04:00
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
2019-09-14 17:14:49 +03:00
|
|
|
|
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
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
private final CameraValues CamVals;
|
2019-09-15 19:07:58 -04:00
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
VisionProcess(CameraValues camVals){
|
|
|
|
|
CamVals = camVals;
|
2019-09-14 20:38:50 +03:00
|
|
|
}
|
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-16 04:08:23 -04:00
|
|
|
private Mat hsvImage = new Mat();
|
|
|
|
|
void HSVThreshold(Mat srcImage, Mat dst, @NotNull Scalar hsvLower, @NotNull Scalar hsvUpper, boolean shouldErode, boolean shouldDilate) {
|
|
|
|
|
Imgproc.cvtColor(srcImage, hsvImage, Imgproc.COLOR_RGB2HSV,3);
|
|
|
|
|
Core.inRange(hsvImage, hsvLower, hsvUpper, dst);
|
|
|
|
|
if (shouldErode){
|
|
|
|
|
Imgproc.erode(dst, dst, Kernel);
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
2019-09-16 04:08:23 -04:00
|
|
|
if (shouldDilate){
|
|
|
|
|
Imgproc.dilate(dst, dst, Kernel);
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
2019-09-16 04:08:23 -04:00
|
|
|
hsvImage.release();
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
|
|
|
|
|
private List<MatOfPoint> FoundContours = new ArrayList<>();
|
2019-09-16 04:08:23 -04:00
|
|
|
private Mat binaryMat = new Mat();
|
|
|
|
|
List<MatOfPoint> FindContours(Mat src) {
|
|
|
|
|
src.copyTo(binaryMat);
|
2019-09-15 19:07:58 -04:00
|
|
|
FoundContours.clear();
|
2019-09-16 04:08:23 -04:00
|
|
|
Imgproc.findContours(binaryMat, FoundContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
|
|
|
|
binaryMat.release();
|
2019-09-15 19:07:58 -04:00
|
|
|
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>();
|
2019-09-16 11:45:56 -04:00
|
|
|
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);
|
2019-09-16 04:08:23 -04:00
|
|
|
double targetArea = (contourArea / CamVals.ImageArea) * 100;
|
2019-09-16 11:45:56 -04:00
|
|
|
if (targetArea <= area.get(0) || targetArea >= area.get(1)){
|
2019-09-14 20:38:50 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
var rect = Imgproc.minAreaRect(new MatOfPoint2f(Contour.toArray()));
|
2019-09-17 02:12:53 -04:00
|
|
|
var targetFullness = (contourArea / rect.size.area()) * 100;
|
2019-09-14 20:38:50 +03:00
|
|
|
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
|
|
|
}
|
2019-09-16 04:08:23 -04:00
|
|
|
catch (Exception ignored) { }
|
2019-09-14 20:38:50 +03:00
|
|
|
}
|
|
|
|
|
return FilteredContours;
|
|
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
|
2019-09-17 02:12:53 -04:00
|
|
|
private static Comparator<RotatedRect> SortByLargestComparator = (rect1, rect2) -> Double.compare(rect2.size.area(), rect1.size.area());
|
|
|
|
|
private static Comparator<RotatedRect> SortBySmallestComparator = SortByLargestComparator.reversed();
|
|
|
|
|
|
|
|
|
|
private static Comparator<RotatedRect> SortByHighestComparator = (rect1, rect2) -> Double.compare(rect2.center.y, rect1.center.y);
|
|
|
|
|
private static Comparator<RotatedRect> SortByLowestComparator = SortByHighestComparator.reversed();
|
|
|
|
|
|
|
|
|
|
private static Comparator<RotatedRect> SortByLeftmostComparator = Comparator.comparingDouble(rect -> rect.center.x);
|
|
|
|
|
private static Comparator<RotatedRect> SortByRightmostComparator = SortByLeftmostComparator.reversed();
|
|
|
|
|
|
|
|
|
|
private double calcDistance(RotatedRect rect) {
|
|
|
|
|
return FastMath.sqrt(FastMath.pow(CamVals.CenterX - rect.center.x, 2) + FastMath.pow(CamVals.CenterY - rect.center.y, 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Comparator<RotatedRect> SortByCentermostComparator = Comparator.comparingDouble(this::calcDistance);
|
|
|
|
|
|
|
|
|
|
RotatedRect SortTargetsToOne(List<RotatedRect> inputRects, String sortMode) {
|
|
|
|
|
switch (sortMode) {
|
|
|
|
|
case "Largest":
|
|
|
|
|
return Collections.max(inputRects, Comparator.comparing(rect -> rect.size.area()));
|
|
|
|
|
case "Smallest":
|
|
|
|
|
return Collections.min(inputRects, Comparator.comparing(rect -> rect.size.area()));
|
|
|
|
|
case "Highest":
|
|
|
|
|
return Collections.min(inputRects, Comparator.comparing(rect -> rect.center.y));
|
|
|
|
|
case "Lowest":
|
|
|
|
|
return Collections.max(inputRects, Comparator.comparing(rect -> rect.center.y));
|
|
|
|
|
case "Leftmost":
|
|
|
|
|
return Collections.min(inputRects, Comparator.comparing(rect -> rect.center.x));
|
|
|
|
|
case "Rightmost":
|
|
|
|
|
return Collections.max(inputRects, Comparator.comparing(rect -> rect.center.x));
|
|
|
|
|
case "Centermost":
|
2019-09-17 11:22:54 +03:00
|
|
|
return Collections.min(inputRects, SortByCentermostComparator);
|
|
|
|
|
// return inputRects.stream().sorted(SortByCentermostComparator).collect(Collectors.toList()).get(0);
|
2019-09-17 02:12:53 -04:00
|
|
|
default:
|
|
|
|
|
return inputRects.get(0); // default to whatever the first contour is, but this should never happen
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SortTargets(List<RotatedRect> inputRects, String sortMode) {
|
|
|
|
|
switch (sortMode) {
|
|
|
|
|
case "Largest":
|
|
|
|
|
inputRects.sort(SortByLargestComparator);
|
|
|
|
|
break;
|
|
|
|
|
case "Smallest":
|
|
|
|
|
inputRects.sort(SortBySmallestComparator);
|
|
|
|
|
break;
|
|
|
|
|
case "Highest":
|
|
|
|
|
inputRects.sort(SortByHighestComparator);
|
|
|
|
|
break;
|
|
|
|
|
case "Lowest":
|
|
|
|
|
inputRects.sort(SortByLowestComparator);
|
|
|
|
|
break;
|
|
|
|
|
case "Leftmost":
|
|
|
|
|
inputRects.sort(SortByLeftmostComparator);
|
|
|
|
|
break;
|
|
|
|
|
case "Rightmost":
|
|
|
|
|
inputRects.sort(SortByRightmostComparator);
|
|
|
|
|
break;
|
|
|
|
|
case "Centermost":
|
|
|
|
|
inputRects.sort(SortByCentermostComparator);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 21:45:04 +03:00
|
|
|
private List<RotatedRect> FinalCountours = new ArrayList<>();
|
2019-09-17 02:12:53 -04:00
|
|
|
List<RotatedRect> GroupTargets(List<MatOfPoint> InputContours, String IntersectionPoint, String TargetGroup) {
|
2019-09-15 19:07:58 -04:00
|
|
|
FinalCountours.clear();
|
2019-09-14 20:38:50 +03:00
|
|
|
if (!TargetGroup.equals("Single")){
|
|
|
|
|
for (var i = 0; i < InputContours.size(); i++){
|
2019-09-16 21:45:04 +03:00
|
|
|
List<Point> FinalContourList = new ArrayList<>(InputContours.get(i).toList());
|
2019-09-17 02:12:53 -04:00
|
|
|
for (var c = 0; c < (TargetGrouping.get(TargetGroup) - 1); c++){
|
2019-09-14 20:38:50 +03:00
|
|
|
try{
|
|
|
|
|
MatOfPoint firstContour = InputContours.get(i + c);
|
2019-09-17 02:12:53 -04:00
|
|
|
MatOfPoint secondContour = InputContours.get(i + c + 1);
|
2019-09-15 19:07:58 -04:00
|
|
|
if (IsIntersecting(firstContour, secondContour, IntersectionPoint)){
|
2019-09-16 21:45:04 +03:00
|
|
|
FinalContourList.addAll(secondContour.toList());
|
2019-09-14 20:38:50 +03:00
|
|
|
}
|
2019-09-15 19:07:58 -04:00
|
|
|
firstContour.release();
|
|
|
|
|
secondContour.release();
|
2019-09-16 21:45:04 +03:00
|
|
|
MatOfPoint2f contour = new MatOfPoint2f();
|
|
|
|
|
contour.fromList(FinalContourList);
|
2019-09-17 02:12:53 -04:00
|
|
|
if (contour.cols() != 0 && contour.rows() != 0){
|
2019-09-16 21:45:04 +03:00
|
|
|
RotatedRect rect = Imgproc.minAreaRect(contour);
|
|
|
|
|
FinalCountours.add(rect);
|
|
|
|
|
}
|
2019-09-14 20:38:50 +03:00
|
|
|
} catch (IndexOutOfBoundsException e){
|
2019-09-16 21:45:04 +03:00
|
|
|
FinalContourList.clear();
|
2019-09-14 20:38:50 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 21:45:04 +03:00
|
|
|
} else {
|
2019-09-17 02:12:53 -04:00
|
|
|
for (MatOfPoint inputContour : InputContours) {
|
2019-09-16 21:45:04 +03:00
|
|
|
MatOfPoint2f contour = new MatOfPoint2f();
|
2019-09-17 02:12:53 -04:00
|
|
|
contour.fromArray(inputContour.toArray());
|
|
|
|
|
if (contour.cols() != 0 && contour.rows() != 0) {
|
2019-09-16 21:45:04 +03:00
|
|
|
RotatedRect rect = Imgproc.minAreaRect(contour);
|
|
|
|
|
FinalCountours.add(rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-14 20:38:50 +03:00
|
|
|
}
|
2019-09-16 21:45:04 +03:00
|
|
|
return FinalCountours;
|
2019-09-14 20:38:50 +03:00
|
|
|
}
|
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) {
|
2019-09-16 21:45:04 +03:00
|
|
|
if (IntersectionPoint.equals("None")){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
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);
|
|
|
|
|
double vxA = intersectMatA.get(0,0)[0];
|
|
|
|
|
double vyA = intersectMatA.get(1,0)[0];
|
|
|
|
|
double x0A = intersectMatA.get(2,0)[0];
|
|
|
|
|
double y0A = intersectMatA.get(3,0)[0];
|
|
|
|
|
double mA = vyA / vxA;
|
|
|
|
|
double vxB = intersectMatB.get(0,0)[0];
|
|
|
|
|
double vyB = intersectMatB.get(1,0)[0];
|
|
|
|
|
double x0B = intersectMatB.get(2,0)[0];
|
|
|
|
|
double y0B = intersectMatB.get(3,0)[0];
|
|
|
|
|
double mB = vyB / vxB;
|
|
|
|
|
double intersectionX = (mA * x0A) - y0A - (mB * x0B) + y0B / (mA - mB);
|
|
|
|
|
double intersectionY = (mA * (intersectionX - x0A)) + y0A;
|
|
|
|
|
switch (IntersectionPoint){
|
|
|
|
|
case "Up" :{
|
|
|
|
|
if (intersectionY < CamVals.CenterY){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "Down": {
|
|
|
|
|
if (intersectionY > CamVals.CenterY){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "Left": {
|
|
|
|
|
if (intersectionX < CamVals.CenterX){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "Right": {
|
|
|
|
|
if (intersectionX > CamVals.CenterX){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-09-14 20:38:50 +03:00
|
|
|
}
|
2019-09-10 23:47:06 +03:00
|
|
|
}
|