-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Pattonville-Robotics/opencv-features
Opencv features
- Loading branch information
Showing
6 changed files
with
420 additions
and
35 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
library/src/main/java/org/pattonvillerobotics/commoncode/opmodes/OpenCVTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.pattonvillerobotics.commoncode.opmodes; | ||
|
||
import com.qualcomm.robotcore.eventloop.opmode.Autonomous; | ||
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; | ||
|
||
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder; | ||
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer; | ||
import org.pattonvillerobotics.commoncode.robotclasses.opencv.ImageProcessor; | ||
import org.pattonvillerobotics.commoncode.robotclasses.opencv.JewelColorDetector; | ||
import org.pattonvillerobotics.commoncode.robotclasses.opencv.util.PhoneOrientation; | ||
import org.pattonvillerobotics.commoncode.robotclasses.vuforia.VuforiaNavigation; | ||
import org.pattonvillerobotics.commoncode.robotclasses.vuforia.VuforiaParameters; | ||
|
||
/** | ||
* Created by greg on 10/7/2017. | ||
*/ | ||
|
||
@Autonomous(name = "OpenCV Test", group = OpModeGroups.TESTING) | ||
public class OpenCVTest extends LinearOpMode { | ||
|
||
private JewelColorDetector jewelColorDetector; | ||
private VuforiaNavigation vuforia; | ||
|
||
private VuforiaParameters VUFORIA_PARAMETERS = new VuforiaParameters.Builder() | ||
.phoneLocation(0, 0, 0, AxesOrder.XYZ, 90, -90, 0) | ||
.cameraDirection(VuforiaLocalizer.CameraDirection.BACK) | ||
.licenseKey("AclLpHb/////AAAAGa41kVT84EtWtYJZW0bIHf9DHg5EHVYWCqExQMx6bbuBtjFeYdvzZLExJiXnT31qDi3WI3QQnOXH8pLZ4cmb39d1w0Oi7aCwy35ODjMvG5qX+e2+3v0l3r1hPpM8P7KPTkRPIl+CGYEBvoNkVbGGjalCW7N9eFDV/T5CN/RQvZjonX/uBPKkEd8ciqK8vWgfy9aPEipAoyr997DDagnMQJ0ajpwKn/SAfaVPA4osBZ5euFf07/3IUnpLEMdMKfoIH6QYLVgwbPuVtUiJWM6flzWaAw5IIhy0XXWwI0nGXrzVjPwZlN3El4Su73ADK36qqOax/pNxD4oYBrlpfYiaFaX0Q+BNro09weXQEoz/Mfgm") | ||
.build(); | ||
|
||
@Override | ||
public void runOpMode() throws InterruptedException { | ||
ImageProcessor.initOpenCV(hardwareMap, this); | ||
|
||
jewelColorDetector = new JewelColorDetector(PhoneOrientation.PORTRAIT); | ||
vuforia = new VuforiaNavigation(VUFORIA_PARAMETERS); | ||
|
||
JewelColorDetector.Analysis analysis; | ||
|
||
waitForStart(); | ||
|
||
while (opModeIsActive()) { | ||
jewelColorDetector.process(vuforia.getImage()); | ||
analysis = jewelColorDetector.getAnalysis(); | ||
|
||
telemetry.addData("Left: ", analysis.leftJewelColor); | ||
telemetry.addData("Right: ", analysis.rightJewelColor); | ||
telemetry.update(); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...c/main/java/org/pattonvillerobotics/commoncode/robotclasses/opencv/ColorBlobDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.pattonvillerobotics.commoncode.robotclasses.opencv; | ||
|
||
import org.opencv.core.Core; | ||
import org.opencv.core.Mat; | ||
import org.opencv.core.MatOfPoint; | ||
import org.opencv.core.Scalar; | ||
import org.opencv.core.Size; | ||
import org.opencv.imgproc.Imgproc; | ||
import org.pattonvillerobotics.commoncode.enums.ColorSensorColor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ColorBlobDetector { | ||
|
||
private final List<MatOfPoint> contours = new ArrayList<>(); | ||
private final Mat blurMat = new Mat(); | ||
private final Mat thresholdMat = new Mat(); | ||
private final Mat hierarchyMat = new Mat(); | ||
private final Mat hsvMat = new Mat(); | ||
private Scalar lowerBoundHSV; | ||
private Scalar upperBoundHSV; | ||
|
||
public ColorBlobDetector(Scalar lowerBoundHSV, Scalar upperBoundHSV) { | ||
setHSVBounds(lowerBoundHSV, upperBoundHSV); | ||
} | ||
|
||
public ColorBlobDetector(ColorSensorColor color) { | ||
setHSVBounds(color); | ||
} | ||
|
||
public void setHSVBounds(Scalar lower, Scalar upper) { | ||
lowerBoundHSV = lower; | ||
upperBoundHSV = upper; | ||
} | ||
|
||
public void setHSVBounds(ColorSensorColor color) { | ||
switch (color) { | ||
case RED: | ||
setHSVBounds(new Scalar(160, 40, 0), new Scalar(180, 255, 255)); | ||
break; | ||
case BLUE: | ||
setHSVBounds(new Scalar(90, 40, 0), new Scalar(110, 255, 255)); | ||
break; | ||
case GREEN: | ||
setHSVBounds(new Scalar(45, 40, 0), new Scalar(75, 255, 255)); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Must provide RED, BLUE, or GREEN!"); | ||
} | ||
} | ||
|
||
public void process(Mat rgbaMat) { | ||
Imgproc.blur(rgbaMat, blurMat, new Size(10, 10)); | ||
|
||
Imgproc.cvtColor(blurMat, hsvMat, Imgproc.COLOR_RGB2HSV); | ||
Core.inRange(hsvMat, lowerBoundHSV, upperBoundHSV, thresholdMat); | ||
|
||
List<MatOfPoint> tmp = new ArrayList<>(); | ||
|
||
Imgproc.findContours(thresholdMat, tmp, hierarchyMat, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); | ||
|
||
// filters out super small contours | ||
contours.clear(); | ||
for (MatOfPoint contour : tmp) { | ||
if (Imgproc.contourArea(contour) > 500) { | ||
contours.add(contour); | ||
} | ||
} | ||
} | ||
|
||
public Mat getThresholdMat() { | ||
return thresholdMat; | ||
} | ||
|
||
public List<MatOfPoint> getContours() { | ||
return contours; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.