From b618b7f897186bb7b00020100dc19150a14f8c76 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 2 Oct 2019 22:12:52 -0700 Subject: [PATCH] Refactor member variables and functions to start with lowercase letters --- .../vision/process/CVProcess.java | 78 +++++++++---------- .../vision/process/VisionProcess.java | 43 +++++----- 2 files changed, 60 insertions(+), 61 deletions(-) diff --git a/Main/src/main/java/com/chameleonvision/vision/process/CVProcess.java b/Main/src/main/java/com/chameleonvision/vision/process/CVProcess.java index d701efbf5..bee2cb7ae 100644 --- a/Main/src/main/java/com/chameleonvision/vision/process/CVProcess.java +++ b/Main/src/main/java/com/chameleonvision/vision/process/CVProcess.java @@ -11,54 +11,54 @@ import java.util.*; @SuppressWarnings("WeakerAccess") public class CVProcess { - private final CameraValues CamVals; - private HashMap TargetGrouping = new HashMap<>() {{ + private final CameraValues cameraValues; + private HashMap targetGrouping = new HashMap<>() {{ put("Single", 1); put("Dual", 2); put("Triple", 3); put("Quadruple", 4); put("Quintuple", 5); }}; - private Mat Kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5)); + private Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5)); private Mat hsvImage = new Mat(); - private List FoundContours = new ArrayList<>(); + private List foundContours = new ArrayList<>(); private Mat binaryMat = new Mat(); - private List FilteredContours = new ArrayList<>(); - private Comparator SortByCentermostComparator = Comparator.comparingDouble(this::calcDistance); - private List FinalCountours = new ArrayList<>(); + private List filteredContours = new ArrayList<>(); + private Comparator sortByCentermostComparator = Comparator.comparingDouble(this::calcDistance); + private List finalCountours = new ArrayList<>(); private Mat intersectMatA = new Mat(); private Mat intersectMatB = new Mat(); - CVProcess(CameraValues camVals) { - CamVals = camVals; + CVProcess(CameraValues cameraValues) { + this.cameraValues = cameraValues; } - void HSVThreshold(Mat srcImage, Mat dst, @NotNull Scalar hsvLower, @NotNull Scalar hsvUpper, boolean shouldErode, boolean shouldDilate) { + 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); + Imgproc.erode(dst, dst, kernel); } if (shouldDilate) { - Imgproc.dilate(dst, dst, Kernel); + Imgproc.dilate(dst, dst, kernel); } hsvImage.release(); } - List FindContours(Mat src) { + List findContours(Mat src) { src.copyTo(binaryMat); - FoundContours.clear(); - Imgproc.findContours(binaryMat, FoundContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1); + foundContours.clear(); + Imgproc.findContours(binaryMat, foundContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1); binaryMat.release(); - return FoundContours; + return foundContours; } - List FilterContours(List InputContours, List area, List ratio, List extent) { - for (MatOfPoint Contour : InputContours) { + List filterContours(List inputContours, List area, List ratio, List extent) { + for (MatOfPoint Contour : inputContours) { try { double contourArea = Imgproc.contourArea(Contour); - double minArea = (MathHandler.sigmoid(area.get(0)) * CamVals.ImageArea) / 100; - double maxArea = (MathHandler.sigmoid(area.get(1)) * CamVals.ImageArea) / 100; + double minArea = (MathHandler.sigmoid(area.get(0)) * cameraValues.ImageArea) / 100; + double maxArea = (MathHandler.sigmoid(area.get(1)) * cameraValues.ImageArea) / 100; if (contourArea <= minArea || contourArea >= maxArea) { continue; } @@ -74,20 +74,20 @@ public class CVProcess { if (aspectRatio < ratio.get(0) || aspectRatio > ratio.get(1)) { continue; } - FilteredContours.add(Contour); + filteredContours.add(Contour); } catch (Exception e) { System.err.println("Error while filtering contours"); e.printStackTrace(); } } - return FilteredContours; + return filteredContours; } private double calcDistance(RotatedRect rect) { - return FastMath.sqrt(FastMath.pow(CamVals.CenterX - rect.center.x, 2) + FastMath.pow(CamVals.CenterY - rect.center.y, 2)); + return FastMath.sqrt(FastMath.pow(cameraValues.CenterX - rect.center.x, 2) + FastMath.pow(cameraValues.CenterY - rect.center.y, 2)); } - RotatedRect SortTargetsToOne(List inputRects, String sortMode) { + RotatedRect sortTargetsToOne(List inputRects, String sortMode) { switch (sortMode) { case "Largest": return Collections.max(inputRects, Comparator.comparing(rect -> rect.size.area())); @@ -102,22 +102,22 @@ public class CVProcess { case "Rightmost": return Collections.max(inputRects, Comparator.comparing(rect -> rect.center.x)); case "Centermost": - return Collections.min(inputRects, SortByCentermostComparator); + return Collections.min(inputRects, sortByCentermostComparator); default: return inputRects.get(0); // default to whatever the first contour is, but this should never happen } } - List GroupTargets(List InputContours, String IntersectionPoint, String TargetGroup) { - FinalCountours.clear(); - if (!TargetGroup.equals("Single")) { - for (var i = 0; i < InputContours.size(); i++) { - List FinalContourList = new ArrayList<>(InputContours.get(i).toList()); - for (var c = 0; c < (TargetGrouping.get(TargetGroup) - 1); c++) { + List groupTargets(List inputContours, String intersectionPoint, String targetGroup) { + finalCountours.clear(); + if (!targetGroup.equals("Single")) { + for (var i = 0; i < inputContours.size(); i++) { + List FinalContourList = new ArrayList<>(inputContours.get(i).toList()); + for (var c = 0; c < (targetGrouping.get(targetGroup) - 1); c++) { try { - MatOfPoint firstContour = InputContours.get(i + c); - MatOfPoint secondContour = InputContours.get(i + c + 1); - if (IsIntersecting(firstContour, secondContour, IntersectionPoint)) { + MatOfPoint firstContour = inputContours.get(i + c); + MatOfPoint secondContour = inputContours.get(i + c + 1); + if (isIntersecting(firstContour, secondContour, intersectionPoint)) { FinalContourList.addAll(secondContour.toList()); } else{ @@ -130,7 +130,7 @@ public class CVProcess { contour.fromList(FinalContourList); if (contour.cols() != 0 && contour.rows() != 0) { RotatedRect rect = Imgproc.minAreaRect(contour); - FinalCountours.add(rect); + finalCountours.add(rect); } } catch (IndexOutOfBoundsException e) { FinalContourList.clear(); @@ -140,19 +140,19 @@ public class CVProcess { } } else { - for (MatOfPoint inputContour : InputContours) { + for (MatOfPoint inputContour : inputContours) { MatOfPoint2f contour = new MatOfPoint2f(); contour.fromArray(inputContour.toArray()); if (contour.cols() != 0 && contour.rows() != 0) { RotatedRect rect = Imgproc.minAreaRect(contour); - FinalCountours.add(rect); + finalCountours.add(rect); } } } - return FinalCountours; + return finalCountours; } - private boolean IsIntersecting(MatOfPoint ContourOne, MatOfPoint ContourTwo, String IntersectionPoint) { + private boolean isIntersecting(MatOfPoint ContourOne, MatOfPoint ContourTwo, String IntersectionPoint) { if (IntersectionPoint.equals("None")) { return true; } diff --git a/Main/src/main/java/com/chameleonvision/vision/process/VisionProcess.java b/Main/src/main/java/com/chameleonvision/vision/process/VisionProcess.java index 96c95fcf0..8440f108f 100644 --- a/Main/src/main/java/com/chameleonvision/vision/process/VisionProcess.java +++ b/Main/src/main/java/com/chameleonvision/vision/process/VisionProcess.java @@ -3,7 +3,6 @@ package com.chameleonvision.vision.process; import com.chameleonvision.settings.SettingsManager; import com.chameleonvision.vision.Pipeline; import com.chameleonvision.vision.camera.Camera; -import com.chameleonvision.web.Server; import com.chameleonvision.web.ServerHandler; import edu.wpi.cscore.VideoException; import edu.wpi.first.networktables.*; @@ -31,14 +30,14 @@ public class VisionProcess implements Runnable { private Pipeline currentPipeline; private CVProcess cvProcess; // pipeline process items - private List FoundContours = new ArrayList<>(); - private List FilteredContours = new ArrayList<>(); - private List GroupedContours = new ArrayList<>(); + private List foundContours = new ArrayList<>(); + private List filteredContours = new ArrayList<>(); + private List groupedContours = new ArrayList<>(); private Mat cameraInputMat = new Mat(); private Mat hsvThreshMat = new Mat(); private Mat streamOutputMat = new Mat(); private Scalar contourRectColor = new Scalar(255, 0, 0); - private long TimeStamp = 0; + private long timeStamp = 0; public VisionProcess(Camera processCam) { camera = processCam; @@ -53,8 +52,8 @@ public class VisionProcess implements Runnable { ntDistanceEntry = ntTable.getEntry("distance"); ntTimeStampEntry = ntTable.getEntry("timestamp"); ntValidEntry = ntTable.getEntry("is_valid"); - ntDriverModeEntry.addListener(this::DriverModeListener, EntryListenerFlags.kUpdate); - ntPipelineEntry.addListener(this::PipelineListener, EntryListenerFlags.kUpdate); + ntDriverModeEntry.addListener(this::driverModeListener, EntryListenerFlags.kUpdate); + ntPipelineEntry.addListener(this::pipelineListener, EntryListenerFlags.kUpdate); ntDriverModeEntry.setBoolean(false); ntPipelineEntry.setNumber(camera.getCurrentPipelineIndex()); @@ -63,7 +62,7 @@ public class VisionProcess implements Runnable { cameraProcess = new CameraProcess(camera); } - private void DriverModeListener(EntryNotification entryNotification) { + private void driverModeListener(EntryNotification entryNotification) { if (entryNotification.value.getBoolean()) { camera.setExposure(25); camera.setBrightness(15); @@ -74,7 +73,7 @@ public class VisionProcess implements Runnable { } } - private void PipelineListener(EntryNotification entryNotification) { + private void pipelineListener(EntryNotification entryNotification) { var ntPipelineIndex = (int) entryNotification.value.getDouble(); if (camera.getPipelines().containsKey(ntPipelineIndex)) { // camera.setEntryNotification.value.getString()); @@ -117,7 +116,7 @@ public class VisionProcess implements Runnable { ntDistanceEntry.setNumber(pipelineResult.Area); NetworkTableInstance.getDefault().flush(); } - ntTimeStampEntry.setNumber(TimeStamp); + ntTimeStampEntry.setNumber(timeStamp); } private PipelineResult runVisionProcess(Mat inputImage, Mat outputImage) { @@ -136,20 +135,20 @@ public class VisionProcess implements Runnable { Scalar hsvLower = new Scalar(currentPipeline.hue.get(0), currentPipeline.saturation.get(0), currentPipeline.value.get(0)); Scalar hsvUpper = new Scalar(currentPipeline.hue.get(1), currentPipeline.saturation.get(1), currentPipeline.value.get(1)); - cvProcess.HSVThreshold(inputImage, hsvThreshMat, hsvLower, hsvUpper, currentPipeline.erode, currentPipeline.dilate); + cvProcess.hsvThreshold(inputImage, hsvThreshMat, hsvLower, hsvUpper, currentPipeline.erode, currentPipeline.dilate); if (currentPipeline.is_binary == 1) { Imgproc.cvtColor(hsvThreshMat, outputImage, Imgproc.COLOR_GRAY2BGR, 3); } else { inputImage.copyTo(outputImage); } - FoundContours = cvProcess.FindContours(hsvThreshMat); - if (FoundContours.size() > 0) { - FilteredContours = cvProcess.FilterContours(FoundContours, currentPipeline.area, currentPipeline.ratio, currentPipeline.extent); - if (FilteredContours.size() > 0) { - GroupedContours = cvProcess.GroupTargets(FilteredContours, currentPipeline.target_intersection, currentPipeline.target_group); - if (GroupedContours.size() > 0) { - var finalRect = cvProcess.SortTargetsToOne(GroupedContours, currentPipeline.sort_mode); + foundContours = cvProcess.findContours(hsvThreshMat); + if (foundContours.size() > 0) { + filteredContours = cvProcess.filterContours(foundContours, currentPipeline.area, currentPipeline.ratio, currentPipeline.extent); + if (filteredContours.size() > 0) { + groupedContours = cvProcess.groupTargets(filteredContours, currentPipeline.target_intersection, currentPipeline.target_group); + if (groupedContours.size() > 0) { + var finalRect = cvProcess.sortTargetsToOne(groupedContours, currentPipeline.sort_mode); // System.out.printf("Largest Contour Area: %.2f\n", finalRect.size.area()); pipelineResult.RawPoint = finalRect; pipelineResult.IsValid = true; @@ -188,9 +187,9 @@ public class VisionProcess implements Runnable { while (!Thread.interrupted()) { startTime = System.nanoTime(); if ((startTime - lastFrameEndNanosec) * 1e-6 >= 1000.0 / maxFps + 3) { // 3 additional fps to allow for overhead - FoundContours.clear(); - FilteredContours.clear(); - GroupedContours.clear(); + foundContours.clear(); + filteredContours.clear(); + groupedContours.clear(); // update FPS for ui only every 0.5 seconds if ((startTime - fpsLastTime) * 1e-6 >= 500) { @@ -204,7 +203,7 @@ public class VisionProcess implements Runnable { currentPipeline = camera.getCurrentPipeline(); // start fps counter right before grabbing input frame - TimeStamp = cameraProcess.getLatestFrame(cameraInputMat); + timeStamp = cameraProcess.getLatestFrame(cameraInputMat); if (cameraInputMat.cols() == 0 && cameraInputMat.rows() == 0) { continue; }