use of org.opencv.core.MatOfPoint2f in project Relic_Main by TeamOverdrive.
the class GenericDetector method processFrame.
@Override
public Mat processFrame(Mat rgba, Mat gray) {
Size initSize = rgba.size();
newSize = new Size(initSize.width * downScaleFactor, initSize.height * downScaleFactor);
rgba.copyTo(workingMat);
Imgproc.resize(workingMat, workingMat, newSize);
if (rotateMat) {
Mat tempBefore = workingMat.t();
// mRgba.t() is the transpose
Core.flip(tempBefore, workingMat, -1);
tempBefore.release();
}
Mat preConvert = workingMat.clone();
colorFilter.process(preConvert, mask);
if (stretch) {
structure = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, stretchKernal);
Imgproc.morphologyEx(mask, mask, Imgproc.MORPH_CLOSE, structure);
}
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(mask, contours, hiarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.drawContours(workingMat, contours, -1, new Scalar(230, 70, 70), 2);
Rect chosenRect = null;
double chosenScore = Integer.MAX_VALUE;
MatOfPoint2f approxCurve = new MatOfPoint2f();
for (MatOfPoint c : contours) {
MatOfPoint2f contour2f = new MatOfPoint2f(c.toArray());
// Processing on mMOP2f1 which is in type MatOfPoint2f
double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02;
Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
// Convert back to MatOfPoint
MatOfPoint points = new MatOfPoint(approxCurve.toArray());
// Get bounding rect of contour
Rect rect = Imgproc.boundingRect(points);
// You can find this by printing the area of each found rect, then looking and finding what u deem to be perfect.
// Run this with the bot, on a balance board, with jewels in their desired location. Since jewels should mostly be
// in the same position, this hack could work nicely.
double area = Imgproc.contourArea(c);
double areaDiffrence = 0;
switch(detectionMode) {
case MAX_AREA:
areaDiffrence = -area * areaWeight;
break;
case PERFECT_AREA:
areaDiffrence = Math.abs(perfectArea - area);
break;
}
// Just declaring vars to make my life eassy
double x = rect.x;
double y = rect.y;
double w = rect.width;
double h = rect.height;
Point centerPoint = new Point(x + (w / 2), y + (h / 2));
// Get the ratio. We use max in case h and w get swapped??? it happens when u account for rotation
double cubeRatio = Math.max(Math.abs(h / w), Math.abs(w / h));
double ratioDiffrence = Math.abs(cubeRatio - perfectRatio);
double finalDiffrence = (ratioDiffrence * ratioWeight) + (areaDiffrence * areaWeight);
// Think of diffrence as score. 0 = perfect
if (finalDiffrence < chosenScore && finalDiffrence < maxDiffrence && area > minArea) {
chosenScore = finalDiffrence;
chosenRect = rect;
}
if (debugContours && area > 100) {
Imgproc.circle(workingMat, centerPoint, 3, new Scalar(0, 255, 255), 3);
Imgproc.putText(workingMat, "Area: " + String.format("%.1f", area), centerPoint, 0, 0.5, new Scalar(0, 255, 255));
}
}
if (chosenRect != null) {
Imgproc.rectangle(workingMat, new Point(chosenRect.x, chosenRect.y), new Point(chosenRect.x + chosenRect.width, chosenRect.y + chosenRect.height), new Scalar(0, 255, 0), 3);
Imgproc.putText(workingMat, "Result: " + String.format("%.2f", chosenScore), new Point(chosenRect.x - 5, chosenRect.y - 10), Core.FONT_HERSHEY_PLAIN, 1.3, new Scalar(0, 255, 0), 2);
Point centerPoint = new Point(chosenRect.x + (chosenRect.width / 2), chosenRect.y + (chosenRect.height / 2));
resultRect = chosenRect;
resultLocation = centerPoint;
resultFound = true;
} else {
resultFound = false;
resultRect = null;
resultLocation = null;
}
Imgproc.resize(workingMat, workingMat, initSize);
preConvert.release();
Imgproc.putText(workingMat, "DogeCV v1.1 Generic: " + newSize.toString() + " - " + speed.toString() + " - " + detectionMode.toString(), new Point(5, 30), 0, 1.2, new Scalar(0, 255, 255), 2);
return workingMat;
}
use of org.opencv.core.MatOfPoint2f in project Relic_Main by TeamOverdrive.
the class Converters method Mat_to_vector_vector_Point2f.
// vector_vector_Point2f
public static void Mat_to_vector_vector_Point2f(Mat m, List<MatOfPoint2f> pts) {
if (pts == null)
throw new java.lang.IllegalArgumentException("Output List can't be null");
if (m == null)
throw new java.lang.IllegalArgumentException("Input Mat can't be null");
List<Mat> mats = new ArrayList<Mat>(m.rows());
Mat_to_vector_Mat(m, mats);
for (Mat mi : mats) {
MatOfPoint2f pt = new MatOfPoint2f(mi);
pts.add(pt);
mi.release();
}
mats.clear();
}
use of org.opencv.core.MatOfPoint2f in project Relic_Main by TeamOverdrive.
the class Converters method vector_vector_Point2f_to_Mat.
// vector_vector_Point2f
public static Mat vector_vector_Point2f_to_Mat(List<MatOfPoint2f> pts, List<Mat> mats) {
Mat res;
int lCount = (pts != null) ? pts.size() : 0;
if (lCount > 0) {
for (MatOfPoint2f vpt : pts) mats.add(vpt);
res = vector_Mat_to_Mat(mats);
} else {
res = new Mat();
}
return res;
}
use of org.opencv.core.MatOfPoint2f in project kifu-recorder by leonardost.
the class DetectorDeTabuleiro method detectarQuadrilateros.
private List<MatOfPoint> detectarQuadrilateros(List<MatOfPoint> contornos) {
List<MatOfPoint> quadrilateros = new ArrayList<>();
for (MatOfPoint contorno : contornos) {
MatOfPoint2f contour2f = new MatOfPoint2f();
MatOfPoint2f approx2f = new MatOfPoint2f();
contorno.convertTo(contour2f, CvType.CV_32FC2);
// * 0.02 e * 0.03 também têm resultados interessantes
// Aparentemente, quanto maior o epsilon, mais curvas que não se encaixam perfeitamente nos contornos
// são consideradas. Contudo, esse parâmetro parece ser bastante sensível.
// Imgproc.approxPolyDP(contour2f, approx2f, Imgproc.arcLength(contour2f, true) * 0.04, true);
// Imgproc.approxPolyDP(contour2f, approx2f, Imgproc.arcLength(contour2f, true) * 0.008, true); // perde muitos quadrados
// Imgproc.approxPolyDP(contour2f, approx2f, Imgproc.arcLength(contour2f, true) * 0.01, true); // borda do tabuleiro encontrada bem, mas quadrados internos sofrem. Talvez seja melhor usar este por detectar melhor o quadrado externo
// Imgproc.approxPolyDP(contour2f, approx2f, Imgproc.arcLength(contour2f, true) * 0.015, true); // melhores resultados até agora
Imgproc.approxPolyDP(contour2f, approx2f, Imgproc.arcLength(contour2f, true) * 0.012, true);
// Imgproc.approxPolyDP(contour2f, approx2f, Imgproc.arcLength(contour2f, true) * 0.008, true);
MatOfPoint approx = new MatOfPoint();
approx2f.convertTo(approx, CvType.CV_32S);
double contourArea = Math.abs(Imgproc.contourArea(approx2f));
// Se tem 4 lados, é convexo e não é muito pequeno, é um quadrado válido
if (approx2f.toList().size() == 4 && contourArea > 400 && Imgproc.isContourConvex(approx)) {
quadrilateros.add(approx);
}
}
Log.d("kifu-recorder", "Número de quadriláteros encontrados: " + quadrilateros.size());
return quadrilateros;
}
use of org.opencv.core.MatOfPoint2f in project kifu-recorder by leonardost.
the class HierarquiaDeQuadrilateros method estaDentro.
/**
* Verifica se um quadrilátero está dentro de outro.
*
* @param quadrilateroExterno
* @param quadrilateroInterno
* @return
*/
private boolean estaDentro(MatOfPoint quadrilateroExterno, MatOfPoint quadrilateroInterno) {
final double ESTA_DENTRO_DO_CONTORNO = 1;
double result;
MatOfPoint2f quadrilateroExterno2f = new MatOfPoint2f();
quadrilateroExterno.convertTo(quadrilateroExterno2f, CvType.CV_32FC2);
for (Point point : quadrilateroInterno.toList()) {
result = Imgproc.pointPolygonTest(quadrilateroExterno2f, point, false);
if (result != ESTA_DENTRO_DO_CONTORNO) {
return false;
}
}
return true;
}
Aggregations