use of br.edu.ifspsaocarlos.sdm.kifurecorder.models.Board in project kifu-recorder by leonardost.
the class RecordGameActivity method addMoveToGameRecord.
private void addMoveToGameRecord() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
final EditText input = new EditText(RecordGameActivity.this);
dialog.setTitle(R.string.dialog_add_move).setMessage(getString(R.string.dialog_add_move)).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Move manuallyAddedMove = processManualMove(input.getText().toString());
Board newBoard = game.getLastBoard().generateNewBoardWith(manuallyAddedMove);
if (game.addMoveIfItIsValid(newBoard)) {
newMoveWasAdded();
game.updateNumberOfManualAdditions();
logger.addToLog("Move " + manuallyAddedMove + " was manually added");
}
}
}).setNegativeButton(R.string.no, null).setView(input).show();
}
use of br.edu.ifspsaocarlos.sdm.kifurecorder.models.Board 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();
}
use of br.edu.ifspsaocarlos.sdm.kifurecorder.models.Board in project kifu-recorder by leonardost.
the class RecordGameActivity method onCameraFrame.
/**
* This method is called every time a camera frame is ready to be processed
*/
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
logger.startLoggingFrame();
logger.setCameraFrame(inputFrame.rgba().clone());
cameraFrame = inputFrame.rgba();
Mat originalImage = inputFrame.rgba();
if (isCornerTrackingActive) {
for (int i = 0; i < 4; i++) {
cornerDetector[i].setImageIndex((int) logger.getFrameNumber());
}
updateCornerPositions(originalImage.clone());
}
if (state == STATE_LOOKING_FOR_BOARD) {
logger.addToLog("Board is not inside contour");
logger.addToLog("");
if (orthogonalBoard != null) {
orthogonalBoard.copyTo(originalImage.rowRange(0, 500).colRange(0, 500));
}
Drawer.drawLostBoardContour(originalImage, boardContour);
Drawer.drawBoard(originalImage, game.getLastBoard(), 0, 500, 400, game.getLastMove());
logger.log();
return originalImage;
}
// Throttling, processes twice per second
if (System.currentTimeMillis() - timeOfLastImageProcessing < 500) {
if (orthogonalBoard != null) {
orthogonalBoard.copyTo(originalImage.rowRange(0, 500).colRange(0, 500));
}
Drawer.drawBoardContour(originalImage, boardContour);
Drawer.drawBoard(originalImage, game.getLastBoard(), 0, 500, 400, game.getLastMove());
logger.log();
return originalImage;
} else
timeOfLastImageProcessing = System.currentTimeMillis();
orthogonalBoard = ImageUtils.transformOrthogonally(originalImage, boardPositionInImage);
logger.setOrtogonalBoardImage(orthogonalBoard.clone());
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: Check the size of the orthogonal board image here
// This affects the radius size when calculating the average color around an intersection
// int imageWidth = (int)orthogonalBoard.size().width;
// int imageHeight = (int)orthogonalBoard.size().height;
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
stoneDetector.setBoardImage(orthogonalBoard);
Board board = stoneDetector.detect(game.getLastBoard(), game.canNextMoveBe(Board.BLACK_STONE), game.canNextMoveBe(Board.WHITE_STONE));
// snapshotAtual = stoneDetector.snapshot.toString();
logger.logCurrentBoardState();
if (!paused) {
if (lastDetectedBoard.equals(board)) {
timeSinceLastBoardChange += SystemClock.elapsedRealtime() - timeOfLastBoardDetection;
timeOfLastBoardDetection = SystemClock.elapsedRealtime();
if (timeSinceLastBoardChange > timeLimit && game.addMoveIfItIsValid(board)) {
newMoveWasAdded();
}
} else {
timeSinceLastBoardChange = 0;
timeOfLastBoardDetection = SystemClock.elapsedRealtime();
}
}
lastDetectedBoard = board;
Drawer.drawBoardContour(originalImage, boardContour);
logger.setCameraImageWithBoardContour(originalImage.clone());
// Draw the orthogonal board on the screen
if (orthogonalBoard != null) {
orthogonalBoard.copyTo(originalImage.rowRange(0, 500).colRange(0, 500));
}
if (paused) {
// When it's paused, draws stone detector's current output (useful for debugging)
Drawer.drawBoard(originalImage, board, 0, 500, 400, null);
} else {
Drawer.drawBoard(originalImage, game.getLastBoard(), 0, 500, 400, game.getLastMove());
}
logger.log();
return originalImage;
}
use of br.edu.ifspsaocarlos.sdm.kifurecorder.models.Board in project kifu-recorder by leonardost.
the class StoneDetector method detect.
/**
* Stone detection that does not use the previous state of the game.
*/
public Board detect() {
Board board = new Board(boardDimension);
double[] averageColorOfBoard = calculateAverageColorOfBoard(boardImage);
for (int i = 0; i < boardDimension; ++i) {
for (int j = 0; j < boardDimension; ++j) {
double[] color = calculateAverageColorOnPosition(i, j);
int hypothesis = calculateColorHypothesis(color, averageColorOfBoard);
if (hypothesis != Board.EMPTY) {
board.putStone(i, j, hypothesis);
}
}
}
return board;
}
Aggregations