Search in sources :

Example 1 with Corner

use of br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner in project kifu-recorder by leonardost.

the class FileHelper method restoreGameStoredTemporarily.

public void restoreGameStoredTemporarily(Game game, Corner[] boardCorners) {
    File arquivo = getTempFile();
    // TODO: Precisa fazer esta checagem aqui? Porque aqui só é feita a leitura de arquivos
    if (isExternalStorageWritable()) {
        try {
            FileInputStream fis = new FileInputStream(arquivo);
            ObjectInputStream ois = new ObjectInputStream(fis);
            game.copy((Game) ois.readObject());
            Corner[] savedCorners = (Corner[]) ois.readObject();
            for (int i = 0; i < 4; i++) {
                boardCorners[i].copy(savedCorners[i]);
            }
            ois.close();
            fis.close();
            Log.i("KifuRecorder", "Partida recuperada.");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("KifuRecorder", "External storage not available to restore temporary game state.");
    }
}
Also used : Corner(br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with Corner

use of br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner in project kifu-recorder by leonardost.

the class RecordGameActivity method updateCornerPositions.

private void updateCornerPositions(Mat image) {
    Imgproc.cvtColor(image, image, Imgproc.COLOR_RGBA2BGR);
    Corner[] possibleNewCorners = new Corner[4];
    boolean wereAllCornersFound = true;
    for (int i = 0; i < 4; i++) {
        long start = System.currentTimeMillis();
        possibleNewCorners[i] = cornerDetector[i].detectCornerIn(image);
        long duration = System.currentTimeMillis() - start;
        if (possibleNewCorners[i] != null) {
            logger.addToLog(possibleNewCorners[i].toString());
        } else {
            logger.addToLog("Corner " + i + " was not found");
        }
        logger.addToLog("Time to process corner " + i + " = " + duration + "ms");
        if (possibleNewCorners[i] == null) {
            wereAllCornersFound = false;
        }
    }
    Mat orthogonalBoardImage = wereAllCornersFound ? ImageUtils.generateOrthogonalBoardImage(image, possibleNewCorners) : null;
    if (wereAllCornersFound && boardDetector.isBoardContainedIn(orthogonalBoardImage)) {
        logger.addToLog("Board is inside contour");
        int numberOfCornersThatMoved = getNumberOfCornersThatMoved(possibleNewCorners, boardCorners);
        logger.addToLog("Number of corners that moved: " + numberOfCornersThatMoved);
        int numberOfEmptyCornersThatMoved = getNumberOfEmptyCornersThatMoved(possibleNewCorners, boardCorners);
        logger.addToLog("Number of empty corners that moved: " + numberOfEmptyCornersThatMoved);
        double[] distanceToNewPoint = new double[4];
        for (int i = 0; i < 4; i++) {
            distanceToNewPoint[i] = possibleNewCorners[i].distanceTo(boardCorners[i]);
            logger.addToLog("Distance to old corner point " + (i + 1) + " = " + distanceToNewPoint[i]);
        }
        for (int i = 0; i < 4; i++) {
            if (numberOfCornersThatMoved < 4) {
                // Update relative corner position of possible corners with stones
                if (possibleNewCorners[i].isStone) {
                    if (!boardCorners[i].isStone) {
                        possibleNewCorners[i].updateDisplacementVectorRelativeTo(boardCorners[i].position);
                    } else {
                        possibleNewCorners[i].updateDisplacementVectorRelativeTo(boardCorners[i].getRealCornerPosition());
                    }
                }
            } else if (possibleNewCorners[i].isStone) {
                // All corners moved together, so this is probably a board displacement and we
                // don't update the corners's relative position to the real corners
                possibleNewCorners[i].displacementToRealCorner = boardCorners[i].displacementToRealCorner;
            }
        }
        Mat orthogonalBoardImage2 = ImageUtils.generateOrthogonalBoardImage(image, possibleNewCorners);
        double similarity = lastValidOrtogonalBoardImage != null ? fingerprintMatching.calculateSimilatiryBetween(lastValidOrtogonalBoardImage, orthogonalBoardImage2) : -1;
        logger.addToLog("Similarity between new orthogonal board image to last valid one = " + similarity);
        logger.setOrtogonalBoardImage2(orthogonalBoardImage2);
        if (logger.getFrameNumber() <= 3 || numberOfFramesWithDissimilarOrtogonalImages >= 5 || fingerprintMatching.areImagesSimilar(lastValidOrtogonalBoardImage, orthogonalBoardImage2)) {
            // This condition should be time based and not frame based
            if (numberOfFramesWithDissimilarOrtogonalImages >= 5) {
                logger.addToLog("Forcing orthogonal image to be similar");
            } else {
                logger.addToLog("New orthogonal board image is similar to last valid one");
            }
            for (int i = 0; i < 4; i++) {
                if (!possibleNewCorners[i].isStone) {
                    numberOfFramesWithoutStone[i]++;
                } else {
                    numberOfFramesWithoutStone[i] = 0;
                }
                if (!boardCorners[i].isStone && !possibleNewCorners[i].isStone && numberOfCornersThatMoved < 3 && numberOfEmptyCornersThatMoved == 1) {
                    // This means a single empty corner moved by itself, which is not possible. This addresses a wrong
                    // corner detection in frame 70 of sequence 16.
                    logger.addToLog("Corner " + i + " - This empty corner moved by itself");
                    continue;
                }
                if (!possibleNewCorners[i].isStone && boardCorners[i].isStone && possibleNewCorners[i].distanceTo(boardCorners[i].getRealCornerPosition()) > MOVEMENT_THRESHOLD && // This condition should be time based instead of frame based, something like 2 or 3 seconds or so
                numberOfFramesWithoutStone[i] < 5) {
                    // If a corner was a stone and is not anymore, the new empty corner should match the real corner
                    // position that the stone was on. This addresses a wrong corner detection in frame 74 of sequence 14.
                    logger.addToLog("Corner " + i + " - This now empty corner is in a wrong position");
                    logger.addToLog("Number of frames without stone = " + numberOfFramesWithoutStone[i]);
                    continue;
                }
                boardCorners[i] = possibleNewCorners[i];
                cornerDetector[i].setCorner(possibleNewCorners[i]);
            }
            numberOfFramesWithDissimilarOrtogonalImages = 0;
            lastValidOrtogonalBoardImage = orthogonalBoardImage2.clone();
            logger.setLastValidOrtogonalBoardImage(lastValidOrtogonalBoardImage);
        } else {
            logger.addToLog("New orthogonal board image is NOT similar to last valid one");
            numberOfFramesWithDissimilarOrtogonalImages++;
        }
        processBoardCorners();
        state = STATE_RUNNING;
    } else {
        state = STATE_LOOKING_FOR_BOARD;
        logger.addToLog("Board is NOT inside contour");
        logger.addToLog("were all corners found = " + wereAllCornersFound);
    }
    logger.logCornerPositions(boardCorners);
}
Also used : Mat(org.opencv.core.Mat) Corner(br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint)

Example 3 with Corner

use of br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner in project kifu-recorder by leonardost.

the class RecordGameActivity method initializeProcessing.

private void initializeProcessing() {
    Intent i = getIntent();
    String blackPlayer = i.getStringExtra("blackPlayer");
    String whitePlayer = i.getStringExtra("whitePlayer");
    String komi = i.getStringExtra("komi");
    boardDimension = i.getIntExtra("boardDimension", -1);
    int[] foundBoardCorners = i.getIntArrayExtra("boardPositionInImage");
    stoneDetector.setBoardDimension(boardDimension);
    game = new Game(boardDimension, blackPlayer, whitePlayer, komi);
    lastDetectedBoard = new Board(boardDimension);
    timeOfLastBoardDetection = SystemClock.elapsedRealtime();
    timeSinceLastBoardChange = 0;
    timeOfLastImageProcessing = SystemClock.elapsedRealtime();
    originalBoardCorners = new Corner[4];
    originalBoardCorners[0] = new Corner(foundBoardCorners[0], foundBoardCorners[1]);
    originalBoardCorners[1] = new Corner(foundBoardCorners[2], foundBoardCorners[3]);
    originalBoardCorners[2] = new Corner(foundBoardCorners[4], foundBoardCorners[5]);
    originalBoardCorners[3] = new Corner(foundBoardCorners[6], foundBoardCorners[7]);
    boardCorners = new Corner[4];
    boardCorners[0] = new Corner(foundBoardCorners[0], foundBoardCorners[1]);
    boardCorners[1] = new Corner(foundBoardCorners[2], foundBoardCorners[3]);
    boardCorners[2] = new Corner(foundBoardCorners[4], foundBoardCorners[5]);
    boardCorners[3] = new Corner(foundBoardCorners[6], foundBoardCorners[7]);
    cornerDetector = new CornerDetector[4];
    for (int cornerIndex = 0; cornerIndex < 4; cornerIndex++) {
        cornerDetector[cornerIndex] = new CornerDetector();
        cornerDetector[cornerIndex].setCorner(boardCorners[cornerIndex]);
        cornerDetector[cornerIndex].setCornerIndex(cornerIndex + 1);
    }
    processBoardCorners();
}
Also used : Game(br.edu.ifspsaocarlos.sdm.kifurecorder.models.Game) Board(br.edu.ifspsaocarlos.sdm.kifurecorder.models.Board) Corner(br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner) Intent(android.content.Intent) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint) CornerDetector(br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.CornerDetector)

Example 4 with Corner

use of br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner in project kifu-recorder by leonardost.

the class RecordGameActivity method rotate.

private void rotate(int direction) {
    logger.addToLog("Rotated board in direction " + direction);
    Corner[] rotatedBoardCorners = new Corner[4];
    for (int i = 0; i < 4; i++) {
        rotatedBoardCorners[i] = new Corner();
    }
    // Counter-clockwise
    if (direction == -1) {
        rotatedBoardCorners[0] = boardCorners[1];
        rotatedBoardCorners[1] = boardCorners[2];
        rotatedBoardCorners[2] = boardCorners[3];
        rotatedBoardCorners[3] = boardCorners[0];
    } else // Clockwise
    if (direction == 1) {
        rotatedBoardCorners[0] = boardCorners[3];
        rotatedBoardCorners[1] = boardCorners[0];
        rotatedBoardCorners[2] = boardCorners[1];
        rotatedBoardCorners[3] = boardCorners[2];
    }
    boardCorners = rotatedBoardCorners;
    for (int i = 0; i < 4; i++) {
        cornerDetector[i].setCorner(boardCorners[i]);
    }
    processBoardCorners();
    lastValidOrtogonalBoardImage = ImageUtils.rotateImage(lastValidOrtogonalBoardImage, direction);
    logger.setLastValidOrtogonalBoardImage(lastValidOrtogonalBoardImage);
    game.rotate(direction);
}
Also used : Corner(br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint)

Aggregations

Corner (br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner)4 MatOfPoint (org.opencv.core.MatOfPoint)3 Point (org.opencv.core.Point)3 Intent (android.content.Intent)1 Board (br.edu.ifspsaocarlos.sdm.kifurecorder.models.Board)1 Game (br.edu.ifspsaocarlos.sdm.kifurecorder.models.Game)1 CornerDetector (br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.CornerDetector)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 Mat (org.opencv.core.Mat)1