Search in sources :

Example 56 with Point

use of org.opencv.core.Point in project kifu-recorder by leonardost.

the class InitialBoardDetector method process.

/**
 * Processes the provided image. Returns true if the complete processing ran with success, i.e.,
 * if a Go board was detected in the image. Returns false otherwise.
 *
 * @return boolean
 */
public boolean process() {
    if (image == null) {
        // throw error
        return false;
    }
    Mat imageWithBordersInEvidence = detectBorders();
    // Se quiser ver a saída do detector de bordas Cammy
    // Imgproc.cvtColor(imageWithBordersInEvidence, previewImage, Imgproc.COLOR_GRAY2BGR, 4); if (true) return false;
    // previewImage = imageWithBordersInEvidence; if (true) return false;
    List<MatOfPoint> contours = detectContours(imageWithBordersInEvidence);
    if (contours.isEmpty()) {
        Log.i("KifuRecorder", "> Image processing: contours were not found.");
        return false;
    }
    // Se quiser ver a saída do detector de contours
    // Imgproc.drawContours(previewImage, contours, -1, new Scalar(0, 0, 255), 2); if (true) return false;
    List<MatOfPoint> quadrilaterals = detectQuadrilaterals(contours);
    if (quadrilaterals.isEmpty()) {
        Log.i("KifuRecorder", "> Image processing: quadrilaterals were not found.");
        return false;
    }
    // Se quiser ver a saída do detector de quadriláteros
    // for (MatOfPoint quadrilatero : quadrilaterals) { List<MatOfPoint> listaContorno = new ArrayList<MatOfPoint>(); listaContorno.add(quadrilatero); Imgproc.drawContours(previewImage, listaContorno, -1, new Scalar(255, 0, 0), 3); } if (true) return false;
    MatOfPoint boardQuadrilateral = detectBoard(quadrilaterals);
    if (boardQuadrilateral == null) {
        Log.i("KifuRecorder", "> Image processing: board quadrilateral was not found.");
        return false;
    }
    QuadrilateralHierarchy quadrilateralHierarchy = new QuadrilateralHierarchy(quadrilaterals);
    double averageArea = 0;
    for (MatOfPoint quadrilateral : quadrilateralHierarchy.hierarchy.get(boardQuadrilateral)) {
        averageArea += Imgproc.contourArea(quadrilateral);
    }
    averageArea /= quadrilateralHierarchy.hierarchy.get(boardQuadrilateral).size();
    double boardArea = Imgproc.contourArea(boardQuadrilateral);
    double ratio = averageArea / boardArea;
    // internal quadrilaterals and the area of the board quadrilateral
    if (ratio <= 1.0 / 324.0) {
        // 18 quadrados por 18
        boardDimension = 19;
    } else if (ratio <= 1.0 / 144.0) {
        // 12 quadrados por 12
        boardDimension = 13;
    } else {
        boardDimension = 9;
    }
    List<Point> boardCorners = orderCorners(boardQuadrilateral);
    if (shouldDrawPreview) {
        // Drawer.desenhaInterseccoesECantosDoTabuleiro(previewImage, intersecoes, boardCorners);
        Drawer.drawBoardContour(previewImage, boardQuadrilateral);
    }
    positionOfBoardInImage = new Mat(4, 1, CvType.CV_32FC2);
    positionOfBoardInImage.put(0, 0, (int) boardCorners.get(0).x, (int) boardCorners.get(0).y, (int) boardCorners.get(1).x, (int) boardCorners.get(1).y, (int) boardCorners.get(2).x, (int) boardCorners.get(2).y, (int) boardCorners.get(3).x, (int) boardCorners.get(3).y);
    /*
        for (int i = 0; i < positionOfBoardInImage.rows(); ++i) {
            for (int j = 0; j < positionOfBoardInImage.cols(); ++j) {
                double[] valor = positionOfBoardInImage.get(i, j);
                Log.d("KifuRecorder", "(" + i + ", " + j + ") = " + valor[0] + ", " + valor[1]);
            }
        }
        */
    processedWithSuccess = true;
    return true;
}
Also used : Mat(org.opencv.core.Mat) MatOfPoint(org.opencv.core.MatOfPoint) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint)

Aggregations

Point (org.opencv.core.Point)56 MatOfPoint (org.opencv.core.MatOfPoint)30 Mat (org.opencv.core.Mat)23 ArrayList (java.util.ArrayList)11 Scalar (org.opencv.core.Scalar)9 Line (com.disnodeteam.dogecv.math.Line)7 Rect (org.opencv.core.Rect)7 Size (org.opencv.core.Size)7 MatOfPoint2f (org.opencv.core.MatOfPoint2f)4 TimingLogger (android.util.TimingLogger)2 Ponto (br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Ponto)2 KeyPoint (org.opencv.core.KeyPoint)2 MatOfKeyPoint (org.opencv.core.MatOfKeyPoint)2 Pair (android.util.Pair)1 Corner (br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner)1 Rectangle (java.awt.Rectangle)1 List (java.util.List)1 ImageNotFoundException (org.getopentest.exceptions.ImageNotFoundException)1 Core (org.opencv.core.Core)1 MinMaxLocResult (org.opencv.core.Core.MinMaxLocResult)1