Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.disnodeteam.dogecv.detectors.skystone;

import com.disnodeteam.dogecv.DogeCV;
import com.disnodeteam.dogecv.detectors.DogeCVDetector;
import com.disnodeteam.dogecv.filters.DogeCVColorFilter;
import com.disnodeteam.dogecv.filters.LeviColorFilter;
import com.disnodeteam.dogecv.scoring.MaxAreaScorer;
import com.disnodeteam.dogecv.scoring.PerfectAreaScorer;
import com.disnodeteam.dogecv.scoring.RatioScorer;

import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.List;

public class RedFoundationDetector extends DogeCVDetector {
public DogeCV.AreaScoringMethod areaScoringMethod = DogeCV.AreaScoringMethod.MAX_AREA; // Setting to decide to use MaxAreaScorer or PerfectAreaScorer

//Create the default filters and scorers
public MaxAreaScorer maxAreaScorer = new MaxAreaScorer(6); // Used to find largest objects
public DogeCVColorFilter redFilter = new LeviColorFilter(LeviColorFilter.ColorPreset.RED, 164);
public DogeCVColorFilter blueFilter = new LeviColorFilter(LeviColorFilter.ColorPreset.BLUE, 145);

public boolean found = false;
private Point screenPosition = new Point();
private Rect foundRect = new Rect(); // Found rect

private Mat rawImage = new Mat();
private Mat workingMat = new Mat();
private Mat displayMat = new Mat();
private Mat mask = new Mat();
private Mat hierarchy = new Mat();

private Alliance alliance = Alliance.RED;

//which color foundation should be detected?
public enum Alliance{
RED,
BLUE
}

public FoundationDetector(Alliance alliance) {
detectorName = "Foundation Detector";
this.alliance = alliance;
addScorer(maxAreaScorer);
}

@Override
public Mat process(Mat input) {
input.copyTo(rawImage);
input.copyTo(workingMat);
input.copyTo(displayMat);
input.copyTo(mask);

// Imgproc.GaussianBlur(workingMat,workingMat,new Size(5,5),0);
switch(alliance){
case RED:
redFilter.process(workingMat.clone(), mask);
break;
case BLUE:
blueFilter.process(workingMat.clone(), mask);
break;
}

List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(mask, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.drawContours(displayMat,contours,-1,new Scalar(230,70,70),2);

// Current result
Rect bestRect = null;
double bestDifference = Double.MAX_VALUE; // MAX_VALUE since less difference = better

// Loop through the contours and score them, searching for the best result
for(MatOfPoint cont : contours){
double score = calculateScore(cont); // Get the difference score using the scoring API

// Get bounding rect of contour
Rect rect = Imgproc.boundingRect(cont);
Imgproc.rectangle(displayMat, rect.tl(), rect.br(), new Scalar(0,0,255),2); // Draw rect

// If the result is better then the previously tracked one, set this rect as the new best
if(score < bestDifference){
bestDifference = score;
bestRect = rect;
}
}

if(bestRect != null) {
// Show chosen result
Imgproc.rectangle(displayMat, bestRect.tl(), bestRect.br(), new Scalar(255,0,0),4);
Imgproc.putText(displayMat, "Chosen", bestRect.tl(),0,1,new Scalar(255,255,255));

screenPosition = new Point(bestRect.x, bestRect.y);
foundRect = bestRect;
found = true;
}
else {
found = false;
}

switch (stageToRenderToViewport) {
case THRESHOLD: {
Imgproc.cvtColor(mask, mask, Imgproc.COLOR_GRAY2BGR);

return mask;
}
case RAW_IMAGE: {
return rawImage;
}
default: {
return displayMat;
}
}
}
}