Exploit databaseDownload repository ZIP
GH-920d303960990c99657f0beda79b6404
0xcub3/12377-into-the-deep-seattlesolvers
Sources for CVE-2019-11358
1,181 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files3
0xcub3/12377-into-the-deep-seattlesolversRevision a94ecf0e02a7
- ↑..Parent directory
- SampleDetectionDemoTeleop.java
- SampleDetectionPipeline.java
- SampleDetectionPipelinePNP.java
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/commandbase/vision/SampleDetectionPipelinePNP.java
Java · 16,198 bytespackage org.firstinspires.ftc.teamcode.commandbase.vision;
import com.acmerobotics.dashboard.config.Config;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDouble;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.MatOfPoint3f;
import org.opencv.core.Point;
import org.opencv.core.Point3;
import org.opencv.core.RotatedRect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.openftc.easyopencv.OpenCvPipeline;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Config
public class SampleDetectionPipelinePNP extends OpenCvPipeline {
/*
* Our working image buffers
*/
Mat ycrcbMat = new Mat();
Mat crMat = new Mat();
Mat cbMat = new Mat();
Mat blueThresholdMat = new Mat();
Mat redThresholdMat = new Mat();
Mat yellowThresholdMat = new Mat();
Mat morphedBlueThreshold = new Mat();
Mat morphedRedThreshold = new Mat();
Mat morphedYellowThreshold = new Mat();
Mat contoursOnPlainImageMat = new Mat();
static List<String> pointsList = new ArrayList<>();
static List<String> samplesList = new ArrayList<>();
// Assuming the object is a rectangle with known dimensions
public static double objectWidth = 8.5; // Replace with your object's width in real-world units (e.g., centimeters)
public static double objectHeight = 3.0; // Replace with your object's height in real-world units
/*
* Threshold values
*/
static final int YELLOW_MASK_THRESHOLD = 57;
static final int BLUE_MASK_THRESHOLD = 150;
static final int RED_MASK_THRESHOLD = 198;
/*
* The elements we use for noise reduction
*/
Mat erodeElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3.5, 3.5));
Mat dilateElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3.5, 3.5));
/*
* Colors
*/
static final Scalar RED = new Scalar(255, 0, 0);
static final Scalar BLUE = new Scalar(0, 0, 255);
static final Scalar YELLOW = new Scalar(255, 255, 0);
static final int CONTOUR_LINE_THICKNESS = 2;
static class AnalyzedStone {
double angle;
String color;
Mat rvec;
Mat tvec;
}
ArrayList<AnalyzedStone> internalStoneList = new ArrayList<>();
volatile ArrayList<AnalyzedStone> clientStoneList = new ArrayList<>();
/*
* Camera Calibration Parameters
*/
Mat cameraMatrix = new Mat(3, 3, CvType.CV_64FC1);
MatOfDouble distCoeffs = new MatOfDouble();
/*
* Some stuff to handle returning our various buffers
*/
enum Stage {
FINAL,
YCrCb,
MASKS,
MASKS_NR,
CONTOURS;
}
Stage[] stages = Stage.values();
// Keep track of what stage the viewport is showing
int stageNum = 0;
public SampleDetectionPipelinePNP() {
// Initialize camera parameters
// Replace these values with your actual camera calibration parameters
// Focal lengths (fx, fy) and principal point (cx, cy)
double fx = 800; // Replace with your camera's focal length in pixels
double fy = 800;
double cx = 320; // Replace with your camera's principal point x-coordinate (usually image width / 2)
double cy = 240; // Replace with your camera's principal point y-coordinate (usually image height / 2)
cameraMatrix.put(0, 0,
fx, 0, cx,
0, fy, cy,
0, 0, 1);
// Distortion coefficients (k1, k2, p1, p2, k3)
// If you have calibrated your camera and have these values, use them
// Otherwise, you can assume zero distortion for simplicity
distCoeffs = new MatOfDouble(0, 0, 0, 0, 0);
}
@Override
public void onViewportTapped() {
int nextStageNum = stageNum + 1;
if(nextStageNum >= stages.length)
{
nextStageNum = 0;
}
stageNum = nextStageNum;
}
@Override
public Mat processFrame(Mat input) {
// We'll be updating this with new data below
internalStoneList.clear();
/*
* Run the image processing
*/
findContours(input);
clientStoneList = new ArrayList<>(internalStoneList);
/*
* Decide which buffer to send to the viewport
*/
switch (stages[stageNum])
{
case YCrCb:
{
return ycrcbMat;
}
case FINAL:
{
return input;
}
case MASKS:
{
Mat masks = new Mat();
Core.addWeighted(yellowThresholdMat, 1.0, redThresholdMat, 1.0, 0.0, masks);
Core.addWeighted(masks, 1.0, blueThresholdMat, 1.0, 0.0, masks);
return masks;
}
case MASKS_NR:
{
Mat masksNR = new Mat();
Core.addWeighted(morphedYellowThreshold, 1.0, morphedRedThreshold, 1.0, 0.0, masksNR);
Core.addWeighted(masksNR, 1.0, morphedBlueThreshold, 1.0, 0.0, masksNR);
return masksNR;
}
case CONTOURS:
{
return contoursOnPlainImageMat;
}
}
return input;
}
public ArrayList<AnalyzedStone> getDetectedStones() {
return clientStoneList;
}
void findContours(Mat input) {
// Convert the input image to YCrCb color space
Imgproc.cvtColor(input, ycrcbMat, Imgproc.COLOR_RGB2YCrCb);
// Extract the Cb channel for blue detection
Core.extractChannel(ycrcbMat, cbMat, 2); // Cb channel index is 2
// Extract the Cr channel for red detection
Core.extractChannel(ycrcbMat, crMat, 1); // Cr channel index is 1
// Threshold the Cb channel to form a mask for blue
Imgproc.threshold(cbMat, blueThresholdMat, BLUE_MASK_THRESHOLD, 255, Imgproc.THRESH_BINARY);
// Threshold the Cr channel to form a mask for red
Imgproc.threshold(crMat, redThresholdMat, RED_MASK_THRESHOLD, 255, Imgproc.THRESH_BINARY);
// Threshold the Cb channel to form a mask for yellow
Imgproc.threshold(cbMat, yellowThresholdMat, YELLOW_MASK_THRESHOLD, 255, Imgproc.THRESH_BINARY_INV);
// Apply morphology to the masks
morphMask(blueThresholdMat, morphedBlueThreshold);
morphMask(redThresholdMat, morphedRedThreshold);
morphMask(yellowThresholdMat, morphedYellowThreshold);
// Find contours in the masks
ArrayList<MatOfPoint> blueContoursList = new ArrayList<>();
Imgproc.findContours(morphedBlueThreshold, blueContoursList, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
ArrayList<MatOfPoint> redContoursList = new ArrayList<>();
Imgproc.findContours(morphedRedThreshold, redContoursList, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
ArrayList<MatOfPoint> yellowContoursList = new ArrayList<>();
Imgproc.findContours(morphedYellowThreshold, yellowContoursList, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
// Now analyze the contours
for(MatOfPoint contour : blueContoursList) {
analyzeContour(contour, input, "Blue");
}
for(MatOfPoint contour : redContoursList) {
analyzeContour(contour, input, "Red");
}
for(MatOfPoint contour : yellowContoursList) {
analyzeContour(contour, input, "Yellow");
}
}
void morphMask(Mat input, Mat output) {
/*
* Apply some erosion and dilation for noise reduction
*/
Imgproc.erode(input, output, erodeElement);
Imgproc.erode(output, output, erodeElement);
Imgproc.dilate(output, output, dilateElement);
Imgproc.dilate(output, output, dilateElement);
}
void analyzeContour(MatOfPoint contour, Mat input, String color) {
// Transform the contour to a different format
Point[] points = contour.toArray();
MatOfPoint2f contour2f = new MatOfPoint2f(points);
// Do a rect fit to the contour, and draw it on the screen
RotatedRect rotatedRectFitToContour = Imgproc.minAreaRect(contour2f);
drawRotatedRect(rotatedRectFitToContour, input, color);
// The angle OpenCV gives us can be ambiguous, so look at the shape of
// the rectangle to fix that.
double rotRectAngle = rotatedRectFitToContour.angle;
if (rotatedRectFitToContour.size.width < rotatedRectFitToContour.size.height)
{
rotRectAngle += 90;
}
// Compute the angle and store it
double angle = -(rotRectAngle - 180);
int intAngle = (int) Math.round(angle);
drawTagText(rotatedRectFitToContour, ((samplesList.size())) + " " + (intAngle) + " deg", input, color);
// Prepare object points and image points for solvePnP
// Define the 3D coordinates of the object corners in the object coordinate space
MatOfPoint3f objectPoints = new MatOfPoint3f (
new Point3(-objectWidth / 2, -objectHeight / 2, 0),
new Point3(objectWidth / 2, -objectHeight / 2, 0),
new Point3(objectWidth / 2, objectHeight / 2, 0),
new Point3(-objectWidth / 2, objectHeight / 2, 0)
);
// Get the 2D image points from the detected rectangle corners
Point[] rectPoints = new Point[4];
rotatedRectFitToContour.points(rectPoints);
// Order the image points in the same order as object points
Point[] orderedRectPoints = orderPoints(rectPoints);
MatOfPoint2f imagePoints = new MatOfPoint2f(orderedRectPoints);
// Solve PnP
Mat rvec = new Mat();
Mat tvec = new Mat();
boolean success = Calib3d.solvePnP (
objectPoints, // Object points in 3D
imagePoints, // Corresponding image points
cameraMatrix,
distCoeffs,
rvec,
tvec
);
if (success) {
// Draw the coordinate axes on the image
drawAxis(input, rvec, tvec, cameraMatrix, distCoeffs);
// Store the pose information
AnalyzedStone analyzedStone = new AnalyzedStone();
analyzedStone.angle = rotRectAngle;
analyzedStone.color = color;
analyzedStone.rvec = rvec;
analyzedStone.tvec = tvec;
internalStoneList.add(analyzedStone);
}
}
void drawAxis(Mat img, Mat rvec, Mat tvec, Mat cameraMatrix, MatOfDouble distCoeffs) {
// Length of the axis lines
double axisLength = 5.0;
// Define the points in 3D space for the axes
MatOfPoint3f axisPoints = new MatOfPoint3f(
new Point3(0, 0, 0),
new Point3(axisLength, 0, 0),
new Point3(0, axisLength, 0),
new Point3(0, 0, -axisLength) // Z axis pointing away from the camera
);
// Project the 3D points to 2D image points
MatOfPoint2f imagePoints = new MatOfPoint2f();
Calib3d.projectPoints(axisPoints, rvec, tvec, cameraMatrix, distCoeffs, imagePoints);
Point[] imgPts = imagePoints.toArray();
// Draw the axis lines
Imgproc.line(img, imgPts[0], imgPts[1], new Scalar(0, 0, 255), 2); // X axis in red
Imgproc.line(img, imgPts[0], imgPts[2], new Scalar(0, 255, 0), 2); // Y axis in green
Imgproc.line(img, imgPts[0], imgPts[3], new Scalar(255, 0, 0), 2); // Z axis in blue
}
static Point[] orderPoints(Point[] pts)
{
// Orders the array of 4 points in the order: top-left, top-right, bottom-right, bottom-left
Point[] orderedPts = new Point[4];
// Sum and difference of x and y coordinates
double[] sum = new double[4];
double[] diff = new double[4];
for (int i = 0; i < 4; i++)
{
sum[i] = pts[i].x + pts[i].y;
diff[i] = pts[i].y - pts[i].x;
}
// Top-left point has the smallest sum
int tlIndex = indexOfMin(sum);
orderedPts[0] = pts[tlIndex];
// Bottom-right point has the largest sum
int brIndex = indexOfMax(sum);
orderedPts[2] = pts[brIndex];
// Top-right point has the smallest difference
int trIndex = indexOfMin(diff);
orderedPts[1] = pts[trIndex];
// Bottom-left point has the largest difference
int blIndex = indexOfMax(diff);
orderedPts[3] = pts[blIndex];
return orderedPts;
}
static int indexOfMin(double[] array)
{
int index = 0;
double min = array[0];
for (int i = 1; i < array.length; i++)
{
if (array[i] < min)
{
min = array[i];
index = i;
}
}
return index;
}
static int indexOfMax(double[] array)
{
int index = 0;
double max = array[0];
for (int i = 1; i < array.length; i++)
{
if (array[i] > max)
{
max = array[i];
index = i;
}
}
return index;
}
static void drawTagText(RotatedRect rect, String text, Mat mat, String color) {
Scalar colorScalar = getColorScalar(color);
Imgproc.putText(
mat, // The buffer we're drawing on
(Math.ceil(rect.size.width) + ", " + Math.ceil(rect.size.height)), // The text we're drawing
new Point( // The anchor point for the text
rect.center.x - 50, // x anchor point
rect.center.y + 25), // y anchor point
Imgproc.FONT_HERSHEY_PLAIN, // Font
3, // Font size
colorScalar, // Font color
5); // Font thickness
}
static void drawRotatedRect(RotatedRect rect, Mat drawOn, String color) {
/*
* Draws a rotated rect by drawing each of the 4 lines individually
*/
Point[] points = new Point[4];
rect.points(points);
Scalar colorScalar = getColorScalar(color);
for (int i = 0; i < 4; ++i) {
Imgproc.line(drawOn, points[i], points[(i + 1) % 4], colorScalar, 2);
pointsList = Collections.singletonList(((points[i]).toString() + (points[(i + 1) % 4]).toString()));
}
}
static Scalar getColorScalar(String color)
{
switch (color)
{
case "Blue":
return BLUE;
case "Yellow":
return YELLOW;
default:
return RED;
}
}
public static double calculateDistance(String pointsString) {
try {
pointsString = pointsString.replaceAll("[\\[\\]]", "");
Pattern pattern = Pattern.compile("\\{(\\d+\\.?\\d*),\\s*(\\d+\\.?\\d*)\\}");
Matcher matcher = pattern.matcher(pointsString);
double[] point1 = new double[2];
double[] point2 = new double[2];
if (matcher.find()) {
point1[0] = Double.parseDouble(Objects.requireNonNull(matcher.group(1)));
point1[1] = Double.parseDouble(Objects.requireNonNull(matcher.group(2)));
}
if (matcher.find()) {
point2[0] = Double.parseDouble(Objects.requireNonNull(matcher.group(1)));
point2[1] = Double.parseDouble(Objects.requireNonNull(matcher.group(2)));
}
double dx = point1[0] - point2[0];
double dy = point1[1] - point2[1];
return Math.sqrt(dx * dx + dy * dy);
}
catch (Exception ignored) {
return -1;
}
}
}